From f5ac6124b0edb0c4bc4d7d0a9c9ab777040cb8ff Mon Sep 17 00:00:00 2001 From: scawful Date: Sun, 10 Nov 2024 12:31:03 -0500 Subject: [PATCH] Implement LoadBinaryGfx functionality: Refactor GFX loading from BIN file into a separate method, improve error handling, and update UI button for better user experience --- src/app/editor/graphics/screen_editor.cc | 64 +++++++++++++----------- src/app/editor/graphics/screen_editor.h | 6 ++- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/app/editor/graphics/screen_editor.cc b/src/app/editor/graphics/screen_editor.cc index 228e2078..7400ad35 100644 --- a/src/app/editor/graphics/screen_editor.cc +++ b/src/app/editor/graphics/screen_editor.cc @@ -41,8 +41,7 @@ absl::Status ScreenEditor::Update() { DrawTitleScreenEditor(); DrawNamingScreenEditor(); END_TAB_BAR() - - return absl::OkStatus(); + return status_; } void ScreenEditor::DrawInventoryMenuEditor() { @@ -319,7 +318,7 @@ void ScreenEditor::DrawDungeonMapsTabs() { gui::InputHexWord("Boss Room", ¤t_dungeon.boss_room); - const ImVec2 button_size = ImVec2(120, 0); + const ImVec2 button_size = ImVec2(130, 0); // Add Floor Button if (ImGui::Button("Add Floor", button_size) && @@ -421,6 +420,7 @@ void ScreenEditor::DrawDungeonMapsEditor() { if (!screen_canvas_.points().empty()) { dungeon_maps_[selected_dungeon] .floor_gfx[floor_number][selected_room] = selected_tile16_; + tilesheet_canvas_.mutable_points()->clear(); } } } @@ -432,38 +432,42 @@ void ScreenEditor::DrawDungeonMapsEditor() { tilemap_canvas_.DrawBitmapTable(sheets_); tilemap_canvas_.DrawGrid(); tilemap_canvas_.DrawOverlay(); - - if (ImGui::Button("Load GFX from BIN file")) { - std::string bin_file = core::FileDialogWrapper::ShowOpenFileDialog(); - if (!bin_file.empty()) { - std::ifstream file(bin_file, std::ios::binary); - if (file.is_open()) { - // Read the gfx data into a buffer - std::vector bin_data((std::istreambuf_iterator(file)), - std::istreambuf_iterator()); - auto converted_bin = gfx::SnesTo8bppSheet(bin_data, 4, 4); - tile16_sheet_.clear(); - LoadDungeonMapTile16(converted_bin, true); - sheets_.clear(); - std::vector> gfx_sheets; - - // Divide the bin into 4 sheets - for (int i = 0; i < 4; i++) { - gfx_sheets.emplace_back(converted_bin.begin() + (i * 0x1000), - converted_bin.begin() + ((i + 1) * 0x1000)); - sheets_.emplace(i, gfx::Bitmap(128, 32, 8, gfx_sheets[i])); - sheets_[i].ApplyPalette(*rom()->mutable_dungeon_palette(3)); - Renderer::GetInstance().RenderBitmap(&sheets_[i]); - } - file.close(); - } - } - } + ImGui::Separator(); + if (ImGui::Button("Load GFX from BIN file")) LoadBinaryGfx(); ImGui::EndTable(); } } +void ScreenEditor::LoadBinaryGfx() { + std::string bin_file = core::FileDialogWrapper::ShowOpenFileDialog(); + if (!bin_file.empty()) { + std::ifstream file(bin_file, std::ios::binary); + if (file.is_open()) { + // Read the gfx data into a buffer + std::vector bin_data((std::istreambuf_iterator(file)), + std::istreambuf_iterator()); + auto converted_bin = gfx::SnesTo8bppSheet(bin_data, 4, 4); + tile16_sheet_.clear(); + if (LoadDungeonMapTile16(converted_bin, true).ok()) { + sheets_.clear(); + std::vector> gfx_sheets; + for (int i = 0; i < 4; i++) { + gfx_sheets.emplace_back(converted_bin.begin() + (i * 0x1000), + converted_bin.begin() + ((i + 1) * 0x1000)); + sheets_.emplace(i, gfx::Bitmap(128, 32, 8, gfx_sheets[i])); + sheets_[i].ApplyPalette(*rom()->mutable_dungeon_palette(3)); + Renderer::GetInstance().RenderBitmap(&sheets_[i]); + } + binary_gfx_loaded_ = true; + } else { + status_ = absl::InternalError("Failed to load dungeon map tile16"); + } + file.close(); + } + } +} + void ScreenEditor::DrawTitleScreenEditor() { TAB_ITEM("Title Screen") END_TAB_ITEM() diff --git a/src/app/editor/graphics/screen_editor.h b/src/app/editor/graphics/screen_editor.h index 5dcfe53b..29a0dbc9 100644 --- a/src/app/editor/graphics/screen_editor.h +++ b/src/app/editor/graphics/screen_editor.h @@ -64,11 +64,15 @@ class ScreenEditor : public SharedRom, public Editor { void DrawInventoryToolset(); absl::Status LoadDungeonMaps(); - absl::Status LoadDungeonMapTile16(const std::vector& gfx_data, bool bin_mode = false); + absl::Status LoadDungeonMapTile16(const std::vector& gfx_data, + bool bin_mode = false); void DrawDungeonMapsTabs(); void DrawDungeonMapsEditor(); + void LoadBinaryGfx(); + bool dungeon_maps_loaded_ = false; + bool binary_gfx_loaded_ = false; uint8_t selected_room = 0; uint8_t boss_room = 0;