clocktoy/clock.lua

127 lines
2.8 KiB
Lua
Raw Normal View History

2024-03-24 22:26:07 +00:00
local MAX_SECONDS = 60 * 60 * 12
2024-03-27 03:57:50 +00:00
local Time = {}
Time.__index = Time
function Time:new(hour, minute)
local t = {
hour = (hour or 12),
minute = (minute or 0)
}
return setmetatable(t, self)
end
function Time:getHour()
return self.hour
end
function Time:getMinute()
return self.minute
end
function Time:fmt()
return string.format("%2d:%02d", self.hour, self.minute)
end
2024-03-24 22:26:07 +00:00
local Hand = {}
Hand.__index = Hand
2024-03-27 03:57:50 +00:00
function Hand:new(length, color)
2024-03-24 22:26:07 +00:00
local h = {
length = (length or 10),
color = (color or {1,1,1}),
2024-03-27 03:57:50 +00:00
turns = 0
2024-03-24 22:26:07 +00:00
}
return setmetatable(h, self)
end
2024-03-27 03:57:50 +00:00
function Hand:setTurns(turns)
self.turns = turns
2024-03-24 22:26:07 +00:00
end
2024-03-27 03:57:50 +00:00
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
2024-03-24 22:26:07 +00:00
love.graphics.push("all")
love.graphics.setColor(unpack(self.color))
2024-03-27 03:57:50 +00:00
love.graphics.rotate(2 * math.pi * (self.turns + 0.25))
2024-03-24 22:26:07 +00:00
love.graphics.rectangle("fill", -10, 10, 20, -self.length)
love.graphics.pop()
end
2024-03-27 03:57:50 +00:00
local Clock = {}
Clock.__index = Clock
function Clock:new(time, diameter, transform)
local c = {
time = (time or Time:new()),
minuteHand = Hand:new(240, {1,0,0}, 60*60),
dragging = false,
transform = transform or love.math.newTransform(),
radius = diameter and diameter/2 or 300
}
return setmetatable(c, self)
end
function Clock:setTime(time)
self.time = time
end
function Clock:getTime()
return self.time
end
function Clock:draw()
-- debug
love.graphics.print(self.minuteHand:getTurns(), 10, 50)
-- end debug
love.graphics.push("all")
love.graphics.applyTransform(self.transform)
love.graphics.setColor(0.5, 0.5, 0.5)
love.graphics.circle("fill", 0, 0, self.radius)
self.minuteHand:draw()
love.graphics.pop()
end
local function clockContainsPoint(clock, x, y)
local localX, localY = clock.transform:inverseTransformPoint(x, y)
local dist = math.sqrt(localX^2 + localY^2)
return dist <= clock.radius
end
local function clockPointAt(clock, x, y)
local localX, localY = clock.transform:inverseTransformPoint(x, y)
local turns = math.atan2(localY, localX) / 2 / math.pi
clock.minuteHand:setTurns(turns)
end
function Clock:movemouse(x, y)
if self.dragging then
local localX, localY = self.transform:inverseTransformPoint(x, y)
clockPointAt(self, x, y)
-- TODO update clock hands
end
end
function Clock:pressmouse(x, y, button)
if not self.dragging and button == 1 and clockContainsPoint(self, x,y) then
self.dragging = true
clockPointAt(self, x, y)
-- TODO update clock hands
end
end
function Clock:releasemouse(button)
if button == 1 then self.dragging = false end
end
return Clock