info = { type = "app", title = "Flip", author = "BRRRO", license = "MIT", desc = "Split-flap clock, like an airport board", category = "time", api = 42, } local W, H = gfx.W, gfx.H local clear, rect, round_rect, clip = gfx.clear, gfx.rect, gfx.round_rect, gfx.clip local blend_rect = gfx.blend_rect local text, text_bounds, shade = gfx.text, gfx.text_bounds, gfx.shade local fmt, floor, gmatch, min = string.format, math.floor, string.gmatch, math.min txt = { notime = "NO TIME", ink = "Own digit colour", card = "Own card colour", own_h = "Picking a colour switches the look to own colours; digits lighten or darken to stay readable", look = "Look", look_o = { "orange retro", "red", "teal", "cream", "airport", "dark", "own colours" }, under = "Under the clock", under_o = { "day and date in turn", "date", "day of week", "seconds", "nothing" }, anim = "Changing a digit", anim_o = { "flap falls", "rolls over", "just changes" }, days = { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" } } for k, v in pairs(({ sk = { notime = "BEZ ČASU", ink = "Vlastná farba číslic", card = "Vlastná farba kariet", own_h = "Výberom farby sa vzhľad prepne na vlastné farby; číslice sa zosvetlia alebo stmavia, aby boli čitateľné", look = "Vzhľad", look_o = { "oranžový retro", "červený", "tyrkysový", "krémový", "letisko", "tmavý", "vlastné farby" }, under = "Pod hodinami", under_o = { "deň a dátum striedavo", "dátum", "deň v týždni", "sekundy", "nič" }, anim = "Zmena číslice", anim_o = { "klapka padne", "pretočí sa", "len sa zmení" }, days = { "NEDEĽA", "PONDELOK", "UTOROK", "STREDA", "ŠTVRTOK", "PIATOK", "SOBOTA" } }, cs = { notime = "BEZ ČASU", ink = "Vlastní barva číslic", card = "Vlastní barva karet", own_h = "Výběrem barvy se vzhled přepne na vlastní barvy; číslice se zesvětlí nebo ztmaví, aby byly čitelné", look = "Vzhled", look_o = { "oranžový retro", "červený", "tyrkysový", "krémový", "letiště", "tmavý", "vlastní barvy" }, under = "Pod hodinami", under_o = { "den a datum střídavě", "datum", "den v týdnu", "sekundy", "nic" }, anim = "Změna číslice", anim_o = { "klapka spadne", "přetočí se", "jen se změní" }, days = { "NEDĚLE", "PONDĚLÍ", "ÚTERÝ", "STŘEDA", "ČTVRTEK", "PÁTEK", "SOBOTA" } }, })[lang] or {}) do txt[k] = v end settings = { { key = "look", type = "int", default = "orange", label = txt.look, opts = { keys = { "orange", "red", "teal", "cream", "airport", "dark", "own" }, table.unpack(txt.look_o) } }, { key = "under", type = "int", default = "alt", label = txt.under, opts = { keys = { "alt", "date", "wday", "secs", "none" }, table.unpack(txt.under_o) } }, { key = "anim", type = "int", default = "flap", label = txt.anim, opts = { keys = { "flap", "roll", "none" }, table.unpack(txt.anim_o) } }, { key = "ink", type = "color", default = "#F2F2E8", label = txt.ink, hint = txt.own_h, also = { key = "look", value = "own" } }, { key = "card", type = "color", default = "#2B3040", label = txt.card, hint = txt.own_h, also = { key = "look", value = "own" } }, } local function kind(w, h, font, r) local cap = gfx.font(font).cap return { w = w, h = h, f = { font = font }, r = r, mid = h // 2, gy = (h - cap) // 2, top_h = h // 2 - 1, bot_y = h // 2 + 1, bot_h = h - 2 - h // 2, dx = {} } end local BIG = kind(14, 25, "12x24", 2) local MINI = kind(5, 9, "4x6", 1) local MGAP = 0 do local lo, hi = 99, -99 for d = 0, 9 do local l, h = text_bounds(tostring(d), BIG.f) if l < lo then lo = l end if h > hi then hi = h end end local dx = (BIG.w - (hi - lo + 1)) // 2 - lo for d = 0, 9 do BIG.dx[tostring(d)] = dx end end local function glyph_dx(K, ch) local dx = K.dx[ch] if not dx then local lo, hi = text_bounds(ch, K.f) dx = hi < lo and 0 or (K.w - (hi - lo + 1)) // 2 - lo K.dx[ch] = dx end return dx end local GAP = 1 local COLON_W = 4 local PAIR_W = 2 * BIG.w + GAP local X0 = (W - (2 * PAIR_W + COLON_W)) // 2 local CX = { X0, X0 + BIG.w + GAP, X0 + PAIR_W + COLON_W, X0 + PAIR_W + COLON_W + BIG.w + GAP } local COLON_X = X0 + PAIR_W + (COLON_W - 2) // 2 local Y0, MY local ONE_S = 0.33 local SEC_S = 0.25 local FAST_S = 0.06 local MAXF = 12 local STEP_S = 0.11 local ALT_S = 6 local function seq_of(list) local s = { list = list, at = {}, blank = string.rep(" ", #list[1]) } for i, v in ipairs(list) do s.at[v] = i end return s end local function range(a, b, f) local l = {} for v = a, b do l[#l + 1] = fmt(f, v) end return l end local H24, H12 = seq_of(range(0, 23, "%02d")), seq_of(range(1, 12, "%2d")) local TENS, ONES = seq_of(range(0, 5, "%d")), seq_of(range(0, 9, "%d")) local CHARS = {} for c in gmatch(" 0123456789.AÁÄBCČDĎEÉĚFGHIÍJKLĹĽMNŇOÓÔPQRŘSŠTŤUÚŮVWXYÝZŽ", utf8.charpattern) do CHARS[#CHARS + 1] = c end CHARS = seq_of(CHARS) local function drum(S, ia, ib, dt, one) local L, N = S.list, #S.list local n = ia == 0 and ib or (ib - ia) % N if n > MAXF then n = MAXF end if n == 0 or dt >= n * (n == 1 and one or FAST_S) then return L[ib] end if dt < 0 then return ia == 0 and S.blank or L[ia] end local p = n == 1 and one or FAST_S local k = floor(dt / p) local function card(j) if j == 0 then return ia == 0 and S.blank or L[ia] end return L[(ib - 1 - (n - j)) % N + 1] end return card(k), card(k + 1), dt / p - k end local ink, card, top_c, seam_c, notch_c, sub_c local LOOKS = { orange = { body = { 232, 104, 28 }, card = { 236, 228, 208 }, ink = { 30, 30, 30 } }, red = { body = { 206, 44, 40 }, card = { 28, 28, 28 }, ink = { 244, 240, 230 } }, teal = { body = { 0, 150, 140 }, card = { 242, 238, 226 }, ink = { 32, 32, 32 } }, cream = { body = { 228, 220, 200 }, card = { 32, 32, 32 }, ink = { 248, 244, 234 } }, airport = { card = { 22, 22, 22 }, ink = { 255, 197, 46 } }, dark = { card = { 43, 48, 64 }, ink = { 242, 242, 232 } }, } local BODY local BODY_Y0, BODY_Y1 = 2, 56 local BODY_BG local function mixc(c, k) local function ch(v) v = floor(v * k) return v < 0 and 0 or (v > 255 and 255 or v) end return { ch(c[1]), ch(c[2]), ch(c[3]) } end local function lerp(a, b, k) return floor(a[1] + (b[1] - a[1]) * k), floor(a[2] + (b[2] - a[2]) * k), floor(a[3] + (b[3] - a[3]) * k) end function on_settings() local L = LOOKS[cfg.look] BODY = L and L.body local c, i = cfg.card, cfg.ink card = L and L.card or { c.r, c.g, c.b } top_c = mixc(card, 1.35) seam_c = mixc(card, 0.45) notch_c = mixc(card, 0.25) local r, g, b = i.r, i.g, i.b if L then r, g, b = L.ink[1], L.ink[2], L.ink[3] end ink = { shade(top_c[1], top_c[2], top_c[3], r, g, b) } sub_c = { shade(top_c[1], top_c[2], top_c[3], floor(r * 0.8), floor(g * 0.8), floor(b * 0.8)) } if BODY then Y0 = cfg.under ~= "none" and 8 or (BODY_Y0 + BODY_Y1 - BIG.h) // 2 MY = Y0 + BIG.h + 6 else local rows = cfg.under ~= "none" and BIG.h + 3 + MINI.h or BIG.h Y0 = (H - rows) // 2 MY = Y0 + BIG.h + 3 end BODY_BG = nil end local function draw_card(K, x, y) round_rect(x, y, K.w, K.h, card[1], card[2], card[3], card[1], card[2], card[3], K.r) rect(x + 1, y + 1, K.w - 2, K.top_h, top_c[1], top_c[2], top_c[3]) end local SEAM_A = 0.55 local function draw_seam(K, x, y) blend_rect(x, y + K.mid, K.w, 1, seam_c[1], seam_c[2], seam_c[3], SEAM_A) if K == BIG then rect(x, y + K.mid - 1, 1, 3, notch_c[1], notch_c[2], notch_c[3]) rect(x + K.w - 1, y + K.mid - 1, 1, 3, notch_c[1], notch_c[2], notch_c[3]) end end local function draw_glyph(K, x, y, ch, dy, r, g, b) if ch ~= " " then text(x + glyph_dx(K, ch), y + K.gy + dy, ch, r, g, b, K.f) end end local function draw_flap(K, x, y, a, b, u, c) local ix, iw = x + 1, K.w - 2 clip(ix, y + 1, iw, K.top_h) rect(ix, y + 1, iw, K.top_h, top_c[1], top_c[2], top_c[3]) draw_glyph(K, x, y, b, 0, c[1], c[2], c[3]) clip(ix, y + K.bot_y, iw, K.bot_h) rect(ix, y + K.bot_y, iw, K.bot_h, card[1], card[2], card[3]) draw_glyph(K, x, y, a, 0, c[1], c[2], c[3]) if u < 0.5 then local h = floor((1 - u * 2) * K.top_h + 0.5) if h > 0 then local k = u * 2 * 0.6 clip(ix, y + K.mid - h, iw, h) rect(ix, y + K.mid - h, iw, h, lerp(top_c, seam_c, k)) draw_glyph(K, x, y, a, 0, lerp(c, seam_c, k)) end else local h = floor((u - 0.5) * 2 * K.bot_h + 0.5) if h > 0 then local k = (u - 0.5) * 2 clip(ix, y + K.bot_y, iw, h) rect(ix, y + K.bot_y, iw, h, lerp(notch_c, card, k * 0.8)) draw_glyph(K, x, y, b, 0, lerp(seam_c, c, 0.4 + k * 0.6)) end end clip() end local function draw_roll(K, x, y, a, b, u, c) clip(x + 1, y + 1, K.w - 2, K.h - 2) draw_glyph(K, x, y, a, -floor(u * K.h + 0.5), c[1], c[2], c[3]) draw_glyph(K, x, y, b, floor((1 - u) * K.h + 0.5), c[1], c[2], c[3]) clip() end local function draw_slot(K, x, y, a, b, u, c) draw_card(K, x, y) if b == nil then clip(x + 1, y + 1, K.w - 2, K.h - 2) draw_glyph(K, x, y, a, 0, c[1], c[2], c[3]) clip() elseif cfg.anim == "roll" then draw_roll(K, x, y, a, b, u, c) else draw_flap(K, x, y, a, b, u, c) end draw_seam(K, x, y) end local function draw_colon() local m, c = Y0 + BIG.mid, BODY or ink rect(COLON_X, m - 6, 2, 2, c[1], c[2], c[3]) rect(COLON_X, m + 5, 2, 2, c[1], c[2], c[3]) end local function drum_at(S, ib, ia, dt, one, arr, delay) if cfg.anim == "none" then return S.list[ib] end local n = ib > MAXF and MAXF or ib local ad = arr - delay if ad < n * (n == 1 and one or FAST_S) then return drum(S, 0, ib, ad, one) end if ia then return drum(S, ia, ib, dt, one) end return S.list[ib] end local function cells(s, n) local out = {} for c in gmatch(s, utf8.charpattern) do out[#out + 1] = CHARS.at[c] and c or " " end local pad = (n - #out) // 2 for i = #out, 1, -1 do out[i + pad] = out[i] end for i = 1, pad do out[i] = " " end for i = #out + 1, n do out[i] = " " end return out end local WDAY_N = 0 for _, d in ipairs(txt.days) do WDAY_N = math.max(WDAY_N, utf8.len(d)) end WDAY_N = WDAY_N + WDAY_N % 2 local function under_n() local u = cfg.under return (u == "alt" or u == "date") and 10 or (u == "wday" and WDAY_N or 2) end local function build_body(c) clear(0, 0, 0) for y = BODY_Y0, BODY_Y1 do local k = 1.12 - 0.3 * (y - BODY_Y0) / (BODY_Y1 - BODY_Y0) local inset = (y == BODY_Y0 or y == BODY_Y1) and 2 or ((y == BODY_Y0 + 1 or y == BODY_Y1 - 1) and 1 or 0) rect(inset, y, W - 2 * inset, 1, floor(min(255, c[1] * k)), floor(min(255, c[2] * k)), floor(min(255, c[3] * k))) end blend_rect(2, BODY_Y0, W - 4, 1, 255, 255, 255, 0.35) local function window(x, y, w, h) rect(x, y, w, h, 18, 16, 15) rect(x, y, w, 1, 6, 5, 5) blend_rect(x, y + h, w, 1, 255, 255, 255, 0.25) end window(0, Y0 - 2, W, BIG.h + 4) if cfg.under ~= "none" then local w = under_n() * MINI.w window((W - w) // 2 - 2, MY - 2, w + 4, MINI.h + 4) end for _, x in ipairs({ 6, W - 15 }) do rect(x, BODY_Y1 + 1, 9, 1, 128, 120, 112) rect(x, BODY_Y1 + 2, 9, 1, 84, 78, 72) end BODY_BG = gfx.snapshot(0, 0, W, H) end local function draw_under(t, arr, e) local u = cfg.under if u == "none" then return end local s, n local tday = (t.h * 60 + t.m) * 60 + e if u == "alt" then local day, date = txt.days[(t.wday % 7) + 1], fmt("%02d.%02d.%d", t.day, t.month, t.year) local k = floor(tday / ALT_S) local now, was = cells(k % 2 == 0 and day or date, 10), cells(k % 2 == 0 and date or day, 10) local x0 = (W - (10 * (MINI.w + MGAP) - MGAP)) // 2 for i = 1, 10 do local ib, ia = CHARS.at[now[i]], CHARS.at[was[i]] local a, b, q = drum_at(CHARS, ib, ia ~= ib and ia or nil, tday - k * ALT_S - 0.05 * i, ONE_S, min(arr, tday), 0.15 + 0.05 * i) draw_slot(MINI, x0 + (i - 1) * (MINI.w + MGAP), MY, a, b, q, sub_c) end return end if u == "date" then s, n = fmt("%02d.%02d.%d", t.day, t.month, t.year), 10 elseif u == "wday" then s, n = txt.days[(t.wday % 7) + 1], WDAY_N else s, n = fmt("%02d", t.s), 2 end local cs = cells(s, n) local x0 = (W - (n * (MINI.w + MGAP) - MGAP)) // 2 for i = 1, n do local x, a, b, k = x0 + (i - 1) * (MINI.w + MGAP), nil, nil, nil if u == "secs" then local S, v = i == 1 and TENS or ONES, i == 1 and t.s // 10 or t.s % 10 local changed = i == 2 or t.s % 10 == 0 a, b, k = drum_at(S, v + 1, changed and ((v - 1) % #S.list + 1) or nil, t.ms / 1000, SEC_S, arr, 0.15 + 0.05 * i) else a, b, k = drum_at(CHARS, CHARS.at[cs[i]], nil, 0, ONE_S, min(arr, tday), 0.15 + 0.05 * i) end draw_slot(MINI, x, MY, a, b, k, sub_c) end end function render() if BODY then if not BODY_BG then build_body(BODY) end gfx.blit_rgb(BODY_BG, 0, 0, W, H) else clear(0, 0, 0) end local t = data.time if not (t and t.ok) then for i = 1, 4 do draw_card(BIG, CX[i], Y0); draw_seam(BIG, CX[i], Y0) end draw_colon() gfx.text_center(MY + 1, txt.notime, sub_c[1], sub_c[2], sub_c[3], MINI.f) return end local arr, e = data.arrive and frame / gfx.FPS or 3600, t.s + t.ms / 1000 local HS = t.fmt12 and H12 or H24 local hb = t.fmt12 and t.h12 or t.h + 1 local ha = t.m == 0 and ((hb - 2) % #HS.list + 1) or nil local a, b, u = drum_at(HS, hb, ha, e, ONE_S, arr, 0) for i = 1, 2 do draw_slot(BIG, CX[i], Y0, a:sub(i, i), b and b:sub(i, i), u, ink) end local tb, ob = t.m // 10 + 1, t.m % 10 + 1 a, b, u = drum_at(TENS, tb, t.m % 10 == 0 and ((tb - 2) % 6 + 1) or nil, e, ONE_S, arr, 2 * STEP_S) draw_slot(BIG, CX[3], Y0, a, b, u, ink) a, b, u = drum_at(ONES, ob, (ob - 2) % 10 + 1, e, ONE_S, arr, 3 * STEP_S) draw_slot(BIG, CX[4], Y0, a, b, u, ink) draw_colon() draw_under(t, arr, e) end