entirely rework event handling take 3
This commit is contained in:
34
events.lua
34
events.lua
@ -13,14 +13,14 @@ local handlerPriorities = {}
|
||||
-- PRIORITY LEVELS (higher = more priority)
|
||||
---------------------------------------------
|
||||
events.PRIORITY = {
|
||||
PASSWORD_INPUT = 100, -- Highest priority - password entry
|
||||
ENTITY_READ = 50, -- Entity tracking
|
||||
MESSAGE = 50, -- Message handling
|
||||
PASSWORD_INPUT = 100, -- Highest priority - password entry
|
||||
ENTITY_READ = 50, -- Entity tracking
|
||||
MESSAGE = 50, -- Message handling
|
||||
DISCONNECT_BUTTON = 30, -- Manual disconnect button
|
||||
ACTIVATION = 20, -- Incoming wormhole
|
||||
DISCONNECT_CHECK = 10, -- Natural disconnection
|
||||
TIMEOUT = 5, -- Timeout handlers
|
||||
DEFAULT = 1 -- Default priority
|
||||
ACTIVATION = 20, -- Incoming wormhole
|
||||
DISCONNECT_CHECK = 10, -- Natural disconnection
|
||||
TIMEOUT = 5, -- Timeout handlers
|
||||
DEFAULT = 1 -- Default priority
|
||||
}
|
||||
|
||||
---------------------------------------------
|
||||
@ -29,12 +29,12 @@ events.PRIORITY = {
|
||||
|
||||
function events.registerHandler(eventType, callback, priority)
|
||||
priority = priority or events.PRIORITY.DEFAULT
|
||||
|
||||
|
||||
if not eventHandlers[eventType] then
|
||||
eventHandlers[eventType] = {}
|
||||
handlerPriorities[eventType] = {}
|
||||
end
|
||||
|
||||
|
||||
table.insert(eventHandlers[eventType], callback)
|
||||
table.insert(handlerPriorities[eventType], priority)
|
||||
end
|
||||
@ -58,7 +58,7 @@ function events.dispatch(eventType, ...)
|
||||
if not handlers or #handlers == 0 then
|
||||
return nil
|
||||
end
|
||||
|
||||
|
||||
-- Sort handlers by priority (highest first)
|
||||
local sortedHandlers = {}
|
||||
for i = 1, #handlers do
|
||||
@ -67,11 +67,11 @@ function events.dispatch(eventType, ...)
|
||||
priority = handlerPriorities[eventType][i]
|
||||
})
|
||||
end
|
||||
|
||||
|
||||
table.sort(sortedHandlers, function(a, b)
|
||||
return a.priority > b.priority
|
||||
end)
|
||||
|
||||
|
||||
-- Call handlers in priority order until one handles the event
|
||||
for _, handler in ipairs(sortedHandlers) do
|
||||
local result = handler.callback(...)
|
||||
@ -79,7 +79,7 @@ function events.dispatch(eventType, ...)
|
||||
return result
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
@ -89,11 +89,11 @@ end
|
||||
|
||||
function events.pullEvent(filter)
|
||||
while true do
|
||||
local eventData = {os.pullEvent(filter)}
|
||||
local eventData = { os.pullEvent(filter) }
|
||||
local eventType = eventData[1]
|
||||
|
||||
|
||||
local result = events.dispatch(eventType, table.unpack(eventData))
|
||||
|
||||
|
||||
-- If a handler processed the event and returned something, return it
|
||||
if result ~= nil then
|
||||
return result, eventType, eventData
|
||||
@ -102,7 +102,7 @@ function events.pullEvent(filter)
|
||||
end
|
||||
|
||||
function events.waitForAny(...)
|
||||
local functions = {...}
|
||||
local functions = { ... }
|
||||
return parallel.waitForAny(table.unpack(functions))
|
||||
end
|
||||
|
||||
|
||||
43
handlers.lua
43
handlers.lua
@ -37,10 +37,10 @@ end
|
||||
-- CLICK HANDLER (Priority-based)
|
||||
---------------------------------------------
|
||||
|
||||
function handlers.handleMonitorTouch(eventType, side, x, y)
|
||||
-- This is called by the event dispatcher for monitor_touch events
|
||||
-- Returns nil to allow lower priority handlers to process if needed
|
||||
return nil
|
||||
function handlers.handleDefaultClick(eventType, side, x, y)
|
||||
-- Default handler that returns click coordinates
|
||||
-- This has the lowest priority, so other handlers can intercept first
|
||||
return {x = x, y = y}
|
||||
end
|
||||
|
||||
---------------------------------------------
|
||||
@ -49,21 +49,21 @@ end
|
||||
|
||||
function handlers.handlePasswordInput()
|
||||
display.showPasswordPrompt()
|
||||
|
||||
|
||||
local password = ""
|
||||
state.enteringPassword = true
|
||||
|
||||
|
||||
-- Register high-priority click handler for password input
|
||||
local function passwordClickHandler(eventType, side, x, y)
|
||||
if not state.enteringPassword then
|
||||
return nil -- No longer active, let other handlers process
|
||||
end
|
||||
|
||||
|
||||
-- Check number buttons (1-9)
|
||||
if y >= 7 and y <= 15 then
|
||||
local row = math.floor((y - 7) / 3)
|
||||
local col = math.floor((x - 8) / 5)
|
||||
|
||||
|
||||
if col >= 0 and col <= 2 and row >= 0 and row <= 2 then
|
||||
local num = row * 3 + col + 1
|
||||
if num >= 1 and num <= 9 then
|
||||
@ -73,7 +73,7 @@ function handlers.handlePasswordInput()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- Check bottom row buttons
|
||||
if y >= 16 and y <= 18 then
|
||||
if x >= 8 and x <= 11 then
|
||||
@ -92,13 +92,13 @@ function handlers.handlePasswordInput()
|
||||
return "submit"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
return true -- Consume all clicks during password entry
|
||||
end
|
||||
|
||||
|
||||
-- Register the password handler with highest priority
|
||||
events.registerHandler("monitor_touch", passwordClickHandler, events.PRIORITY.PASSWORD_INPUT)
|
||||
|
||||
|
||||
-- Wait for password submission
|
||||
while state.enteringPassword do
|
||||
local result = events.pullEvent("monitor_touch")
|
||||
@ -106,13 +106,13 @@ function handlers.handlePasswordInput()
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- Unregister the password handler
|
||||
events.clearHandlers("monitor_touch")
|
||||
|
||||
|
||||
-- Send password attempt
|
||||
utils.sendPasswordAttempt(password)
|
||||
|
||||
|
||||
return password
|
||||
end
|
||||
|
||||
@ -125,7 +125,7 @@ function handlers.handleDisconnectButton(eventType, side, x, y)
|
||||
if state.enteringPassword then
|
||||
return nil
|
||||
end
|
||||
|
||||
|
||||
-- Check if clicking disconnect button (bottom-right corner)
|
||||
if y >= 17 and y <= 19 and x >= 20 and x <= 28 then
|
||||
gate.disconnectStargate()
|
||||
@ -133,7 +133,7 @@ function handlers.handleDisconnectButton(eventType, side, x, y)
|
||||
utils.log("Manual disconnect triggered")
|
||||
return "disconnect"
|
||||
end
|
||||
|
||||
|
||||
return nil -- Not a disconnect button click
|
||||
end
|
||||
|
||||
@ -166,11 +166,11 @@ end
|
||||
function handlers.handleDisconnect(eventType, side, disCode)
|
||||
redstone.setOutput("top", false)
|
||||
utils.log("Stargate disconnected (code: " .. tostring(disCode) .. ")")
|
||||
|
||||
|
||||
if config.autoOpenIrisAfterDisconnect then
|
||||
utils.openIris()
|
||||
end
|
||||
|
||||
|
||||
return "disconnected"
|
||||
end
|
||||
|
||||
@ -193,10 +193,9 @@ function handlers.setupConnectionHandlers()
|
||||
|
||||
-- Register all handlers with their priorities
|
||||
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_reconstructing_entity", handlers.handleEntityRead, events.PRIORITY.ENTITY_READ)
|
||||
events.registerHandler("stargate_disconnected", handlers.handleDisconnect, events.PRIORITY.DISCONNECT_CHECK)
|
||||
events.registerHandler("stargate_message_received", handlers.handleMessage, events.PRIORITY.MESSAGE)
|
||||
end
|
||||
|
||||
return handlers
|
||||
endreturn handlers
|
||||
|
||||
15
startup.lua
15
startup.lua
@ -86,8 +86,17 @@ end
|
||||
local function GetClick()
|
||||
mon.setTextScale(1)
|
||||
handlers.setupConnectionHandlers()
|
||||
local result = events.pullEvent("monitor_touch")
|
||||
return result == "disconnect" and 0 or 1
|
||||
local result, eventType, eventData = events.pullEvent("monitor_touch")
|
||||
|
||||
-- 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
|
||||
|
||||
local function GetActivation()
|
||||
@ -242,7 +251,7 @@ end
|
||||
|
||||
local function handleIncomingWormhole()
|
||||
local state = getState()
|
||||
|
||||
|
||||
mon.setBackgroundColor(colors.black)
|
||||
mon.clear()
|
||||
mon.setBackgroundColor(colors.red)
|
||||
|
||||
Reference in New Issue
Block a user