info = { type = "image", title = "Typewriter line", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "A typewriter types a short letter, line by line", api = 33, } local tr = ({ sk = { ["Machine"] = "Stroj", ["Speed"] = "Rýchlosť", ["Black"] = "Čierny", ["Mint"] = "Mätový", ["Red"] = "Červený" }, cs = { ["Machine"] = "Stroj", ["Speed"] = "Rychlost", ["Black"] = "Černý", ["Mint"] = "Mátový", ["Red"] = "Červený" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "machine", label = T("Machine"), type = "int", default = "black", opts = { keys = { "black", "mint", "red" }, T("Black"), T("Mint"), T("Red") } }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 4, default = 2 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, cos, max, min = math.floor, math.sin, math.cos, math.max, math.min local W, H = gfx.W, gfx.H local LOOKS = { black = { body = { 38, 38, 46 }, hi = { 92, 92, 106 }, dark = { 14, 14, 20 }, key = { 168, 168, 180 }, roll = { 22, 22, 28 }, wall = { 36, 108, 88 }, wall2 = { 28, 90, 72 } }, mint = { body = { 96, 198, 172 }, hi = { 172, 240, 216 }, dark = { 40, 112, 98 }, key = { 236, 248, 240 }, roll = { 40, 98, 90 }, wall = { 78, 54, 102 }, wall2 = { 64, 44, 86 } }, red = { body = { 188, 50, 56 }, hi = { 238, 112, 106 }, dark = { 100, 24, 34 }, key = { 242, 228, 214 }, roll = { 72, 22, 32 }, wall = { 44, 86, 114 }, wall2 = { 36, 72, 98 } }, } local L = LOOKS.black local INK, PAPER, PAPER_D = { 36, 38, 54 }, { 240, 238, 228 }, { 214, 212, 200 } local STEEL = { 196, 202, 214 } local PAGES = { { "DEAR", "FRAME,", "I TYPED", "THIS FOR", "YOU." }, { "TICK TAK", "GOES THE", "CARRIAGE", "AND THE", "BELL!" }, { "STACK", "SATS,", "DRINK", "TEA,", "SMILE!" }, } local OPTS = { font = "5x7" } local ADV = gfx.font("5x7").adv local PW = 8 * ADV + 4 local SH = 6 local LINE_Y, LINE_DY = 21, 8 local ROLL_Y = 29 local CR, PAUSE, PAUSE_LAST, EJECT, FEED = 24, 10, 36, 22, 22 local SEG, TOT, TC = {}, 1, 7 local function build_timeline() TC = ({ 10, 7, 5, 3 })[cfg.speed or 2] or 7 SEG, TOT = {}, 0 for p = 1, #PAGES do local lines = PAGES[p] for l = 1, #lines do local len = #lines[l] local last = l == #lines local tail = last and (PAUSE_LAST + EJECT + FEED) or (PAUSE + CR) SEG[#SEG + 1] = { t0 = TOT, p = p, l = l, len = len, last = last, tt = len * TC } TOT = TOT + len * TC + tail end end end local BG local function build_bg() local w, w2 = L.wall, L.wall2 rect(0, 0, W, H, w[1], w[2], w[3]) for y = 4, H, 8 do for x = (y // 8 % 2) * 4, W, 8 do rect(x - 1, y, 3, 1, w2[1], w2[2], w2[3]) rect(x, y - 1, 1, 3, w2[1], w2[2], w2[3]) end end rect(0, 57, W, 7, 112, 88, 64) rect(0, 57, W, 1, 150, 122, 90) rect(0, 62, W, 2, 88, 68, 50) for x = 7, W, 16 do rect(x, 58, 1, 4, 98, 76, 56) end local b, h, d = L.body, L.hi, L.dark for y = 34, 56 do local hw = floor(21 + (y - 34) * 0.55) rect(32 - hw, y, 2 * hw, 1, b[1], b[2], b[3]) rect(32 - hw, y, 1, 1, h[1], h[2], h[3]) rect(31 + hw, y, 1, 1, d[1], d[2], d[3]) end rect(0, 56, W, 1, d[1], d[2], d[3]) rect(0, 33, W, 1, h[1], h[2], h[3]) rect(18, 36, 28, 8, d[1], d[2], d[3]) rect(20, 35, 24, 1, d[1], d[2], d[3]) for _, sx in ipairs({ 9, 55 }) do rect(sx - 2, 37, 5, 3, d[1], d[2], d[3]) rect(sx - 1, 36, 3, 5, d[1], d[2], d[3]) px(sx, 38, h[1], h[2], h[3]) end rect(14, 45, 36, 9, d[1], d[2], d[3]) rect(20, 53, 24, 1, L.key[1], L.key[2], L.key[3]) for r = 0, 2 do local n = 10 - r for k = 0, n - 1 do rect(32 - n * 2 + k * 4 + 1, 46 + r * 2 + (r > 0 and 0 or 0), 2, 1, L.key[1], L.key[2], L.key[3]) end end rect(58, 33, 4, 2, 214, 170, 64) BG = gfx.snapshot(0, 0, W, H) end function on_settings() L = LOOKS[cfg.machine] or LOOKS.black BG = nil build_timeline() end local function ease(q) return q * q * (3 - 2 * q) end local function keycap(ch) local r = ch % 3 local n = 10 - r return 32 - n * 2 + (ch * 7 % n) * 4 + 1, 46 + r * 2 end local function arm(ch, tx, p) local bx = 32 + (tx - 32) * 0.5 + ch % 5 - 2 local ax, ay = bx + (tx - bx) * p, 43 - 9 * p local n = 9 for s = 0, n do local u = s / n px(floor(bx + (ax - bx) * u + 0.5), floor(43 + (ay - 43) * u + 0.5), STEEL[1], STEEL[2], STEEL[3]) end rect(floor(ax + 0.5) - 1, floor(ay + 0.5) - 1, 3, 1, L.key[1], L.key[2], L.key[3]) end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame % TOT local si = 1 for i = #SEG, 1, -1 do if SEG[i].t0 <= t then si = i; break end end local s = SEG[si] local k = t - s.t0 local lines = PAGES[s.p] local ln = lines[s.l] local n, hit, sub = 0, nil, 0 local roll, shift, pl_bot, pl_top = 0, 0, ROLL_Y, 0 local bell = false local lift = 0 if k < s.tt then local i = k // TC sub = k % TC n = i + (sub >= 2 and 1 or 0) hit = i + 1 shift = SH * (i + (sub >= 2 and 1 or 0)) / 8 bell = s.len - i <= 2 else n = s.len local kk = k - s.tt shift = SH * s.len / 8 if not s.last then if kk < PAUSE then bell = true else local q = ease(min(1, (kk - PAUSE) / CR)) roll = 8 * q shift = shift * (1 - q) end elseif kk < PAUSE_LAST then bell = kk < 12 else local q = ease(min(1, (kk - PAUSE_LAST) / EJECT)) shift = shift * (1 - q) if kk < PAUSE_LAST + EJECT then lift = 34 * q else n = 0 lift = -1 pl_top = ROLL_Y - 29 * ease(min(1, (kk - PAUSE_LAST - EJECT) / FEED)) end end end local pl = 8 - floor(shift + 0.5) if lift >= 0 then local top = 0 local bot = ROLL_Y - floor(lift) if bot > 0 then rect(pl, top, PW, bot - top, PAPER[1], PAPER[2], PAPER[3]) end if bot > 2 then rect(pl + PW - 1, top, 1, bot - top, PAPER_D[1], PAPER_D[2], PAPER_D[3]) rect(pl, max(top, bot - 2), PW, min(2, bot), PAPER_D[1], PAPER_D[2], PAPER_D[3]) end for j = 1, s.l do local y = LINE_Y - LINE_DY * (s.l - j) - floor(roll + 0.5) - floor(lift) if y > -8 and y < ROLL_Y then local txt = j < s.l and lines[j] or ln:sub(1, n) if #txt > 0 then gfx.text(pl + 2, y, txt, INK[1], INK[2], INK[3], OPTS) end end end else local top = floor(pl_top) rect(pl, top, PW, ROLL_Y - top, PAPER[1], PAPER[2], PAPER[3]) rect(pl + PW - 1, top, 1, ROLL_Y - top, PAPER_D[1], PAPER_D[2], PAPER_D[3]) rect(pl, ROLL_Y - 2, PW, 2, PAPER_D[1], PAPER_D[2], PAPER_D[3]) end local rl = L.roll rect(0, ROLL_Y, W, 1, L.hi[1], L.hi[2], L.hi[3]) rect(0, ROLL_Y + 1, W, 3, rl[1], rl[2], rl[3]) rect(0, ROLL_Y + 4, W, 1, L.dark[1], L.dark[2], L.dark[3]) for x = -(floor(shift + 0.5) % 6), W, 6 do rect(x, ROLL_Y + 2, 1, 1, L.hi[1], L.hi[2], L.hi[3]) end local la = 2.2 if k >= s.tt + PAUSE and not s.last then la = 2.2 - 0.9 * sin(3.14159 * min(1, (k - s.tt - PAUSE) / CR)) end for r = 1, 7 do px(floor(7 + r * cos(la) + 0.5), floor(ROLL_Y + 1 - r * sin(la) + 0.5), STEEL[1], STEEL[2], STEEL[3]) end if hit then for j = 0, 1 do local i, kk = hit - 1 - j, sub + j * TC if i >= 0 and kk < 4 then local ch = ln:byte(i + 1) if ch ~= 32 then local p = kk <= 2 and kk / 2 or (4 - kk) / 2 local tx = 8 - floor(SH * i / 8 + 0.5) + 2 + ADV * i + 2 arm(ch, tx, p) if p > 0.9 then local kx, ky = keycap(ch) rect(kx, ky, 2, 2, L.dark[1], L.dark[2], L.dark[3]) end end end end end if bell and (t // 2) % 2 == 0 then px(57, 31, 255, 222, 120); px(63 - 1, 31, 255, 222, 120) px(56, 29, 255, 222, 120); px(60, 28, 255, 222, 120) end end