Files
BlackAngelTV 0fc96b243a feat(demo): add a full-screen terminal paint editor
The repository only shipped a spreadsheet demo, so there was no example of an
interactive, stateful program. LuaPaint keeps the whole editor in Lua - canvas
primitives with undo, palette, tools and a scriptable session - and its tests
run without a terminal.
2026-11-18 10:18:44 +01:00

79 lines
2.1 KiB
Lua

-- A recorded drawing session: draws a small landscape with the real tools of
-- the application, so `luapaint demo` and the tests exercise the same code.
local palette = require("paint.palette")
local M = {}
local function set_cursor(app, x, y)
app.cursor_x = x
app.cursor_y = y
end
-- Two presses for anchor tools (line, rectangle, ellipse), one for the rest.
-- For an ellipse the first point is the centre and the second one a corner of
-- its bounding box, exactly like the interactive tool.
local function stroke(app, tool, colour_name, x1, y1, x2, y2, filled)
app.tool = tool
app.filled = filled == true
local slot = palette.slot_of(colour_name)
if slot ~= nil then
app.slot = slot
end
app.anchor_x = nil
app.anchor_y = nil
set_cursor(app, x1, y1)
app:handle(" ")
if x2 ~= nil then
set_cursor(app, x2, y2)
app:handle(" ")
end
end
local function star(app, x, y)
stroke(app, 1, "blanc", x, y)
end
function M.run(app, on_stage)
local width = app.canvas.width
local height = app.canvas.height
-- sky, stars and sun
stroke(app, 5, "nuit", 1, 1)
star(app, 5, 2)
star(app, 12, 4)
star(app, 27, 2)
star(app, 44, 6)
star(app, 55, 3)
stroke(app, 4, "jaune", 50, 4, 54, 6, true)
if on_stage ~= nil then
on_stage("ciel, etoiles et soleil")
end
-- ground, house and roof
stroke(app, 3, "vert", 1, height - 4, width, height, true)
stroke(app, 3, "brun", 10, 9, 26, height - 5, true)
for row = 3, 9 do
local half = math.floor((row - 3) * 1.5)
stroke(app, 3, "brun", 18 - half, row, 18 + half, row, true)
end
stroke(app, 3, "nuit", 16, height - 9, 20, height - 5, true)
stroke(app, 3, "cyan", 22, 11, 25, 13, true)
if on_stage ~= nil then
on_stage("maison, toit, porte et fenetre")
end
-- tree
stroke(app, 3, "brun", 33, height - 8, 34, height - 4, true)
stroke(app, 4, "vert", 34, 10, 39, 15, true)
if on_stage ~= nil then
on_stage("arbre et finitions")
end
app.message = "Paysage termine : w pour ecrire, u pour annuler, q pour quitter."
app.cursor_x = 18
app.cursor_y = 6
return app
end
return M