tibia-client/modules/core_widgets/uispinbox.lua

89 lines
2.1 KiB
Lua
Raw Normal View History

2012-01-26 02:11:05 +01:00
UISpinBox = extends(UILineEdit)
function UISpinBox.create()
local spinbox = UISpinBox.internalCreate()
spinbox:setValidCharacters('0123456789')
spinbox.m_minimum = 0
spinbox.m_maximum = 0
spinbox:setCurrentIndex(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:setCurrentIndex(index)
if index >= self.m_minimum and index <= self.m_maximum then
2012-02-03 12:59:55 +01:00
if self:getText():len() > 0 then
self:setText(index)
end
2012-01-26 02:11:05 +01:00
self.m_currentIndex = index
self:onIndexChange(index)
end
end
function UISpinBox:setMinimum(minimum)
if minimum > self.m_maximum then
print("[UISpinBox:setMinimum]: minimum value cant be greater than maximum")
return false
end
if self.m_currentIndex < minimum then
self:setCurrentIndex(minimum)
end
self.m_minimum = minimum
end
function UISpinBox:setMaximum(maximum)
if maximum < self.m_minimum then
print("[UISpinBox:setMaximum]: maximum value cant be lower than minimum")
return false
end
if self.m_currentIndex > maximum then
self:setCurrentIndex(maximum)
end
self.m_maximum = maximum
end
function UISpinBox:getCurrentIndex()
return self.m_currentIndex
end
function UISpinBox:onMouseWheel(mousePos, direction)
if direction == MouseWheelUp then
self:setCurrentIndex(self.m_currentIndex + 1)
elseif direction == MouseWheelDown then
self:setCurrentIndex(self.m_currentIndex - 1)
end
return true
end
2012-02-03 12:59:55 +01:00
function UISpinBox:onTextChange(text, oldText)
if text:len() == 0 then
self:setCurrentIndex(self.m_minimum)
return
end
2012-01-26 02:11:05 +01:00
local number = tonumber(text)
if not number or number > self.m_maximum or number < self.m_minimum then
2012-02-03 12:59:55 +01:00
self:setText(oldText)
return
2012-01-26 02:11:05 +01:00
end
2012-02-03 12:59:55 +01:00
self:setCurrentIndex(number)
2012-01-26 02:11:05 +01:00
end
function UISpinBox:onIndexChange(index)
-- nothing todo
end
2012-02-03 12:59:55 +01:00
function UISpinBox:onStyleApply(styleName, styleNode)
-- tonumber converts to 0 if not valid
if styleNode.maximum and tonumber(styleNode.maximum) then
self:setMaximum(tonumber(styleNode.maximum))
end
if styleNode.minimum and tonumber(styleNode.minimum) then
self:setMinimum(tonumber(styleNode.minimum))
end
end