Make a checkbox to toggle time display

This commit is contained in:
Brandon Dyck 2024-04-21 16:42:22 -06:00
parent 8c86903501
commit 42ab042e59
3 changed files with 87 additions and 10 deletions

46
checkbox.lua Normal file
View File

@ -0,0 +1,46 @@
local Checkbox = {}
Checkbox.__index = Checkbox
function Checkbox:new(width, lineWidth, transform, checked)
local box = {
width = width or 20,
lineWidth = lineWidth or 2,
transform = transform or love.math.newTransform(),
checked = checked,
onChanged = nil
}
return setmetatable(box, self)
end
function Checkbox:draw()
local width = self.width
local lineWidth = self.lineWidth
local halfLine = lineWidth/2
local boxWidth = width - lineWidth
love.graphics.push("all")
love.graphics.applyTransform(self.transform)
love.graphics.setLineWidth(lineWidth)
love.graphics.rectangle("line", halfLine, halfLine, boxWidth, boxWidth)
if self.checked then
local crossEnd = width - halfLine
love.graphics.line(halfLine, halfLine, crossEnd, crossEnd)
love.graphics.line(halfLine, crossEnd, crossEnd, halfLine)
end
love.graphics.pop()
end
function Checkbox:getChecked()
return self.checked
end
function Checkbox:pressmouse(x, y, button)
if button == 1 then
local localX, localY = self.transform:inverseTransformPoint(x, y)
if localX >= 0 and localX < self.width and localY >= 0 and localY < self.width then
self.checked = not self.checked
if self.onChanged then self.onChanged(self.checked) end
end
end
end
return Checkbox

View File

@ -1,12 +1,11 @@
local Time = require"time" local Time = require"time"
local Clock = require"clock" local Clock = require"clock"
local Checkbox = require"checkbox"
function love.load() function love.load()
if arg[#arg] == "-debug" then require("mobdebug").start() end if arg[#arg] == "-debug" then require("mobdebug").start() end
local fontsize = love.graphics.getHeight() / 20
local margin = love.graphics.getHeight() / 20 local margin = love.graphics.getHeight() / 20
local diameter = love.graphics.getHeight() - 2 * margin local diameter = love.graphics.getHeight() - 2 * margin
@ -21,21 +20,49 @@ function love.load()
end end
local exitNote = "Press \"Esc\" to exit." local exitNote = "Press \"Esc\" to exit."
local exitNoteWidth = love.graphics.getWidth() - column2X - margin local column2Width = love.graphics.getWidth() - column2X - margin
local exitNoteFont = love.graphics.newFont(fontsize) local font = love.graphics.newFont(love.graphics.getHeight() / 20)
local _, wraps = exitNoteFont:getWrap(exitNote, exitNoteWidth)
local exitNoteY = love.graphics.getHeight() - table.maxn(wraps) * exitNoteFont:getHeight() - margin local _, exitNoteWraps = font:getWrap(exitNote, column2Width)
local exitNoteY = love.graphics.getHeight() - table.maxn(exitNoteWraps) * font:getHeight() - margin
local checkboxSize = font:getBaseline()
local checkboxLine = love.graphics.getHeight()/180
local checkbox = Checkbox:new(checkboxSize, checkboxLine, love.math.newTransform(column2X, margin), false)
local showTime = checkbox.checked
checkbox.onChanged = function(checked)
showTime = checked
end
local textCanvas = love.graphics.newCanvas(love.graphics.getWidth(), love.graphics.getHeight())
love.graphics.setCanvas(textCanvas)
love.graphics.setColor(1,1,1)
love.graphics.printf(" Show time", font, column2X + checkboxSize + checkboxLine, margin, column2Width)
love.graphics.printf("Press \"Esc\" to exit.", font, column2X, exitNoteY, column2Width)
love.graphics.setCanvas()
local timeString
local timeIndent
local figureWidth = font:getWidth("0")
clock.onSetHands = function(t)
timeString = tostring(clock:getTime())
if #timeString < 5 then
timeIndent = figureWidth
else
timeIndent = 0
end
end
clock.onSetHands(clock:getTime())
love.graphics.setBackgroundColor(0.02,0.53,0.77) love.graphics.setBackgroundColor(0.02,0.53,0.77)
function love.mousemoved(x, y, dx, dy, istouch) function love.mousemoved(x, y, dx, dy, istouch)
clock:movemouse(x, y) clock:movemouse(x, y)
end end
-- todo mousepressed & mousereleased
function love.mousepressed(x, y, button, istouch, presses) function love.mousepressed(x, y, button, istouch, presses)
clock:pressmouse(x, y, button) clock:pressmouse(x, y, button)
checkbox:pressmouse(x, y, button)
end end
function love.mousereleased(x, y, button, istouch, presses) function love.mousereleased(x, y, button, istouch, presses)
@ -44,7 +71,11 @@ function love.load()
function love.draw() function love.draw()
clock:draw() clock:draw()
love.graphics.printf(exitNote, exitNoteFont, column2X, exitNoteY, exitNoteWidth) checkbox:draw()
love.graphics.draw(textCanvas, 0, 0)
if showTime then
love.graphics.printf(timeString, font, column2X+timeIndent, margin+font:getHeight(), column2Width)
end
end end
end end

View File

@ -33,7 +33,7 @@ end
function Time:__tostring() function Time:__tostring()
local h, m = self:get() local h, m = self:get()
return string.format("%2d:%02d", h, m) return string.format("%d:%02d", h, m)
end end
return Time return Time