tibia-client/modules/corelib/ui/uipopupmenu.lua

123 lines
3.0 KiB
Lua
Raw Normal View History

2012-06-26 00:13:30 +02:00
-- @docclass
UIPopupMenu = extends(UIWidget, "UIPopupMenu")
2012-01-02 21:46:40 +01:00
local currentMenu
2012-01-05 02:55:07 +01:00
2012-01-02 21:46:40 +01:00
function UIPopupMenu.create()
local menu = UIPopupMenu.internalCreate()
local layout = UIVerticalLayout.create(menu)
layout:setFitChildren(true)
2012-01-02 21:46:40 +01:00
menu:setLayout(layout)
menu.isGameMenu = false
2012-01-02 21:46:40 +01:00
return menu
end
function UIPopupMenu:display(pos)
-- don't display if not options was added
if self:getChildCount() == 0 then
self:destroy()
return
end
if g_ui.isMouseGrabbed() then
self:destroy()
return
end
if currentMenu then
currentMenu:destroy()
end
if pos == nil then
pos = g_window.getMousePosition()
end
rootWidget:addChild(self)
self:setPosition(pos)
self:grabMouse()
self:focus()
--self:grabKeyboard()
currentMenu = self
2012-01-02 21:46:40 +01:00
end
function UIPopupMenu:onGeometryChange(oldRect, newRect)
local parent = self:getParent()
if not parent then return end
local ymax = parent:getY() + parent:getHeight()
local xmax = parent:getX() + parent:getWidth()
if newRect.y + newRect.height > ymax then
local newy = newRect.y - newRect.height
if newy > 0 and newy + newRect.height < ymax then self:setY(newy) end
end
if newRect.x + newRect.width > xmax then
local newx = newRect.x - newRect.width
if newx > 0 and newx + newRect.width < xmax then self:setX(newx) end
end
2012-01-17 23:28:55 +01:00
self:bindRectToParent()
end
function UIPopupMenu:addOption(optionName, optionCallback, shortcut)
2012-06-26 00:13:30 +02:00
local optionWidget = g_ui.createWidget(self:getStyleName() .. 'Button', self)
2014-07-03 18:15:38 +02:00
optionWidget.onClick = function(widget)
self:destroy()
2013-01-30 21:57:37 +01:00
optionCallback()
2012-01-02 21:46:40 +01:00
end
2012-01-02 23:09:49 +01:00
optionWidget:setText(optionName)
local width = optionWidget:getTextSize().width + optionWidget:getMarginLeft() + optionWidget:getMarginRight() + 15
if shortcut then
local shortcutLabel = g_ui.createWidget(self:getStyleName() .. 'ShortcutLabel', optionWidget)
shortcutLabel:setText(shortcut)
width = width + shortcutLabel:getTextSize().width + shortcutLabel:getMarginLeft() + shortcutLabel:getMarginRight()
end
2012-01-04 12:24:29 +01:00
self:setWidth(math.max(self:getWidth(), width))
2012-01-02 23:09:49 +01:00
end
function UIPopupMenu:addSeparator()
2012-06-26 00:13:30 +02:00
g_ui.createWidget(self:getStyleName() .. 'Separator', self)
2012-01-02 21:46:40 +01:00
end
function UIPopupMenu:setGameMenu(state)
self.isGameMenu = state
end
2012-01-05 19:02:27 +01:00
function UIPopupMenu:onDestroy()
if currentMenu == self then
currentMenu = nil
end
2013-01-30 21:57:37 +01:00
self:ungrabMouse()
2012-01-05 19:02:27 +01:00
end
function UIPopupMenu:onMousePress(mousePos, mouseButton)
2012-01-06 09:48:59 +01:00
-- clicks outside menu area destroys the menu
if not self:containsPoint(mousePos) then
self:destroy()
2012-01-02 21:46:40 +01:00
end
2012-02-07 05:55:20 +01:00
return true
2012-01-02 21:46:40 +01:00
end
function UIPopupMenu:onKeyPress(keyCode, 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
2012-02-03 05:18:54 +01:00
-- close all menus when the window is resized
2012-01-06 09:48:59 +01:00
local function onRootGeometryUpdate()
if currentMenu then
currentMenu:destroy()
2012-01-05 02:55:07 +01:00
end
end
local function onGameEnd()
if currentMenu and currentMenu.isGameMenu then
currentMenu:destroy()
end
end
connect(rootWidget, { onGeometryChange = onRootGeometryUpdate })
connect(g_game, { onGameEnd = onGameEnd } )