Make time controls hideable

This commit is contained in:
Brandon Dyck 2024-04-15 21:27:34 -06:00
parent f2dbc88f3d
commit c042ced031

View File

@ -49,15 +49,54 @@ function love.load()
clock = Clock:new(nil, diameter, clockTransform) clock = Clock:new(nil, diameter, clockTransform)
local column2X = diameter + 2*margin local column2X = diameter + 2*margin
local controls = loveframes.Create("panel"):SetPos(column2X, margin) local timeInputs = loveframes.Create("panel")
local toggleTime = loveframes.Create("checkbox"):SetParent(controls):SetPos(0,0) :SetY(30)
toggleTime:SetText("Show time") local hourBox = loveframes.Create("numberbox")
local hourBox = loveframes.Create("numberbox"):SetMinMax(1, 12):SetValue(12) :SetMinMax(1, 12)
hourBox:SetParent(controls):SetPos(0,30) :SetValue(12)
local minuteBox = loveframes.Create("numberbox"):SetMinMax(0, 59):SetValue(0) :SetParent(timeInputs)
loveframes.Create("text"):SetText(":"):SetParent(controls):SetPos(80,30) timeInputs:SetHeight(hourBox:GetHeight())
minuteBox:SetParent(controls):SetPos(100,30) loveframes.Create("text")
:SetText(":")
:SetParent(timeInputs):SetX(80)
local minuteBox = loveframes.Create("numberbox")
:SetMinMax(0, 59)
:SetValue(0)
:SetParent(timeInputs)
:SetX(100)
local controls = loveframes.Create("collapsiblecategory")
:SetPos(column2X, margin)
:SetObject(timeInputs)
function controls:OnOpenedClosed()
if self:GetOpen() then
self:SetText("Hide time")
else
self:SetText("Show time")
end
end
controls:OnOpenedClosed()
local function setClock()
local t = Time:new(hourBox:GetValue(), minuteBox:GetValue())
clock:setTime(t)
end
hourBox.OnValueChanged = setClock
minuteBox.OnValueChanged = setClock
local function updateInputNoCallback(input, value)
local callback = input.OnValueChanged
input.OnValueChanged = nil
input:SetValue(value)
input.OnValueChanged = callback
end
local function updateInputs(time)
local h, m = time:get()
updateInputNoCallback(hourBox, h)
updateInputNoCallback(minuteBox, math.floor(m))
end
clock.onSetHands = updateInputs
local exitNote = "Press \"Esc\" to exit." local exitNote = "Press \"Esc\" to exit."
local exitNoteWidth = love.graphics.getWidth() - column2X - margin local exitNoteWidth = love.graphics.getWidth() - column2X - margin
@ -65,30 +104,9 @@ function love.load()
local _, wraps = exitNoteFont:getWrap(exitNote, exitNoteWidth) local _, wraps = exitNoteFont:getWrap(exitNote, exitNoteWidth)
local exitNoteY = love.graphics.getHeight() - table.maxn(wraps) * exitNoteFont:getHeight() - margin local exitNoteY = love.graphics.getHeight() - table.maxn(wraps) * exitNoteFont:getHeight() - margin
function setClock()
local t = Time:new(hourBox:GetValue(), minuteBox:GetValue())
clock:setTime(t)
end
hourBox.OnValueChanged = setClock
minuteBox.OnValueChanged = setClock
function updateInputNoCallback(input, value)
local callback = input.OnValueChanged
input.OnValueChanged = nil
input:SetValue(value)
input.OnValueChanged = callback
end
function updateInputs(time)
local h, m = time:get()
updateInputNoCallback(hourBox, h)
updateInputNoCallback(minuteBox, math.floor(m))
end
clock.onSetHands = updateInputs
love.graphics.setBackgroundColor(0.02,0.53,0.77) love.graphics.setBackgroundColor(0.02,0.53,0.77)
function draw() local function draw()
loveframes.draw() loveframes.draw()
clock:draw() clock:draw()
love.graphics.printf(exitNote, exitNoteFont, column2X, exitNoteY, exitNoteWidth) love.graphics.printf(exitNote, exitNoteFont, column2X, exitNoteY, exitNoteWidth)