8#include "absl/strings/str_format.h"
27using ImGui::BeginTable;
30using ImGui::Selectable;
31using ImGui::Separator;
32using ImGui::TableHeadersRow;
33using ImGui::TableNextColumn;
34using ImGui::TableNextRow;
35using ImGui::TableSetupColumn;
40void CopyStringToBuffer(
const std::string& src,
char (&dest)[N]) {
41 std::strncpy(dest, src.c_str(), N - 1);
51 long value = std::strtol(text.c_str(), &end, 0);
52 if (end == text.c_str() || errno == ERANGE) {
55 return static_cast<int>(value);
67 std::make_unique<VanillaSpriteEditorPanel>([
this]() {
69 DrawVanillaSpriteEditor();
71 ImGui::TextDisabled(
"Load a ROM to view vanilla sprites");
75 window_manager->RegisterWindowContent(std::make_unique<CustomSpriteEditorPanel>(
81 return absl::OkStatus();
90 float current_time = ImGui::GetTime();
109 if (ImGui::IsKeyPressed(ImGuiKey_Space,
false) &&
110 !ImGui::GetIO().WantTextInput) {
115 if (ImGui::IsKeyPressed(ImGuiKey_LeftBracket,
false)) {
121 if (ImGui::IsKeyPressed(ImGuiKey_RightBracket,
false)) {
127 if (ImGui::GetIO().KeyCtrl && ImGui::IsKeyPressed(ImGuiKey_UpArrow,
false)) {
133 if (ImGui::GetIO().KeyCtrl &&
134 ImGui::IsKeyPressed(ImGuiKey_DownArrow,
false)) {
144 if (zsm_path.empty()) {
150 return absl::OkStatus();
162 if (ImGui::BeginTable(
"##SpriteCanvasTable", 3, ImGuiTableFlags_Resizable,
164 TableSetupColumn(
"Sprites List", ImGuiTableColumnFlags_WidthFixed, 256);
165 TableSetupColumn(
"Canvas", ImGuiTableColumnFlags_WidthStretch,
166 ImGui::GetContentRegionAvail().x);
167 TableSetupColumn(
"Tile Selector", ImGuiTableColumnFlags_WidthFixed, 256);
175 static int next_tab_id = 0;
194 if (ImGui::BeginTabItem(
196 ImGuiTabItemFlags_None)) {
219 static bool flip_x =
false;
220 static bool flip_y =
false;
221 if (ImGui::BeginChild(
gui::GetID(
"##SpriteCanvas"),
222 ImGui::GetContentRegionAvail(),
true)) {
241 ImGui::SetCursorPos(ImVec2(10, 10));
242 Text(
"Sprite: %s (0x%02X)", layout->name, layout->sprite_id);
243 Text(
"Tiles: %zu", layout->tiles.size());
250 if (ImGui::BeginTable(
"##OAMTable", 7, ImGuiTableFlags_Resizable,
252 TableSetupColumn(
"X", ImGuiTableColumnFlags_WidthStretch);
253 TableSetupColumn(
"Y", ImGuiTableColumnFlags_WidthStretch);
254 TableSetupColumn(
"Tile", ImGuiTableColumnFlags_WidthStretch);
255 TableSetupColumn(
"Palette", ImGuiTableColumnFlags_WidthStretch);
256 TableSetupColumn(
"Priority", ImGuiTableColumnFlags_WidthStretch);
257 TableSetupColumn(
"Flip X", ImGuiTableColumnFlags_WidthStretch);
258 TableSetupColumn(
"Flip Y", ImGuiTableColumnFlags_WidthStretch);
278 if (ImGui::Checkbox(
"##XFlip", &flip_x)) {
283 if (ImGui::Checkbox(
"##YFlip", &flip_y)) {
296 if (ImGui::BeginChild(
gui::GetID(
"sheet_label"),
297 ImVec2(ImGui::GetContentRegionAvail().x, 0),
true,
298 ImGuiWindowFlags_NoDecoration)) {
300 static uint8_t prev_sheets[8] = {0};
301 bool sheets_changed =
false;
303 for (
int i = 0; i < 8; i++) {
304 std::string sheet_label = absl::StrFormat(
"Sheet %d", i);
306 sheets_changed =
true;
313 if (sheets_changed || std::memcmp(prev_sheets,
current_sheets_, 8) != 0) {
322 for (
int i = 0; i < 8; i++) {
334 if (ImGui::BeginChild(
gui::GetID(
"##SpritesList"),
335 ImVec2(ImGui::GetContentRegionAvail().x, 0),
true,
336 ImGuiWindowFlags_NoDecoration)) {
342 if (ImGui::IsItemClicked()) {
358 if (ImGui::Button(
"Add Frame")) {
361 if (ImGui::Button(
"Remove Frame")) {
371 if (BeginTable(
"##CustomSpritesTable", 3,
372 ImGuiTableFlags_Resizable | ImGuiTableFlags_Borders,
374 TableSetupColumn(
"Sprite Data", ImGuiTableColumnFlags_WidthFixed, 300);
375 TableSetupColumn(
"Canvas", ImGuiTableColumnFlags_WidthStretch);
376 TableSetupColumn(
"Tilesheets", ImGuiTableColumnFlags_WidthFixed, 280);
410 if (!file_path.empty()) {
418 if (zsm_path.empty()) {
433 Text(
"Loaded Sprites:");
434 if (ImGui::BeginChild(
"SpriteList", ImVec2(0, 100),
true)) {
460 if (ImGui::BeginTabItem(
"Properties")) {
464 if (ImGui::BeginTabItem(
"Animations")) {
468 if (ImGui::BeginTabItem(
"Routines")) {
475 Text(
"No sprite selected");
482 new_sprite.
sprName =
"New Sprite";
488 new_sprite.
animations.emplace_back(0, 0, 1,
"Idle");
554 static char name_buf[256];
555 CopyStringToBuffer(sprite.sprName, name_buf);
556 if (ImGui::InputText(
"Name", name_buf,
sizeof(name_buf))) {
557 sprite.sprName = name_buf;
558 sprite.property_sprname.Text = name_buf;
562 static char id_buf[32];
563 CopyStringToBuffer(sprite.property_sprid.Text, id_buf);
564 if (ImGui::InputText(
"Sprite ID", id_buf,
sizeof(id_buf))) {
565 sprite.property_sprid.Text = id_buf;
582 int prize = ParseIntOrDefault(sprite.property_prize.Text);
583 if (ImGui::InputInt(
"Prize", &prize)) {
584 sprite.property_prize.Text = std::to_string(std::clamp(prize, 0, 255));
588 int palette = ParseIntOrDefault(sprite.property_palette.Text);
589 if (ImGui::InputInt(
"Palette", &palette)) {
590 sprite.property_palette.Text = std::to_string(std::clamp(palette, 0, 7));
594 int oamnbr = ParseIntOrDefault(sprite.property_oamnbr.Text);
595 if (ImGui::InputInt(
"OAM Count", &oamnbr)) {
596 sprite.property_oamnbr.Text = std::to_string(std::clamp(oamnbr, 0, 255));
600 int hitbox = ParseIntOrDefault(sprite.property_hitbox.Text);
601 if (ImGui::InputInt(
"Hitbox", &hitbox)) {
602 sprite.property_hitbox.Text = std::to_string(std::clamp(hitbox, 0, 255));
606 int health = ParseIntOrDefault(sprite.property_health.Text);
607 if (ImGui::InputInt(
"Health", &health)) {
608 sprite.property_health.Text = std::to_string(std::clamp(health, 0, 255));
612 int damage = ParseIntOrDefault(sprite.property_damage.Text);
613 if (ImGui::InputInt(
"Damage", &damage)) {
614 sprite.property_damage.Text = std::to_string(std::clamp(damage, 0, 255));
622 Text(
"Behavior Flags");
625 if (ImGui::BeginTable(
"BoolProps", 2, ImGuiTableFlags_None)) {
627 ImGui::TableNextColumn();
628 if (ImGui::Checkbox(
"Blockable", &sprite.property_blockable.IsChecked))
630 if (ImGui::Checkbox(
"Can Fall", &sprite.property_canfall.IsChecked))
632 if (ImGui::Checkbox(
"Collision Layer",
633 &sprite.property_collisionlayer.IsChecked))
635 if (ImGui::Checkbox(
"Custom Death", &sprite.property_customdeath.IsChecked))
637 if (ImGui::Checkbox(
"Damage Sound", &sprite.property_damagesound.IsChecked))
639 if (ImGui::Checkbox(
"Deflect Arrows",
640 &sprite.property_deflectarrows.IsChecked))
642 if (ImGui::Checkbox(
"Deflect Projectiles",
643 &sprite.property_deflectprojectiles.IsChecked))
645 if (ImGui::Checkbox(
"Fast", &sprite.property_fast.IsChecked))
647 if (ImGui::Checkbox(
"Harmless", &sprite.property_harmless.IsChecked))
649 if (ImGui::Checkbox(
"Impervious", &sprite.property_impervious.IsChecked))
653 ImGui::TableNextColumn();
654 if (ImGui::Checkbox(
"Impervious Arrow",
655 &sprite.property_imperviousarrow.IsChecked))
657 if (ImGui::Checkbox(
"Impervious Melee",
658 &sprite.property_imperviousmelee.IsChecked))
660 if (ImGui::Checkbox(
"Interaction", &sprite.property_interaction.IsChecked))
662 if (ImGui::Checkbox(
"Is Boss", &sprite.property_isboss.IsChecked))
664 if (ImGui::Checkbox(
"Persist", &sprite.property_persist.IsChecked))
666 if (ImGui::Checkbox(
"Shadow", &sprite.property_shadow.IsChecked))
668 if (ImGui::Checkbox(
"Small Shadow", &sprite.property_smallshadow.IsChecked))
670 if (ImGui::Checkbox(
"Stasis", &sprite.property_statis.IsChecked))
672 if (ImGui::Checkbox(
"Statue", &sprite.property_statue.IsChecked))
674 if (ImGui::Checkbox(
"Water Sprite", &sprite.property_watersprite.IsChecked))
714 Text(
"Frame: %d / %d",
current_frame_, (
int)sprite.editor.Frames.size() - 1);
721 int frame_count =
static_cast<int>(sprite.editor.Frames.size());
722 sprite.animations.emplace_back(0, frame_count > 0 ? frame_count - 1 : 0, 1,
728 if (ImGui::BeginChild(
"AnimList", ImVec2(0, 120),
true)) {
729 for (
size_t i = 0; i < sprite.animations.size(); i++) {
730 auto& anim = sprite.animations[i];
731 std::string label = anim.frame_name.empty() ?
"Unnamed" : anim.frame_name;
747 Text(
"Animation Properties");
749 static char anim_name[128];
750 CopyStringToBuffer(anim.frame_name, anim_name);
751 if (ImGui::InputText(
"Name##Anim", anim_name,
sizeof(anim_name))) {
752 anim.frame_name = anim_name;
756 int start = anim.frame_start;
757 int end = anim.frame_end;
758 int speed = anim.frame_speed;
760 if (ImGui::SliderInt(
"Start Frame", &start, 0,
761 std::max(0, (
int)sprite.editor.Frames.size() - 1))) {
762 anim.frame_start =
static_cast<uint8_t
>(start);
765 if (ImGui::SliderInt(
"End Frame", &end, 0,
766 std::max(0, (
int)sprite.editor.Frames.size() - 1))) {
767 anim.frame_end =
static_cast<uint8_t
>(end);
770 if (ImGui::SliderInt(
"Speed", &speed, 1, 16)) {
771 anim.frame_speed =
static_cast<uint8_t
>(speed);
775 if (ImGui::Button(
"Delete Animation") && sprite.animations.size() > 1) {
776 sprite.animations.erase(sprite.animations.begin() +
794 sprite.editor.Frames.emplace_back();
801 sprite.editor.Frames.erase(sprite.editor.Frames.begin() +
current_frame_);
810 if (ImGui::BeginChild(
"FrameList", ImVec2(0, 80),
true,
811 ImGuiWindowFlags_HorizontalScrollbar)) {
812 for (
size_t i = 0; i < sprite.editor.Frames.size(); i++) {
813 ImGui::PushID(
static_cast<int>(i));
814 std::string label = absl::StrFormat(
"F%d", i);
816 ImGuiSelectableFlags_None, ImVec2(40, 40))) {
835 frame.Tiles.emplace_back();
841 if (ImGui::BeginChild(
"TileList", ImVec2(0, 100),
true)) {
842 for (
size_t i = 0; i < frame.Tiles.size(); i++) {
843 auto& tile = frame.Tiles[i];
844 std::string label = absl::StrFormat(
"Tile %d (ID: %d)", i, tile.id);
857 int tile_id = tile.id;
858 if (ImGui::InputInt(
"Tile ID", &tile_id)) {
859 tile.id =
static_cast<uint16_t
>(std::clamp(tile_id, 0, 511));
864 int x = tile.x, y = tile.y;
865 if (ImGui::InputInt(
"X", &x)) {
866 tile.x =
static_cast<uint8_t
>(std::clamp(x, 0, 251));
870 if (ImGui::InputInt(
"Y", &y)) {
871 tile.y =
static_cast<uint8_t
>(std::clamp(y, 0, 219));
876 int pal = tile.palette;
877 if (ImGui::SliderInt(
"Palette##Tile", &pal, 0, 7)) {
878 tile.palette =
static_cast<uint8_t
>(pal);
883 if (ImGui::Checkbox(
"16x16", &tile.size)) {
888 if (ImGui::Checkbox(
"Flip X", &tile.mirror_x)) {
893 if (ImGui::Checkbox(
"Flip Y", &tile.mirror_y)) {
898 if (ImGui::Button(
"Delete Tile")) {
924 float frame_duration = anim.frame_speed / 60.0f;
944 sprite.userRoutines.emplace_back(
"New Routine",
"; ASM code here\n");
950 if (ImGui::BeginChild(
"RoutineList", ImVec2(0, 100),
true)) {
951 for (
size_t i = 0; i < sprite.userRoutines.size(); i++) {
952 auto& routine = sprite.userRoutines[i];
967 static char routine_name[128];
968 CopyStringToBuffer(routine.name, routine_name);
969 if (ImGui::InputText(
"Routine Name", routine_name,
sizeof(routine_name))) {
970 routine.name = routine_name;
977 static char code_buffer[16384];
978 CopyStringToBuffer(routine.code, code_buffer);
979 if (ImGui::InputTextMultiline(
"##RoutineCode", code_buffer,
980 sizeof(code_buffer), ImVec2(-1, 200))) {
981 routine.code = code_buffer;
985 if (ImGui::Button(
"Delete Routine")) {
986 sprite.userRoutines.erase(sprite.userRoutines.begin() +
1008 constexpr int kSheetWidth = 128;
1009 constexpr int kSheetHeight = 32;
1010 constexpr int kRowStride = 128;
1012 for (
int sheet_idx = 0; sheet_idx < 8; sheet_idx++) {
1019 if (!sheet.is_active() || sheet.size() == 0) {
1028 int dest_offset = sheet_idx * (kSheetHeight * kRowStride);
1030 const uint8_t* src_data = sheet.data();
1032 std::min(sheet.size(),
static_cast<size_t>(kSheetWidth * kSheetHeight));
1065 for (
size_t i = 0; i < global.size() && i < 8; i++) {
1082 }
else if (aux3.size() > 0) {
1096 bool changed =
false;
1097 for (
int i = 0; i < 4; i++) {
1135 for (
const auto& entry : layout.
tiles) {
1137 tile.
x =
static_cast<uint8_t
>(entry.x_offset + 128);
1138 tile.
y =
static_cast<uint8_t
>(entry.y_offset + 128);
1139 tile.
id = entry.tile_id;
1141 tile.
size = entry.size_16x16;
1157 for (
size_t col_idx = 0; col_idx < 16 && col_idx < sub_pal.size();
1159 combined_palette.
AddColor(sub_pal[col_idx]);
1162 while (combined_palette.
size() < (pal_idx + 1) * 16) {
1183 if (frame_index < 0 || frame_index >= (
int)sprite.editor.Frames.size()) {
1187 auto& frame = sprite.editor.Frames[frame_index];
1215 for (
size_t col_idx = 0; col_idx < 16 && col_idx < sub_pal.size();
1217 combined_palette.
AddColor(sub_pal[col_idx]);
1220 while (combined_palette.
size() < (pal_idx + 1) * 16) {
1238 for (
size_t i = 0; i < frame.Tiles.size(); i++) {
1239 const auto& tile = frame.Tiles[i];
1240 int tile_size = tile.size ? 16 : 8;
1243 int8_t signed_x =
static_cast<int8_t
>(tile.x);
1244 int8_t signed_y =
static_cast<int8_t
>(tile.y);
1246 int canvas_x = 128 + signed_x;
1247 int canvas_y = 128 + signed_y;
1251 ? ImVec4(0.0f, 1.0f, 0.0f, 0.8f)
1252 : ImVec4(1.0f, 1.0f, 0.0f, 0.3f);
1260 if (ImGui::BeginChild(
gui::GetID(
"##ZSpriteCanvas"),
1261 ImGui::GetContentRegionAvail(),
true)) {
1277 ImGui::SetCursorPos(ImVec2(10, 10));
1345 std::move(restore_fn)));
1360 static const std::string kEmptyPath;
project::ResourceLabelManager * resource_label()
UndoManager undo_manager_
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 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 BeginUndoTransaction()
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)
bool sprite_mutated_this_frame_
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_
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_
int current_animation_index_
void DrawBooleanProperties()
void DrawSpritePropertiesPanel()
void SetCurrentZsmPath(const std::string &path)
absl::Status Save() override
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 Load() override
void LoadSpriteGraphicsBuffer()
std::vector< std::string > custom_sprite_paths_
void LoadSpritePalettes()
gfx::Bitmap vanilla_preview_bitmap_
void Push(std::unique_ptr< UndoAction > action)
void RegisterWindowContent(std::unique_ptr< WindowContent > window)
Register a WindowContent instance for central drawing.
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]
WorkspaceWindowManager * window_manager
Snapshot of a custom ZSprite's editable state for undo/redo.
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 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