Remove dynamic layout code

This commit is contained in:
scawful
2024-12-29 09:58:34 -05:00
parent bfadd435a0
commit a8dcfe3d05
4 changed files with 112 additions and 256 deletions

View File

@@ -1,51 +1,7 @@
#include "editor.h"
#include "app/core/constants.h"
#include "imgui/imgui.h"
namespace yaze {
namespace editor {
absl::Status DrawEditor(EditorLayoutParams *params) {
if (params->editor == nullptr) {
return absl::InternalError("Editor is not initialized");
}
// Draw the editors based on h_split and v_split in a recursive manner
if (params->v_split) {
ImGui::BeginTable("##VerticalSplitTable", 2);
ImGui::TableNextColumn();
if (params->left)
RETURN_IF_ERROR(DrawEditor(params->left));
ImGui::TableNextColumn();
if (params->right)
RETURN_IF_ERROR(DrawEditor(params->right));
ImGui::EndTable();
} else if (params->h_split) {
ImGui::BeginTable("##HorizontalSplitTable", 1);
ImGui::TableNextColumn();
if (params->top)
RETURN_IF_ERROR(DrawEditor(params->top));
ImGui::TableNextColumn();
if (params->bottom)
RETURN_IF_ERROR(DrawEditor(params->bottom));
ImGui::EndTable();
} else {
// No split, just draw the single editor
ImGui::Text("%s Editor",
kEditorNames[static_cast<int>(params->editor->type())]);
RETURN_IF_ERROR(params->editor->Update());
}
return absl::OkStatus();
}
} // namespace editor
} // namespace yaze

View File

@@ -73,30 +73,6 @@ class Editor {
EditorContext context_;
};
/**
* @brief Dynamic Editor Layout Parameters
*/
typedef struct EditorLayoutParams {
bool v_split;
bool h_split;
int v_split_pos;
int h_split_pos;
Editor *editor = nullptr;
EditorLayoutParams *left = nullptr;
EditorLayoutParams *right = nullptr;
EditorLayoutParams *top = nullptr;
EditorLayoutParams *bottom = nullptr;
EditorLayoutParams() {
v_split = false;
h_split = false;
v_split_pos = 0;
h_split_pos = 0;
}
} EditorLayoutParams;
absl::Status DrawEditor(EditorLayoutParams *params);
} // namespace editor
} // namespace yaze

View File

