-
Notifications
You must be signed in to change notification settings - Fork 224
/
action.h
242 lines (189 loc) · 5.34 KB
/
action.h
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
//////////////////////////////////////////////////////////////////////
// This file is part of Remere's Map Editor
//////////////////////////////////////////////////////////////////////
// Remere's Map Editor 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 3 of the License, or
// (at your option) any later version.
//
// Remere's Map Editor is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////
#ifndef RME_ACTION_H_
#define RME_ACTION_H_
#include "position.h"
#include <deque>
class Editor;
class Tile;
class House;
class Waypoint;
class Change;
class Action;
class BatchAction;
class ActionQueue;
enum ActionIdentifier {
ACTION_MOVE,
ACTION_REMOTE,
ACTION_SELECT,
ACTION_UNSELECT,
ACTION_DELETE_TILES,
ACTION_CUT_TILES,
ACTION_PASTE_TILES,
ACTION_RANDOMIZE,
ACTION_BORDERIZE,
ACTION_DRAW,
ACTION_ERASE,
ACTION_SWITCHDOOR,
ACTION_ROTATE_ITEM,
ACTION_REPLACE_ITEMS,
ACTION_CHANGE_PROPERTIES,
};
enum ChangeType {
CHANGE_NONE,
CHANGE_TILE,
CHANGE_MOVE_HOUSE_EXIT,
CHANGE_MOVE_WAYPOINT,
};
struct HouseData {
uint32_t id;
Position position;
};
struct WaypointData {
std::string id;
Position position;
};
class Change
{
Change();
public:
Change(Tile* tile);
~Change();
static Change* Create(House* house, const Position& position);
static Change* Create(Waypoint* waypoint, const Position& position);
void clear();
ChangeType getType() const noexcept { return type; }
void* getData() const noexcept { return data; }
uint32_t memsize() const;
private:
ChangeType type;
void* data;
friend class Action;
};
typedef std::vector<Change*> ChangeList;
// A dirty list represents a list of all tiles that was changed in an action
class DirtyList
{
public:
struct ValueType {
uint32_t pos;
uint32_t floors;
};
uint32_t owner = 0;
protected:
struct Comparator {
bool operator()(const ValueType& a, const ValueType& b) const {
return a.pos < b.pos;
}
};
public:
typedef std::set<ValueType, Comparator> SetType;
void AddPosition(int x, int y, int z);
void AddChange(Change* c);
bool Empty() const { return iset.empty() && ichanges.empty(); }
SetType& GetPosList();
ChangeList& GetChanges();
protected:
SetType iset;
ChangeList ichanges;
};
class Action
{
public:
virtual ~Action();
void addChange(Change* t) {
changes.push_back(t);
}
// Get memory footprint
size_t approx_memsize() const;
size_t memsize() const;
size_t size() const noexcept { return changes.size(); }
bool empty() const noexcept { return changes.empty(); }
ActionIdentifier getType() const noexcept { return type; }
void commit(DirtyList* dirty_list);
bool isCommited() const noexcept { return commited; }
void undo(DirtyList* dirty_list);
void redo(DirtyList* dirty_list) { commit(dirty_list); }
protected:
Action(Editor& editor, ActionIdentifier ident);
bool commited;
ChangeList changes;
Editor& editor;
ActionIdentifier type;
friend class ActionQueue;
};
typedef std::vector<Action*> ActionVector;
class BatchAction
{
public:
virtual ~BatchAction();
void resetTimer() noexcept { timestamp = 0; }
// Get memory footprint
size_t memsize(bool resize = false) const;
size_t size() const noexcept { return batch.size(); }
bool empty() const noexcept { return batch.empty(); }
ActionIdentifier getType() const noexcept { return type; }
const wxString& getLabel() const noexcept { return label; }
bool isNoSelection() const noexcept;
virtual void addAction(Action* action);
virtual void addAndCommitAction(Action* action);
protected:
BatchAction(Editor& editor, ActionIdentifier ident);
virtual void commit();
virtual void undo();
virtual void redo();
void merge(BatchAction* other);
Editor& editor;
int timestamp;
uint32_t memory_size;
ActionIdentifier type;
ActionVector batch;
wxString label;
friend class ActionQueue;
};
class ActionQueue
{
public:
ActionQueue(Editor& editor);
virtual ~ActionQueue();
typedef std::deque<BatchAction*> ActionList;
void resetTimer();
virtual Action* createAction(ActionIdentifier identifier) const;
virtual Action* createAction(BatchAction* parent) const;
virtual BatchAction* createBatch(ActionIdentifier identifier) const;
void addBatch(BatchAction* action, int stacking_delay = 0);
void addAction(Action* action, int stacking_delay = 0);
bool undo();
bool redo();
void clear();
const ActionList& getActions() const noexcept { return actions; }
const BatchAction* getAction(size_t index) const;
int getCurrentIndex() const noexcept { return current; }
bool canUndo() const noexcept { return current > 0; }
bool canRedo() const noexcept { return current < actions.size(); }
size_t size() const noexcept { return actions.size(); }
bool empty() const noexcept { return actions.empty(); }
bool hasChanges() const;
void generateLabels();
protected:
static wxString createLabel(ActionIdentifier type);
size_t current;
size_t memory_size;
Editor& editor;
ActionList actions;
};
#endif