2012-06-26 00:13:30 +02:00
|
|
|
-- @docclass
|
2014-06-06 18:10:14 +02:00
|
|
|
UISpinBox = extends(UITextEdit, "UISpinBox")
|
2012-01-26 02:11:05 +01:00
|
|
|
|
|
|
|
function UISpinBox.create()
|
|
|
|
local spinbox = UISpinBox.internalCreate()
|
2013-01-16 17:20:17 +01:00
|
|
|
spinbox:setFocusable(false)
|
2012-01-26 02:11:05 +01:00
|
|
|
spinbox:setValidCharacters('0123456789')
|
2012-08-28 15:40:04 +02:00
|
|
|
spinbox.displayButtons = true
|
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
|
2013-01-16 17:20:17 +01:00
|
|
|
spinbox.step = 1
|
|
|
|
spinbox.firstchange = true
|
2014-07-12 15:46:47 +02:00
|
|
|
spinbox.mouseScroll = true
|
2012-02-03 12:59:55 +01:00
|
|
|
spinbox:setText("0")
|
2012-01-26 02:11:05 +01:00
|
|
|
return spinbox
|
|
|
|
end
|
|
|
|
|
2013-02-20 11:08:03 +01:00
|
|
|
function UISpinBox:onSetup()
|
|
|
|
g_mouse.bindAutoPress(self:getChildById('up'), function() self:up() end, 300)
|
|
|
|
g_mouse.bindAutoPress(self:getChildById('down'), function() self:down() end, 300)
|
|
|
|
end
|
|
|
|
|
2012-01-26 02:11:05 +01:00
|
|
|
function UISpinBox:onMouseWheel(mousePos, direction)
|
2014-07-12 15:46:47 +02:00
|
|
|
if not self.mouseScroll then
|
|
|
|
return false
|
|
|
|
end
|
2012-01-26 02:11:05 +01:00
|
|
|
if direction == MouseWheelUp then
|
2012-08-27 23:56:05 +02:00
|
|
|
self:up()
|
2012-01-26 02:11:05 +01:00
|
|
|
elseif direction == MouseWheelDown then
|
2012-08-27 23:56:05 +02:00
|
|
|
self:down()
|
2012-01-26 02:11:05 +01:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-01-16 17:20:17 +01:00
|
|
|
function UISpinBox:onKeyPress()
|
|
|
|
if self.firstchange then
|
|
|
|
self.firstchange = false
|
|
|
|
self:setText('')
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
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-08-28 15:40:04 +02:00
|
|
|
addEvent(function() self:setMaximum(value) end)
|
2012-03-29 15:45:40 +02:00
|
|
|
elseif name == 'minimum' then
|
2012-08-28 15:40:04 +02:00
|
|
|
addEvent(function() self:setMinimum(value) end)
|
2014-07-12 15:46:47 +02:00
|
|
|
elseif name == 'mouse-scroll' then
|
|
|
|
addEvent(function() self:setMouseScroll(value) end)
|
2012-08-28 15:40:04 +02:00
|
|
|
elseif name == 'buttons' then
|
|
|
|
addEvent(function()
|
|
|
|
if value then
|
|
|
|
self:showButtons()
|
|
|
|
else
|
|
|
|
self:hideButtons()
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2012-08-28 15:40:04 +02:00
|
|
|
function UISpinBox:showButtons()
|
|
|
|
self:getChildById('up'):show()
|
|
|
|
self:getChildById('down'):show()
|
|
|
|
self.displayButtons = true
|
|
|
|
end
|
|
|
|
|
|
|
|
function UISpinBox:hideButtons()
|
|
|
|
self:getChildById('up'):hide()
|
|
|
|
self:getChildById('down'):hide()
|
|
|
|
self.displayButtons = false
|
|
|
|
end
|
|
|
|
|
2012-08-27 23:56:05 +02:00
|
|
|
function UISpinBox:up()
|
2013-01-16 17:20:17 +01:00
|
|
|
self:setValue(self.value + self.step)
|
2012-08-27 23:56:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function UISpinBox:down()
|
2013-01-16 17:20:17 +01:00
|
|
|
self:setValue(self.value - self.step)
|
2012-08-27 23:56:05 +02:00
|
|
|
end
|
|
|
|
|
2012-03-29 15:45:40 +02:00
|
|
|
function UISpinBox:setValue(value)
|
2013-01-16 17:20:17 +01:00
|
|
|
value = value or 0
|
2012-03-29 15:45:40 +02:00
|
|
|
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
|
2012-08-29 08:11:38 +02:00
|
|
|
|
|
|
|
local upButton = self:getChildById('up')
|
|
|
|
local downButton = self:getChildById('down')
|
|
|
|
if upButton then
|
|
|
|
upButton:setEnabled(self.maximum ~= self.minimum and self.value ~= self.maximum)
|
|
|
|
end
|
|
|
|
if downButton then
|
|
|
|
downButton:setEnabled(self.maximum ~= self.minimum and self.value ~= self.minimum)
|
|
|
|
end
|
|
|
|
|
2012-03-29 15:45:40 +02:00
|
|
|
signalcall(self.onValueChange, self, value)
|
|
|
|
end
|
|
|
|
|
2013-02-01 02:21:20 +01:00
|
|
|
function UISpinBox:getValue()
|
|
|
|
return self.value
|
|
|
|
end
|
|
|
|
|
2012-03-29 15:45:40 +02:00
|
|
|
function UISpinBox:setMinimum(minimum)
|
2013-01-16 17:20:17 +01:00
|
|
|
minimum = minimum or -9223372036854775808
|
2012-03-29 15:45:40 +02:00
|
|
|
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
|
|
|
|
|
2013-02-01 02:21:20 +01:00
|
|
|
function UISpinBox:getMinimum()
|
|
|
|
return self.minimum
|
|
|
|
end
|
|
|
|
|
2012-03-29 15:45:40 +02:00
|
|
|
function UISpinBox:setMaximum(maximum)
|
2013-01-16 17:20:17 +01:00
|
|
|
maximum = maximum or 9223372036854775807
|
2012-03-29 15:45:40 +02:00
|
|
|
self.maximum = maximum
|
|
|
|
if self.value > maximum then
|
|
|
|
self:setValue(maximum)
|
2012-02-03 12:59:55 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-01 02:21:20 +01:00
|
|
|
function UISpinBox:getMaximum()
|
|
|
|
return self.maximum
|
2013-01-16 17:20:17 +01:00
|
|
|
end
|
|
|
|
|
2013-02-01 02:21:20 +01:00
|
|
|
function UISpinBox:setStep(step)
|
|
|
|
self.step = step or 1
|
2012-07-26 12:19:34 +02:00
|
|
|
end
|
2014-07-12 15:46:47 +02:00
|
|
|
|
|
|
|
function UISpinBox:setMouseScroll(mouseScroll)
|
|
|
|
self.mouseScroll = mouseScroll
|
|
|
|
end
|
|
|
|
|
|
|
|
function UISpinBox:getMouseScroll()
|
|
|
|
return self.mouseScroll
|
|
|
|
end
|