8#include "absl/strings/str_format.h"
26using ImGui::BeginTable;
29using ImGui::Selectable;
30using ImGui::Separator;
31using ImGui::TableHeadersRow;
32using ImGui::TableNextColumn;
33using ImGui::TableNextRow;
34using ImGui::TableSetupColumn;
39void CopyStringToBuffer(
const std::string& src,
char (&dest)[N]) {
40 std::strncpy(dest, src.c_str(), N - 1);
50 long value = std::strtol(text.c_str(), &end, 0);
51 if (end == text.c_str() || errno == ERANGE) {
54 return static_cast<int>(value);
66 std::make_unique<VanillaSpriteEditorPanel>([
this]() {
68 DrawVanillaSpriteEditor();
70 ImGui::TextDisabled(
"Load a ROM to view vanilla sprites");
74 panel_manager->RegisterEditorPanel(std::make_unique<CustomSpriteEditorPanel>(
80 return absl::OkStatus();
89 float current_time = ImGui::GetTime();
105 if (ImGui::IsKeyPressed(ImGuiKey_Space,
false) &&
106 !ImGui::GetIO().WantTextInput) {
111 if (ImGui::IsKeyPressed(ImGuiKey_LeftBracket,
false)) {
117 if (ImGui::IsKeyPressed(ImGuiKey_RightBracket,
false)) {
123 if (ImGui::GetIO().KeyCtrl && ImGui::IsKeyPressed(ImGuiKey_UpArrow,
false)) {
129 if (ImGui::GetIO().KeyCtrl &&
130 ImGui::IsKeyPressed(ImGuiKey_DownArrow,
false)) {
140 if (zsm_path.empty()) {
146 return absl::OkStatus();
158 if (ImGui::BeginTable(
"##SpriteCanvasTable", 3, ImGuiTableFlags_Resizable,
160 TableSetupColumn(
"Sprites List", ImGuiTableColumnFlags_WidthFixed, 256);
161 TableSetupColumn(
"Canvas", ImGuiTableColumnFlags_WidthStretch,
162 ImGui::GetContentRegionAvail().x);
163 TableSetupColumn(
"Tile Selector", ImGuiTableColumnFlags_WidthFixed, 256);
171 static int next_tab_id = 0;
190 if (ImGui::BeginTabItem(
192 ImGuiTabItemFlags_None)) {
215 static bool flip_x =
false;
216 static bool flip_y =
false;
217 if (ImGui::BeginChild(
gui::GetID(
"##SpriteCanvas"),
218 ImGui::GetContentRegionAvail(),
true)) {
237 ImGui::SetCursorPos(ImVec2(10, 10));
238 Text(
"Sprite: %s (0x%02X)", layout->name, layout->sprite_id);
239 Text(
"Tiles: %zu", layout->tiles.size());
246 if (ImGui::BeginTable(
"##OAMTable", 7, ImGuiTableFlags_Resizable,
248 TableSetupColumn(
"X", ImGuiTableColumnFlags_WidthStretch);
249 TableSetupColumn(
"Y", ImGuiTableColumnFlags_WidthStretch);
250 TableSetupColumn(
"Tile", ImGuiTableColumnFlags_WidthStretch);
251 TableSetupColumn(
"Palette", ImGuiTableColumnFlags_WidthStretch);
252 TableSetupColumn(
"Priority", ImGuiTableColumnFlags_WidthStretch);
253 TableSetupColumn(
"Flip X", ImGuiTableColumnFlags_WidthStretch);
254 TableSetupColumn(
"Flip Y", ImGuiTableColumnFlags_WidthStretch);
274 if (ImGui::Checkbox(
"##XFlip", &flip_x)) {
279 if (ImGui::Checkbox(
"##YFlip", &flip_y)) {
292 if (ImGui::BeginChild(
gui::GetID(
"sheet_label"),
293 ImVec2(ImGui::GetContentRegionAvail().x, 0),
true,
294 ImGuiWindowFlags_NoDecoration)) {
296 static uint8_t prev_sheets[8] = {0};
297 bool sheets_changed =
false;
299 for (
int i = 0; i < 8; i++) {
300 std::string sheet_label = absl::StrFormat(
"Sheet %d", i);
302 sheets_changed =
true;
309 if (sheets_changed || std::memcmp(prev_sheets,
current_sheets_, 8) != 0) {
318 for (
int i = 0; i < 8; i++) {
330 if (ImGui::BeginChild(
gui::GetID(
"##SpritesList"),
331 ImVec2(ImGui::GetContentRegionAvail().x, 0),
true,
332 ImGuiWindowFlags_NoDecoration)) {
338 if (ImGui::IsItemClicked()) {
354 if (ImGui::Button(
"Add Frame")) {
357 if (ImGui::Button(
"Remove Frame")) {
367 if (BeginTable(
"##CustomSpritesTable", 3,
368 ImGuiTableFlags_Resizable | ImGuiTableFlags_Borders,
370 TableSetupColumn(
"Sprite Data", ImGuiTableColumnFlags_WidthFixed, 300);
371 TableSetupColumn(
"Canvas", ImGuiTableColumnFlags_WidthStretch);
372 TableSetupColumn(
"Tilesheets", ImGuiTableColumnFlags_WidthFixed, 280);
398 if (!file_path.empty()) {
406 if (zsm_path.empty()) {
421 Text(
"Loaded Sprites:");
422 if (ImGui::BeginChild(
"SpriteList", ImVec2(0, 100),
true)) {
448 if (ImGui::BeginTabItem(
"Properties")) {
452 if (ImGui::BeginTabItem(
"Animations")) {
456 if (ImGui::BeginTabItem(
"Routines")) {
463 Text(
"No sprite selected");
470 new_sprite.
sprName =
"New Sprite";
476 new_sprite.
animations.emplace_back(0, 0, 1,
"Idle");
542 static char name_buf[256];
543 CopyStringToBuffer(sprite.sprName, name_buf);
544 if (ImGui::InputText(
"Name", name_buf,
sizeof(name_buf))) {
545 sprite.sprName = name_buf;
546 sprite.property_sprname.Text = name_buf;
550 static char id_buf[32];
551 CopyStringToBuffer(sprite.property_sprid.Text, id_buf);
552 if (ImGui::InputText(
"Sprite ID", id_buf,
sizeof(id_buf))) {
553 sprite.property_sprid.Text = id_buf;
570 int prize = ParseIntOrDefault(sprite.property_prize.Text);
571 if (ImGui::InputInt(
"Prize", &prize)) {
572 sprite.property_prize.Text = std::to_string(std::clamp(prize, 0, 255));
576 int palette = ParseIntOrDefault(sprite.property_palette.Text);
577 if (ImGui::InputInt(
"Palette", &palette)) {
578 sprite.property_palette.Text = std::to_string(std::clamp(palette, 0, 7));
582 int oamnbr = ParseIntOrDefault(sprite.property_oamnbr.Text);
583 if (ImGui::InputInt(
"OAM Count", &oamnbr)) {
584 sprite.property_oamnbr.Text = std::to_string(std::clamp(oamnbr, 0, 255));
588 int hitbox = ParseIntOrDefault(sprite.property_hitbox.Text);
589 if (ImGui::InputInt(
"Hitbox", &hitbox)) {
590 sprite.property_hitbox.Text = std::to_string(std::clamp(hitbox, 0, 255));
594 int health = ParseIntOrDefault(sprite.property_health.Text);
595 if (ImGui::InputInt(
"Health", &health)) {
596 sprite.property_health.Text = std::to_string(std::clamp(health, 0, 255));
600 int damage = ParseIntOrDefault(sprite.property_damage.Text);
601 if (ImGui::InputInt(
"Damage", &damage)) {
602 sprite.property_damage.Text = std::to_string(std::clamp(damage, 0, 255));
610 Text(
"Behavior Flags");
613 if (ImGui::BeginTable(
"BoolProps", 2, ImGuiTableFlags_None)) {
615 ImGui::TableNextColumn();
616 if (ImGui::Checkbox(
"Blockable", &sprite.property_blockable.IsChecked))
618 if (ImGui::Checkbox(
"Can Fall", &sprite.property_canfall.IsChecked))
620 if (ImGui::Checkbox(
"Collision Layer",
621 &sprite.property_collisionlayer.IsChecked))
623 if (ImGui::Checkbox(
"Custom Death", &sprite.property_customdeath.IsChecked))
625 if (ImGui::Checkbox(
"Damage Sound", &sprite.property_damagesound.IsChecked))
627 if (ImGui::Checkbox(
"Deflect Arrows",
628 &sprite.property_deflectarrows.IsChecked))
630 if (ImGui::Checkbox(
"Deflect Projectiles",
631 &sprite.property_deflectprojectiles.IsChecked))
633 if (ImGui::Checkbox(
"Fast", &sprite.property_fast.IsChecked))
635 if (ImGui::Checkbox(
"Harmless", &sprite.property_harmless.IsChecked))
637 if (ImGui::Checkbox(
"Impervious", &sprite.property_impervious.IsChecked))
641 ImGui::TableNextColumn();
642 if (ImGui::Checkbox(
"Impervious Arrow",
643 &sprite.property_imperviousarrow.IsChecked))
645 if (ImGui::Checkbox(
"Impervious Melee",
646 &sprite.property_imperviousmelee.IsChecked))
648 if (ImGui::Checkbox(
"Interaction", &sprite.property_interaction.IsChecked))
650 if (ImGui::Checkbox(
"Is Boss", &sprite.property_isboss.IsChecked))
652 if (ImGui::Checkbox(
"Persist", &sprite.property_persist.IsChecked))
654 if (ImGui::Checkbox(
"Shadow", &sprite.property_shadow.IsChecked))
656 if (ImGui::Checkbox(
"Small Shadow", &sprite.property_smallshadow.IsChecked))
658 if (ImGui::Checkbox(
"Stasis", &sprite.property_statis.IsChecked))
660 if (ImGui::Checkbox(
"Statue", &sprite.property_statue.IsChecked))
662 if (ImGui::Checkbox(
"Water Sprite", &sprite.property_watersprite.IsChecked))
702 Text(
"Frame: %d / %d",
current_frame_, (
int)sprite.editor.Frames.size() - 1);
709 int frame_count =
static_cast<int>(sprite.editor.Frames.size());
710 sprite.animations.emplace_back(0, frame_count > 0 ? frame_count - 1 : 0, 1,
716 if (ImGui::BeginChild(
"AnimList", ImVec2(0, 120),
true)) {
717 for (
size_t i = 0; i < sprite.animations.size(); i++) {
718 auto& anim = sprite.animations[i];
719 std::string label = anim.frame_name.empty() ?
"Unnamed" : anim.frame_name;
735 Text(
"Animation Properties");
737 static char anim_name[128];
738 CopyStringToBuffer(anim.frame_name, anim_name);
739 if (ImGui::InputText(
"Name##Anim", anim_name,
sizeof(anim_name))) {
740 anim.frame_name = anim_name;
744 int start = anim.frame_start;
745 int end = anim.frame_end;
746 int speed = anim.frame_speed;
748 if (ImGui::SliderInt(
"Start Frame", &start, 0,
749 std::max(0, (
int)sprite.editor.Frames.size() - 1))) {
750 anim.frame_start =
static_cast<uint8_t
>(start);
753 if (ImGui::SliderInt(
"End Frame", &end, 0,
754 std::max(0, (
int)sprite.editor.Frames.size() - 1))) {
755 anim.frame_end =
static_cast<uint8_t
>(end);
758 if (ImGui::SliderInt(
"Speed", &speed, 1, 16)) {
759 anim.frame_speed =
static_cast<uint8_t
>(speed);
763 if (ImGui::Button(
"Delete Animation") && sprite.animations.size() > 1) {
764 sprite.animations.erase(sprite.animations.begin() +
782 sprite.editor.Frames.emplace_back();
789 sprite.editor.Frames.erase(sprite.editor.Frames.begin() +
current_frame_);
798 if (ImGui::BeginChild(
"FrameList", ImVec2(0, 80),
true,
799 ImGuiWindowFlags_HorizontalScrollbar)) {
800 for (
size_t i = 0; i < sprite.editor.Frames.size(); i++) {
801 ImGui::PushID(
static_cast<int>(i));
802 std::string label = absl::StrFormat(
"F%d", i);
804 ImGuiSelectableFlags_None, ImVec2(40, 40))) {
823 frame.Tiles.emplace_back();
829 if (ImGui::BeginChild(
"TileList", ImVec2(0, 100),
true)) {
830 for (
size_t i = 0; i < frame.Tiles.size(); i++) {
831 auto& tile = frame.Tiles[i];
832 std::string label = absl::StrFormat(
"Tile %d (ID: %d)", i, tile.id);
845 int tile_id = tile.id;
846 if (ImGui::InputInt(
"Tile ID", &tile_id)) {
847 tile.id =
static_cast<uint16_t
>(std::clamp(tile_id, 0, 511));
852 int x = tile.x, y = tile.y;
853 if (ImGui::InputInt(
"X", &x)) {
854 tile.x =
static_cast<uint8_t
>(std::clamp(x, 0, 251));
858 if (ImGui::InputInt(
"Y", &y)) {
859 tile.y =
static_cast<uint8_t
>(std::clamp(y, 0, 219));
864 int pal = tile.palette;
865 if (ImGui::SliderInt(
"Palette##Tile", &pal, 0, 7)) {
866 tile.palette =
static_cast<uint8_t
>(pal);
871 if (ImGui::Checkbox(
"16x16", &tile.size)) {
876 if (ImGui::Checkbox(
"Flip X", &tile.mirror_x)) {
881 if (ImGui::Checkbox(
"Flip Y", &tile.mirror_y)) {
886 if (ImGui::Button(
"Delete Tile")) {
912 float frame_duration = anim.frame_speed / 60.0f;
932 sprite.userRoutines.emplace_back(
"New Routine",
"; ASM code here\n");
938 if (ImGui::BeginChild(
"RoutineList", ImVec2(0, 100),
true)) {
939 for (
size_t i = 0; i < sprite.userRoutines.size(); i++) {
940 auto& routine = sprite.userRoutines[i];
955 static char routine_name[128];
956 CopyStringToBuffer(routine.name, routine_name);
957 if (ImGui::InputText(
"Routine Name", routine_name,
sizeof(routine_name))) {
958 routine.name = routine_name;
965 static char code_buffer[16384];
966 CopyStringToBuffer(routine.code, code_buffer);
967 if (ImGui::InputTextMultiline(
"##RoutineCode", code_buffer,
968 sizeof(code_buffer), ImVec2(-1, 200))) {
969 routine.code = code_buffer;
973 if (ImGui::Button(
"Delete Routine")) {
974 sprite.userRoutines.erase(sprite.userRoutines.begin() +
996 constexpr int kSheetWidth = 128;
997 constexpr int kSheetHeight = 32;
998 constexpr int kRowStride = 128;
1000 for (
int sheet_idx = 0; sheet_idx < 8; sheet_idx++) {
1007 if (!sheet.is_active() || sheet.size() == 0) {
1016 int dest_offset = sheet_idx * (kSheetHeight * kRowStride);
1018 const uint8_t* src_data = sheet.data();
1020 std::min(sheet.size(),
static_cast<size_t>(kSheetWidth * kSheetHeight));
1053 for (
size_t i = 0; i < global.size() && i < 8; i++) {
1070 }
else if (aux3.size() > 0) {
1084 bool changed =
false;
1085 for (
int i = 0; i < 4; i++) {
1123 for (
const auto& entry : layout.
tiles) {
1125 tile.
x =
static_cast<uint8_t
>(entry.x_offset + 128);
1126 tile.
y =
static_cast<uint8_t
>(entry.y_offset + 128);
1127 tile.
id = entry.tile_id;
1129 tile.
size = entry.size_16x16;
1145 for (
size_t col_idx = 0; col_idx < 16 && col_idx < sub_pal.size();
1147 combined_palette.
AddColor(sub_pal[col_idx]);
1150 while (combined_palette.
size() < (pal_idx + 1) * 16) {
1171 if (frame_index < 0 || frame_index >= (
int)sprite.editor.Frames.size()) {
1175 auto& frame = sprite.editor.Frames[frame_index];
1203 for (
size_t col_idx = 0; col_idx < 16 && col_idx < sub_pal.size();
1205 combined_palette.
AddColor(sub_pal[col_idx]);
1208 while (combined_palette.
size() < (pal_idx + 1) * 16) {
1226 for (
size_t i = 0; i < frame.Tiles.size(); i++) {
1227 const auto& tile = frame.Tiles[i];
1228 int tile_size = tile.size ? 16 : 8;
1231 int8_t signed_x =
static_cast<int8_t
>(tile.x);
1232 int8_t signed_y =
static_cast<int8_t
>(tile.y);
1234 int canvas_x = 128 + signed_x;
1235 int canvas_y = 128 + signed_y;
1239 ? ImVec4(0.0f, 1.0f, 0.0f, 0.8f)
1240 : ImVec4(1.0f, 1.0f, 0.0f, 0.3f);
1248 if (ImGui::BeginChild(
gui::GetID(
"##ZSpriteCanvas"),
1249 ImGui::GetContentRegionAvail(),
true)) {
1265 ImGui::SetCursorPos(ImVec2(10, 10));
1282 static const std::string kEmptyPath;
project::ResourceLabelManager * resource_label()
zelda3::GameData * game_data() const
EditorDependencies dependencies_
void RegisterEditorPanel(std::unique_ptr< EditorPanel > panel)
Register an EditorPanel instance for central drawing.
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 DrawOamTile(gfx::Bitmap &bitmap, const zsprite::OamTile &tile, int origin_x, int origin_y)
Draw a single ZSM OAM tile to bitmap.
void SetGraphicsBuffer(const uint8_t *buffer)
Set the graphics buffer for tile lookup.
ImVector< int > active_sprites_
int current_custom_sprite_index_
void UpdateAnimationPlayback(float delta_time)
void DrawAnimationFrames()
void DrawAnimationPanel()
void EnsureCustomSpritePaths()
void RenderZSpriteFrame(int frame_index)
void LoadSheetsForSprite(const std::array< uint8_t, 4 > &sheets)
bool preview_needs_update_
void DrawZSpriteOnCanvas()
bool vanilla_preview_needs_update_
absl::Status Update() override
void HandleEditorShortcuts()
gfx::PaletteGroup sprite_palettes_
void LoadZsmFile(const std::string &path)
void DrawUserRoutinesPanel()
void RenderVanillaSprite(const zelda3::SpriteOamLayout &layout)
void Initialize() override
void DrawVanillaSpriteEditor()
int selected_routine_index_
const std::string & GetCurrentZsmPath() const
uint8_t current_sheets_[8]
SpriteDrawer sprite_drawer_
gui::Canvas graphics_sheet_canvas_
std::vector< zsprite::ZSprite > custom_sprites_
std::vector< uint8_t > sprite_gfx_buffer_
gfx::Bitmap sprite_preview_bitmap_
int current_animation_index_
void DrawBooleanProperties()
void DrawSpritePropertiesPanel()
void SetCurrentZsmPath(const std::string &path)
absl::Status Save() override
void DrawCustomSpritesMetadata()
void SaveZsmFile(const std::string &path)
gui::Canvas sprite_canvas_
void DrawStatProperties()
absl::Status Load() override
void LoadSpriteGraphicsBuffer()
std::vector< std::string > custom_sprite_paths_
void LoadSpritePalettes()
gfx::Bitmap vanilla_preview_bitmap_
std::array< gfx::Bitmap, 223 > & gfx_sheets()
Get reference to all graphics sheets.
void Create(int width, int height, int depth, std::span< uint8_t > data)
Create a bitmap with the given dimensions and data.
void Reformat(int format)
Reformat the bitmap to use a different pixel format.
void SetPalette(const SnesPalette &palette)
Set the palette for the bitmap using SNES palette format.
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)
void DrawBackground(ImVec2 canvas_size=ImVec2(0, 0))
void DrawGrid(float grid_step=64.0f, int tile_id_offset=8)
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...
static const SpriteOamLayout * GetLayout(uint8_t sprite_id)
Get the OAM layout for a sprite ID.
#define ICON_MD_FOLDER_OPEN
#define ICON_MD_PLAY_ARROW
#define ICON_MD_SKIP_NEXT
#define ICON_MD_SKIP_PREVIOUS
#define HOVER_HINT(string)
int ParseIntOrDefault(const std::string &text, int fallback=0)
constexpr ImGuiTabItemFlags kSpriteTabFlags
constexpr ImGuiTabBarFlags kSpriteTabBarFlags
bool InputHexWord(const char *label, uint16_t *data, float input_width, bool no_step)
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)
const std::string kSpriteDefaultNames[256]
PanelManager * panel_manager
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 AddPalette(SnesPalette pal)
void SelectableLabelWithNameEdit(bool selected, const std::string &type, const std::string &key, const std::string &defaultValue)
gfx::PaletteGroupMap palette_groups
Complete OAM layout for a vanilla sprite.
std::vector< SpriteOamEntry > tiles