implement combobox and do some ui rework

This commit is contained in:
Eduardo Bart
2012-01-04 08:26:58 -02:00
parent 02ae3ac616
commit b8150d160e
48 changed files with 413 additions and 592 deletions

View File

@@ -7,6 +7,8 @@ Module
onLoad: |
require 'tooltip/tooltip'
require 'messagebox/messagebox'
require 'uibutton'
require 'uilabel'
require 'uicombobox'
require 'uipopupmenu'
return true

View File

@@ -49,9 +49,9 @@ local function onWidgetHoverChange(widget, hovered)
end
end
local function onWidgetStyleApply(widget, style)
if style and style.tooltip then
widget.tooltip = style.tooltip
local function onWidgetStyleApply(widget, styleName, styleNode)
if styleNode.tooltip then
widget.tooltip = styleNode.tooltip
end
end

View File

@@ -0,0 +1,7 @@
UIButton = extends(UIWidget)
function UIButton.create()
local button = UIButton.internalCreate()
button:setFocusable(false)
return button
end

View File

@@ -1 +1,41 @@
UIComboBox = extends(UIWidget)
function UIComboBox.create()
local combobox = UIComboBox.internalCreate()
combobox.options = {}
combobox.currentIndex = -1
return combobox
end
function UIComboBox:setCurrentOption(text)
if not self.options then return end
for i,v in ipairs(self.options) do
if v.text == text and self.currentIndex ~= i then
self.currentIndex = i
self:setText(text)
self:onOptionChange(text, data)
return
end
end
end
function UIComboBox:addOption(text, data)
table.insert(self.options, { text = text, data = data })
local index = #self.options
if index == 1 then self:setCurrentOption(text) end
return index
end
function UIComboBox:onMousePress(mousePos, mouseButton)
local menu = createWidget('PopupMenu', self)
for i,v in ipairs(self.options) do
menu:addOption(v.text, function() self:setCurrentOption(v.text) end)
end
menu:setWidth(self:getWidth())
menu:display({ x = self:getX(), y = self:getY() + self:getHeight() })
return true
end
function UIComboBox:onOptionChange(optionText, optionData)
-- nothing todo
end

View File

@@ -0,0 +1,9 @@
UILabel = extends(UIWidget)
function UILabel.create()
local label = UILabel.internalCreate()
label:setPhantom(true)
label:setFocusable(false)
label:setTextAlign(AlignLeft)
return label
end

View File

@@ -1,7 +1,5 @@
-- extends UIWidget
UIPopupMenu = extends(UIWidget)
-- public functions
function UIPopupMenu.create()
local menu = UIPopupMenu.internalCreate()
local layout = UIVerticalLayout.create(menu)
@@ -10,41 +8,39 @@ function UIPopupMenu.create()
return menu
end
function UIPopupMenu.display(menu, pos)
displayUI(menu, {x = pos.x, y = pos.y})
menu:bindRectToParent()
menu:grabMouse()
menu:grabKeyboard()
return menu
function UIPopupMenu:display(pos)
displayUI(self, {x = pos.x, y = pos.y})
self:bindRectToParent()
self:grabMouse()
self:grabKeyboard()
end
function UIPopupMenu.addOption(menu, optionName, optionCallback)
local optionWidget = createWidget(menu:getStyleName() .. 'Button', menu)
local lastOptionWidget = menu:getLastChild()
function UIPopupMenu:addOption(optionName, optionCallback)
local optionWidget = createWidget(self:getStyleName() .. 'Button', self)
local lastOptionWidget = self:getLastChild()
optionWidget.onClick = function()
optionCallback()
menu:destroy()
self:destroy()
end
optionWidget:setText(optionName)
end
function UIPopupMenu.addSeparator(menu)
local separatorWidget = createWidget(menu:getStyleName() .. 'Separator', menu)
function UIPopupMenu:addSeparator()
createWidget(self:getStyleName() .. 'Separator', self)
end
-- hooked events
function UIPopupMenu.onMousePress(menu, mousePos, mouseButton)
-- clicks outside menu area destroys the menu
if not menu:containsPoint(mousePos) then
menu:destroy()
function UIPopupMenu:onMousePress(mousePos, mouseButton)
-- clicks outside self area destroys the self
if not self:containsPoint(mousePos) then
self:destroy()
return true
end
return false
end
function UIPopupMenu.onKeyPress(menu, keyCode, keyText, keyboardModifiers)
function UIPopupMenu:onKeyPress(keyCode, keyText, keyboardModifiers)
if keyCode == KeyEscape then
menu:destroy()
self:destroy()
return true
end
return false