Add zero-padding to numberbox
This commit is contained in:
parent
67af92b047
commit
d8e257a3c0
@ -28,6 +28,7 @@ function newobject:initialize()
|
||||
self.internal = false
|
||||
self.canmodify = false
|
||||
self.lastbuttonclicked = false
|
||||
self.zeropad = false
|
||||
self.internals = {}
|
||||
self.OnValueChanged = nil
|
||||
|
||||
@ -60,6 +61,9 @@ function newobject:initialize()
|
||||
end
|
||||
end
|
||||
end
|
||||
input.OnFocusLost = function(object)
|
||||
self:updateinput()
|
||||
end
|
||||
input.Update = function(object)
|
||||
object:SetSize(object.parent.width - 20, object.parent.height)
|
||||
end
|
||||
@ -141,6 +145,29 @@ function newobject:initialize()
|
||||
self:SetDrawFunc()
|
||||
end
|
||||
|
||||
function newobject:updateinput()
|
||||
|
||||
local value = self.value
|
||||
if self.zeropad then
|
||||
local maxabs = math.max(math.abs(self.min), math.abs(self.max))
|
||||
-- A range from 0 to 0 would be unusual, but it would break the math.
|
||||
if maxabs == 0 then maxabs = 1 end
|
||||
local integralwidth = math.ceil(math.log10(maxabs))
|
||||
local width = integralwidth
|
||||
if self.decimals > 0 then
|
||||
width = width + decimals + 1
|
||||
end
|
||||
if value < 0 then
|
||||
width = width + 1
|
||||
end
|
||||
local formatstring = string.format("%%0%d.%df", width, self.decimals)
|
||||
value = string.format(formatstring, value)
|
||||
end
|
||||
local input = self.internals[1]
|
||||
input:SetText(value)
|
||||
|
||||
end
|
||||
|
||||
--[[---------------------------------------------------------
|
||||
- func: update(deltatime)
|
||||
- desc: updates the element
|
||||
@ -231,11 +258,10 @@ function newobject:SetValue(value)
|
||||
local curvalue = self.value
|
||||
local value = tonumber(value) or min
|
||||
local internals = self.internals
|
||||
local input = internals[1]
|
||||
local onvaluechanged = self.OnValueChanged
|
||||
|
||||
self.value = value
|
||||
input:SetText(value)
|
||||
self:updateinput()
|
||||
|
||||
if value ~= curvalue and onvaluechanged then
|
||||
onvaluechanged(self, value)
|
||||
@ -303,15 +329,13 @@ end
|
||||
--]]---------------------------------------------------------
|
||||
function newobject:SetMax(max)
|
||||
|
||||
local internals = self.internals
|
||||
local input = internals[1]
|
||||
local onvaluechanged = self.OnValueChanged
|
||||
|
||||
self.max = max
|
||||
|
||||
if self.value > max then
|
||||
self.value = max
|
||||
input:SetValue(max)
|
||||
self:updateinput()
|
||||
if onvaluechanged then
|
||||
onvaluechanged(self, max)
|
||||
end
|
||||
@ -337,15 +361,13 @@ end
|
||||
--]]---------------------------------------------------------
|
||||
function newobject:SetMin(min)
|
||||
|
||||
local internals = self.internals
|
||||
local input = internals[1]
|
||||
local onvaluechanged = self.OnValueChanged
|
||||
|
||||
self.min = min
|
||||
|
||||
if self.value < min then
|
||||
self.value = min
|
||||
input:SetValue(min)
|
||||
self:updateinput()
|
||||
if onvaluechanged then
|
||||
onvaluechanged(self, min)
|
||||
end
|
||||
@ -371,8 +393,6 @@ end
|
||||
--]]---------------------------------------------------------
|
||||
function newobject:SetMinMax(min, max)
|
||||
|
||||
local internals = self.internals
|
||||
local input = internals[1]
|
||||
local onvaluechanged = self.OnValueChanged
|
||||
|
||||
self.min = min
|
||||
@ -380,7 +400,7 @@ function newobject:SetMinMax(min, max)
|
||||
|
||||
if self.value > max then
|
||||
self.value = max
|
||||
input:SetValue(max)
|
||||
self:updateinput()
|
||||
if onvaluechanged then
|
||||
onvaluechanged(self, max)
|
||||
end
|
||||
@ -388,7 +408,7 @@ function newobject:SetMinMax(min, max)
|
||||
|
||||
if self.value < min then
|
||||
self.value = min
|
||||
input:SetValue(min)
|
||||
self:updateinput()
|
||||
if onvaluechanged then
|
||||
onvaluechanged(self, min)
|
||||
end
|
||||
@ -415,8 +435,6 @@ end
|
||||
function newobject:ModifyValue(type)
|
||||
|
||||
local value = self.value
|
||||
local internals = self.internals
|
||||
local input = internals[1]
|
||||
local decimals = self.decimals
|
||||
local onvaluechanged = self.OnValueChanged
|
||||
|
||||
@ -432,7 +450,7 @@ function newobject:ModifyValue(type)
|
||||
self.value = max
|
||||
end
|
||||
self.value = loveframes.Round(self.value, decimals)
|
||||
input:SetText(self.value)
|
||||
self:updateinput()
|
||||
if value ~= self.value then
|
||||
if onvaluechanged then
|
||||
onvaluechanged(self, self.value)
|
||||
@ -446,7 +464,7 @@ function newobject:ModifyValue(type)
|
||||
self.value = min
|
||||
end
|
||||
self.value = loveframes.Round(self.value, decimals)
|
||||
input:SetText(self.value)
|
||||
self:updateinput()
|
||||
if value ~= self.value then
|
||||
if onvaluechanged then
|
||||
onvaluechanged(self, self.value)
|
||||
@ -481,5 +499,29 @@ function newobject:GetDecimals()
|
||||
|
||||
end
|
||||
|
||||
--[[---------------------------------------------------------
|
||||
- func: SetZeroPad(decimals)
|
||||
- desc: sets whether to pad the object's value
|
||||
with zeroes
|
||||
--]]---------------------------------------------------------
|
||||
function newobject:SetZeroPad(zeropad)
|
||||
|
||||
self.zeropad = zeropad
|
||||
self:updateinput()
|
||||
return self
|
||||
|
||||
end
|
||||
|
||||
--[[---------------------------------------------------------
|
||||
- func: GetZeroPad()
|
||||
- desc: gets whether to pad the object's value
|
||||
with zeroes
|
||||
--]]---------------------------------------------------------
|
||||
function newobject:GetZeroPad()
|
||||
|
||||
return self.zeropad
|
||||
|
||||
end
|
||||
|
||||
---------- module end ----------
|
||||
end
|
||||
|
2
main.lua
2
main.lua
@ -84,6 +84,7 @@ function love.load()
|
||||
:SetMinMax(1, 12)
|
||||
:SetValue(12)
|
||||
:SetParent(timeInputs)
|
||||
:SetZeroPad(true)
|
||||
timeInputs:SetHeight(hourBox:GetHeight())
|
||||
loveframes.Create("text")
|
||||
:SetText(":")
|
||||
@ -93,6 +94,7 @@ function love.load()
|
||||
:SetValue(0)
|
||||
:SetParent(timeInputs)
|
||||
:SetX(100)
|
||||
:SetZeroPad(true)
|
||||
local controls = loveframes.Create("collapsiblecategory")
|
||||
:SetPos(column2X, margin)
|
||||
:SetObject(timeInputs)
|
||||
|
Loading…
Reference in New Issue
Block a user