1-- @docclass
2UISpinBox = extends(UITextEdit, "UISpinBox")
3
4function UISpinBox.create()
5 local spinbox = UISpinBox.internalCreate()
6 spinbox:setFocusable(false)
7 spinbox:setValidCharacters('0123456789')
8 spinbox.displayButtons = true
9 spinbox.minimum = 0
10 spinbox.maximum = 1
11 spinbox.value = 0
12 spinbox.step = 1
13 spinbox.firstchange = true
14 spinbox.mouseScroll = true
15 spinbox:setText("1")
16 spinbox:setValue(1)
17 return spinbox
18end
19
20function UISpinBox:onSetup()
21 g_mouse.bindAutoPress(self:getChildById('up'), function() self:up() end, 300)
22 g_mouse.bindAutoPress(self:getChildById('down'), function() self:down() end, 300)
23end
24
25function UISpinBox:onMouseWheel(mousePos, direction)
26 if not self.mouseScroll or self.disableScroll then
27 return false
28 end
29 if direction == MouseWheelUp then
30 self:up()
31 elseif direction == MouseWheelDown then
32 self:down()
33 end
34 return true
35end
36
37function UISpinBox:onKeyPress()
38 if self.firstchange then
39 self.firstchange = false
40 self:setText('')
41 end
42 return false
43end
44
45function UISpinBox:onTextChange(text, oldText)
46 if text:len() == 0 then
47 self:setValue(self.minimum)
48 return
49 end
50
51 local number = tonumber(text)
52 if not number then
53 self:setText(number)
54 return
55 else
56 if number < self.minimum then
57 self:setText(self.minimum)
58 return
59 elseif number > self.maximum then
60 self:setText(self.maximum)
61 return
62 end
63 end
64
65 self:setValue(number)
66end
67
68function UISpinBox:onValueChange(value)
69 -- nothing to do
70end
71
72function UISpinBox:onFocusChange(focused)
73 if not focused then
74 if self:getText():len() == 0 then
75 self:setText(self.minimum)
76 end
77 end
78end
79
80function UISpinBox:onStyleApply(styleName, styleNode)
81 for name, value in pairs(styleNode) do
82 if name == 'maximum' then
83 self.maximum = value
84 addEvent(function() self:setMaximum(value) end)
85 elseif name == 'minimum' then
86 self.minimum = value
87 addEvent(function() self:setMinimum(value) end)
88 elseif name == 'mouse-scroll' then
89 addEvent(function() self:setMouseScroll(value) end)
90 elseif name == 'buttons' then
91 addEvent(function()
92 if value then
93 self:showButtons()
94 else
95 self:hideButtons()
96 end
97 end)
98 end
99 end
100end
101
102function UISpinBox:showButtons()
103 self:getChildById('up'):show()
104 self:getChildById('down'):show()
105 self.displayButtons = true
106end
107
108function UISpinBox:hideButtons()
109 self:getChildById('up'):hide()
110 self:getChildById('down'):hide()
111 self.displayButtons = false
112end
113
114function UISpinBox:up()
115 self:setValue(self.value + self.step)
116end
117
118function UISpinBox:down()
119 self:setValue(self.value - self.step)
120end
121
122function UISpinBox:setValue(value, dontSignal)
123 if type(value) == "string" then
124 value = tonumber(value)
125 end
126 value = value or 0
127 value = math.max(math.min(self.maximum, value), self.minimum)
128
129 if value == self.value then return end
130
131 self.value = value
132 if self:getText():len() > 0 then
133 self:setText(value)
134 end
135
136 local upButton = self:getChildById('up')
137 local downButton = self:getChildById('down')
138 if upButton then
139 upButton:setEnabled(self.maximum ~= self.minimum and self.value ~= self.maximum)
140 end
141 if downButton then
142 downButton:setEnabled(self.maximum ~= self.minimum and self.value ~= self.minimum)
143 end
144
145 if not dontSignal then
146 signalcall(self.onValueChange, self, value)
147 end
148end
149
150function UISpinBox:getValue()
151 return self.value
152end
153
154function UISpinBox:setMinimum(minimum)
155 minimum = minimum or -9223372036854775808
156 self.minimum = minimum
157 if self.minimum > self.maximum then
158 self.maximum = self.minimum
159 end
160 if self.value < minimum then
161 self:setValue(minimum)
162 end
163end
164
165function UISpinBox:getMinimum()
166 return self.minimum
167end
168
169function UISpinBox:setMaximum(maximum)
170 maximum = maximum or 9223372036854775807
171 self.maximum = maximum
172 if self.value > maximum then
173 self:setValue(maximum)
174 end
175end
176
177function UISpinBox:getMaximum()
178 return self.maximum
179end
180
181function UISpinBox:setStep(step)
182 self.step = step or 1
183end
184
185function UISpinBox:setMouseScroll(mouseScroll)
186 self.mouseScroll = mouseScroll
187end
188
189function UISpinBox:getMouseScroll()
190 return self.mouseScroll
191end