PanelManager refactored into a reusable, generic class.

Each editor tool can now use it's own panel manager.
Gui_Panel_Refactor
Joeyrp 4 years ago
parent 8ac5ef51d5
commit 4c48c455b1

@ -28,7 +28,8 @@ namespace lunarium
enum Result
{
OK,
CANCEL
CANCEL,
STILL_WAITING // TODO: Implement STILL_WAITING
};
enum SelectionMode

@ -16,7 +16,10 @@
#include <internal_data/dataManager.h>
#include <gui/dearimgui/imgui.h>
// Panels
#include "panels/worldTree.h"
#include "panels/worldView.h"
#include "panels/propertiesView.h"
#include "panels/assetBrowser.h"
// Tools
@ -24,6 +27,9 @@
using namespace lunarium::gui;
// ERROR LOG MACRO
#define LOG_ERR_AND_RETURN(result) { if (Failed(result)) { Logger::Log(mLogCat, LogLevel::ERROR, result.Description.c_str()); return result; } }
namespace lunarium
{
namespace editor
@ -42,7 +48,18 @@ namespace editor
// Initialize internal data
DataManager::Initialize();
PanelManager::GetInstance().Initialize(this);
// Init editor panels
mAboutPanel.SetOpen(false);
mPanelManager.Initialize(this, "Lunarium editor");
OpRes result = mPanelManager.AddPanel(gui::PanelType::PT_ASSET_BROWSER, new AssetBrowser(""));
LOG_ERR_AND_RETURN(result);
result = mPanelManager.AddPanel(gui::PanelType::PT_WORLD_TREE, new WorldTree());
LOG_ERR_AND_RETURN(result);
result = mPanelManager.AddPanel(gui::PanelType::PT_WORLD_VIEW, new WorldView());
LOG_ERR_AND_RETURN(result);
result = mPanelManager.AddPanel(gui::PanelType::PT_PROPERTIES_VIEW, new PropertiesView());
LOG_ERR_AND_RETURN(result);
return OpRes::OK();
}
@ -50,15 +67,13 @@ namespace editor
void Editor::Shutdown()
{
DataManager::Shutdown();
PanelManager::GetInstance().Shutdown();
PanelManager::FreeInstance();
mPanelManager.Shutdown();
}
void Editor::OnTick(double delta)
{
// Panels
PanelManager::GetInstance().OnTick(delta);
mPanelManager.OnTick(delta);
// Tools
if (mpMapEditor)
@ -73,12 +88,17 @@ namespace editor
{
DoMainMenu();
DoStatusBar();
PanelManager::GetInstance().OnRender(g);
mPanelManager.OnRender(g);
if (mpMapEditor)
{
mpMapEditor->OnRender(g);
}
if (mAboutPanel.IsOpen())
{
mAboutPanel.DoFrame();
}
if (mpFileBrowser)
{
@ -163,7 +183,7 @@ namespace editor
{
Logger::Log(mLogCat, LogLevel::ERROR, "Could not create a new project: %s", result.Description);
}
((AssetBrowser*)PanelManager::GetInstance().GetPanel(PanelType::PT_ASSET_BROWSER))->SetAssetDirectory(*mpPath / std::filesystem::path("contents/assets"));
((AssetBrowser*)mPanelManager.GetPanel(PanelType::PT_ASSET_BROWSER))->SetAssetDirectory(*mpPath / std::filesystem::path("contents/assets"));
}
else
{
@ -208,7 +228,7 @@ namespace editor
{
Logger::Log(mLogCat, LogLevel::ERROR, "Could not open project: %s -- reason: %s", mpPath->string().c_str(), result.Description);
}
((AssetBrowser*)PanelManager::GetInstance().GetPanel(PanelType::PT_ASSET_BROWSER))->SetAssetDirectory(mpPath->parent_path() / std::filesystem::path("contents/assets"));
((AssetBrowser*)mPanelManager.GetPanel(PanelType::PT_ASSET_BROWSER))->SetAssetDirectory(mpPath->parent_path() / std::filesystem::path("contents/assets"));
}
else
{
@ -281,7 +301,7 @@ namespace editor
{
if (ImGui::MenuItem("Reset Window Docking"))
{
PanelManager::GetInstance().ResetDocking();
mPanelManager.ResetDocking();
}
ImGui::EndMenu();
}
@ -306,7 +326,7 @@ namespace editor
{
if (ImGui::MenuItem("About"))
{
PanelManager::GetInstance().OpenPanel(PanelType::PT_ABOUT);
mAboutPanel.SetOpen(true);
}
ImGui::EndMenu();

@ -13,6 +13,8 @@
#include <utils/opRes.h>
#include "project/project.h"
#include "panel_manager.h"
#include "panels/about.h"
#include <filesystem>
#include <map>
@ -50,7 +52,7 @@ namespace lunarium
private: // Data
uint32_t mLogCat;
PanelManager mPanelManager;
Project mProject;
// Tools
@ -60,6 +62,9 @@ namespace lunarium
FileBrowser* mpFileBrowser;
const std::filesystem::path* mpPath;
// Non-Docking panels
AboutPanel mAboutPanel;
// Menu Bar Events
// Don't want to handles these events during rendering
bool mDoNewProject;

@ -23,33 +23,16 @@ using namespace lunarium::gui;
namespace lunarium { namespace editor
{
PanelManager* PanelManager::mpInstance = nullptr;
PanelManager& PanelManager::GetInstance()
{
if (!mpInstance)
{
mpInstance = new PanelManager();
}
return *mpInstance;
}
void PanelManager::FreeInstance()
{
delete mpInstance;
mpInstance = nullptr;
}
PanelManager::PanelManager()
: mpEditor(nullptr), mResetDockSpace(false)
: mpEditor(nullptr), mResetDockSpace(false), mDockSpaceID(0)
{
}
OpRes PanelManager::Initialize(Editor* editor)
OpRes PanelManager::Initialize(Editor* editor, std::string name)
{
mpEditor = editor;
mName = name;
memset(&mWindowClass, 0, sizeof(ImGuiWindowClass));
mWindowClass.ClassId = mpEditor->GetNextWindowID();
@ -59,8 +42,6 @@ namespace lunarium { namespace editor
mResetDockSpace = true;
}
CreatePanels();
return OpRes::OK();
}
@ -69,6 +50,19 @@ namespace lunarium { namespace editor
DestoryPanels();
}
OpRes PanelManager::AddPanel(gui::PanelType type, gui::Panel* panel)
{
if (GetPanel(type))
{
return OpRes::Fail("Cannot add panel - panel already exists. Panel Name: %s", panel->GetName());
}
mPanels[type] = panel;
return OpRes::OK();
}
void PanelManager::OpenPanel(PanelType type)
{
Panel* p = GetPanel(type);
@ -139,7 +133,8 @@ namespace lunarium { namespace editor
{
// mpMainPanel->DoFrame();
ImGuiViewport* Viewport = ImGui::GetMainViewport();
//ImGuiViewport* Viewport = ImGui::GetMainViewport();
ImGuiViewport* Viewport = ImGui::GetWindowViewport();
ImGui::SetNextWindowPos( Viewport->WorkPos );
ImGui::SetNextWindowSize( Viewport->WorkSize );
ImGui::SetNextWindowViewport( Viewport->ID );
@ -151,7 +146,7 @@ namespace lunarium { namespace editor
ImGui::PushStyleVar( ImGuiStyleVar_WindowBorderSize, 0.0f );
ImGui::PushStyleVar( ImGuiStyleVar_WindowPadding, ImVec2( 0.0f, 0.0f ) );
ImGui::Begin("Lunarium Editor", nullptr, WindowFlags);
ImGui::Begin(mName.c_str(), nullptr, WindowFlags);
ImGui::PopStyleVar(3);
MakeDockSpaces();
@ -171,36 +166,27 @@ namespace lunarium { namespace editor
////////////////////////////////////////////////////////////
// HELPER METHODS
////////////////////////////////////////////////////////////
void PanelManager::CreatePanels()
{
mPanels[PanelType::PT_ABOUT] = new AboutPanel;
mPanels[PanelType::PT_ASSET_BROWSER] = new AssetBrowser("");
mPanels[PanelType::PT_WORLD_TREE] = new WorldTree();
mPanels[PanelType::PT_WORLD_VIEW] = new WorldView();
mPanels[PanelType::PT_PROPERTIES_VIEW] = new PropertiesView();
}
void PanelManager::DestoryPanels()
{
delete mPanels[PanelType::PT_ABOUT];
delete mPanels[PanelType::PT_ASSET_BROWSER];
delete mPanels[PanelType::PT_WORLD_TREE];
delete mPanels[PanelType::PT_WORLD_VIEW];
delete mPanels[PanelType::PT_PROPERTIES_VIEW];
for (auto iter = mPanels.begin(); iter != mPanels.end(); iter++)
{
delete iter->second;
}
mPanels.clear();
}
void PanelManager::MakeDockSpaces()
{
ImGuiViewport* Viewport = ImGui::GetMainViewport();
//ImGuiViewport* Viewport = ImGui::GetMainViewport();
ImGuiViewport* Viewport = ImGui::GetWindowViewport();
ImGuiID windowID = mpEditor->GetNextWindowID();
mDockSpaceID = ImGui::DockSpace(ImGui::GetID("Lunarium Editor Dockspace"), ImVec2(0, 0), 0, &mWindowClass);
std::string dock_name = mName;
dock_name += " DockSpace";
mDockSpaceID = ImGui::DockSpace(ImGui::GetID(dock_name.c_str()), ImVec2(0, 0), 0, &mWindowClass);
if (!ImGui::DockBuilderGetNode(mDockSpaceID) || mResetDockSpace)
{
Logger::Log(mpEditor->GetLogCat(), LogLevel::INFO, "Resetting Dockspaces");
Logger::Log(mpEditor->GetLogCat(), LogLevel::INFO_VERBOSE, "Resetting Dockspace: %s", dock_name.c_str());
mResetDockSpace = false;
ImGui::DockBuilderRemoveNode(mDockSpaceID);

@ -25,11 +25,11 @@ namespace editor
{
public:
static PanelManager& GetInstance();
static void FreeInstance();
OpRes Initialize(Editor* editor);
PanelManager();
OpRes Initialize(Editor* editor, std::string name);
void Shutdown();
OpRes AddPanel(gui::PanelType type, gui::Panel* panel);
void OpenPanel(gui::PanelType type);
void ClosePanel(gui::PanelType type);
bool IsOpen(gui::PanelType type);
@ -43,7 +43,7 @@ namespace editor
private:
Editor* mpEditor;
// MainPanel* mpMainPanel;
std::string mName;
bool mResetDockSpace;
std::map<gui::PanelType, gui::Panel*> mPanels;
unsigned int mDockSpaceID;
@ -51,10 +51,6 @@ namespace editor
std::map<gui::PanelDockZone, unsigned int> mDockZoneIDs;
private:
static PanelManager* mpInstance;
PanelManager();
void CreatePanels();
void DestoryPanels();
void MakeDockSpaces();

Loading…
Cancel
Save