From 33dc1983bc9528648c12ecd030b38e28423b81e3 Mon Sep 17 00:00:00 2001 From: scawful Date: Tue, 31 Dec 2024 11:43:33 -0500 Subject: [PATCH] Add Rom dependency injection to OverworldEditor --- src/app/editor/editor_manager.cc | 10 +-- src/app/editor/editor_manager.h | 5 +- src/app/editor/overworld/overworld_editor.cc | 65 ++++++++------------ src/app/editor/overworld/overworld_editor.h | 14 ++--- 4 files changed, 38 insertions(+), 56 deletions(-) diff --git a/src/app/editor/editor_manager.cc b/src/app/editor/editor_manager.cc index af2434ab..ccdc9b0f 100644 --- a/src/app/editor/editor_manager.cc +++ b/src/app/editor/editor_manager.cc @@ -48,12 +48,11 @@ bool IsEditorActive(Editor *editor, std::vector &active_editors) { } // namespace -void EditorManager::SetupScreen(std::string filename) { +void EditorManager::Initialize(std::string filename) { if (!filename.empty()) { PRINT_IF_ERROR(rom()->LoadFromFile(filename)); } - overworld_editor_.InitializeZeml(); - InitializeCommands(); + overworld_editor_.Initialize(); } absl::Status EditorManager::Update() { @@ -298,11 +297,6 @@ void EditorManager::ManageKeyboardShortcuts() { } } -void EditorManager::InitializeCommands() { - editor_context_.command_manager.RegisterPrefix("window", 'w', - "window management", ""); -} - void EditorManager::DrawStatusPopup() { static absl::Status prev_status; if (!status_.ok()) { diff --git a/src/app/editor/editor_manager.h b/src/app/editor/editor_manager.h index fd01d4b4..a62f74d1 100644 --- a/src/app/editor/editor_manager.h +++ b/src/app/editor/editor_manager.h @@ -47,7 +47,7 @@ class EditorManager : public SharedRom, public core::ExperimentFlags { active_editors_.push_back(&screen_editor_); } - void SetupScreen(std::string filename = ""); + void Initialize(std::string filename = ""); absl::Status Update(); auto emulator() -> emu::Emulator & { return emulator_; } @@ -56,7 +56,6 @@ class EditorManager : public SharedRom, public core::ExperimentFlags { private: void ManageActiveEditors(); void ManageKeyboardShortcuts(); - void InitializeCommands(); void DrawStatusPopup(); void DrawAboutPopup(); @@ -91,7 +90,7 @@ class EditorManager : public SharedRom, public core::ExperimentFlags { DungeonEditor dungeon_editor_; GraphicsEditor graphics_editor_; MusicEditor music_editor_; - OverworldEditor overworld_editor_; + OverworldEditor overworld_editor_{*rom()}; PaletteEditor palette_editor_; ScreenEditor screen_editor_; SpriteEditor sprite_editor_; diff --git a/src/app/editor/overworld/overworld_editor.cc b/src/app/editor/overworld/overworld_editor.cc index ac4cc9a8..7d4dfe2e 100644 --- a/src/app/editor/overworld/overworld_editor.cc +++ b/src/app/editor/overworld/overworld_editor.cc @@ -58,12 +58,11 @@ constexpr int kTile16Size = 0x10; absl::Status OverworldEditor::Update() { status_ = absl::OkStatus(); - if (rom()->is_loaded() && !all_gfx_loaded_) { + if (rom_.is_loaded() && !all_gfx_loaded_) { RETURN_IF_ERROR(tile16_editor_.InitBlockset( tile16_blockset_bmp_, current_gfx_bmp_, tile16_individual_, *overworld_.mutable_all_tiles_types())); - ASSIGN_OR_RETURN(entrance_tiletypes_, - zelda3::LoadEntranceTileTypes(*rom())); + ASSIGN_OR_RETURN(entrance_tiletypes_, zelda3::LoadEntranceTileTypes(rom_)); all_gfx_loaded_ = true; } @@ -99,21 +98,17 @@ void OverworldEditor::DrawToolset() { if (toolset_table_.column_contents.empty()) { gui::AddTableColumn(toolset_table_, "##Undo", [&]() { - if (Button(ICON_MD_UNDO)) - status_ = Undo(); + if (Button(ICON_MD_UNDO)) status_ = Undo(); }); gui::AddTableColumn(toolset_table_, "##Redo", [&]() { - if (Button(ICON_MD_REDO)) - status_ = Redo(); + if (Button(ICON_MD_REDO)) status_ = Redo(); }); gui::AddTableColumn(toolset_table_, "##Sep1", ICON_MD_MORE_VERT); gui::AddTableColumn(toolset_table_, "##ZoomOut", [&]() { - if (Button(ICON_MD_ZOOM_OUT)) - ow_map_canvas_.ZoomOut(); + if (Button(ICON_MD_ZOOM_OUT)) ow_map_canvas_.ZoomOut(); }); gui::AddTableColumn(toolset_table_, "##ZoomIn", [&]() { - if (Button(ICON_MD_ZOOM_IN)) - ow_map_canvas_.ZoomIn(); + if (Button(ICON_MD_ZOOM_IN)) ow_map_canvas_.ZoomIn(); }); gui::AddTableColumn(toolset_table_, "##Fullscreen", [&]() { if (Button(ICON_MD_OPEN_IN_FULL)) @@ -168,13 +163,11 @@ void OverworldEditor::DrawToolset() { HOVER_HINT("Music"); }); gui::AddTableColumn(toolset_table_, "##Tile16Editor", [&]() { - if (Button(ICON_MD_GRID_VIEW)) - show_tile16_editor_ = !show_tile16_editor_; + if (Button(ICON_MD_GRID_VIEW)) show_tile16_editor_ = !show_tile16_editor_; HOVER_HINT("Tile16 Editor"); }); gui::AddTableColumn(toolset_table_, "##GfxGroupEditor", [&]() { - if (Button(ICON_MD_TABLE_CHART)) - show_gfx_group = !show_gfx_group; + if (Button(ICON_MD_TABLE_CHART)) show_gfx_group = !show_gfx_group; HOVER_HINT("Gfx Group Editor"); }); gui::AddTableColumn(toolset_table_, "##sep3", ICON_MD_MORE_VERT); @@ -503,12 +496,10 @@ void OverworldEditor::CheckForOverworldEdits() { int end_x = std::floor(end.x / kTile16Size) * kTile16Size; int end_y = std::floor(end.y / kTile16Size) * kTile16Size; - if (start_x > end_x) - std::swap(start_x, end_x); - if (start_y > end_y) - std::swap(start_y, end_y); + if (start_x > end_x) std::swap(start_x, end_x); + if (start_y > end_y) std::swap(start_y, end_y); - constexpr int local_map_size = 512; // Size of each local map + constexpr int local_map_size = 512; // Size of each local map // Number of tiles per local map (since each tile is 16x16) constexpr int tiles_per_local_map = local_map_size / kTile16Size; @@ -659,8 +650,7 @@ void OverworldEditor::DrawOverworldCanvas() { if (current_mode == EditingMode::DRAW_TILE) { CheckForOverworldEdits(); } - if (IsItemHovered()) - status_ = CheckForCurrentMap(); + if (IsItemHovered()) status_ = CheckForCurrentMap(); } ow_map_canvas_.DrawGrid(); @@ -715,7 +705,7 @@ void OverworldEditor::DrawTile8Selector() { graphics_bin_canvas_.DrawContextMenu(); if (all_gfx_loaded_) { int key = 0; - for (auto &value : rom()->gfx_sheets()) { + for (auto &value : rom_.gfx_sheets()) { int offset = 0x40 * (key + 1); int top_left_y = graphics_bin_canvas_.zero_point().y + 2; if (key >= 1) { @@ -1041,7 +1031,7 @@ absl::Status OverworldEditor::Save() { absl::Status OverworldEditor::LoadGraphics() { // Load the Link to the Past overworld. - RETURN_IF_ERROR(overworld_.Load(*rom())) + RETURN_IF_ERROR(overworld_.Load(rom_)) palette_ = overworld_.current_area_palette(); // Create the area graphics image @@ -1146,8 +1136,7 @@ void OverworldEditor::RefreshOverworldMap() { // We need to update the map and its siblings if it's a large map for (int i = 1; i < 4; i++) { int sibling_index = overworld_.overworld_map(source_map_id)->parent() + i; - if (i >= 2) - sibling_index += 6; + if (i >= 2) sibling_index += 6; futures.push_back( std::async(std::launch::async, refresh_map_async, sibling_index)); indices[i] = sibling_index; @@ -1176,8 +1165,7 @@ absl::Status OverworldEditor::RefreshMapPalette() { // We need to update the map and its siblings if it's a large map for (int i = 1; i < 4; i++) { int sibling_index = overworld_.overworld_map(current_map_)->parent() + i; - if (i >= 2) - sibling_index += 6; + if (i >= 2) sibling_index += 6; RETURN_IF_ERROR( overworld_.mutable_overworld_map(sibling_index)->LoadPalette()); RETURN_IF_ERROR( @@ -1378,7 +1366,7 @@ absl::Status OverworldEditor::UpdateUsageStats() { if (BeginChild("UnusedSpritesetScroll", ImVec2(0, 0), true, ImGuiWindowFlags_HorizontalScrollbar)) { for (int i = 0; i < 0x81; i++) { - auto entrance_name = rom()->resource_label()->GetLabel( + auto entrance_name = rom_.resource_label()->GetLabel( "Dungeon Entrance Names", core::HexByte(i)); std::string str = absl::StrFormat("%#x - %s", i, entrance_name); if (Selectable(str.c_str(), selected_entrance_ == i, @@ -1420,7 +1408,7 @@ void OverworldEditor::DrawUsageGrid() { int totalSquares = 128; int squaresWide = 8; int squaresTall = (totalSquares + squaresWide - 1) / - squaresWide; // Ceiling of totalSquares/squaresWide + squaresWide; // Ceiling of totalSquares/squaresWide // Loop through each row for (int row = 0; row < squaresTall; ++row) { @@ -1435,8 +1423,9 @@ void OverworldEditor::DrawUsageGrid() { // Set highlight color if needed if (highlight) { - PushStyleColor(ImGuiCol_Button, ImVec4(1.0f, 0.5f, 0.0f, - 1.0f)); // Or any highlight color + PushStyleColor(ImGuiCol_Button, + ImVec4(1.0f, 0.5f, 0.0f, + 1.0f)); // Or any highlight color } // Create a button or selectable for each square @@ -1492,7 +1481,7 @@ void OverworldEditor::DrawDebugWindow() { } } -void OverworldEditor::InitializeZeml() { +void OverworldEditor::Initialize() { // Load zeml string from layouts/overworld.zeml std::string layout = gui::zeml::LoadFile("overworld.zeml"); // Parse the zeml string into a Node object @@ -1503,23 +1492,23 @@ void OverworldEditor::InitializeZeml() { gui::zeml::Bind(&*layout_node_.GetNode("OverworldTileSelector"), [this]() { status_ = DrawTileSelector(); }); gui::zeml::Bind(&*layout_node_.GetNode("OwUsageStats"), [this]() { - if (rom()->is_loaded()) { + if (rom_.is_loaded()) { status_ = UpdateUsageStats(); } }); gui::zeml::Bind(&*layout_node_.GetNode("owToolset"), [this]() { DrawToolset(); }); gui::zeml::Bind(&*layout_node_.GetNode("OwTile16Editor"), [this]() { - if (rom()->is_loaded()) { + if (rom_.is_loaded()) { status_ = tile16_editor_.Update(); } }); gui::zeml::Bind(&*layout_node_.GetNode("OwGfxGroupEditor"), [this]() { - if (rom()->is_loaded()) { + if (rom_.is_loaded()) { status_ = gfx_group_editor_.Update(); } }); } -} // namespace editor -} // namespace yaze +} // namespace editor +} // namespace yaze diff --git a/src/app/editor/overworld/overworld_editor.h b/src/app/editor/overworld/overworld_editor.h index 0e837307..d1816b0d 100644 --- a/src/app/editor/overworld/overworld_editor.h +++ b/src/app/editor/overworld/overworld_editor.h @@ -9,8 +9,8 @@ #include "app/gfx/bitmap.h" #include "app/gfx/snes_palette.h" #include "app/gui/canvas.h" -#include "app/gui/zeml.h" #include "app/gui/input.h" +#include "app/gui/zeml.h" #include "app/rom.h" #include "app/zelda3/overworld/overworld.h" #include "imgui/imgui.h" @@ -56,8 +56,6 @@ constexpr absl::string_view kTileSelectorTab = "##TileSelectorTabBar"; constexpr absl::string_view kOWEditTable = "##OWEditTable"; constexpr absl::string_view kOWMapTable = "#MapSettingsTable"; - - /** * @class OverworldEditor * @brief Manipulates the Overworld and OverworldMap data in a Rom. @@ -75,13 +73,12 @@ constexpr absl::string_view kOWMapTable = "#MapSettingsTable"; * */ class OverworldEditor : public Editor, - public SharedRom, public gfx::GfxContext, public core::ExperimentFlags { public: - OverworldEditor() { type_ = EditorType::kOverworld; } + OverworldEditor(Rom& rom) : rom_(rom) { type_ = EditorType::kOverworld; } - void InitializeZeml(); + void Initialize(); absl::Status Update() final; absl::Status Undo() override { return absl::UnimplementedError("Undo"); } @@ -244,6 +241,8 @@ class OverworldEditor : public Editor, std::vector> tile8_individual_data_; std::vector tile8_individual_; + Rom& rom_; + Tile16Editor tile16_editor_; GfxGroupEditor gfx_group_editor_; PaletteEditor palette_editor_; @@ -283,7 +282,8 @@ class OverworldEditor : public Editor, gui::Canvas properties_canvas_; gui::Table toolset_table_{"##ToolsetTable0", 22, kToolsetTableFlags}; - gui::Table map_settings_table_{kOWMapTable.data(), 8, kOWMapFlags, ImVec2(0,0)}; + gui::Table map_settings_table_{kOWMapTable.data(), 8, kOWMapFlags, + ImVec2(0, 0)}; gui::zeml::Node layout_node_; absl::Status status_;