The Battle for Wesnoth  1.17.0-dev
action.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
3  by Tomasz Sniatowski <kailoran@gmail.com>
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  * Editor action classes. Some important points:
19  * - This is a polymorphic hierarchy of classes, so actions are usually passed around
20  * as editor_action pointers
21  * - The pointers can, in general, be null. Always check for null before doing anything.
22  * The helper functions perform_ that take a pointer do that.
23  * - The perform() functions can throw when an error occurs. Use smart pointers if you
24  * need to ensure the pointer is deleted.
25  */
26 
27 #pragma once
28 
31 
32 namespace editor
33 {
34 /**
35  * Replace contents of the entire map,
36  * Useful as a fallback undo method when something else would be impractical
37  */
39 {
40 public:
42  : m_(m)
43  {
44  }
45 
46  std::unique_ptr<editor_action> clone() const override;
47  void perform_without_undo(map_context& m) const override;
48  const std::string& get_name() const override;
49 
50 protected:
52 };
53 
54 /**
55  * Base class for actions that:
56  * 1) operate on an area
57  * 2) can be used as undo for a click-drag operation
58  * 3) can be extended so one undo action undos several actual drag actions
59  */
61 {
62 public:
64  {
65  }
66 
67  /**
68  * The crux of the extendable contract. This member function must be
69  * implemented so that the undo behavior is consistent, exactly the
70  * same as would be with separate undo actions for every part of
71  * the drag.
72  */
73  virtual void extend(const editor_map& map, const std::set<map_location>& locs) = 0;
74 
75  const std::string& get_name() const override
76  {
77  static const std::string name("extendable");
78  return name;
79  }
80 };
81 
82 /**
83  * Container action wrapping several actions into one.
84  * The actions are performed in the order they are added,
85  * i.e. in the usual iteration order through the container.
86  */
88 {
89 public:
90  /**
91  * Create an empty action chain
92  */
94  : actions_()
95  {
96  }
97 
99 
100  editor_action_chain& operator=(const editor_action_chain& other);
101 
102  /** Inherited from editor_action, implemented by IMPLEMENT_ACTION. */
103  std::unique_ptr<editor_action> clone() const override;
104 
105  /**
106  * Create an action chain from a deque of action pointers.
107  * Note: the action chain assumes ownership of the pointers.
108  */
109  explicit editor_action_chain(std::deque<std::unique_ptr<editor_action>> actions)
110  : actions_(std::move(actions))
111  {
112  }
113 
114  /**
115  * The destructor deletes all the owned action pointers
116  */
117  ~editor_action_chain() override = default;
118 
119  /**
120  * Go through the chain and add up all the action counts
121  */
122  int action_count() const override;
123 
124  /**
125  * Add an action at the end of the chain
126  */
127  void append_action(std::unique_ptr<editor_action> a);
128 
129  /**
130  * Add an action at the beginning of the chain
131  */
132  void prepend_action(std::unique_ptr<editor_action> a);
133 
134  /**
135  * @return true when there are no actions in the chain. Empty
136  * action chains should usually be discarded as to not keep
137  * "empty" actions around.
138  */
139  bool empty() const;
140 
141  /**
142  * Remove the last added action and return it, transferring
143  * ownership to the caller
144  */
145  std::unique_ptr<editor_action> pop_last_action();
146 
147  /**
148  * Remove the first added action and return it, transferring
149  * ownership to the caller
150  */
151  std::unique_ptr<editor_action> pop_first_action();
152 
153  /**
154  * Perform all the actions in order and create a undo action chain
155  */
156  std::unique_ptr<editor_action> perform(map_context& m) const override;
157 
158  /**
159  * Perform all the actions in order
160  */
161  void perform_without_undo(map_context& m) const override;
162 
163  const std::string& get_name() const override;
164 
165 protected:
166  /**
167  * The action pointers owned by this action chain
168  */
169  std::deque<std::unique_ptr<editor_action>> actions_;
170 };
171 
172 /**
173  * Base class for actions which act on a specified location (and possibly on other locations
174  * that can be derived from the staring hex)
175  */
177 {
178 public:
180  : loc_(loc)
181  {
182  }
183 
184  const std::string& get_name() const override
185  {
186  static const std::string name("location");
187  return name;
188  }
189 
190 protected:
192 };
193 
194 /** Base class for actions which in addition to acting on a hex,
195  * act with one terrain type, i.e. paint-related actions.
196  */
198 {
199 public:
202  , t_(t)
203  {
204  }
205 
206  const std::string& get_name() const override
207  {
208  static const std::string name("location_terrain");
209  return name;
210  }
211 
212 protected:
214 };
215 
216 /**
217  * Base class for area-affecting actions
218  */
220 {
221 public:
222  editor_action_area(const std::set<map_location>& area)
223  : area_(area)
224  {
225  }
226 
227  void extend(const editor_map& map, const std::set<map_location>& locs) override;
228 
229  const std::string& get_name() const override
230  {
231  static const std::string name("area");
232  return name;
233  }
234 
235 protected:
236  std::set<map_location> area_;
237 };
238 
239 /**
240  * Paste a map fragment into the map. No offset is used.
241  */
243 {
244 public:
246  : offset_(offset)
247  , paste_(paste)
248  {
249  }
250 
251  std::unique_ptr<editor_action> clone() const override;
252  std::unique_ptr<editor_action> perform(map_context& mc) const override;
253  void perform_without_undo(map_context& mc) const override;
254  void extend(const editor_map& map, const std::set<map_location>& locs) override;
255  const std::string& get_name() const override;
256 
257 protected:
260 };
261 
262 /**
263  * Paint the same terrain on a number of locations on the map.
264  */
266 {
267 public:
269  const std::set<map_location>& area, const t_translation::terrain_code& t, bool one_layer = false)
270  : editor_action_area(area)
271  , t_(t)
272  , one_layer_(one_layer)
273  {
274  }
275 
276  std::unique_ptr<editor_action> clone() const override;
277  std::unique_ptr<editor_action> perform(map_context& mc) const override;
278  void perform_without_undo(map_context& mc) const override;
279  const std::string& get_name() const override;
280 
281 protected:
283 
285 };
286 
287 /**
288  * Flood fill. Somewhat redundant with paint_area.
289  */
291 {
292 public:
293  editor_action_fill(map_location loc, const t_translation::terrain_code& t, bool one_layer = false)
295  , one_layer_(one_layer)
296  {
297  }
298 
299  std::unique_ptr<editor_action> clone() const override;
300  std::unique_ptr<editor_action> perform(map_context& mc) const override;
301  void perform_without_undo(map_context& mc) const override;
302  const std::string& get_name() const override;
303 
304 protected:
306 };
307 
308 /**
309  * Set starting position action, sets location ids (both for starting locations
310  * and for non-starting locations).
311  */
313 {
314 public:
317  , loc_id_(loc_id)
318  {
319  }
320 
321  std::unique_ptr<editor_action> clone() const override;
322  std::unique_ptr<editor_action> perform(map_context& mc) const override;
323  void perform_without_undo(map_context& mc) const override;
324  const std::string& get_name() const override;
325 
326 protected:
327  std::string loc_id_;
328 };
329 
330 /**
331  * Resize the map. The offsets specify, indirectly, the direction of expanding/shrinking,
332  * and fill=NONE enables copying of edge terrain instead of filling.
333  */
335 {
336 public:
338  int y_size,
339  int x_offset,
340  int y_offset,
342  : x_size_(x_size)
343  , y_size_(y_size)
344  , x_offset_(x_offset)
345  , y_offset_(y_offset)
346  , fill_(fill)
347  {
348  }
349 
350  std::unique_ptr<editor_action> clone() const override;
351  void perform_without_undo(map_context& mc) const override;
352  const std::string& get_name() const override;
353 
354 protected:
355  int x_size_;
356  int y_size_;
359 
361 };
362 
364 {
365 public:
367  : mask_(mask)
368  {
369  }
370 
371  std::unique_ptr<editor_action> clone() const override;
372  void perform_without_undo(map_context& mc) const override;
373  const std::string& get_name() const override;
374 
375 private:
377 };
378 
380 {
381 public:
383  : target_(target)
384  {
385  }
386 
387  std::unique_ptr<editor_action> clone() const override;
388  void perform_without_undo(map_context& mc) const override;
389  const std::string& get_name() const override;
390 
391 private:
393 };
394 
395 /**
396  * Randomize terrain in an area
397  */
399 {
400 public:
401  editor_action_shuffle_area(const std::set<map_location>& area)
402  : editor_action_area(area)
403  {
404  }
405 
406  std::unique_ptr<editor_action> clone() const override;
407  std::unique_ptr<editor_action> perform(map_context& mc) const override;
408  void perform_without_undo(map_context& mc) const override;
409  const std::string& get_name() const override;
410 };
411 
412 } // end namespace editor
Randomize terrain in an area.
Definition: action.hpp:398
A map fragment – a collection of locations and information abut them.
const terrain_code NONE_TERRAIN
Definition: translation.hpp:59
static const map_location & ZERO()
Definition: location.hpp:75
editor_action_paint_area(const std::set< map_location > &area, const t_translation::terrain_code &t, bool one_layer=false)
Definition: action.hpp:268
const std::string & get_name() const override
Definition: action.hpp:184
Base class for actions which act on a specified location (and possibly on other locations that can be...
Definition: action.hpp:176
#define a
const std::string & get_name() const override
Definition: action.hpp:206
Base class for area-affecting actions.
Definition: action.hpp:219
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:50
Paste a map fragment into the map.
Definition: action.hpp:242
STL namespace.
Base class for editor actions.
Container action wrapping several actions into one.
Definition: action.hpp:87
Replace contents of the entire map, Useful as a fallback undo method when something else would be imp...
Definition: action.hpp:38
editor_action_starting_position(map_location loc, std::string loc_id)
Definition: action.hpp:315
editor_action_create_mask(const editor_map &target)
Definition: action.hpp:382
Set starting position action, sets location ids (both for starting locations and for non-starting loc...
Definition: action.hpp:312
const std::string & get_name() const override
Definition: action.hpp:229
map_location loc_
std::deque< std::unique_ptr< editor_action > > actions_
The action pointers owned by this action chain.
Definition: action.hpp:169
void perform_without_undo(map_context &m) const override
Perform the action without creating an undo action.
Definition: action.cpp:73
Encapsulates the map of the game.
Definition: map.hpp:171
editor_action_apply_mask(const gamemap &mask)
Definition: action.hpp:366
Base class for actions which in addition to acting on a hex, act with one terrain type...
Definition: action.hpp:197
Manage the empty-palette in the editor.
Definition: action.cpp:30
Paint the same terrain on a number of locations on the map.
Definition: action.hpp:265
editor_action_shuffle_area(const std::set< map_location > &area)
Definition: action.hpp:401
Encapsulates the map of the game.
Definition: location.hpp:38
editor_action_fill(map_location loc, const t_translation::terrain_code &t, bool one_layer=false)
Definition: action.hpp:293
editor_action_whole_map(const editor_map &m)
Definition: action.hpp:41
editor_action_location(map_location loc)
Definition: action.hpp:179
virtual int action_count() const
Definition: action.cpp:54
This class adds extra editor-specific functionality to a normal gamemap.
Definition: editor_map.hpp:70
editor_action_paste(const map_fragment &paste, const map_location &offset=map_location::ZERO())
Definition: action.hpp:245
t_translation::terrain_code t_
Definition: action.hpp:213
Base class for all editor actions.
Definition: action_base.hpp:41
editor_action_resize_map(int x_size, int y_size, int x_offset, int y_offset, const t_translation::terrain_code &fill=t_translation::NONE_TERRAIN)
Definition: action.hpp:337
Base class for actions that: 1) operate on an area 2) can be used as undo for a click-drag operation ...
Definition: action.hpp:60
This class wraps around a map to provide a concise interface for the editor to work with...
Definition: map_context.hpp:60
virtual std::unique_ptr< editor_action > perform(map_context &) const
Perform the action, returning an undo action that, when performed, shall reverse any effects of this ...
Definition: action.cpp:64
t_translation::terrain_code fill_
Definition: action.hpp:360
editor_action_chain(std::deque< std::unique_ptr< editor_action >> actions)
Create an action chain from a deque of action pointers.
Definition: action.hpp:109
double t
Definition: astarsearch.cpp:65
const std::string & get_name() const override
Definition: action.hpp:75
t_translation::terrain_code t_
Definition: action.hpp:282
editor_action_chain()
Create an empty action chain.
Definition: action.hpp:93
std::unique_ptr< editor_action > clone() const override
Action cloning.
editor_action_area(const std::set< map_location > &area)
Definition: action.hpp:222
std::set< map_location > area_
Definition: action.hpp:236
editor_action_location_terrain(map_location loc, const t_translation::terrain_code &t)
Definition: action.hpp:200
const std::string & get_name() const override