yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
project_graphics_source.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_OVERWORLD_PROJECT_GRAPHICS_SOURCE_H_
2#define YAZE_APP_EDITOR_OVERWORLD_PROJECT_GRAPHICS_SOURCE_H_
3
4#include <cstddef>
5#include <cstdint>
6#include <map>
7#include <memory>
8#include <string>
9#include <vector>
10
11#include "absl/status/status.h"
12#include "absl/status/statusor.h"
13#include "nlohmann/json.hpp"
15
16namespace yaze::editor {
17
18// A source document for external project graphics. Tile indices belong to the
19// selected resource, never to the ROM's native graphics-sheet namespace.
21 public:
22 struct Slice {
23 std::string profile_id;
24 std::vector<int> parent_maps;
25 size_t source_offset = 0;
26 size_t byte_count = 0;
29 size_t first_resource_tile() const { return source_offset / 32; }
30 int first_character() const {
32 }
33 };
34
35 struct Resource {
36 std::string id;
37 std::string source_path;
38 std::string portable_path;
39 std::string sha256;
40 std::vector<uint8_t> original_bytes;
41 std::vector<uint8_t> pending_bytes;
42 std::vector<zelda3::PreviewTile8> tiles;
43 // All consumers, including profiles outside the opened parent map.
44 std::vector<Slice> slices;
45 std::vector<std::string> profile_ids;
46 std::vector<int> parent_maps;
47 bool dirty() const { return original_bytes != pending_bytes; }
48 };
49
55 ProjectGraphicsSource& operator=(const ProjectGraphicsSource&) = delete;
56
57 // source_root is an explicitly bound Oracle repository root. Both complete
58 // V2 catalogs must validate and describe the same resources and consumers.
59 // A failed Load clears the document, preventing cross-project edits.
60 absl::Status Load(const std::string& source_root,
61 const std::string& portable_catalog, int parent_map);
62 const std::vector<Resource>& resources() const { return resources_; }
63 const Resource* selected_resource() const;
64 std::string selected_resource_id() const {
65 const auto* resource = selected_resource();
66 return resource ? resource->id : std::string();
67 }
68 absl::Status SelectResource(const std::string& resource_id);
69 const std::string& source_root() const { return source_root_; }
70 const std::string& portable_catalog() const { return portable_catalog_; }
71 const std::string& profile_id() const { return profile_id_; }
72 int parent_map() const { return parent_map_; }
73 bool loaded() const { return !resources_.empty(); }
74 bool backend_available() const;
75
76 absl::StatusOr<uint8_t> GetPixel(size_t tile, int x, int y) const;
77 absl::Status SetPixel(size_t tile, int x, int y, uint8_t index);
78 absl::Status Undo();
79 absl::Status Redo();
80 bool can_undo() const;
81 bool can_redo() const;
82 bool dirty() const;
83 bool any_dirty() const;
84
85 // Atomically replace one loaded resource without recording pixel actions.
86 // The source_path overload binds to the exact current Resource::source_path
87 // and does not change the selected resource. Publication may change that path.
88 // A changed replacement clears only that resource's pixel history; callers
89 // own before/after snapshots for a combined import command's Undo/Redo.
90 absl::Status ReplaceResourceBytes(const std::vector<uint8_t>& bytes);
91 absl::Status ReplaceResourceBytes(const std::string& source_path,
92 const std::vector<uint8_t>& bytes);
93
94 // Resolve pending bytes directly into character overlays without temporary
95 // files or publication. Includes every known consumer of the loaded resources.
96 // Other profiles may also use resources not loaded in this document; callers
97 // must merge these overlays into their complete project graphics preview.
98 absl::StatusOr<zelda3::OverworldGraphicsPreview> PendingGraphicsPreview()
99 const;
100
101 // Preview writes only a private replacement and the publisher's review plan.
102 // Any pixel/history/selection change invalidates that held preview. Publish
103 // verifies the loaded disk bytes, replacement and plan before invoking apply.
104 absl::StatusOr<nlohmann::json> Preview();
105 absl::StatusOr<nlohmann::json> Publish();
106 bool has_preview() const { return preview_ != nullptr; }
107 const nlohmann::json& preview_json() const { return preview_json_; }
108
109 private:
110 struct PixelAction {
111 size_t tile;
112 int x;
113 int y;
114 uint8_t before;
115 uint8_t after;
116 };
117 struct History {
118 std::vector<PixelAction> undo;
119 std::vector<PixelAction> redo;
120 };
121 struct PreviewFiles;
122
123 absl::Status CheckUnchanged() const;
124 absl::Status ReplaceResourceBytesAt(size_t resource,
125 const std::vector<uint8_t>& bytes);
126 absl::Status CheckPixel(size_t tile, int x, int y) const;
127 void WritePixel(size_t tile, int x, int y, uint8_t index);
128 void InvalidatePreview();
129 std::string source_root_;
130 std::string portable_catalog_;
131 std::string profile_id_;
132 int parent_map_ = -1;
133 size_t selected_ = 0;
134 std::vector<Resource> resources_;
135 std::vector<History> histories_;
136 std::map<std::string, std::string> loaded_hashes_;
137 std::unique_ptr<PreviewFiles> preview_;
138 nlohmann::json preview_json_;
139};
140
141} // namespace yaze::editor
142#endif
void WritePixel(size_t tile, int x, int y, uint8_t index)
const nlohmann::json & preview_json() const
const std::vector< Resource > & resources() const
absl::Status ReplaceResourceBytes(const std::vector< uint8_t > &bytes)
absl::StatusOr< zelda3::OverworldGraphicsPreview > PendingGraphicsPreview() const
absl::Status CheckPixel(size_t tile, int x, int y) const
std::unique_ptr< PreviewFiles > preview_
std::map< std::string, std::string > loaded_hashes_
const std::string & portable_catalog() const
absl::Status ReplaceResourceBytesAt(size_t resource, const std::vector< uint8_t > &bytes)
absl::StatusOr< nlohmann::json > Preview()
ProjectGraphicsSource(ProjectGraphicsSource &&) noexcept
absl::Status Load(const std::string &source_root, const std::string &portable_catalog, int parent_map)
absl::Status SelectResource(const std::string &resource_id)
absl::StatusOr< uint8_t > GetPixel(size_t tile, int x, int y) const
absl::StatusOr< nlohmann::json > Publish()
absl::Status SetPixel(size_t tile, int x, int y, uint8_t index)
Editors are the view controllers for the application.