Popup window base & popup management

Added Scene save prompt popup (WIP)
This commit is contained in:
SHAM-DP 2023-01-07 17:34:07 +08:00
parent 1188c61c7d
commit f726592557
7 changed files with 165 additions and 1 deletions

View File

@ -0,0 +1,33 @@
#include "SHpch.h"
#include "SHEditorPopups.h"
#include "Editor/SHEditor.h"
#include "misc/cpp/imgui_stdlib.h"
namespace SHADE
{
void SHSceneSavePrompt::Draw()
{
if(Begin())
{
static std::string newSceneName{};
ImGui::Text("Enter new scene name");
ImGui::InputText("##name", &newSceneName);
ImGui::BeginDisabled(newSceneName.empty());
if (ImGui::Button("Save"))
{
editor->SaveScene(newSceneName);
newSceneName.clear();
isOpen = false;
ImGui::CloseCurrentPopup();
}
ImGui::EndDisabled();
ImGui::SameLine();
if (ImGui::Button("Cancel"))
{
isOpen = false;
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
}
}

View File

@ -0,0 +1,16 @@
#pragma once
#include "Editor/EditorWindow/SHPopUpWindow.h"
namespace SHADE
{
class SHSceneSavePrompt : SHPopUpWindow
{
public:
SHSceneSavePrompt():SHPopUpWindow("Save Scene As", true, 0, 0){}
void Draw() override;
private:
};
}

View File

@ -4,5 +4,7 @@
namespace SHADE namespace SHADE
{ {
SHEditorWindowManager::EditorWindowMap SHEditorWindowManager::editorWindows{}; SHEditorWindowManager::EditorWindowMap SHEditorWindowManager::editorWindows{};
SHEditorWindowManager::PopupWindowMap SHEditorWindowManager::popupWindows{};
SHEditorWindowManager::EditorWindowID SHEditorWindowManager::windowCount{}; SHEditorWindowManager::EditorWindowID SHEditorWindowManager::windowCount{};
SHEditorWindowManager::EditorWindowID SHEditorWindowManager::popupWindowCount{};
} }

View File

@ -3,6 +3,7 @@
#include <memory> #include <memory>
#include <unordered_map> #include <unordered_map>
#include "SHEditorWindow.h" #include "SHEditorWindow.h"
#include "SHPopUpWindow.h"
#include "Tools/Logger/SHLog.h" #include "Tools/Logger/SHLog.h"
namespace SHADE namespace SHADE
@ -16,6 +17,10 @@ namespace SHADE
using EditorWindowID = uint8_t; using EditorWindowID = uint8_t;
using EditorWindowPtr = std::unique_ptr<SHEditorWindow>; using EditorWindowPtr = std::unique_ptr<SHEditorWindow>;
using EditorWindowMap = std::unordered_map<EditorWindowID, EditorWindowPtr>; using EditorWindowMap = std::unordered_map<EditorWindowID, EditorWindowPtr>;
using PopupWindowPtr = std::unique_ptr<SHPopUpWindow>;
using PopupWindowMap = std::unordered_map<EditorWindowID, PopupWindowPtr>;
/** /**
* @brief Get ID for the Editor Window Type * @brief Get ID for the Editor Window Type
* *
@ -67,10 +72,63 @@ namespace SHADE
return reinterpret_cast<T*>(editorWindows[GetEditorWindowID<T>()].get()); return reinterpret_cast<T*>(editorWindows[GetEditorWindowID<T>()].get());
} }
/**
* @brief Get ID for the Popup Window Type
*
* @tparam T Type of Popup Window
* @return EditorWindowID ID of Popup Window Type
*/
template <typename T, std::enable_if_t<std::is_base_of_v<SHPopUpWindow, T>, bool> = true>
static EditorWindowID GetPopupWindowID()
{
static EditorWindowID id;
static bool idCreated = false;
if (!idCreated)
{
id = popupWindowCount++;
idCreated = true;
}
return id;
}
/**
* @brief Create an Popup Window
*
* @tparam T Type of Popup Window to create
*/
template <typename T, std::enable_if_t<std::is_base_of_v<SHPopUpWindow, T>, bool> = true>
static void CreatePopupWindow()
{
static bool isCreated = false;
if (!isCreated)
{
popupWindows[GetPopupWindowID<T>()] = std::make_unique<T>();
isCreated = true;
}
else
{
SHLog::Warning("Attempt to create duplicate of Popup window type");
}
}
/**
* @brief Get pointer to the Editor Window
*
* @tparam T Type of editor window to retrieve
* @return T* Pointer to the editor window
*/
template <typename T, std::enable_if_t<std::is_base_of_v<SHPopUpWindow, T>, bool> = true>
static T* GetPopupWindow()
{
return reinterpret_cast<T*>(popupWindows[GetPopupWindowID<T>()].get());
}
static EditorWindowMap editorWindows; static EditorWindowMap editorWindows;
static PopupWindowMap popupWindows;
private: private:
// Number of windows; used for Editor Window ID Generation // Number of windows; used for Editor Window ID Generation
static EditorWindowID windowCount; static EditorWindowID windowCount;
static EditorWindowID popupWindowCount;
// Map of Editor Windows // Map of Editor Windows
friend class SHEditor; friend class SHEditor;
}; };

View File

@ -0,0 +1,21 @@
#include "SHpch.h"
#include "SHPopUpWindow.h"
#include "ECS_Base/Managers/SHSystemManager.h"
#include "Editor/SHEditor.h"
namespace SHADE
{
SHPopUpWindow::SHPopUpWindow(std::string_view const& name, bool modal, ImGuiPopupFlags inPopupFlags, ImGuiWindowFlags inWindowFlags)
:editor(nullptr), windowName(name), popupFlags(inPopupFlags), windowFlags(inWindowFlags), isOpen(false), isModal(modal)
{
editor = SHSystemManager::GetSystem<SHEditor>();
}
bool SHPopUpWindow::Begin()
{
if (isOpen)
ImGui::OpenPopup(windowName.data(), popupFlags);
return isModal ? ImGui::BeginPopupModal(windowName.data(), &isOpen, windowFlags) : ImGui::BeginPopup(windowName.data(), windowFlags);
}
}

View File

@ -0,0 +1,29 @@
#pragma once
//#==============================================================#
//|| STL Includes ||
//#==============================================================#
#include <string>
#include <imgui.h>
namespace SHADE
{
class SHEditor;
class SHPopUpWindow
{
public:
SHPopUpWindow(std::string_view const& name, bool modal, ImGuiPopupFlags inPopupFlags, ImGuiWindowFlags inWindowFlags);
virtual void Draw(){};
protected:
virtual bool Begin();
SHEditor* editor;
std::string_view windowName;
ImGuiPopupFlags popupFlags;
ImGuiWindowFlags windowFlags;
bool isOpen;
bool isModal;
};
}

View File

@ -150,7 +150,7 @@ namespace SHADE
{ {
(void)dt; (void)dt;
NewFrame(); NewFrame();
for (const auto& window : SHEditorWindowManager::editorWindows | std::views::values) for (auto const& window : SHEditorWindowManager::editorWindows | std::views::values)
{ {
if(window->isOpen) if(window->isOpen)
{ {
@ -158,6 +158,11 @@ namespace SHADE
} }
} }
for(auto const& popupWindow : SHEditorWindowManager::popupWindows | std::views::values)
{
popupWindow->Draw();
}
RenderSceneNamePrompt(); RenderSceneNamePrompt();
RenderUnsavedChangesPrompt(); RenderUnsavedChangesPrompt();
//PollPicking(); //PollPicking();