tibia-client/modules/core_lib/widgets/uicombobox.lua

71 lines
1.9 KiB
Lua
Raw Normal View History

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, v.data)
return
end
end
end
2012-01-19 20:15:38 +01:00
function UIComboBox:setCurrentIndex(index)
if index >= 1 and index <= #self.options then
local v = self.options[index]
self.currentIndex = index
2012-01-19 20:15:38 +01:00
self:setText(v.text)
self:onOptionChange(v.text, v.data)
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)
2012-03-29 22:21:59 +02:00
local menu = createWidget(self:getStyleName() .. 'PopupMenu')
menu:setId(self:getId() .. 'PopupMenu')
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() })
2012-01-05 19:02:27 +01:00
connect(menu, { onDestroy = function() self:setOn(false) end })
self:setOn(true)
return true
end
2012-01-19 20:15:38 +01:00
function UIComboBox:onMouseWheel(mousePos, direction)
if direction == MouseWheelUp and self.currentIndex > 1 then
self:setCurrentIndex(self.currentIndex - 1)
elseif direction == MouseWheelDown and self.currentIndex < #self.options then
self:setCurrentIndex(self.currentIndex + 1)
2012-01-19 20:15:38 +01:00
end
2012-01-20 02:16:10 +01:00
return true
2012-01-19 20:15:38 +01:00
end
function UIComboBox:onStyleApply(styleName, styleNode)
if styleNode.options then
for k,option in pairs(styleNode.options) do
self:addOption(option)
end
end
end
function UIComboBox:onOptionChange(optionText, optionData)
-- nothing todo
end