The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
pump.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2017 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project http://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 /**
16  * @file
17  * Define the game's event mechanism.
18  *
19  * Events might be units moving or fighting, or when victory or defeat occurs.
20  * A scenario's configuration file will define actions to take when certain events occur.
21  * This module is responsible for the processing of events.
22  *
23  * Note that game events have nothing to do with SDL events,
24  * like mouse movement, keyboard events, etc.
25  * See events.hpp for how they are handled.
26  */
27 
28 #pragma once
29 
30 #include "game_events/fwd.hpp"
32 #include "game_events/handlers.hpp"
33 #include "game_events/manager.hpp"
34 
35 #include "config.hpp"
36 
37 #include <sstream>
38 #include <string>
39 
40 class game_display;
41 class vconfig;
42 
43 namespace lg
44 {
45 class logger;
46 }
47 
48 namespace game_events
49 {
51 {
53  const std::string& id,
54  const entity_location& loc1,
55  const entity_location& loc2,
56  const config& data)
57  : name(name)
58  , id(id)
59  , loc1(loc1)
60  , loc2(loc2)
61  , data(data)
62  {
63  std::replace(this->name.begin(), this->name.end(), ' ', '_');
64  }
65 
71 };
72 
73 struct pump_impl;
74 class manager;
75 
77 {
78  const std::unique_ptr<pump_impl> impl_;
79 
80 public:
83 
84  /**
85  * Context: The general environment within which events are processed.
86  * Returns whether or not audoing is impossible due to wml.
87  */
88  bool undo_disabled();
89 
90  /** [allow_undo] implementation */
91  void set_undo_disabled(bool mutated);
92 
93  /**
94  * Returns whether or not wml wants to abort the currently executed user action.
95  */
96  bool action_canceled();
97 
98  /** Sets whether or not wml wants to abort the currently executed user action. */
99  void set_action_canceled();
100 
101  /** Returns whether or not we are skipping messages. */
102  bool context_skip_messages();
103 
104  /** Sets whether or not we are skipping messages. */
105  void context_skip_messages(bool skip);
106 
107  /*
108  * Helper function which determines whether a wml_message text can
109  * really be pushed into the wml_messages_stream, and does it.
110  */
111  void put_wml_message(const std::string& logger, const std::string& message, bool in_chat);
112 
113  /**
114  * Function to fire an event.
115  *
116  * Events may have up to two arguments, both of which must be locations.
117  */
118  pump_result_t fire(const std::string& event,
121  const config& data = config());
122 
123  pump_result_t fire(const std::string& event,
124  const std::string& id,
127  const config& data = config());
128 
129  void raise(const std::string& event,
130  const std::string& id,
133  const config& data = config());
134 
135  inline void raise(const std::string& event,
138  const config& data = config())
139  {
140  raise(event, "", loc1, loc2, data);
141  }
142 
144 
145  /** Flushes WML messages and errors. */
146  void flush_messages();
147 
148  /** This function can be used to detect when no WML/Lua has been executed. */
149  size_t wml_tracking();
150 
151 private:
152  bool filter_event(const event_handler& handler, const queued_event& ev);
153 
154  void process_event(handler_ptr& handler_p, const queued_event& ev);
155 
156  void fill_wml_messages_map(std::map<std::string, int>& msg_map, std::stringstream& source);
157 
158  void show_wml_messages(std::stringstream& source, const std::string& caption, bool to_cerr);
159 
160  void show_wml_errors();
161 
162  void show_wml_messages();
163 
164  void put_wml_message(lg::logger& logger, const std::string& prefix, const std::string& message, bool in_chat);
165 };
166 }
std::vector< char_t > string
entity_location loc2
Definition: pump.hpp:69
wml_event_pump(manager &)
Definition: pump.cpp:654
std::string name
Definition: pump.hpp:66
bool action_canceled()
Returns whether or not wml wants to abort the currently executed user action.
Definition: pump.cpp:451
Definitions for the interface to Wesnoth Markup Language (WML).
size_t wml_tracking()
This function can be used to detect when no WML/Lua has been executed.
Definition: pump.cpp:649
static const entity_location null_entity
void show_wml_messages()
Shows a summary of the messages generated so far by WML.
Definition: pump.cpp:402
void flush_messages()
Flushes WML messages and errors.
Definition: pump.cpp:625
void show_wml_errors()
Shows a summary of the errors encountered in WML so far, to avoid a lot of the same messages to be sh...
Definition: pump.cpp:388
Instruction that causes us to skip upcoming instructions.
queued_event(const std::string &name, const std::string &id, const entity_location &loc1, const entity_location &loc2, const config &data)
Definition: pump.hpp:52
Domain specific events.
Definition: action_wml.cpp:88
Definition: pump.hpp:43
void put_wml_message(const std::string &logger, const std::string &message, bool in_chat)
Helper function which determines whether a wml_message text can really be pushed into the wml_message...
Definition: pump.cpp:480
bool undo_disabled()
Context: The general environment within which events are processed.
Definition: pump.cpp:439
void set_action_canceled()
Sets whether or not wml wants to abort the currently executed user action.
Definition: pump.cpp:457
pump_result_t operator()()
Definition: pump.cpp:524
std::shared_ptr< event_handler > handler_ptr
Shared pointer to handler objects.
Definition: handlers.hpp:40
The game event manager loads the scenario configuration object, and ensures that events are handled a...
Definition: manager.hpp:45
void process_event(handler_ptr &handler_p, const queued_event &ev)
Processes an event through a single event handler.
Definition: pump.cpp:282
pump_result_t fire(const std::string &event, const entity_location &loc1=entity_location::null_entity, const entity_location &loc2=entity_location::null_entity, const config &data=config())
Function to fire an event.
Definition: pump.cpp:493
entity_location loc1
Definition: pump.hpp:68
const std::unique_ptr< pump_impl > impl_
Definition: pump.hpp:78
Define the handlers for the game's events mechanism.
bool filter_event(const event_handler &handler, const queued_event &ev)
Returns true iff the given event passes all its filters.
Definition: pump.cpp:198
void set_undo_disabled(bool mutated)
[allow_undo] implementation
Definition: pump.cpp:445
A variable-expanding proxy for the config class.
Definition: variable.hpp:42
void fill_wml_messages_map(std::map< std::string, int > &msg_map, std::stringstream &source)
Helper function for show_wml_messages(), which gathers the messages from a stringstream.
Definition: pump.cpp:322
std::tuple< bool, bool > pump_result_t
Definition: fwd.hpp:7
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
Define locations as used by the game's events mechanism.
bool context_skip_messages()
Returns whether or not we are skipping messages.
Definition: pump.cpp:464