2012-06-26 00:13:30 +02:00
|
|
|
-- @docclass
|
2014-06-06 18:10:14 +02:00
|
|
|
UIComboBox = extends(UIWidget, "UIComboBox")
|
2012-01-04 11:26:58 +01:00
|
|
|
|
|
|
|
function UIComboBox.create()
|
|
|
|
local combobox = UIComboBox.internalCreate()
|
2013-01-16 17:20:17 +01:00
|
|
|
combobox:setFocusable(false)
|
2012-03-22 22:47:52 +01:00
|
|
|
combobox.options = {}
|
|
|
|
combobox.currentIndex = -1
|
2013-01-07 18:17:01 +01:00
|
|
|
combobox.mouseScroll = true
|
2014-07-03 18:15:38 +02:00
|
|
|
combobox.menuScroll = false
|
|
|
|
combobox.menuHeight = 100
|
|
|
|
combobox.menuScrollStep = 0
|
2012-01-04 11:26:58 +01:00
|
|
|
return combobox
|
|
|
|
end
|
|
|
|
|
2012-07-23 17:11:53 +02:00
|
|
|
function UIComboBox:clearOptions()
|
|
|
|
self.options = {}
|
|
|
|
self.currentIndex = -1
|
|
|
|
self:clearText()
|
|
|
|
end
|
|
|
|
|
2013-01-16 17:20:17 +01:00
|
|
|
function UIComboBox:getOption(text)
|
|
|
|
if not self.options then return nil end
|
|
|
|
for i,v in ipairs(self.options) do
|
|
|
|
if v.text == text then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-30 09:58:34 +02:00
|
|
|
function UIComboBox:setOption(text, dontSignal)
|
|
|
|
self:setCurrentOption(text, dontSignal)
|
|
|
|
end
|
|
|
|
|
2014-07-08 18:07:02 +02:00
|
|
|
function UIComboBox:setCurrentOption(text, dontSignal)
|
2012-03-22 22:47:52 +01:00
|
|
|
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
|
2012-01-04 11:26:58 +01:00
|
|
|
self:setText(text)
|
2014-07-08 18:07:02 +02:00
|
|
|
if not dontSignal then
|
|
|
|
signalcall(self.onOptionChange, self, text, v.data)
|
|
|
|
end
|
2012-01-04 11:26:58 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-08 18:07:02 +02:00
|
|
|
function UIComboBox:setCurrentOptionByData(data, dontSignal)
|
2013-01-16 17:20:17 +01:00
|
|
|
if not self.options then return end
|
|
|
|
for i,v in ipairs(self.options) do
|
|
|
|
if v.data == data and self.currentIndex ~= i then
|
|
|
|
self.currentIndex = i
|
|
|
|
self:setText(v.text)
|
2014-07-08 18:07:02 +02:00
|
|
|
if not dontSignal then
|
|
|
|
signalcall(self.onOptionChange, self, v.text, v.data)
|
|
|
|
end
|
2013-01-16 17:20:17 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-19 20:15:38 +01:00
|
|
|
function UIComboBox:setCurrentIndex(index)
|
2012-03-22 22:47:52 +01:00
|
|
|
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)
|
2013-02-12 17:14:16 +01:00
|
|
|
signalcall(self.onOptionChange, self, v.text, v.data)
|
2012-01-19 20:15:38 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-23 17:11:53 +02:00
|
|
|
function UIComboBox:getCurrentOption()
|
2013-01-29 07:26:32 +01:00
|
|
|
if table.haskey(self.options, self.currentIndex) then
|
2012-07-23 17:11:53 +02:00
|
|
|
return self.options[self.currentIndex]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-04 11:26:58 +01:00
|
|
|
function UIComboBox:addOption(text, data)
|
2012-03-22 22:47:52 +01:00
|
|
|
table.insert(self.options, { text = text, data = data })
|
|
|
|
local index = #self.options
|
2012-01-04 11:26:58 +01:00
|
|
|
if index == 1 then self:setCurrentOption(text) end
|
|
|
|
return index
|
|
|
|
end
|
|
|
|
|
2012-08-27 09:47:08 +02:00
|
|
|
function UIComboBox:removeOption(text)
|
|
|
|
for i,v in ipairs(self.options) do
|
|
|
|
if v.text == text then
|
|
|
|
table.remove(self.options, i)
|
|
|
|
if self.currentIndex == i then
|
|
|
|
self:setCurrentIndex(1)
|
|
|
|
elseif self.currentIndex > i then
|
|
|
|
self.currentIndex = self.currentIndex - 1
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-04 11:26:58 +01:00
|
|
|
function UIComboBox:onMousePress(mousePos, mouseButton)
|
2014-07-03 18:15:38 +02:00
|
|
|
local menu
|
|
|
|
if self.menuScroll then
|
|
|
|
menu = g_ui.createWidget(self:getStyleName() .. 'PopupScrollMenu')
|
|
|
|
menu:setHeight(self.menuHeight)
|
|
|
|
if self.menuScrollStep > 0 then
|
|
|
|
menu:setScrollbarStep(self.menuScrollStep)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
menu = g_ui.createWidget(self:getStyleName() .. 'PopupMenu')
|
|
|
|
end
|
2012-03-29 22:21:59 +02:00
|
|
|
menu:setId(self:getId() .. 'PopupMenu')
|
2012-03-22 22:47:52 +01:00
|
|
|
for i,v in ipairs(self.options) do
|
2012-01-04 11:26:58 +01:00
|
|
|
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)
|
2012-01-04 11:26:58 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2012-01-19 20:15:38 +01:00
|
|
|
function UIComboBox:onMouseWheel(mousePos, direction)
|
2013-01-07 18:17:01 +01:00
|
|
|
if not self.mouseScroll then
|
|
|
|
return false
|
|
|
|
end
|
2012-03-22 22:47:52 +01:00
|
|
|
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
|
|
|
|
|
2012-01-16 09:26:57 +01:00
|
|
|
function UIComboBox:onStyleApply(styleName, styleNode)
|
|
|
|
if styleNode.options then
|
|
|
|
for k,option in pairs(styleNode.options) do
|
|
|
|
self:addOption(option)
|
|
|
|
end
|
|
|
|
end
|
2013-02-01 17:31:24 +01:00
|
|
|
|
|
|
|
if styleNode.data then
|
|
|
|
for k,data in pairs(styleNode.data) do
|
|
|
|
local option = self.options[k]
|
|
|
|
if option then
|
|
|
|
option.data = data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-07 18:17:01 +01:00
|
|
|
for name,value in pairs(styleNode) do
|
|
|
|
if name == 'mouse-scroll' then
|
|
|
|
self.mouseScroll = value
|
2014-07-03 18:15:38 +02:00
|
|
|
elseif name == 'menu-scroll' then
|
|
|
|
self.menuScroll = value
|
|
|
|
elseif name == 'menu-height' then
|
|
|
|
self.menuHeight = value
|
|
|
|
elseif name == 'menu-scroll-step' then
|
|
|
|
self.menuScrollStep = value
|
2013-01-07 18:17:01 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIComboBox:setMouseScroll(scroll)
|
|
|
|
self.mouseScroll = scroll
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIComboBox:canMouseScroll()
|
|
|
|
return self.mouseScroll
|
2012-01-16 09:26:57 +01:00
|
|
|
end
|