clocktoy/main.lua
2024-04-15 20:56:58 -06:00

103 lines
3.0 KiB
Lua

local Time = require"time"
local loveframes = require"loveframes"
local Clock = require"clock"
local clock
local exitNote
local inputSource
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)
loveframes.mousepressed(x, y, button)
end
function love.mousereleased(x, y, button, istouch, presses)
clock:releasemouse(button)
loveframes.mousereleased(x, y, button)
end
function love.keypressed(key, scancode, isrepeat)
if scancode == "escape" then
love.event.quit()
end
loveframes.keypressed(key, isrepeat)
end
function love.keyreleased(key)
loveframes.keyreleased(key)
end
function love.textinput(text)
loveframes.textinput(text)
end
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)
clock = Clock:new(nil, diameter, clockTransform)
local column2X = diameter + 2*margin
local controls = loveframes.Create("panel"):SetPos(column2X, margin)
local toggleTime = loveframes.Create("checkbox"):SetParent(controls):SetPos(0,0)
toggleTime:SetText("Show time")
local hourBox = loveframes.Create("numberbox"):SetMinMax(1, 12):SetValue(12)
hourBox:SetParent(controls):SetPos(0,30)
local minuteBox = loveframes.Create("numberbox"):SetMinMax(0, 59):SetValue(0)
loveframes.Create("text"):SetText(":"):SetParent(controls):SetPos(80,30)
minuteBox:SetParent(controls):SetPos(100,30)
local exitNote = "Press \"Esc\" to exit."
local exitNoteWidth = love.graphics.getWidth() - column2X - margin
local exitNoteFont = love.graphics.newFont(love.graphics.getHeight()/20)
local _, wraps = exitNoteFont:getWrap(exitNote, exitNoteWidth)
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)
function draw()
loveframes.draw()
clock:draw()
love.graphics.printf(exitNote, exitNoteFont, column2X, exitNoteY, exitNoteWidth)
end
love.draw = draw
end
function love.update(dt)
loveframes.update(dt)
end