clocktoy/main.lua

88 lines
2.8 KiB
Lua

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 margin = love.graphics.getHeight() / 20
local diameter = love.graphics.getHeight() - 2 * margin
local offset = diameter/2 + margin
local clockTransform = love.math.newTransform(offset, offset)
local clock = Clock:new(nil, diameter, clockTransform)
local column2X = diameter + 2*margin
clock.onSetHands = function(time)
local h, m = time:get()
end
local exitNote = "Press \"Esc\" to exit."
local column2Width = love.graphics.getWidth() - column2X - margin
local textFont = love.graphics.newFont(love.graphics.getHeight() / 20)
local timeFont = love.graphics.newFont(love.graphics.getHeight() / 10)
local _, exitNoteWraps = textFont:getWrap(exitNote, column2Width)
local exitNoteY = love.graphics.getHeight() - table.maxn(exitNoteWraps) * textFont:getHeight() - margin
local checkboxSize = textFont: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", textFont, column2X + checkboxSize + checkboxLine, margin, column2Width)
love.graphics.printf("Press \"Esc\" to exit.", textFont, column2X, exitNoteY, column2Width)
love.graphics.setCanvas()
local timeString
local timeIndent
local figureWidth = timeFont: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
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)
clock:releasemouse(button)
end
function love.draw()
clock:draw()
checkbox:draw()
love.graphics.draw(textCanvas, 0, 0)
if showTime then
love.graphics.printf(timeString, timeFont, column2X+timeIndent, margin+textFont:getHeight(), column2Width)
end
end
end
function love.keypressed(key, scancode, isrepeat)
if scancode == "escape" then
love.event.quit()
end
end