The Battle for Wesnoth  1.17.0-dev
manager.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
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 filter_context;
27 class game_data;
28 
29 namespace game_events
30 {
31 class wml_event_pump;
32 class event_handlers;
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);
59  ~manager();
60 
61  /** Create an event handler. */
62  void add_event_handler(const config& handler, bool is_menu_item = false);
63 
64  /** Removes an event handler. */
65  void remove_event_handler(const std::string& id);
66 
67  /** Gets an event handler by ID */
68  const handler_ptr get_event_handler_by_id(const std::string& id);
69 
70  void add_events(const config::const_child_itors& cfgs, const std::string& type = std::string());
71 
72  void write_events(config& cfg) const;
73 
74  using event_func_t = std::function<void(game_events::manager&, handler_ptr&)>;
75  void execute_on_events(const std::string& event_id, event_func_t func);
76 
77  bool is_event_running() const;
78 
80 
82  {
83  return wml_menu_items_;
84  }
85 };
86 }
void remove_event_handler(const std::string &id)
Removes an event handler.
Definition: manager.cpp:75
bool is_event_running() const
Definition: manager.cpp:224
Definitions for the interface to Wesnoth Markup Language (WML).
void read_scenario(const config &scenario_cfg)
Definition: manager.cpp:96
game_events::wmi_manager wml_menu_items_
Definition: manager.hpp:51
manager & operator=(const manager &)=delete
std::set< std::string > unit_wml_ids_
Definition: manager.hpp:48
const handler_ptr get_event_handler_by_id(const std::string &id)
Gets an event handler by ID.
Definition: manager.cpp:81
Declarations for a container for wml_menu_item.
std::function< void(game_events::manager &, handler_ptr &)> event_func_t
Definition: manager.hpp:74
const std::unique_ptr< event_handlers > event_handlers_
Definition: manager.hpp:47
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:206
Domain specific events.
Definition: action_wml.cpp:87
game_events::wmi_manager & wml_menu_items()
Definition: manager.hpp:81
const std::unique_ptr< game_events::wml_event_pump > pump_
Definition: manager.hpp:50
void write_events(config &cfg) const
Definition: manager.cpp:136
void add_event_handler(const config &handler, bool is_menu_item=false)
Create an event handler.
Definition: manager.cpp:69
The game event manager loads the scenario configuration object, and ensures that events are handled a...
Definition: manager.hpp:44
game_events::wml_event_pump & pump()
Definition: manager.cpp:230
std::shared_ptr< event_handler > handler_ptr
Definition: fwd.hpp:25
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
void execute_on_events(const std::string &event_id, event_func_t func)
Definition: manager.cpp:162
void add_events(const config::const_child_itors &cfgs, const std::string &type=std::string())
Definition: manager.cpp:116