moved basalt's event system into a coroutine
This commit is contained in:
64
src/main.lua
64
src/main.lua
@@ -12,6 +12,7 @@ local expect = require("libraries/expect")
|
|||||||
--- @field traceback boolean Whether to show a traceback on errors
|
--- @field traceback boolean Whether to show a traceback on errors
|
||||||
--- @field _events table A table of events and their callbacks
|
--- @field _events table A table of events and their callbacks
|
||||||
--- @field _schedule function[] A table of scheduled functions
|
--- @field _schedule function[] A table of scheduled functions
|
||||||
|
--- @field _eventQueue table A table of unfinished events
|
||||||
--- @field _plugins table A table of plugins
|
--- @field _plugins table A table of plugins
|
||||||
--- @field isRunning boolean Whether the Basalt runtime is active
|
--- @field isRunning boolean Whether the Basalt runtime is active
|
||||||
--- @field LOGGER Log The logger instance
|
--- @field LOGGER Log The logger instance
|
||||||
@@ -20,6 +21,7 @@ local basalt = {}
|
|||||||
basalt.traceback = true
|
basalt.traceback = true
|
||||||
basalt._events = {}
|
basalt._events = {}
|
||||||
basalt._schedule = {}
|
basalt._schedule = {}
|
||||||
|
basalt._eventQueue = {}
|
||||||
basalt._plugins = {}
|
basalt._plugins = {}
|
||||||
basalt.isRunning = false
|
basalt.isRunning = false
|
||||||
basalt.LOGGER = require("log")
|
basalt.LOGGER = require("log")
|
||||||
@@ -223,25 +225,59 @@ local keyEvents = {
|
|||||||
local function updateEvent(event, ...)
|
local function updateEvent(event, ...)
|
||||||
if(event=="terminate")then basalt.stop() return end
|
if(event=="terminate")then basalt.stop() return end
|
||||||
if lazyElementsEventHandler(event, ...) then return end
|
if lazyElementsEventHandler(event, ...) then return end
|
||||||
|
local args = {...}
|
||||||
|
|
||||||
if(mouseEvents[event])then
|
local function basaltEvent()
|
||||||
if activeFrames[main] then
|
if(mouseEvents[event])then
|
||||||
activeFrames[main]:dispatchEvent(event, ...)
|
if activeFrames[main] then
|
||||||
|
activeFrames[main]:dispatchEvent(event, table.unpack(args))
|
||||||
|
end
|
||||||
|
elseif(keyEvents[event])then
|
||||||
|
if(focusedFrame~=nil)then
|
||||||
|
focusedFrame:dispatchEvent(event, table.unpack(args))
|
||||||
|
end
|
||||||
|
else
|
||||||
|
for _, frame in pairs(activeFrames) do
|
||||||
|
frame:dispatchEvent(event, table.unpack(args))
|
||||||
|
end
|
||||||
|
--activeFrames[main]:dispatchEvent(event, table.unpack(args)) -- continue here
|
||||||
end
|
end
|
||||||
elseif(keyEvents[event])then
|
|
||||||
if(focusedFrame~=nil)then
|
|
||||||
focusedFrame:dispatchEvent(event, ...)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
for _, frame in pairs(activeFrames) do
|
|
||||||
frame:dispatchEvent(event, ...)
|
|
||||||
end
|
|
||||||
--activeFrames[main]:dispatchEvent(event, ...) -- continue here
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Main event coroutine system
|
||||||
|
for k,v in pairs(basalt._eventQueue) do
|
||||||
|
if coroutine.status(v.coroutine) == "suspended" then
|
||||||
|
if v.filter == event or v.filter == nil then
|
||||||
|
v.filter = nil
|
||||||
|
local ok, result = coroutine.resume(v.coroutine, event, ...)
|
||||||
|
if not ok then
|
||||||
|
errorManager.header = "Basalt Event Error"
|
||||||
|
errorManager.error(result)
|
||||||
|
end
|
||||||
|
v.filter = result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if coroutine.status(v.coroutine) == "dead" then
|
||||||
|
table.remove(basalt._eventQueue, k)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local newEvent = {coroutine=coroutine.create(basaltEvent), filter=event}
|
||||||
|
local ok, result = coroutine.resume(newEvent.coroutine, event, ...)
|
||||||
|
if(not ok)then
|
||||||
|
errorManager.header = "Basalt Event Error"
|
||||||
|
errorManager.error(result)
|
||||||
|
end
|
||||||
|
if(result~=nil)then
|
||||||
|
newEvent.filter = result
|
||||||
|
end
|
||||||
|
table.insert(basalt._eventQueue, newEvent)
|
||||||
|
|
||||||
|
-- Schedule event coroutine system
|
||||||
for _, func in ipairs(basalt._schedule) do
|
for _, func in ipairs(basalt._schedule) do
|
||||||
if(coroutine.status(func.coroutine)=="suspended")then
|
if coroutine.status(func.coroutine)=="suspended" then
|
||||||
if(event==func.filter)or(func.filter==nil)then
|
if event==func.filter or func.filter==nil then
|
||||||
|
func.filter = nil
|
||||||
local ok, result = coroutine.resume(func.coroutine, event, ...)
|
local ok, result = coroutine.resume(func.coroutine, event, ...)
|
||||||
if(not ok)then
|
if(not ok)then
|
||||||
errorManager.header = "Basalt Schedule Error"
|
errorManager.header = "Basalt Schedule Error"
|
||||||
|
|||||||
Reference in New Issue
Block a user