The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
action.cpp
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
18  */
19 #define GETTEXT_DOMAIN "wesnoth-editor"
20 
21 #include "editor/action/action.hpp"
23 #include "gettext.hpp"
24 #include "util.hpp"
25 
26 #include "resources.hpp"
27 
28 namespace editor {
29 
32 
34 : id_(next_id_++)
35 {
37 #ifdef EDITOR_DEBUG_ACTION_LIFETIME
38  LOG_ED << "Action " << std::setw(2) << id_ << " ctor " << this << " (count is " << instance_count << "\n";
39 #endif
40 }
41 
43 {
45 #ifdef EDITOR_DEBUG_ACTION_LIFETIME
46  LOG_ED << "Action " << std::setw(2) << id_ << " dtor " << this << " (count is " << instance_count << "\n";
47 #endif
48 }
49 
51 {
52  return 1;
53 }
54 
56 {
57  return "Unknown action";
58 }
59 
61 {
64  return undo.release();
65 }
66 
68 {
69  return new editor_action_whole_map(*this);
70 }
72  mc.set_map(m_);
73 }
74 
76  : editor_action(), actions_()
77 {
78  for (editor_action* a : other.actions_) {
79  actions_.push_back(a->clone());
80  }
81 }
83 {
84  if (this == &other) return *this;
85  for (editor_action* a : actions_) {
86  delete a;
87  }
88  actions_.clear();
89  for (editor_action* a : other.actions_) {
90  actions_.push_back(a->clone());
91  }
92  return *this;
93 }
95 {
96  for (editor_action* a : actions_) {
97  delete a;
98  }
99 }
101 {
102  return new editor_action_chain(*this);
103 }
105  int count = 0;
106  for (const editor_action* a : actions_) {
107  if (a) {
108  count += a->action_count();
109  }
110  }
111  return count;
112 }
114  actions_.push_back(a);
115 }
117  actions_.push_front(a);
118 }
120  return actions_.empty();
121 }
123  if (empty()) throw editor_action_exception("pop_last_action requested on an empty action_chain");
124  editor_action* last = actions_.back();
125  actions_.pop_back();
126  return last;
127 }
129  if (empty()) throw editor_action_exception("pop_first_action requested on an empty action_chain");
130  editor_action* last = actions_.front();
131  actions_.pop_front();
132  return last;
133 }
136  for (editor_action* a : actions_) {
137  if (a != nullptr) {
138  undo->append_action(a->perform(mc));
139  }
140  }
141  std::reverse(undo->actions_.begin(), undo->actions_.end());
142  return undo.release();
143 }
145 {
146  for (editor_action* a : actions_) {
147  if (a != nullptr) {
148  a->perform_without_undo(mc);
149  }
150  }
151 }
152 
153 void editor_action_area::extend(const editor_map& /*map*/, const std::set<map_location>& locs)
154 {
155  area_.insert(locs.begin(), locs.end());
156 }
157 
159 {
160  return new editor_action_paste(*this);
161 }
162 void editor_action_paste::extend(const editor_map& map, const std::set<map_location>& locs)
163 {
164  paste_.add_tiles(map, locs);
165 }
167 {
171  return undo.release();
172 }
174 {
178 }
179 
180 
182 {
183  return new editor_action_paint_area(*this);
184 }
186 {
187  map_fragment mf(mc.get_map(), area_);
190  return undo.release();
191 }
193 {
196 }
197 
199 {
200  return new editor_action_fill(*this);
201 }
203 {
204  std::set<map_location> to_fill = mc.get_map().get_contiguous_terrain_tiles(loc_);
206  mc.draw_terrain(t_, to_fill, one_layer_);
208  return undo.release();
209 }
211 {
212  std::set<map_location> to_fill = mc.get_map().get_contiguous_terrain_tiles(loc_);
213  mc.draw_terrain(t_, to_fill, one_layer_);
215 }
216 
218 {
219  return new editor_action_starting_position(*this);
220 }
222 {
224  const std::string* old_loc_id = mc.get_map().is_starting_position(loc_);
226  if (old_loc_id != nullptr) {
227  // If another player was starting at the location, we actually perform two actions, so the undo is an action_chain.
228  editor_action_chain* undo_chain = new editor_action_chain();
229  undo_chain->append_action(new editor_action_starting_position(loc_, *old_loc_id));
230  undo_chain->append_action(new editor_action_starting_position(old_loc, loc_id_));
231  undo.reset(undo_chain);
232  LOG_ED << "ssp actual: " << *old_loc_id << " to " << map_location() << "\n";
233  mc.get_map().set_special_location(*old_loc_id, map_location());
234  } else {
235  undo.reset(new editor_action_starting_position(old_loc, loc_id_));
236  }
237  LOG_ED << "ssp actual: " << loc_id_ << " to " << loc_ << "\n";
240  return undo.release();
241 }
243 {
244  const std::string* old_id = mc.get_map().is_starting_position(loc_);
245  if (old_id != nullptr) {
246  mc.get_map().set_special_location(*old_id, map_location());
247  }
250 }
251 
252 
253 
254 
256 {
257  return new editor_action_resize_map(*this);
258 }
260 {
262  mc.set_needs_reload();
263 }
264 
266 {
267  return new editor_action_apply_mask(*this);
268 }
270 {
271  mc.get_map().overlay(mask_, config(), 0, 0, false);
273 }
274 
276 {
277  return new editor_action_create_mask(*this);
278 }
280 {
283 }
284 
286 {
287  return new editor_action_shuffle_area(*this);
288 }
290 {
291  map_fragment mf(mc.get_map(), area_);
294  return undo.release();
295 }
296 
298 {
299  std::vector<map_location> shuffle;
300  std::copy(area_.begin(), area_.end(), std::inserter(shuffle, shuffle.begin()));
301  std::random_shuffle(shuffle.begin(), shuffle.end());
302  std::vector<map_location>::const_iterator shuffle_it = shuffle.begin();
303  std::set<map_location>::const_iterator orig_it = area_.begin();
304  while (orig_it != area_.end()) {
305  t_translation::t_terrain tmp = mc.get_map().get_terrain(*orig_it);
306  mc.draw_terrain(mc.get_map().get_terrain(*shuffle_it), *orig_it);
307  mc.draw_terrain(tmp, *shuffle_it);
308  ++orig_it;
309  ++shuffle_it;
310  }
312 }
313 
314 } //end namespace editor
void add_changed_location(const map_location &loc)
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.
virtual std::string get_description() const
A textual description of the action.
Definition: action.cpp:55
std::string id_
Definition: formula.cpp:636
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
void resize(int width, int height, int x_offset, int y_offset, const t_translation::t_terrain &filler=t_translation::NONE_TERRAIN)
Resize the map.
Definition: editor_map.cpp:226
editor_action_shuffle_area * clone() const
Action cloning.
Definition: action.cpp:285
#define LOG_ED
editor_action_chain * perform(map_context &m) const
Perform all the actions in order and create a undo action chain.
Definition: action.cpp:134
editor_action_paste * clone() const
Action cloning.
Definition: action.cpp:158
editor_action_chain & operator=(const editor_action_chain &other)
Definition: action.cpp:82
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
void paste_into(gamemap &map, const map_location &loc) const
Paste the map fragment into the map, treating loc as the (0,0) point (offset).
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
void draw_terrain(const t_translation::t_terrain &terrain, const map_location &loc, bool one_layer_only=false)
Draw a terrain on a single location on the map.
editor_action_paint_area * clone() const
Action cloning.
Definition: action.cpp:181
editor_action_create_mask(const editor_map &target)
Definition: action.hpp:340
Set starting position action.
Definition: action.hpp:283
void set_needs_reload(bool value=true)
Setter for the reload flag.
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action.cpp:210
std::set< map_location > get_contiguous_terrain_tiles(const map_location &start) const
Get a contiguous set of tiles having the same terrain as the starting location.
Definition: editor_map.cpp:126
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action.cpp:242
void set_map(const editor_map &map)
virtual int action_count() const
Definition: action.cpp:50
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
std::set< map_location > get_offset_area(const map_location &offset) const
Get the area covered by this map fragment, shifted by an offset.
gamemap mask_to(const gamemap &target) const
A sort-of diff operation returning a mask that, when applied to the current editor_map, will transform it into the target map.
Definition: editor_map.cpp:271
std::vector< ttip > shuffle(const std::vector< ttip > &tips)
Shuffles the tips.
Definition: tips.cpp:49
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
editor_map & get_map()
Map accessor.
Definition: map_context.hpp:75
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
editor_action_apply_mask(const gamemap &mask)
Definition: action.hpp:326
void set_special_location(const std::string &id, const map_location &loc)
Definition: map.cpp:445
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
Editor action classes.
std::deque< editor_action * > actions_
The action pointers owned by this action chain.
Definition: action.hpp:169
void reset(T *p=nullptr)
Definition: util.hpp:442
editor_action * pop_last_action()
Remove the last added action and return it, transferring ownership to the caller. ...
Definition: action.cpp:122
virtual editor_action * clone() const =0
Action cloning.
Manage the empty-palette in the editor.
Definition: action.cpp:28
Templates and utility-routines for strings and numbers.
GLuint GLuint GLsizei count
Definition: glew.h:1221
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
void set_needs_terrain_rebuild(bool value=true)
Setter for the terrain rebuild flag.
Encapsulates the map of the game.
Definition: location.hpp:38
virtual ~editor_action()
Definition: action.cpp:42
editor_action_whole_map(const editor_map &m)
Definition: action.hpp:41
t_translation::t_terrain fill_
Definition: action.hpp:320
map_location special_location(const std::string &id) const
Definition: map.cpp:409
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
static int instance_count_
Definition: action_base.hpp:98
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
This class wraps around a map to provide a concise interface for the editor to work with...
Definition: map_context.hpp:41
void add_tiles(const gamemap &map, const std::set< map_location > &loc)
Add many locations and pull their info from the map.
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
t_translation::t_terrain get_terrain(const map_location &loc) const
Looks up terrain at a particular location.
Definition: map.cpp:341
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
T * release()
Definition: util.hpp:448
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
void set_needs_labels_reset(bool value=true)
Setter for the labels reset flag.
~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
const std::string * is_starting_position(const map_location &loc) const
returns the side number of the side starting at position loc, 0 if no such side exists.
Definition: map.cpp:439
void perform_without_undo(map_context &m) const
Perform all the actions in order.
Definition: action.cpp:144
virtual void perform_without_undo(map_context &) const =0
Perform the action without creating an undo action.
void overlay(const gamemap &m, const config &rules, int x=0, int y=0, bool border=false)
Overlays another map onto this one at the given position.
Definition: map.cpp:274
editor_action_create_mask * clone() const
Action cloning.
Definition: action.cpp:275
virtual 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:60
editor_action_resize_map * clone() const
Action cloning.
Definition: action.cpp:255
std::set< map_location > area_
Definition: action.hpp:218
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
editor_action_chain * clone() const
Action cloning.
Definition: action.cpp:100