Refactor EditorManager to utilize EditorSet for managing multiple editors per ROM, enhance ROM loading and saving processes, and improve popup management integration.

This commit is contained in:
scawful
2025-04-11 02:57:50 -04:00
parent b6bed9e92e
commit 216ee23f51
4 changed files with 303 additions and 220 deletions

View File

@@ -18,4 +18,5 @@ set(
app/editor/system/command_manager.cc app/editor/system/command_manager.cc
app/editor/system/extension_manager.cc app/editor/system/extension_manager.cc
app/editor/system/shortcut_manager.cc app/editor/system/shortcut_manager.cc
app/editor/system/popup_manager.cc
) )

View File

@@ -22,7 +22,7 @@ struct EditorContext {
CommandManager command_manager; CommandManager command_manager;
ExtensionManager extension_manager; ExtensionManager extension_manager;
HistoryManager history_manager; HistoryManager history_manager;
PopupManager popup_manager; PopupManager* popup_manager = nullptr;
ShortcutManager shortcut_manager; ShortcutManager shortcut_manager;
}; };
@@ -76,6 +76,8 @@ class Editor {
virtual absl::Status Find() = 0; virtual absl::Status Find() = 0;
virtual absl::Status Clear() { return absl::OkStatus(); }
EditorType type() const { return type_; } EditorType type() const { return type_; }
void set_context(EditorContext* context) { context_ = context; } void set_context(EditorContext* context) { context_ = context; }

View File

@@ -43,56 +43,55 @@ bool BeginCentered(const char *name) {
} }
bool IsEditorActive(Editor *editor, std::vector<Editor *> &active_editors) { bool IsEditorActive(Editor *editor, std::vector<Editor *> &active_editors) {
return std::find(active_editors.begin(), active_editors.end(), editor) != return std::ranges::find(active_editors, editor) != active_editors.end();
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)];
} }
void DrawRomSelector(EditorManager &editor_manager) {
const auto &roms = editor_manager.GetRoms();
const Rom *current_rom = editor_manager.GetCurrentRom();
SameLine((GetWindowWidth() / 2) - 100);
if (current_rom && current_rom->is_loaded()) {
SetNextItemWidth(GetWindowWidth() / 4);
if (BeginCombo("##ROMSelector", current_rom->filename().c_str())) {
for (const auto &rom : roms) {
if (MenuItem(rom->filename().c_str())) {
editor_manager.SetCurrentRom(rom.get());
}
}
EndCombo();
}
} else {
Text("No ROM loaded");
}
}
} // namespace } // namespace
void EditorManager::Initialize(const std::string &filename) { void EditorManager::Initialize(const std::string &filename) {
if (!filename.empty()) { if (!filename.empty()) {
PRINT_IF_ERROR(rom()->LoadFromFile(filename)); PRINT_IF_ERROR(OpenRomOrProject(filename));
} }
std::vector<gui::MenuItem> recent_files; // Initialize popup manager
static RecentFilesManager manager("recent_files.txt"); popup_manager_ = std::make_unique<PopupManager>(this);
manager.Load(); popup_manager_->Initialize();
if (manager.GetRecentFiles().empty()) {
recent_files.emplace_back("No Recent Files", "", nullptr);
} else {
for (const auto &filePath : manager.GetRecentFiles()) {
recent_files.emplace_back(filePath, "",
[&]() { OpenRomOrProject(filePath); });
}
}
std::vector<gui::MenuItem> options_subitems; // Set the popup manager in the context
options_subitems.emplace_back( context_.popup_manager = popup_manager_.get();
"Backup ROM", "", [&]() { backup_rom_ |= backup_rom_; },
[&]() { return backup_rom_; });
options_subitems.emplace_back(
"Save New Auto", "", [&]() { save_new_auto_ |= save_new_auto_; },
[&]() { return save_new_auto_; });
std::vector<gui::MenuItem> project_menu_subitems;
project_menu_subitems.emplace_back("New Project", "",
[&]() { new_project_menu = true; });
project_menu_subitems.emplace_back("Open Project", "",
[&]() { status_ = OpenProject(); });
project_menu_subitems.emplace_back(
"Save Project", "", [&]() { SaveProject(); },
[&]() { return current_project_.project_opened_; });
context_.shortcut_manager.RegisterShortcut( context_.shortcut_manager.RegisterShortcut(
"Open", {ImGuiKey_O, ImGuiMod_Ctrl}, [&]() { LoadRom(); }); "Open", {ImGuiKey_O, ImGuiMod_Ctrl}, [&]() { LoadRom(); });
context_.shortcut_manager.RegisterShortcut( context_.shortcut_manager.RegisterShortcut(
"Save", {ImGuiKey_S, ImGuiMod_Ctrl}, [&]() { SaveRom(); }); "Save", {ImGuiKey_S, ImGuiMod_Ctrl}, [&]() { SaveRom(); });
context_.shortcut_manager.RegisterShortcut( context_.shortcut_manager.RegisterShortcut(
"Close", {ImGuiKey_W, ImGuiMod_Ctrl}, [&]() { rom()->Close(); }); "Close", {ImGuiKey_W, ImGuiMod_Ctrl}, [&]() {
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}, [&]() { quit_ = true; });
@@ -117,6 +116,7 @@ void EditorManager::Initialize(const std::string &filename) {
context_.shortcut_manager.RegisterShortcut( context_.shortcut_manager.RegisterShortcut(
"Load Last ROM", {ImGuiKey_R, ImGuiMod_Ctrl}, [&]() { "Load Last ROM", {ImGuiKey_R, ImGuiMod_Ctrl}, [&]() {
static RecentFilesManager manager("recent_files.txt");
manager.Load(); manager.Load();
if (!manager.GetRecentFiles().empty()) { if (!manager.GetRecentFiles().empty()) {
auto front = manager.GetRecentFiles().front(); auto front = manager.GetRecentFiles().front();
@@ -124,8 +124,38 @@ void EditorManager::Initialize(const std::string &filename) {
} }
}); });
context_.shortcut_manager.RegisterShortcut("F1", ImGuiKey_F1, context_.shortcut_manager.RegisterShortcut(
[&]() { about_ = true; }); "F1", ImGuiKey_F1, [&]() { popup_manager_->Show("About"); });
// Initialize menu items
std::vector<gui::MenuItem> recent_files;
static RecentFilesManager manager("recent_files.txt");
manager.Load();
if (manager.GetRecentFiles().empty()) {
recent_files.emplace_back("No Recent Files", "", nullptr);
} else {
for (const auto &filePath : manager.GetRecentFiles()) {
recent_files.emplace_back(
filePath, "", [filePath, this]() { OpenRomOrProject(filePath); });
}
}
std::vector<gui::MenuItem> options_subitems;
options_subitems.emplace_back(
"Backup ROM", "", [&]() { backup_rom_ |= backup_rom_; },
[&]() { return backup_rom_; });
options_subitems.emplace_back(
"Save New Auto", "", [&]() { save_new_auto_ |= save_new_auto_; },
[&]() { return save_new_auto_; });
std::vector<gui::MenuItem> project_menu_subitems;
project_menu_subitems.emplace_back(
"New Project", "", [&]() { popup_manager_->Show("New Project"); });
project_menu_subitems.emplace_back("Open Project", "",
[&]() { status_ = OpenProject(); });
project_menu_subitems.emplace_back(
"Save Project", "", [&]() { SaveProject(); },
[&]() { return current_project_.project_opened_; });
gui::kMainMenu = { gui::kMainMenu = {
{"File", {"File",
@@ -142,11 +172,13 @@ void EditorManager::Initialize(const std::string &filename) {
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.."), "",
[&]() { save_as_menu_ = true; }}, [&]() { 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"), "",
[&]() { 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},
@@ -184,43 +216,45 @@ void EditorManager::Initialize(const std::string &filename) {
{}, {},
{}, {},
{ {
{absl::StrCat(ICON_MD_HOME, " Home"), "",
[&]() { show_homepage_ = true; }},
{absl::StrCat(ICON_MD_CODE, " Assembly Editor"), "", {absl::StrCat(ICON_MD_CODE, " Assembly Editor"), "",
[&]() { show_asm_editor_ = true; }, [&]() { show_asm_editor_ = true; },
[&]() { return show_asm_editor_; }}, [&]() { return show_asm_editor_; }},
{absl::StrCat(ICON_MD_CASTLE, " Dungeon Editor"), "", {absl::StrCat(ICON_MD_CASTLE, " Dungeon Editor"), "",
[&]() { dungeon_editor_.set_active(true); }, [&]() { current_editor_set_->dungeon_editor_.set_active(true); },
[&]() { return *dungeon_editor_.active(); }}, [&]() { return *current_editor_set_->dungeon_editor_.active(); }},
{absl::StrCat(ICON_MD_PHOTO, " Graphics Editor"), "", {absl::StrCat(ICON_MD_PHOTO, " Graphics Editor"), "",
[&]() { graphics_editor_.set_active(true); }, [&]() { current_editor_set_->graphics_editor_.set_active(true); },
[&]() { return *graphics_editor_.active(); }}, [&]() { return *current_editor_set_->graphics_editor_.active(); }},
{absl::StrCat(ICON_MD_MUSIC_NOTE, " Music Editor"), "", {absl::StrCat(ICON_MD_MUSIC_NOTE, " Music Editor"), "",
[&]() { music_editor_.set_active(true); }, [&]() { current_editor_set_->music_editor_.set_active(true); },
[&]() { return *music_editor_.active(); }}, [&]() { return *current_editor_set_->music_editor_.active(); }},
{absl::StrCat(ICON_MD_LAYERS, " Overworld Editor"), "", {absl::StrCat(ICON_MD_LAYERS, " Overworld Editor"), "",
[&]() { overworld_editor_.set_active(true); }, [&]() { current_editor_set_->overworld_editor_.set_active(true); },
[&]() { return *overworld_editor_.active(); }}, [&]() { return *current_editor_set_->overworld_editor_.active(); }},
{absl::StrCat(ICON_MD_PALETTE, " Palette Editor"), "", {absl::StrCat(ICON_MD_PALETTE, " Palette Editor"), "",
[&]() { palette_editor_.set_active(true); }, [&]() { current_editor_set_->palette_editor_.set_active(true); },
[&]() { return *palette_editor_.active(); }}, [&]() { return *current_editor_set_->palette_editor_.active(); }},
{absl::StrCat(ICON_MD_SCREENSHOT, " Screen Editor"), "", {absl::StrCat(ICON_MD_SCREENSHOT, " Screen Editor"), "",
[&]() { screen_editor_.set_active(true); }, [&]() { current_editor_set_->screen_editor_.set_active(true); },
[&]() { return *screen_editor_.active(); }}, [&]() { return *current_editor_set_->screen_editor_.active(); }},
{absl::StrCat(ICON_MD_SMART_TOY, " Sprite Editor"), "", {absl::StrCat(ICON_MD_SMART_TOY, " Sprite Editor"), "",
[&]() { sprite_editor_.set_active(true); }, [&]() { current_editor_set_->sprite_editor_.set_active(true); },
[&]() { return *sprite_editor_.active(); }}, [&]() { return *current_editor_set_->sprite_editor_.active(); }},
{absl::StrCat(ICON_MD_MESSAGE, " Message Editor"), "", {absl::StrCat(ICON_MD_MESSAGE, " Message Editor"), "",
[&]() { message_editor_.set_active(true); }, [&]() { current_editor_set_->message_editor_.set_active(true); },
[&]() { return *message_editor_.active(); }}, [&]() { return *current_editor_set_->message_editor_.active(); }},
{absl::StrCat(ICON_MD_SETTINGS, " Settings Editor"), "", {absl::StrCat(ICON_MD_SETTINGS, " Settings Editor"), "",
[&]() { settings_editor_.set_active(true); }, [&]() { current_editor_set_->settings_editor_.set_active(true); },
[&]() { return *settings_editor_.active(); }}, [&]() { return *current_editor_set_->settings_editor_.active(); }},
{gui::kSeparator, "", nullptr, []() { return true; }}, {gui::kSeparator, "", nullptr, []() { return true; }},
{absl::StrCat(ICON_MD_GAMEPAD, " Emulator"), "", {absl::StrCat(ICON_MD_GAMEPAD, " Emulator"), "",
[&]() { show_emulator_ = true; }}, [&]() { show_emulator_ = true; }},
{absl::StrCat(ICON_MD_MEMORY, " Memory Editor"), "", {absl::StrCat(ICON_MD_MEMORY, " Memory Editor"), "",
[&]() { show_memory_editor_ = true; }}, [&]() { show_memory_editor_ = true; }},
{absl::StrCat(ICON_MD_SIM_CARD, " ROM Metadata"), "", {absl::StrCat(ICON_MD_SIM_CARD, " ROM Metadata"), "",
[&]() { rom_info_ = true; }}, [&]() { popup_manager_->Show("ROM Information"); }},
{gui::kSeparator, "", nullptr, []() { return true; }}, {gui::kSeparator, "", nullptr, []() { return true; }},
{absl::StrCat(ICON_MD_HELP, " ImGui Demo"), "", {absl::StrCat(ICON_MD_HELP, " ImGui Demo"), "",
[&]() { show_imgui_demo_ = true; }}, [&]() { show_imgui_demo_ = true; }},
@@ -241,41 +275,38 @@ void EditorManager::Initialize(const std::string &filename) {
{}, {},
{ {
{absl::StrCat(ICON_MD_HELP, " How to open a ROM"), "", {absl::StrCat(ICON_MD_HELP, " How to open a ROM"), "",
[&]() { open_rom_help = true; }}, [&]() { popup_manager_->Show("Open a ROM"); }},
{absl::StrCat(ICON_MD_HELP, " Supported Features"), "", {absl::StrCat(ICON_MD_HELP, " Supported Features"), "",
[&]() { open_supported_features = true; }}, [&]() { popup_manager_->Show("Supported Features"); }},
{absl::StrCat(ICON_MD_HELP, " How to manage a project"), "", {absl::StrCat(ICON_MD_HELP, " How to manage a project"), "",
[&]() { open_manage_project = true; }}, [&]() { popup_manager_->Show("Manage Project"); }},
{absl::StrCat(ICON_MD_HELP, " About"), "F1", {absl::StrCat(ICON_MD_HELP, " About"), "F1",
[&]() { about_ = true; }}, [&]() { popup_manager_->Show("About"); }},
}}}; }}};
overworld_editor_.Initialize();
} }
absl::Status EditorManager::Update() { absl::Status EditorManager::Update() {
DrawPopups(); popup_manager_->DrawPopups();
ExecuteShortcuts(context_.shortcut_manager); ExecuteShortcuts(context_.shortcut_manager);
for (auto editor : active_editors_) { if (current_editor_set_) {
if (*editor->active()) { for (auto editor : current_editor_set_->active_editors_) {
if (ImGui::Begin(GetEditorName(editor->type()).c_str(), if (*editor->active()) {
editor->active())) { if (ImGui::Begin(GetEditorName(editor->type()).c_str(),
current_editor_ = editor; editor->active())) {
status_ = editor->Update(); current_editor_ = editor;
status_ = editor->Update();
}
ImGui::End();
} }
ImGui::End();
} }
} }
static bool show_home = true; if (show_homepage_) {
ImGui::Begin("Home", &show_home); ImGui::Begin("Home", &show_homepage_);
if (!current_rom_) {
DrawHomepage(); DrawHomepage();
} else { ImGui::End();
ManageActiveEditors();
} }
ImGui::End();
return absl::OkStatus(); return absl::OkStatus();
} }
@@ -288,8 +319,13 @@ void EditorManager::DrawHomepage() {
"The editor is still in development, so please report any bugs or issues " "The editor is still in development, so please report any bugs or issues "
"you encounter."); "you encounter.");
if (gui::ClickableText("Open a ROM")) { if (current_rom_) {
LoadRom(); TextWrapped("Current ROM: %s", current_rom_->filename().c_str());
} else {
TextWrapped("No ROM loaded.");
if (gui::ClickableText("Open a ROM")) {
status_ = LoadRom();
}
} }
} }
@@ -306,6 +342,8 @@ void EditorManager::DrawMenuBar() {
if (BeginMenuBar()) { if (BeginMenuBar()) {
gui::DrawMenu(gui::kMainMenu); gui::DrawMenu(gui::kMainMenu);
DrawRomSelector(*this);
SameLine(GetWindowWidth() - GetStyle().ItemSpacing.x - SameLine(GetWindowWidth() - GetStyle().ItemSpacing.x -
CalcTextSize(ICON_MD_DISPLAY_SETTINGS).x - 110); CalcTextSize(ICON_MD_DISPLAY_SETTINGS).x - 110);
PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
@@ -325,8 +363,12 @@ void EditorManager::DrawMenuBar() {
if (show_imgui_demo_) ShowDemoWindow(); if (show_imgui_demo_) ShowDemoWindow();
if (show_imgui_metrics_) ShowMetricsWindow(&show_imgui_metrics_); if (show_imgui_metrics_) ShowMetricsWindow(&show_imgui_metrics_);
if (show_memory_editor_) memory_editor_.Update(show_memory_editor_); if (show_memory_editor_ && current_editor_set_) {
if (show_asm_editor_) assembly_editor_.Update(show_asm_editor_); current_editor_set_->memory_editor_.Update(show_memory_editor_);
}
if (show_asm_editor_ && current_editor_set_) {
current_editor_set_->assembly_editor_.Update(show_asm_editor_);
}
if (show_emulator_) { if (show_emulator_) {
Begin("Emulator", &show_emulator_, ImGuiWindowFlags_MenuBar); Begin("Emulator", &show_emulator_, ImGuiWindowFlags_MenuBar);
@@ -334,86 +376,18 @@ void EditorManager::DrawMenuBar() {
End(); End();
} }
if (show_palette_editor_) { if (show_palette_editor_ && current_editor_set_) {
Begin("Palette Editor", &show_palette_editor_); Begin("Palette Editor", &show_palette_editor_);
status_ = palette_editor_.Update(); status_ = current_editor_set_->palette_editor_.Update();
End(); End();
} }
if (open_supported_features) OpenPopup("Supported Features"); if (show_resource_label_manager && current_rom_) {
if (BeginPopupModal("Supported Features", nullptr, current_rom_->resource_label()->DisplayLabels(&show_resource_label_manager);
ImGuiWindowFlags_AlwaysAutoResize)) {
Text("Overworld");
BulletText("LW/DW/SW Tilemap Editing");
BulletText("LW/DW/SW Map Properties");
BulletText("Create/Delete/Update Entrances");
BulletText("Create/Delete/Update Exits");
BulletText("Create/Delete/Update Sprites");
BulletText("Create/Delete/Update Items");
Text("Dungeon");
BulletText("View Room Header Properties");
BulletText("View Entrance Properties");
Text("Graphics");
BulletText("View Decompressed Graphics Sheets");
BulletText("View/Update Graphics Groups");
Text("Palettes");
BulletText("View Palette Groups");
Text("Saveable");
BulletText("All Listed Overworld Features");
BulletText("Hex Editor Changes");
if (Button("Close", gui::kDefaultModalSize)) {
open_supported_features = false;
CloseCurrentPopup();
}
EndPopup();
}
if (open_rom_help) OpenPopup("Open a ROM");
if (BeginPopupModal("Open a ROM", nullptr,
ImGuiWindowFlags_AlwaysAutoResize)) {
Text("File -> Open");
Text("Select a ROM file to open");
Text("Supported ROMs (headered or unheadered):");
Text("The Legend of Zelda: A Link to the Past");
Text("US Version 1.0");
Text("JP Version 1.0");
if (Button("Close", gui::kDefaultModalSize)) {
open_rom_help = false;
CloseCurrentPopup();
}
EndPopup();
}
if (open_manage_project) OpenPopup("Manage Project");
if (BeginPopupModal("Manage Project", nullptr,
ImGuiWindowFlags_AlwaysAutoResize)) {
Text("Project Menu");
Text("Create a new project or open an existing one.");
Text("Save the project to save the current state of the project.");
TextWrapped(
"To save a project, you need to first open a ROM and initialize your "
"code path and labels file. Label resource manager can be found in "
"the View menu. Code path is set in the Code editor after opening a "
"folder.");
if (Button("Close", gui::kDefaultModalSize)) {
open_manage_project = false;
CloseCurrentPopup();
}
EndPopup();
}
if (show_resource_label_manager) {
rom()->resource_label()->DisplayLabels(&show_resource_label_manager);
if (current_project_.project_opened_ && if (current_project_.project_opened_ &&
!current_project_.labels_filename_.empty()) { !current_project_.labels_filename_.empty()) {
current_project_.labels_filename_ = rom()->resource_label()->filename_; current_project_.labels_filename_ =
current_rom_->resource_label()->filename_;
} }
} }
@@ -422,7 +396,7 @@ void EditorManager::DrawMenuBar() {
Begin("Save As..", &save_as_menu, ImGuiWindowFlags_AlwaysAutoResize); Begin("Save As..", &save_as_menu, ImGuiWindowFlags_AlwaysAutoResize);
InputText("Filename", &save_as_filename); InputText("Filename", &save_as_filename);
if (Button("Save", gui::kDefaultModalSize)) { if (Button("Save", gui::kDefaultModalSize)) {
SaveRom(); status_ = SaveRom();
save_as_menu = false; save_as_menu = false;
} }
SameLine(); SameLine();
@@ -474,88 +448,156 @@ void EditorManager::DrawMenuBar() {
} }
} }
void EditorManager::LoadRom() { absl::Status EditorManager::LoadRom() {
auto file_name = FileDialogWrapper::ShowOpenFileDialog(); auto file_name = FileDialogWrapper::ShowOpenFileDialog();
auto load_rom = rom()->LoadFromFile(file_name); auto new_rom = std::make_unique<Rom>();
if (load_rom.ok()) { RETURN_IF_ERROR(new_rom->LoadFromFile(file_name));
current_rom_ = rom();
static RecentFilesManager manager("recent_files.txt"); current_rom_ = new_rom.get();
manager.Load(); roms_.push_back(std::move(new_rom));
manager.AddFile(file_name);
manager.Save(); // Create new editor set for this ROM
LoadAssets(); auto editor_set = std::make_unique<EditorSet>(current_rom_);
for (auto *editor : editor_set->active_editors_) {
editor->set_context(&context_);
} }
current_editor_set_ = editor_set.get();
editor_sets_[current_rom_] = std::move(editor_set);
static RecentFilesManager manager("recent_files.txt");
manager.Load();
manager.AddFile(file_name);
manager.Save();
RETURN_IF_ERROR(LoadAssets());
// Show ROM info popup after loading
popup_manager_->Show("ROM Information");
return absl::OkStatus();
} }
void EditorManager::LoadAssets() { absl::Status EditorManager::LoadAssets() {
auto load_rom_assets = [&]() -> absl::Status { if (!current_rom_ || !current_editor_set_) {
auto &sheet_manager = GraphicsSheetManager::GetInstance(); return absl::FailedPreconditionError("No ROM or editor set loaded");
ASSIGN_OR_RETURN(*sheet_manager.mutable_gfx_sheets(),
LoadAllGraphicsData(*rom()))
RETURN_IF_ERROR(overworld_editor_.Load());
RETURN_IF_ERROR(dungeon_editor_.Load());
return absl::OkStatus();
};
if (!load_rom_assets().ok()) {
status_ = load_rom_assets();
} }
current_editor_set_->overworld_editor_.Initialize();
auto &sheet_manager = GraphicsSheetManager::GetInstance();
ASSIGN_OR_RETURN(*sheet_manager.mutable_gfx_sheets(),
LoadAllGraphicsData(*current_rom_));
RETURN_IF_ERROR(current_editor_set_->overworld_editor_.Load());
RETURN_IF_ERROR(current_editor_set_->dungeon_editor_.Load());
return absl::OkStatus();
} }
void EditorManager::SaveRom() { absl::Status EditorManager::SaveRom() {
if (!current_rom_ || !current_editor_set_) {
return absl::FailedPreconditionError("No ROM or editor set loaded");
}
if (core::FeatureFlags::get().kSaveDungeonMaps) { if (core::FeatureFlags::get().kSaveDungeonMaps) {
status_ = screen_editor_.SaveDungeonMaps(); RETURN_IF_ERROR(current_editor_set_->screen_editor_.SaveDungeonMaps());
RETURN_VOID_IF_ERROR(status_);
} }
status_ = overworld_editor_.Save(); RETURN_IF_ERROR(current_editor_set_->overworld_editor_.Save());
RETURN_VOID_IF_ERROR(status_);
if (core::FeatureFlags::get().kSaveGraphicsSheet) if (core::FeatureFlags::get().kSaveGraphicsSheet)
PRINT_IF_ERROR(SaveAllGraphicsData( RETURN_IF_ERROR(SaveAllGraphicsData(
*rom(), GraphicsSheetManager::GetInstance().gfx_sheets())); *current_rom_, GraphicsSheetManager::GetInstance().gfx_sheets()));
status_ = rom()->SaveToFile(backup_rom_, save_new_auto_); return current_rom_->SaveToFile(backup_rom_, save_new_auto_);
} }
void EditorManager::OpenRomOrProject(const std::string &filename) { absl::Status EditorManager::OpenRomOrProject(const std::string &filename) {
if (absl::StrContains(filename, ".yaze")) { if (absl::StrContains(filename, ".yaze")) {
status_ = current_project_.Open(filename); RETURN_IF_ERROR(current_project_.Open(filename));
if (status_.ok()) { RETURN_IF_ERROR(OpenProject());
status_ = OpenProject();
}
} else { } else {
status_ = rom()->LoadFromFile(filename); auto new_rom = std::make_unique<Rom>();
current_rom_ = rom(); RETURN_IF_ERROR(new_rom->LoadFromFile(filename));
LoadAssets(); current_rom_ = new_rom.get();
SharedRom::set_rom(current_rom_);
roms_.push_back(std::move(new_rom));
// Create new editor set for this ROM
auto editor_set = std::make_unique<EditorSet>(current_rom_);
for (auto *editor : editor_set->active_editors_) {
editor->set_context(&context_);
}
current_editor_set_ = editor_set.get();
editor_sets_[current_rom_] = std::move(editor_set);
RETURN_IF_ERROR(LoadAssets());
} }
return absl::OkStatus();
} }
absl::Status EditorManager::OpenProject() { absl::Status EditorManager::OpenProject() {
RETURN_IF_ERROR(rom()->LoadFromFile(current_project_.rom_filename_)); auto new_rom = std::make_unique<Rom>();
current_rom_ = rom(); RETURN_IF_ERROR(new_rom->LoadFromFile(current_project_.rom_filename_));
current_rom_ = new_rom.get();
roms_.push_back(std::move(new_rom));
if (!rom()->resource_label()->LoadLabels(current_project_.labels_filename_)) { if (!current_rom_->resource_label()->LoadLabels(
current_project_.labels_filename_)) {
return absl::InternalError( return absl::InternalError(
"Could not load labels file, update your project file."); "Could not load labels file, update your project file.");
} }
// Create new editor set for this ROM
auto editor_set = std::make_unique<EditorSet>(current_rom_);
for (auto *editor : editor_set->active_editors_) {
editor->set_context(&context_);
}
current_editor_set_ = editor_set.get();
editor_sets_[current_rom_] = std::move(editor_set);
static RecentFilesManager manager("recent_files.txt"); static RecentFilesManager manager("recent_files.txt");
manager.Load(); manager.Load();
manager.AddFile(current_project_.filepath + "/" + current_project_.name + manager.AddFile(current_project_.filepath + "/" + current_project_.name +
".yaze"); ".yaze");
manager.Save(); manager.Save();
assembly_editor_.OpenFolder(current_project_.code_folder_); if (current_editor_set_) {
current_editor_set_->assembly_editor_.OpenFolder(
current_project_.code_folder_);
}
current_project_.project_opened_ = true; current_project_.project_opened_ = true;
LoadAssets(); RETURN_IF_ERROR(LoadAssets());
return absl::OkStatus(); return absl::OkStatus();
} }
void EditorManager::SaveProject() { absl::Status EditorManager::SaveProject() {
if (current_project_.project_opened_) { if (current_project_.project_opened_) {
status_ = current_project_.Save(); RETURN_IF_ERROR(current_project_.Save());
} else { } else {
new_project_menu = true; new_project_menu = true;
} }
return absl::OkStatus();
}
absl::Status EditorManager::SetCurrentRom(Rom *rom) {
if (!rom) {
return absl::InvalidArgumentError("Invalid ROM pointer");
}
auto it = editor_sets_.find(rom);
if (it == editor_sets_.end()) {
// Create new editor set if it doesn't exist
auto editor_set = std::make_unique<EditorSet>(rom);
for (auto *editor : editor_set->active_editors_) {
editor->set_context(&context_);
}
current_editor_set_ = editor_set.get();
editor_sets_[rom] = std::move(editor_set);
current_rom_ = rom;
RETURN_IF_ERROR(LoadAssets());
} else {
current_editor_set_ = it->second.get();
current_rom_ = rom;
SharedRom::set_rom(rom);
}
return absl::OkStatus();
} }
} // namespace editor } // namespace editor

View File

@@ -3,6 +3,8 @@
#define IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS
#include <unordered_map>
#include "absl/status/status.h" #include "absl/status/status.h"
#include "app/core/project.h" #include "app/core/project.h"
#include "app/editor/code/assembly_editor.h" #include "app/editor/code/assembly_editor.h"
@@ -25,6 +27,9 @@
namespace yaze { namespace yaze {
namespace editor { namespace editor {
// Forward declaration
class EditorSet;
/** /**
* @class EditorManager * @class EditorManager
* @brief The EditorManager controls the main editor window and manages the * @brief The EditorManager controls the main editor window and manages the
@@ -36,17 +41,9 @@ namespace editor {
* variable points to the currently active editor in the tab view. * variable points to the currently active editor in the tab view.
* *
*/ */
class EditorManager : public SharedRom { class EditorManager {
public: public:
EditorManager() { EditorManager() {
current_editor_ = &overworld_editor_;
active_editors_ = {&overworld_editor_, &dungeon_editor_, &graphics_editor_,
&palette_editor_, &sprite_editor_, &message_editor_,
&music_editor_, &screen_editor_, &settings_editor_,
&assembly_editor_};
for (auto *editor : active_editors_) {
editor->set_context(&context_);
}
std::stringstream ss; std::stringstream ss;
ss << YAZE_VERSION_MAJOR << "." << YAZE_VERSION_MINOR << "." ss << YAZE_VERSION_MAJOR << "." << YAZE_VERSION_MINOR << "."
<< YAZE_VERSION_PATCH; << YAZE_VERSION_PATCH;
@@ -60,28 +57,27 @@ class EditorManager : public SharedRom {
auto emulator() -> emu::Emulator & { return emulator_; } auto emulator() -> emu::Emulator & { return emulator_; }
auto quit() const { return quit_; } auto quit() const { return quit_; }
auto version() const { return version_; }
absl::Status SetCurrentRom(Rom *rom);
auto GetRoms() -> std::vector<std::unique_ptr<Rom>> & { return roms_; }
auto GetCurrentRom() -> Rom * { return current_rom_; }
auto GetCurrentEditorSet() -> EditorSet* { return current_editor_set_; }
private: private:
void ManageActiveEditors();
void DrawHomepage(); void DrawHomepage();
void DrawPopups(); void DrawPopups();
void LoadRom(); absl::Status LoadRom();
void LoadAssets(); absl::Status LoadAssets();
void SaveRom(); absl::Status SaveRom();
void OpenRomOrProject(const std::string &filename); absl::Status OpenRomOrProject(const std::string &filename);
absl::Status OpenProject(); absl::Status OpenProject();
void SaveProject(); absl::Status SaveProject();
bool quit_ = false; bool quit_ = false;
bool about_ = false;
bool rom_info_ = false;
bool backup_rom_ = false; bool backup_rom_ = false;
bool save_as_menu_ = false;
bool save_new_auto_ = true; bool save_new_auto_ = true;
bool open_rom_help = false;
bool open_manage_project = false;
bool open_supported_features = false;
bool new_project_menu = false; bool new_project_menu = false;
bool show_emulator_ = false; bool show_emulator_ = false;
@@ -92,20 +88,62 @@ class EditorManager : public SharedRom {
bool show_palette_editor_ = false; bool show_palette_editor_ = false;
bool show_resource_label_manager = false; bool show_resource_label_manager = false;
bool show_workspace_layout = false; bool show_workspace_layout = false;
bool show_homepage_ = true;
std::string version_ = ""; std::string version_ = "";
absl::Status status_; absl::Status status_;
emu::Emulator emulator_; emu::Emulator emulator_;
std::vector<Editor *> active_editors_;
std::vector<std::unique_ptr<Rom>> roms_; std::vector<std::unique_ptr<Rom>> roms_;
Rom *current_rom_ = nullptr; std::unordered_map<Rom*, std::unique_ptr<EditorSet>> editor_sets_;
Rom* current_rom_ = nullptr;
EditorSet* current_editor_set_ = nullptr;
Editor* current_editor_ = nullptr;
Project current_project_; Project current_project_;
EditorContext context_; EditorContext context_;
std::unique_ptr<PopupManager> popup_manager_; std::unique_ptr<PopupManager> popup_manager_;
}; };
/**
* @class EditorSet
* @brief Contains a complete set of editors for a single ROM instance
*/
class EditorSet {
public:
explicit EditorSet(Rom* rom)
: assembly_editor_(),
dungeon_editor_(),
graphics_editor_(),
music_editor_(),
overworld_editor_(*rom),
palette_editor_(),
screen_editor_(),
sprite_editor_(),
settings_editor_(),
message_editor_(),
memory_editor_() {
active_editors_ = {&overworld_editor_, &dungeon_editor_, &graphics_editor_,
&palette_editor_, &sprite_editor_, &message_editor_,
&music_editor_, &screen_editor_, &settings_editor_,
&assembly_editor_};
}
AssemblyEditor assembly_editor_;
DungeonEditor dungeon_editor_;
GraphicsEditor graphics_editor_;
MusicEditor music_editor_;
OverworldEditor overworld_editor_;
PaletteEditor palette_editor_;
ScreenEditor screen_editor_;
SpriteEditor sprite_editor_;
SettingsEditor settings_editor_;
MessageEditor message_editor_;
MemoryEditorWithDiffChecker memory_editor_;
std::vector<Editor*> active_editors_;
};
} // namespace editor } // namespace editor
} // namespace yaze } // namespace yaze