-- Windowed paint application: layout, mouse tools, keyboard shortcuts and -- rendering. The `gui` global only provides a window and rectangles, so every -- piece of application logic lives here and can be tested without a window. local canvas_module = require("paint.canvas") local palette = require("paint.palette") local font = require("gui.font") local M = {} local App = {} App.__index = App M.tools = { "CRAYON", "LIGNE", "RECT", "ELLIPSE", "REMPLIR", "GOMME" } local THEME = { background = { 24, 26, 32 }, panel = { 38, 42, 50 }, button = { 52, 58, 68 }, text = { 228, 231, 238 }, muted = { 138, 146, 158 }, accent = { 64, 160, 240 }, accent_text = { 10, 14, 20 }, canvas = { 18, 20, 26 }, cursor = { 250, 250, 250 }, status = { 30, 34, 42 }, } local WINDOW_WIDTH = 960 local WINDOW_HEIGHT = 640 local TOOLBAR_HEIGHT = 60 local PALETTE_HEIGHT = 28 local STATUS_HEIGHT = 28 local CELL_SIZE = 6 local function inside(rectangle, x, y) return x >= rectangle.x and x < rectangle.x + rectangle.w and y >= rectangle.y and y < rectangle.y + rectangle.h end function M.new(options) options = options or {} local width = options.width or WINDOW_WIDTH local height = options.height or WINDOW_HEIGHT local cell = options.cell_size or CELL_SIZE local canvas_top = TOOLBAR_HEIGHT + PALETTE_HEIGHT local canvas_pixels = height - canvas_top - STATUS_HEIGHT local app = setmetatable({ surface = options.surface or gui, width = width, height = height, cell_size = cell, canvas_top = canvas_top, status_top = height - STATUS_HEIGHT, canvas = canvas_module.new(math.floor(width / cell), math.floor(canvas_pixels / cell)), tool = 1, slot = options.slot or palette.default_slot, filled = false, stroke = false, last_x = nil, last_y = nil, anchor_x = nil, anchor_y = nil, cursor_x = 1, cursor_y = 1, cursor_in_canvas = false, erase_button = false, message = "CLIC POUR DESSINER - 1-6 OUTILS - Q QUITTER", tool_buttons = {}, action_buttons = {}, swatches = {}, }, App) for index = 1, #M.tools do app.tool_buttons[index] = { x = 8 + (index - 1) * 92, y = 8, w = 86, h = 36, tool = index } end app.action_buttons = { { x = 568, y = 8, w = 80, h = 36, action = "undo", label = "UNDO" }, { x = 652, y = 8, w = 80, h = 36, action = "clear", label = "CLEAR" }, { x = 736, y = 8, w = 80, h = 36, action = "save", label = "SAVE" }, { x = 820, y = 8, w = 80, h = 36, action = "quit", label = "QUIT" }, } for index = 1, palette.count() do app.swatches[index] = { x = 8 + (index - 1) * 30, y = 62, w = 26, h = 22, slot = index } end return app end function App:tool_name() return M.tools[self.tool] end function App:colour_index() if self.tool == 6 then return 0 end return palette.index_of_slot(self.slot) end function App:cell_at(x, y) if y < self.canvas_top or y >= self.status_top then return nil end local column = math.floor(x / self.cell_size) + 1 local row = math.floor((y - self.canvas_top) / self.cell_size) + 1 if column < 1 or row < 1 or column > self.canvas.width or row > self.canvas.height then return nil end return column, row end function App:set_tool(index) if index >= 1 and index <= #M.tools then self.tool = index self.anchor_x = nil self.anchor_y = nil self.message = "OUTIL " .. M.tools[index] return "tool" end return nil end function App:cycle_colour(step) if step > 0 then self.slot = palette.next_slot(self.slot) else self.slot = palette.previous_slot(self.slot) end self.message = "COULEUR " .. string.upper(palette.label(self.slot)) return "colour" end function App:undo() if self.canvas:undo() then self.message = "ANNULE" else self.message = "RIEN A ANNULER" end return "undo" end function App:clear() self.canvas:push_history() self.canvas:clear() self.message = "TOILE VIDE" return "clear" end function App:save(path) local name = path or "guipaint.ppm" local handle = io.open(name, "wb") if handle == nil then self.message = "ECRITURE IMPOSSIBLE" return false end local width, height = self.canvas.width, self.canvas.height handle:write(string.format("P6\n%d %d\n255\n", width, height)) local chunks = {} for row = 1, height do for column = 1, width do local rgb = palette.rgb_of(self.canvas:get(column, row)) chunks[#chunks + 1] = string.char(rgb[1], rgb[2], rgb[3]) end end handle:write(table.concat(chunks)) handle:close() self.message = "ECRIT " .. string.upper(name) return true end function App:preview_canvas() if self.anchor_x == nil then return nil end local scratch = canvas_module.new(self.canvas.width, self.canvas.height) local colour = self:colour_index() if colour == 0 then colour = 1 end if self.tool == 2 then scratch:line(self.anchor_x, self.anchor_y, self.cursor_x, self.cursor_y, colour) elseif self.tool == 3 then scratch:rect(self.anchor_x, self.anchor_y, self.cursor_x, self.cursor_y, colour, self.filled) elseif self.tool == 4 then scratch:ellipse(self.anchor_x, self.anchor_y, math.abs(self.cursor_x - self.anchor_x), math.abs(self.cursor_y - self.anchor_y), colour, self.filled) end return scratch end function App:press(x, y, button) if y < TOOLBAR_HEIGHT then for index = 1, #self.tool_buttons do if inside(self.tool_buttons[index], x, y) then return self:set_tool(self.tool_buttons[index].tool) end end for index = 1, #self.action_buttons do local button_rect = self.action_buttons[index] if inside(button_rect, x, y) then if button_rect.action == "undo" then return self:undo() elseif button_rect.action == "clear" then return self:clear() elseif button_rect.action == "save" then self:save() return "save" else return "quit" end end end return nil end if y < self.canvas_top then for index = 1, #self.swatches do if inside(self.swatches[index], x, y) then self.slot = self.swatches[index].slot self.message = "COULEUR " .. string.upper(palette.label(self.slot)) return "colour" end end return nil end local column, row = self:cell_at(x, y) if column == nil then return nil end if button == 3 then self.tool = 6 self.erase_button = true end self.cursor_x, self.cursor_y = column, row self.cursor_in_canvas = true self.canvas:push_history() self.stroke = true self.last_x, self.last_y = column, row local colour = self:colour_index() if self.tool == 5 then local painted = self.canvas:fill(column, row, colour) self.message = string.format("REMPLI %d CASES", painted) self.stroke = false elseif self.tool == 1 or self.tool == 6 then self.canvas:set(column, row, colour) else self.anchor_x, self.anchor_y = column, row end return "draw" end function App:motion(x, y) local column, row = self:cell_at(x, y) if column == nil then self.cursor_in_canvas = false return nil end self.cursor_in_canvas = true self.cursor_x, self.cursor_y = column, row if not self.stroke or self.anchor_x ~= nil then return "move" end if self.tool == 1 or self.tool == 6 then if self.last_x ~= nil then self.canvas:line(self.last_x, self.last_y, column, row, self:colour_index()) end self.last_x, self.last_y = column, row end return "move" end function App:release(x, y, button) local column, row = self:cell_at(x, y) if column ~= nil then self.cursor_x, self.cursor_y = column, row end if not self.stroke then return nil end if self.anchor_x ~= nil then local colour = self:colour_index() local target_x = column or self.cursor_x local target_y = row or self.cursor_y if self.tool == 2 then self.canvas:line(self.anchor_x, self.anchor_y, target_x, target_y, colour) elseif self.tool == 3 then self.canvas:rect(self.anchor_x, self.anchor_y, target_x, target_y, colour, self.filled) elseif self.tool == 4 then self.canvas:ellipse(self.anchor_x, self.anchor_y, math.abs(target_x - self.anchor_x), math.abs(target_y - self.anchor_y), colour, self.filled) end self.anchor_x, self.anchor_y = nil, nil end self.stroke = false self.last_x, self.last_y = nil, nil if self.erase_button then self.erase_button = false self.tool = 1 end return "commit" end function App:key(key) if key == "escape" or key == "q" then return "quit" end if key == "u" then return self:undo() end if key == "c" then return self:clear() end if key == "s" then self:save() return "save" 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 return self:cycle_colour(1) end if key == "m" then return self:cycle_colour(-1) end local number = tonumber(key) if number ~= nil then return self:set_tool(number) end return nil end function App:handle(event) if event == nil then return nil end local kind = event.type if kind == "quit" then return "quit" end if kind == "keydown" then if event["repeat"] then return nil end return self:key(event.key) end if kind == "mousedown" then return self:press(event.x, event.y, event.button) end if kind == "mousemove" then return self:motion(event.x, event.y) end if kind == "mouseup" then return self:release(event.x, event.y, event.button) end return nil end -- Every drawing primitive is collected in a flat array (x, y, w, h, r, g, b) -- and handed to the window in one native call: one Lua->C crossing per frame -- instead of thousands. -- Plain functions on purpose: the font and the window both draw through a -- three-argument `rect(x, y, w, h, r, g, b)` surface, so the collector must look -- exactly like one. local function new_recorder() local recorder = { flat = {}, n = 0, background = { 0, 0, 0 } } function recorder.clear(r, g, b) local background = recorder.background background[1], background[2], background[3] = r, g, b recorder.n = 0 end function recorder.rect(x, y, w, h, r, g, b) if w <= 0 or h <= 0 then return end local flat = recorder.flat local next_index = recorder.n + 1 flat[next_index], flat[next_index + 1], flat[next_index + 2], flat[next_index + 3] = x, y, w, h flat[next_index + 4], flat[next_index + 5], flat[next_index + 6] = r, g, b recorder.n = next_index + 6 end return recorder end local function flush(surface, recorder) local background = recorder.background surface.clear(background[1], background[2], background[3]) local count = recorder.n if count == 0 then return 0 end local flat = recorder.flat if type(surface.rects) == "function" then surface.rects(flat, count) return count // 7 end local rect = surface.rect for index = 1, count, 7 do rect(surface, flat[index], flat[index + 1], flat[index + 2], flat[index + 3], flat[index + 4], flat[index + 5], flat[index + 6]) end return count // 7 end -- Painted cells are cached as horizontal runs: a uniform row costs one -- rectangle instead of one per cell, and the list is only rebuilt when the -- canvas changed (its version counter moved). function App:canvas_runs() local canvas = self.canvas if self.runs_version == canvas.version then return self.runs, self.runs_count end local runs = self.runs or {} local count = 0 local cells = canvas.cells local width = canvas.width local size = self.cell_size local top = self.canvas_top local rgb_cache = self.rgb_cache if rgb_cache == nil then rgb_cache = {} self.rgb_cache = rgb_cache end for row = 1, canvas.height do local base = (row - 1) * width local y = top + (row - 1) * size local column = 1 while column <= width do local colour = cells[base + column] local run_end = column + 1 while run_end <= width and cells[base + run_end] == colour do run_end = run_end + 1 end if colour ~= 0 then local rgb = rgb_cache[colour] if rgb == nil then rgb = palette.rgb_of(colour) rgb_cache[colour] = rgb end count = count + 7 runs[count - 6] = (column - 1) * size runs[count - 5] = y runs[count - 4] = (run_end - column) * size runs[count - 3] = size runs[count - 2] = rgb[1] runs[count - 1] = rgb[2] runs[count] = rgb[3] end column = run_end end end self.runs = runs self.runs_count = count self.runs_version = canvas.version return runs, count end -- Painted cell count, cached until the canvas changes again: the status bar -- asks for it on every frame. function App:painted() local canvas = self.canvas if self.painted_version ~= canvas.version then self.painted_count = canvas:count() self.painted_version = canvas.version end return self.painted_count end function App:render() local surface = self.surface if surface == nil then return 0 end local recorder = self.recorder if recorder == nil then recorder = new_recorder() self.recorder = recorder end local drawn = 0 recorder.clear(THEME.background[1], THEME.background[2], THEME.background[3]) recorder.rect(0, 0, self.width, TOOLBAR_HEIGHT, THEME.panel[1], THEME.panel[2], THEME.panel[3]) for index = 1, #self.tool_buttons do local button = self.tool_buttons[index] local selected = index == self.tool local r, g, b = THEME.button[1], THEME.button[2], THEME.button[3] local text_r, text_g, text_b = THEME.text[1], THEME.text[2], THEME.text[3] if selected then r, g, b = THEME.accent[1], THEME.accent[2], THEME.accent[3] text_r, text_g, text_b = THEME.accent_text[1], THEME.accent_text[2], THEME.accent_text[3] end recorder.rect(button.x, button.y, button.w, button.h, r, g, b) local label = M.tools[index] local text_x = button.x + math.floor((button.w - font.width(label, 2)) / 2) local text_y = button.y + math.floor((button.h - font.height(2)) / 2) drawn = drawn + font.draw(recorder, text_x, text_y, label, text_r, text_g, text_b, 2) end for index = 1, #self.action_buttons do local button = self.action_buttons[index] recorder.rect(button.x, button.y, button.w, button.h, THEME.button[1], THEME.button[2], THEME.button[3]) local text_x = button.x + math.floor((button.w - font.width(button.label, 1)) / 2) local text_y = button.y + math.floor((button.h - font.height(1)) / 2) drawn = drawn + font.draw(recorder, text_x, text_y, button.label, THEME.text[1], THEME.text[2], THEME.text[3], 1) end recorder.rect(0, TOOLBAR_HEIGHT, self.width, PALETTE_HEIGHT, THEME.panel[1], THEME.panel[2], THEME.panel[3]) for index = 1, #self.swatches do local swatch = self.swatches[index] local rgb = palette.entries[index].rgb recorder.rect(swatch.x, swatch.y, swatch.w, swatch.h, rgb[1], rgb[2], rgb[3]) if index == self.slot then recorder.rect(swatch.x - 2, swatch.y - 2, swatch.w + 4, 2, 250, 250, 250) recorder.rect(swatch.x - 2, swatch.y + swatch.h, swatch.w + 4, 2, 250, 250, 250) recorder.rect(swatch.x - 2, swatch.y - 2, 2, swatch.h + 4, 250, 250, 250) recorder.rect(swatch.x + swatch.w, swatch.y - 2, 2, swatch.h + 4, 250, 250, 250) end drawn = drawn + 1 end drawn = drawn + font.draw(recorder, 392, TOOLBAR_HEIGHT + 7, "COULEUR " .. string.upper(palette.label(self.slot)), THEME.muted[1], THEME.muted[2], THEME.muted[3], 1) local canvas = self.canvas recorder.rect(0, self.canvas_top, self.width, canvas.height * self.cell_size, THEME.canvas[1], THEME.canvas[2], THEME.canvas[3]) local runs, run_count = self:canvas_runs() if run_count > 0 then table.move(runs, 1, run_count, recorder.n + 1, recorder.flat) recorder.n = recorder.n + run_count drawn = drawn + run_count // 7 end local preview = self:preview_canvas() if preview ~= nil then for row = 1, preview.height do for column = 1, preview.width do if preview:get(column, row) ~= 0 then recorder.rect((column - 1) * self.cell_size + 1, self.canvas_top + (row - 1) * self.cell_size + 1, self.cell_size - 2, self.cell_size - 2, THEME.accent[1], THEME.accent[2], THEME.accent[3]) drawn = drawn + 1 end end end end if self.cursor_in_canvas then local x = (self.cursor_x - 1) * self.cell_size local y = self.canvas_top + (self.cursor_y - 1) * self.cell_size recorder.rect(x, y, self.cell_size, 1, THEME.cursor[1], THEME.cursor[2], THEME.cursor[3]) recorder.rect(x, y + self.cell_size - 1, self.cell_size, 1, THEME.cursor[1], THEME.cursor[2], THEME.cursor[3]) recorder.rect(x, y, 1, self.cell_size, THEME.cursor[1], THEME.cursor[2], THEME.cursor[3]) recorder.rect(x + self.cell_size - 1, y, 1, self.cell_size, THEME.cursor[1], THEME.cursor[2], THEME.cursor[3]) end recorder.rect(0, self.status_top, self.width, STATUS_HEIGHT, THEME.status[1], THEME.status[2], THEME.status[3]) local status = string.format("%s | %s | X %d Y %d | %d CASES", self:tool_name(), string.upper(palette.label(self.slot)), self.cursor_x, self.cursor_y, self:painted()) drawn = drawn + font.draw(recorder, 10, self.status_top + 10, status, THEME.text[1], THEME.text[2], THEME.text[3], 1) local message = self.message or "" local room = self.width - 20 - font.width(status, 1) - 20 local maximum = math.floor(room / 6) if maximum > 0 and #message > maximum then message = string.sub(message, 1, maximum) end if #message > 0 then drawn = drawn + font.draw(recorder, self.width - 10 - font.width(message, 1), self.status_top + 10, message, THEME.accent[1], THEME.accent[2], THEME.accent[3], 1) end return flush(surface, recorder) end M.App = App return M