/
action.cpp
313 lines (284 loc) · 9.19 KB
/
action.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
Copyright (C) 2008 - 2017 by Tomasz Sniatowski <kailoran@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Editor action classes
*/
#define GETTEXT_DOMAIN "wesnoth-editor"
#include "editor/action/action.hpp"
#include "editor/map/map_context.hpp"
#include "gettext.hpp"
#include <memory>
namespace editor {
int editor_action::next_id_ = 1;
int editor_action::instance_count_ = 0;
editor_action::editor_action()
: id_(next_id_++)
{
instance_count_++;
#ifdef EDITOR_DEBUG_ACTION_LIFETIME
LOG_ED << "Action " << std::setw(2) << id_ << " ctor " << this << " (count is " << instance_count << "\n";
#endif
}
editor_action::~editor_action()
{
instance_count_--;
#ifdef EDITOR_DEBUG_ACTION_LIFETIME
LOG_ED << "Action " << std::setw(2) << id_ << " dtor " << this << " (count is " << instance_count << "\n";
#endif
}
int editor_action::action_count() const
{
return 1;
}
std::string editor_action::get_description() const
{
return "Unknown action";
}
editor_action* editor_action::perform(map_context& mc) const
{
std::unique_ptr<editor_action> undo(new editor_action_whole_map(mc.get_map()));
perform_without_undo(mc);
return undo.release();
}
editor_action_whole_map* editor_action_whole_map::clone() const
{
return new editor_action_whole_map(*this);
}
void editor_action_whole_map::perform_without_undo(map_context& mc) const {
mc.set_map(m_);
}
editor_action_chain::editor_action_chain(const editor::editor_action_chain &other)
: editor_action(), actions_()
{
for (editor_action* a : other.actions_) {
actions_.push_back(a->clone());
}
}
editor_action_chain& editor_action_chain::operator=(const editor_action_chain& other)
{
if (this == &other) return *this;
for (editor_action* a : actions_) {
delete a;
}
actions_.clear();
for (editor_action* a : other.actions_) {
actions_.push_back(a->clone());
}
return *this;
}
editor_action_chain::~editor_action_chain()
{
for (editor_action* a : actions_) {
delete a;
}
}
editor_action_chain* editor_action_chain::clone() const
{
return new editor_action_chain(*this);
}
int editor_action_chain::action_count() const {
int count = 0;
for (const editor_action* a : actions_) {
if (a) {
count += a->action_count();
}
}
return count;
}
void editor_action_chain::append_action(editor_action* a) {
actions_.push_back(a);
}
void editor_action_chain::prepend_action(editor_action* a) {
actions_.push_front(a);
}
bool editor_action_chain::empty() const {
return actions_.empty();
}
editor_action* editor_action_chain::pop_last_action() {
if (empty()) throw editor_action_exception("pop_last_action requested on an empty action_chain");
editor_action* last = actions_.back();
actions_.pop_back();
return last;
}
editor_action* editor_action_chain::pop_first_action() {
if (empty()) throw editor_action_exception("pop_first_action requested on an empty action_chain");
editor_action* last = actions_.front();
actions_.pop_front();
return last;
}
editor_action_chain* editor_action_chain::perform(map_context& mc) const {
std::unique_ptr<editor_action_chain> undo(new editor_action_chain());
for (editor_action* a : actions_) {
if (a != nullptr) {
undo->append_action(a->perform(mc));
}
}
std::reverse(undo->actions_.begin(), undo->actions_.end());
return undo.release();
}
void editor_action_chain::perform_without_undo(map_context& mc) const
{
for (editor_action* a : actions_) {
if (a != nullptr) {
a->perform_without_undo(mc);
}
}
}
void editor_action_area::extend(const editor_map& /*map*/, const std::set<map_location>& locs)
{
area_.insert(locs.begin(), locs.end());
}
editor_action_paste* editor_action_paste::clone() const
{
return new editor_action_paste(*this);
}
void editor_action_paste::extend(const editor_map& map, const std::set<map_location>& locs)
{
paste_.add_tiles(map, locs);
}
editor_action_paste* editor_action_paste::perform(map_context& mc) const
{
map_fragment mf(mc.get_map(), paste_.get_offset_area(offset_));
std::unique_ptr<editor_action_paste> undo(new editor_action_paste(mf));
perform_without_undo(mc);
return undo.release();
}
void editor_action_paste::perform_without_undo(map_context& mc) const
{
paste_.paste_into(mc.get_map(), offset_);
mc.add_changed_location(paste_.get_offset_area(offset_));
mc.set_needs_terrain_rebuild();
}
editor_action_paint_area* editor_action_paint_area::clone() const
{
return new editor_action_paint_area(*this);
}
editor_action_paste* editor_action_paint_area::perform(map_context& mc) const
{
map_fragment mf(mc.get_map(), area_);
std::unique_ptr<editor_action_paste> undo(new editor_action_paste(mf));
perform_without_undo(mc);
return undo.release();
}
void editor_action_paint_area::perform_without_undo(map_context& mc) const
{
mc.draw_terrain(t_, area_, one_layer_);
mc.set_needs_terrain_rebuild();
}
editor_action_fill* editor_action_fill::clone() const
{
return new editor_action_fill(*this);
}
editor_action_paint_area* editor_action_fill::perform(map_context& mc) const
{
std::set<map_location> to_fill = mc.get_map().get_contiguous_terrain_tiles(loc_);
std::unique_ptr<editor_action_paint_area> undo(new editor_action_paint_area(to_fill, mc.get_map().get_terrain(loc_)));
mc.draw_terrain(t_, to_fill, one_layer_);
mc.set_needs_terrain_rebuild();
return undo.release();
}
void editor_action_fill::perform_without_undo(map_context& mc) const
{
std::set<map_location> to_fill = mc.get_map().get_contiguous_terrain_tiles(loc_);
mc.draw_terrain(t_, to_fill, one_layer_);
mc.set_needs_terrain_rebuild();
}
editor_action_starting_position* editor_action_starting_position::clone() const
{
return new editor_action_starting_position(*this);
}
editor_action* editor_action_starting_position::perform(map_context& mc) const
{
std::unique_ptr<editor_action> undo;
const std::string* old_loc_id = mc.get_map().is_starting_position(loc_);
map_location old_loc = mc.get_map().special_location(loc_id_);
if (old_loc_id != nullptr) {
// If another player was starting at the location, we actually perform two actions, so the undo is an action_chain.
editor_action_chain* undo_chain = new editor_action_chain();
undo_chain->append_action(new editor_action_starting_position(loc_, *old_loc_id));
undo_chain->append_action(new editor_action_starting_position(old_loc, loc_id_));
undo.reset(undo_chain);
LOG_ED << "ssp actual: " << *old_loc_id << " to " << map_location() << "\n";
mc.get_map().set_special_location(*old_loc_id, map_location());
} else {
undo.reset(new editor_action_starting_position(old_loc, loc_id_));
}
LOG_ED << "ssp actual: " << loc_id_ << " to " << loc_ << "\n";
mc.get_map().set_special_location(loc_id_, loc_);
mc.set_needs_labels_reset();
return undo.release();
}
void editor_action_starting_position::perform_without_undo(map_context& mc) const
{
const std::string* old_id = mc.get_map().is_starting_position(loc_);
if (old_id != nullptr) {
mc.get_map().set_special_location(*old_id, map_location());
}
mc.get_map().set_special_location(loc_id_, loc_);
mc.set_needs_labels_reset();
}
editor_action_resize_map* editor_action_resize_map::clone() const
{
return new editor_action_resize_map(*this);
}
void editor_action_resize_map::perform_without_undo(map_context& mc) const
{
mc.get_map().resize(x_size_, y_size_, x_offset_, y_offset_, fill_);
mc.set_needs_reload();
}
editor_action_apply_mask* editor_action_apply_mask::clone() const
{
return new editor_action_apply_mask(*this);
}
void editor_action_apply_mask::perform_without_undo(map_context& mc) const
{
mc.get_map().overlay(mask_, config(), {0, 0});
mc.set_needs_terrain_rebuild();
}
editor_action_create_mask* editor_action_create_mask::clone() const
{
return new editor_action_create_mask(*this);
}
void editor_action_create_mask::perform_without_undo(map_context& mc) const
{
mc.set_map(editor_map(mc.get_map().mask_to(target_)));
mc.set_needs_terrain_rebuild();
}
editor_action_shuffle_area* editor_action_shuffle_area::clone() const
{
return new editor_action_shuffle_area(*this);
}
editor_action_paste* editor_action_shuffle_area::perform(map_context& mc) const
{
map_fragment mf(mc.get_map(), area_);
std::unique_ptr<editor_action_paste> undo(new editor_action_paste(mf));
perform_without_undo(mc);
return undo.release();
}
void editor_action_shuffle_area::perform_without_undo(map_context& mc) const
{
std::vector<map_location> shuffle;
std::copy(area_.begin(), area_.end(), std::inserter(shuffle, shuffle.begin()));
std::random_shuffle(shuffle.begin(), shuffle.end());
std::vector<map_location>::const_iterator shuffle_it = shuffle.begin();
std::set<map_location>::const_iterator orig_it = area_.begin();
while (orig_it != area_.end()) {
t_translation::terrain_code tmp = mc.get_map().get_terrain(*orig_it);
mc.draw_terrain(mc.get_map().get_terrain(*shuffle_it), *orig_it);
mc.draw_terrain(tmp, *shuffle_it);
++orig_it;
++shuffle_it;
}
mc.set_needs_terrain_rebuild();
}
} //end namespace editor