The Battle for Wesnoth  1.19.5+dev
manager.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 
26 class game_lua_kernel;
27 
28 namespace game_events
29 {
30 class wml_event_pump;
31 class event_handlers;
32 class 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  */
44 class manager
45 {
46 private:
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 
53 public:
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  void add_events(const config::const_child_itors& cfgs, game_lua_kernel& lk, const std::string& type = std::string());
73 
74  // Normally non-serializable events are skipped when serializing (with a warning).
75  // If include_nonserializable is true, the game attempts to serialize them anyway.
76  // This will produce output that kind of looks like the event but would not deserialize to the same event.
77  void write_events(config& cfg, bool include_nonserializable=false) const;
78 
79  using event_func_t = std::function<void(game_events::manager&, handler_ptr&)>;
80  void execute_on_events(const std::string& event_id, event_func_t func);
81 
82  bool is_event_running() const;
83 
85 
87  {
88  return wml_menu_items_;
89  }
90 };
91 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:296
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:79
void execute_on_events(const std::string &event_id, event_func_t func)
Definition: manager.cpp:205
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:253
manager & operator=(const manager &)=delete
void write_events(config &cfg, bool include_nonserializable=false) const
Definition: manager.cpp:173
void remove_event_handler(const std::string &id)
Removes an event handler.
Definition: manager.cpp:112
void read_scenario(const config &scenario_cfg, game_lua_kernel &lk)
Definition: manager.cpp:133
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:66
void add_events(const config::const_child_itors &cfgs, game_lua_kernel &lk, const std::string &type=std::string())
Definition: manager.cpp:153
const handler_ptr get_event_handler_by_id(const std::string &id)
Gets an event handler by ID.
Definition: manager.cpp:118
bool is_event_running() const
Definition: manager.cpp:247
const std::unique_ptr< event_handlers > event_handlers_
Definition: manager.hpp:47
game_events::wmi_manager wml_menu_items_
Definition: manager.hpp:51
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:106
game_events::wmi_manager & wml_menu_items()
Definition: manager.hpp:86
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).
Domain specific events.
std::shared_ptr< event_handler > handler_ptr
Definition: fwd.hpp:25
Declarations for a container for wml_menu_item.