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)
|
-- 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
|
||||||
}
|
}
|
||||||
|
|
||||||
---------------------------------------------
|
---------------------------------------------
|
||||||
@ -29,12 +29,12 @@ events.PRIORITY = {
|
|||||||
|
|
||||||
function events.registerHandler(eventType, callback, priority)
|
function events.registerHandler(eventType, callback, priority)
|
||||||
priority = priority or events.PRIORITY.DEFAULT
|
priority = priority or events.PRIORITY.DEFAULT
|
||||||
|
|
||||||
if not eventHandlers[eventType] then
|
if not eventHandlers[eventType] then
|
||||||
eventHandlers[eventType] = {}
|
eventHandlers[eventType] = {}
|
||||||
handlerPriorities[eventType] = {}
|
handlerPriorities[eventType] = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(eventHandlers[eventType], callback)
|
table.insert(eventHandlers[eventType], callback)
|
||||||
table.insert(handlerPriorities[eventType], priority)
|
table.insert(handlerPriorities[eventType], priority)
|
||||||
end
|
end
|
||||||
@ -58,7 +58,7 @@ function events.dispatch(eventType, ...)
|
|||||||
if not handlers or #handlers == 0 then
|
if not handlers or #handlers == 0 then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Sort handlers by priority (highest first)
|
-- Sort handlers by priority (highest first)
|
||||||
local sortedHandlers = {}
|
local sortedHandlers = {}
|
||||||
for i = 1, #handlers do
|
for i = 1, #handlers do
|
||||||
@ -67,11 +67,11 @@ function events.dispatch(eventType, ...)
|
|||||||
priority = handlerPriorities[eventType][i]
|
priority = handlerPriorities[eventType][i]
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
table.sort(sortedHandlers, function(a, b)
|
table.sort(sortedHandlers, function(a, b)
|
||||||
return a.priority > b.priority
|
return a.priority > b.priority
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- Call handlers in priority order until one handles the event
|
-- Call handlers in priority order until one handles the event
|
||||||
for _, handler in ipairs(sortedHandlers) do
|
for _, handler in ipairs(sortedHandlers) do
|
||||||
local result = handler.callback(...)
|
local result = handler.callback(...)
|
||||||
@ -79,7 +79,7 @@ function events.dispatch(eventType, ...)
|
|||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -89,11 +89,11 @@ 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))
|
||||||
|
|
||||||
-- If a handler processed the event and returned something, return it
|
-- If a handler processed the event and returned something, return it
|
||||||
if result ~= nil then
|
if result ~= nil then
|
||||||
return result, eventType, eventData
|
return result, eventType, 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
|
||||||
|
|
||||||
|
|||||||
43
handlers.lua
43
handlers.lua
@ -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
|
||||||
|
|
||||||
---------------------------------------------
|
---------------------------------------------
|
||||||
@ -49,21 +49,21 @@ end
|
|||||||
|
|
||||||
function handlers.handlePasswordInput()
|
function handlers.handlePasswordInput()
|
||||||
display.showPasswordPrompt()
|
display.showPasswordPrompt()
|
||||||
|
|
||||||
local password = ""
|
local password = ""
|
||||||
state.enteringPassword = true
|
state.enteringPassword = true
|
||||||
|
|
||||||
-- Register high-priority click handler for password input
|
-- Register high-priority click handler for password input
|
||||||
local function passwordClickHandler(eventType, side, x, y)
|
local function passwordClickHandler(eventType, side, x, y)
|
||||||
if not state.enteringPassword then
|
if not state.enteringPassword then
|
||||||
return nil -- No longer active, let other handlers process
|
return nil -- No longer active, let other handlers process
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Check number buttons (1-9)
|
-- Check number buttons (1-9)
|
||||||
if y >= 7 and y <= 15 then
|
if y >= 7 and y <= 15 then
|
||||||
local row = math.floor((y - 7) / 3)
|
local row = math.floor((y - 7) / 3)
|
||||||
local col = math.floor((x - 8) / 5)
|
local col = math.floor((x - 8) / 5)
|
||||||
|
|
||||||
if col >= 0 and col <= 2 and row >= 0 and row <= 2 then
|
if col >= 0 and col <= 2 and row >= 0 and row <= 2 then
|
||||||
local num = row * 3 + col + 1
|
local num = row * 3 + col + 1
|
||||||
if num >= 1 and num <= 9 then
|
if num >= 1 and num <= 9 then
|
||||||
@ -73,7 +73,7 @@ function handlers.handlePasswordInput()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Check bottom row buttons
|
-- Check bottom row buttons
|
||||||
if y >= 16 and y <= 18 then
|
if y >= 16 and y <= 18 then
|
||||||
if x >= 8 and x <= 11 then
|
if x >= 8 and x <= 11 then
|
||||||
@ -92,13 +92,13 @@ function handlers.handlePasswordInput()
|
|||||||
return "submit"
|
return "submit"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return true -- Consume all clicks during password entry
|
return true -- Consume all clicks during password entry
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Register the password handler with highest priority
|
-- Register the password handler with highest priority
|
||||||
events.registerHandler("monitor_touch", passwordClickHandler, events.PRIORITY.PASSWORD_INPUT)
|
events.registerHandler("monitor_touch", passwordClickHandler, events.PRIORITY.PASSWORD_INPUT)
|
||||||
|
|
||||||
-- Wait for password submission
|
-- Wait for password submission
|
||||||
while state.enteringPassword do
|
while state.enteringPassword do
|
||||||
local result = events.pullEvent("monitor_touch")
|
local result = events.pullEvent("monitor_touch")
|
||||||
@ -106,13 +106,13 @@ function handlers.handlePasswordInput()
|
|||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Unregister the password handler
|
-- Unregister the password handler
|
||||||
events.clearHandlers("monitor_touch")
|
events.clearHandlers("monitor_touch")
|
||||||
|
|
||||||
-- Send password attempt
|
-- Send password attempt
|
||||||
utils.sendPasswordAttempt(password)
|
utils.sendPasswordAttempt(password)
|
||||||
|
|
||||||
return password
|
return password
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -125,7 +125,7 @@ function handlers.handleDisconnectButton(eventType, side, x, y)
|
|||||||
if state.enteringPassword then
|
if state.enteringPassword then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Check if clicking disconnect button (bottom-right corner)
|
-- Check if clicking disconnect button (bottom-right corner)
|
||||||
if y >= 17 and y <= 19 and x >= 20 and x <= 28 then
|
if y >= 17 and y <= 19 and x >= 20 and x <= 28 then
|
||||||
gate.disconnectStargate()
|
gate.disconnectStargate()
|
||||||
@ -133,7 +133,7 @@ function handlers.handleDisconnectButton(eventType, side, x, y)
|
|||||||
utils.log("Manual disconnect triggered")
|
utils.log("Manual disconnect triggered")
|
||||||
return "disconnect"
|
return "disconnect"
|
||||||
end
|
end
|
||||||
|
|
||||||
return nil -- Not a disconnect button click
|
return nil -- Not a disconnect button click
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -166,11 +166,11 @@ end
|
|||||||
function handlers.handleDisconnect(eventType, side, disCode)
|
function handlers.handleDisconnect(eventType, side, disCode)
|
||||||
redstone.setOutput("top", false)
|
redstone.setOutput("top", false)
|
||||||
utils.log("Stargate disconnected (code: " .. tostring(disCode) .. ")")
|
utils.log("Stargate disconnected (code: " .. tostring(disCode) .. ")")
|
||||||
|
|
||||||
if config.autoOpenIrisAfterDisconnect then
|
if config.autoOpenIrisAfterDisconnect then
|
||||||
utils.openIris()
|
utils.openIris()
|
||||||
end
|
end
|
||||||
|
|
||||||
return "disconnected"
|
return "disconnected"
|
||||||
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
|
|
||||||
|
|||||||
15
startup.lua
15
startup.lua
@ -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()
|
||||||
@ -242,7 +251,7 @@ end
|
|||||||
|
|
||||||
local function handleIncomingWormhole()
|
local function handleIncomingWormhole()
|
||||||
local state = getState()
|
local state = getState()
|
||||||
|
|
||||||
mon.setBackgroundColor(colors.black)
|
mon.setBackgroundColor(colors.black)
|
||||||
mon.clear()
|
mon.clear()
|
||||||
mon.setBackgroundColor(colors.red)
|
mon.setBackgroundColor(colors.red)
|
||||||
|
|||||||
Reference in New Issue
Block a user