Align clock face and mouse inputs to center of screen

This commit is contained in:
Brandon Dyck 2024-04-15 19:51:42 -06:00
parent 4e9be28c42
commit 2283224516
2 changed files with 41 additions and 35 deletions

View File

@ -47,7 +47,7 @@ local function drawFaceCanvas(r, lineWidth)
local canvas = love.graphics.newCanvas(2*r,2*r) local canvas = love.graphics.newCanvas(2*r,2*r)
love.graphics.setCanvas(canvas) love.graphics.setCanvas(canvas)
love.graphics.push("all") love.graphics.push("all")
love.graphics.translate(r, r) love.graphics.translate(r,r)
local rbezel = r-lineWidth/2 local rbezel = r-lineWidth/2
love.graphics.setColor(YELLOW) love.graphics.setColor(YELLOW)
love.graphics.circle("fill", 0, 0, rbezel) love.graphics.circle("fill", 0, 0, rbezel)
@ -89,9 +89,9 @@ end
local Clock = {} local Clock = {}
Clock.__index = Clock Clock.__index = Clock
function Clock:new(time, diameter) function Clock:new(time, diameter, transform)
local radius = diameter and diameter/2 or 300 local radius = diameter and diameter/2 or 300
local transform = love.math.newTransform(radius, radius) transform = transform or love.math.newTransform()
local lineWidth = radius/200 local lineWidth = radius/200
local c = { local c = {
time = (time or Time:new()), time = (time or Time:new()),
@ -118,8 +118,8 @@ function Clock:draw()
local _, m = self.time:get() local _, m = self.time:get()
local h = self.time:getHoursSinceTwelve() local h = self.time:getHoursSinceTwelve()
love.graphics.push("all") love.graphics.push("all")
love.graphics.draw(self.faceCanvas)
love.graphics.applyTransform(self.transform) love.graphics.applyTransform(self.transform)
love.graphics.draw(self.faceCanvas, -self.radius, -self.radius)
self.minuteHand:draw(m / 60) self.minuteHand:draw(m / 60)
self.hourHand:draw(h / 12) self.hourHand:draw(h / 12)
love.graphics.pop() love.graphics.pop()

View File

@ -1,38 +1,9 @@
local Time = require"time" local Time = require"time"
local loveframes = require"loveframes" local loveframes = require"loveframes"
local Clock = require"clock" local Clock = require"clock"
local clock = Clock:new(nil, love.graphics.getHeight())
local controls = loveframes.Create("panel"):SetPos(10,10) local clock
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)
function setClock()
local t = Time:new(hourBox:GetValue(), minuteBox:GetValue())
clock:setTime(t)
end
hourBox.OnValueChanged = setClock
minuteBox.OnValueChanged = setClock
function updateInputNoCallback(input, value)
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
function love.mousemoved(x, y, dx, dy, istouch) function love.mousemoved(x, y, dx, dy, istouch)
clock:movemouse(x, y) clock:movemouse(x, y)
@ -62,6 +33,41 @@ function love.textinput(text)
end end
function love.load() function love.load()
if arg[#arg] == "-debug" then require("mobdebug").start() end
local clockTransform = love.math.newTransform(love.graphics.getWidth()/2, love.graphics.getHeight()/2)
clock = Clock:new(nil, love.graphics.getHeight(), clockTransform)
local controls = loveframes.Create("panel"):SetPos(10,10)
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)
function setClock()
local t = Time:new(hourBox:GetValue(), minuteBox:GetValue())
clock:setTime(t)
end
hourBox.OnValueChanged = setClock
minuteBox.OnValueChanged = setClock
function updateInputNoCallback(input, value)
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) love.graphics.setBackgroundColor(0.02,0.53,0.77)
end end