From 72fb87e667987ee71d28166c72c64fba637cec5a Mon Sep 17 00:00:00 2001 From: Joeyrp Date: Mon, 14 Feb 2022 15:23:00 -0500 Subject: [PATCH] Adds LogIfFailed to the OpRes class --- src/internal_libs/gui/panel_defs.h | 2 ++ src/internal_libs/utils/opRes.cpp | 16 +++++++++++ src/internal_libs/utils/opRes.h | 2 ++ src/run_modes/editor/editor.cpp | 35 ++++++++++++++++------ src/run_modes/editor/editor.h | 2 ++ src/run_modes/editor/panel_manager.cpp | 40 ++++++++++++++++++++++++-- src/run_modes/editor/panel_manager.h | 7 ++++- 7 files changed, 91 insertions(+), 13 deletions(-) diff --git a/src/internal_libs/gui/panel_defs.h b/src/internal_libs/gui/panel_defs.h index 7a3d8b4..2f75fc8 100644 --- a/src/internal_libs/gui/panel_defs.h +++ b/src/internal_libs/gui/panel_defs.h @@ -33,6 +33,8 @@ namespace lunarium { namespace gui DDZ_CENTER, DDZ_RIGHT, DDZ_BOTTOM, + DDZ_BOT_LEFT, + DDZ_BOT_RIGHT, DDZ_NONE }; diff --git a/src/internal_libs/utils/opRes.cpp b/src/internal_libs/utils/opRes.cpp index 312c025..b705c58 100644 --- a/src/internal_libs/utils/opRes.cpp +++ b/src/internal_libs/utils/opRes.cpp @@ -7,6 +7,7 @@ * ******************************************************************************/ +#include "logger.h" #include "opRes.h" #include @@ -40,6 +41,21 @@ namespace lunarium return { ResultType::FAIL, OpRes::Buffer }; } + OpRes OpRes::LogIfFailed(int category, const char* prepend_msg) + { + if (ResultType::FAIL == Type) + { + std::string msg; + if (prepend_msg) + { + msg = prepend_msg; + } + msg += Description; + Logger::Log(category, LogLevel::ERROR, msg.c_str()); + } + return *this; + } + bool IsOK(OpRes&& res) { return res.Type == ResultType::OK; diff --git a/src/internal_libs/utils/opRes.h b/src/internal_libs/utils/opRes.h index 7b42a20..f5bd956 100644 --- a/src/internal_libs/utils/opRes.h +++ b/src/internal_libs/utils/opRes.h @@ -38,6 +38,8 @@ namespace lunarium static OpRes Fail(const char* why, ...); + OpRes LogIfFailed(int category, const char* prepend_msg = nullptr); + private: static const int BUFFER_SIZE = 512; static char Buffer[OpRes::BUFFER_SIZE]; diff --git a/src/run_modes/editor/editor.cpp b/src/run_modes/editor/editor.cpp index f560cb6..232ab93 100644 --- a/src/run_modes/editor/editor.cpp +++ b/src/run_modes/editor/editor.cpp @@ -51,15 +51,16 @@ namespace editor // 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); + OpRes res = mPanelManager.Initialize(this, "Lunarium editor", true).LogIfFailed(mLogCat); + if (Failed(res)) + { + return res; + } + + mPanelManager.AddPanel(gui::PanelType::PT_ASSET_BROWSER, new AssetBrowser("")).LogIfFailed(mLogCat); + mPanelManager.AddPanel(gui::PanelType::PT_WORLD_TREE, new WorldTree()).LogIfFailed(mLogCat); + mPanelManager.AddPanel(gui::PanelType::PT_WORLD_VIEW, new WorldView()).LogIfFailed(mLogCat); + mPanelManager.AddPanel(gui::PanelType::PT_PROPERTIES_VIEW, new PropertiesView()).LogIfFailed(mLogCat); return OpRes::OK(); } @@ -303,6 +304,13 @@ namespace editor { mPanelManager.ResetDocking(); } + ImGui::Separator(); + + HanelOpenPanel("Asset Browser", gui::PanelType::PT_ASSET_BROWSER); + HanelOpenPanel("World Tree", gui::PanelType::PT_WORLD_TREE); + HanelOpenPanel("World View", gui::PanelType::PT_WORLD_VIEW); + HanelOpenPanel("Properties", gui::PanelType::PT_PROPERTIES_VIEW); + ImGui::EndMenu(); } @@ -335,6 +343,15 @@ namespace editor ImGui::EndMainMenuBar(); } + + void Editor::HanelOpenPanel(const char* name, gui::PanelType type) + { + if (ImGui::MenuItem(name)) + { + mPanelManager.OpenPanel(type); + } + } + void Editor::DoStatusBar() { const ImGuiViewport* viewport = ImGui::GetMainViewport(); diff --git a/src/run_modes/editor/editor.h b/src/run_modes/editor/editor.h index 476cb46..0312952 100644 --- a/src/run_modes/editor/editor.h +++ b/src/run_modes/editor/editor.h @@ -78,6 +78,8 @@ namespace lunarium void DoStatusBar(); void DestroyTools(); void HandleMenuEvents(); + + void HanelOpenPanel(const char* name, gui::PanelType type); }; } diff --git a/src/run_modes/editor/panel_manager.cpp b/src/run_modes/editor/panel_manager.cpp index 0f9fdb0..1db1319 100644 --- a/src/run_modes/editor/panel_manager.cpp +++ b/src/run_modes/editor/panel_manager.cpp @@ -24,15 +24,16 @@ using namespace lunarium::gui; namespace lunarium { namespace editor { PanelManager::PanelManager() - : mpEditor(nullptr), mResetDockSpace(false), mDockSpaceID(0) + : mpEditor(nullptr), mResetDockSpace(false), mDockSpaceID(0), mSplitBottom(false) { } - OpRes PanelManager::Initialize(Editor* editor, std::string name) + OpRes PanelManager::Initialize(Editor* editor, std::string name, bool split_bottom) { mpEditor = editor; mName = name; + mSplitBottom = split_bottom; memset(&mWindowClass, 0, sizeof(ImGuiWindowClass)); mWindowClass.ClassId = mpEditor->GetNextWindowID(); @@ -53,7 +54,7 @@ namespace lunarium { namespace editor OpRes PanelManager::AddPanel(gui::PanelType type, gui::Panel* panel) { - if (GetPanel(type)) + if (mPanels.find(type) != mPanels.end()) { return OpRes::Fail("Cannot add panel - panel already exists. Panel Name: %s", panel->GetName()); } @@ -113,6 +114,17 @@ namespace lunarium { namespace editor return &mWindowClass; } + + bool PanelManager::IsBottomSplit() const + { + return mSplitBottom; + } + + void PanelManager::SetSplitBottom(bool split_bottom) + { + mSplitBottom = split_bottom; + } + void PanelManager::ResetDocking() { mResetDockSpace = true; @@ -195,6 +207,12 @@ namespace lunarium { namespace editor //ImGui::DockBuilderSetNodePos(mDockSpaces.Main, Viewport->WorkPos); ImGui::DockBuilderSplitNode(mDockSpaceID, ImGuiDir_Down, 0.25f, &mDockZoneIDs[PanelDockZone::DDZ_BOTTOM], &mDockZoneIDs[PanelDockZone::DDZ_CENTER]); + + if (mSplitBottom) + { + ImGui::DockBuilderSplitNode(mDockZoneIDs[PanelDockZone::DDZ_BOTTOM], ImGuiDir_Left, 0.2f, &mDockZoneIDs[PanelDockZone::DDZ_BOT_LEFT], &mDockZoneIDs[PanelDockZone::DDZ_BOT_RIGHT]); + } + ImGui::DockBuilderSplitNode(mDockZoneIDs[PanelDockZone::DDZ_CENTER], ImGuiDir_Right, 0.2f, &mDockZoneIDs[PanelDockZone::DDZ_RIGHT], &mDockZoneIDs[PanelDockZone::DDZ_CENTER]); ImGui::DockBuilderSplitNode(mDockZoneIDs[PanelDockZone::DDZ_CENTER], ImGuiDir_Left, 0.2f, &mDockZoneIDs[PanelDockZone::DDZ_LEFT], &mDockZoneIDs[PanelDockZone::DDZ_CENTER]); ImGui::DockBuilderFinish(mDockSpaceID); @@ -205,6 +223,22 @@ namespace lunarium { namespace editor PanelDockZone dz = iter->second->GetDockZone(); if (dz != PanelDockZone::DDZ_NONE) { + // If the panel expects the bottom node to be split but it isn't + // we need to change it to just dock to the bottom + if ((dz == PanelDockZone::DDZ_BOT_LEFT || + dz == PanelDockZone::DDZ_BOT_RIGHT) && + !mSplitBottom) + { + dz = PanelDockZone::DDZ_BOTTOM; + } + + // Also need to account for the opposite situation + // Default it to the bottom left + if (dz == PanelDockZone::DDZ_BOTTOM && mSplitBottom) + { + dz = PanelDockZone::DDZ_BOT_LEFT; + } + ImGui::DockBuilderDockWindow(iter->second->GetName(), mDockZoneIDs[dz]); } } diff --git a/src/run_modes/editor/panel_manager.h b/src/run_modes/editor/panel_manager.h index 1fb8446..3c50ab8 100644 --- a/src/run_modes/editor/panel_manager.h +++ b/src/run_modes/editor/panel_manager.h @@ -26,9 +26,10 @@ namespace editor public: PanelManager(); - OpRes Initialize(Editor* editor, std::string name); + OpRes Initialize(Editor* editor, std::string name, bool split_bottom = false); void Shutdown(); + // Panel interface OpRes AddPanel(gui::PanelType type, gui::Panel* panel); void OpenPanel(gui::PanelType type); void ClosePanel(gui::PanelType type); @@ -36,6 +37,9 @@ namespace editor gui::Panel* GetPanel(gui::PanelType type); const ImGuiWindowClass* GetWindowClass() const; + // Docking + bool IsBottomSplit() const; + void SetSplitBottom(bool split_bottom); void ResetDocking(); void OnTick(double delta); @@ -44,6 +48,7 @@ namespace editor private: Editor* mpEditor; std::string mName; + bool mSplitBottom; // If ture the bottom dock node will also be split into left/right bool mResetDockSpace; std::map mPanels; unsigned int mDockSpaceID;