Serge Bazanski | 18c1a26 | 2022-07-07 14:24:53 +0200 | [diff] [blame] | 1 | local node = Node:extend('nodes.countdown', { |
| 2 | target = 1498780800, |
| 3 | description = 'to get the fuck out of here', |
| 4 | precision = 3, |
| 5 | }) |
| 6 | |
| 7 | local textFont = love.graphics.newFont('fonts/Lato-Thin.ttf', 100) |
| 8 | local smallFont = love.graphics.newFont('fonts/Lato-Light.ttf', 60) |
| 9 | |
| 10 | function node:init(config) |
| 11 | node.super.init(self, config) |
| 12 | end |
| 13 | |
| 14 | function timefmt(time, precision) |
| 15 | precision = precision or 3 |
| 16 | |
| 17 | local p = { |
| 18 | {60, "seconds"}, |
| 19 | {60, "minutes"}, |
| 20 | {24, "hours"}, |
| 21 | {7, "days"}, |
| 22 | {nil, "weeks"}, |
| 23 | } |
| 24 | local parts = {} |
| 25 | local v |
| 26 | for i, e in ipairs(p) do |
| 27 | if e[1] == nil then |
| 28 | v = time |
| 29 | else |
| 30 | v = time % e[1] |
| 31 | time = math.floor(time / e[1]) |
| 32 | end |
| 33 | |
| 34 | if v ~= 0 then |
| 35 | table.insert(parts, 1, tostring(v) .. " " .. e[2]) |
| 36 | end |
| 37 | end |
| 38 | |
| 39 | return table.concat(lume.slice(parts, 1, precision), " ") |
| 40 | end |
| 41 | |
| 42 | function node:render() |
| 43 | love.graphics.setColor( 0, 0, 0 ) |
| 44 | love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight()) |
| 45 | |
| 46 | love.graphics.setColor( 1.0, 1.0, 1.0 ) |
| 47 | |
| 48 | love.graphics.setFont(textFont); |
| 49 | love.graphics.printf(timefmt(math.abs(self.target - os.time()), self.precision), 0, 0.3*love.graphics.getHeight(), love.graphics.getWidth(), 'center'); |
| 50 | |
| 51 | love.graphics.setFont(smallFont); |
| 52 | love.graphics.printf(self.description, 0, 0.7*love.graphics.getHeight(), love.graphics.getWidth(), 'center'); |
| 53 | end |
| 54 | |
| 55 | return node |