载入中...
搜索中...
未找到
EditorHistory.cpp
浏览该文件的文档.
2
3#include "common/Exception.h"
4
5namespace eve::level_editing {
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())
64 return eve::editing::failed<std::string>(editing::Status::NotFound, editing::RuleId("editor.history.nothing-to-undo"),
65 "Editor history has no action to undo");
66 Action a = undoStack_.back();
67 undoStack_.pop_back();
68 redoStack_.push_back(a);
69 lastApplied_ = a;
70 hasLast_ = true;
71 lastWasUndo_ = true;
72 return eve::editing::applied<std::string>(a.name);
73}
74
76 if (redoStack_.empty())
77 return eve::editing::failed<std::string>(editing::Status::NotFound, editing::RuleId("editor.history.nothing-to-redo"),
78 "Editor history has no action to redo");
79 Action a = redoStack_.back();
80 redoStack_.pop_back();
81 undoStack_.push_back(a);
82 lastApplied_ = a;
83 hasLast_ = true;
84 lastWasUndo_ = false;
85 return eve::editing::applied<std::string>(a.name);
86}
87
88bool EditorHistory::undo() { return undoAction().code() == editing::Status::Applied; }
89
90bool EditorHistory::redo() { return redoAction().code() == editing::Status::Applied; }
91
93 if (!hasLast_)
94 return eve::editing::failed<void>(editing::Status::NotFound, editing::RuleId("editor.history.no-last-action"),
95 "Editor history has no applied action");
96 if (lastApplied_.kind != "tiles")
97 return eve::editing::failed<void>(editing::Status::Unsupported, editing::RuleId("editor.history.action-not-tiles"),
98 "The latest history action does not contain tile changes");
99 for (const auto &t : lastApplied_.tiles) {
100 if (!buffer.containsCell(t.x, t.y)) continue;
101 buffer.setGid(t.x, t.y, lastWasUndo_ ? t.oldGid : t.newGid);
102 }
103 return eve::editing::applied<void>();
104}
105
109
110std::string EditorHistory::getLastActionName() const { return hasLast_ ? lastApplied_.name : ""; }
111std::string EditorHistory::getLastActionKind() const { return hasLast_ ? lastApplied_.kind : ""; }
112std::string EditorHistory::getLastPayload() const { return hasLast_ ? lastApplied_.payload : ""; }
113
115 return hasLast_ ? static_cast<int>(lastApplied_.tiles.size()) : 0;
116}
117
118bool EditorHistory::validLastTile(int index) const {
119 return hasLast_ && index >= 0 && index < static_cast<int>(lastApplied_.tiles.size());
120}
121
123 if (!validLastTile(index)) throw Exception("EditorHistory::getLastTileX: bad index");
124 return lastApplied_.tiles[index].x;
125}
127 if (!validLastTile(index)) throw Exception("EditorHistory::getLastTileY: bad index");
128 return lastApplied_.tiles[index].y;
129}
131 if (!validLastTile(index)) throw Exception("EditorHistory::getLastTileOldGid: bad index");
132 return lastApplied_.tiles[index].oldGid;
133}
135 if (!validLastTile(index)) throw Exception("EditorHistory::getLastTileNewGid: bad index");
136 return lastApplied_.tiles[index].newGid;
137}
138
139} // namespace eve::level_editing
Value::Object payload
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
std::string name
MeleePoint3 a
Definition MeleeHit.cpp:40
std::unique_ptr< gpgpu::GpuBuffer > buffer
Definition OnnxGpgpu.cpp:26
float t
uint32_t index
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
Move-only operation result carrying either a value or Status.
Definition Result.h:155
UUID-backed identifier adapter for legacy textual boundaries.
Definition Identity.h:314
bool redo()
Compatibility-only boolean projection of redoAction().
int getUndoCount() const
Returns the undo count.
std::string getLastPayload() const
Returns the last payload.
std::string getLastActionKind() const
Returns the last action kind.
int getLastTileCount() const
Returns the last tile count.
int getLastTileY(int index) const
Returns the last tile y.
void beginGroup(const std::string &name)
Begins group.
editing::Result< void > applyLastToBufferChecked(TileBuffer &buffer)
Apply last undone/redone tile group to buffer (no-op for opaque actions).
void push(const std::string &name, const std::string &payload)
Pushes .
editing::Result< std::string > undoAction()
Move the latest action to redo history and return its stable name.
bool undo()
Compatibility-only boolean projection of undoAction().
bool applyLastToBuffer(TileBuffer *buffer)
Compatibility-only nullable pointer projection of applyLastToBufferChecked().
int getLastTileNewGid(int index) const
Returns the last tile new gid.
void recordTile(int x, int y, int oldGid, int newGid)
Record tile.
int getLastTileOldGid(int index) const
Returns the last tile old gid.
int getRedoCount() const
Returns the redo count.
editing::Result< std::string > redoAction()
Move the latest redo action back to undo history and return its stable name.
int getLastTileX(int index) const
Returns the last tile x.
std::string getLastActionName() const
Returns the last action name.
Independent GID grid for map brushes (no hard dependency on map.TileLayer).
Definition TileBuffer.h:11