2024-04-12 03:42:11 +00:00
|
|
|
local Time = require"time"
|
2024-03-27 06:05:06 +00:00
|
|
|
|
2024-04-12 03:42:11 +00:00
|
|
|
local numberFont = love.graphics.newFont(100)
|
2024-03-27 06:05:06 +00:00
|
|
|
|
|
|
|
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
|
2024-03-27 03:57:50 +00:00
|
|
|
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),
|
2024-03-27 06:05:06 +00:00
|
|
|
color = (color or {1,1,1})
|
2024-03-24 22:26:07 +00:00
|
|
|
}
|
|
|
|
return setmetatable(h, self)
|
|
|
|
end
|
|
|
|
|
2024-03-27 06:05:06 +00:00
|
|
|
function Hand:draw(turns)
|
2024-03-24 22:26:07 +00:00
|
|
|
love.graphics.push("all")
|
|
|
|
love.graphics.setColor(unpack(self.color))
|
2024-03-27 06:05:06 +00:00
|
|
|
love.graphics.rotate(turnsToRads(turns) + 0.5 * math.pi)
|
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
|
|
|
|
2024-04-12 03:42:11 +00:00
|
|
|
local function drawFaceCanvas(r)
|
|
|
|
local oldCanvas = love.graphics.getCanvas()
|
|
|
|
local canvas = love.graphics.newCanvas(2*r,2*r)
|
|
|
|
love.graphics.setCanvas(canvas)
|
|
|
|
love.graphics.push("all")
|
|
|
|
love.graphics.translate(r, r)
|
|
|
|
love.graphics.setColor(0.93, 0.8, 0)
|
|
|
|
love.graphics.circle("fill", 0, 0, r)
|
|
|
|
love.graphics.setColor(1, 1, 1)
|
|
|
|
love.graphics.circle("fill", 0, 0, 0.84*r)
|
|
|
|
love.graphics.setColor(0,0,0)
|
|
|
|
for i=1,60 do
|
|
|
|
love.graphics.rotate(math.pi/30)
|
|
|
|
love.graphics.circle("fill", 0.6*r, 0, 0.01*r)
|
|
|
|
end
|
|
|
|
love.graphics.setColor(0.87,0,0)
|
|
|
|
for i=1,12 do
|
|
|
|
love.graphics.rotate(math.pi/6)
|
|
|
|
love.graphics.circle("fill", 0.6*r, 0, 0.015*r)
|
|
|
|
end
|
|
|
|
|
|
|
|
local font = love.graphics.newFont("font/JosefinSans-Regular.ttf", r/6)
|
|
|
|
love.graphics.setColor(0,0,0)
|
|
|
|
for i=1,12 do
|
|
|
|
local s = tostring(i)
|
|
|
|
local h = font:getHeight()
|
|
|
|
local w = font:getWidth(s)
|
|
|
|
local tr = love.math.newTransform(0,0,-math.pi/6*i+math.pi)
|
|
|
|
tr = tr:apply(love.math.newTransform(w*0.5, h*0.4))
|
|
|
|
local x, y = tr:inverseTransformPoint(0, 0.72*r)
|
|
|
|
love.graphics.printf(s, font, x, y, r)
|
|
|
|
end
|
|
|
|
love.graphics.pop()
|
|
|
|
love.graphics.setCanvas(oldCanvas)
|
|
|
|
return canvas
|
|
|
|
end
|
|
|
|
|
2024-03-27 03:57:50 +00:00
|
|
|
local Clock = {}
|
|
|
|
Clock.__index = Clock
|
|
|
|
|
2024-04-12 03:42:11 +00:00
|
|
|
function Clock:new(time, diameter)
|
|
|
|
local radius = diameter and diameter/2 or 300
|
|
|
|
local transform = love.math.newTransform(radius, radius)
|
2024-03-27 03:57:50 +00:00
|
|
|
local c = {
|
|
|
|
time = (time or Time:new()),
|
2024-03-27 06:05:06 +00:00
|
|
|
minuteHand = Hand:new(240, {1,0,0}),
|
|
|
|
hourHand = Hand:new(180, {0,0.5,0.5}),
|
2024-03-27 03:57:50 +00:00
|
|
|
dragging = false,
|
2024-04-12 03:42:11 +00:00
|
|
|
radius = radius,
|
|
|
|
onSetHands = nil,
|
|
|
|
transform = transform,
|
|
|
|
faceCanvas = drawFaceCanvas(radius)
|
2024-03-27 03:57:50 +00:00
|
|
|
}
|
|
|
|
return setmetatable(c, self)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Clock:setTime(time)
|
|
|
|
self.time = time
|
|
|
|
end
|
|
|
|
|
|
|
|
function Clock:getTime()
|
|
|
|
return self.time
|
|
|
|
end
|
|
|
|
|
|
|
|
function Clock:draw()
|
2024-03-27 06:05:06 +00:00
|
|
|
local _, m = self.time:get()
|
|
|
|
local h = self.time:getHoursSinceTwelve()
|
2024-03-27 03:57:50 +00:00
|
|
|
love.graphics.push("all")
|
2024-04-12 03:42:11 +00:00
|
|
|
love.graphics.draw(self.faceCanvas)
|
2024-03-27 03:57:50 +00:00
|
|
|
love.graphics.applyTransform(self.transform)
|
2024-03-27 06:05:06 +00:00
|
|
|
self.minuteHand:draw(m / 60)
|
|
|
|
self.hourHand:draw(h / 12)
|
2024-03-27 03:57:50 +00:00
|
|
|
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)
|
2024-03-27 06:05:06 +00:00
|
|
|
local minutes = pointToTurns(localX, localY) * 60
|
|
|
|
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)
|
2024-04-12 03:42:11 +00:00
|
|
|
if clock.onSetHands then clock.onSetHands(clock.time) end
|
2024-03-27 03:57:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Clock:movemouse(x, y)
|
|
|
|
if self.dragging then
|
|
|
|
local localX, localY = self.transform:inverseTransformPoint(x, y)
|
|
|
|
clockPointAt(self, x, y)
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Clock:releasemouse(button)
|
|
|
|
if button == 1 then self.dragging = false end
|
|
|
|
end
|
|
|
|
|
|
|
|
return Clock
|