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

93 lines
1.9 KiB
Lua
Raw Normal View History

2019-02-06 04:03:57 +00:00
local class = require('class')
local Event = require('event')
local Sound = require('sound')
local UI = require('ui')
local Util = require('util')
local colors = _G.colors
UI.Notification = class(UI.Window)
UI.Notification.defaults = {
UIElement = 'Notification',
backgroundColor = colors.gray,
closeInd = '\215',
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()
end
function UI.Notification:enable()
end
function UI.Notification:error(value, timeout)
self.backgroundColor = colors.red
Sound.play('entity.villager.no', .5)
self:display(value, timeout)
end
function UI.Notification:info(value, timeout)
2019-03-26 06:10:10 +00:00
self.backgroundColor = colors.lightGray
2019-02-06 04:03:57 +00:00
self:display(value, timeout)
end
function UI.Notification:success(value, timeout)
self.backgroundColor = colors.green
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
2019-02-06 04:03:57 +00:00
if self.canvas then
self.enabled = false
self.canvas:removeLayer()
self.canvas = nil
end
end
function UI.Notification:display(value, timeout)
2019-03-26 06:10:10 +00:00
self:cancel()
2019-02-06 04:03:57 +00:00
self.enabled = true
local lines = Util.wordWrap(value, self.width - 3)
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.canvas = self:addLayer(self.backgroundColor, self.textColor)
self:addTransition('expandUp', { ticks = self.height })
else
self.canvas = self:addLayer(self.backgroundColor, self.textColor)
self.y = 1
end
2019-02-06 04:03:57 +00:00
self.canvas:setVisible(true)
self:clear()
for k,v in pairs(lines) do
self:write(2, k, v)
end
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:write(self.width, 1, self.closeInd)
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