147 lines
3.5 KiB
Lua
147 lines
3.5 KiB
Lua
local MAX_MINUTES = 60 * 12
|
|
|
|
local Time = {}
|
|
Time.__index = Time
|
|
|
|
local function newTimeRaw(rawMinutes)
|
|
local t = {
|
|
rawminutes = rawMinutes % MAX_MINUTES
|
|
}
|
|
return setmetatable(t, Time)
|
|
end
|
|
|
|
function Time:new(hour, minute)
|
|
return newTimeRaw((hour or 12) % 12 * 60 + (minute or 0))
|
|
end
|
|
|
|
function Time:get()
|
|
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
|
|
|
|
function Time:fmt()
|
|
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
|
|
|
|
|
|
local Hand = {}
|
|
Hand.__index = Hand
|
|
|
|
function Hand:new(length, color)
|
|
local h = {
|
|
length = (length or 10),
|
|
color = (color or {1,1,1})
|
|
}
|
|
return setmetatable(h, self)
|
|
end
|
|
|
|
function Hand:draw(turns)
|
|
love.graphics.push("all")
|
|
love.graphics.setColor(unpack(self.color))
|
|
love.graphics.rotate(turnsToRads(turns) + 0.5 * math.pi)
|
|
love.graphics.rectangle("fill", -10, 10, 20, -self.length)
|
|
love.graphics.pop()
|
|
end
|
|
|
|
|
|
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}),
|
|
hourHand = Hand:new(180, {0,0.5,0.5}),
|
|
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()
|
|
local _, m = self.time:get()
|
|
local h = self.time:getHoursSinceTwelve()
|
|
|
|
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(m / 60)
|
|
self.hourHand:draw(h / 12)
|
|
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 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)
|
|
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 |