The Battle for Wesnoth  1.17.12+dev
manager.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2022
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 class game_lua_kernel;
29 
30 namespace game_events
31 {
32 class wml_event_pump;
33 class event_handlers;
34 class pending_event_handler;
35 
36 /**
37  * The game event manager loads the scenario configuration object,
38  * and ensures that events are handled according to the
39  * scenario configuration for its lifetime.
40  *
41  * Thus, a manager object should be created when a scenario is played,
42  * and destroyed at the end of the scenario.
43  * If a second manager object is created before destroying the previous
44  * one, the game will crash with an assertion failure.
45  */
46 class manager
47 {
48 private:
49  const std::unique_ptr<event_handlers> event_handlers_;
50  std::set<std::string> unit_wml_ids_;
51 
52  const std::unique_ptr<game_events::wml_event_pump> pump_;
54 
55 public:
56  manager(const manager&) = delete;
57  manager& operator=(const manager&) = delete;
58 
59  explicit manager();
60  void read_scenario(const config& scenario_cfg, game_lua_kernel& lk);
61  ~manager();
62 
63  /** Create an event handler from an [event] tag. */
64  void add_event_handler_from_wml(const config& handler, game_lua_kernel& lk, bool is_menu_item = false);
65  /** Create an empty event handler. Expects the caller to finish setting up the event. */
66  pending_event_handler add_event_handler_from_lua(const std::string& name, const std::string& id, bool repeat = false, bool is_menu_item = false);
67 
68  /** Removes an event handler. */
69  void remove_event_handler(const std::string& id);
70 
71  /** Gets an event handler by ID */
72  const handler_ptr get_event_handler_by_id(const std::string& id);
73 
74  void add_events(const config::const_child_itors& cfgs, game_lua_kernel& lk, const std::string& type = std::string());
75 
76  // Normally non-serializable events are skipped when serializing (with a warning).
77  // If include_nonserializable is true, the game attempts to serialize them anyway.
78  // This will produce output that kind of looks like the event but would not deserialize to the same event.
79  void write_events(config& cfg, bool include_nonserializable=false) const;
80 
81  using event_func_t = std::function<void(game_events::manager&, handler_ptr&)>;
82  void execute_on_events(const std::string& event_id, event_func_t func);
83 
84  bool is_event_running() const;
85 
87 
89  {
90  return wml_menu_items_;
91  }
92 };
93 }
void remove_event_handler(const std::string &id)
Removes an event handler.
Definition: manager.cpp:107
bool is_event_running() const
Definition: manager.cpp:246
Represents a handler that is about to be added to the events manager but is still waiting for some da...
void write_events(config &cfg, bool include_nonserializable=false) const
Definition: manager.cpp:168
pending_event_handler add_event_handler_from_lua(const std::string &name, const std::string &id, bool repeat=false, bool is_menu_item=false)
Create an empty event handler.
Definition: manager.cpp:101
Definitions for the interface to Wesnoth Markup Language (WML).
game_events::wmi_manager wml_menu_items_
Definition: manager.hpp:53
manager & operator=(const manager &)=delete
std::set< std::string > unit_wml_ids_
Definition: manager.hpp:50
const handler_ptr get_event_handler_by_id(const std::string &id)
Gets an event handler by ID.
Definition: manager.cpp:113
Declarations for a container for wml_menu_item.
std::function< void(game_events::manager &, handler_ptr &)> event_func_t
Definition: manager.hpp:81
const std::unique_ptr< event_handlers > event_handlers_
Definition: manager.hpp:49
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:205
Domain specific events.
game_events::wmi_manager & wml_menu_items()
Definition: manager.hpp:88
const std::unique_ptr< game_events::wml_event_pump > pump_
Definition: manager.hpp:52
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
void read_scenario(const config &scenario_cfg, game_lua_kernel &lk)
Definition: manager.cpp:128
void add_events(const config::const_child_itors &cfgs, game_lua_kernel &lk, const std::string &type=std::string())
Definition: manager.cpp:148
The game event manager loads the scenario configuration object, and ensures that events are handled a...
Definition: manager.hpp:46
game_events::wml_event_pump & pump()
Definition: manager.cpp:252
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:60
void execute_on_events(const std::string &event_id, event_func_t func)
Definition: manager.cpp:200