action.h

main 235 lines · 4.5 KB Raw
Alex Alex Commit Map Editor 18/10/2026 23:40
1//////////////////////////////////////////////////////////////////////
2// This file is part of Remere's Map Editor
3//////////////////////////////////////////////////////////////////////
4// Remere's Map Editor is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Remere's Map Editor is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//////////////////////////////////////////////////////////////////////
17
18#ifndef RME_ACTION_H_
19#define RME_ACTION_H_
20
21#include "position.h"
22
23#include <deque>
24
25class Editor;
26class Tile;
27class House;
28class Waypoint;
29class Change;
30class Action;
31class BatchAction;
32class ActionQueue;
33
34enum ChangeType {
35 CHANGE_NONE,
36 CHANGE_TILE,
37 CHANGE_MOVE_HOUSE_EXIT,
38 CHANGE_MOVE_WAYPOINT,
39};
40
41class Change {
42private:
43 ChangeType type;
44 void* data;
45
46 Change();
47
48public:
49 Change(Tile* tile);
50 static Change* Create(House* house, const Position& where);
51 static Change* Create(Waypoint* wp, const Position& where);
52 ~Change();
53 void clear();
54
55 ChangeType getType() const {
56 return type;
57 }
58 void* getData() const {
59 return data;
60 }
61
62 // Get memory footprint
63 uint32_t memsize() const;
64
65 friend class Action;
66};
67
68typedef std::vector<Change*> ChangeList;
69
70// A dirty list represents a list of all tiles that was changed in an action
71class DirtyList {
72public:
73 DirtyList();
74 ~DirtyList();
75
76 struct ValueType {
77 uint32_t pos;
78 uint32_t floors;
79 };
80
81 uint32_t owner;
82
83protected:
84 struct Comparator {
85 bool operator()(const ValueType& a, const ValueType& b) const {
86 return a.pos < b.pos;
87 }
88 };
89
90public:
91 typedef std::set<ValueType, Comparator> SetType;
92
93 void AddPosition(int x, int y, int z);
94 void AddChange(Change* c);
95 bool Empty() const {
96 return iset.empty() && ichanges.empty();
97 }
98 SetType& GetPosList();
99 ChangeList& GetChanges();
100
101protected:
102 SetType iset;
103 ChangeList ichanges;
104};
105
106enum ActionIdentifier {
107 ACTION_MOVE,
108 ACTION_REMOTE,
109 ACTION_SELECT,
110 ACTION_DELETE_TILES,
111 ACTION_CUT_TILES,
112 ACTION_PASTE_TILES,
113 ACTION_RANDOMIZE,
114 ACTION_BORDERIZE,
115 ACTION_DRAW,
116 ACTION_SWITCHDOOR,
117 ACTION_ROTATE_ITEM,
118 ACTION_REPLACE_ITEMS,
119 ACTION_CHANGE_PROPERTIES,
120};
121
122class Action {
123public:
124 virtual ~Action();
125
126 void addChange(Change* t) {
127 changes.push_back(t);
128 }
129
130 // Get memory footprint
131 size_t approx_memsize() const;
132 size_t memsize() const;
133 size_t size() const {
134 return changes.size();
135 }
136 ActionIdentifier getType() const {
137 return type;
138 }
139
140 void commit(DirtyList* dirty_list);
141 bool isCommited() const {
142 return commited;
143 }
144 void undo(DirtyList* dirty_list);
145 void redo(DirtyList* dirty_list) {
146 commit(dirty_list);
147 }
148
149protected:
150 Action(Editor& editor, ActionIdentifier ident);
151
152 bool commited;
153 ChangeList changes;
154 Editor& editor;
155 ActionIdentifier type;
156
157 friend class ActionQueue;
158};
159
160typedef std::vector<Action*> ActionVector;
161
162class BatchAction {
163public:
164 virtual ~BatchAction();
165
166 void resetTimer() {
167 timestamp = 0;
168 }
169
170 // Get memory footprint
171 size_t memsize(bool resize = false) const;
172 size_t size() const {
173 return batch.size();
174 }
175 ActionIdentifier getType() const {
176 return type;
177 }
178
179 virtual void addAction(Action* action);
180 virtual void addAndCommitAction(Action* action);
181
182protected:
183 BatchAction(Editor& editor, ActionIdentifier ident);
184
185 virtual void commit();
186 virtual void undo();
187 virtual void redo();
188
189 void merge(BatchAction* other);
190
191 Editor& editor;
192 int timestamp;
193 uint32_t memory_size;
194 ActionIdentifier type;
195 ActionVector batch;
196
197 friend class ActionQueue;
198};
199
200class ActionQueue {
201public:
202 ActionQueue(Editor& editor);
203 virtual ~ActionQueue();
204
205 typedef std::deque<BatchAction*> ActionList;
206
207 void resetTimer();
208
209 virtual Action* createAction(ActionIdentifier ident);
210 virtual Action* createAction(BatchAction* parent);
211 virtual BatchAction* createBatch(ActionIdentifier ident);
212
213 void addBatch(BatchAction* action, int stacking_delay = 0);
214 void addAction(Action* action, int stacking_delay = 0);
215
216 void undo();
217 void redo();
218 void clear();
219
220 bool canUndo() {
221 return current > 0;
222 }
223 bool canRedo() {
224 return current < actions.size();
225 }
226
227protected:
228 size_t current;
229 size_t memory_size;
230 Editor& editor;
231 ActionList actions;
232};
233
234#endif
235
Top