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)
|
|
|
|
return menu
|
|
|
|
end
|
|
|
|
|
2012-01-02 23:09:49 +01:00
|
|
|
function UIPopupMenu.display(menu, pos)
|
2012-01-03 01:42:53 +01:00
|
|
|
displayUI(menu, {x = pos.x, y = pos.y})
|
2012-01-02 23:09:49 +01:00
|
|
|
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)
|
2012-01-03 01:42:53 +01:00
|
|
|
local optionWidget = createWidget(menu.buttonStyle, menu)
|
2012-01-02 23:09:49 +01:00
|
|
|
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)
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIPopupMenu.addSeparator(menu)
|
2012-01-03 01:42:53 +01:00
|
|
|
local separatorWidget = createWidget(menu.separatorStyle, 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
|
2012-01-03 01:42:53 +01:00
|
|
|
|
|
|
|
function UIPopupMenu.onStyleApply(menu, style)
|
|
|
|
if style['button-style'] then
|
|
|
|
menu.buttonStyle = style['button-style']
|
|
|
|
end
|
|
|
|
if style['separator-style'] then
|
|
|
|
menu.separatorStyle = style['separator-style']
|
|
|
|
end
|
|
|
|
end
|