The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
manager.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2003 - 2025
3 by David White <dave@whitevine.net>
4 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY.
12
13 See the COPYING file for more details.
14*/
15
16#pragma once
17
18#include "game_events/fwd.hpp"
20#include "config.hpp"
21
22#include <functional>
23#include <set>
24#include <string>
25
26class game_lua_kernel;
27
28namespace game_events
29{
30class wml_event_pump;
31class event_handlers;
32class pending_event_handler;
33
34/**
35 * The game event manager loads the scenario configuration object,
36 * and ensures that events are handled according to the
37 * scenario configuration for its lifetime.
38 *
39 * Thus, a manager object should be created when a scenario is played,
40 * and destroyed at the end of the scenario.
41 * If a second manager object is created before destroying the previous
42 * one, the game will crash with an assertion failure.
43 */
45{
46private:
47 const std::unique_ptr<event_handlers> event_handlers_;
48 std::set<std::string> unit_wml_ids_;
49
50 const std::unique_ptr<game_events::wml_event_pump> pump_;
52
53public:
54 manager(const manager&) = delete;
55 manager& operator=(const manager&) = delete;
56
57 explicit manager();
58 void read_scenario(const config& scenario_cfg, game_lua_kernel& lk);
59 ~manager();
60
61 /** Create an event handler from an [event] tag. */
62 void add_event_handler_from_wml(const config& handler, game_lua_kernel& lk, bool is_menu_item = false);
63 /** Create an empty event handler. Expects the caller to finish setting up the event. */
64 pending_event_handler add_event_handler_from_lua(const std::string& name, const std::string& id, bool repeat = false, double priority = 0., bool is_menu_item = false);
65
66 /** Removes an event handler. */
67 void remove_event_handler(const std::string& id);
68
69 /** Gets an event handler by ID */
70 const handler_ptr get_event_handler_by_id(const std::string& id);
71
72 /**
73 * Registers a batch of [event] tags found elsewhere in WML.
74 *
75 * @param cfgs The [event] tag to register.
76 * @param lk The Lua kernel used to store the events.
77 * @param type The unit type the event is from. Used to prevent duplication
78 * @param origin A human-readable description of where cfgs came from, used only for the
79 * warning logged when type is empty and an [event] here has no id=.
80 */
81 void add_events(const config::const_child_itors& cfgs, game_lua_kernel& lk, const std::string& type, const std::string& origin);
82
83 // Normally non-serializable events are skipped when serializing (with a warning).
84 // If include_nonserializable is true, the game attempts to serialize them anyway.
85 // This will produce output that kind of looks like the event but would not deserialize to the same event.
86 void write_events(config& cfg, bool include_nonserializable=false) const;
87
88 using event_func_t = std::function<void(game_events::manager&, handler_ptr&)>;
89 void execute_on_events(const std::string& event_id, const event_func_t& func);
90
91 bool is_event_running() const;
92
94
99};
100}
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
boost::iterator_range< const_child_iterator > const_child_itors
Definition config.hpp:281
The game event manager loads the scenario configuration object, and ensures that events are handled a...
Definition manager.hpp:45
std::function< void(game_events::manager &, handler_ptr &)> event_func_t
Definition manager.hpp:88
game_events::wmi_manager & wml_menu_items()
Definition manager.hpp:95
void execute_on_events(const std::string &event_id, const event_func_t &func)
Definition manager.cpp:209
manager(const manager &)=delete
const std::unique_ptr< game_events::wml_event_pump > pump_
Definition manager.hpp:50
game_events::wml_event_pump & pump()
Definition manager.cpp:257
void write_events(config &cfg, bool include_nonserializable=false) const
Definition manager.cpp:175
void remove_event_handler(const std::string &id)
Removes an event handler.
Definition manager.cpp:114
void read_scenario(const config &scenario_cfg, game_lua_kernel &lk)
Definition manager.cpp:135
void add_event_handler_from_wml(const config &handler, game_lua_kernel &lk, bool is_menu_item=false)
Create an event handler from an [event] tag.
Definition manager.cpp:68
const handler_ptr get_event_handler_by_id(const std::string &id)
Gets an event handler by ID.
Definition manager.cpp:120
bool is_event_running() const
Definition manager.cpp:251
manager & operator=(const manager &)=delete
const std::unique_ptr< event_handlers > event_handlers_
Definition manager.hpp:47
game_events::wmi_manager wml_menu_items_
Definition manager.hpp:51
void add_events(const config::const_child_itors &cfgs, game_lua_kernel &lk, const std::string &type, const std::string &origin)
Registers a batch of [event] tags found elsewhere in WML.
Definition manager.cpp:155
pending_event_handler add_event_handler_from_lua(const std::string &name, const std::string &id, bool repeat=false, double priority=0., bool is_menu_item=false)
Create an empty event handler.
Definition manager.cpp:108
std::set< std::string > unit_wml_ids_
Definition manager.hpp:48
Represents a handler that is about to be added to the events manager but is still waiting for some da...
Definitions for the interface to Wesnoth Markup Language (WML).
const config * cfg
Domain specific events.
std::shared_ptr< event_handler > handler_ptr
Definition fwd.hpp:25
Declarations for a container for wml_menu_item.