From 8e5b474f17c7edadae87fd8ab282b52f18804961 Mon Sep 17 00:00:00 2001 From: Robert Jelic <36573031+NoryiE@users.noreply.github.com> Date: Thu, 13 Mar 2025 20:46:24 +0100 Subject: [PATCH] Display Element added --- .github/workflows/main.yml | 2 +- src/elements/Display.lua | 85 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/elements/Display.lua diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d76480d..fbe6741 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -30,7 +30,7 @@ jobs: lua tools/generate-config.lua - + # Step 3: Bundle and Minify - name: Bundle and Minify run: | diff --git a/src/elements/Display.lua b/src/elements/Display.lua new file mode 100644 index 0000000..e7cd3d8 --- /dev/null +++ b/src/elements/Display.lua @@ -0,0 +1,85 @@ +local elementManager = require("elementManager") +local VisualElement = elementManager.getElement("VisualElement") +local getCenteredPosition = require("libraries/utils").getCenteredPosition +local deepcopy = require("libraries/utils").deepcopy +---@cofnigDescription The Display is a special element which uses the cc window API which you can use. +---@configDefault false + +--- The Display is a special element where you can use the window (term) API to draw on a element, useful when you need to use external APIs. +---@class Display : VisualElement +local Display = setmetatable({}, VisualElement) +Display.__index = Display + +--- @shortDescription Creates a new Display instance +--- @return table self The created instance +--- @private +function Display.new() + local self = setmetatable({}, Display):__init() + self.set("width", 25) + self.set("height", 8) + self.set("z", 5) + return self +end + +--- @shortDescription Initializes the Display instance +--- @param props table The properties to initialize the element with +--- @param basalt table The basalt instance +--- @protected +function Display:init(props, basalt) + VisualElement.init(self, props, basalt) + self.set("type", "Display") + self._window = window.create(basalt.getActiveFrame():getTerm(), 1, 1, self.get("width"), self.get("height"), false) + local reposition = self._window.reposition + self._window.reposition = function(self, x, y, width, height) + self.set("x", x) + self.set("y", y) + self.set("width", width) + self.set("height", height) + reposition(self, 1, 1, width, height) + end + + self._window.getPosition = function(self) + return self.get("x"), self.get("y") + end + + self._window.setVisible = function(self, visible) + self.set("visible", visible) + end + + self._window.isVisible = function(self) + return self.get("visible") + end + + self:observe("width", function(self, width) + local window = self.get("window") + if window then + window.reposition(1, 1, width, self.get("height")) + end + end) + self:observe("height", function(self, height) + local window = self.get("window") + if window then + window.reposition(1, 1, self.get("width"), height) + end + end) +end + +function Display:getWindow() + return self._window +end + +--- @shortDescription Renders the Display +--- @protected +function Display:render() + VisualElement.render(self) + local window = self._window + local _, height = window.getSize() + if window then + for y = 1, height do + local text, fg, bg = window.getLine(y) + self:blit(1, y, text, fg, bg) + end + end +end + +return Display \ No newline at end of file