载入中...
搜索中...
未找到
EditorHistory.cpp
浏览该文件的文档.
2
3#include "common/Exception.h"
4
5namespace eve::editor {
6
8 undoStack_.clear();
9 redoStack_.clear();
10 grouping_ = false;
11 pending_ = Action{};
12 hasLast_ = false;
13 lastWasUndo_ = false;
14 lastApplied_ = Action{};
15}
16
17void EditorHistory::trimRedo() { redoStack_.clear(); }
18
19void EditorHistory::push(const std::string &name, const std::string &payload) {
20 if (grouping_) throw Exception("EditorHistory::push: finish beginGroup/endGroup first");
21 Action a;
22 a.kind = "opaque";
23 a.name = name;
24 a.payload = payload;
25 undoStack_.push_back(a);
26 trimRedo();
27}
28
29void EditorHistory::beginGroup(const std::string &name) {
30 if (grouping_) throw Exception("EditorHistory::beginGroup: already grouping");
31 grouping_ = true;
32 pending_ = Action{};
33 pending_.kind = "tiles";
34 pending_.name = name;
35}
36
37void EditorHistory::recordTile(int x, int y, int oldGid, int newGid) {
38 if (!grouping_) throw Exception("EditorHistory::recordTile: call beginGroup first");
39 TileChange t;
40 t.x = x;
41 t.y = y;
42 t.oldGid = oldGid;
43 t.newGid = newGid;
44 pending_.tiles.push_back(t);
45}
46
48 if (!grouping_) throw Exception("EditorHistory::endGroup: not grouping");
49 grouping_ = false;
50 if (!pending_.tiles.empty()) {
51 undoStack_.push_back(pending_);
52 trimRedo();
53 }
54 pending_ = Action{};
55}
56
57bool EditorHistory::canUndo() const { return !undoStack_.empty(); }
58bool EditorHistory::canRedo() const { return !redoStack_.empty(); }
59int EditorHistory::getUndoCount() const { return static_cast<int>(undoStack_.size()); }
60int EditorHistory::getRedoCount() const { return static_cast<int>(redoStack_.size()); }
61
63 if (undoStack_.empty()) return false;
64 Action a = undoStack_.back();
65 undoStack_.pop_back();
66 redoStack_.push_back(a);
67 lastApplied_ = a;
68 hasLast_ = true;
69 lastWasUndo_ = true;
70 return true;
71}
72
74 if (redoStack_.empty()) return false;
75 Action a = redoStack_.back();
76 redoStack_.pop_back();
77 undoStack_.push_back(a);
78 lastApplied_ = a;
79 hasLast_ = true;
80 lastWasUndo_ = false;
81 return true;
82}
83
85 if (!hasLast_ || !buffer) return false;
86 if (lastApplied_.kind != "tiles") return false;
87 for (const auto &t : lastApplied_.tiles) {
88 if (!buffer->inBounds(t.x, t.y)) continue;
89 buffer->setGid(t.x, t.y, lastWasUndo_ ? t.oldGid : t.newGid);
90 }
91 return true;
92}
93
94std::string EditorHistory::getLastActionName() const { return hasLast_ ? lastApplied_.name : ""; }
95std::string EditorHistory::getLastActionKind() const { return hasLast_ ? lastApplied_.kind : ""; }
96std::string EditorHistory::getLastPayload() const { return hasLast_ ? lastApplied_.payload : ""; }
97
99 return hasLast_ ? static_cast<int>(lastApplied_.tiles.size()) : 0;
100}
101
102bool EditorHistory::validLastTile(int index) const {
103 return hasLast_ && index >= 0 && index < static_cast<int>(lastApplied_.tiles.size());
104}
105
106int EditorHistory::getLastTileX(int index) const {
107 if (!validLastTile(index)) throw Exception("EditorHistory::getLastTileX: bad index");
108 return lastApplied_.tiles[index].x;
109}
110int EditorHistory::getLastTileY(int index) const {
111 if (!validLastTile(index)) throw Exception("EditorHistory::getLastTileY: bad index");
112 return lastApplied_.tiles[index].y;
113}
115 if (!validLastTile(index)) throw Exception("EditorHistory::getLastTileOldGid: bad index");
116 return lastApplied_.tiles[index].oldGid;
117}
119 if (!validLastTile(index)) throw Exception("EditorHistory::getLastTileNewGid: bad index");
120 return lastApplied_.tiles[index].newGid;
121}
122
123} // namespace eve::editor
int y
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
uint32_t a
const char * name
Definition RockMesh.cpp:21
std::string getLastActionName() const
int getLastTileY(int index) const
int getLastTileOldGid(int index) const
int getLastTileX(int index) const
int getLastTileNewGid(int index) const
void recordTile(int x, int y, int oldGid, int newGid)
std::string getLastActionKind() const
void push(const std::string &name, const std::string &payload)
bool applyLastToBuffer(TileBuffer *buffer)
Apply last undone/redone tile group to buffer (no-op for opaque actions).
std::string getLastPayload() const
void beginGroup(const std::string &name)
Independent GID grid for map brushes (no hard dependency on map.TileLayer).
Definition TileBuffer.h:9
bool inBounds(int x, int y) const
void setGid(int x, int y, int gid)