The Battle for Wesnoth  1.13.4+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 - 2016 by Tomasz Sniatowski <[email protected]>
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 #ifndef EDITOR_ACTION_HPP
27 #define EDITOR_ACTION_HPP
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  }
46  void perform_without_undo(map_context& m) const;
47  const char* get_name() const { return "whole_map"; }
48  protected:
50 };
51 
52 /**
53  * Base class for actions that:
54  * 1) operate on an area
55  * 2) can be used as undo for a click-drag operation
56  * 3) can be extended so one undo action undos several actual drag actions
57  */
59 {
60  public:
62  {
63  }
64  /**
65  * The crux of the extendable contract. This member function must be
66  * implemented so that the undo behavior is consistent, exactly the
67  * same as would be with separate undo actions for every part of
68  * the drag.
69  */
70  virtual void extend(const editor_map& map, const std::set<map_location>& locs) = 0;
71  const char* get_name() const { return "extendable"; }
72 };
73 
74 /**
75  * Container action wrapping several actions into one.
76  * The actions are performed in the order they are added,
77  * i.e. in the usual iteration order through the container.
78  */
80 {
81  public:
82  /**
83  * Create an empty action chain
84  */
86  actions_()
87  {
88  }
89 
91 
93 
94  editor_action_chain* clone() const;
95 
96  /**
97  * Create an action chain from a deque of action pointers.
98  * Note: the action chain assumes ownership of the pointers.
99  */
100  explicit editor_action_chain(std::deque<editor_action*> actions)
101  : actions_(actions)
102  {
103  }
104 
105  /**
106  * Create an action chain by wrapping around a single action pointer.
107  * Note: the action chain assumes ownership of the pointer.
108  */
110  : actions_(1, action)
111  {
112  }
113 
114  /**
115  * The destructor deletes all the owned action pointers
116  */
118 
119  /**
120  * Go through the chain and add up all the action counts
121  */
122  int action_count() const;
123 
124  /**
125  * Add an action at the end of the chain
126  */
128 
129  /**
130  * Add an action at the beginning of the chain
131  */
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  */
146 
147  /**
148  * Remove the first added action and return it, transferring
149  * ownership to the caller
150  */
152 
153  /**
154  * Perform all the actions in order and create a undo action chain
155  */
157 
158  /**
159  * Perform all the actions in order
160  */
161  void perform_without_undo(map_context& m) const;
162 
163  const char* get_name() const { return "chain"; }
164 
165  protected:
166  /**
167  * The action pointers owned by this action chain
168  */
169  std::deque<editor_action*> actions_;
170 };
171 
172 
173 /**
174  * Base class for actions which act on a specified location (and possibly on other locations
175  * that can be derived from the staring hex)
176  */
178 {
179  public:
181  : loc_(loc)
182  {
183  }
184  const char* get_name() const { return "location"; }
185  protected:
187 };
188 
189 /** Base class for actions which in addition to acting on a hex,
190  * act with one terrain type, i.e. paint-related actions.
191  */
193 {
194  public:
196  const t_translation::t_terrain & t)
197  : editor_action_location(loc), t_(t)
198  {
199  }
200  const char* get_name() const { return "location_terrain"; }
201  protected:
203 };
204 
205 /**
206  * Base class for area-affecting actions
207  */
209 {
210  public:
211  editor_action_area(const std::set<map_location>& area)
212  : area_(area)
213  {
214  }
215  void extend(const editor_map& map, const std::set<map_location>& locs);
216  const char* get_name() const { return "area"; }
217  protected:
218  std::set<map_location> area_;
219 };
220 
221 /**
222  * Paste a map fragment into the map. No offset is used.
223  */
225 {
226  public:
228  : offset_(offset), paste_(paste)
229  {
230  }
231  editor_action_paste* clone() const;
233  void perform_without_undo(map_context& mc) const;
234  void extend(const editor_map& map, const std::set<map_location>& locs);
235  const char* get_name() const { return "paste"; }
236  protected:
239 };
240 
241 /**
242  * Paint the same terrain on a number of locations on the map.
243  */
245 {
246  public:
247  editor_action_paint_area(const std::set<map_location>& area,
248  const t_translation::t_terrain & t, bool one_layer=false)
249  : editor_action_area(area), t_(t), one_layer_(one_layer)
250  {
251  }
254  void perform_without_undo(map_context& mc) const;
255  const char* get_name() const { return "paint_area"; }
256  protected:
259 };
260 
261 /**
262  * Flood fill. Somewhat redundant with paint_area.
263  */
265 {
266  public:
268  const t_translation::t_terrain & t, bool one_layer=false)
269  : editor_action_location_terrain(loc, t), one_layer_(one_layer)
270  {
271  }
272  editor_action_fill* clone() const;
274  void perform_without_undo(map_context& mc) const;
275  const char* get_name() const { return "fill"; }
276  protected:
278 };
279 
280 /**
281  * Set starting position action
282  */
284 {
285  public:
287  : editor_action_location(loc), loc_id_(loc_id)
288  {
289  }
291  editor_action* perform(map_context& mc) const;
292  void perform_without_undo(map_context& mc) const;
293  const char* get_name() const { return "starting_pos"; }
294  protected:
296 };
297 
298 
299 
300 /**
301  * Resize the map. The offsets specify, indirectly, the direction of expanding/shrinking,
302  * and fill=NONE enables copying of edge terrain instead of filling.
303  */
305 {
306  public:
307  editor_action_resize_map(int x_size, int y_size, int x_offset, int y_offset,
309  : x_size_(x_size), y_size_(y_size), x_offset_(x_offset), y_offset_(y_offset), fill_(fill)
310  {
311  }
313  void perform_without_undo(map_context& mc) const;
314  const char* get_name() const { return "resize"; }
315  protected:
316  int x_size_;
317  int y_size_;
321 };
322 
324 {
325  public:
327  : mask_(mask)
328  {
329  }
331  void perform_without_undo(map_context& mc) const;
332  const char* get_name() const { return "apply_mask"; }
333  private:
335 };
336 
338 {
339  public:
341  : target_(target)
342  {
343  }
345  void perform_without_undo(map_context& mc) const;
346  const char* get_name() const { return "create_mask"; }
347  private:
349 };
350 
351 /**
352  * Randomize terrain in an area
353  */
355 {
356  public:
357  editor_action_shuffle_area(const std::set<map_location>& area)
358  : editor_action_area(area)
359  {
360  }
363  void perform_without_undo(map_context& mc) const;
364  const char* get_name() const { return "shuffle_area"; }
365 };
366 
367 
368 } //end namespace editor
369 
370 #endif
t_translation::t_terrain t_
Definition: action.hpp:202
Randomize terrain in an area.
Definition: action.hpp:354
A map fragment – a collection of locations and information abut them.
const char * get_name() const
Definition: action.hpp:293
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action.cpp:259
void append_action(editor_action *a)
Add an action at the end of the chain.
Definition: action.cpp:113
virtual void extend(const editor_map &map, const std::set< map_location > &locs)=0
The crux of the extendable contract.
const char * get_name() const
Definition: action.hpp:255
const t_terrain NONE_TERRAIN
Definition: translation.hpp:56
Base class for actions which act on a specified location (and possibly on other locations that can be...
Definition: action.hpp:177
const char * get_name() const
Definition: action.hpp:235
editor_action_shuffle_area * clone() const
Action cloning.
Definition: action.cpp:285
editor_action_chain * perform(map_context &m) const
Perform all the actions in order and create a undo action chain.
Definition: action.cpp:134
static const map_location & ZERO()
Old implementation:
Definition: location.hpp:190
editor_action_paste * clone() const
Action cloning.
Definition: action.cpp:158
editor_action_chain & operator=(const editor_action_chain &other)
Definition: action.cpp:82
Base class for area-affecting actions.
Definition: action.hpp:208
const char * get_name() const
Definition: action.hpp:275
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action.cpp:192
Paste a map fragment into the map.
Definition: action.hpp:224
Base class for editor actions.
Container action wrapping several actions into one.
Definition: action.hpp:79
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:286
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:221
editor_action_paint_area * clone() const
Action cloning.
Definition: action.cpp:181
editor_action_create_mask(const editor_map &target)
Definition: action.hpp:340
GLdouble GLdouble t
Definition: glew.h:1366
Set starting position action.
Definition: action.hpp:283
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action.cpp:210
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action.cpp:242
const char * get_name() const
Definition: action.hpp:346
GLintptr offset
Definition: glew.h:1650
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action.cpp:279
void perform_without_undo(map_context &m) const
Perform the action without creating an undo action.
Definition: action.cpp:71
editor_action_paint_area(const std::set< map_location > &area, const t_translation::t_terrain &t, bool one_layer=false)
Definition: action.hpp:247
const char * get_name() const
Definition: action.hpp:184
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action.cpp:269
void extend(const editor_map &map, const std::set< map_location > &locs)
The crux of the extendable contract.
Definition: action.cpp:153
editor_action * pop_first_action()
Remove the first added action and return it, transferring ownership to the caller.
Definition: action.cpp:128
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
void extend(const editor_map &map, const std::set< map_location > &locs)
The crux of the extendable contract.
Definition: action.cpp:162
Encapsulates the map of the game.
Definition: map.hpp:37
editor_action_apply_mask(const gamemap &mask)
Definition: action.hpp:326
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:166
t_translation::t_terrain t_
Definition: action.hpp:257
std::deque< editor_action * > actions_
The action pointers owned by this action chain.
Definition: action.hpp:169
editor_action * pop_last_action()
Remove the last added action and return it, transferring ownership to the caller. ...
Definition: action.cpp:122
Base class for actions which in addition to acting on a hex, act with one terrain type...
Definition: action.hpp:192
GLenum GLint GLuint mask
Definition: glew.h:1813
Manage the empty-palette in the editor.
Definition: action.cpp:28
const char * get_name() const
Definition: action.hpp:364
Paint the same terrain on a number of locations on the map.
Definition: action.hpp:244
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:47
editor_action_shuffle_area(const std::set< map_location > &area)
Definition: action.hpp:357
Encapsulates the map of the game.
Definition: location.hpp:38
editor_action_whole_map(const editor_map &m)
Definition: action.hpp:41
const char * get_name() const
Definition: action.hpp:200
const char * get_name() const
Definition: action.hpp:163
t_translation::t_terrain fill_
Definition: action.hpp:320
editor_action_location(map_location loc)
Definition: action.hpp:180
editor_action_apply_mask * clone() const
Action cloning.
Definition: action.cpp:265
editor_action_fill(map_location loc, const t_translation::t_terrain &t, bool one_layer=false)
Definition: action.hpp:267
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:227
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:289
int action_count() const
Go through the chain and add up all the action counts.
Definition: action.cpp:104
const char * get_name() const
Definition: action.hpp:47
editor_action_chain(std::deque< editor_action * > actions)
Create an action chain from a deque of action pointers.
Definition: action.hpp:100
Base class for all editor actions.
Definition: action_base.hpp:41
void prepend_action(editor_action *a)
Add an action at the beginning of the chain.
Definition: action.cpp:116
editor_action_whole_map * clone() const
Action cloning.
Definition: action.cpp:67
Base class for actions that: 1) operate on an area 2) can be used as undo for a click-drag operation ...
Definition: action.hpp:58
This class wraps around a map to provide a concise interface for the editor to work with...
Definition: map_context.hpp:41
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action.cpp:297
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:202
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:185
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action.cpp:173
editor_action_starting_position * clone() const
Action cloning.
Definition: action.cpp:217
const GLdouble * m
Definition: glew.h:6968
editor_action_resize_map(int x_size, int y_size, int x_offset, int y_offset, const t_translation::t_terrain &fill=t_translation::NONE_TERRAIN)
Definition: action.hpp:307
editor_action_fill * clone() const
Action cloning.
Definition: action.cpp:198
~editor_action_chain()
The destructor deletes all the owned action pointers.
Definition: action.cpp:94
editor_action_chain()
Create an empty action chain.
Definition: action.hpp:85
void perform_without_undo(map_context &m) const
Perform all the actions in order.
Definition: action.cpp:144
const char * get_name() const
Definition: action.hpp:314
editor_action_area(const std::set< map_location > &area)
Definition: action.hpp:211
editor_action_create_mask * clone() const
Action cloning.
Definition: action.cpp:275
editor_action_resize_map * clone() const
Action cloning.
Definition: action.cpp:255
std::set< map_location > area_
Definition: action.hpp:218
const char * get_name() const
Definition: action.hpp:71
editor_action_location_terrain(map_location loc, const t_translation::t_terrain &t)
Definition: action.hpp:195
GLsizei const GLcharARB ** string
Definition: glew.h:4503
editor_action_chain * clone() const
Action cloning.
Definition: action.cpp:100
const char * get_name() const
Definition: action.hpp:332
GLenum target
Definition: glew.h:5190
const char * get_name() const
Definition: action.hpp:216
editor_action_chain(editor_action *action)
Create an action chain by wrapping around a single action pointer.
Definition: action.hpp:109