tibia-client/modules/core_widgets/uipopupmenu.lua

73 lines
1.8 KiB
Lua
Raw Normal View History

2012-01-02 21:46:40 +01:00
UIPopupMenu = extends(UIWidget)
2012-01-05 02:55:07 +01:00
local displayedMenuList = {}
2012-01-02 21:46:40 +01:00
function UIPopupMenu.create()
local menu = UIPopupMenu.internalCreate()
local layout = UIVerticalLayout.create(menu)
layout:setFitParent(true)
menu:setLayout(layout)
return menu
end
2012-01-05 02:55:07 +01:00
function UIPopupMenu:destroy()
table.removevalue(displayedMenuList, self)
UIWidget.destroy(self)
end
function UIPopupMenu:display(pos)
-- don't display if not options was added
if self:getChildCount() == 0 then
self:destroy()
return
end
displayUI(self, {x = pos.x, y = pos.y})
self:bindRectToParent()
self:grabMouse()
self:grabKeyboard()
2012-01-05 02:55:07 +01:00
table.insert(displayedMenuList, self)
2012-01-02 21:46:40 +01:00
end
function UIPopupMenu:addOption(optionName, optionCallback)
local optionWidget = createWidget(self:getStyleName() .. 'Button', self)
local lastOptionWidget = self:getLastChild()
2012-01-02 23:09:49 +01:00
optionWidget.onClick = function()
optionCallback()
self:destroy()
2012-01-02 21:46:40 +01:00
end
2012-01-02 23:09:49 +01:00
optionWidget:setText(optionName)
2012-01-04 12:24:29 +01:00
local width = optionWidget:getTextSize().width + optionWidget:getMarginLeft() + optionWidget:getMarginRight() + 6
self:setWidth(math.max(self:getWidth(), width))
2012-01-02 23:09:49 +01:00
end
function UIPopupMenu:addSeparator()
createWidget(self:getStyleName() .. 'Separator', self)
2012-01-02 21:46:40 +01:00
end
function UIPopupMenu:onMousePress(mousePos, mouseButton)
-- clicks outside self area destroys the self
if not self:containsPoint(mousePos) then
self:destroy()
2012-01-02 21:46:40 +01:00
return true
end
return false
end
function UIPopupMenu:onKeyPress(keyCode, keyText, keyboardModifiers)
2012-01-02 23:09:49 +01:00
if keyCode == KeyEscape then
self:destroy()
2012-01-02 23:09:49 +01:00
return true
end
return false
end
2012-01-05 02:55:07 +01:00
local function onRootGeometryUpdate()
-- close all menus when the window is resized
for i,menu in ipairs(displayedMenuList) do
menu:destroy()
end
end
2012-01-05 03:42:17 +01:00
connect(rootWidget, { onGeometryChange = onRootGeometryUpdate} )