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 enum Result
{ {
OK, OK,
CANCEL CANCEL,
STILL_WAITING // TODO: Implement STILL_WAITING
}; };
enum SelectionMode enum SelectionMode

@ -16,7 +16,10 @@
#include <internal_data/dataManager.h> #include <internal_data/dataManager.h>
#include <gui/dearimgui/imgui.h> #include <gui/dearimgui/imgui.h>
// Panels
#include "panels/worldTree.h"
#include "panels/worldView.h"
#include "panels/propertiesView.h"
#include "panels/assetBrowser.h" #include "panels/assetBrowser.h"
// Tools // Tools
@ -24,6 +27,9 @@
using namespace lunarium::gui; 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 lunarium
{ {
namespace editor namespace editor
@ -42,7 +48,18 @@ namespace editor
// Initialize internal data // Initialize internal data
DataManager::Initialize(); 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(); return OpRes::OK();
} }
@ -50,15 +67,13 @@ namespace editor
void Editor::Shutdown() void Editor::Shutdown()
{ {
DataManager::Shutdown(); DataManager::Shutdown();
PanelManager::GetInstance().Shutdown(); mPanelManager.Shutdown();
PanelManager::FreeInstance();
} }
void Editor::OnTick(double delta) void Editor::OnTick(double delta)
{ {
// Panels // Panels
PanelManager::GetInstance().OnTick(delta); mPanelManager.OnTick(delta);
// Tools // Tools
if (mpMapEditor) if (mpMapEditor)
@ -73,12 +88,17 @@ namespace editor
{ {
DoMainMenu(); DoMainMenu();
DoStatusBar(); DoStatusBar();
PanelManager::GetInstance().OnRender(g); mPanelManager.OnRender(g);
if (mpMapEditor) if (mpMapEditor)
{ {
mpMapEditor->OnRender(g); mpMapEditor->OnRender(g);
} }
if (mAboutPanel.IsOpen())
{
mAboutPanel.DoFrame();
}
if (mpFileBrowser) if (mpFileBrowser)
{ {
@ -163,7 +183,7 @@ namespace editor
{ {
Logger::Log(mLogCat, LogLevel::ERROR, "Could not create a new project: %s", result.Description); 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 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); 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 else
{ {
@ -281,7 +301,7 @@ namespace editor
{ {
if (ImGui::MenuItem("Reset Window Docking")) if (ImGui::MenuItem("Reset Window Docking"))
{ {
PanelManager::GetInstance().ResetDocking(); mPanelManager.ResetDocking();
} }
ImGui::EndMenu(); ImGui::EndMenu();
} }
@ -306,7 +326,7 @@ namespace editor
{ {
if (ImGui::MenuItem("About")) if (ImGui::MenuItem("About"))
{ {
PanelManager::GetInstance().OpenPanel(PanelType::PT_ABOUT); mAboutPanel.SetOpen(true);
} }
ImGui::EndMenu(); ImGui::EndMenu();

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

@ -23,33 +23,16 @@ using namespace lunarium::gui;
namespace lunarium { namespace editor 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() 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; mpEditor = editor;
mName = name;
memset(&mWindowClass, 0, sizeof(ImGuiWindowClass)); memset(&mWindowClass, 0, sizeof(ImGuiWindowClass));
mWindowClass.ClassId = mpEditor->GetNextWindowID(); mWindowClass.ClassId = mpEditor->GetNextWindowID();
@ -59,8 +42,6 @@ namespace lunarium { namespace editor
mResetDockSpace = true; mResetDockSpace = true;
} }
CreatePanels();
return OpRes::OK(); return OpRes::OK();
} }
@ -69,6 +50,19 @@ namespace lunarium { namespace editor
DestoryPanels(); 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) void PanelManager::OpenPanel(PanelType type)
{ {
Panel* p = GetPanel(type); Panel* p = GetPanel(type);
@ -139,7 +133,8 @@ namespace lunarium { namespace editor
{ {
// mpMainPanel->DoFrame(); // mpMainPanel->DoFrame();
ImGuiViewport* Viewport = ImGui::GetMainViewport(); //ImGuiViewport* Viewport = ImGui::GetMainViewport();
ImGuiViewport* Viewport = ImGui::GetWindowViewport();
ImGui::SetNextWindowPos( Viewport->WorkPos ); ImGui::SetNextWindowPos( Viewport->WorkPos );
ImGui::SetNextWindowSize( Viewport->WorkSize ); ImGui::SetNextWindowSize( Viewport->WorkSize );
ImGui::SetNextWindowViewport( Viewport->ID ); ImGui::SetNextWindowViewport( Viewport->ID );
@ -151,7 +146,7 @@ namespace lunarium { namespace editor
ImGui::PushStyleVar( ImGuiStyleVar_WindowBorderSize, 0.0f ); ImGui::PushStyleVar( ImGuiStyleVar_WindowBorderSize, 0.0f );
ImGui::PushStyleVar( ImGuiStyleVar_WindowPadding, ImVec2( 0.0f, 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); ImGui::PopStyleVar(3);
MakeDockSpaces(); MakeDockSpaces();
@ -171,36 +166,27 @@ namespace lunarium { namespace editor
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// HELPER METHODS // 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() void PanelManager::DestoryPanels()
{ {
delete mPanels[PanelType::PT_ABOUT]; for (auto iter = mPanels.begin(); iter != mPanels.end(); iter++)
delete mPanels[PanelType::PT_ASSET_BROWSER]; {
delete mPanels[PanelType::PT_WORLD_TREE]; delete iter->second;
delete mPanels[PanelType::PT_WORLD_VIEW]; }
delete mPanels[PanelType::PT_PROPERTIES_VIEW];
mPanels.clear(); mPanels.clear();
} }
void PanelManager::MakeDockSpaces() void PanelManager::MakeDockSpaces()
{ {
ImGuiViewport* Viewport = ImGui::GetMainViewport(); //ImGuiViewport* Viewport = ImGui::GetMainViewport();
ImGuiViewport* Viewport = ImGui::GetWindowViewport();
ImGuiID windowID = mpEditor->GetNextWindowID(); 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) 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; mResetDockSpace = false;
ImGui::DockBuilderRemoveNode(mDockSpaceID); ImGui::DockBuilderRemoveNode(mDockSpaceID);

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

Loading…
Cancel
Save