yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
sprite_editor_assets.cc
Go to the documentation of this file.
2
3#include <filesystem>
4
6#include "core/project.h"
9
10namespace yaze::editor {
19
21 behavior_candidate_.clear();
22 behavior_status_ = absl::OkStatus();
23 if (const auto* binding = current_sprite_binding()) {
24 std::copy(binding->sheets.begin(), binding->sheets.end(), current_sheets_);
25 gfx_buffer_loaded_ = false;
27 source_checked_ = false;
28 palette_binding_status_ = absl::OkStatus();
29 graphics_binding_status_ = absl::OkStatus();
30 }
31}
32
34 const std::array<uint8_t, 8>& sheets,
35 const std::array<project::SpritePaletteBinding, 8>& rows) {
37 return absl::FailedPreconditionError("Select a sprite asset");
38 auto updated = *current_sprite_binding();
39 updated.sheets = sheets;
40 updated.palette_rows = rows;
41 auto validation = updated;
42 if (validation.zsm_path.empty())
43 validation.zsm_path = "unsaved.zsm";
44 auto parsed =
46 if (!parsed.ok())
47 return parsed.status();
53 return absl::OkStatus();
54}
55
56absl::Status SpriteEditor::OpenSpriteAsset(const std::string& path) {
57 const auto absolute =
58 std::filesystem::absolute(path).lexically_normal().string();
59 auto found = std::find(custom_sprite_paths_.begin(),
60 custom_sprite_paths_.end(), absolute);
61 if (found != custom_sprite_paths_.end()) {
63 std::distance(custom_sprite_paths_.begin(), found);
65 custom_sprites_[current_custom_sprite_index_].editor.Frames.empty() ? -1
66 : 0;
68 custom_sprites_[current_custom_sprite_index_].animations.empty() ? -1
69 : 0;
71 animation_playing_ = false;
74 return absl::OkStatus();
75 }
76 LoadZsmFile(absolute);
77 return status_;
78}
79
80absl::Status SpriteEditor::SaveSpriteAsset(const std::string& path) {
82 return absl::FailedPreconditionError("Select a sprite asset");
83 const auto absolute =
84 std::filesystem::absolute(path).lexically_normal().string();
85 SaveZsmFile(absolute);
86 return status_;
87}
88
90 absl::Status result;
91 if (project()) {
92 for (const auto& asset : project()->sprite_assets) {
93 auto loaded = OpenSpriteAsset(project()->GetAbsolutePath(asset.zsm_path));
94 if (!loaded.ok() && result.ok())
95 result = loaded;
96 }
97 }
98 // An unavailable asset is a panel-local error, not a ROM/editor load failure.
99 status_ = absl::OkStatus();
100 return result;
101}
102
104 source_checked_ = true;
105 const auto* asset = current_sprite_binding();
106 if (!asset || asset->source_path.empty()) {
107 source_check_status_ = absl::NotFoundError("Asset has no imported source");
109 }
110 const auto root =
111 project() ? project()->GetAbsolutePath(project()->sprite_source_root)
112 : "";
113 auto source = zelda3::ReadSpriteSource(
114 root,
115 {"draw", asset->source_path, asset->source_label, asset->draw_adapter});
116 if (!source.ok())
117 source_check_status_ = source.status();
118 else if (core::ComputeSourceArtifactSha256(source->content) !=
119 asset->source_sha256)
120 source_check_status_ = absl::FailedPreconditionError(
121 "Draw source changed since import. Edited asset preserved; review "
122 "before export.");
123 else
124 source_check_status_ = absl::OkStatus();
126}
127
128absl::StatusOr<std::string> SpriteEditor::ExportCurrentSpriteDraw() {
130 return absl::FailedPreconditionError("Select a sprite asset");
132 *current_custom_sprite(), current_sprite_binding()->draw_adapter));
133 if (!current_sprite_binding()->source_path.empty())
136}
137
139
140 if (!palette_binding_status_.ok())
141 ImGui::TextWrapped("%s", palette_binding_status_.ToString().c_str());
142 if (!graphics_binding_status_.ok())
143 ImGui::TextWrapped("%s", graphics_binding_status_.ToString().c_str());
144 const auto* current = current_sprite_binding();
145 if (!current || !ImGui::CollapsingHeader("Asset bindings"))
146 return;
147 ImGui::TextWrapped(
148 "Save the ZSM, then save the project to retain these bindings.");
149 if (!current->catalog_key.empty())
150 ImGui::TextWrapped("Catalog: %s", current->catalog_key.c_str());
151 if (!current->source_path.empty()) {
152 ImGui::TextWrapped("Source: %s : %s", current->source_path.c_str(),
153 current->source_label.c_str());
154 ImGui::TextWrapped("Adapter: %s", current->draw_adapter.c_str());
155 if (ImGui::Button("Check source hash"))
157 if (source_checked_)
158 ImGui::TextWrapped("%s", source_check_status_.ok()
159 ? "Source matched at last check"
160 : source_check_status_.ToString().c_str());
161 }
162 constexpr const char* groups[] = {"auto", "global_sprites", "sprites_aux1",
163 "sprites_aux2", "sprites_aux3"};
164 auto rows = current->palette_rows;
165 bool changed = false;
166 for (int i = 0; i < 8; ++i) {
167 ImGui::PushID(i);
168 ImGui::Text("OAM palette %d", i);
169 int group = 0;
170 for (int j = 0; j < 5; ++j)
171 if (rows[i].group == groups[j])
172 group = j;
173 if (ImGui::Combo("Group", &group, groups, 5)) {
174 rows[i].group = groups[group];
175 changed = true;
176 }
177 if (rows[i].group != "auto" &&
178 ImGui::InputInt("Palette index", &rows[i].index)) {
179 rows[i].index = std::clamp(rows[i].index, 0, 255);
180 changed = true;
181 }
182 ImGui::PopID();
183 }
184 if (changed) {
185 (void)SetSpriteGraphics(current->sheets, rows);
187 }
188}
189} // namespace yaze::editor
project::YazeProject * project() const
Definition editor.h:358
absl::Status OpenSpriteAsset(const std::string &path)
std::vector< project::SpriteAssetBinding > custom_sprite_bindings_
const zsprite::ZSprite * current_custom_sprite() const
absl::StatusOr< std::string > ExportCurrentSpriteDraw()
void LoadZsmFile(const std::string &path)
absl::Status palette_binding_status_
absl::Status SaveSpriteAsset(const std::string &path)
std::vector< zsprite::ZSprite > custom_sprites_
const project::SpriteAssetBinding * current_sprite_binding() const
void SaveZsmFile(const std::string &path)
absl::Status graphics_binding_status_
std::vector< std::string > custom_sprite_paths_
absl::Status SetSpriteGraphics(const std::array< uint8_t, 8 > &sheets, const std::array< project::SpritePaletteBinding, 8 > &rows)
std::string ComputeSourceArtifactSha256(std::string_view content)
absl::StatusOr< std::string > ExportDrawTables(const zsprite::ZSprite &sprite)
absl::Status ValidateDrawAdapter(const zsprite::ZSprite &sprite, const std::string &adapter)
Editors are the view controllers for the application.
nlohmann::json SpriteAssetToJson(const SpriteAssetBinding &asset)
absl::StatusOr< SpriteAssetBinding > SpriteAssetFromJson(const nlohmann::json &value)
absl::StatusOr< SpriteSourceDocument > ReadSpriteSource(const std::string &source_root, const SpriteSourceBinding &binding)
#define RETURN_IF_ERROR(expr)
Definition snes.cc:22
std::vector< SpriteAssetBinding > sprite_assets
Definition project.h:333
std::string GetAbsolutePath(const std::string &relative_path) const
Definition project.cc:2028