summaryrefslogtreecommitdiffstats
path: root/nodes/countdown.lua
blob: 19cc965d61f0162d9100441cb0666c4d2d9d2db3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
local node = Node:extend('nodes.countdown', {
  target = 1498780800,
  description = 'to get the fuck out of here',
  precision = 3,
})

local textFont = love.graphics.newFont('fonts/Lato-Thin.ttf', 100)
local smallFont = love.graphics.newFont('fonts/Lato-Light.ttf', 60)

function node:init(config)
  node.super.init(self, config)
end

function timefmt(time, precision)
  precision = precision or 3

  local p = {
    {60, "seconds"},
    {60, "minutes"},
    {24, "hours"},
    {7, "days"},
    {nil, "weeks"},
  }
  local parts = {}
  local v
  for i, e in ipairs(p) do
    if e[1] == nil then
      v = time
    else
      v = time % e[1]
      time = math.floor(time / e[1])
    end

    if v ~= 0 then
      table.insert(parts, 1, tostring(v) .. " " .. e[2])
    end
  end

  return table.concat(lume.slice(parts, 1, precision), " ")
end

function node:render()
  love.graphics.setColor( 0, 0, 0 )
  love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())

  love.graphics.setColor( 255, 255, 255 )

  love.graphics.setFont(textFont);
  love.graphics.printf(timefmt(math.abs(self.target - os.time()), self.precision), 0, 0.3*love.graphics.getHeight(), love.graphics.getWidth(), 'center');

  love.graphics.setFont(smallFont);
  love.graphics.printf(self.description, 0, 0.7*love.graphics.getHeight(), love.graphics.getWidth(), 'center');
end

return node