info = { type = "image", title = "Sketch pad", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "A red drawing toy sketches a house, cat, hills", api = 33, } local tr = ({ sk = { ["Frame"] = "Rám", ["Speed"] = "Rýchlosť" }, cs = { ["Frame"] = "Rám", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "body", label = T("Frame"), type = "color", default = "#CE2C2C" }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend = gfx.rect, gfx.px, gfx.blend local floor, sin, max, min = math.floor, math.sin, math.max, math.min local W, H = gfx.W, gfx.H local DESK, SCREEN, LINE = { 34, 30, 46 }, { 178, 182, 174 }, { 58, 60, 66 } local SX, SY, SW, SH = 8, 10, 48, 34 local HOLD, SHAKE, BLANK, WIPE = 45, 40, 25, 24 local MOTIFS = { { 8, 30, 8, 16, 20, 4, 32, 16, 32, 30, 16, 30, 16, 22, 22, 22, 22, 30, 44, 30, 40, 30, 40, 22, 34, 16, 40, 10, 46, 16, 40, 22 }, { 14, 30, 14, 22, 18, 18, 18, 8, 22, 12, 26, 12, 30, 8, 30, 18, 34, 22, 34, 30, 14, 30, 38, 30, 42, 26, 42, 18, 38, 14 }, { 2, 30, 14, 18, 18, 22, 30, 10, 38, 18, 42, 14, 46, 18, 46, 30, 2, 30, 2, 12, 6, 8, 10, 12, 6, 16, 2, 12 }, } local SEG, LEN = {}, {} for m, p in ipairs(MOTIFS) do local s, n = {}, 0 for k = 3, #p - 1, 2 do local x0, y0, x1, y1 = p[k - 2], p[k - 1], p[k], p[k + 1] local L = max(math.abs(x1 - x0), math.abs(y1 - y0)) s[#s + 1] = { x0, y0, x1 > x0 and 1 or (x1 < x0 and -1 or 0), y1 > y0 and 1 or (y1 < y0 and -1 or 0), L, n } n = n + L end SEG[m], LEN[m] = s, n end local BG, BODY local DUR, TOTAL, PXF = {}, 0, 0.7 function on_settings() BODY = cfg.body or { r = 206, g = 44, b = 44 } BG = nil PXF = 0.4 + 0.1 * (cfg.speed or 3) TOTAL = 0 for m = 1, #MOTIFS do DUR[m] = floor(LEN[m] / PXF) + 1 + HOLD + SHAKE + BLANK TOTAL = TOTAL + DUR[m] end end local KW = { 4, 8, 8, 10, 10, 10, 10, 8, 8, 4 } local IW = { 4, 6, 6, 6, 6, 4 } local MARK = { { 7, 4 }, { 6, 6 }, { 4, 7 }, { 2, 6 }, { 1, 4 }, { 2, 2 }, { 4, 1 }, { 6, 2 } } local KX, KY = { 6, 48 }, 49 local function build_bg() local b = BODY rect(0, 0, W, H, DESK[1], DESK[2], DESK[3]) rect(2, 6, 60, 56, b.r, b.g, b.b) for _, c in ipairs({ { 2, 6 }, { 61, 6 }, { 2, 61 }, { 61, 61 } }) do px(c[1], c[2], DESK[1], DESK[2], DESK[3]) end rect(SX - 1, SY - 1, SW + 2, SH + 2, b.r * 6 // 10, b.g * 6 // 10, b.b * 6 // 10) rect(SX, SY, SW, SH, SCREEN[1], SCREEN[2], SCREEN[3]) for y = 51, 57, 3 do rect(24, y, 16, 1, b.r * 7 // 10, b.g * 7 // 10, b.b * 7 // 10) end for _, kx in ipairs(KX) do for r = 0, 9 do rect(kx + (10 - KW[r + 1]) // 2, KY + r, KW[r + 1], 1, 206, 198, 182) end for r = 2, 7 do rect(kx + (10 - IW[r - 1]) // 2, KY + r, IW[r - 1], 1, 240, 234, 220) end end BG = gfx.snapshot(0, 0, W, H) end local function trace(m, d, X, Y) local c = LINE local hx, hy for _, s in ipairs(SEG[m]) do local x0, y0, dx, dy, L = s[1], s[2], s[3], s[4], s[5] local l = d - s[6] if l < 0 then break end if l > L then l = L end local n = floor(l) hx, hy = x0 + dx * l, y0 + dy * l if dy == 0 then rect(X + min(x0, x0 + dx * n), Y + y0, n + 1, 1, c[1], c[2], c[3]) elseif dx == 0 then rect(X + x0, Y + min(y0, y0 + dy * n), 1, n + 1, c[1], c[2], c[3]) else for k = 0, n do px(X + x0 + dx * k, Y + y0 + dy * k, c[1], c[2], c[3]) end end if l < L then local f = l - n blend(X + x0 + dx * (n + 1), Y + y0 + dy * (n + 1), c[1], c[2], c[3], f) end end return hx, hy end local function knob(i, ph, ox, oy) local mk = MARK[ph % 8 + 1] rect(KX[i] + mk[1] + ox, KY + mk[2] + oy, 2, 2, 80, 70, 66) end function render() if not BG then build_bg() end local t = frame % TOTAL local m = 1 while t >= DUR[m] do t = t - DUR[m]; m = m + 1 end local Td = DUR[m] - HOLD - SHAKE - BLANK local ox, oy, d, wipe = 0, 0, 0, 0 if t < Td then d = t * PXF elseif t < Td + HOLD then d = LEN[m] elseif t < Td + HOLD + SHAKE then local u, k = t - Td - HOLD, 1 - (t - Td - HOLD) / SHAKE d = LEN[m] ox, oy = floor(2.2 * sin(u * 1.4) * k + 0.5), floor(1.6 * sin(u * 1.9 + 1) * k + 0.5) wipe = max(0, (u - (SHAKE - WIPE)) / WIPE) else d = -1 end gfx.clear(DESK[1], DESK[2], DESK[3]) gfx.blit_rgb(BG, ox, oy, W, H) local hx, hy = 0, 0 if d >= 0 then if wipe > 0 and wipe < 1 then gfx.clip(SX + ox, SY + oy + floor(wipe * SH), SW, SH - floor(wipe * SH)) end if wipe < 1 then hx, hy = trace(m, d, SX + ox, SY + oy) end gfx.clip() end hx, hy = hx or 0, hy or 0 knob(1, floor(hx * 0.67), ox, oy) knob(2, floor(hy * 0.67), ox, oy) end