@@ -31,8 +31,8 @@ using core::FileDialogWrapper;
namespace {
bool BeginCentered(const char* name) {
ImGuiIO const& io = GetIO();
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 =
@@ -41,12 +41,12 @@ bool BeginCentered(const char* name) {
return Begin(name, nullptr, flags);
}
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) !=
active_editors.end();
}
} // namespace
} // namespace
void EditorManager::SetupScreen(std::string filename) {
if (!filename.empty()) {
@@ -65,17 +65,12 @@ absl::Status EditorManager::Update() {
DrawInfoPopup();
if (rom()->is_loaded() && !rom_assets_loaded_) {
// Load all of the graphics data from the game.
RETURN_IF_ERROR(rom()->LoadAllGraphicsData())
// Initialize overworld graphics, maps, and palettes
RETURN_IF_ERROR(overworld_editor_.LoadGraphics());
rom_assets_loaded_ = true;
}
if (dynamic_layout_)
RETURN_IF_ERROR(DrawDynamicLayout())
else
ManageActiveEditors();
ManageActiveEditors();
return absl::OkStatus();
}
@@ -83,7 +78,8 @@ absl::Status EditorManager::Update() {
void EditorManager::ManageActiveEditors() {
// Show popup pane to select an editor to add
static bool show_add_editor = false;
if (show_add_editor) OpenPopup("AddEditor");
if (show_add_editor)
OpenPopup("AddEditor");
if (BeginPopup("AddEditor", ImGuiWindowFlags_AlwaysAutoResize)) {
if (MenuItem("Overworld", nullptr, false,
@@ -148,85 +144,85 @@ void EditorManager::ManageActiveEditors() {
for (auto editor : active_editors_) {
bool open = true;
switch (editor->type()) {
case EditorType::kOverworld:
if (overworld_editor_.jump_to_tab() == -1) {
if (BeginTabItem("Overworld", &open)) {
current_editor_ = &overworld_editor_;
status_ = overworld_editor_.Update();
EndTabItem();
}
}
break;
case EditorType::kDungeon:
if (BeginTabItem("Dungeon", &open)) {
current_editor_ = &dungeon_editor_;
status_ = dungeon_editor_.Update();
if (overworld_editor_.jump_to_tab() != -1) {
dungeon_editor_.add_room(overworld_editor_.jump_to_tab());
overworld_editor_.jump_to_tab_ = -1;
}
case EditorType::kOverworld:
if (overworld_editor_.jump_to_tab() == -1) {
if (BeginTabItem("Overworld", &open)) {
current_editor_ = &overworld_editor_;
status_ = overworld_editor_.Update();
EndTabItem();
}
break;
case EditorType::kGraphics:
if (BeginTabItem("Graphics", &open)) {
current_editor_ = &graphics_editor_;
status_ = graphics_editor_.Update();
EndTabItem();
}
break;
case EditorType::kDungeon:
if (BeginTabItem("Dungeon", &open)) {
current_editor_ = &dungeon_editor_;
status_ = dungeon_editor_.Update();
if (overworld_editor_.jump_to_tab() != -1) {
dungeon_editor_.add_room(overworld_editor_.jump_to_tab());
overworld_editor_.jump_to_tab_ = -1;
}
break;
case EditorType::kMusic:
if (BeginTabItem("Music", &open)) {
current_editor_ = &music_editor_;
EndTabItem();
}
break;
case EditorType::kGraphics:
if (BeginTabItem("Graphics", &open)) {
current_editor_ = &graphics_editor_;
status_ = graphics_editor_.Update();
EndTabItem();
}
break;
case EditorType::kMusic:
if (BeginTabItem("Music", &open)) {
current_editor_ = &music_editor_;
status_ = music_editor_.Update();
EndTabItem();
}
break;
case EditorType::kPalette:
if (BeginTabItem("Palette", &open)) {
current_editor_ = &palette_editor_;
status_ = palette_editor_.Update();
EndTabItem();
}
break;
case EditorType::kScreen:
if (BeginTabItem("Screen", &open)) {
current_editor_ = &screen_editor_;
status_ = screen_editor_.Update();
EndTabItem();
}
break;
case EditorType::kSprite:
if (BeginTabItem("Sprite", &open)) {
current_editor_ = &sprite_editor_;
status_ = sprite_editor_.Update();
EndTabItem();
}
break;
case EditorType::kAssembly:
if (BeginTabItem("Code", &open)) {
current_editor_ = &assembly_editor_;
assembly_editor_.UpdateCodeView();
EndTabItem();
}
break;
case EditorType::kSettings:
if (BeginTabItem("Settings", &open)) {
current_editor_ = &settings_editor_;
status_ = settings_editor_.Update();
EndTabItem();
}
break;
case EditorType::kMessage:
if (BeginTabItem("Message", &open)) {
current_editor_ = &message_editor_;
status_ = message_editor_.Update();
EndTabItem();
}
break;
default:
break;
status_ = music_editor_.Update();
EndTabItem();
}
break;
case EditorType::kPalette:
if (BeginTabItem("Palette", &open)) {
current_editor_ = &palette_editor_;
status_ = palette_editor_.Update();
EndTabItem();
}
break;
case EditorType::kScreen:
if (BeginTabItem("Screen", &open)) {
current_editor_ = &screen_editor_;
status_ = screen_editor_.Update();
EndTabItem();
}
break;
case EditorType::kSprite:
if (BeginTabItem("Sprite", &open)) {
current_editor_ = &sprite_editor_;
status_ = sprite_editor_.Update();
EndTabItem();
}
break;
case EditorType::kAssembly:
if (BeginTabItem("Code", &open)) {
current_editor_ = &assembly_editor_;
assembly_editor_.UpdateCodeView();
EndTabItem();
}
break;
case EditorType::kSettings:
if (BeginTabItem("Settings", &open)) {
current_editor_ = &settings_editor_;
status_ = settings_editor_.Update();
EndTabItem();
}
break;
case EditorType::kMessage:
if (BeginTabItem("Message", &open)) {
current_editor_ = &message_editor_;
status_ = message_editor_.Update();
EndTabItem();
}
break;
default:
break;
}
if (!open) {
active_editors_.erase(
@@ -243,12 +239,6 @@ void EditorManager::ManageActiveEditors() {
}
}
absl::Status EditorManager::DrawDynamicLayout() {
// Dynamic layout for multiple editors to be open at once
// Allows for tiling and resizing of editors using ImGui
return DrawEditor(&root_layout_);
}
void EditorManager::ManageKeyboardShortcuts() {
bool ctrl_or_super = (GetIO().KeyCtrl || GetIO().KeySuper);
@@ -309,78 +299,8 @@ void EditorManager::ManageKeyboardShortcuts() {
}
void EditorManager::InitializeCommands() {
if (root_layout_.editor == nullptr) {
root_layout_.editor = &overworld_editor_;
}
// New editor popup for window management commands
static EditorLayoutParams new_layout;
if (ImGui::BeginPopup("NewEditor")) {
ImGui::Text("New Editor");
ImGui::Separator();
if (ImGui::Button("Overworld")) {
new_layout.editor = &overworld_editor_;
ImGui::CloseCurrentPopup();
}
if (ImGui::Button("Dungeon")) {
new_layout.editor = &dungeon_editor_;
ImGui::CloseCurrentPopup();
}
if (ImGui::Button("Graphics")) {
new_layout.editor = &graphics_editor_;
ImGui::CloseCurrentPopup();
}
if (ImGui::Button("Music")) {
new_layout.editor = &music_editor_;
ImGui::CloseCurrentPopup();
}
if (ImGui::Button("Palette")) {
new_layout.editor = &palette_editor_;
ImGui::CloseCurrentPopup();
}
if (ImGui::Button("Screen")) {
new_layout.editor = &screen_editor_;
ImGui::CloseCurrentPopup();
}
if (ImGui::Button("Sprite")) {
new_layout.editor = &sprite_editor_;
ImGui::CloseCurrentPopup();
}
if (ImGui::Button("Code")) {
new_layout.editor = &assembly_editor_;
ImGui::CloseCurrentPopup();
}
if (ImGui::Button("Settings")) {
new_layout.editor = &settings_editor_;
ImGui::CloseCurrentPopup();
}
if (ImGui::Button("Message")) {
new_layout.editor = &message_editor_;
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
editor_context_.command_manager.RegisterPrefix("window", 'w',
"window management", "");
editor_context_.command_manager.RegisterSubcommand(
"window", "vsplit", '/', "vertical split",
"split windows vertically and place editor in new window", [this]() {
ImGui::OpenPopup("NewEditor");
root_layout_.v_split = true;
});
editor_context_.command_manager.RegisterSubcommand(
"window", "hsplit", '-', "horizontal split",
"split windows horizontally and place editor in new window", [this]() {
ImGui::OpenPopup("NewEditor");
root_layout_.h_split = true;
});
editor_context_.command_manager.RegisterSubcommand(
"window", "close", 'd', "close", "close the current editor", [this]() {
if (root_layout_.editor != nullptr) {
root_layout_.editor = nullptr;
}
});
}
void EditorManager::DrawStatusPopup() {
@@ -412,7 +332,8 @@ void EditorManager::DrawStatusPopup() {
}
void EditorManager::DrawAboutPopup() {
if (about_) OpenPopup("About");
if (about_)
OpenPopup("About");
if (BeginPopupModal("About", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
Text("Yet Another Zelda3 Editor - v%s", core::kYazeVersion.data());
Text("Written by: scawful");
@@ -429,7 +350,8 @@ void EditorManager::DrawAboutPopup() {
}
void EditorManager::DrawInfoPopup() {
if (rom_info_) OpenPopup("ROM Information");
if (rom_info_)
OpenPopup("ROM Information");
if (BeginPopupModal("ROM Information", nullptr,
ImGuiWindowFlags_AlwaysAutoResize)) {
Text("Title: %s", rom()->title().c_str());
@@ -482,7 +404,7 @@ void EditorManager::DrawYazeMenuBar() {
if (manager.GetRecentFiles().empty()) {
MenuItem("No Recent Files", nullptr, false, false);
} else {
for (const auto& filePath : manager.GetRecentFiles()) {
for (const auto &filePath : manager.GetRecentFiles()) {
if (MenuItem(filePath.c_str())) {
OpenRomOrProject(filePath);
}
@@ -624,10 +546,14 @@ void EditorManager::DrawYazeMenuBar() {
static bool show_palette_editor = false;
static bool show_emulator = false;
if (show_imgui_demo) ShowDemoWindow();
if (show_imgui_metrics) ShowMetricsWindow(&show_imgui_metrics);
if (show_memory_editor) memory_editor_.Update(show_memory_editor);
if (show_asm_editor) assembly_editor_.Update(show_asm_editor);
if (show_imgui_demo)
ShowDemoWindow();
if (show_imgui_metrics)
ShowMetricsWindow(&show_imgui_metrics);
if (show_memory_editor)
memory_editor_.Update(show_memory_editor);
if (show_asm_editor)
assembly_editor_.Update(show_asm_editor);
if (show_emulator) {
Begin("Emulator", &show_emulator, ImGuiWindowFlags_MenuBar);
@@ -642,7 +568,6 @@ void EditorManager::DrawYazeMenuBar() {
}
if (BeginMenu("View")) {
MenuItem("Dynamic Layout", nullptr, &dynamic_layout_);
MenuItem("Emulator", nullptr, &show_emulator);
Separator();
MenuItem("Memory Editor", nullptr, &show_memory_editor);
@@ -673,15 +598,20 @@ void EditorManager::DrawYazeMenuBar() {
static bool open_supported_features = false;
static bool open_manage_project = false;
if (BeginMenu("Help")) {
if (MenuItem("How to open a ROM")) open_rom_help = true;
if (MenuItem("Supported Features")) open_supported_features = true;
if (MenuItem("How to manage a project")) open_manage_project = true;
if (MenuItem("How to open a ROM"))
open_rom_help = true;
if (MenuItem("Supported Features"))
open_supported_features = true;
if (MenuItem("How to manage a project"))
open_manage_project = true;
if (MenuItem("About", "F1")) about_ = true;
if (MenuItem("About", "F1"))
about_ = true;
EndMenu();
}
if (open_supported_features) OpenPopup("Supported Features");
if (open_supported_features)
OpenPopup("Supported Features");
if (BeginPopupModal("Supported Features", nullptr,
ImGuiWindowFlags_AlwaysAutoResize)) {
Text("Overworld");
@@ -714,7 +644,8 @@ void EditorManager::DrawYazeMenuBar() {
EndPopup();
}
if (open_rom_help) OpenPopup("Open a ROM");
if (open_rom_help)
OpenPopup("Open a ROM");
if (BeginPopupModal("Open a ROM", nullptr,
ImGuiWindowFlags_AlwaysAutoResize)) {
Text("File -> Open");
@@ -731,7 +662,8 @@ void EditorManager::DrawYazeMenuBar() {
EndPopup();
}
if (open_manage_project) OpenPopup("Manage Project");
if (open_manage_project)
OpenPopup("Manage Project");
if (BeginPopupModal("Manage Project", nullptr,
ImGuiWindowFlags_AlwaysAutoResize)) {
Text("Project Menu");
@@ -782,7 +714,7 @@ void EditorManager::SaveRom() {
status_ = rom()->SaveToFile(backup_rom_, save_new_auto_);
}
void EditorManager::OpenRomOrProject(const std::string& filename) {
void EditorManager::OpenRomOrProject(const std::string &filename) {
if (absl::StrContains(filename, ".yaze")) {
status_ = current_project_.Open(filename);
if (status_.ok()) {
@@ -814,5 +746,5 @@ absl::Status EditorManager::OpenProject() {
return absl::OkStatus();
}
} // namespace editor
} // namespace yaze
} // namespace editor
} // namespace yaze

View File

@@ -55,8 +55,6 @@ class EditorManager : public SharedRom, public core::ExperimentFlags {
private:
void ManageActiveEditors();
absl::Status DrawDynamicLayout();
void ManageKeyboardShortcuts();
void InitializeCommands();
@@ -80,16 +78,10 @@ class EditorManager : public SharedRom, public core::ExperimentFlags {
bool save_new_auto_ = true;
bool show_status_ = false;
bool rom_assets_loaded_ = false;
bool dynamic_layout_ = false;
absl::Status status_;
emu::Emulator emulator_;
std::vector<Editor *> active_editors_;
std::vector<EditorLayoutParams> active_layouts_;
EditorLayoutParams root_layout_;
Project current_project_;
EditorContext editor_context_;