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.
This commit is contained in:
scawful
2025-08-03 17:53:34 -04:00
parent fdda77c172
commit c9921c91bf
5 changed files with 75 additions and 116 deletions

View File

@@ -14,8 +14,7 @@
#include "imgui_memory_editor.h" #include "imgui_memory_editor.h"
#include "util/hex.h" #include "util/hex.h"
namespace yaze { namespace yaze::editor {
namespace editor {
using core::Renderer; using core::Renderer;
@@ -112,24 +111,19 @@ absl::Status DungeonEditor::Update() {
absl::Status DungeonEditor::RefreshGraphics() { absl::Status DungeonEditor::RefreshGraphics() {
std::for_each_n( std::for_each_n(
rooms_[current_room_id_].blocks().begin(), 8, rooms_[current_room_id_].blocks().begin(), 8, [this](int block) {
[this](int block) -> absl::Status {
gfx::Arena::Get().gfx_sheets()[block].SetPaletteWithTransparent( gfx::Arena::Get().gfx_sheets()[block].SetPaletteWithTransparent(
current_palette_group_[current_palette_id_], 0); current_palette_group_[current_palette_id_], 0);
Renderer::Get().UpdateBitmap( Renderer::Get().UpdateBitmap(&gfx::Arena::Get().gfx_sheets()[block]);
&gfx::Arena::Get().gfx_sheets()[block]);
return absl::OkStatus();
}); });
auto sprites_aux1_pal_group = rom()->palette_group().sprites_aux1; auto sprites_aux1_pal_group = rom()->palette_group().sprites_aux1;
std::for_each_n( std::for_each_n(
rooms_[current_room_id_].blocks().begin() + 8, 8, 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( gfx::Arena::Get().gfx_sheets()[block].SetPaletteWithTransparent(
sprites_aux1_pal_group[current_palette_id_], 0); sprites_aux1_pal_group[current_palette_id_], 0);
Renderer::Get().UpdateBitmap( Renderer::Get().UpdateBitmap(&gfx::Arena::Get().gfx_sheets()[block]);
&gfx::Arena::Get().gfx_sheets()[block]);
return absl::OkStatus();
}); });
return absl::OkStatus(); return absl::OkStatus();
} }
@@ -144,18 +138,17 @@ void DungeonEditor::LoadDungeonRoomSize() {
// Process and calculate room sizes within each bank // Process and calculate room sizes within each bank
for (auto &bank_rooms : rooms_by_bank) { for (auto &bank_rooms : rooms_by_bank) {
// Sort the rooms within this 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) { for (size_t i = 0; i < bank_rooms.second.size(); ++i) {
int room_ptr = bank_rooms.second[i]; int room_ptr = bank_rooms.second[i];
// Identify the room ID for the current room pointer // Identify the room ID for the current room pointer
int room_id = int room_id =
std::find_if(room_size_addresses_.begin(), room_size_addresses_.end(), std::ranges::find_if(room_size_addresses_, [room_ptr](
[room_ptr](const auto &entry) { const auto &entry) {
return entry.second == room_ptr; return entry.second == room_ptr;
}) })->first;
->first;
if (room_ptr != 0x0A8000) { if (room_ptr != 0x0A8000) {
if (i < bank_rooms.second.size() - 1) { if (i < bank_rooms.second.size() - 1) {
@@ -218,21 +211,13 @@ absl::Status DungeonEditor::UpdateDungeonRoomView() {
} }
void DungeonEditor::DrawToolset() { void DungeonEditor::DrawToolset() {
if (BeginTable("DWToolset", 13, ImGuiTableFlags_SizingFixedFit, if (BeginTable("DWToolset", 14, ImGuiTableFlags_SizingFixedFit,
ImVec2(0, 0))) { ImVec2(0, 0))) {
TableSetupColumn("#undoTool"); static std::array<const char *, 14> tool_names = {
TableSetupColumn("#redoTool"); "Undo", "Redo", "Separator", "Any", "BG1", "BG2", "BG3",
TableSetupColumn("#separator"); "Separator", "Sprite", "Item", "Door", "Block", "Palette"};
TableSetupColumn("#anyTool"); std::ranges::for_each(tool_names,
[](const char *name) { TableSetupColumn(name); });
TableSetupColumn("#bg1Tool");
TableSetupColumn("#bg2Tool");
TableSetupColumn("#bg3Tool");
TableSetupColumn("#separator");
TableSetupColumn("#spriteTool");
TableSetupColumn("#itemTool");
TableSetupColumn("#doorTool");
TableSetupColumn("#blockTool");
TableNextColumn(); TableNextColumn();
if (Button(ICON_MD_UNDO)) { if (Button(ICON_MD_UNDO)) {
@@ -837,5 +822,4 @@ void DungeonEditor::DrawUsageGrid() {
} }
} }
} // namespace editor } // namespace yaze::editor
} // namespace yaze

View File

@@ -33,20 +33,6 @@ using core::FileDialogWrapper;
namespace { 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<Editor *> &active_editors) {
return std::ranges::find(active_editors, editor) != active_editors.end();
}
std::string GetEditorName(EditorType type) { std::string GetEditorName(EditorType type) {
return kEditorNames[static_cast<int>(type)]; return kEditorNames[static_cast<int>(type)];
} }
@@ -82,37 +68,37 @@ void EditorManager::Initialize(const std::string &filename) {
context_.popup_manager = popup_manager_.get(); context_.popup_manager = popup_manager_.get();
context_.shortcut_manager.RegisterShortcut( context_.shortcut_manager.RegisterShortcut(
"Open", {ImGuiKey_O, ImGuiMod_Ctrl}, [&]() { status_ = LoadRom(); }); "Open", {ImGuiKey_O, ImGuiMod_Ctrl}, [this]() { status_ = LoadRom(); });
context_.shortcut_manager.RegisterShortcut( context_.shortcut_manager.RegisterShortcut(
"Save", {ImGuiKey_S, ImGuiMod_Ctrl}, [&]() { status_ = SaveRom(); }); "Save", {ImGuiKey_S, ImGuiMod_Ctrl}, [this]() { status_ = SaveRom(); });
context_.shortcut_manager.RegisterShortcut( context_.shortcut_manager.RegisterShortcut(
"Close", {ImGuiKey_W, ImGuiMod_Ctrl}, [&]() { "Close", {ImGuiKey_W, ImGuiMod_Ctrl}, [this]() {
if (current_rom_) current_rom_->Close(); if (current_rom_) current_rom_->Close();
}); });
context_.shortcut_manager.RegisterShortcut( context_.shortcut_manager.RegisterShortcut(
"Quit", {ImGuiKey_Q, ImGuiMod_Ctrl}, [&]() { quit_ = true; }); "Quit", {ImGuiKey_Q, ImGuiMod_Ctrl}, [this]() { quit_ = true; });
context_.shortcut_manager.RegisterShortcut( context_.shortcut_manager.RegisterShortcut(
"Undo", {ImGuiKey_Z, ImGuiMod_Ctrl}, "Undo", {ImGuiKey_Z, ImGuiMod_Ctrl},
[&]() { status_ = current_editor_->Undo(); }); [this]() { status_ = current_editor_->Undo(); });
context_.shortcut_manager.RegisterShortcut( context_.shortcut_manager.RegisterShortcut(
"Redo", {ImGuiKey_Y, ImGuiMod_Ctrl}, "Redo", {ImGuiKey_Y, ImGuiMod_Ctrl},
[&]() { status_ = current_editor_->Redo(); }); [this]() { status_ = current_editor_->Redo(); });
context_.shortcut_manager.RegisterShortcut( context_.shortcut_manager.RegisterShortcut(
"Cut", {ImGuiKey_X, ImGuiMod_Ctrl}, "Cut", {ImGuiKey_X, ImGuiMod_Ctrl},
[&]() { status_ = current_editor_->Cut(); }); [this]() { status_ = current_editor_->Cut(); });
context_.shortcut_manager.RegisterShortcut( context_.shortcut_manager.RegisterShortcut(
"Copy", {ImGuiKey_C, ImGuiMod_Ctrl}, "Copy", {ImGuiKey_C, ImGuiMod_Ctrl},
[&]() { status_ = current_editor_->Copy(); }); [this]() { status_ = current_editor_->Copy(); });
context_.shortcut_manager.RegisterShortcut( context_.shortcut_manager.RegisterShortcut(
"Paste", {ImGuiKey_V, ImGuiMod_Ctrl}, "Paste", {ImGuiKey_V, ImGuiMod_Ctrl},
[&]() { status_ = current_editor_->Paste(); }); [this]() { status_ = current_editor_->Paste(); });
context_.shortcut_manager.RegisterShortcut( context_.shortcut_manager.RegisterShortcut(
"Find", {ImGuiKey_F, ImGuiMod_Ctrl}, "Find", {ImGuiKey_F, ImGuiMod_Ctrl},
[&]() { status_ = current_editor_->Find(); }); [this]() { status_ = current_editor_->Find(); });
context_.shortcut_manager.RegisterShortcut( 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"); static RecentFilesManager manager("recent_files.txt");
manager.Load(); manager.Load();
if (!manager.GetRecentFiles().empty()) { if (!manager.GetRecentFiles().empty()) {
@@ -122,7 +108,7 @@ void EditorManager::Initialize(const std::string &filename) {
}); });
context_.shortcut_manager.RegisterShortcut( context_.shortcut_manager.RegisterShortcut(
"F1", ImGuiKey_F1, [&]() { popup_manager_->Show("About"); }); "F1", ImGuiKey_F1, [this]() { popup_manager_->Show("About"); });
// Initialize menu items // Initialize menu items
std::vector<gui::MenuItem> recent_files; std::vector<gui::MenuItem> recent_files;
@@ -140,20 +126,20 @@ void EditorManager::Initialize(const std::string &filename) {
std::vector<gui::MenuItem> options_subitems; std::vector<gui::MenuItem> options_subitems;
options_subitems.emplace_back( options_subitems.emplace_back(
"Backup ROM", "", [&]() { backup_rom_ |= backup_rom_; }, "Backup ROM", "", [this]() { backup_rom_ |= backup_rom_; },
[&]() { return backup_rom_; }); [this]() { return backup_rom_; });
options_subitems.emplace_back( options_subitems.emplace_back(
"Save New Auto", "", [&]() { save_new_auto_ |= save_new_auto_; }, "Save New Auto", "", [this]() { save_new_auto_ |= save_new_auto_; },
[&]() { return save_new_auto_; }); [this]() { return save_new_auto_; });
std::vector<gui::MenuItem> project_menu_subitems; std::vector<gui::MenuItem> project_menu_subitems;
project_menu_subitems.emplace_back( 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", "", project_menu_subitems.emplace_back("Open Project", "",
[&]() { status_ = OpenProject(); }); [this]() { status_ = OpenProject(); });
project_menu_subitems.emplace_back( project_menu_subitems.emplace_back(
"Save Project", "", [&]() { status_ = SaveProject(); }, "Save Project", "", [this]() { status_ = SaveProject(); },
[&]() { return current_project_.project_opened_; }); [this]() { return current_project_.project_opened_; });
gui::kMainMenu = { gui::kMainMenu = {
{"File", {"File",
@@ -164,24 +150,24 @@ void EditorManager::Initialize(const std::string &filename) {
{absl::StrCat(ICON_MD_FILE_OPEN, " Open"), {absl::StrCat(ICON_MD_FILE_OPEN, " Open"),
context_.shortcut_manager.GetKeys("Open"), context_.shortcut_manager.GetKeys("Open"),
context_.shortcut_manager.GetCallback("Open")}, context_.shortcut_manager.GetCallback("Open")},
{"Open Recent", "", [&]() {}, {"Open Recent", "", []() {},
[]() { return !manager.GetRecentFiles().empty(); }, recent_files}, []() { return !manager.GetRecentFiles().empty(); }, recent_files},
{absl::StrCat(ICON_MD_FILE_DOWNLOAD, " Save"), {absl::StrCat(ICON_MD_FILE_DOWNLOAD, " Save"),
context_.shortcut_manager.GetKeys("Save"), context_.shortcut_manager.GetKeys("Save"),
context_.shortcut_manager.GetCallback("Save")}, context_.shortcut_manager.GetCallback("Save")},
{absl::StrCat(ICON_MD_SAVE_AS, " Save As.."), "", {absl::StrCat(ICON_MD_SAVE_AS, " Save As.."), "",
[&]() { popup_manager_->Show("Save As.."); }}, [this]() { popup_manager_->Show("Save As.."); }},
{absl::StrCat(ICON_MD_BALLOT, " Project"), "", [&]() {}, {absl::StrCat(ICON_MD_BALLOT, " Project"), "", []() {},
[]() { return true; }, project_menu_subitems}, []() { return true; }, project_menu_subitems},
{absl::StrCat(ICON_MD_CLOSE, " Close"), "", {absl::StrCat(ICON_MD_CLOSE, " Close"), "",
[&]() { [this]() {
if (current_rom_) current_rom_->Close(); if (current_rom_) current_rom_->Close();
}}, }},
{gui::kSeparator, "", nullptr, []() { return true; }}, {gui::kSeparator, "", nullptr, []() { return true; }},
{absl::StrCat(ICON_MD_MISCELLANEOUS_SERVICES, " Options"), "", {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", {absl::StrCat(ICON_MD_EXIT_TO_APP, " Quit"), "Ctrl+Q",
[&]() { quit_ = true; }}, [this]() { quit_ = true; }},
}}, }},
{"Edit", {"Edit",
{}, {},
@@ -286,7 +272,15 @@ absl::Status EditorManager::Update() {
popup_manager_->DrawPopups(); popup_manager_->DrawPopups();
ExecuteShortcuts(context_.shortcut_manager); ExecuteShortcuts(context_.shortcut_manager);
if (current_editor_set_) { 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_) { for (auto editor : current_editor_set_->active_editors_) {
if (*editor->active()) { if (*editor->active()) {
if (editor->type() == EditorType::kOverworld) { if (editor->type() == EditorType::kOverworld) {
@@ -308,13 +302,6 @@ absl::Status EditorManager::Update() {
ImGui::End(); ImGui::End();
} }
} }
}
if (show_homepage_) {
ImGui::Begin("Home", &show_homepage_);
DrawHomepage();
ImGui::End();
}
return absl::OkStatus(); return absl::OkStatus();
} }
@@ -591,7 +578,6 @@ absl::Status EditorManager::OpenRomOrProject(const std::string &filename) {
auto new_rom = std::make_unique<Rom>(); auto new_rom = std::make_unique<Rom>();
RETURN_IF_ERROR(new_rom->LoadFromFile(filename)); RETURN_IF_ERROR(new_rom->LoadFromFile(filename));
current_rom_ = new_rom.get(); current_rom_ = new_rom.get();
SharedRom::set_rom(current_rom_);
roms_.push_back(std::move(new_rom)); roms_.push_back(std::move(new_rom));
// Create new editor set for this 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); editor_sets_[rom] = std::move(editor_set);
current_rom_ = rom; current_rom_ = rom;
SharedRom::set_rom(rom);
RETURN_IF_ERROR(LoadAssets()); RETURN_IF_ERROR(LoadAssets());
} else { } else {
current_editor_set_ = it->second.get(); current_editor_set_ = it->second.get();
current_rom_ = rom; current_rom_ = rom;
SharedRom::set_rom(current_rom_);
} }
return absl::OkStatus(); return absl::OkStatus();

View File

@@ -4,12 +4,10 @@
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include "absl/log/log.h"
#include "app/gfx/bitmap.h" #include "app/gfx/bitmap.h"
#include "app/gfx/snes_tile.h" #include "app/gfx/snes_tile.h"
namespace yaze { namespace yaze::gfx {
namespace gfx {
BackgroundBuffer::BackgroundBuffer(int width, int height) BackgroundBuffer::BackgroundBuffer(int width, int height)
: width_(width), height_(height) { : width_(width), height_(height) {
@@ -31,9 +29,7 @@ uint16_t BackgroundBuffer::GetTileAt(int x, int y) const {
return 0; return 0;
} }
void BackgroundBuffer::ClearBuffer() { void BackgroundBuffer::ClearBuffer() { std::ranges::fill(buffer_, 0); }
std::fill(buffer_.begin(), buffer_.end(), 0);
}
void BackgroundBuffer::DrawTile(const TileInfo& tile, uint8_t* canvas, void BackgroundBuffer::DrawTile(const TileInfo& tile, uint8_t* canvas,
const uint8_t* tiledata, int indexoffset) { const uint8_t* tiledata, int indexoffset) {
@@ -78,7 +74,7 @@ void BackgroundBuffer::DrawBackground(std::span<uint8_t> gfx16_data) {
void BackgroundBuffer::DrawFloor(const std::vector<uint8_t>& rom_data, void BackgroundBuffer::DrawFloor(const std::vector<uint8_t>& rom_data,
int tile_address, int tile_address_floor, int tile_address, int tile_address_floor,
uint8_t floor_graphics) { 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 // Create floor tiles from ROM data
gfx::TileInfo floorTile1(rom_data[tile_address + f], gfx::TileInfo floorTile1(rom_data[tile_address + f],
@@ -115,5 +111,4 @@ void BackgroundBuffer::DrawFloor(const std::vector<uint8_t>& rom_data,
} }
} }
} // namespace gfx } // namespace yaze::gfx
} // namespace yaze

View File

@@ -8,12 +8,8 @@ namespace yaze {
namespace gui { namespace gui {
ImVec4 ConvertSnesColorToImVec4(const gfx::SnesColor& color) { ImVec4 ConvertSnesColorToImVec4(const gfx::SnesColor& color) {
return ImVec4(static_cast<float>(color.rgb().x) / 255.0f, return ImVec4(color.rgb().x / 255.0f, color.rgb().y / 255.0f,
static_cast<float>(color.rgb().y) / 255.0f, color.rgb().z / 255.0f, 1.0f);
static_cast<float>(color.rgb().z) / 255.0f,
1.0f // Assuming alpha is always fully opaque for SNES colors,
// adjust if necessary
);
} }
gfx::SnesColor ConvertImVec4ToSnesColor(const ImVec4& color) { gfx::SnesColor ConvertImVec4ToSnesColor(const ImVec4& color) {

View File

@@ -55,7 +55,7 @@ void Overworld::FetchLargeMaps() {
overworld_maps_[136].SetAsSmallMap(); overworld_maps_[136].SetAsSmallMap();
std::array<bool, kNumMapsPerWorld> map_checked; std::array<bool, kNumMapsPerWorld> map_checked;
std::fill(map_checked.begin(), map_checked.end(), false); std::ranges::fill(map_checked, false);
int xx = 0; int xx = 0;
int yy = 0; int yy = 0;