mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-06 22:36:34 +00:00

Added some features: 1) on_event now supports a comma separated list of event names in its first parameter 2) on_event now supports a number as second parameter that specifies in which order the event handlers are called. 3) on_event now passes wesnoth.current.event_context to the handler function, since the handler function will most likeley need it.
45 lines
1.4 KiB
Lua
45 lines
1.4 KiB
Lua
local utils = wesnoth.require "lua/wml-utils.lua"
|
|
-- registers an event handler. note that, like all lua variables this is not persitent in savefiles,
|
|
-- so you have to call this function from a toplevel lua tag or from a preload event.
|
|
-- It is also not possible to use this for first_time_only=yes events.
|
|
|
|
local event_handlers = {}
|
|
local old_on_event = wesnoth.game_events.on_event or function(eventname) end
|
|
wesnoth.game_events.on_event = function(eventname)
|
|
old_on_event(eventname)
|
|
local context = nil
|
|
for k,v in pairs(event_handlers[eventname] or {}) do
|
|
if context == nil then
|
|
context = wesnoth.current.event_context
|
|
end
|
|
v.h(context)
|
|
end
|
|
end
|
|
|
|
local function on_event(eventname, arg1, arg2)
|
|
if string.match(eventname, ",") then
|
|
for elem in utils.split(eventname or "") do
|
|
global_events.add_priority_event_handler(elem, arg1, arg2)
|
|
end
|
|
end
|
|
local priority = 0
|
|
local handler = nil
|
|
if type(arg1) == "function" then
|
|
handler = arg1
|
|
else
|
|
priority = arg1
|
|
handler = arg2
|
|
end
|
|
eventname = string.gsub(eventname, " ", "_")
|
|
event_handlers[eventname] = event_handlers[eventname] or {}
|
|
table.insert(event_handlers[eventname], { h = handler, p = priority})
|
|
-- sort it.
|
|
for i = #event_handlers - 1, 1, -1 do
|
|
if event_handlers[i].p < event_handlers[i + 1].p then
|
|
event_handlers[i], event_handlers[i + 1] = event_handlers[i + 1], event_handlers[i]
|
|
end
|
|
end
|
|
end
|
|
|
|
return on_event
|