add PopupManager

This commit is contained in:
SweetId 2023-10-17 18:04:12 -04:00
parent 37c1297cf4
commit 9d2f992297
5 changed files with 160 additions and 0 deletions

View File

@ -10,6 +10,7 @@
#include <NazaraEditor/Core/Application/EditorLogger.hpp> #include <NazaraEditor/Core/Application/EditorLogger.hpp>
#include <NazaraEditor/Core/Application/Level.hpp> #include <NazaraEditor/Core/Application/Level.hpp>
#include <NazaraEditor/Core/Components/NameComponent.hpp> #include <NazaraEditor/Core/Components/NameComponent.hpp>
#include <NazaraEditor/Core/UI/PopupManager.hpp>
#include <NazaraEditor/Core/UI/Window.hpp> #include <NazaraEditor/Core/UI/Window.hpp>
#include <NazaraEditor/Core/Reflection/Math.hpp> #include <NazaraEditor/Core/Reflection/Math.hpp>
#include <NazaraEditor/Core/Reflection/Core.hpp> #include <NazaraEditor/Core/Reflection/Core.hpp>

View File

@ -10,6 +10,7 @@
#include <NazaraEditor/Core/Application/Action.hpp> #include <NazaraEditor/Core/Application/Action.hpp>
#include <NazaraEditor/Core/Application/ActionStack.hpp> #include <NazaraEditor/Core/Application/ActionStack.hpp>
#include <NazaraEditor/Core/Application/Level.hpp> #include <NazaraEditor/Core/Application/Level.hpp>
#include <NazaraEditor/Core/UI/PopupManager.hpp>
#include <NazaraEditor/Core/UI/Window.hpp> #include <NazaraEditor/Core/UI/Window.hpp>
#include <NazaraImgui/NazaraImgui.hpp> #include <NazaraImgui/NazaraImgui.hpp>
@ -71,6 +72,8 @@ namespace Nz
std::filesystem::path m_resourceFolder; std::filesystem::path m_resourceFolder;
Nz::ActionStack m_actionStack; Nz::ActionStack m_actionStack;
Nz::EditorPopupManager m_popupManager;
Nz::Level m_level; Nz::Level m_level;
}; };
} }

View File

@ -0,0 +1,67 @@
#pragma once
#include <NazaraEditor/Core/Core.hpp>
#include <memory>
#include <vector>
namespace Nz
{
struct EditorPopupParameters
{
struct Choice
{
std::string name;
std::function<void(void)> callback;
};
std::string title;
std::string description;
std::vector<Choice> choices;
};
class EditorPopup
: public Nz::ImguiHandler
{
public:
EditorPopup(uint64_t id, const EditorPopupParameters& parameters);
~EditorPopup();
void OnRenderImgui() override;
uint64_t GetId() const { return m_id; }
private:
uint64_t m_id;
EditorPopupParameters m_parameters;
};
class NAZARAEDITOR_CORE_API EditorPopupManager
{
public:
static EditorPopupManager* Instance();
void Update();
uint64_t CreatePopup(const EditorPopupParameters& parameters);
void DestroyPopup(uint64_t popupId);
private:
EditorPopupManager();
~EditorPopupManager();
EditorPopupManager(const EditorPopupManager&) = delete;
EditorPopupManager& operator=(const EditorPopupManager&) = delete;
EditorPopupManager(EditorPopup&&) noexcept = delete;
EditorPopupManager& operator=(EditorPopup&&) = delete;
void DestroyPopupInternal(uint64_t popupId);
uint64_t m_currentIndex;
std::vector<std::unique_ptr<EditorPopup>> m_popups;
std::vector<uint64_t> m_popupsToDelete;
static EditorPopupManager* s_instance;
friend class EditorBaseApplication;
};
}

View File

@ -36,6 +36,8 @@ namespace Nz
window.ProcessEvents(); window.ProcessEvents();
m_popupManager.Update();
Nz::RenderFrame frame = m_windowSwapchain->AcquireFrame(); Nz::RenderFrame frame = m_windowSwapchain->AcquireFrame();
if (!frame) if (!frame)
return; return;

View File

@ -0,0 +1,87 @@
#include <NazaraEditor/Core/UI/PopupManager.hpp>
namespace Nz
{
EditorPopup::EditorPopup(uint64_t id, const EditorPopupParameters& parameters)
: m_id(id)
, m_parameters(parameters)
{
Nz::Imgui::Instance()->AddHandler(this);
}
EditorPopup::~EditorPopup()
{
Nz::Imgui::Instance()->RemoveHandler(this);
}
void EditorPopup::OnRenderImgui()
{
ImGui::OpenPopup(m_parameters.title.c_str());
if (ImGui::BeginPopupModal(m_parameters.title.c_str(), nullptr, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoCollapse))
{
ImGui::Text(m_parameters.description.c_str());
for (auto&& choice : m_parameters.choices)
{
if (ImGui::Button(choice.name.c_str()))
{
if (choice.callback)
choice.callback();
EditorPopupManager::Instance()->DestroyPopup(m_id);
}
ImGui::SameLine();
}
ImGui::EndPopup();
}
}
EditorPopupManager* EditorPopupManager::s_instance = nullptr;
EditorPopupManager* EditorPopupManager::Instance()
{
return s_instance;
}
EditorPopupManager::EditorPopupManager()
: m_currentIndex(0)
{
NazaraAssert(s_instance == nullptr, "EditorPopupManager already exists");
s_instance = this;
}
EditorPopupManager::~EditorPopupManager()
{
s_instance = nullptr;
}
void EditorPopupManager::Update()
{
for (uint64_t id : m_popupsToDelete)
DestroyPopupInternal(id);
m_popupsToDelete.clear();
}
uint64_t EditorPopupManager::CreatePopup(const EditorPopupParameters& parameters)
{
int64_t currentIndex = m_currentIndex++;
m_popups.push_back(std::make_unique<EditorPopup>(currentIndex, parameters));
return currentIndex;
}
void EditorPopupManager::DestroyPopup(uint64_t popupId)
{
m_popupsToDelete.push_back(popupId);
}
void EditorPopupManager::DestroyPopupInternal(uint64_t popupId)
{
auto it = std::find_if(m_popups.begin(), m_popups.end(), [popupId](auto&& popup) { return popup->GetId() == popupId; });
if (it == m_popups.end())
return;
m_popups.erase(it);
}
}