add zero-padding to numberbox

This commit is contained in:
Brandon Dyck 2024-04-22 12:18:06 -06:00
parent 09f1b941ca
commit 9e4fbccf0c
2 changed files with 67 additions and 17 deletions

View File

@ -6,13 +6,20 @@ function example.func(loveframes, centerarea)
local frame = loveframes.Create("frame") local frame = loveframes.Create("frame")
frame:SetName("Numberbox") frame:SetName("Numberbox")
frame:SetSize(210, 60) frame:SetSize(210, 90)
frame:CenterWithinArea(unpack(centerarea)) frame:CenterWithinArea(unpack(centerarea))
local numberbox = loveframes.Create("numberbox", frame) local numberbox = loveframes.Create("numberbox", frame)
numberbox:SetPos(5, 30) numberbox:SetPos(5, 30)
numberbox:SetSize(200, 25) numberbox:SetSize(200, 25)
local togglebutton = loveframes.Create("button", frame)
togglebutton:SetPos(5, 60)
togglebutton:SetWidth(200)
togglebutton:SetText("Toggle Padding")
togglebutton.OnClick = function(object)
numberbox:SetPad(not numberbox:GetPad())
end
end end
return example return example

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.pad = 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.pad 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.floor(math.log10(maxabs)) + 1
local width = integralwidth
if self.decimals > 0 then
width = width + self.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,19 +329,17 @@ 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)
if onvaluechanged then if onvaluechanged then
onvaluechanged(self, max) onvaluechanged(self, max)
end end
end end
self:updateinput()
return self return self
@ -337,19 +361,17 @@ 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)
if onvaluechanged then if onvaluechanged then
onvaluechanged(self, min) onvaluechanged(self, min)
end end
end end
self:updateinput()
return self return self
@ -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)
@ -466,6 +484,7 @@ end
function newobject:SetDecimals(decimals) function newobject:SetDecimals(decimals)
self.decimals = decimals self.decimals = decimals
self:updateinput()
return self return self
end end
@ -481,5 +500,29 @@ function newobject:GetDecimals()
end end
--[[---------------------------------------------------------
- func: SetPad(decimals)
- desc: sets whether to pad the object's value
with zeroes
--]]---------------------------------------------------------
function newobject:SetPad(pad)
self.pad = pad
self:updateinput()
return self
end
--[[---------------------------------------------------------
- func: GetPad()
- desc: gets whether to pad the object's value
with zeroes
--]]---------------------------------------------------------
function newobject:GetPad()
return self.pad
end
---------- module end ---------- ---------- module end ----------
end end