1-- @docclass
2UIPopupMenu = extends(UIWidget, "UIPopupMenu")
3
4local currentMenu
5
6function UIPopupMenu.create()
7 local menu = UIPopupMenu.internalCreate()
8 local layout = UIVerticalLayout.create(menu)
9 layout:setFitChildren(true)
10 menu:setLayout(layout)
11 menu.isGameMenu = false
12 return menu
13end
14
15function UIPopupMenu:display(pos)
16 -- don't display if not options was added
17 if self:getChildCount() == 0 then
18 self:destroy()
19 return
20 end
21
22 if g_ui.isMouseGrabbed() then
23 self:destroy()
24 return
25 end
26
27 if currentMenu then
28 currentMenu:destroy()
29 end
30
31 if pos == nil then
32 pos = g_window.getMousePosition()
33 end
34
35 rootWidget:addChild(self)
36 self:setPosition(pos)
37 self:grabMouse()
38 self:focus()
39 --self:grabKeyboard()
40 currentMenu = self
41end
42
43function UIPopupMenu:onGeometryChange(oldRect, newRect)
44 local parent = self:getParent()
45 if not parent then return end
46 local ymax = parent:getY() + parent:getHeight()
47 local xmax = parent:getX() + parent:getWidth()
48 if newRect.y + newRect.height > ymax then
49 local newy = ymax - newRect.height
50 if newy > 0 and newy + newRect.height < ymax then self:setY(newy) end
51 end
52 if newRect.x + newRect.width > xmax then
53 local newx = xmax - newRect.width
54 if newx > 0 and newx + newRect.width < xmax then self:setX(newx) end
55 end
56 self:bindRectToParent()
57end
58
59function UIPopupMenu:addOption(optionName, optionCallback, shortcut)
60 local optionWidget = g_ui.createWidget(self:getStyleName() .. 'Button', self)
61 optionWidget.onClick = function(widget)
62 self:destroy()
63 optionCallback()
64 end
65 optionWidget:setText(optionName)
66 local width = optionWidget:getTextSize().width + optionWidget:getMarginLeft() + optionWidget:getMarginRight() + 15
67
68 if shortcut then
69 local shortcutLabel = g_ui.createWidget(self:getStyleName() .. 'ShortcutLabel', optionWidget)
70 shortcutLabel:setText(shortcut)
71 width = width + shortcutLabel:getTextSize().width + shortcutLabel:getMarginLeft() + shortcutLabel:getMarginRight()
72 end
73
74 self:setWidth(math.max(self:getWidth(), width))
75end
76
77function UIPopupMenu:addSeparator()
78 g_ui.createWidget(self:getStyleName() .. 'Separator', self)
79end
80
81function UIPopupMenu:setGameMenu(state)
82 self.isGameMenu = state
83end
84
85function UIPopupMenu:onDestroy()
86 if currentMenu == self then
87 currentMenu = nil
88 end
89 self:ungrabMouse()
90end
91
92function UIPopupMenu:onMousePress(mousePos, mouseButton)
93 -- clicks outside menu area destroys the menu
94 if not self:containsPoint(mousePos) then
95 self:destroy()
96 end
97 return true
98end
99
100function UIPopupMenu:onKeyPress(keyCode, keyboardModifiers)
101 if keyCode == KeyEscape then
102 self:destroy()
103 return true
104 end
105 return false
106end
107
108-- close all menus when the window is resized
109local function onRootGeometryUpdate()
110 if currentMenu then
111 currentMenu:destroy()
112 end
113end
114
115local function onGameEnd()
116 if currentMenu and currentMenu.isGameMenu then
117 currentMenu:destroy()
118 end
119end
120
121connect(rootWidget, { onGeometryChange = onRootGeometryUpdate })
122connect(g_game, { onGameEnd = onGameEnd } )
123