The Battle for Wesnoth  1.15.0-dev
manager.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "game_events/fwd.hpp"
19 #include "config.hpp"
20 
21 #include <functional>
22 #include <set>
23 #include <string>
24 
25 class filter_context;
26 class game_data;
27 
28 namespace game_events
29 {
30 class wml_event_pump;
31 class event_handlers;
32 
33 /**
34  * The game event manager loads the scenario configuration object,
35  * and ensures that events are handled according to the
36  * scenario configuration for its lifetime.
37  *
38  * Thus, a manager object should be created when a scenario is played,
39  * and destroyed at the end of the scenario.
40  * If a second manager object is created before destroying the previous
41  * one, the game will crash with an assertion failure.
42  */
43 class manager
44 {
45 private:
46  const std::unique_ptr<event_handlers> event_handlers_;
47  std::set<std::string> unit_wml_ids_;
48 
49  const std::unique_ptr<game_events::wml_event_pump> pump_;
51 
52 public:
53  manager(const manager&) = delete;
54  manager& operator=(const manager&) = delete;
55 
56  explicit manager();
57  void read_scenario(const config& scenario_cfg);
58  ~manager();
59 
60  /** Create an event handler. */
61  void add_event_handler(const config& handler, bool is_menu_item = false);
62 
63  /** Removes an event handler. */
64  void remove_event_handler(const std::string& id);
65 
66  /** Gets an event handler by ID */
67  const handler_ptr get_event_handler_by_id(const std::string& id);
68 
69  void add_events(const config::const_child_itors& cfgs, const std::string& type = std::string());
70 
71  void write_events(config& cfg) const;
72 
73  using event_func_t = std::function<void(game_events::manager&, handler_ptr&)>;
74  void execute_on_events(const std::string& event_id, event_func_t func);
75 
76  bool is_event_running() const;
77 
79 
81  {
82  return wml_menu_items_;
83  }
84 };
85 }
void remove_event_handler(const std::string &id)
Removes an event handler.
Definition: manager.cpp:74
bool is_event_running() const
Definition: manager.cpp:223
Definitions for the interface to Wesnoth Markup Language (WML).
void read_scenario(const config &scenario_cfg)
Definition: manager.cpp:95
game_events::wmi_manager wml_menu_items_
Definition: manager.hpp:50
manager & operator=(const manager &)=delete
std::set< std::string > unit_wml_ids_
Definition: manager.hpp:47
const handler_ptr get_event_handler_by_id(const std::string &id)
Gets an event handler by ID.
Definition: manager.cpp:80
Declarations for a container for wml_menu_item.
std::function< void(game_events::manager &, handler_ptr &)> event_func_t
Definition: manager.hpp:73
const std::unique_ptr< event_handlers > event_handlers_
Definition: manager.hpp:46
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:238
Domain specific events.
Definition: action_wml.cpp:88
game_events::wmi_manager & wml_menu_items()
Definition: manager.hpp:80
const std::unique_ptr< game_events::wml_event_pump > pump_
Definition: manager.hpp:49
void write_events(config &cfg) const
Definition: manager.cpp:135
void add_event_handler(const config &handler, bool is_menu_item=false)
Create an event handler.
Definition: manager.cpp:68
The game event manager loads the scenario configuration object, and ensures that events are handled a...
Definition: manager.hpp:43
game_events::wml_event_pump & pump()
Definition: manager.cpp:229
std::shared_ptr< event_handler > handler_ptr
Definition: fwd.hpp:24
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
void execute_on_events(const std::string &event_id, event_func_t func)
Definition: manager.cpp:161
void add_events(const config::const_child_itors &cfgs, const std::string &type=std::string())
Definition: manager.cpp:115