opus/sys/modules/opus/ui/components/Notification.lua

120 lines
2.4 KiB
Lua
Raw Normal View History

local class = require('opus.class')
local Event = require('opus.event')
local Sound = require('opus.sound')
local UI = require('opus.ui')
local Util = require('opus.util')
2019-02-06 04:03:57 +00:00
UI.Notification = class(UI.Window)
UI.Notification.defaults = {
UIElement = 'Notification',
backgroundColor = 'gray',
2019-03-26 13:08:39 +00:00
closeInd = UI.extChars and '\215' or '*',
2019-02-06 04:03:57 +00:00
height = 3,
timeout = 3,
2019-03-26 06:10:10 +00:00
anchor = 'bottom',
2019-02-06 04:03:57 +00:00
}
function UI.Notification.draw()
2019-02-06 04:03:57 +00:00
end
function UI.Notification.enable()
2019-02-06 04:03:57 +00:00
end
function UI.Notification:error(value, timeout)
self.backgroundColor = 'red'
2019-02-06 04:03:57 +00:00
Sound.play('entity.villager.no', .5)
self:display(value, timeout)
end
function UI.Notification:info(value, timeout)
self.backgroundColor = 'lightGray'
2019-02-06 04:03:57 +00:00
self:display(value, timeout)
end
function UI.Notification:success(value, timeout)
self.backgroundColor = 'green'
2019-02-06 04:03:57 +00:00
self:display(value, timeout)
end
function UI.Notification:cancel()
2019-02-24 11:58:26 +00:00
if self.timer then
Event.off(self.timer)
self.timer = nil
end
self:disable()
2019-02-06 04:03:57 +00:00
end
function UI.Notification:display(value, timeout)
local lines = Util.wordWrap(value, self.width - 3)
self.enabled = true
self.height = #lines
2019-02-06 04:03:57 +00:00
2019-03-26 06:10:10 +00:00
if self.anchor == 'bottom' then
self.y = self.parent.height - self.height + 1
self:addTransition('expandUp', { ticks = self.height })
else
self.y = 1
end
self:reposition(self.x, self.y, self.width, self.height)
self:raise()
2019-02-06 04:03:57 +00:00
self:clear()
for k,v in pairs(lines) do
self:write(2, k, v)
end
self:write(self.width, 1, self.closeInd)
if self.timer then
Event.off(self.timer)
self.timer = nil
end
2019-02-06 04:03:57 +00:00
timeout = timeout or self.timeout
if timeout > 0 then
2019-03-26 06:10:10 +00:00
self.timer = Event.onTimeout(timeout, function()
self:cancel()
self:sync()
end)
2019-03-26 06:10:10 +00:00
else
self:sync()
end
end
function UI.Notification:eventHandler(event)
if event.type == 'mouse_click' then
if event.x == self.width then
self:cancel()
return true
end
end
2019-02-06 04:03:57 +00:00
end
function UI.Notification.example()
return UI.Window {
2019-11-17 05:12:02 +00:00
notify1 = UI.Notification {
anchor = 'top',
},
2019-11-17 05:12:02 +00:00
notify2 = UI.Notification { },
button1 = UI.Button {
x = 2, y = 3,
2019-11-17 05:12:02 +00:00
text = 'example 1',
event = 'test_success',
},
button2 = UI.Button {
x = 2, y = 5,
2019-11-17 05:12:02 +00:00
text = 'example 2',
event = 'test_error',
},
eventHandler = function (self, event)
if event.type == 'test_success' then
2019-11-17 05:12:02 +00:00
self.notify1:success('Example text')
elseif event.type == 'test_error' then
self.notify2:error([[Example text test test
test test test test test
test test test]], 0)
end
end,
}
end