Add zero-padding to numberbox

This commit is contained in:
Brandon Dyck 2024-04-19 10:00:39 -06:00
parent 67af92b047
commit d8e257a3c0
2 changed files with 60 additions and 16 deletions

View File

@ -28,6 +28,7 @@ function newobject:initialize()
self.internal = false self.internal = false
self.canmodify = false self.canmodify = false
self.lastbuttonclicked = false self.lastbuttonclicked = false
self.zeropad = false
self.internals = {} self.internals = {}
self.OnValueChanged = nil self.OnValueChanged = nil
@ -60,6 +61,9 @@ function newobject:initialize()
end end
end end
end end
input.OnFocusLost = function(object)
self:updateinput()
end
input.Update = function(object) input.Update = function(object)
object:SetSize(object.parent.width - 20, object.parent.height) object:SetSize(object.parent.width - 20, object.parent.height)
end end
@ -141,6 +145,29 @@ function newobject:initialize()
self:SetDrawFunc() self:SetDrawFunc()
end 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) - func: update(deltatime)
- desc: updates the element - desc: updates the element
@ -231,11 +258,10 @@ function newobject:SetValue(value)
local curvalue = self.value local curvalue = self.value
local value = tonumber(value) or min local value = tonumber(value) or min
local internals = self.internals local internals = self.internals
local input = internals[1]
local onvaluechanged = self.OnValueChanged local onvaluechanged = self.OnValueChanged
self.value = value self.value = value
input:SetText(value) self:updateinput()
if value ~= curvalue and onvaluechanged then if value ~= curvalue and onvaluechanged then
onvaluechanged(self, value) onvaluechanged(self, value)
@ -303,15 +329,13 @@ end
--]]--------------------------------------------------------- --]]---------------------------------------------------------
function newobject:SetMax(max) function newobject:SetMax(max)
local internals = self.internals
local input = internals[1]
local onvaluechanged = self.OnValueChanged local onvaluechanged = self.OnValueChanged
self.max = max self.max = max
if self.value > max then if self.value > max then
self.value = max self.value = max
input:SetValue(max) self:updateinput()
if onvaluechanged then if onvaluechanged then
onvaluechanged(self, max) onvaluechanged(self, max)
end end
@ -337,15 +361,13 @@ end
--]]--------------------------------------------------------- --]]---------------------------------------------------------
function newobject:SetMin(min) function newobject:SetMin(min)
local internals = self.internals
local input = internals[1]
local onvaluechanged = self.OnValueChanged local onvaluechanged = self.OnValueChanged
self.min = min self.min = min
if self.value < min then if self.value < min then
self.value = min self.value = min
input:SetValue(min) self:updateinput()
if onvaluechanged then if onvaluechanged then
onvaluechanged(self, min) onvaluechanged(self, min)
end end
@ -371,8 +393,6 @@ end
--]]--------------------------------------------------------- --]]---------------------------------------------------------
function newobject:SetMinMax(min, max) function newobject:SetMinMax(min, max)
local internals = self.internals
local input = internals[1]
local onvaluechanged = self.OnValueChanged local onvaluechanged = self.OnValueChanged
self.min = min self.min = min
@ -380,7 +400,7 @@ function newobject:SetMinMax(min, max)
if self.value > max then if self.value > max then
self.value = max self.value = max
input:SetValue(max) self:updateinput()
if onvaluechanged then if onvaluechanged then
onvaluechanged(self, max) onvaluechanged(self, max)
end end
@ -388,7 +408,7 @@ function newobject:SetMinMax(min, max)
if self.value < min then if self.value < min then
self.value = min self.value = min
input:SetValue(min) self:updateinput()
if onvaluechanged then if onvaluechanged then
onvaluechanged(self, min) onvaluechanged(self, min)
end end
@ -415,8 +435,6 @@ end
function newobject:ModifyValue(type) function newobject:ModifyValue(type)
local value = self.value local value = self.value
local internals = self.internals
local input = internals[1]
local decimals = self.decimals local decimals = self.decimals
local onvaluechanged = self.OnValueChanged local onvaluechanged = self.OnValueChanged
@ -432,7 +450,7 @@ function newobject:ModifyValue(type)
self.value = max self.value = max
end end
self.value = loveframes.Round(self.value, decimals) self.value = loveframes.Round(self.value, decimals)
input:SetText(self.value) self:updateinput()
if value ~= self.value then if value ~= self.value then
if onvaluechanged then if onvaluechanged then
onvaluechanged(self, self.value) onvaluechanged(self, self.value)
@ -446,7 +464,7 @@ function newobject:ModifyValue(type)
self.value = min self.value = min
end end
self.value = loveframes.Round(self.value, decimals) self.value = loveframes.Round(self.value, decimals)
input:SetText(self.value) self:updateinput()
if value ~= self.value then if value ~= self.value then
if onvaluechanged then if onvaluechanged then
onvaluechanged(self, self.value) onvaluechanged(self, self.value)
@ -481,5 +499,29 @@ function newobject:GetDecimals()
end 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 ---------- ---------- module end ----------
end end

View File

@ -84,6 +84,7 @@ function love.load()
:SetMinMax(1, 12) :SetMinMax(1, 12)
:SetValue(12) :SetValue(12)
:SetParent(timeInputs) :SetParent(timeInputs)
:SetZeroPad(true)
timeInputs:SetHeight(hourBox:GetHeight()) timeInputs:SetHeight(hourBox:GetHeight())
loveframes.Create("text") loveframes.Create("text")
:SetText(":") :SetText(":")
@ -93,6 +94,7 @@ function love.load()
:SetValue(0) :SetValue(0)
:SetParent(timeInputs) :SetParent(timeInputs)
:SetX(100) :SetX(100)
:SetZeroPad(true)
local controls = loveframes.Create("collapsiblecategory") local controls = loveframes.Create("collapsiblecategory")
:SetPos(column2X, margin) :SetPos(column2X, margin)
:SetObject(timeInputs) :SetObject(timeInputs)