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

@ -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