From 90b4928e6fc5341c3bfda58cef60327452c8730a Mon Sep 17 00:00:00 2001 From: Robert Jelic <36573031+NoryiE@users.noreply.github.com> Date: Tue, 8 Apr 2025 09:59:40 +0200 Subject: [PATCH] Fixed Input not sending events Fixed :destroy sending unnecessary errors Other small fixes --- src/elements/BaseElement.lua | 16 +++---------- src/elements/Container.lua | 21 +++++++++-------- src/elements/Input.lua | 3 ++- src/elements/Program.lua | 45 +++++++++++++++++++++++++++++++++--- src/plugins/canvas.lua | 15 ++++-------- src/plugins/theme.lua | 3 +++ 6 files changed, 66 insertions(+), 37 deletions(-) diff --git a/src/elements/BaseElement.lua b/src/elements/BaseElement.lua index 09a0802..baf786b 100644 --- a/src/elements/BaseElement.lua +++ b/src/elements/BaseElement.lua @@ -252,22 +252,12 @@ end --- Destroys the element and cleans up all references --- @shortDescription Destroys the element and cleans up all references function BaseElement:destroy() - if self.parent then - self.parent:removeChild(self) - end - for event in pairs(self._registeredEvents) do self:listenEvent(event, false) end - self._values.eventCallbacks = {} - - self._props = nil - self._values = nil - self.basalt = nil - self.parent = nil - self.__index = nil - - setmetatable(self, nil) + if(self.parent) then + self.parent:removeChild(self) + end end --- Requests a render update for this element diff --git a/src/elements/Container.lua b/src/elements/Container.lua index d30c4ca..0809436 100644 --- a/src/elements/Container.lua +++ b/src/elements/Container.lua @@ -139,12 +139,13 @@ function Container:addChild(child) if child == self then error("Cannot add container to itself") end - - table.insert(self._values.children, child) - child.parent = self - child:postInit() - self.set("childrenSorted", false) - self:registerChildrenEvents(child) + if(child ~= nil)then + table.insert(self._values.children, child) + child.parent = self + child:postInit() + self.set("childrenSorted", false) + self:registerChildrenEvents(child) + end return self end @@ -254,9 +255,11 @@ end --- @param child table The child to unregister events for --- @return Container self The container instance function Container:removeChildrenEvents(child) - if(child._registeredEvents == nil)then return self end - for event in pairs(child._registeredEvents) do - self:unregisterChildEvent(child, event) + if child ~= nil then + if(child._registeredEvents == nil)then return self end + for event in pairs(child._registeredEvents) do + self:unregisterChildEvent(child, event) + end end return self end diff --git a/src/elements/Input.lua b/src/elements/Input.lua index 9efea63..50c9ca2 100644 --- a/src/elements/Input.lua +++ b/src/elements/Input.lua @@ -87,7 +87,7 @@ function Input:char(char) local relPos = self.get("cursorPos") - self.get("viewOffset") self:setCursor(relPos, 1, true, self.get("cursorColor") or self.get("foreground")) - + VisualElement.char(self, char) return true end @@ -127,6 +127,7 @@ function Input:key(key) local relativePos = self.get("cursorPos") - self.get("viewOffset") self:setCursor(relativePos, 1, true, self.get("cursorColor") or self.get("foreground")) + VisualElement.key(self, key) return true end diff --git a/src/elements/Program.lua b/src/elements/Program.lua index 0c3126b..0cd0b9e 100644 --- a/src/elements/Program.lua +++ b/src/elements/Program.lua @@ -23,10 +23,11 @@ BasaltProgram.__index = BasaltProgram local newPackage = dofile("rom/modules/main/cc/require.lua").make ---@private -function BasaltProgram.new() +function BasaltProgram.new(program) local self = setmetatable({}, BasaltProgram) self.env = {} self.args = {} + self.program = program return self end @@ -62,6 +63,13 @@ function BasaltProgram:run(path, width, height) local ok, result = coroutine.resume(self.coroutine) term.redirect(current) if not ok then + if self.onError then + local result = self.onError(self.program, result) + if(result==false)then + self.filter = nil + return + end + end errorManager.header = "Basalt Program Error ".. path errorManager.error(result) end @@ -95,6 +103,13 @@ function BasaltProgram:resume(event, ...) if ok then self.filter = result else + if self.onError then + local result = self.onError(self.program, result) + if(result==false)then + self.filter = nil + return ok, result + end + end errorManager.header = "Basalt Program Error" errorManager.error(result) end @@ -103,7 +118,9 @@ end ---@private function BasaltProgram:stop() - + if self.coroutine==nil or coroutine.status(self.coroutine)=="dead" then return end + coroutine.close(self.coroutine) + self.coroutine = nil end --- @shortDescription Creates a new Program instance @@ -135,13 +152,35 @@ end function Program:execute(path) self.set("path", path) self.set("running", true) - local program = BasaltProgram.new() + local program = BasaltProgram.new(self) self.set("program", program) program:run(path, self.get("width"), self.get("height")) self:updateRender() return self end +--- Sends an event to the program +--- @shortDescription Sends an event to the program +--- @param event string The event to send +--- @param ... any The event arguments +--- @return Program self The Program instance +function Program:sendEvent(event, ...) + self:dispatchEvent(event, ...) + return self +end + +--- Registers a callback for the program's error event, if the function returns false, the program won't stop +--- @shortDescription Registers a callback for the program's error event +--- @param fn function The callback function to register +--- @return Program self The Program instance +function Program:onError(fn) + local program = self.get("program") + if program then + program.onError = fn + end + return self +end + --- @shortDescription Handles all incomming events --- @param event string The event to handle --- @param ... any The event arguments diff --git a/src/plugins/canvas.lua b/src/plugins/canvas.lua index 56bfbc1..e94beb1 100644 --- a/src/plugins/canvas.lua +++ b/src/plugins/canvas.lua @@ -41,9 +41,7 @@ function Canvas:addCommand(drawFn) end function Canvas:setCommand(index, drawFn) - if index > 0 and index <= #self.commands then - self.commands[index] = drawFn - end + self.commands[index] = drawFn return self end @@ -92,12 +90,7 @@ function Canvas:rect(x, y, width, height, char, fg, bg) local bgLine = _bg and sub(_bg:rep(_width), 1, _width) local fgLine = _fg and sub(_fg:rep(_width), 1, _width) local textLine = _char and sub(_char:rep(_width), 1, _width) - - if _bg and _fg and _char then - render:multiBlit(_x, _y, _width, _height, textLine, fgLine, bgLine) - return - end - + for i = 0, _height - 1 do if _bg then render:drawBg(_x, _y + i, bgLine) end if _fg then render:drawFg(_x, _y + i, fgLine) end @@ -261,7 +254,7 @@ end function VisualElement.hooks.render(self) local canvas = self.get("canvas") if canvas and #canvas.commands.pre > 0 then - for _, cmd in ipairs(canvas.commands.pre) do + for _, cmd in pairs(canvas.commands.pre) do cmd(self) end end @@ -270,7 +263,7 @@ end function VisualElement.hooks.postRender(self) local canvas = self.get("canvas") if canvas and #canvas.commands.post > 0 then - for _, cmd in ipairs(canvas.commands.post) do + for _, cmd in pairs(canvas.commands.post) do cmd(self) end end diff --git a/src/plugins/theme.lua b/src/plugins/theme.lua index 660a3f5..f341d11 100644 --- a/src/plugins/theme.lua +++ b/src/plugins/theme.lua @@ -50,6 +50,9 @@ local BaseElement = { hooks = { postInit = { pre = function(self) + if self._postInitialized then + return self + end self:applyTheme() end }