From c9921c91bf2efe3c8f8551662b313ae9fb68266d Mon Sep 17 00:00:00 2001 From: scawful Date: Sun, 3 Aug 2025 17:53:34 -0400 Subject: [PATCH] Refactor editor manager and dungeon editor for improved clarity and performance - Removed unused functions and streamlined shortcut registration in EditorManager for better readability. - Updated lambda captures to use 'this' pointer for consistency and clarity. - Refactored DungeonEditor to utilize ranges for sorting and filling operations, enhancing performance and readability. - Simplified table setup in DungeonEditor by using a static array for tool names, improving maintainability. --- src/app/editor/dungeon/dungeon_editor.cc | 50 ++++------ src/app/editor/editor_manager.cc | 118 ++++++++++------------- src/app/gfx/background_buffer.cc | 13 +-- src/app/gui/color.cc | 8 +- src/app/zelda3/overworld/overworld.cc | 2 +- 5 files changed, 75 insertions(+), 116 deletions(-) diff --git a/src/app/editor/dungeon/dungeon_editor.cc b/src/app/editor/dungeon/dungeon_editor.cc index b515064f..2dbdd6d1 100644 --- a/src/app/editor/dungeon/dungeon_editor.cc +++ b/src/app/editor/dungeon/dungeon_editor.cc @@ -14,8 +14,7 @@ #include "imgui_memory_editor.h" #include "util/hex.h" -namespace yaze { -namespace editor { +namespace yaze::editor { using core::Renderer; @@ -112,24 +111,19 @@ absl::Status DungeonEditor::Update() { absl::Status DungeonEditor::RefreshGraphics() { std::for_each_n( - rooms_[current_room_id_].blocks().begin(), 8, - [this](int block) -> absl::Status { + rooms_[current_room_id_].blocks().begin(), 8, [this](int block) { gfx::Arena::Get().gfx_sheets()[block].SetPaletteWithTransparent( current_palette_group_[current_palette_id_], 0); - Renderer::Get().UpdateBitmap( - &gfx::Arena::Get().gfx_sheets()[block]); - return absl::OkStatus(); + Renderer::Get().UpdateBitmap(&gfx::Arena::Get().gfx_sheets()[block]); }); auto sprites_aux1_pal_group = rom()->palette_group().sprites_aux1; std::for_each_n( rooms_[current_room_id_].blocks().begin() + 8, 8, - [this, &sprites_aux1_pal_group](int block) -> absl::Status { + [this, &sprites_aux1_pal_group](int block) { gfx::Arena::Get().gfx_sheets()[block].SetPaletteWithTransparent( sprites_aux1_pal_group[current_palette_id_], 0); - Renderer::Get().UpdateBitmap( - &gfx::Arena::Get().gfx_sheets()[block]); - return absl::OkStatus(); + Renderer::Get().UpdateBitmap(&gfx::Arena::Get().gfx_sheets()[block]); }); return absl::OkStatus(); } @@ -144,18 +138,17 @@ void DungeonEditor::LoadDungeonRoomSize() { // Process and calculate room sizes within each bank for (auto &bank_rooms : rooms_by_bank) { // Sort the rooms within this bank - std::sort(bank_rooms.second.begin(), bank_rooms.second.end()); + std::ranges::sort(bank_rooms.second); for (size_t i = 0; i < bank_rooms.second.size(); ++i) { int room_ptr = bank_rooms.second[i]; // Identify the room ID for the current room pointer int room_id = - std::find_if(room_size_addresses_.begin(), room_size_addresses_.end(), - [room_ptr](const auto &entry) { - return entry.second == room_ptr; - }) - ->first; + std::ranges::find_if(room_size_addresses_, [room_ptr]( + const auto &entry) { + return entry.second == room_ptr; + })->first; if (room_ptr != 0x0A8000) { if (i < bank_rooms.second.size() - 1) { @@ -218,21 +211,13 @@ absl::Status DungeonEditor::UpdateDungeonRoomView() { } void DungeonEditor::DrawToolset() { - if (BeginTable("DWToolset", 13, ImGuiTableFlags_SizingFixedFit, + if (BeginTable("DWToolset", 14, ImGuiTableFlags_SizingFixedFit, ImVec2(0, 0))) { - TableSetupColumn("#undoTool"); - TableSetupColumn("#redoTool"); - TableSetupColumn("#separator"); - TableSetupColumn("#anyTool"); - - TableSetupColumn("#bg1Tool"); - TableSetupColumn("#bg2Tool"); - TableSetupColumn("#bg3Tool"); - TableSetupColumn("#separator"); - TableSetupColumn("#spriteTool"); - TableSetupColumn("#itemTool"); - TableSetupColumn("#doorTool"); - TableSetupColumn("#blockTool"); + static std::array tool_names = { + "Undo", "Redo", "Separator", "Any", "BG1", "BG2", "BG3", + "Separator", "Sprite", "Item", "Door", "Block", "Palette"}; + std::ranges::for_each(tool_names, + [](const char *name) { TableSetupColumn(name); }); TableNextColumn(); if (Button(ICON_MD_UNDO)) { @@ -837,5 +822,4 @@ void DungeonEditor::DrawUsageGrid() { } } -} // namespace editor -} // namespace yaze +} // namespace yaze::editor diff --git a/src/app/editor/editor_manager.cc b/src/app/editor/editor_manager.cc index 5f28584a..2a76498e 100644 --- a/src/app/editor/editor_manager.cc +++ b/src/app/editor/editor_manager.cc @@ -33,20 +33,6 @@ using core::FileDialogWrapper; namespace { -bool BeginCentered(const char *name) { - ImGuiIO const &io = GetIO(); - ImVec2 pos(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f); - SetNextWindowPos(pos, ImGuiCond_Always, ImVec2(0.5f, 0.5f)); - ImGuiWindowFlags flags = - ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDecoration | - ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings; - return Begin(name, nullptr, flags); -} - -bool IsEditorActive(Editor *editor, std::vector &active_editors) { - return std::ranges::find(active_editors, editor) != active_editors.end(); -} - std::string GetEditorName(EditorType type) { return kEditorNames[static_cast(type)]; } @@ -82,37 +68,37 @@ void EditorManager::Initialize(const std::string &filename) { context_.popup_manager = popup_manager_.get(); context_.shortcut_manager.RegisterShortcut( - "Open", {ImGuiKey_O, ImGuiMod_Ctrl}, [&]() { status_ = LoadRom(); }); + "Open", {ImGuiKey_O, ImGuiMod_Ctrl}, [this]() { status_ = LoadRom(); }); context_.shortcut_manager.RegisterShortcut( - "Save", {ImGuiKey_S, ImGuiMod_Ctrl}, [&]() { status_ = SaveRom(); }); + "Save", {ImGuiKey_S, ImGuiMod_Ctrl}, [this]() { status_ = SaveRom(); }); context_.shortcut_manager.RegisterShortcut( - "Close", {ImGuiKey_W, ImGuiMod_Ctrl}, [&]() { + "Close", {ImGuiKey_W, ImGuiMod_Ctrl}, [this]() { if (current_rom_) current_rom_->Close(); }); context_.shortcut_manager.RegisterShortcut( - "Quit", {ImGuiKey_Q, ImGuiMod_Ctrl}, [&]() { quit_ = true; }); + "Quit", {ImGuiKey_Q, ImGuiMod_Ctrl}, [this]() { quit_ = true; }); context_.shortcut_manager.RegisterShortcut( "Undo", {ImGuiKey_Z, ImGuiMod_Ctrl}, - [&]() { status_ = current_editor_->Undo(); }); + [this]() { status_ = current_editor_->Undo(); }); context_.shortcut_manager.RegisterShortcut( "Redo", {ImGuiKey_Y, ImGuiMod_Ctrl}, - [&]() { status_ = current_editor_->Redo(); }); + [this]() { status_ = current_editor_->Redo(); }); context_.shortcut_manager.RegisterShortcut( "Cut", {ImGuiKey_X, ImGuiMod_Ctrl}, - [&]() { status_ = current_editor_->Cut(); }); + [this]() { status_ = current_editor_->Cut(); }); context_.shortcut_manager.RegisterShortcut( "Copy", {ImGuiKey_C, ImGuiMod_Ctrl}, - [&]() { status_ = current_editor_->Copy(); }); + [this]() { status_ = current_editor_->Copy(); }); context_.shortcut_manager.RegisterShortcut( "Paste", {ImGuiKey_V, ImGuiMod_Ctrl}, - [&]() { status_ = current_editor_->Paste(); }); + [this]() { status_ = current_editor_->Paste(); }); context_.shortcut_manager.RegisterShortcut( "Find", {ImGuiKey_F, ImGuiMod_Ctrl}, - [&]() { status_ = current_editor_->Find(); }); + [this]() { status_ = current_editor_->Find(); }); context_.shortcut_manager.RegisterShortcut( - "Load Last ROM", {ImGuiKey_R, ImGuiMod_Ctrl}, [&]() { + "Load Last ROM", {ImGuiKey_R, ImGuiMod_Ctrl}, [this]() { static RecentFilesManager manager("recent_files.txt"); manager.Load(); if (!manager.GetRecentFiles().empty()) { @@ -122,7 +108,7 @@ void EditorManager::Initialize(const std::string &filename) { }); context_.shortcut_manager.RegisterShortcut( - "F1", ImGuiKey_F1, [&]() { popup_manager_->Show("About"); }); + "F1", ImGuiKey_F1, [this]() { popup_manager_->Show("About"); }); // Initialize menu items std::vector recent_files; @@ -140,20 +126,20 @@ void EditorManager::Initialize(const std::string &filename) { std::vector options_subitems; options_subitems.emplace_back( - "Backup ROM", "", [&]() { backup_rom_ |= backup_rom_; }, - [&]() { return backup_rom_; }); + "Backup ROM", "", [this]() { backup_rom_ |= backup_rom_; }, + [this]() { return backup_rom_; }); options_subitems.emplace_back( - "Save New Auto", "", [&]() { save_new_auto_ |= save_new_auto_; }, - [&]() { return save_new_auto_; }); + "Save New Auto", "", [this]() { save_new_auto_ |= save_new_auto_; }, + [this]() { return save_new_auto_; }); std::vector project_menu_subitems; project_menu_subitems.emplace_back( - "New Project", "", [&]() { popup_manager_->Show("New Project"); }); + "New Project", "", [this]() { popup_manager_->Show("New Project"); }); project_menu_subitems.emplace_back("Open Project", "", - [&]() { status_ = OpenProject(); }); + [this]() { status_ = OpenProject(); }); project_menu_subitems.emplace_back( - "Save Project", "", [&]() { status_ = SaveProject(); }, - [&]() { return current_project_.project_opened_; }); + "Save Project", "", [this]() { status_ = SaveProject(); }, + [this]() { return current_project_.project_opened_; }); gui::kMainMenu = { {"File", @@ -164,24 +150,24 @@ void EditorManager::Initialize(const std::string &filename) { {absl::StrCat(ICON_MD_FILE_OPEN, " Open"), context_.shortcut_manager.GetKeys("Open"), context_.shortcut_manager.GetCallback("Open")}, - {"Open Recent", "", [&]() {}, + {"Open Recent", "", []() {}, []() { return !manager.GetRecentFiles().empty(); }, recent_files}, {absl::StrCat(ICON_MD_FILE_DOWNLOAD, " Save"), context_.shortcut_manager.GetKeys("Save"), context_.shortcut_manager.GetCallback("Save")}, {absl::StrCat(ICON_MD_SAVE_AS, " Save As.."), "", - [&]() { popup_manager_->Show("Save As.."); }}, - {absl::StrCat(ICON_MD_BALLOT, " Project"), "", [&]() {}, + [this]() { popup_manager_->Show("Save As.."); }}, + {absl::StrCat(ICON_MD_BALLOT, " Project"), "", []() {}, []() { return true; }, project_menu_subitems}, {absl::StrCat(ICON_MD_CLOSE, " Close"), "", - [&]() { + [this]() { if (current_rom_) current_rom_->Close(); }}, {gui::kSeparator, "", nullptr, []() { return true; }}, {absl::StrCat(ICON_MD_MISCELLANEOUS_SERVICES, " Options"), "", - [&]() {}, []() { return true; }, options_subitems}, + []() {}, []() { return true; }, options_subitems}, {absl::StrCat(ICON_MD_EXIT_TO_APP, " Quit"), "Ctrl+Q", - [&]() { quit_ = true; }}, + [this]() { quit_ = true; }}, }}, {"Edit", {}, @@ -286,35 +272,36 @@ absl::Status EditorManager::Update() { popup_manager_->DrawPopups(); ExecuteShortcuts(context_.shortcut_manager); - if (current_editor_set_) { - for (auto editor : current_editor_set_->active_editors_) { - if (*editor->active()) { - if (editor->type() == EditorType::kOverworld) { - auto &overworld_editor = static_cast(*editor); - if (overworld_editor.jump_to_tab() != -1) { - current_editor_set_->dungeon_editor_.set_active(true); - // Set the dungeon editor to the jump to tab - current_editor_set_->dungeon_editor_.add_room( - overworld_editor.jump_to_tab()); - overworld_editor.jump_to_tab_ = -1; - } - } - - if (ImGui::Begin(GetEditorName(editor->type()).c_str(), - editor->active())) { - current_editor_ = editor; - status_ = editor->Update(); - } - ImGui::End(); - } - } - } - if (show_homepage_) { ImGui::Begin("Home", &show_homepage_); DrawHomepage(); ImGui::End(); } + + if (!current_editor_set_) { + return absl::OkStatus(); + } + for (auto editor : current_editor_set_->active_editors_) { + if (*editor->active()) { + if (editor->type() == EditorType::kOverworld) { + auto &overworld_editor = static_cast(*editor); + if (overworld_editor.jump_to_tab() != -1) { + current_editor_set_->dungeon_editor_.set_active(true); + // Set the dungeon editor to the jump to tab + current_editor_set_->dungeon_editor_.add_room( + overworld_editor.jump_to_tab()); + overworld_editor.jump_to_tab_ = -1; + } + } + + if (ImGui::Begin(GetEditorName(editor->type()).c_str(), + editor->active())) { + current_editor_ = editor; + status_ = editor->Update(); + } + ImGui::End(); + } + } return absl::OkStatus(); } @@ -591,7 +578,6 @@ absl::Status EditorManager::OpenRomOrProject(const std::string &filename) { auto new_rom = std::make_unique(); RETURN_IF_ERROR(new_rom->LoadFromFile(filename)); current_rom_ = new_rom.get(); - SharedRom::set_rom(current_rom_); roms_.push_back(std::move(new_rom)); // Create new editor set for this ROM @@ -665,12 +651,10 @@ absl::Status EditorManager::SetCurrentRom(Rom *rom) { editor_sets_[rom] = std::move(editor_set); current_rom_ = rom; - SharedRom::set_rom(rom); RETURN_IF_ERROR(LoadAssets()); } else { current_editor_set_ = it->second.get(); current_rom_ = rom; - SharedRom::set_rom(current_rom_); } return absl::OkStatus(); diff --git a/src/app/gfx/background_buffer.cc b/src/app/gfx/background_buffer.cc index 88de5b64..c983b238 100644 --- a/src/app/gfx/background_buffer.cc +++ b/src/app/gfx/background_buffer.cc @@ -4,12 +4,10 @@ #include #include -#include "absl/log/log.h" #include "app/gfx/bitmap.h" #include "app/gfx/snes_tile.h" -namespace yaze { -namespace gfx { +namespace yaze::gfx { BackgroundBuffer::BackgroundBuffer(int width, int height) : width_(width), height_(height) { @@ -31,9 +29,7 @@ uint16_t BackgroundBuffer::GetTileAt(int x, int y) const { return 0; } -void BackgroundBuffer::ClearBuffer() { - std::fill(buffer_.begin(), buffer_.end(), 0); -} +void BackgroundBuffer::ClearBuffer() { std::ranges::fill(buffer_, 0); } void BackgroundBuffer::DrawTile(const TileInfo& tile, uint8_t* canvas, const uint8_t* tiledata, int indexoffset) { @@ -78,7 +74,7 @@ void BackgroundBuffer::DrawBackground(std::span gfx16_data) { void BackgroundBuffer::DrawFloor(const std::vector& rom_data, int tile_address, int tile_address_floor, uint8_t floor_graphics) { - uint8_t f = (uint8_t)(floor_graphics << 4); + auto f = (uint8_t)(floor_graphics << 4); // Create floor tiles from ROM data gfx::TileInfo floorTile1(rom_data[tile_address + f], @@ -115,5 +111,4 @@ void BackgroundBuffer::DrawFloor(const std::vector& rom_data, } } -} // namespace gfx -} // namespace yaze \ No newline at end of file +} // namespace yaze::gfx diff --git a/src/app/gui/color.cc b/src/app/gui/color.cc index bf5c8afd..4d12a06c 100644 --- a/src/app/gui/color.cc +++ b/src/app/gui/color.cc @@ -8,12 +8,8 @@ namespace yaze { namespace gui { ImVec4 ConvertSnesColorToImVec4(const gfx::SnesColor& color) { - return ImVec4(static_cast(color.rgb().x) / 255.0f, - static_cast(color.rgb().y) / 255.0f, - static_cast(color.rgb().z) / 255.0f, - 1.0f // Assuming alpha is always fully opaque for SNES colors, - // adjust if necessary - ); + return ImVec4(color.rgb().x / 255.0f, color.rgb().y / 255.0f, + color.rgb().z / 255.0f, 1.0f); } gfx::SnesColor ConvertImVec4ToSnesColor(const ImVec4& color) { diff --git a/src/app/zelda3/overworld/overworld.cc b/src/app/zelda3/overworld/overworld.cc index 6a7fa686..af18cdfd 100644 --- a/src/app/zelda3/overworld/overworld.cc +++ b/src/app/zelda3/overworld/overworld.cc @@ -55,7 +55,7 @@ void Overworld::FetchLargeMaps() { overworld_maps_[136].SetAsSmallMap(); std::array map_checked; - std::fill(map_checked.begin(), map_checked.end(), false); + std::ranges::fill(map_checked, false); int xx = 0; int yy = 0;