2012-01-02 21:46:40 +01:00
|
|
|
-- extends UIWidget
|
|
|
|
UIPopupMenu = extends(UIWidget)
|
|
|
|
|
|
|
|
-- public functions
|
|
|
|
function UIPopupMenu.create()
|
|
|
|
local menu = UIPopupMenu.internalCreate()
|
|
|
|
local layout = UIVerticalLayout.create(menu)
|
|
|
|
layout:setFitParent(true)
|
|
|
|
menu:setLayout(layout)
|
2012-01-02 23:09:49 +01:00
|
|
|
menu:setStyle('PopupMenu')
|
2012-01-02 21:46:40 +01:00
|
|
|
return menu
|
|
|
|
end
|
|
|
|
|
2012-01-02 23:09:49 +01:00
|
|
|
function UIPopupMenu.display(menu, pos)
|
|
|
|
UI.display(menu, {x = pos.x, y = pos.y})
|
|
|
|
menu:bindRectToParent()
|
|
|
|
menu:grabMouse()
|
|
|
|
menu:grabKeyboard()
|
2012-01-02 21:46:40 +01:00
|
|
|
return menu
|
|
|
|
end
|
|
|
|
|
2012-01-02 23:09:49 +01:00
|
|
|
function UIPopupMenu.addOption(menu, optionName, optionCallback)
|
|
|
|
local optionWidget = UIButton.create()
|
|
|
|
local lastOptionWidget = menu:getLastChild()
|
|
|
|
optionWidget.onClick = function()
|
|
|
|
optionCallback()
|
|
|
|
menu:destroy()
|
2012-01-02 21:46:40 +01:00
|
|
|
end
|
2012-01-02 23:09:49 +01:00
|
|
|
optionWidget:setText(optionName)
|
|
|
|
optionWidget:setStyle('PopupMenuButton')
|
|
|
|
menu:addChild(optionWidget)
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIPopupMenu.addSeparator(menu)
|
|
|
|
local separatorWidget = UIWidget.create()
|
|
|
|
separatorWidget:setStyle('PopupMenuSeparator')
|
|
|
|
menu:addChild(separator)
|
2012-01-02 21:46:40 +01:00
|
|
|
end
|
|
|
|
|
2012-01-02 23:09:49 +01:00
|
|
|
-- hooked events
|
|
|
|
function UIPopupMenu.onMousePress(menu, mousePos, mouseButton)
|
|
|
|
-- clicks outside menu area destroys the menu
|
|
|
|
if not menu:containsPoint(mousePos) then
|
|
|
|
menu:destroy()
|
2012-01-02 21:46:40 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2012-01-02 23:09:49 +01:00
|
|
|
function UIPopupMenu.onKeyPress(menu, keyCode, keyText, keyboardModifiers)
|
|
|
|
if keyCode == KeyEscape then
|
|
|
|
menu:destroy()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|