yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
undo_manager.cc
Go to the documentation of this file.
2
3#include <memory>
4#include <string>
5#include <utility>
6
7#include "absl/status/status.h"
8
9namespace yaze {
10namespace editor {
11
12void UndoManager::Push(std::unique_ptr<UndoAction> action) {
13 // Clear redo stack on new action
14 redo_stack_.clear();
15
16 // Try to merge with the top of the undo stack
17 if (!undo_stack_.empty() && action->CanMergeWith(*undo_stack_.back())) {
18 action->MergeWith(*undo_stack_.back());
19 undo_stack_.pop_back();
20 }
21
22 undo_stack_.push_back(std::move(action));
24}
25
26absl::Status UndoManager::Undo() {
27 if (undo_stack_.empty()) {
28 return absl::FailedPreconditionError("Nothing to undo");
29 }
30
31 auto& action = undo_stack_.back();
32 auto status = action->Undo();
33 if (!status.ok()) {
34 return status;
35 }
36
37 redo_stack_.push_back(std::move(action));
38 undo_stack_.pop_back();
39 return absl::OkStatus();
40}
41
42absl::Status UndoManager::Redo() {
43 if (redo_stack_.empty()) {
44 return absl::FailedPreconditionError("Nothing to redo");
45 }
46
47 auto& action = redo_stack_.back();
48 auto status = action->Redo();
49 if (!status.ok()) {
50 return status;
51 }
52
53 undo_stack_.push_back(std::move(action));
54 redo_stack_.pop_back();
55 return absl::OkStatus();
56}
57
58std::string UndoManager::GetUndoDescription() const {
59 if (undo_stack_.empty())
60 return "";
61 return undo_stack_.back()->Description();
62}
63
64std::string UndoManager::GetRedoDescription() const {
65 if (redo_stack_.empty())
66 return "";
67 return redo_stack_.back()->Description();
68}
69
70void UndoManager::Clear() {
71 undo_stack_.clear();
72 redo_stack_.clear();
73}
74
76 while (undo_stack_.size() > max_stack_size_) {
77 undo_stack_.pop_front();
78 }
79}
80
81} // namespace editor
82} // namespace yaze
void Push(std::unique_ptr< UndoAction > action)
absl::Status Redo()
Redo the top action. Returns error if stack is empty.
std::string GetRedoDescription() const
Description of the action that would be redone (for UI)
std::deque< std::unique_ptr< UndoAction > > redo_stack_
std::string GetUndoDescription() const
Description of the action that would be undone (for UI)
absl::Status Undo()
Undo the top action. Returns error if stack is empty.
std::deque< std::unique_ptr< UndoAction > > undo_stack_