1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-15 13:14:49 +00:00
opus/sys/apis/ui/components/Menu.lua

61 lines
1.3 KiB
Lua
Raw Normal View History

2019-02-06 04:03:57 +00:00
local class = require('class')
local UI = require('ui')
--[[-- Menu --]]--
UI.Menu = class(UI.Grid)
UI.Menu.defaults = {
UIElement = 'Menu',
disableHeader = true,
columns = { { heading = 'Prompt', key = 'prompt', width = 20 } },
}
function UI.Menu:postInit()
self.values = self.menuItems
self.pageSize = #self.menuItems
end
function UI.Menu:setParent()
UI.Grid.setParent(self)
self.itemWidth = 1
for _,v in pairs(self.values) do
if #v.prompt > self.itemWidth then
self.itemWidth = #v.prompt
end
end
self.columns[1].width = self.itemWidth
if self.centered then
self:center()
else
self.width = self.itemWidth + 2
end
end
function UI.Menu:center()
self.x = (self.width - self.itemWidth + 2) / 2
self.width = self.itemWidth + 2
end
function UI.Menu:eventHandler(event)
if event.type == 'key' then
if event.key == 'enter' then
local selected = self.menuItems[self.index]
self:emit({
type = selected.event or 'menu_select',
selected = selected
})
return true
end
elseif event.type == 'mouse_click' then
if event.y <= #self.menuItems then
UI.Grid.setIndex(self, event.y)
local selected = self.menuItems[self.index]
self:emit({
type = selected.event or 'menu_select',
selected = selected
})
return true
end
end
return UI.Grid.eventHandler(self, event)
end