Fixed Input not sending events
Fixed :destroy sending unnecessary errors Other small fixes
This commit is contained in:
@@ -252,22 +252,12 @@ end
|
|||||||
--- Destroys the element and cleans up all references
|
--- Destroys the element and cleans up all references
|
||||||
--- @shortDescription Destroys the element and cleans up all references
|
--- @shortDescription Destroys the element and cleans up all references
|
||||||
function BaseElement:destroy()
|
function BaseElement:destroy()
|
||||||
if self.parent then
|
|
||||||
self.parent:removeChild(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
for event in pairs(self._registeredEvents) do
|
for event in pairs(self._registeredEvents) do
|
||||||
self:listenEvent(event, false)
|
self:listenEvent(event, false)
|
||||||
end
|
end
|
||||||
self._values.eventCallbacks = {}
|
if(self.parent) then
|
||||||
|
self.parent:removeChild(self)
|
||||||
self._props = nil
|
end
|
||||||
self._values = nil
|
|
||||||
self.basalt = nil
|
|
||||||
self.parent = nil
|
|
||||||
self.__index = nil
|
|
||||||
|
|
||||||
setmetatable(self, nil)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Requests a render update for this element
|
--- Requests a render update for this element
|
||||||
|
|||||||
@@ -139,12 +139,13 @@ function Container:addChild(child)
|
|||||||
if child == self then
|
if child == self then
|
||||||
error("Cannot add container to itself")
|
error("Cannot add container to itself")
|
||||||
end
|
end
|
||||||
|
if(child ~= nil)then
|
||||||
table.insert(self._values.children, child)
|
table.insert(self._values.children, child)
|
||||||
child.parent = self
|
child.parent = self
|
||||||
child:postInit()
|
child:postInit()
|
||||||
self.set("childrenSorted", false)
|
self.set("childrenSorted", false)
|
||||||
self:registerChildrenEvents(child)
|
self:registerChildrenEvents(child)
|
||||||
|
end
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -254,9 +255,11 @@ end
|
|||||||
--- @param child table The child to unregister events for
|
--- @param child table The child to unregister events for
|
||||||
--- @return Container self The container instance
|
--- @return Container self The container instance
|
||||||
function Container:removeChildrenEvents(child)
|
function Container:removeChildrenEvents(child)
|
||||||
if(child._registeredEvents == nil)then return self end
|
if child ~= nil then
|
||||||
for event in pairs(child._registeredEvents) do
|
if(child._registeredEvents == nil)then return self end
|
||||||
self:unregisterChildEvent(child, event)
|
for event in pairs(child._registeredEvents) do
|
||||||
|
self:unregisterChildEvent(child, event)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ function Input:char(char)
|
|||||||
|
|
||||||
local relPos = self.get("cursorPos") - self.get("viewOffset")
|
local relPos = self.get("cursorPos") - self.get("viewOffset")
|
||||||
self:setCursor(relPos, 1, true, self.get("cursorColor") or self.get("foreground"))
|
self:setCursor(relPos, 1, true, self.get("cursorColor") or self.get("foreground"))
|
||||||
|
VisualElement.char(self, char)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -127,6 +127,7 @@ function Input:key(key)
|
|||||||
|
|
||||||
local relativePos = self.get("cursorPos") - self.get("viewOffset")
|
local relativePos = self.get("cursorPos") - self.get("viewOffset")
|
||||||
self:setCursor(relativePos, 1, true, self.get("cursorColor") or self.get("foreground"))
|
self:setCursor(relativePos, 1, true, self.get("cursorColor") or self.get("foreground"))
|
||||||
|
VisualElement.key(self, key)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -23,10 +23,11 @@ BasaltProgram.__index = BasaltProgram
|
|||||||
local newPackage = dofile("rom/modules/main/cc/require.lua").make
|
local newPackage = dofile("rom/modules/main/cc/require.lua").make
|
||||||
|
|
||||||
---@private
|
---@private
|
||||||
function BasaltProgram.new()
|
function BasaltProgram.new(program)
|
||||||
local self = setmetatable({}, BasaltProgram)
|
local self = setmetatable({}, BasaltProgram)
|
||||||
self.env = {}
|
self.env = {}
|
||||||
self.args = {}
|
self.args = {}
|
||||||
|
self.program = program
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -62,6 +63,13 @@ function BasaltProgram:run(path, width, height)
|
|||||||
local ok, result = coroutine.resume(self.coroutine)
|
local ok, result = coroutine.resume(self.coroutine)
|
||||||
term.redirect(current)
|
term.redirect(current)
|
||||||
if not ok then
|
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.header = "Basalt Program Error ".. path
|
||||||
errorManager.error(result)
|
errorManager.error(result)
|
||||||
end
|
end
|
||||||
@@ -95,6 +103,13 @@ function BasaltProgram:resume(event, ...)
|
|||||||
if ok then
|
if ok then
|
||||||
self.filter = result
|
self.filter = result
|
||||||
else
|
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.header = "Basalt Program Error"
|
||||||
errorManager.error(result)
|
errorManager.error(result)
|
||||||
end
|
end
|
||||||
@@ -103,7 +118,9 @@ end
|
|||||||
|
|
||||||
---@private
|
---@private
|
||||||
function BasaltProgram:stop()
|
function BasaltProgram:stop()
|
||||||
|
if self.coroutine==nil or coroutine.status(self.coroutine)=="dead" then return end
|
||||||
|
coroutine.close(self.coroutine)
|
||||||
|
self.coroutine = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
--- @shortDescription Creates a new Program instance
|
--- @shortDescription Creates a new Program instance
|
||||||
@@ -135,13 +152,35 @@ end
|
|||||||
function Program:execute(path)
|
function Program:execute(path)
|
||||||
self.set("path", path)
|
self.set("path", path)
|
||||||
self.set("running", true)
|
self.set("running", true)
|
||||||
local program = BasaltProgram.new()
|
local program = BasaltProgram.new(self)
|
||||||
self.set("program", program)
|
self.set("program", program)
|
||||||
program:run(path, self.get("width"), self.get("height"))
|
program:run(path, self.get("width"), self.get("height"))
|
||||||
self:updateRender()
|
self:updateRender()
|
||||||
return self
|
return self
|
||||||
end
|
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
|
--- @shortDescription Handles all incomming events
|
||||||
--- @param event string The event to handle
|
--- @param event string The event to handle
|
||||||
--- @param ... any The event arguments
|
--- @param ... any The event arguments
|
||||||
|
|||||||
@@ -41,9 +41,7 @@ function Canvas:addCommand(drawFn)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Canvas:setCommand(index, drawFn)
|
function Canvas:setCommand(index, drawFn)
|
||||||
if index > 0 and index <= #self.commands then
|
self.commands[index] = drawFn
|
||||||
self.commands[index] = drawFn
|
|
||||||
end
|
|
||||||
return self
|
return self
|
||||||
end
|
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 bgLine = _bg and sub(_bg:rep(_width), 1, _width)
|
||||||
local fgLine = _fg and sub(_fg:rep(_width), 1, _width)
|
local fgLine = _fg and sub(_fg:rep(_width), 1, _width)
|
||||||
local textLine = _char and sub(_char: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
|
for i = 0, _height - 1 do
|
||||||
if _bg then render:drawBg(_x, _y + i, bgLine) end
|
if _bg then render:drawBg(_x, _y + i, bgLine) end
|
||||||
if _fg then render:drawFg(_x, _y + i, fgLine) end
|
if _fg then render:drawFg(_x, _y + i, fgLine) end
|
||||||
@@ -261,7 +254,7 @@ end
|
|||||||
function VisualElement.hooks.render(self)
|
function VisualElement.hooks.render(self)
|
||||||
local canvas = self.get("canvas")
|
local canvas = self.get("canvas")
|
||||||
if canvas and #canvas.commands.pre > 0 then
|
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)
|
cmd(self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -270,7 +263,7 @@ end
|
|||||||
function VisualElement.hooks.postRender(self)
|
function VisualElement.hooks.postRender(self)
|
||||||
local canvas = self.get("canvas")
|
local canvas = self.get("canvas")
|
||||||
if canvas and #canvas.commands.post > 0 then
|
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)
|
cmd(self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -50,6 +50,9 @@ local BaseElement = {
|
|||||||
hooks = {
|
hooks = {
|
||||||
postInit = {
|
postInit = {
|
||||||
pre = function(self)
|
pre = function(self)
|
||||||
|
if self._postInitialized then
|
||||||
|
return self
|
||||||
|
end
|
||||||
self:applyTheme()
|
self:applyTheme()
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user