Two hands with time set by dragging

This commit is contained in:
Brandon Dyck 2024-03-27 00:05:06 -06:00
parent d50fefa68f
commit 4e391e667f

View File

@ -1,26 +1,50 @@
local MAX_SECONDS = 60 * 60 * 12 local MAX_MINUTES = 60 * 12
local Time = {} local Time = {}
Time.__index = Time Time.__index = Time
function Time:new(hour, minute) local function newTimeRaw(rawMinutes)
local t = { local t = {
hour = (hour or 12), rawminutes = rawMinutes % MAX_MINUTES
minute = (minute or 0)
} }
return setmetatable(t, self) return setmetatable(t, Time)
end end
function Time:getHour() function Time:new(hour, minute)
return self.hour return newTimeRaw((hour or 12) % 12 * 60 + (minute or 0))
end end
function Time:getMinute() function Time:get()
return self.minute local h = math.floor(self.rawminutes / 60 % 12)
if h == 0 then h = 12 end
local m = self.rawminutes % 60
return h, m
end
function Time:getHoursSinceTwelve()
local h = self.rawminutes / 60
if h < 1 then h = h + 12 end
return h
end
function Time:addMinutes(minutes)
return newTimeRaw(self.rawminutes + minutes)
end end
function Time:fmt() function Time:fmt()
return string.format("%2d:%02d", self.hour, self.minute) local h, m = self:get()
return string.format("%2d:%02d", h, m)
end
local function pointToTurns(x, y)
local rawTurns = math.atan2(y, x) / 2 / math.pi
local _, clockTurns = math.modf(rawTurns + 1.25)
return clockTurns
end
local function turnsToRads(turns)
return turns * 2 * math.pi - 0.5 * math.pi
end end
@ -30,28 +54,15 @@ Hand.__index = Hand
function Hand:new(length, color) function Hand:new(length, color)
local h = { local h = {
length = (length or 10), length = (length or 10),
color = (color or {1,1,1}), color = (color or {1,1,1})
turns = 0
} }
return setmetatable(h, self) return setmetatable(h, self)
end end
function Hand:setTurns(turns) function Hand:draw(turns)
self.turns = turns
end
function Hand:getTurns()
return self.turns
end
function Hand:draw()
-- local turns = sec_elapsed / self.sec_per_turn
-- if self.ticks_per_turn then
-- turns = math.floor(turns * self.ticks_per_turn) / self.ticks_per_turn
-- end
love.graphics.push("all") love.graphics.push("all")
love.graphics.setColor(unpack(self.color)) love.graphics.setColor(unpack(self.color))
love.graphics.rotate(2 * math.pi * (self.turns + 0.25)) love.graphics.rotate(turnsToRads(turns) + 0.5 * math.pi)
love.graphics.rectangle("fill", -10, 10, 20, -self.length) love.graphics.rectangle("fill", -10, 10, 20, -self.length)
love.graphics.pop() love.graphics.pop()
end end
@ -63,7 +74,8 @@ Clock.__index = Clock
function Clock:new(time, diameter, transform) function Clock:new(time, diameter, transform)
local c = { local c = {
time = (time or Time:new()), time = (time or Time:new()),
minuteHand = Hand:new(240, {1,0,0}, 60*60), minuteHand = Hand:new(240, {1,0,0}),
hourHand = Hand:new(180, {0,0.5,0.5}),
dragging = false, dragging = false,
transform = transform or love.math.newTransform(), transform = transform or love.math.newTransform(),
radius = diameter and diameter/2 or 300 radius = diameter and diameter/2 or 300
@ -80,15 +92,15 @@ function Clock:getTime()
end end
function Clock:draw() function Clock:draw()
-- debug local _, m = self.time:get()
love.graphics.print(self.minuteHand:getTurns(), 10, 50) local h = self.time:getHoursSinceTwelve()
-- end debug
love.graphics.push("all") love.graphics.push("all")
love.graphics.applyTransform(self.transform) love.graphics.applyTransform(self.transform)
love.graphics.setColor(0.5, 0.5, 0.5) love.graphics.setColor(0.5, 0.5, 0.5)
love.graphics.circle("fill", 0, 0, self.radius) love.graphics.circle("fill", 0, 0, self.radius)
self.minuteHand:draw() self.minuteHand:draw(m / 60)
self.hourHand:draw(h / 12)
love.graphics.pop() love.graphics.pop()
end end
@ -100,8 +112,16 @@ end
local function clockPointAt(clock, x, y) local function clockPointAt(clock, x, y)
local localX, localY = clock.transform:inverseTransformPoint(x, y) local localX, localY = clock.transform:inverseTransformPoint(x, y)
local turns = math.atan2(localY, localX) / 2 / math.pi local minutes = pointToTurns(localX, localY) * 60
clock.minuteHand:setTurns(turns) local _, oldMinutes = clock.time:get()
local dMinutes = minutes - oldMinutes
-- Adjust for rolling over
if dMinutes < -30 then
dMinutes = dMinutes + 60
elseif dMinutes > 30 then
dMinutes = dMinutes - 60
end
clock.time = clock.time:addMinutes(dMinutes)
end end
function Clock:movemouse(x, y) function Clock:movemouse(x, y)