6#include "absl/strings/str_format.h"
22using ImGui::BeginTable;
25using ImGui::Selectable;
26using ImGui::Separator;
27using ImGui::TableHeadersRow;
28using ImGui::TableNextColumn;
29using ImGui::TableNextRow;
30using ImGui::TableSetupColumn;
41 std::make_unique<VanillaSpriteEditorPanel>([
this]() {
43 DrawVanillaSpriteEditor();
45 ImGui::TextDisabled(
"Load a ROM to view vanilla sprites");
49 panel_manager->RegisterEditorPanel(std::make_unique<CustomSpriteEditorPanel>(
55 return absl::OkStatus();
64 float current_time = ImGui::GetTime();
80 if (ImGui::IsKeyPressed(ImGuiKey_Space,
false) &&
81 !ImGui::GetIO().WantTextInput) {
86 if (ImGui::IsKeyPressed(ImGuiKey_LeftBracket,
false)) {
92 if (ImGui::IsKeyPressed(ImGuiKey_RightBracket,
false)) {
98 if (ImGui::GetIO().KeyCtrl && ImGui::IsKeyPressed(ImGuiKey_UpArrow,
false)) {
104 if (ImGui::GetIO().KeyCtrl &&
105 ImGui::IsKeyPressed(ImGuiKey_DownArrow,
false)) {
120 return absl::OkStatus();
132 if (ImGui::BeginTable(
"##SpriteCanvasTable", 3, ImGuiTableFlags_Resizable,
134 TableSetupColumn(
"Sprites List", ImGuiTableColumnFlags_WidthFixed, 256);
135 TableSetupColumn(
"Canvas", ImGuiTableColumnFlags_WidthStretch,
136 ImGui::GetContentRegionAvail().x);
137 TableSetupColumn(
"Tile Selector", ImGuiTableColumnFlags_WidthFixed, 256);
145 static int next_tab_id = 0;
164 if (ImGui::BeginTabItem(
166 ImGuiTabItemFlags_None)) {
189 static bool flip_x =
false;
190 static bool flip_y =
false;
191 if (ImGui::BeginChild(
gui::GetID(
"##SpriteCanvas"),
192 ImGui::GetContentRegionAvail(),
true)) {
211 ImGui::SetCursorPos(ImVec2(10, 10));
212 Text(
"Sprite: %s (0x%02X)", layout->name, layout->sprite_id);
213 Text(
"Tiles: %zu", layout->tiles.size());
220 if (ImGui::BeginTable(
"##OAMTable", 7, ImGuiTableFlags_Resizable,
222 TableSetupColumn(
"X", ImGuiTableColumnFlags_WidthStretch);
223 TableSetupColumn(
"Y", ImGuiTableColumnFlags_WidthStretch);
224 TableSetupColumn(
"Tile", ImGuiTableColumnFlags_WidthStretch);
225 TableSetupColumn(
"Palette", ImGuiTableColumnFlags_WidthStretch);
226 TableSetupColumn(
"Priority", ImGuiTableColumnFlags_WidthStretch);
227 TableSetupColumn(
"Flip X", ImGuiTableColumnFlags_WidthStretch);
228 TableSetupColumn(
"Flip Y", ImGuiTableColumnFlags_WidthStretch);
248 if (ImGui::Checkbox(
"##XFlip", &flip_x)) {
253 if (ImGui::Checkbox(
"##YFlip", &flip_y)) {
266 if (ImGui::BeginChild(
gui::GetID(
"sheet_label"),
267 ImVec2(ImGui::GetContentRegionAvail().x, 0),
true,
268 ImGuiWindowFlags_NoDecoration)) {
270 static uint8_t prev_sheets[8] = {0};
271 bool sheets_changed =
false;
273 for (
int i = 0; i < 8; i++) {
274 std::string sheet_label = absl::StrFormat(
"Sheet %d", i);
276 sheets_changed =
true;
283 if (sheets_changed || std::memcmp(prev_sheets,
current_sheets_, 8) != 0) {
292 for (
int i = 0; i < 8; i++) {
304 if (ImGui::BeginChild(
gui::GetID(
"##SpritesList"),
305 ImVec2(ImGui::GetContentRegionAvail().x, 0),
true,
306 ImGuiWindowFlags_NoDecoration)) {
312 if (ImGui::IsItemClicked()) {
328 if (ImGui::Button(
"Add Frame")) {
331 if (ImGui::Button(
"Remove Frame")) {
341 if (BeginTable(
"##CustomSpritesTable", 3,
342 ImGuiTableFlags_Resizable | ImGuiTableFlags_Borders,
344 TableSetupColumn(
"Sprite Data", ImGuiTableColumnFlags_WidthFixed, 300);
345 TableSetupColumn(
"Canvas", ImGuiTableColumnFlags_WidthStretch);
346 TableSetupColumn(
"Tilesheets", ImGuiTableColumnFlags_WidthFixed, 280);
372 if (!file_path.empty()) {
394 Text(
"Loaded Sprites:");
395 if (ImGui::BeginChild(
"SpriteList", ImVec2(0, 100),
true)) {
413 if (ImGui::BeginTabBar(
"SpriteDataTabs")) {
414 if (ImGui::BeginTabItem(
"Properties")) {
418 if (ImGui::BeginTabItem(
"Animations")) {
422 if (ImGui::BeginTabItem(
"Routines")) {
429 Text(
"No sprite selected");
436 new_sprite.
sprName =
"New Sprite";
442 new_sprite.
animations.emplace_back(0, 0, 1,
"Idle");
495 static char name_buf[256];
496 strncpy(name_buf, sprite.sprName.c_str(),
sizeof(name_buf) - 1);
497 if (ImGui::InputText(
"Name", name_buf,
sizeof(name_buf))) {
498 sprite.sprName = name_buf;
499 sprite.property_sprname.Text = name_buf;
503 static char id_buf[32];
504 strncpy(id_buf, sprite.property_sprid.Text.c_str(),
sizeof(id_buf) - 1);
505 if (ImGui::InputText(
"Sprite ID", id_buf,
sizeof(id_buf))) {
506 sprite.property_sprid.Text = id_buf;
523 int prize = sprite.property_prize.Text.empty()
525 : std::stoi(sprite.property_prize.Text);
526 if (ImGui::InputInt(
"Prize", &prize)) {
527 sprite.property_prize.Text = std::to_string(std::clamp(prize, 0, 255));
531 int palette = sprite.property_palette.Text.empty()
533 : std::stoi(sprite.property_palette.Text);
534 if (ImGui::InputInt(
"Palette", &palette)) {
535 sprite.property_palette.Text = std::to_string(std::clamp(palette, 0, 7));
539 int oamnbr = sprite.property_oamnbr.Text.empty()
541 : std::stoi(sprite.property_oamnbr.Text);
542 if (ImGui::InputInt(
"OAM Count", &oamnbr)) {
543 sprite.property_oamnbr.Text = std::to_string(std::clamp(oamnbr, 0, 255));
547 int hitbox = sprite.property_hitbox.Text.empty()
549 : std::stoi(sprite.property_hitbox.Text);
550 if (ImGui::InputInt(
"Hitbox", &hitbox)) {
551 sprite.property_hitbox.Text = std::to_string(std::clamp(hitbox, 0, 255));
555 int health = sprite.property_health.Text.empty()
557 : std::stoi(sprite.property_health.Text);
558 if (ImGui::InputInt(
"Health", &health)) {
559 sprite.property_health.Text = std::to_string(std::clamp(health, 0, 255));
563 int damage = sprite.property_damage.Text.empty()
565 : std::stoi(sprite.property_damage.Text);
566 if (ImGui::InputInt(
"Damage", &damage)) {
567 sprite.property_damage.Text = std::to_string(std::clamp(damage, 0, 255));
575 Text(
"Behavior Flags");
578 if (ImGui::BeginTable(
"BoolProps", 2, ImGuiTableFlags_None)) {
580 ImGui::TableNextColumn();
581 if (ImGui::Checkbox(
"Blockable", &sprite.property_blockable.IsChecked))
583 if (ImGui::Checkbox(
"Can Fall", &sprite.property_canfall.IsChecked))
585 if (ImGui::Checkbox(
"Collision Layer",
586 &sprite.property_collisionlayer.IsChecked))
588 if (ImGui::Checkbox(
"Custom Death", &sprite.property_customdeath.IsChecked))
590 if (ImGui::Checkbox(
"Damage Sound", &sprite.property_damagesound.IsChecked))
592 if (ImGui::Checkbox(
"Deflect Arrows",
593 &sprite.property_deflectarrows.IsChecked))
595 if (ImGui::Checkbox(
"Deflect Projectiles",
596 &sprite.property_deflectprojectiles.IsChecked))
598 if (ImGui::Checkbox(
"Fast", &sprite.property_fast.IsChecked))
600 if (ImGui::Checkbox(
"Harmless", &sprite.property_harmless.IsChecked))
602 if (ImGui::Checkbox(
"Impervious", &sprite.property_impervious.IsChecked))
606 ImGui::TableNextColumn();
607 if (ImGui::Checkbox(
"Impervious Arrow",
608 &sprite.property_imperviousarrow.IsChecked))
610 if (ImGui::Checkbox(
"Impervious Melee",
611 &sprite.property_imperviousmelee.IsChecked))
613 if (ImGui::Checkbox(
"Interaction", &sprite.property_interaction.IsChecked))
615 if (ImGui::Checkbox(
"Is Boss", &sprite.property_isboss.IsChecked))
617 if (ImGui::Checkbox(
"Persist", &sprite.property_persist.IsChecked))
619 if (ImGui::Checkbox(
"Shadow", &sprite.property_shadow.IsChecked))
621 if (ImGui::Checkbox(
"Small Shadow", &sprite.property_smallshadow.IsChecked))
623 if (ImGui::Checkbox(
"Stasis", &sprite.property_statis.IsChecked))
625 if (ImGui::Checkbox(
"Statue", &sprite.property_statue.IsChecked))
627 if (ImGui::Checkbox(
"Water Sprite", &sprite.property_watersprite.IsChecked))
665 Text(
"Frame: %d / %d",
current_frame_, (
int)sprite.editor.Frames.size() - 1);
672 int frame_count =
static_cast<int>(sprite.editor.Frames.size());
673 sprite.animations.emplace_back(0, frame_count > 0 ? frame_count - 1 : 0, 1,
678 if (ImGui::BeginChild(
"AnimList", ImVec2(0, 120),
true)) {
679 for (
size_t i = 0; i < sprite.animations.size(); i++) {
680 auto& anim = sprite.animations[i];
681 std::string label = anim.frame_name.empty() ?
"Unnamed" : anim.frame_name;
697 Text(
"Animation Properties");
699 static char anim_name[128];
700 strncpy(anim_name, anim.frame_name.c_str(),
sizeof(anim_name) - 1);
701 if (ImGui::InputText(
"Name##Anim", anim_name,
sizeof(anim_name))) {
702 anim.frame_name = anim_name;
706 int start = anim.frame_start;
707 int end = anim.frame_end;
708 int speed = anim.frame_speed;
710 if (ImGui::SliderInt(
"Start Frame", &start, 0,
711 std::max(0, (
int)sprite.editor.Frames.size() - 1))) {
712 anim.frame_start =
static_cast<uint8_t
>(start);
715 if (ImGui::SliderInt(
"End Frame", &end, 0,
716 std::max(0, (
int)sprite.editor.Frames.size() - 1))) {
717 anim.frame_end =
static_cast<uint8_t
>(end);
720 if (ImGui::SliderInt(
"Speed", &speed, 1, 16)) {
721 anim.frame_speed =
static_cast<uint8_t
>(speed);
725 if (ImGui::Button(
"Delete Animation") && sprite.animations.size() > 1) {
726 sprite.animations.erase(sprite.animations.begin() +
743 sprite.editor.Frames.emplace_back();
749 sprite.editor.Frames.erase(sprite.editor.Frames.begin() +
current_frame_);
757 if (ImGui::BeginChild(
"FrameList", ImVec2(0, 80),
true,
758 ImGuiWindowFlags_HorizontalScrollbar)) {
759 for (
size_t i = 0; i < sprite.editor.Frames.size(); i++) {
760 ImGui::PushID(
static_cast<int>(i));
761 std::string label = absl::StrFormat(
"F%d", i);
763 ImGuiSelectableFlags_None, ImVec2(40, 40))) {
782 frame.Tiles.emplace_back();
787 if (ImGui::BeginChild(
"TileList", ImVec2(0, 100),
true)) {
788 for (
size_t i = 0; i < frame.Tiles.size(); i++) {
789 auto& tile = frame.Tiles[i];
790 std::string label = absl::StrFormat(
"Tile %d (ID: %d)", i, tile.id);
803 int tile_id = tile.id;
804 if (ImGui::InputInt(
"Tile ID", &tile_id)) {
805 tile.id =
static_cast<uint16_t
>(std::clamp(tile_id, 0, 511));
810 int x = tile.x, y = tile.y;
811 if (ImGui::InputInt(
"X", &x)) {
812 tile.x =
static_cast<uint8_t
>(std::clamp(x, 0, 251));
816 if (ImGui::InputInt(
"Y", &y)) {
817 tile.y =
static_cast<uint8_t
>(std::clamp(y, 0, 219));
822 int pal = tile.palette;
823 if (ImGui::SliderInt(
"Palette##Tile", &pal, 0, 7)) {
824 tile.palette =
static_cast<uint8_t
>(pal);
829 if (ImGui::Checkbox(
"16x16", &tile.size)) {
834 if (ImGui::Checkbox(
"Flip X", &tile.mirror_x)) {
839 if (ImGui::Checkbox(
"Flip Y", &tile.mirror_y)) {
844 if (ImGui::Button(
"Delete Tile")) {
869 float frame_duration = anim.frame_speed / 60.0f;
889 sprite.userRoutines.emplace_back(
"New Routine",
"; ASM code here\n");
894 if (ImGui::BeginChild(
"RoutineList", ImVec2(0, 100),
true)) {
895 for (
size_t i = 0; i < sprite.userRoutines.size(); i++) {
896 auto& routine = sprite.userRoutines[i];
911 static char routine_name[128];
912 strncpy(routine_name, routine.name.c_str(),
sizeof(routine_name) - 1);
913 if (ImGui::InputText(
"Routine Name", routine_name,
sizeof(routine_name))) {
914 routine.name = routine_name;
921 static char code_buffer[16384];
922 strncpy(code_buffer, routine.code.c_str(),
sizeof(code_buffer) - 1);
923 code_buffer[
sizeof(code_buffer) - 1] =
'\0';
924 if (ImGui::InputTextMultiline(
"##RoutineCode", code_buffer,
925 sizeof(code_buffer), ImVec2(-1, 200))) {
926 routine.code = code_buffer;
930 if (ImGui::Button(
"Delete Routine")) {
931 sprite.userRoutines.erase(sprite.userRoutines.begin() +
952 constexpr int kSheetWidth = 128;
953 constexpr int kSheetHeight = 32;
954 constexpr int kRowStride = 128;
956 for (
int sheet_idx = 0; sheet_idx < 8; sheet_idx++) {
963 if (!sheet.is_active() || sheet.size() == 0) {
972 int dest_offset = sheet_idx * (kSheetHeight * kRowStride);
974 const uint8_t* src_data = sheet.data();
976 std::min(sheet.size(),
static_cast<size_t>(kSheetWidth * kSheetHeight));
1009 for (
size_t i = 0; i < global.size() && i < 8; i++) {
1026 }
else if (aux3.size() > 0) {
1040 bool changed =
false;
1041 for (
int i = 0; i < 4; i++) {
1079 for (
const auto& entry : layout.
tiles) {
1081 tile.
x =
static_cast<uint8_t
>(entry.x_offset + 128);
1082 tile.
y =
static_cast<uint8_t
>(entry.y_offset + 128);
1083 tile.
id = entry.tile_id;
1085 tile.
size = entry.size_16x16;
1101 for (
size_t col_idx = 0; col_idx < 16 && col_idx < sub_pal.size();
1103 combined_palette.
AddColor(sub_pal[col_idx]);
1106 while (combined_palette.
size() < (pal_idx + 1) * 16) {
1127 if (frame_index < 0 || frame_index >= (
int)sprite.editor.Frames.size()) {
1131 auto& frame = sprite.editor.Frames[frame_index];
1159 for (
size_t col_idx = 0; col_idx < 16 && col_idx < sub_pal.size();
1161 combined_palette.
AddColor(sub_pal[col_idx]);
1164 while (combined_palette.
size() < (pal_idx + 1) * 16) {
1182 for (
size_t i = 0; i < frame.Tiles.size(); i++) {
1183 const auto& tile = frame.Tiles[i];
1184 int tile_size = tile.size ? 16 : 8;
1187 int8_t signed_x =
static_cast<int8_t
>(tile.x);
1188 int8_t signed_y =
static_cast<int8_t
>(tile.y);
1190 int canvas_x = 128 + signed_x;
1191 int canvas_y = 128 + signed_y;
1195 ? ImVec4(0.0f, 1.0f, 0.0f, 0.8f)
1196 : ImVec4(1.0f, 1.0f, 0.0f, 0.3f);
1204 if (ImGui::BeginChild(
gui::GetID(
"##ZSpriteCanvas"),
1205 ImGui::GetContentRegionAvail(),
true)) {
1221 ImGui::SetCursorPos(ImVec2(10, 10));
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 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_
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()
absl::Status Save() override
void DrawCustomSpritesMetadata()
void SaveZsmFile(const std::string &path)
gui::Canvas sprite_canvas_
void DrawStatProperties()
std::string current_zsm_path_
absl::Status Load() override
void LoadSpriteGraphicsBuffer()
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
constexpr ImGuiTabBarFlags kSpriteTabBarFlags
bool InputHexWord(const char *label, uint16_t *data, float input_width, bool no_step)
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