The Battle for Wesnoth  1.17.14+dev
pump.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 /**
17  * @file
18  * Define the game's event mechanism.
19  *
20  * Events might be units moving or fighting, or when victory or defeat occurs.
21  * A scenario's configuration file will define actions to take when certain events occur.
22  * This module is responsible for the processing of events.
23  *
24  * Note that game events have nothing to do with SDL events,
25  * like mouse movement, keyboard events, etc.
26  * See events.hpp for how they are handled.
27  */
28 
29 #pragma once
30 
31 #include "game_events/fwd.hpp"
33 #include "game_events/manager.hpp"
34 
35 #include "config.hpp"
36 
37 #include <sstream>
38 #include <string>
39 
40 namespace lg
41 {
42 class logger;
43 }
44 
45 namespace game_events
46 {
48 {
49  queued_event(const std::string& name,
50  const std::string& id,
51  const entity_location& loc1,
52  const entity_location& loc2,
53  const config& data)
54  : name(name)
55  , id(id)
56  , loc1(loc1)
57  , loc2(loc2)
58  , data(data)
59  {
60  std::replace(this->name.begin(), this->name.end(), ' ', '_');
61  }
62 
63  std::string name;
64  std::string id;
68 };
69 
70 struct pump_impl;
71 class manager;
72 
74 {
75  const std::unique_ptr<pump_impl> impl_;
76 
77 public:
79  ~wml_event_pump();
80 
81  /**
82  * Context: The general environment within which events are processed.
83  * Returns whether or not audoing is impossible due to wml.
84  */
85  bool undo_disabled();
86 
87  /** [allow_undo] implementation */
88  void set_undo_disabled(bool mutated);
89 
90  /**
91  * Returns whether or not wml wants to abort the currently executed user action.
92  */
93  bool action_canceled();
94 
95  /** Sets whether or not wml wants to abort the currently executed user action. */
96  void set_action_canceled();
97 
98  /** Returns whether or not we are skipping messages. */
99  bool context_skip_messages();
100 
101  /** Sets whether or not we are skipping messages. */
102  void context_skip_messages(bool skip);
103 
104  /*
105  * Helper function which determines whether a wml_message text can
106  * really be pushed into the wml_messages_stream, and does it.
107  */
108  void put_wml_message(const std::string& logger, const std::string& message, bool in_chat);
109 
110  /**
111  * Function to fire an event.
112  *
113  * Events may have up to two arguments, both of which must be locations.
114  */
115  pump_result_t fire(const std::string& event,
116  const entity_location& loc1 = entity_location::null_entity,
117  const entity_location& loc2 = entity_location::null_entity,
118  const config& data = config());
119 
120  pump_result_t fire(const std::string& event,
121  const std::string& id,
122  const entity_location& loc1 = entity_location::null_entity,
123  const entity_location& loc2 = entity_location::null_entity,
124  const config& data = config());
125 
126  void raise(const std::string& event,
127  const std::string& id,
128  const entity_location& loc1 = entity_location::null_entity,
129  const entity_location& loc2 = entity_location::null_entity,
130  const config& data = config());
131 
132  inline void raise(const std::string& event,
133  const entity_location& loc1 = entity_location::null_entity,
134  const entity_location& loc2 = entity_location::null_entity,
135  const config& data = config())
136  {
137  raise(event, "", loc1, loc2, data);
138  }
139 
140  pump_result_t operator()();
141 
142  /** Flushes WML messages and errors. */
143  void flush_messages();
144 
145 private:
146  void process_event(handler_ptr& handler_p, const queued_event& ev);
147 
148  void fill_wml_messages_map(std::map<std::string, int>& msg_map, std::stringstream& source);
149 
150  void show_wml_messages(std::stringstream& source, const std::string& caption);
151 
152  void show_wml_errors();
153 
154  void show_wml_messages();
155 
156  void put_wml_message(lg::logger& logger, const std::string& prefix, const std::string& message, bool in_chat);
157 };
158 }
entity_location loc2
Definition: pump.hpp:66
std::string_view data
Definition: picture.cpp:206
std::string name
Definition: pump.hpp:63
Definitions for the interface to Wesnoth Markup Language (WML).
queued_event(const std::string &name, const std::string &id, const entity_location &loc1, const entity_location &loc2, const config &data)
Definition: pump.hpp:49
Domain specific events.
Definition: pump.hpp:40
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:215
The game event manager loads the scenario configuration object, and ensures that events are handled a...
Definition: manager.hpp:46
entity_location loc1
Definition: pump.hpp:65
const std::unique_ptr< pump_impl > impl_
Definition: pump.hpp:75
std::shared_ptr< event_handler > handler_ptr
Definition: fwd.hpp:25
std::tuple< bool, bool > pump_result_t
Definition: fwd.hpp:29
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
Define locations as used by the game&#39;s events mechanism.