10#include "absl/strings/str_format.h"
41using ImGui::BeginTable;
44using ImGui::Selectable;
45using ImGui::Separator;
46using ImGui::TableHeadersRow;
47using ImGui::TableNextColumn;
48using ImGui::TableNextRow;
49using ImGui::TableSetupColumn;
54void CopyStringToBuffer(
const std::string& src,
char (&dest)[N]) {
55 std::strncpy(dest, src.c_str(), N - 1);
65 long value = std::strtol(text.c_str(), &end, 0);
66 if (end == text.c_str() || errno == ERANGE) {
69 return static_cast<int>(value);
81 std::make_unique<VanillaSpriteEditorPanel>([
this]() {
83 DrawVanillaSpriteEditor();
85 ImGui::TextDisabled(tr(
"Load a ROM to view vanilla sprites"));
89 window_manager->RegisterWindowContent(
90 std::make_unique<CustomSpriteEditorPanel>(
92 window_manager->RegisterWindowContent(
112 return absl::OkStatus();
121 float current_time = ImGui::GetTime();
140 if (ImGui::IsKeyPressed(ImGuiKey_Space,
false) &&
141 !ImGui::GetIO().WantTextInput) {
146 if (ImGui::IsKeyPressed(ImGuiKey_LeftBracket,
false)) {
152 if (ImGui::IsKeyPressed(ImGuiKey_RightBracket,
false)) {
158 if (ImGui::GetIO().KeyCtrl && ImGui::IsKeyPressed(ImGuiKey_UpArrow,
false)) {
163 if (ImGui::GetIO().KeyCtrl &&
164 ImGui::IsKeyPressed(ImGuiKey_DownArrow,
false)) {
173 if (zsm_path.empty()) {
191 if (sprite_id < 0 || sprite_id >= 256)
209 if (ImGui::BeginTable(
"##SpriteCanvasTable", 3, ImGuiTableFlags_Resizable,
211 TableSetupColumn(
"Sprites List", ImGuiTableColumnFlags_WidthFixed, 256);
212 TableSetupColumn(
"Canvas", ImGuiTableColumnFlags_WidthStretch,
213 ImGui::GetContentRegionAvail().x);
214 TableSetupColumn(
"Tile Selector", ImGuiTableColumnFlags_WidthFixed, 256);
223 ImGui::TextWrapped(
"Select a sprite from the list to open its preview.");
234 if (sprite_id < 0 || sprite_id >= 256) {
240 absl::StrFormat(
"%02X %s###SpriteTab%02X", sprite_id,
243 ? ImGuiTabItemFlags_SetSelected
244 : ImGuiTabItemFlags_None;
245 const bool selected = ImGui::BeginTabItem(label.c_str(), &open, flags);
247 absl::StrFormat(
"SpriteTab%02X", sprite_id));
276 if (ImGui::BeginChild(
gui::GetID(
"##SpriteCanvas"),
277 ImGui::GetContentRegionAvail(),
true)) {
283 ImGui::SetCursorPos(ImVec2(10, 10));
287 "No static OAM layout for sprite $%02X. Open or import a ZSM in "
288 "Custom Sprites to preview authored frames.",
290 }
else if (layout->graphics_resource) {
292 "%s ($%02X) requires external graphics (%s). This editor does not "
293 "yet preview that graphics source.",
294 layout->name, layout->sprite_id, layout->graphics_resource);
301 ImGui::TextWrapped(
"Preview unavailable: %s",
302 std::string(error.message()).c_str());
306 Text(tr(
"Sprite: %s (0x%02X)"), layout->name, layout->sprite_id);
307 Text(tr(
"Tiles: %zu"), layout->tiles.size());
310 [](uint8_t pixel) { return (pixel & 0x0F) != 0; })) {
312 "The selected sheets contain no visible pixels for this layout. "
313 "Choose the sprite's spriteset or graphics sheets.");
315 if (ImGui::Button(tr(
"Edit preview copy"))) {
318 copy.sprName = std::string(layout->name) +
" (preview copy)";
325 visible.push_back(
"sprite.custom_editor");
332 "Uses the selected sheets and palette. Animation, behavior, and "
333 "runtime graphics changes are not simulated.");
343 if (
game_data() ==
nullptr || spriteset < 0 ||
357 std::copy(slots.begin(), slots.end(), binding.sheets.begin());
366 current_frame_ >=
static_cast<int>(sprite->editor.Frames.size())) {
367 return absl::FailedPreconditionError(
"Select a sprite frame");
370 return absl::FailedPreconditionError(
371 "Load a ROM before editing sprite pixels");
380 std::array<uint8_t, 8> slots;
384 const std::string source =
386 ? absl::StrFormat(
"room %03X CGRAM rows 8-15; sprite slots %02X",
390 "sprite asset binding; preview spriteset %02X",
392 : absl::StrFormat(
"preview spriteset %02X",
396 sprite->sprName, source);
400 context->render_preview = [frame, slots, palettes, owner]() {
406 return absl::FailedPreconditionError(
"Editor manager unavailable");
410 context->return_to_source = [
this, manager, sprite_index, frame_index,
412 if (sprite_index >= 0 &&
426 auto* editor_set = manager->GetCurrentEditorSet();
427 if (!editor_set || !editor_set->GetGraphicsEditor()) {
428 return absl::FailedPreconditionError(
"Graphics editor unavailable");
433 status = editor_set->GetGraphicsEditor()->OpenInContext(std::move(*
context));
437 return absl::OkStatus();
441 if (!ImGui::CollapsingHeader(tr(
"Spriteset preview"),
442 ImGuiTreeNodeFlags_DefaultOpen)) {
446 ImGui::TextDisabled(tr(
"Load a ROM to preview against spritesets."));
465 "Slots 0-3 take the static sprite sheets, slots 4-7 this spriteset's "
466 "values + 0x73 (OAM tiles 0x100-0x1FF).");
472 "Values %02X %02X %02X %02X -> sheets %02X %02X %02X %02X", values[0],
473 values[1], values[2], values[3], slots[4], slots[5], slots[6], slots[7]);
477 auto hex_list = [](
const std::vector<int>& ids) {
479 return std::string(
"-");
482 for (
size_t i = 0; i < ids.size() && i < 24; ++i) {
483 out += absl::StrFormat(
"%s%02X", i == 0 ?
"" :
" ", ids[i]);
485 if (ids.size() > 24) {
486 out += absl::StrFormat(
" (+%zu)", ids.size() - 24);
490 for (
int state = 0; state < 3; ++state) {
491 ImGui::TextWrapped(
"OW areas, state %d: %s", state,
492 hex_list(usage.ow_areas_by_state[state]).c_str());
494 ImGui::TextWrapped(
"Rooms: %s", hex_list(usage.rooms).c_str());
498 ImGui::InputScalar(
"Room", ImGuiDataType_U16, &
preview_room_,
nullptr,
499 nullptr,
"%03X", ImGuiInputTextFlags_CharsHexadecimal);
515 HOVER_HINT(
"Use the room's header spriteset (+0x40) and its sprite palette");
516 if (ImGui::Checkbox(tr(
"Room palette (CGRAM rows 8-15)"),
523 "Off: palette rows from the sprite binding (Sprite Asset panel). On: "
524 "the rows the game loads for the room above.");
533 current_frame_ >=
static_cast<int>(sprite->editor.Frames.size())) {
538 std::array<uint8_t, 8> slots;
543 for (uint8_t sheet : slots) {
544 key += absl::StrFormat(
"%02X", sheet);
546 for (
int tile : tiles) {
547 key += absl::StrFormat(
",%X", tile);
549 for (uint16_t sheet : options.reserved_sheets) {
550 key += absl::StrFormat(
"r%X", sheet);
567 " Frame %d: %zu of %zu 8x8 tiles need "
570 for (
size_t i = 0; i <
tile_issues_.size() && i < 8; ++i) {
577 :
"sheet unreadable");
578 ImGui::BulletText(
"tile 0x%03X, slot %d (sheet 0x%02X): %s", issue.tile,
579 issue.slot, issue.sheet, what);
582 ImGui::TextDisabled(
"... %zu more",
tile_issues_.size() - 8);
587 if (ImGui::BeginChild(
gui::GetID(
"sheet_label"),
588 ImVec2(ImGui::GetContentRegionAvail().x, 0),
true,
589 ImGuiWindowFlags_NoDecoration)) {
592 bool sheets_changed =
false;
596 for (
int i = 0; i < 8; i++) {
597 std::string sheet_label = absl::StrFormat(
"Sheet %d", i);
599 sheets_changed =
true;
606 if (sheets_changed) {
621 for (
int i = 0; i < 8; i++) {
625 if (sheet.is_active() && !sheet.texture())
626 sheet.CreateTexture();
636 if (ImGui::BeginChild(
gui::GetID(
"##SpritesList"),
637 ImVec2(ImGui::GetContentRegionAvail().x, 0),
true,
638 ImGuiWindowFlags_NoDecoration)) {
644 if (ImGui::IsItemClicked()) {
658 if (BeginTable(
"##CustomSpritesTable", 3,
659 ImGuiTableFlags_Resizable | ImGuiTableFlags_Borders,
661 TableSetupColumn(
"Sprite Data", ImGuiTableColumnFlags_WidthFixed, 300);
662 TableSetupColumn(
"Canvas", ImGuiTableColumnFlags_WidthStretch);
663 TableSetupColumn(
"Tilesheets", ImGuiTableColumnFlags_WidthFixed, 280);
670 if (ImGui::BeginChild(
"##SpriteMetadata", ImVec2(0, 0)))
694 if (!file_path.empty()) {
702 if (zsm_path.empty()) {
717 Text(tr(
"Loaded Sprites:"));
718 const float avail_y = std::max(0.0f, ImGui::GetContentRegionAvail().y);
719 const float metadata_reserve = std::min(160.0f, avail_y * 0.45f);
720 const float list_budget = std::max(0.0f, avail_y - metadata_reserve);
721 const float preferred_list = std::clamp(avail_y * 0.35f, 180.0f, 280.0f);
722 const float list_height = std::min(preferred_list, list_budget);
723 if (ImGui::BeginChild(
"SpriteList", ImVec2(0, list_height),
true)) {
728 ImGui::PushID(
static_cast<int>(i));
755 if (ImGui::BeginTabItem(tr(
"Properties"))) {
759 if (ImGui::BeginTabItem(tr(
"Animations"))) {
763 if (ImGui::BeginTabItem(tr(
"Behavior"))) {
767 if (ImGui::BeginTabItem(tr(
"Routines"))) {
774 Text(tr(
"No sprite selected"));
782 return absl::FailedPreconditionError(
783 "Select a custom sprite frame to copy");
786 if (current_frame_ < 0 || current_frame_ >=
static_cast<int>(frames.size()))
787 return absl::FailedPreconditionError(
"Select a frame to copy");
789 return absl::OkStatus();
795 if (sprite.editor.Frames.size() <= 1)
796 return absl::FailedPreconditionError(
797 "Cannot cut the last frame; copied it instead");
801 static_cast<int>(sprite.editor.Frames.size()) - 1);
805 return absl::OkStatus();
811 return absl::FailedPreconditionError(
812 "Copy a frame and select a destination sprite");
815 return absl::OutOfRangeError(
"ZSM animations address at most 256 frames");
818 current_frame_ =
static_cast<int>(sprite.editor.Frames.size()) - 1;
822 return absl::OkStatus();
828 new_sprite.
sprName =
"New Sprite";
834 new_sprite.
animations.emplace_back(0, 0, 1,
"Idle");
893 binding.zsm_path = path;
897 std::find_if(assets.begin(), assets.end(), [&](
const auto& asset) {
898 return project()->GetAbsolutePath(asset.zsm_path) == path;
900 if (found == assets.end())
901 assets.push_back(binding);
928 Text(tr(
"Sprite Info"));
931 static char name_buf[256];
932 CopyStringToBuffer(sprite.sprName, name_buf);
933 if (ImGui::InputText(tr(
"Name"), name_buf,
sizeof(name_buf))) {
934 sprite.sprName = name_buf;
935 sprite.property_sprname.Text = name_buf;
939 static char id_buf[32];
940 CopyStringToBuffer(sprite.property_sprid.Text, id_buf);
941 if (ImGui::InputText(tr(
"Sprite ID"), id_buf,
sizeof(id_buf))) {
942 sprite.property_sprid.Text = id_buf;
959 int prize = ParseIntOrDefault(sprite.property_prize.Text);
960 if (ImGui::InputInt(tr(
"Prize"), &prize)) {
961 sprite.property_prize.Text = std::to_string(std::clamp(prize, 0, 255));
965 int palette = ParseIntOrDefault(sprite.property_palette.Text);
966 if (ImGui::InputInt(tr(
"Palette"), &palette)) {
967 sprite.property_palette.Text = std::to_string(std::clamp(palette, 0, 7));
971 int oamnbr = ParseIntOrDefault(sprite.property_oamnbr.Text);
972 if (ImGui::InputInt(tr(
"OAM Count"), &oamnbr)) {
973 sprite.property_oamnbr.Text = std::to_string(std::clamp(oamnbr, 0, 255));
977 int hitbox = ParseIntOrDefault(sprite.property_hitbox.Text);
978 if (ImGui::InputInt(tr(
"Hitbox"), &hitbox)) {
979 sprite.property_hitbox.Text = std::to_string(std::clamp(hitbox, 0, 255));
983 int health = ParseIntOrDefault(sprite.property_health.Text);
984 if (ImGui::InputInt(tr(
"Health"), &health)) {
985 sprite.property_health.Text = std::to_string(std::clamp(health, 0, 255));
989 int damage = ParseIntOrDefault(sprite.property_damage.Text);
990 if (ImGui::InputInt(tr(
"Damage"), &damage)) {
991 sprite.property_damage.Text = std::to_string(std::clamp(damage, 0, 255));
999 Text(tr(
"Behavior Flags"));
1002 if (ImGui::BeginTable(
"BoolProps", 2, ImGuiTableFlags_None)) {
1004 ImGui::TableNextColumn();
1005 if (ImGui::Checkbox(tr(
"Blockable"), &sprite.property_blockable.IsChecked))
1007 if (ImGui::Checkbox(tr(
"Can Fall"), &sprite.property_canfall.IsChecked))
1009 if (ImGui::Checkbox(tr(
"Collision Layer"),
1010 &sprite.property_collisionlayer.IsChecked))
1012 if (ImGui::Checkbox(tr(
"Custom Death"),
1013 &sprite.property_customdeath.IsChecked))
1015 if (ImGui::Checkbox(tr(
"Damage Sound"),
1016 &sprite.property_damagesound.IsChecked))
1018 if (ImGui::Checkbox(tr(
"Deflect Arrows"),
1019 &sprite.property_deflectarrows.IsChecked))
1021 if (ImGui::Checkbox(tr(
"Deflect Projectiles"),
1022 &sprite.property_deflectprojectiles.IsChecked))
1024 if (ImGui::Checkbox(tr(
"Fast"), &sprite.property_fast.IsChecked))
1026 if (ImGui::Checkbox(tr(
"Harmless"), &sprite.property_harmless.IsChecked))
1028 if (ImGui::Checkbox(tr(
"Impervious"),
1029 &sprite.property_impervious.IsChecked))
1033 ImGui::TableNextColumn();
1034 if (ImGui::Checkbox(tr(
"Impervious Arrow"),
1035 &sprite.property_imperviousarrow.IsChecked))
1037 if (ImGui::Checkbox(tr(
"Impervious Melee"),
1038 &sprite.property_imperviousmelee.IsChecked))
1040 if (ImGui::Checkbox(tr(
"Interaction"),
1041 &sprite.property_interaction.IsChecked))
1043 if (ImGui::Checkbox(tr(
"Is Boss"), &sprite.property_isboss.IsChecked))
1045 if (ImGui::Checkbox(tr(
"Persist"), &sprite.property_persist.IsChecked))
1047 if (ImGui::Checkbox(tr(
"Shadow"), &sprite.property_shadow.IsChecked))
1049 if (ImGui::Checkbox(tr(
"Small Shadow"),
1050 &sprite.property_smallshadow.IsChecked))
1052 if (ImGui::Checkbox(tr(
"Stasis"), &sprite.property_statis.IsChecked))
1054 if (ImGui::Checkbox(tr(
"Statue"), &sprite.property_statue.IsChecked))
1056 if (ImGui::Checkbox(tr(
"Water Sprite"),
1057 &sprite.property_watersprite.IsChecked))
1071 if (ImGui::Button(tr(
"Copy draw tables (ASM)"))) {
1075 ImGui::SetClipboardText(tables->c_str());
1078 "Copy a draw-table candidate. Does not register or patch a sprite.");
1109 (
int)sprite.editor.Frames.size() - 1);
1114 Text(tr(
"Animations"));
1115 if (ImGui::Button(
ICON_MD_ADD " Add Animation")) {
1116 int frame_count =
static_cast<int>(sprite.editor.Frames.size());
1117 sprite.animations.emplace_back(0, std::clamp(frame_count - 1, 0, 255), 1,
1123 if (ImGui::BeginChild(
"AnimList", ImVec2(0, 120),
true)) {
1124 for (
size_t i = 0; i < sprite.animations.size(); i++) {
1125 auto& anim = sprite.animations[i];
1126 std::string label = anim.frame_name.empty() ?
"Unnamed" : anim.frame_name;
1127 ImGui::PushID(
static_cast<int>(i));
1145 Text(tr(
"Animation Properties"));
1147 static char anim_name[128];
1148 CopyStringToBuffer(anim.frame_name, anim_name);
1149 if (ImGui::InputText(tr(
"Name##Anim"), anim_name,
sizeof(anim_name))) {
1150 anim.frame_name = anim_name;
1154 int start = anim.frame_start;
1155 int end = anim.frame_end;
1156 int speed = anim.frame_speed;
1158 if (ImGui::SliderInt(
1159 tr(
"Start Frame"), &start, 0,
1160 std::clamp((
int)sprite.editor.Frames.size() - 1, 0, 255))) {
1161 anim.frame_start =
static_cast<uint8_t
>(start);
1162 anim.frame_end = std::max(anim.frame_start, anim.frame_end);
1163 end = anim.frame_end;
1166 if (ImGui::SliderInt(
1167 tr(
"End Frame"), &end, 0,
1168 std::clamp((
int)sprite.editor.Frames.size() - 1, 0, 255))) {
1169 anim.frame_end =
static_cast<uint8_t
>(end);
1170 anim.frame_start = std::min(anim.frame_start, anim.frame_end);
1173 if (ImGui::SliderInt(tr(
"Ticks per frame (60 Hz)"), &speed, 1, 255)) {
1174 anim.frame_speed =
static_cast<uint8_t
>(speed);
1180 bool used_by_action =
1181 std::any_of(actions.begin(), actions.end(), [&](
const auto& action) {
1182 return action.animation == current_animation_index_;
1184 ImGui::BeginDisabled(used_by_action);
1185 if (ImGui::Button(tr(
"Delete Animation")) && sprite.animations.size() > 1) {
1186 for (
auto& action : actions)
1189 sprite.animations.erase(sprite.animations.begin() +
1195 ImGui::EndDisabled();
1197 ImGui::TextDisabled(
1198 "Assign another animation to referencing actions before deleting.");
1212 current_frame_ =
static_cast<int>(sprite.editor.Frames.size()) - 1;
1229 HOVER_HINT(
"Delete the current frame and repair animation ranges");
1230 if (ImGui::Button(tr(
"Duplicate Frame")) &&
current_frame_ >= 0 &&
1232 current_frame_ =
static_cast<int>(sprite.editor.Frames.size()) - 1;
1236 ImGui::TextDisabled(
"Up to 256 frames; duplicate appends a frame.");
1239 if (ImGui::BeginChild(
"FrameList", ImVec2(0, 80),
true,
1240 ImGuiWindowFlags_HorizontalScrollbar)) {
1241 for (
size_t i = 0; i < sprite.editor.Frames.size(); i++) {
1242 ImGui::PushID(
static_cast<int>(i));
1243 std::string label = absl::StrFormat(
"F%d", i);
1245 ImGuiSelectableFlags_None, ImVec2(40, 40))) {
1264 frame.Tiles.emplace_back();
1273 if (ImGui::BeginChild(
"TileList", ImVec2(0, 100),
true)) {
1274 for (
size_t i = 0; i < frame.Tiles.size(); i++) {
1275 auto& tile = frame.Tiles[i];
1276 std::string label = absl::StrFormat(
"Tile %d (ID: %d)", i, tile.id);
1289 int tile_id = tile.id;
1290 if (ImGui::InputInt(tr(
"Tile ID"), &tile_id)) {
1291 tile.id =
static_cast<uint16_t
>(std::clamp(tile_id, 0, 511));
1298 if (ImGui::InputInt(tr(
"X offset"), &x)) {
1299 tile.x =
static_cast<uint8_t
>(std::clamp(x, -128, 127) +
1304 if (ImGui::InputInt(tr(
"Y offset"), &y)) {
1305 tile.y =
static_cast<uint8_t
>(std::clamp(y, -112, 143) +
1311 int pal = tile.palette;
1312 if (ImGui::SliderInt(tr(
"Palette##Tile"), &pal, 0, 7)) {
1313 tile.palette =
static_cast<uint8_t
>(pal);
1318 int priority = tile.priority;
1319 if (ImGui::SliderInt(tr(
"BG priority"), &priority, 0, 3)) {
1320 tile.priority =
static_cast<uint8_t
>(priority);
1323 ImGui::TextDisabled(
1324 "BG priority is stored; preview has no background layers.");
1325 if (ImGui::Checkbox(tr(
"16x16"), &tile.size)) {
1330 if (ImGui::Checkbox(tr(
"Flip X"), &tile.mirror_x)) {
1335 if (ImGui::Checkbox(tr(
"Flip Y"), &tile.mirror_y)) {
1340 if (ImGui::Button(tr(
"Edit selected OAM pixels"))) {
1348 if (ImGui::Button(tr(
"Duplicate Tile"))) {
1349 const auto copy = tile;
1350 frame.Tiles.push_back(copy);
1354 if (ImGui::Button(tr(
"Delete Tile"))) {
1393 sprite.userRoutines.emplace_back(
"New Routine",
"; ASM code here\n");
1399 if (ImGui::BeginChild(
"RoutineList", ImVec2(0, 100),
true)) {
1400 for (
size_t i = 0; i < sprite.userRoutines.size(); i++) {
1401 auto& routine = sprite.userRoutines[i];
1416 static char routine_name[128];
1417 CopyStringToBuffer(routine.name, routine_name);
1418 if (ImGui::InputText(tr(
"Routine Name"), routine_name,
1419 sizeof(routine_name))) {
1420 routine.name = routine_name;
1424 Text(tr(
"ASM Code:"));
1427 static char code_buffer[16384];
1428 CopyStringToBuffer(routine.code, code_buffer);
1429 if (ImGui::InputTextMultiline(
"##RoutineCode", code_buffer,
1430 sizeof(code_buffer), ImVec2(-1, 200))) {
1431 routine.code = code_buffer;
1435 if (ImGui::Button(tr(
"Delete Routine"))) {
1436 sprite.userRoutines.erase(sprite.userRoutines.begin() +
1451 const auto palette_revision =
1460 for (
int slot = 0; slot < 8; ++slot) {
1461 const auto revision =
1470 for (
int slot = 0; slot < 8; ++slot) {
1492 constexpr int kSheetWidth = 128;
1493 constexpr int kSheetHeight = 32;
1494 constexpr int kRowStride = 128;
1496 for (
int sheet_idx = 0; sheet_idx < 8; sheet_idx++) {
1499 !
game_data()->sheet_store.HasSheet(sheet_id)) {
1501 "Graphics sheet $%02X in slot %d is unavailable in this ROM session",
1502 sheet_id, sheet_idx));
1512 int dest_offset = sheet_idx * (kSheetHeight * kRowStride);
1514 const uint8_t* src_data = sheet.data();
1516 std::min(sheet.size(),
static_cast<size_t>(kSheetWidth * kSheetHeight));
1541 "Load ROM graphics and palettes before previewing sprites");
1557 if (global.empty() && aux1.empty() && aux2.empty() && aux3.empty() &&
1560 "Sprite palette colors are unavailable in this ROM session");
1562 if (
const auto* binding =
1610 origin_x, origin_y);
1620 for (
size_t col_idx = 0; col_idx < 16 && col_idx < sub_pal.size();
1622 combined_palette.
AddColor(sub_pal[col_idx]);
1625 while (combined_palette.
size() < (pal_idx + 1) * 16) {
1651 if (frame_index < 0 || frame_index >= (
int)sprite.editor.Frames.size()) {
1655 auto& frame = sprite.editor.Frames[frame_index];
1686 for (
size_t col_idx = 0; col_idx < 16 && col_idx < sub_pal.size();
1688 combined_palette.
AddColor(sub_pal[col_idx]);
1691 while (combined_palette.
size() < (pal_idx + 1) * 16) {
1713 for (
size_t i = 0; i < frame.Tiles.size(); i++) {
1714 const auto& tile = frame.Tiles[i];
1715 int tile_size = tile.size ? 16 : 8;
1721 int canvas_x = 128 + signed_x;
1722 int canvas_y = 128 + signed_y;
1726 ? ImVec4(0.0f, 1.0f, 0.0f, 0.8f)
1727 : ImVec4(1.0f, 1.0f, 0.0f, 0.3f);
1731 tile_size * 2, color);
1737 if (ImGui::BeginChild(
gui::GetID(
"##ZSpriteCanvas"),
1738 ImGui::GetContentRegionAvail(),
true)) {
1747 ImGui::SetCursorPos(ImVec2(10, 34));
1751 ImGui::TextWrapped(
"Preview unavailable: %s",
1752 std::string(error.message()).c_str());
1755 ImGui::SetCursorPos(ImVec2(10, 10));
1757 "Open a ZSM or import a draw copy to preview its frames.");
1766 ImGui::SetCursorPos(ImVec2(10, 10));
1768 std::snprintf(label,
sizeof(label), tr(
"Frame: %d | Tiles: %d"),
1774 const ImVec2 position = ImGui::GetCursorScreenPos();
1775 const ImVec2 size = ImGui::CalcTextSize(label);
1777 ImGui::GetWindowDrawList()->AddRectFilled(
1778 ImVec2(position.x - 3, position.y - 2),
1779 ImVec2(position.x + size.x + 3, position.y + size.y + 2),
1780 IM_COL32(24, 24, 24, 255), 3.0f);
1781 ImGui::TextColored(ImVec4(1, 1, 1, 1),
"%s", label);
1848 std::move(restore_fn)));
1865 static const std::string kEmptyPath;
project::ResourceLabelManager * resource_label()
The EditorManager controls the main editor window and manages the various editor classes.
EditorContext context() const
UndoManager undo_manager_
project::YazeProject * project() const
zelda3::GameData * game_data() const
EditorDependencies dependencies_
void SetPalettes(const gfx::PaletteGroup *palettes)
Set the palette group for color mapping.
void ClearBitmap(gfx::Bitmap &bitmap)
Clear the bitmap with transparent color.
void DrawFrame(gfx::Bitmap &bitmap, const zsprite::Frame &frame, int origin_x, int origin_y)
Draw all tiles in a ZSM frame.
bool IsReady() const
Check if drawer is ready to render.
void SetGraphicsBuffer(const uint8_t *buffer)
Set the graphics buffer for tile lookup.
ImVector< int > active_sprites_
absl::Status OpenSpriteAsset(const std::string &path)
int current_custom_sprite_index_
void UpdateAnimationPlayback(float delta_time)
std::vector< project::SpriteAssetBinding > custom_sprite_bindings_
absl::Status ReloadProjectSpriteAssets()
std::vector< zelda3::SpriteTileIssue > tile_issues_
absl::Status OpenSelectedOamGraphics()
void DrawAnimationPanel()
void EnsureCustomSpritePaths()
void ApplySpritesetToSheets(int spriteset)
void DrawSpriteAssetBindings()
const zsprite::ZSprite * current_custom_sprite() const
void BeginUndoTransaction()
void RefreshSpritePreviewGraphics(bool use_asset_binding)
std::array< uint64_t, 8 > loaded_preview_revisions_
void RenderZSpriteFrame(int frame_index)
absl::Status Paste() override
std::array< uint8_t, 8 > loaded_preview_sheets_
bool preview_use_room_palette_
absl::StatusOr< std::string > ExportCurrentSpriteDraw()
bool preview_needs_update_
void DrawZSpriteOnCanvas()
void LoadSpritePalettes(bool use_asset_binding=true)
std::optional< zsprite::Frame > frame_clipboard_
bool vanilla_preview_needs_update_
absl::Status Update() override
uint64_t preview_palette_revision_
void DrawSpriteBehaviorPanel()
void HandleEditorShortcuts()
gfx::PaletteGroup sprite_palettes_
void LoadZsmFile(const std::string &path)
bool sprite_mutated_this_frame_
void ApplyCurrentSpriteBinding()
void DrawUserRoutinesPanel()
absl::Status Cut() override
void RenderVanillaSprite(const zelda3::SpriteOamLayout &layout)
absl::Status palette_binding_status_
void DrawSpritesetPicker()
void Initialize() override
void DrawFrameTileWarnings()
void DrawVanillaSpriteEditor()
int selected_routine_index_
std::string behavior_candidate_
std::optional< std::map< int, zelda3::RoomGfxInfo > > usage_rooms_
const std::string & GetCurrentZsmPath() const
uint8_t current_sheets_[8]
SpriteDrawer sprite_drawer_
absl::Status asset_load_status_
gui::Canvas graphics_sheet_canvas_
int room_palette_cache_room_
void RestoreFromSnapshot(const SpriteSnapshot &snapshot)
bool undo_snapshot_pending_
std::vector< zsprite::ZSprite > custom_sprites_
std::vector< uint8_t > sprite_gfx_buffer_
gfx::Bitmap sprite_preview_bitmap_
absl::Status ReloadSpriteCatalog()
int current_animation_index_
const project::SpriteAssetBinding * current_sprite_binding() const
void DrawBooleanProperties()
void DrawSpritePropertiesPanel()
void SetCurrentZsmPath(const std::string &path)
absl::Status Copy() override
const zelda3::GameData * preview_graphics_owner_
absl::Status Save() override
std::optional< std::map< int, zelda3::OverworldAreaGfxInfo > > usage_areas_
void DrawCustomSpritesMetadata()
SpriteSnapshot undo_before_snapshot_
void SaveZsmFile(const std::string &path)
gui::Canvas sprite_canvas_
void CommitUndoTransaction()
SpriteSnapshot CaptureCurrentSpriteSnapshot() const
void DrawStatProperties()
absl::Status graphics_binding_status_
absl::Status graphics_context_status_
std::optional< gfx::PaletteGroup > room_palette_cache_
absl::Status CheckCurrentSpriteSource()
absl::Status Load() override
void LoadSpriteGraphicsBuffer()
std::vector< std::string > custom_sprite_paths_
void SelectVanillaSprite(int sprite_id)
std::string tile_check_key_
absl::Status draw_export_status_
gfx::Bitmap vanilla_preview_bitmap_
const zelda3::SpriteOamLayout * CurrentPreviewLayout() const
void Push(std::unique_ptr< UndoAction > action)
void SetVisibleWindows(size_t session_id, const std::vector< std::string > &window_ids)
Set which panels should be visible for a session.
void RegisterWindowContent(std::unique_ptr< WindowContent > window)
Register a WindowContent instance for central drawing.
std::vector< std::string > GetVisibleWindowIds(size_t session_id) const
Get list of currently visible panel IDs for a session.
GraphicsSheets & gfx_sheets()
const std::vector< uint8_t > & vector() const
void SetPalette(const SnesPalette &palette)
Set the palette for the bitmap using SNES palette format.
uint64_t ColorRevision(const zelda3::GameData *game_data) const
static PaletteManager & Get()
Get the singleton instance.
RAII timer for automatic timing management.
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
void AddColor(const SnesColor &color)
void DrawBitmap(Bitmap &bitmap, int border_offset, float scale)
bool DrawTileSelector(int size, int size_y=0)
void DrawRect(int x, int y, int w, int h, ImVec4 color)
CanvasConfig & GetConfig()
void DrawBackground(ImVec2 canvas_size=ImVec2(0, 0), std::optional< ImVec2 > viewport_size=std::nullopt)
void DrawGrid(float grid_step=64.0f, int tile_id_offset=8)
static float GetHexInputWidth()
static std::string ShowSaveFileDialog(const std::string &default_name="", const std::string &default_extension="")
ShowSaveFileDialog opens a save file dialog and returns the selected filepath. Uses global feature fl...
static std::string ShowOpenFileDialog()
ShowOpenFileDialog opens a file dialog and returns the selected filepath. Uses global feature flag to...
uint64_t Revision(uint16_t sheet) const
absl::Span< const uint8_t > Sheet(uint16_t sheet) const
The sheet's 4096 pixels, or an empty span when it is not held.
static const SpriteOamLayout * GetLayout(uint8_t sprite_id)
Get the OAM layout for a sprite ID.
static const SpriteOamLayout * GetPreviewOverride(uint8_t sprite_id, std::string_view hack_name)
#define ICON_MD_MEETING_ROOM
#define ICON_MD_FOLDER_OPEN
#define ICON_MD_PLAY_ARROW
#define ICON_MD_SKIP_NEXT
#define ICON_MD_SKIP_PREVIOUS
#define HOVER_HINT(string)
zelda3::GfxSheetInventoryOptions BuildGfxSheetInventoryOptions(const project::YazeProject *project)
int ParseIntOrDefault(const std::string &text, int fallback=0)
void EnsureSpritePreviewBitmapReady(gfx::Bitmap &bmp, int width, int height, int depth, const std::vector< uint8_t > &gfx_buffer)
void PublishSpritePreviewPixels(gfx::Bitmap &bmp)
std::vector< int > FrameTiles8x8(const zsprite::Frame &frame)
gfx::PaletteGroup SpritePalettesFromCgram(const std::array< SDL_Color, 256 > &cgram)
gfx::PaletteGroup DefaultSpritePreviewPalettes(const gfx::PaletteGroup &global, const gfx::PaletteGroup &aux1, const gfx::PaletteGroup &aux2, const gfx::PaletteGroup &aux3)
int OffsetX(const zsprite::OamTile &tile)
bool Advance(const zsprite::AnimationGroup &animation, size_t frame_count, float delta, int &frame, float &remainder)
absl::StatusOr< gfx::PaletteGroup > BindPaletteRows(const project::SpriteAssetBinding &binding, const gfx::PaletteGroup &defaults, const gfx::PaletteGroup &global, const gfx::PaletteGroup &aux1, const gfx::PaletteGroup &aux2, const gfx::PaletteGroup &aux3)
int OffsetY(const zsprite::OamTile &tile)
constexpr size_t kMaxFrames
bool AppendFrame(zsprite::ZSprite &sprite, int copy_from=-1)
zsprite::Frame CopyVanillaLayout(const zelda3::SpriteOamLayout &layout)
bool DeleteFrame(zsprite::ZSprite &sprite, int index)
absl::StatusOr< GraphicsEditContext > Make(const zsprite::Frame &frame, int frame_index, int oam_index, const std::array< uint8_t, 8 > &slots, const gfx::PaletteGroup &palettes, const zelda3::GameData *owner, const std::string &source_label, const std::string &sheet_source)
absl::StatusOr< usage_preview::PreviewImage > RenderCapturedFrame(const zsprite::Frame &frame, const std::array< uint8_t, 8 > &slots, const gfx::PaletteGroup &palettes, const zelda3::GameData *owner)
constexpr ImGuiTabItemFlags kSpriteTabFlags
constexpr ImGuiTabBarFlags kSpriteTabBarFlags
void AutoRegisterLastItem(const std::string &widget_type, const std::string &explicit_label, const std::string &description)
Automatically register the last ImGui item.
bool BeginThemedTabBar(const char *id, ImGuiTabBarFlags flags)
A stylized tab bar with "Mission Control" branding.
ImGuiID GetID(const std::string &id)
bool InputHexByte(const char *label, uint8_t *data, float input_width, bool no_step)
std::string HexByte(uint8_t byte, HexStringParams params)
constexpr int kNumDungeonRooms
GfxGroupUsage FindSpritesetUsage(int spriteset, const std::map< int, OverworldAreaGfxInfo > &areas, const std::map< int, RoomGfxInfo > &rooms)
Room LoadRoomHeaderFromRom(Rom *rom, int room_id)
std::map< int, OverworldAreaGfxInfo > CollectOverworldAreaGfx(Rom &rom, GameData *game_data)
constexpr uint16_t kDungeonSpritesetBase
std::map< int, RoomGfxInfo > CollectRoomGfx(const Rom &rom)
const std::string kSpriteDefaultNames[256]
constexpr uint32_t kNumSpritesets
constexpr bool IsUnderworldSpriteset(int spriteset)
Dungeon rooms use spritesets 0x40-0x8F (room header value + 0x40).
std::array< uint8_t, 8 > SpriteSheetSlots(const std::array< uint8_t, 4 > &spriteset_values, bool underworld)
The eight sheets for a spriteset's four values.
std::array< SDL_Color, 256 > BuildDungeonSpriteRenderPalette(const Room &room, const GameData *game_data)
std::vector< SpriteTileIssue > CheckSpriteTiles(const Rom &rom, const std::vector< int > &tiles, const std::array< uint8_t, 8 > &slots, const std::set< uint16_t > &reserved_sheets)
#define RETURN_IF_ERROR(expr)
WorkspaceWindowManager * window_manager
Snapshot of a custom ZSprite's editable state for undo/redo.
project::SpriteAssetBinding binding
zsprite::ZSprite sprite_data
int current_animation_index
std::vector< Frame > Frames
void Reset()
Reset all sprite data to defaults.
absl::Status Load(const std::string &filename)
Load a ZSM file from disk.
std::vector< AnimationGroup > animations
PaletteGroup sprites_aux1
PaletteGroup sprites_aux2
PaletteGroup sprites_aux3
PaletteGroup global_sprites
auto palette(int i) const
void SelectableLabelWithNameEdit(bool selected, const std::string &type, const std::string &key, const std::string &defaultValue)
std::array< uint8_t, 8 > sheets
std::vector< SpriteAssetBinding > sprite_assets
std::string GetAbsolutePath(const std::string &relative_path) const
std::array< std::array< uint8_t, 4 >, kNumSpritesets > spriteset_ids
gfx::PaletteGroupMap palette_groups
GraphicsSheetStore sheet_store
Complete OAM layout for a vanilla sprite.