Files
Lua-compilateur/Demo/paint/canvas.lua
T
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

297 lines
6.6 KiB
Lua

-- Pixel grid with the drawing primitives of the paint application:
-- Bresenham lines, rectangles, ellipses, flood fill and an undo/redo stack.
local M = {}
local Canvas = {}
Canvas.__index = Canvas
local HISTORY_LIMIT = 32
function M.new(width, height)
local canvas = setmetatable({
width = width,
height = height,
cells = {},
version = 0,
history = {},
future = {},
}, Canvas)
for index = 1, width * height do
canvas.cells[index] = 0
end
return canvas
end
function Canvas:in_bounds(x, y)
return x >= 1 and y >= 1 and x <= self.width and y <= self.height
end
function Canvas:index(x, y)
if not self:in_bounds(x, y) then
return nil
end
return (y - 1) * self.width + x
end
function Canvas:get(x, y)
local index = self:index(x, y)
if index == nil then
return 0
end
return self.cells[index]
end
function Canvas:set(x, y, colour)
local index = self:index(x, y)
if index ~= nil and self.cells[index] ~= colour then
self.cells[index] = colour
self.version = self.version + 1
end
end
function Canvas:copy_cells()
local copy = {}
for index = 1, #self.cells do
copy[index] = self.cells[index]
end
return copy
end
function Canvas:push_history()
table.insert(self.history, self:copy_cells())
if #self.history > HISTORY_LIMIT then
table.remove(self.history, 1)
end
self.future = {}
end
function Canvas:undo()
if #self.history == 0 then
return false
end
table.insert(self.future, self:copy_cells())
self.cells = table.remove(self.history)
self.version = self.version + 1
return true
end
function Canvas:redo()
if #self.future == 0 then
return false
end
table.insert(self.history, self:copy_cells())
self.cells = table.remove(self.future)
self.version = self.version + 1
return true
end
function Canvas:history_size()
return #self.history
end
function Canvas:future_size()
return #self.future
end
function Canvas:line(x1, y1, x2, y2, colour)
local dx = math.abs(x2 - x1)
local dy = math.abs(y2 - y1)
local step_x = 1
if x2 < x1 then
step_x = -1
end
local step_y = 1
if y2 < y1 then
step_y = -1
end
local error = dx - dy
local x, y = x1, y1
local guard = (dx + dy) * 2 + 4
while guard > 0 do
guard = guard - 1
self:set(x, y, colour)
if x == x2 and y == y2 then
break
end
local doubled = 2 * error
if doubled > -dy then
error = error - dy
x = x + step_x
end
if doubled < dx then
error = error + dx
y = y + step_y
end
end
end
function Canvas:rect(x1, y1, x2, y2, colour, filled)
local left = math.min(x1, x2)
local right = math.max(x1, x2)
local top = math.min(y1, y2)
local bottom = math.max(y1, y2)
if filled then
for y = top, bottom do
for x = left, right do
self:set(x, y, colour)
end
end
return
end
self:line(left, top, right, top, colour)
self:line(right, top, right, bottom, colour)
self:line(right, bottom, left, bottom, colour)
self:line(left, bottom, left, top, colour)
end
function Canvas:ellipse(centre_x, centre_y, radius_x, radius_y, colour, filled)
local rx = math.abs(radius_x)
local ry = math.abs(radius_y)
if rx == 0 and ry == 0 then
self:set(centre_x, centre_y, colour)
return
end
if ry == 0 then
self:line(centre_x - rx, centre_y, centre_x + rx, centre_y, colour)
return
end
if rx == 0 then
self:line(centre_x, centre_y - ry, centre_x, centre_y + ry, colour)
return
end
local function inside(x, y)
local normalised_x = (x - centre_x) / rx
local normalised_y = (y - centre_y) / ry
return normalised_x * normalised_x + normalised_y * normalised_y <= 1.0
end
for y = centre_y - ry, centre_y + ry do
for x = centre_x - rx, centre_x + rx do
if inside(x, y) then
local edge = not (inside(x - 1, y) and inside(x + 1, y) and inside(x, y - 1) and inside(x, y + 1))
if filled or edge then
self:set(x, y, colour)
end
end
end
end
end
-- Flood fill of the connected region sharing the colour under (x, y).
-- Iterative on purpose: no recursion depth limit on big areas.
function Canvas:fill(x, y, colour)
if not self:in_bounds(x, y) then
return 0
end
local target = self:get(x, y)
if target == colour then
return 0
end
local stack = { (y - 1) * self.width + x }
local painted = 0
while #stack > 0 do
local index = table.remove(stack)
if self.cells[index] == target then
self.cells[index] = colour
painted = painted + 1
local zero_based = index - 1
local column = zero_based % self.width
local row = zero_based // self.width
if column > 0 then
table.insert(stack, index - 1)
end
if column < self.width - 1 then
table.insert(stack, index + 1)
end
if row > 0 then
table.insert(stack, index - self.width)
end
if row < self.height - 1 then
table.insert(stack, index + self.width)
end
end
end
if painted > 0 then
self.version = self.version + 1
end
return painted
end
function Canvas:clear()
local touched = false
for index = 1, #self.cells do
if self.cells[index] ~= 0 then
self.cells[index] = 0
touched = true
end
end
if touched then
self.version = self.version + 1
end
end
function Canvas:count(colour)
if colour == nil then
local total = 0
for index = 1, #self.cells do
if self.cells[index] ~= 0 then
total = total + 1
end
end
return total
end
local total = 0
for index = 1, #self.cells do
if self.cells[index] == colour then
total = total + 1
end
end
return total
end
function Canvas:is_empty()
for index = 1, #self.cells do
if self.cells[index] ~= 0 then
return false
end
end
return true
end
-- Plain text view of the grid: '#' painted, '.' empty. Used by the tests and
-- by the `snapshot` command.
function Canvas:to_ascii()
local lines = {}
for y = 1, self.height do
local parts = {}
for x = 1, self.width do
if self:get(x, y) == 0 then
parts[#parts + 1] = "."
else
parts[#parts + 1] = "#"
end
end
lines[#lines + 1] = table.concat(parts)
end
return table.concat(lines, "\n")
end
-- Colours of each cell as hexadecimal, for the save command.
function Canvas:to_hex()
local lines = {}
for y = 1, self.height do
local parts = {}
for x = 1, self.width do
parts[#parts + 1] = string.format("%02x", self:get(x, y))
end
lines[#lines + 1] = table.concat(parts, " ")
end
return table.concat(lines, "\n")
end
M.Canvas = Canvas
return M