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