Fixed Input not sending events

Fixed :destroy sending unnecessary errors
Other small fixes
This commit is contained in:
Robert Jelic
2025-04-08 09:59:40 +02:00
parent 4b4b2e3ca7
commit 90b4928e6f
6 changed files with 66 additions and 37 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -50,6 +50,9 @@ local BaseElement = {
hooks = {
postInit = {
pre = function(self)
if self._postInitialized then
return self
end
self:applyTheme()
end
}