swap to more Normal event system layout

This commit is contained in:
Jill 2023-05-04 17:36:49 +03:00
parent 57225d526c
commit dd7234cc9e
Signed by: oat
GPG Key ID: 33489AA58A955108
4 changed files with 31 additions and 43 deletions

View File

@ -127,20 +127,6 @@ dw = 0
--- The display height. --- The display height.
dh = 0 dh = 0
--- The Uranium Template table! Mostly callback-related stuff goes here.
uranium = {}
--- A callback for initialization. Called on `OnCommand`.
uranium.init = function() end
--- A callback for updates. Called every frame. Draw stuff here!
uranium.update = function() end
---@param event string
---@param ... any
---@return any
--- Call a defined callback.
function uranium:call(event, ...) end
--- Equivalent to a modfile-sandboxed `_G`, similar to Mirin's `xero`. You shouldn't need this; and if you do, *what are you doing?* --- Equivalent to a modfile-sandboxed `_G`, similar to Mirin's `xero`. You shouldn't need this; and if you do, *what are you doing?*
oat = _G oat = _G

View File

@ -26,7 +26,7 @@ end
function setShader(actor, shader) function setShader(actor, shader)
if not shader.__raw then if not shader.__raw then
function uranium.init() setShader(actor, shader) end uranium.on('init', function() setShader(actor, shader) end)
else else
actor:SetShader(shader.__raw) actor:SetShader(shader.__raw)
end end
@ -34,7 +34,7 @@ end
function setShaderfuck(shader) function setShaderfuck(shader)
if not shader.__raw then if not shader.__raw then
function uranium.init() setShaderfuck(shader) end uranium.on('init', function() setShaderfuck(shader) end)
else else
DISPLAY:ShaderFuck(shader.__raw) DISPLAY:ShaderFuck(shader.__raw)
end end
@ -81,7 +81,7 @@ end
oat._globalQueue = {} -- for resetting oat._globalQueue = {} -- for resetting
function reset(actor) function reset(actor)
if not actorsInitialized then error('uranium: cannot reset an actor during initialization', 2) end if not oat._actorsInitialized then error('uranium: cannot reset an actor during initialization', 2) end
for _, q in ipairs(oat._globalQueue) do for _, q in ipairs(oat._globalQueue) do
local queueActor = q[1] local queueActor = q[1]
if queueActor == actor.__raw then if queueActor == actor.__raw then

View File

@ -1,18 +1,23 @@
useProfiler = false useProfiler = false
profilerInfo = {} profilerInfo = {}
local uraniumFunc = {} local callbacks = {}
local debugCache = {} local debugCache = {}
function uraniumFunc:call(event, ...)
if self._callbacks[event] then ---@param event string
---@param ... any
---@return any
--- Call a defined callback.
function uranium.call(event, ...)
if callbacks[event] then
profilerInfo[event] = {} profilerInfo[event] = {}
for _, callback in ipairs(self._callbacks[event]) do for _, callback in ipairs(callbacks[event]) do
local start = os.clock() local start = os.clock()
local res = callback(unpack(arg)) local res = callback(unpack(arg))
local dur = os.clock() - start local dur = os.clock() - start
if oat.useProfiler then if useProfiler then
if not debugCache[callback] then if not debugCache[callback] then
debugCache[callback] = debug.getinfo(callback, 'Sl') -- cached cus debug.getinfo is EXPENSIVE debugCache[callback] = debug.getinfo(callback, 'Sl') -- cached cus debug.getinfo is EXPENSIVE
end end
@ -29,16 +34,12 @@ function uraniumFunc:call(event, ...)
end end
end end
local uraniumMeta = {} ---@param event string
---@param f function
function uraniumMeta:__newindex(key, value) --- Register a callback handler.
if self._callbacks[key] then function uranium.on(event, f)
table.insert(self._callbacks[key], value) if not callbacks[event] then
else callbacks[event] = {}
self._callbacks[key] = {value}
end end
end table.insert(callbacks[event], f)
end
uraniumMeta.__index = uraniumFunc
uranium = setmetatable({_callbacks = {}}, uraniumMeta)

View File

@ -28,6 +28,7 @@ dh = DISPLAY:GetDisplayHeight()
local resetOnFrameStartCfg = false local resetOnFrameStartCfg = false
local resetOnFrameStartActors = {} local resetOnFrameStartActors = {}
--- The Uranium Template table! All template-related functionality is stored here.
uranium = {} uranium = {}
require 'uranium.events' require 'uranium.events'
@ -36,7 +37,7 @@ local hasExited = false
local function exit() local function exit()
if hasExited then return end if hasExited then return end
hasExited = true hasExited = true
uranium:call('exit') uranium.call('exit')
-- good templates clean up after themselves -- good templates clean up after themselves
uranium = nil uranium = nil
_G.oat = nil _G.oat = nil
@ -66,7 +67,7 @@ local function onCommand(self)
resetOnFrameStartActors_[k.__raw] = v resetOnFrameStartActors_[k.__raw] = v
end end
resetOnFrameStartActors = resetOnFrameStartActors_ resetOnFrameStartActors = resetOnFrameStartActors_
uranium:call('init') uranium.call('init')
end end
-- runs once during ScreenReadyCommand, before the user code is loaded -- runs once during ScreenReadyCommand, before the user code is loaded
@ -141,7 +142,7 @@ local function screenReadyCommand(self)
firstrun = false firstrun = false
dt = 0 dt = 0
self:GetChildren()[2]:hidden(1) self:GetChildren()[2]:hidden(1)
uranium:call('ready') uranium.call('ready')
end end
drawfunctionArguments = {} drawfunctionArguments = {}
@ -165,9 +166,9 @@ local function screenReadyCommand(self)
end end
end end
uranium:call('preUpdate', dt) uranium.call('preUpdate', dt)
uranium:call('update', dt) uranium.call('update', dt)
uranium:call('postUpdate', dt) uranium.call('postUpdate', dt)
errored = false errored = false
@ -189,7 +190,7 @@ if success then
print('---') print('---')
actorsInitializing = true oat._actorsInitializing = true
oat._transformQueueToTree() oat._transformQueueToTree()
--Trace(fullDump(oat._actorTree)) --Trace(fullDump(oat._actorTree))
oat._currentPath = oat._actorTree oat._currentPath = oat._actorTree
@ -199,10 +200,10 @@ if success then
_main:addcommand('Off', exit) _main:addcommand('Off', exit)
_main:addcommand('SaltyReset', exit) _main:addcommand('SaltyReset', exit)
_main:addcommand('WindowFocus', function() _main:addcommand('WindowFocus', function()
uranium:call('focus', true) uranium.call('focus', true)
end) end)
_main:addcommand('WindowFocusLost', function() _main:addcommand('WindowFocusLost', function()
uranium:call('focus', false) uranium.call('focus', false)
end) end)
_main:queuecommand('Ready') _main:queuecommand('Ready')
else else