mirror of
https://github.com/wesnoth/wesnoth
synced 2025-04-30 19:23:25 +00:00

Fixes #6898. The issue is that non-WML events added through the new events API always disable undo with no equivalent of WML's `[allow_undo]`. The long-term fix is to add a way to do that; however until that's available then listeners for `moveto` need to use the old `on_event` API. The old `on_event` API can't be deprecated yet, and this is enforced by our unit tests (the build fails if there are unexpected deprecation warnings during the tests). Reverts most of 7e234f8833282424b3535b9c334c751748f7222b. Does not revert files that only listen for non-undoable events such as `die` or `new turn`. Reverts the deprecation part of #5663's 8cd133263058a5df85f64988e348d2cf54d13a48.
56 lines
1.6 KiB
Lua
56 lines
1.6 KiB
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.
|
|
|
|
if rawget(_G, "core_on_event") then
|
|
return rawget(_G, "core_on_event") -- prevent double execution
|
|
end
|
|
|
|
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 _, entry in pairs(event_handlers[eventname] or {}) do
|
|
if context == nil then
|
|
context = wesnoth.current.event_context
|
|
end
|
|
entry.h(context)
|
|
end
|
|
end
|
|
|
|
|
|
---Register an event handler
|
|
---@param eventname string The event to handle; can be a comma-separated list
|
|
---@param priority? number Events execute in order of decreasing priority, and secondarily in order of adding
|
|
---@param fcn fun(ctx:event_context)
|
|
local function on_event(eventname, priority, fcn)
|
|
if string.match(eventname, ",") then
|
|
for _,elem in ipairs((eventname or ""):split()) do
|
|
on_event(elem, priority, fcn)
|
|
end
|
|
return
|
|
end
|
|
local handler
|
|
if type(priority) == "function" then
|
|
handler = priority
|
|
priority = 0
|
|
else
|
|
handler = fcn
|
|
end
|
|
eventname = string.gsub(eventname, " ", "_")
|
|
event_handlers[eventname] = event_handlers[eventname] or {}
|
|
local eh = event_handlers[eventname]
|
|
table.insert(eh, { h = handler, p = priority})
|
|
-- prioritize last entry
|
|
for i = #eh - 1, 1, -1 do
|
|
if eh[i].p < eh[i + 1].p then
|
|
eh[i], eh[i + 1] = eh[i + 1], eh[i]
|
|
end
|
|
end
|
|
end
|
|
|
|
core_on_event = on_event
|
|
return on_event
|