-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
manager.hpp
132 lines (107 loc) · 3.9 KB
/
manager.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
Copyright (C) 2003 - 2017 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#pragma once
#include "game_events/handlers.hpp"
#include "game_events/wmi_manager.hpp"
#include <functional>
#include <set>
#include <string>
class game_lua_kernel;
class filter_context;
class game_display;
class game_data;
class unit_map;
namespace game_events
{
class wml_event_pump;
class event_handlers;
/**
* The game event manager loads the scenario configuration object,
* and ensures that events are handled according to the
* scenario configuration for its lifetime.
*
* Thus, a manager object should be created when a scenario is played,
* and destroyed at the end of the scenario.
* If a second manager object is created before destroying the previous
* one, the game will crash with an assertion failure.
*/
class manager
{
private:
/**
* This class is similar to an input iterator through event handlers,
* except each instance knows its own end (determined when constructed).
* Subsequent dereferences are not guaranteed to return the same element,
* so it is important to assign a dereference to a variable if you want
* to use it more than once. On the other hand, a dereference will not
* return a null pointer until the end of the iteration is reached (and
* this is how to detect the end of the iteration).
*
* For simplicity, this class is neither assignable nor equality
* comparable nor default constructable, and there is no postincrement.
* Typedefs are also skipped.
*/
class iteration
{
public:
/// Event-specific constructor.
explicit iteration(const std::string& event_name, manager&);
// Increment:
iteration& operator++();
// Dereference:
handler_ptr operator*();
private:
/// The fixed-name event handlers for this iteration.
handler_list& main_list_;
/// The varying-name event handlers for this iteration.
handler_list& var_list_;
/// The event name for this iteration.
const std::string event_name_;
/// Set to true upon dereferencing.
bool current_is_known_;
/// true if the most recent dereference was taken from main_list_.
bool main_is_current_;
/// The current (or next) element from main_list_.
handler_list::iterator main_it_;
/// The current (or next) element from var_list_.
handler_list::iterator var_it_;
game_data* gamedata_;
};
// Performs an assertion check to ensure these members are not null.
friend void event_handler::disable();
const std::unique_ptr<event_handlers> event_handlers_;
std::set<std::string> unit_wml_ids_;
const std::unique_ptr<game_events::wml_event_pump> pump_;
game_events::wmi_manager wml_menu_items_;
public:
manager(const manager&) = delete;
manager& operator=(const manager&) = delete;
explicit manager();
void read_scenario(const config& scenario_cfg);
~manager();
/** Create an event handler. */
void add_event_handler(const config& handler, bool is_menu_item = false);
/** Removes an event handler. */
void remove_event_handler(const std::string& id);
/** Gets an event handler by ID */
const handler_ptr get_event_handler_by_id(const std::string& id);
void add_events(const config::const_child_itors& cfgs, const std::string& type = std::string());
void write_events(config& cfg) const;
using event_func_t = std::function<void(game_events::manager&, handler_ptr&)>;
void execute_on_events(const std::string& event_id, event_func_t func);
game_events::wml_event_pump& pump();
game_events::wmi_manager& wml_menu_items()
{
return wml_menu_items_;
}
};
}