yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
graphics_undo_actions.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_GRAPHICS_UNDO_ACTIONS_H_
2#define YAZE_APP_EDITOR_GRAPHICS_UNDO_ACTIONS_H_
3
4#include <cstddef>
5#include <cstdint>
6#include <functional>
7#include <string>
8#include <utility>
9#include <vector>
10
11#include "absl/status/status.h"
12#include "absl/strings/str_format.h"
15
16namespace yaze {
17namespace editor {
18
28 public:
29 using MarkDirtyFn = std::function<void(uint16_t)>;
30
31 GraphicsPixelEditAction(uint16_t sheet_id, std::vector<uint8_t> before_data,
32 std::vector<uint8_t> after_data,
33 std::string description, MarkDirtyFn mark_dirty)
34 : sheet_id_(sheet_id),
35 before_data_(std::move(before_data)),
36 after_data_(std::move(after_data)),
37 description_(std::move(description)),
38 mark_dirty_(std::move(mark_dirty)) {}
39
40 absl::Status Undo() override {
41 auto* sheets = gfx::Arena::Get().mutable_gfx_sheets();
42 if (sheet_id_ >= sheets->size()) {
43 return absl::OutOfRangeError(
44 absl::StrFormat("Sheet %02X out of range", sheet_id_));
45 }
46 auto& sheet = sheets->at(sheet_id_);
47 sheet.set_data(before_data_);
49 MarkDirty();
50 return absl::OkStatus();
51 }
52
53 absl::Status Redo() override {
54 auto* sheets = gfx::Arena::Get().mutable_gfx_sheets();
55 if (sheet_id_ >= sheets->size()) {
56 return absl::OutOfRangeError(
57 absl::StrFormat("Sheet %02X out of range", sheet_id_));
58 }
59 auto& sheet = sheets->at(sheet_id_);
60 sheet.set_data(after_data_);
62 MarkDirty();
63 return absl::OkStatus();
64 }
65
66 std::string Description() const override { return description_; }
67
68 size_t MemoryUsage() const override {
69 return before_data_.size() + after_data_.size();
70 }
71
72 bool CanMergeWith(const UndoAction& /*prev*/) const override {
73 // Pixel edit strokes are already batched per mouse-down/mouse-up,
74 // so merging is not needed.
75 return false;
76 }
77
78 private:
79 void MarkDirty() {
80 if (mark_dirty_) {
82 }
83 }
84
85 uint16_t sheet_id_;
86 std::vector<uint8_t> before_data_;
87 std::vector<uint8_t> after_data_;
88 std::string description_;
90};
91
92} // namespace editor
93} // namespace yaze
94
95#endif // YAZE_APP_EDITOR_GRAPHICS_UNDO_ACTIONS_H_
Undoable action for pixel edits on a graphics sheet.
bool CanMergeWith(const UndoAction &) const override
GraphicsPixelEditAction(uint16_t sheet_id, std::vector< uint8_t > before_data, std::vector< uint8_t > after_data, std::string description, MarkDirtyFn mark_dirty)
size_t MemoryUsage() const override
Approximate memory footprint for budget enforcement.
std::string Description() const override
Human-readable description (e.g., "Paint 12 tiles on map 5")
std::function< void(uint16_t)> MarkDirtyFn
Abstract base for all undoable actions (Command pattern)
Definition undo_action.h:20
auto mutable_gfx_sheets()
Get mutable reference to all graphics sheets.
Definition arena.h:178
void NotifySheetModified(int sheet_index)
Notify Arena that a graphics sheet has been modified.
Definition arena.cc:449
static Arena & Get()
Definition arena.cc:21