2012-06-26 00:13:30 +02:00
|
|
|
-- @docclass
|
2012-01-02 21:46:40 +01:00
|
|
|
UIPopupMenu = extends(UIWidget)
|
|
|
|
|
2012-02-09 07:42:07 +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)
|
2012-01-15 22:19:52 +01:00
|
|
|
layout:setFitChildren(true)
|
2012-01-02 21:46:40 +01:00
|
|
|
menu:setLayout(layout)
|
|
|
|
return menu
|
|
|
|
end
|
|
|
|
|
2012-01-04 11:26:58 +01:00
|
|
|
function UIPopupMenu:display(pos)
|
2012-01-04 12:29:59 +01:00
|
|
|
-- don't display if not options was added
|
|
|
|
if self:getChildCount() == 0 then
|
|
|
|
self:destroy()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2013-01-30 21:23:26 +01:00
|
|
|
if g_ui.isMouseGrabbed() then
|
|
|
|
self:destroy()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-02-09 07:42:07 +01:00
|
|
|
if currentMenu then
|
|
|
|
currentMenu:destroy()
|
|
|
|
end
|
|
|
|
|
2013-01-16 17:20:17 +01:00
|
|
|
if pos == nil then
|
|
|
|
pos = g_window.getMousePosition()
|
|
|
|
end
|
|
|
|
|
2012-02-20 03:27:08 +01:00
|
|
|
rootWidget:addChild(self)
|
|
|
|
self:setPosition(pos)
|
2012-01-04 11:26:58 +01:00
|
|
|
self:grabMouse()
|
2013-01-16 17:20:17 +01:00
|
|
|
--self:grabKeyboard()
|
2012-02-09 07:42:07 +01:00
|
|
|
currentMenu = self
|
2012-01-02 21:46:40 +01:00
|
|
|
end
|
|
|
|
|
2013-01-26 19:05:34 +01:00
|
|
|
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
|
|
|
|
|
2013-01-26 19:05:34 +01:00
|
|
|
function UIPopupMenu:addOption(optionName, optionCallback, shortcut)
|
2012-06-26 00:13:30 +02:00
|
|
|
local optionWidget = g_ui.createWidget(self:getStyleName() .. 'Button', self)
|
2012-01-04 11:26:58 +01:00
|
|
|
local lastOptionWidget = self:getLastChild()
|
2012-02-08 23:58:27 +01:00
|
|
|
optionWidget.onClick = function(self)
|
|
|
|
self:getParent():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)
|
2012-12-28 17:15:57 +01:00
|
|
|
local width = optionWidget:getTextSize().width + optionWidget:getMarginLeft() + optionWidget:getMarginRight() + 15
|
2013-01-26 19:05:34 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-01-04 11:26:58 +01:00
|
|
|
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
|
|
|
|
|
2012-01-05 19:02:27 +01:00
|
|
|
function UIPopupMenu:onDestroy()
|
2012-02-09 07:42:07 +01:00
|
|
|
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
|
|
|
|
|
2012-01-04 11:26:58 +01:00
|
|
|
function UIPopupMenu:onMousePress(mousePos, mouseButton)
|
2012-01-06 09:48:59 +01:00
|
|
|
-- clicks outside menu area destroys the menu
|
2012-01-04 11:26:58 +01:00
|
|
|
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
|
|
|
|
|
2012-02-06 13:53:28 +01:00
|
|
|
function UIPopupMenu:onKeyPress(keyCode, keyboardModifiers)
|
2012-01-02 23:09:49 +01:00
|
|
|
if keyCode == KeyEscape then
|
2012-01-04 11:26:58 +01:00
|
|
|
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()
|
2012-02-09 07:42:07 +01:00
|
|
|
if currentMenu then
|
|
|
|
currentMenu:destroy()
|
2012-01-05 02:55:07 +01:00
|
|
|
end
|
|
|
|
end
|
2012-01-05 03:42:17 +01:00
|
|
|
connect(rootWidget, { onGeometryChange = onRootGeometryUpdate} )
|