Make a checkbox to toggle time display
This commit is contained in:
parent
8c86903501
commit
42ab042e59
46
checkbox.lua
Normal file
46
checkbox.lua
Normal 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
|
49
main.lua
49
main.lua
@ -1,11 +1,10 @@
|
||||
|
||||
local Time = require"time"
|
||||
local Clock = require"clock"
|
||||
local Checkbox = require"checkbox"
|
||||
|
||||
function love.load()
|
||||
if arg[#arg] == "-debug" then require("mobdebug").start() end
|
||||
|
||||
local fontsize = love.graphics.getHeight() / 20
|
||||
|
||||
local margin = love.graphics.getHeight() / 20
|
||||
|
||||
@ -21,21 +20,49 @@ function love.load()
|
||||
end
|
||||
|
||||
local exitNote = "Press \"Esc\" to exit."
|
||||
local exitNoteWidth = love.graphics.getWidth() - column2X - margin
|
||||
local exitNoteFont = love.graphics.newFont(fontsize)
|
||||
local _, wraps = exitNoteFont:getWrap(exitNote, exitNoteWidth)
|
||||
local exitNoteY = love.graphics.getHeight() - table.maxn(wraps) * exitNoteFont:getHeight() - margin
|
||||
local column2Width = love.graphics.getWidth() - column2X - margin
|
||||
local font = love.graphics.newFont(love.graphics.getHeight() / 20)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
function love.mousemoved(x, y, dx, dy, istouch)
|
||||
clock:movemouse(x, y)
|
||||
end
|
||||
|
||||
-- todo mousepressed & mousereleased
|
||||
function love.mousepressed(x, y, button, istouch, presses)
|
||||
clock:pressmouse(x, y, button)
|
||||
checkbox:pressmouse(x, y, button)
|
||||
end
|
||||
|
||||
function love.mousereleased(x, y, button, istouch, presses)
|
||||
@ -44,7 +71,11 @@ function love.load()
|
||||
|
||||
function love.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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user