1-- @docclass
2UIComboBox = extends(UIWidget, "UIComboBox")
3
4function UIComboBox.create()
5 local combobox = UIComboBox.internalCreate()
6 combobox:setFocusable(false)
7 combobox.options = {}
8 combobox.currentIndex = -1
9 combobox.mouseScroll = true
10 combobox.menuScroll = false
11 combobox.menuHeight = 100
12 combobox.menuScrollStep = 0
13 return combobox
14end
15
16function UIComboBox:clearOptions()
17 self.options = {}
18 self.currentIndex = -1
19 self:clearText()
20end
21
22function UIComboBox:clear()
23 return self:clearOptions()
24end
25
26function UIComboBox:getOptionsCount()
27 return #self.options
28end
29
30function UIComboBox:isOption(text)
31 if not self.options then return false end
32 for i,v in ipairs(self.options) do
33 if v.text == text then
34 return true
35 end
36 end
37 return false
38end
39
40function UIComboBox:setOption(text, dontSignal)
41 self:setCurrentOption(text, dontSignal)
42end
43
44function UIComboBox:setCurrentOption(text, dontSignal)
45 if not self.options then return end
46 for i,v in ipairs(self.options) do
47 if v.text == text and self.currentIndex ~= i then
48 self.currentIndex = i
49 self:setText(text)
50 if not dontSignal then
51 signalcall(self.onOptionChange, self, text, v.data)
52 end
53 return
54 end
55 end
56end
57
58function UIComboBox:updateCurrentOption(newText)
59 self.options[self.currentIndex].text = newText
60 self:setText(newText)
61end
62
63function UIComboBox:setCurrentOptionByData(data, dontSignal)
64 if not self.options then return end
65 for i,v in ipairs(self.options) do
66 if v.data == data and self.currentIndex ~= i then
67 self.currentIndex = i
68 self:setText(v.text)
69 if not dontSignal then
70 signalcall(self.onOptionChange, self, v.text, v.data)
71 end
72 return
73 end
74 end
75end
76
77function UIComboBox:setCurrentIndex(index, dontSignal)
78 if index >= 1 and index <= #self.options then
79 local v = self.options[index]
80 self.currentIndex = index
81 self:setText(v.text)
82 if not dontSignal then
83 signalcall(self.onOptionChange, self, v.text, v.data)
84 end
85 end
86end
87
88function UIComboBox:getCurrentOption()
89 if table.haskey(self.options, self.currentIndex) then
90 return self.options[self.currentIndex]
91 end
92end
93
94function UIComboBox:addOption(text, data)
95 table.insert(self.options, { text = text, data = data })
96 local index = #self.options
97 if index == 1 then self:setCurrentOption(text) end
98 return index
99end
100
101function UIComboBox:removeOption(text)
102 for i,v in ipairs(self.options) do
103 if v.text == text then
104 table.remove(self.options, i)
105 if self.currentIndex == i then
106 self:setCurrentIndex(1)
107 elseif self.currentIndex > i then
108 self.currentIndex = self.currentIndex - 1
109 end
110 return
111 end
112 end
113end
114
115function UIComboBox:onMousePress(mousePos, mouseButton)
116 local menu
117 if self.menuScroll then
118 menu = g_ui.createWidget(self:getStyleName() .. 'PopupScrollMenu')
119 menu:setHeight(self.menuHeight)
120 if self.menuScrollStep > 0 then
121 menu:setScrollbarStep(self.menuScrollStep)
122 end
123 else
124 menu = g_ui.createWidget(self:getStyleName() .. 'PopupMenu')
125 end
126 menu:setId(self:getId() .. 'PopupMenu')
127 for i,v in ipairs(self.options) do
128 menu:addOption(v.text, function() self:setCurrentOption(v.text) end)
129 end
130 menu:setWidth(self:getWidth())
131 menu:display({ x = self:getX(), y = self:getY() + self:getHeight() })
132 connect(menu, { onDestroy = function() self:setOn(false) end })
133 self:setOn(true)
134 return true
135end
136
137function UIComboBox:onMouseWheel(mousePos, direction)
138 if not self.mouseScroll or self.disableScroll then
139 return false
140 end
141 if direction == MouseWheelUp and self.currentIndex > 1 then
142 self:setCurrentIndex(self.currentIndex - 1)
143 elseif direction == MouseWheelDown and self.currentIndex < #self.options then
144 self:setCurrentIndex(self.currentIndex + 1)
145 end
146 return true
147end
148
149function UIComboBox:onStyleApply(styleName, styleNode)
150 if styleNode.options then
151 for k,option in pairs(styleNode.options) do
152 self:addOption(option)
153 end
154 end
155
156 if styleNode.data then
157 for k,data in pairs(styleNode.data) do
158 local option = self.options[k]
159 if option then
160 option.data = data
161 end
162 end
163 end
164
165 for name,value in pairs(styleNode) do
166 if name == 'mouse-scroll' then
167 self.mouseScroll = value
168 elseif name == 'menu-scroll' then
169 self.menuScroll = value
170 elseif name == 'menu-height' then
171 self.menuHeight = value
172 elseif name == 'menu-scroll-step' then
173 self.menuScrollStep = value
174 end
175 end
176end
177
178function UIComboBox:setMouseScroll(scroll)
179 self.mouseScroll = scroll
180end
181
182function UIComboBox:canMouseScroll()
183 return self.mouseScroll
184end
185