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
|
|
|
|
|
2012-02-09 07:42:07 +01:00
|
|
|
if currentMenu then
|
|
|
|
currentMenu:destroy()
|
|
|
|
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()
|
|
|
|
self:grabKeyboard()
|
2012-02-09 07:42:07 +01:00
|
|
|
currentMenu = self
|
2012-01-02 21:46:40 +01:00
|
|
|
end
|
|
|
|
|
2012-01-17 23:28:55 +01:00
|
|
|
function UIPopupMenu:onGeometryChange()
|
|
|
|
self:bindRectToParent()
|
|
|
|
end
|
|
|
|
|
2012-01-04 11:26:58 +01:00
|
|
|
function UIPopupMenu:addOption(optionName, optionCallback)
|
|
|
|
local optionWidget = createWidget(self:getStyleName() .. 'Button', self)
|
|
|
|
local lastOptionWidget = self:getLastChild()
|
2012-02-08 23:58:27 +01:00
|
|
|
optionWidget.onClick = function(self)
|
2012-01-02 23:09:49 +01:00
|
|
|
optionCallback()
|
2012-02-08 23:58:27 +01:00
|
|
|
self:getParent():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
|
|
|
|
|
2012-01-04 11:26:58 +01:00
|
|
|
function UIPopupMenu:addSeparator()
|
|
|
|
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
|
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} )
|