clocktoy/main.lua

150 lines
3.9 KiB
Lua
Raw Normal View History

local Time = require"time"
local loveframes = require"loveframes"
2024-03-27 03:57:50 +00:00
local Clock = require"clock"
local clock
2024-04-16 02:56:58 +00:00
local exitNote
2024-03-27 03:57:50 +00:00
local inputSource
2024-03-27 03:57:50 +00:00
function love.mousemoved(x, y, dx, dy, istouch)
clock:movemouse(x, y)
2024-03-24 22:26:07 +00:00
end
2024-03-27 03:57:50 +00:00
-- todo mousepressed & mousereleased
function love.mousepressed(x, y, button, istouch, presses)
clock:pressmouse(x, y, button)
loveframes.mousepressed(x, y, button)
2024-03-24 22:26:07 +00:00
end
2024-03-27 03:57:50 +00:00
function love.mousereleased(x, y, button, istouch, presses)
clock:releasemouse(button)
loveframes.mousereleased(x, y, button)
end
function love.keypressed(key, scancode, isrepeat)
2024-04-16 02:56:58 +00:00
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)
2024-03-27 03:57:50 +00:00
end
2024-03-24 22:26:07 +00:00
2024-04-19 04:13:07 +00:00
-- TODO delete this when the skin is done
function setupSkin(fontsize)
local font = love.graphics.newFont(fontsize)
loveframes.basicfont = font
loveframes.config["ACTIVESKIN"] = "Blue"
local skin = loveframes.GetActiveSkin()
skin.controls.smallfont = font
loveframes.RegisterTemplate({
name = "Clock",
properties = {
numberbox = {
height = fontsize+10
},
textinput = {
width = font:getWidth("999")+10
},
collapsiblecategory = {
height = fontsize+10,
closedheight = fontsize+10
}
}
})
end
2024-03-24 22:26:07 +00:00
function love.load()
if arg[#arg] == "-debug" then require("mobdebug").start() end
2024-04-19 04:13:07 +00:00
loveframes.SetActiveSkin("Clock")
local fontsize = love.graphics.getHeight() / 20
-- setupSkin(fontsize)
2024-04-16 02:56:58 +00:00
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)
2024-04-16 02:56:58 +00:00
local column2X = diameter + 2*margin
2024-04-16 03:27:34 +00:00
local timeInputs = loveframes.Create("panel")
:SetY(30)
local hourBox = loveframes.Create("numberbox")
:SetMinMax(1, 12)
:SetValue(12)
:SetParent(timeInputs)
timeInputs:SetHeight(hourBox:GetHeight())
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()
2024-04-16 02:56:58 +00:00
2024-04-16 03:27:34 +00:00
local function setClock()
local t = Time:new(hourBox:GetValue(), minuteBox:GetValue())
clock:setTime(t)
end
hourBox.OnValueChanged = setClock
minuteBox.OnValueChanged = setClock
2024-04-16 03:27:34 +00:00
local function updateInputNoCallback(input, value)
local callback = input.OnValueChanged
input.OnValueChanged = nil
input:SetValue(value)
input.OnValueChanged = callback
end
2024-04-16 03:27:34 +00:00
local function updateInputs(time)
local h, m = time:get()
updateInputNoCallback(hourBox, h)
updateInputNoCallback(minuteBox, math.floor(m))
end
2024-04-16 03:27:34 +00:00
clock.onSetHands = updateInputs
2024-04-16 03:27:34 +00:00
local exitNote = "Press \"Esc\" to exit."
local exitNoteWidth = love.graphics.getWidth() - column2X - margin
2024-04-19 04:13:07 +00:00
local exitNoteFont = love.graphics.newFont(fontsize)
2024-04-16 03:27:34 +00:00
local _, wraps = exitNoteFont:getWrap(exitNote, exitNoteWidth)
local exitNoteY = love.graphics.getHeight() - table.maxn(wraps) * exitNoteFont:getHeight() - margin
love.graphics.setBackgroundColor(0.02,0.53,0.77)
2024-04-16 02:56:58 +00:00
2024-04-16 03:27:34 +00:00
local function draw()
2024-04-16 02:56:58 +00:00
loveframes.draw()
clock:draw()
love.graphics.printf(exitNote, exitNoteFont, column2X, exitNoteY, exitNoteWidth)
end
love.draw = draw
end
function love.update(dt)
loveframes.update(dt)
2024-03-24 22:26:07 +00:00
end