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 <string>
7#include <utility>
8#include <vector>
9
10#include "absl/status/status.h"
11#include "absl/strings/str_format.h"
14
15namespace yaze {
16namespace editor {
17
27 public:
28 GraphicsPixelEditAction(uint16_t sheet_id,
29 std::vector<uint8_t> before_data,
30 std::vector<uint8_t> after_data,
31 std::string description)
32 : sheet_id_(sheet_id),
33 before_data_(std::move(before_data)),
34 after_data_(std::move(after_data)),
35 description_(std::move(description)) {}
36
37 absl::Status Undo() override {
38 auto* sheets = gfx::Arena::Get().mutable_gfx_sheets();
39 if (sheet_id_ >= sheets->size()) {
40 return absl::OutOfRangeError(
41 absl::StrFormat("Sheet %02X out of range", sheet_id_));
42 }
43 auto& sheet = sheets->at(sheet_id_);
44 sheet.set_data(before_data_);
46 return absl::OkStatus();
47 }
48
49 absl::Status Redo() override {
50 auto* sheets = gfx::Arena::Get().mutable_gfx_sheets();
51 if (sheet_id_ >= sheets->size()) {
52 return absl::OutOfRangeError(
53 absl::StrFormat("Sheet %02X out of range", sheet_id_));
54 }
55 auto& sheet = sheets->at(sheet_id_);
56 sheet.set_data(after_data_);
58 return absl::OkStatus();
59 }
60
61 std::string Description() const override { return description_; }
62
63 size_t MemoryUsage() const override {
64 return before_data_.size() + after_data_.size();
65 }
66
67 bool CanMergeWith(const UndoAction& /*prev*/) const override {
68 // Pixel edit strokes are already batched per mouse-down/mouse-up,
69 // so merging is not needed.
70 return false;
71 }
72
73 private:
74 uint16_t sheet_id_;
75 std::vector<uint8_t> before_data_;
76 std::vector<uint8_t> after_data_;
77 std::string description_;
78};
79
80} // namespace editor
81} // namespace yaze
82
83#endif // YAZE_APP_EDITOR_GRAPHICS_UNDO_ACTIONS_H_
Undoable action for pixel edits on a graphics sheet.
bool CanMergeWith(const UndoAction &) const override
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")
GraphicsPixelEditAction(uint16_t sheet_id, std::vector< uint8_t > before_data, std::vector< uint8_t > after_data, std::string description)
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:393
static Arena & Get()
Definition arena.cc:21