The Battle for Wesnoth  1.19.5+dev
pump.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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:
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,
118  const config& data = config());
119 
120  pump_result_t fire(const std::string& event,
121  const std::string& id,
124  const config& data = config());
125 
126  void raise(const std::string& event,
127  const std::string& id,
130  const config& data = config());
131 
132  inline void raise(const std::string& event,
135  const config& data = config())
136  {
137  raise(event, "", loc1, loc2, data);
138  }
139 
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 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
The game event manager loads the scenario configuration object, and ensures that events are handled a...
Definition: manager.hpp:45
void set_action_canceled()
Sets whether or not wml wants to abort the currently executed user action.
Definition: pump.cpp:363
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:298
bool undo_disabled()
Context: The general environment within which events are processed.
Definition: pump.cpp:345
bool context_skip_messages()
Returns whether or not we are skipping messages.
Definition: pump.cpp:370
void flush_messages()
Flushes WML messages and errors.
Definition: pump.cpp:521
const std::unique_ptr< pump_impl > impl_
Definition: pump.hpp:75
void show_wml_messages()
Shows a summary of the messages generated so far by WML.
Definition: pump.cpp:310
bool action_canceled()
Returns whether or not wml wants to abort the currently executed user action.
Definition: pump.cpp:357
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:399
pump_result_t operator()()
Definition: pump.cpp:430
void set_undo_disabled(bool mutated)
[allow_undo] implementation
Definition: pump.cpp:351
void process_event(handler_ptr &handler_p, const queued_event &ev)
Processes an event through a single event handler.
Definition: pump.cpp:195
wml_event_pump(manager &)
Definition: pump.cpp:530
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:234
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:386
Definitions for the interface to Wesnoth Markup Language (WML).
Define locations as used by the game's events mechanism.
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:198
Domain specific events.
std::tuple< bool, bool > pump_result_t
Definition: fwd.hpp:29
std::shared_ptr< event_handler > handler_ptr
Definition: fwd.hpp:25
Definition: pump.hpp:41
std::string_view data
Definition: picture.cpp:178
static const entity_location null_entity
entity_location loc1
Definition: pump.hpp:65
entity_location loc2
Definition: pump.hpp:66
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
std::string name
Definition: pump.hpp:63