2012-06-26 00:13:30 +02:00
|
|
|
-- @docclass
|
2012-03-31 15:43:01 +02:00
|
|
|
UISpinBox = extends(UITextEdit)
|
2012-01-26 02:11:05 +01:00
|
|
|
|
|
|
|
function UISpinBox.create()
|
|
|
|
local spinbox = UISpinBox.internalCreate()
|
|
|
|
spinbox:setValidCharacters('0123456789')
|
2012-03-22 22:47:52 +01:00
|
|
|
spinbox.minimum = 0
|
|
|
|
spinbox.maximum = 0
|
2012-03-29 15:45:40 +02:00
|
|
|
spinbox.value = 0
|
2012-02-03 12:59:55 +01:00
|
|
|
spinbox:setText("0")
|
2012-01-26 02:11:05 +01:00
|
|
|
return spinbox
|
|
|
|
end
|
|
|
|
|
|
|
|
function UISpinBox:onMouseWheel(mousePos, direction)
|
|
|
|
if direction == MouseWheelUp then
|
2012-03-29 15:45:40 +02:00
|
|
|
self:setValue(self.value + 1)
|
2012-01-26 02:11:05 +01:00
|
|
|
elseif direction == MouseWheelDown then
|
2012-03-29 15:45:40 +02:00
|
|
|
self:setValue(self.value - 1)
|
2012-01-26 02:11:05 +01:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2012-02-03 12:59:55 +01:00
|
|
|
function UISpinBox:onTextChange(text, oldText)
|
|
|
|
if text:len() == 0 then
|
2012-03-29 15:45:40 +02:00
|
|
|
self:setValue(self.minimum)
|
2012-02-03 12:59:55 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-01-26 02:11:05 +01:00
|
|
|
local number = tonumber(text)
|
2012-07-26 12:19:34 +02:00
|
|
|
if not number then
|
|
|
|
self:setText(number)
|
2012-02-03 12:59:55 +01:00
|
|
|
return
|
2012-07-26 12:19:34 +02:00
|
|
|
else
|
|
|
|
if number < self.minimum then
|
|
|
|
self:setText(self.minimum)
|
|
|
|
return
|
|
|
|
elseif number > self.maximum then
|
|
|
|
self:setText(self.maximum)
|
|
|
|
return
|
|
|
|
end
|
2012-01-26 02:11:05 +01:00
|
|
|
end
|
2012-06-26 00:13:30 +02:00
|
|
|
|
2012-03-29 15:45:40 +02:00
|
|
|
self:setValue(number)
|
2012-01-26 02:11:05 +01:00
|
|
|
end
|
|
|
|
|
2012-03-29 15:45:40 +02:00
|
|
|
function UISpinBox:onValueChange(value)
|
2012-01-26 02:11:05 +01:00
|
|
|
-- nothing todo
|
|
|
|
end
|
2012-02-03 12:59:55 +01:00
|
|
|
|
|
|
|
function UISpinBox:onStyleApply(styleName, styleNode)
|
2012-03-29 15:45:40 +02:00
|
|
|
for name, value in pairs(styleNode) do
|
|
|
|
if name == 'maximum' then
|
2012-07-26 12:19:34 +02:00
|
|
|
addEvent(function() self:setMaximum(value)
|
|
|
|
end)
|
2012-03-29 15:45:40 +02:00
|
|
|
elseif name == 'minimum' then
|
2012-07-26 12:19:34 +02:00
|
|
|
addEvent(function() self:setMinimum(value)
|
|
|
|
end)
|
2012-03-29 15:45:40 +02:00
|
|
|
end
|
2012-02-03 12:59:55 +01:00
|
|
|
end
|
2012-03-29 15:45:40 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function UISpinBox:setValue(value)
|
|
|
|
value = math.max(math.min(self.maximum, value), self.minimum)
|
|
|
|
if value == self.value then return end
|
|
|
|
if self:getText():len() > 0 then
|
|
|
|
self:setText(value)
|
|
|
|
end
|
|
|
|
self.value = value
|
|
|
|
signalcall(self.onValueChange, self, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
function UISpinBox:setMinimum(minimum)
|
|
|
|
self.minimum = minimum
|
2012-07-26 12:19:34 +02:00
|
|
|
if self.minimum > self.maximum then
|
|
|
|
self.maximum = self.minimum
|
|
|
|
end
|
2012-03-29 15:45:40 +02:00
|
|
|
if self.value < minimum then
|
|
|
|
self:setValue(minimum)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function UISpinBox:setMaximum(maximum)
|
|
|
|
self.maximum = maximum
|
|
|
|
if self.value > maximum then
|
|
|
|
self:setValue(maximum)
|
2012-02-03 12:59:55 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-26 12:19:34 +02:00
|
|
|
function UISpinBox:getValue()
|
|
|
|
return self.value
|
|
|
|
end
|