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.
355 lines
10 KiB
Lua
355 lines
10 KiB
Lua
-- Application state and interaction: the keyboard-driven paint editor.
|
|
-- The module is UI-framework free: it updates a canvas from key presses and
|
|
-- paints itself onto a tui.screen framebuffer.
|
|
local canvas_module = require("paint.canvas")
|
|
local palette = require("paint.palette")
|
|
local theme = require("tui.theme")
|
|
|
|
local M = {}
|
|
|
|
local App = {}
|
|
App.__index = App
|
|
|
|
M.tools = { "crayon", "ligne", "rectangle", "ellipse", "remplir", "gomme" }
|
|
|
|
local ANCHOR_TOOLS = { [2] = true, [3] = true, [4] = true }
|
|
|
|
local PANEL_WIDTH = theme.minimum.panel_width
|
|
local CHROME_HEIGHT = theme.minimum.chrome_height
|
|
|
|
function M.new(options)
|
|
options = options or {}
|
|
local width = options.width or 60
|
|
local height = options.height or 22
|
|
local app = setmetatable({
|
|
canvas = canvas_module.new(width, height),
|
|
tool = 1,
|
|
slot = options.slot or palette.default_slot,
|
|
cursor_x = math.floor(width / 2) + 1,
|
|
cursor_y = math.floor(height / 2) + 1,
|
|
anchor_x = nil,
|
|
anchor_y = nil,
|
|
filled = false,
|
|
message = "h j k l pour bouger, espace pour dessiner, 1-6 pour l'outil.",
|
|
written = nil,
|
|
}, App)
|
|
return app
|
|
end
|
|
|
|
function App:screen_width()
|
|
return self.canvas.width + PANEL_WIDTH + 3
|
|
end
|
|
|
|
function App:screen_height()
|
|
return self.canvas.height + CHROME_HEIGHT
|
|
end
|
|
|
|
function App:colour()
|
|
return palette.index_of_slot(self.slot)
|
|
end
|
|
|
|
function App:tool_name()
|
|
return M.tools[self.tool]
|
|
end
|
|
|
|
function App:needs_anchor()
|
|
return ANCHOR_TOOLS[self.tool] == true
|
|
end
|
|
|
|
function App:move(dx, dy)
|
|
self.cursor_x = math.max(1, math.min(self.canvas.width, self.cursor_x + dx))
|
|
self.cursor_y = math.max(1, math.min(self.canvas.height, self.cursor_y + dy))
|
|
end
|
|
|
|
function App:cancel_anchor()
|
|
if self.anchor_x ~= nil then
|
|
self.anchor_x = nil
|
|
self.anchor_y = nil
|
|
return true
|
|
end
|
|
return false
|
|
end
|
|
|
|
-- Applies the current tool. Anchor tools need two presses: the first one drops
|
|
-- the anchor, the second one draws between the anchor and the cursor.
|
|
function App:apply()
|
|
local canvas = self.canvas
|
|
local colour = self:colour()
|
|
if self.tool == 6 then
|
|
colour = 0
|
|
end
|
|
|
|
if self:needs_anchor() then
|
|
if self.anchor_x == nil then
|
|
self.anchor_x = self.cursor_x
|
|
self.anchor_y = self.cursor_y
|
|
self.message = string.format("%s : ancre en %d,%d", self:tool_name(), self.cursor_x, self.cursor_y)
|
|
return "anchor"
|
|
end
|
|
|
|
canvas:push_history()
|
|
if self.tool == 2 then
|
|
canvas:line(self.anchor_x, self.anchor_y, self.cursor_x, self.cursor_y, colour)
|
|
elseif self.tool == 3 then
|
|
canvas:rect(self.anchor_x, self.anchor_y, self.cursor_x, self.cursor_y, colour, self.filled)
|
|
else
|
|
local radius_x = math.abs(self.cursor_x - self.anchor_x)
|
|
local radius_y = math.abs(self.cursor_y - self.anchor_y)
|
|
canvas:ellipse(self.anchor_x, self.anchor_y, radius_x, radius_y, colour, self.filled)
|
|
end
|
|
self.anchor_x = nil
|
|
self.anchor_y = nil
|
|
self.message = string.format("%s termine", self:tool_name())
|
|
return "commit"
|
|
end
|
|
|
|
canvas:push_history()
|
|
if self.tool == 1 then
|
|
canvas:set(self.cursor_x, self.cursor_y, colour)
|
|
self.message = "crayon en " .. palette.label(self.slot)
|
|
elseif self.tool == 5 then
|
|
local painted = canvas:fill(self.cursor_x, self.cursor_y, colour)
|
|
self.message = string.format("remplissage : %d cases", painted)
|
|
else
|
|
canvas:set(self.cursor_x, self.cursor_y, 0)
|
|
self.message = "gomme"
|
|
end
|
|
return "commit"
|
|
end
|
|
|
|
-- Keyboard handling. Returns a short action word: "quit", "commit", "move",
|
|
-- "tool", "colour", "undo", "redo", "clear", "write" or nil.
|
|
function App:handle(key)
|
|
if key == nil then
|
|
return "quit"
|
|
end
|
|
if key == "q" then
|
|
return "quit"
|
|
end
|
|
if key == "h" then
|
|
self:move(-1, 0)
|
|
return "move"
|
|
end
|
|
if key == "j" then
|
|
self:move(0, 1)
|
|
return "move"
|
|
end
|
|
if key == "k" then
|
|
self:move(0, -1)
|
|
return "move"
|
|
end
|
|
if key == "l" then
|
|
self:move(1, 0)
|
|
return "move"
|
|
end
|
|
if key == " " then
|
|
return self:apply()
|
|
end
|
|
if key == "f" then
|
|
self.filled = not self.filled
|
|
if self.filled then
|
|
self.message = "formes pleines"
|
|
else
|
|
self.message = "formes vides"
|
|
end
|
|
return "mode"
|
|
end
|
|
if key == "p" then
|
|
self.slot = palette.next_slot(self.slot)
|
|
self.message = "couleur " .. palette.label(self.slot)
|
|
return "colour"
|
|
end
|
|
if key == "m" then
|
|
self.slot = palette.previous_slot(self.slot)
|
|
self.message = "couleur " .. palette.label(self.slot)
|
|
return "colour"
|
|
end
|
|
if key == "u" then
|
|
if self.canvas:undo() then
|
|
self.message = "annule"
|
|
else
|
|
self.message = "rien a annuler"
|
|
end
|
|
return "undo"
|
|
end
|
|
if key == "r" then
|
|
if self.canvas:redo() then
|
|
self.message = "refait"
|
|
else
|
|
self.message = "rien a refaire"
|
|
end
|
|
return "redo"
|
|
end
|
|
if key == "c" then
|
|
self.canvas:push_history()
|
|
self.canvas:clear()
|
|
self.message = "toile vide"
|
|
return "clear"
|
|
end
|
|
if key == "w" then
|
|
local name = "luapaint.txt"
|
|
local handle = io.open(name, "w")
|
|
if handle ~= nil then
|
|
handle:write("LuaPaint " .. self.canvas.width .. "x" .. self.canvas.height .. "\n")
|
|
handle:write(self.canvas:to_hex())
|
|
handle:write("\n")
|
|
handle:close()
|
|
self.message = "ecrit dans " .. name
|
|
return "write"
|
|
end
|
|
self.message = "impossible d'ecrire " .. name
|
|
return nil
|
|
end
|
|
local number = tonumber(key)
|
|
if number ~= nil and number >= 1 and number <= #M.tools then
|
|
self.tool = number
|
|
self:cancel_anchor()
|
|
self.message = "outil " .. self:tool_name()
|
|
return "tool"
|
|
end
|
|
return nil
|
|
end
|
|
|
|
-- Shape preview between the anchor and the cursor, drawn on a scratch canvas.
|
|
function App:preview()
|
|
if not self:needs_anchor() or self.anchor_x == nil then
|
|
return nil
|
|
end
|
|
local scratch = canvas_module.new(self.canvas.width, self.canvas.height)
|
|
if self.tool == 2 then
|
|
scratch:line(self.anchor_x, self.anchor_y, self.cursor_x, self.cursor_y, 1)
|
|
elseif self.tool == 3 then
|
|
scratch:rect(self.anchor_x, self.anchor_y, self.cursor_x, self.cursor_y, 1, self.filled)
|
|
else
|
|
local radius_x = math.abs(self.cursor_x - self.anchor_x)
|
|
local radius_y = math.abs(self.cursor_y - self.anchor_y)
|
|
scratch:ellipse(self.anchor_x, self.anchor_y, radius_x, radius_y, 1, self.filled)
|
|
end
|
|
return scratch
|
|
end
|
|
|
|
local function draw_tools_panel(app, screen, x, y, width, height)
|
|
local background = theme.ui.panel_bg
|
|
screen:box(x, y, width, height, theme.ui.panel_border, background)
|
|
screen:title(x, y, width, "Outils", theme.ui.bar_fg, theme.ui.bar_bg)
|
|
|
|
local row = y + 2
|
|
for index = 1, #M.tools do
|
|
local selected = index == app.tool
|
|
local fg = theme.ui.panel_fg
|
|
local bg = background
|
|
local marker = " "
|
|
if selected then
|
|
fg = theme.ui.accent_fg
|
|
bg = theme.ui.accent_bg
|
|
marker = "> "
|
|
end
|
|
screen:fill_rect(x + 1, row, width - 2, 1, " ", fg, bg)
|
|
screen:put(x + 2, row, string.format("%s%d %s", marker, index, M.tools[index]), fg, bg)
|
|
row = row + 1
|
|
end
|
|
|
|
row = row + 1
|
|
if row < y + height - 1 then
|
|
screen:bar(x + 1, row, width - 2, "Couleurs", theme.ui.accent_fg, theme.ui.accent_bg)
|
|
row = row + 1
|
|
for line = 0, 1 do
|
|
for column = 0, 5 do
|
|
local slot = line * 6 + column + 1
|
|
if slot <= palette.count() then
|
|
local entry = palette.entries[slot]
|
|
local swatch_x = x + 2 + column * 3
|
|
local swatch_y = row + line
|
|
local fg = 16
|
|
if slot == app.slot then
|
|
fg = 231
|
|
end
|
|
screen:put(swatch_x, swatch_y, string.format("%2d", slot), fg, entry.index)
|
|
end
|
|
end
|
|
end
|
|
row = row + 3
|
|
screen:put(x + 2, row, "couleur : " .. palette.label(app.slot), theme.ui.panel_fg, background)
|
|
row = row + 2
|
|
local keys = { "espace dessiner", "f plein/vide", "u / r annuler", "c effacer", "p / m couleur", "w ecrire", "q quitter" }
|
|
for index = 1, #keys do
|
|
if row + index - 1 < y + height - 1 then
|
|
screen:put(x + 2, row + index - 1, keys[index], theme.ui.muted, background)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
function App:render(screen, settings)
|
|
local canvas = self.canvas
|
|
local ui = theme.ui
|
|
local mono = false
|
|
if settings ~= nil then
|
|
mono = settings.mono == true
|
|
end
|
|
local painted = " "
|
|
if mono then
|
|
painted = "#"
|
|
end
|
|
screen:clear(" ", ui.canvas_fg, ui.canvas_bg)
|
|
|
|
local box_width = canvas.width + 2
|
|
local box_height = canvas.height + 2
|
|
screen:box(1, 1, box_width, box_height, ui.panel_border, ui.canvas_bg)
|
|
screen:title(1, 1, box_width, "LuaPaint", ui.bar_fg, ui.bar_bg)
|
|
|
|
local preview = self:preview()
|
|
for y = 1, canvas.height do
|
|
for x = 1, canvas.width do
|
|
local colour = canvas:get(x, y)
|
|
local fg = ui.canvas_fg
|
|
local bg = ui.canvas_bg
|
|
local character = "."
|
|
if colour ~= 0 then
|
|
fg = colour
|
|
bg = colour
|
|
character = painted
|
|
end
|
|
if preview ~= nil and preview:get(x, y) ~= 0 then
|
|
fg = ui.cursor_fg
|
|
bg = ui.accent_bg
|
|
character = ":"
|
|
end
|
|
screen:put(x + 1, y + 1, character, fg, bg)
|
|
end
|
|
end
|
|
|
|
if self.anchor_x ~= nil then
|
|
screen:put(self.anchor_x + 1, self.anchor_y + 1, "o", ui.cursor_fg, ui.cursor_bg)
|
|
end
|
|
screen:put(self.cursor_x + 1, self.cursor_y + 1, "+", ui.cursor_fg, ui.cursor_bg)
|
|
|
|
draw_tools_panel(self, screen, canvas.width + 4, 1, PANEL_WIDTH, box_height)
|
|
|
|
local status_y = canvas.height + 3
|
|
if status_y <= screen.height then
|
|
screen:fill_rect(1, status_y, screen.width, 1, " ", ui.status_fg, ui.status_bg)
|
|
local shape = "vide"
|
|
if self.filled then
|
|
shape = "plein"
|
|
end
|
|
screen:put(2, status_y, string.format(
|
|
"outil %s (%s) | couleur %s | x %d y %d | annuler %d | refaire %d",
|
|
self:tool_name(), shape, palette.label(self.slot), self.cursor_x, self.cursor_y,
|
|
canvas:history_size(), canvas:future_size()
|
|
), ui.status_fg, ui.status_bg)
|
|
end
|
|
|
|
local message_y = canvas.height + 4
|
|
if message_y <= screen.height then
|
|
screen:fill_rect(1, message_y, screen.width, 1, " ", ui.panel_fg, ui.panel_bg)
|
|
screen:put(2, message_y, self.message or "", ui.muted, ui.panel_bg)
|
|
end
|
|
|
|
return screen
|
|
end
|
|
|
|
M.App = App
|
|
|
|
return M
|