From eb30ac0b0b262c8b716be8716bfa6b0dfc1e6d3b Mon Sep 17 00:00:00 2001 From: Zsolt Tasnadi Date: Tue, 17 Mar 2026 23:04:33 +0100 Subject: [PATCH] AsciiArt --- .luacheckrc | 1 + impostor.inc | 1 + inc/init/init.module.lua | 1 + inc/system/system.asciiart.lua | 63 +++++++++++++++++++++++++++++++ inc/window/window.continued.lua | 44 +-------------------- inc/window/window.intro.title.lua | 44 +-------------------- inc/window/window.intro.ttg.lua | 31 ++------------- 7 files changed, 72 insertions(+), 113 deletions(-) create mode 100644 inc/system/system.asciiart.lua diff --git a/.luacheckrc b/.luacheckrc index 82871bd..4e5aa70 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -17,6 +17,7 @@ globals = { "Print", "Input", "Audio", + "AsciiArt", "Config", "Context", "Meter", diff --git a/impostor.inc b/impostor.inc index eac5459..68dacef 100644 --- a/impostor.inc +++ b/impostor.inc @@ -5,6 +5,7 @@ init/init.context.lua system/system.util.lua system/system.print.lua system/system.input.lua +system/system.asciiart.lua logic/logic.meter.lua logic/logic.focus.lua logic/logic.day.lua diff --git a/inc/init/init.module.lua b/inc/init/init.module.lua index cb8557f..6d9c9fb 100644 --- a/inc/init/init.module.lua +++ b/inc/init/init.module.lua @@ -16,3 +16,4 @@ Day = {} Timer = {} Trigger = {} Discussion = {} +AsciiArt = {} diff --git a/inc/system/system.asciiart.lua b/inc/system/system.asciiart.lua new file mode 100644 index 0000000..5ce940f --- /dev/null +++ b/inc/system/system.asciiart.lua @@ -0,0 +1,63 @@ +--- @section AsciiArt +AsciiArt = {} + +--- Draws ASCII art text using rectangles. +--- @param text string The ASCII art text to draw. +--- @param options table Configuration options (char_w, char_h, line_gap, word_gap, color, x, y). +function AsciiArt.draw(text, options) + options = options or {} + local char_w = options.char_w or 4 + local char_h = options.char_h or 5 + local line_gap = options.line_gap or 0 + local word_gap = options.word_gap or 6 + local color = options.color or Config.colors.light_blue + + local lines = {} + local max_len = 0 + -- Get all lines and find max length + for line in (text .. "\n"):gmatch("(.-)\n") do + table.insert(lines, line) + if #line > max_len then max_len = #line end + end + + -- Clean up empty lines from the start/end + while #lines > 0 and lines[1]:gsub("%s+", "") == "" do table.remove(lines, 1) end + while #lines > 0 and lines[#lines]:gsub("%s+", "") == "" do table.remove(lines, #lines) end + + if #lines == 0 then return end + + local total_h = 0 + for _, line in ipairs(lines) do + if line:find("#") then + total_h = total_h + char_h + line_gap + else + total_h = total_h + word_gap + end + end + total_h = total_h - line_gap + + local current_y = options.y or (Config.screen.height - total_h) / 2 + local x_offset = options.x or (Config.screen.width - (max_len * char_w)) / 2 + + for _, line in ipairs(lines) do + if line:find("#") then + for j = 1, #line do + local char = line:sub(j, j) + if char == "#" then + rect(x_offset + (j - 1) * char_w, current_y, char_w - 1, char_h - 1, color) + end + end + current_y = current_y + char_h + line_gap + else + current_y = current_y + word_gap + end + end + + return { + x = x_offset, + y = options.y or (Config.screen.height - total_h) / 2, + width = max_len * char_w, + height = total_h, + bottom = (options.y or (Config.screen.height - total_h) / 2) + total_h + } +end diff --git a/inc/window/window.continued.lua b/inc/window/window.continued.lua index ef85f6d..e5f0ed9 100644 --- a/inc/window/window.continued.lua +++ b/inc/window/window.continued.lua @@ -19,49 +19,7 @@ ContinuedWindow.text = [[ --- @within ContinuedWindow function ContinuedWindow.draw() cls(Config.colors.black) - local lines = {} - local max_len = 0 - -- Get all lines and find max length - for line in (ContinuedWindow.text .. "\n"):gmatch("(.-)\n") do - table.insert(lines, line) - if #line > max_len then max_len = #line end - end - - -- Clean up empty lines from the start/end - if #lines > 0 and lines[1] == "" then table.remove(lines, 1) end - if #lines > 0 and lines[#lines] == "" then table.remove(lines, #lines) end - - local char_w = 4 - local char_h = 5 - local line_gap = 0 - local word_gap = 6 - - local total_h = 0 - for _, line in ipairs(lines) do - if line:find("#") then - total_h = total_h + char_h + line_gap - else - total_h = total_h + word_gap - end - end - total_h = total_h - line_gap - - local current_y = (Config.screen.height - total_h) / 2 - local x_offset = (Config.screen.width - (max_len * char_w)) / 2 - - for _, line in ipairs(lines) do - if line:find("#") then - for j = 1, #line do - local char = line:sub(j, j) - if char == "#" then - rect(x_offset + (j - 1) * char_w, current_y, char_w - 1, char_h - 1, Config.colors.light_blue) - end - end - current_y = current_y + char_h + line_gap - else - current_y = current_y + word_gap - end - end + AsciiArt.draw(ContinuedWindow.text, {}) end --- Updates the continued window logic. diff --git a/inc/window/window.intro.title.lua b/inc/window/window.intro.title.lua index 05b2ac5..e1b4f51 100644 --- a/inc/window/window.intro.title.lua +++ b/inc/window/window.intro.title.lua @@ -23,49 +23,7 @@ TitleIntroWindow.text = [[ --- Draws the title intro window. --- @within TitleIntroWindow function TitleIntroWindow.draw() - local lines = {} - local max_len = 0 - -- Get all lines and find max length - for line in (TitleIntroWindow.text .. "\n"):gmatch("(.-)\n") do - table.insert(lines, line) - if #line > max_len then max_len = #line end - end - - -- Clean up empty lines from the start/end - if #lines > 0 and lines[1] == "" then table.remove(lines, 1) end - if #lines > 0 and lines[#lines] == "" then table.remove(lines, #lines) end - - local char_w = 4 - local char_h = 5 - local line_gap = 0 - local word_gap = 6 - - local total_h = 0 - for _, line in ipairs(lines) do - if line:find("#") then - total_h = total_h + char_h + line_gap - else - total_h = total_h + word_gap - end - end - total_h = total_h - line_gap - - local current_y = (Config.screen.height - total_h) / 2 - local x_offset = (Config.screen.width - (max_len * char_w)) / 2 - - for _, line in ipairs(lines) do - if line:find("#") then - for j = 1, #line do - local char = line:sub(j, j) - if char == "#" then - rect(x_offset + (j - 1) * char_w, current_y, char_w - 1, char_h - 1, Config.colors.light_blue) - end - end - current_y = current_y + char_h + line_gap - else - current_y = current_y + word_gap - end - end + AsciiArt.draw(TitleIntroWindow.text, {}) end --- Updates the title intro window logic. diff --git a/inc/window/window.intro.ttg.lua b/inc/window/window.intro.ttg.lua index a58aa21..b692ab7 100644 --- a/inc/window/window.intro.ttg.lua +++ b/inc/window/window.intro.ttg.lua @@ -12,34 +12,11 @@ TTGIntroWindow.text = [[ --- Draws the TTG intro window. --- @within TTGIntroWindow function TTGIntroWindow.draw() - if not TTGIntroWindow.glitch_started then - Glitch.show() - TTGIntroWindow.glitch_started = true - end - - local lines = {} - local max_len = 0 - for line in TTGIntroWindow.text:gmatch("[^\r\n]+") do - table.insert(lines, line) - if #line > max_len then max_len = #line end - end - - local char_w = 6 - local char_h = 7 - local y = (Config.screen.height - (#lines * char_h + 12)) / 2 - local x_offset = (Config.screen.width - (max_len * char_w)) / 2 - - for i, line in ipairs(lines) do - for j = 1, #line do - local char = line:sub(j, j) - if char == "#" then - rect(x_offset + (j - 1) * char_w, y + (i - 1) * char_h, char_w - 1, char_h - 1, Config.colors.light_blue) - end - end - end - - Print.text_center("Teletype Games", Config.screen.width / 2, y + #lines * char_h + 4, Config.colors.light_blue) + local bounds = AsciiArt.draw(TTGIntroWindow.text, {}) + if not bounds then return end + Print.text_center("Teletype Games", (Config.screen.width / 2 + 3) , (bounds.bottom + 4), Config.colors.light_blue) end + --- Updates the TTG intro window logic. --- @within TTGIntroWindow function TTGIntroWindow.update()