entirely rework event handling take 3

This commit is contained in:
2025-12-29 01:19:07 -05:00
parent 74dd72f478
commit c5413a378b
3 changed files with 50 additions and 42 deletions

View File

@ -13,14 +13,14 @@ local handlerPriorities = {}
-- PRIORITY LEVELS (higher = more priority) -- PRIORITY LEVELS (higher = more priority)
--------------------------------------------- ---------------------------------------------
events.PRIORITY = { events.PRIORITY = {
PASSWORD_INPUT = 100, -- Highest priority - password entry PASSWORD_INPUT = 100, -- Highest priority - password entry
ENTITY_READ = 50, -- Entity tracking ENTITY_READ = 50, -- Entity tracking
MESSAGE = 50, -- Message handling MESSAGE = 50, -- Message handling
DISCONNECT_BUTTON = 30, -- Manual disconnect button DISCONNECT_BUTTON = 30, -- Manual disconnect button
ACTIVATION = 20, -- Incoming wormhole ACTIVATION = 20, -- Incoming wormhole
DISCONNECT_CHECK = 10, -- Natural disconnection DISCONNECT_CHECK = 10, -- Natural disconnection
TIMEOUT = 5, -- Timeout handlers TIMEOUT = 5, -- Timeout handlers
DEFAULT = 1 -- Default priority DEFAULT = 1 -- Default priority
} }
--------------------------------------------- ---------------------------------------------
@ -89,7 +89,7 @@ end
function events.pullEvent(filter) function events.pullEvent(filter)
while true do while true do
local eventData = {os.pullEvent(filter)} local eventData = { os.pullEvent(filter) }
local eventType = eventData[1] local eventType = eventData[1]
local result = events.dispatch(eventType, table.unpack(eventData)) local result = events.dispatch(eventType, table.unpack(eventData))
@ -102,7 +102,7 @@ function events.pullEvent(filter)
end end
function events.waitForAny(...) function events.waitForAny(...)
local functions = {...} local functions = { ... }
return parallel.waitForAny(table.unpack(functions)) return parallel.waitForAny(table.unpack(functions))
end end

View File

@ -37,10 +37,10 @@ end
-- CLICK HANDLER (Priority-based) -- CLICK HANDLER (Priority-based)
--------------------------------------------- ---------------------------------------------
function handlers.handleMonitorTouch(eventType, side, x, y) function handlers.handleDefaultClick(eventType, side, x, y)
-- This is called by the event dispatcher for monitor_touch events -- Default handler that returns click coordinates
-- Returns nil to allow lower priority handlers to process if needed -- This has the lowest priority, so other handlers can intercept first
return nil return {x = x, y = y}
end end
--------------------------------------------- ---------------------------------------------
@ -193,10 +193,9 @@ function handlers.setupConnectionHandlers()
-- Register all handlers with their priorities -- Register all handlers with their priorities
events.registerHandler("monitor_touch", handlers.handleDisconnectButton, events.PRIORITY.DISCONNECT_BUTTON) events.registerHandler("monitor_touch", handlers.handleDisconnectButton, events.PRIORITY.DISCONNECT_BUTTON)
events.registerHandler("monitor_touch", handlers.handleDefaultClick, events.PRIORITY.DEFAULT) -- Lowest priority - returns coordinates
events.registerHandler("stargate_incoming_wormhole", handlers.handleActivation, events.PRIORITY.ACTIVATION) events.registerHandler("stargate_incoming_wormhole", handlers.handleActivation, events.PRIORITY.ACTIVATION)
events.registerHandler("stargate_reconstructing_entity", handlers.handleEntityRead, events.PRIORITY.ENTITY_READ) events.registerHandler("stargate_reconstructing_entity", handlers.handleEntityRead, events.PRIORITY.ENTITY_READ)
events.registerHandler("stargate_disconnected", handlers.handleDisconnect, events.PRIORITY.DISCONNECT_CHECK) events.registerHandler("stargate_disconnected", handlers.handleDisconnect, events.PRIORITY.DISCONNECT_CHECK)
events.registerHandler("stargate_message_received", handlers.handleMessage, events.PRIORITY.MESSAGE) events.registerHandler("stargate_message_received", handlers.handleMessage, events.PRIORITY.MESSAGE)
end endreturn handlers
return handlers

View File

@ -86,8 +86,17 @@ end
local function GetClick() local function GetClick()
mon.setTextScale(1) mon.setTextScale(1)
handlers.setupConnectionHandlers() handlers.setupConnectionHandlers()
local result = events.pullEvent("monitor_touch") local result, eventType, eventData = events.pullEvent("monitor_touch")
return result == "disconnect" and 0 or 1
-- eventData is {eventType, side, x, y}
if result == "disconnect" then
return 0, 0
elseif type(result) == "table" and result.x and result.y then
return result.x, result.y
else
-- Fallback: extract from eventData
return eventData[3] or 0, eventData[4] or 0
end
end end
local function GetActivation() local function GetActivation()