mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 16:40:05 +00:00
Add ImGui Manager to manage creation and deletion of contexts
This commit is contained in:
parent
fef3c597d0
commit
ee2ec329da
5 changed files with 197 additions and 3 deletions
92
scwx-qt/source/scwx/qt/manager/imgui_manager.cpp
Normal file
92
scwx-qt/source/scwx/qt/manager/imgui_manager.cpp
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
#include <scwx/qt/manager/imgui_manager.hpp>
|
||||
#include <scwx/util/logger.hpp>
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
namespace qt
|
||||
{
|
||||
namespace manager
|
||||
{
|
||||
|
||||
static const std::string logPrefix_ = "scwx::qt::manager::imgui_manager";
|
||||
static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
|
||||
|
||||
class ImGuiManagerImpl
|
||||
{
|
||||
public:
|
||||
explicit ImGuiManagerImpl() {}
|
||||
|
||||
~ImGuiManagerImpl() = default;
|
||||
|
||||
std::vector<ImGuiContextInfo> contexts_ {};
|
||||
};
|
||||
|
||||
ImGuiManager::ImGuiManager() :
|
||||
QObject(nullptr), p {std::make_unique<ImGuiManagerImpl>()}
|
||||
{
|
||||
}
|
||||
|
||||
ImGuiManager::~ImGuiManager() {}
|
||||
|
||||
ImGuiContext* ImGuiManager::CreateContext(const std::string& name)
|
||||
{
|
||||
ImGuiContext* context = ImGui::CreateContext();
|
||||
ImGui::SetCurrentContext(context);
|
||||
|
||||
// ImGui Configuration
|
||||
auto& io = ImGui::GetIO();
|
||||
|
||||
// Disable automatic configuration loading/saving
|
||||
io.IniFilename = nullptr;
|
||||
|
||||
// Style
|
||||
auto& style = ImGui::GetStyle();
|
||||
style.WindowMinSize = {10.0f, 10.0f};
|
||||
|
||||
// Register context
|
||||
static size_t nextId_ {0};
|
||||
p->contexts_.emplace_back(ImGuiContextInfo {nextId_++, name, context});
|
||||
|
||||
// Inform observers contexts have been updated
|
||||
emit ContextsUpdated();
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
void ImGuiManager::DestroyContext(const std::string& name)
|
||||
{
|
||||
// Find context from registry
|
||||
auto it = std::find_if(p->contexts_.begin(),
|
||||
p->contexts_.end(),
|
||||
[&](auto& info) { return info.name_ == name; });
|
||||
|
||||
if (it != p->contexts_.end())
|
||||
{
|
||||
// Destroy context and erase from index
|
||||
ImGui::SetCurrentContext(it->context_);
|
||||
ImGui::DestroyContext();
|
||||
p->contexts_.erase(it);
|
||||
|
||||
// Inform observers contexts have been updated
|
||||
emit ContextsUpdated();
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<ImGuiContextInfo> ImGuiManager::contexts() const
|
||||
{
|
||||
return p->contexts_;
|
||||
}
|
||||
|
||||
ImGuiManager& ImGuiManager::Instance()
|
||||
{
|
||||
static ImGuiManager instance_;
|
||||
return instance_;
|
||||
}
|
||||
|
||||
bool ImGuiContextInfo::operator==(const ImGuiContextInfo& o) const = default;
|
||||
|
||||
} // namespace manager
|
||||
} // namespace qt
|
||||
} // namespace scwx
|
||||
55
scwx-qt/source/scwx/qt/manager/imgui_manager.hpp
Normal file
55
scwx-qt/source/scwx/qt/manager/imgui_manager.hpp
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <QObject>
|
||||
|
||||
struct ImGuiContext;
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
namespace qt
|
||||
{
|
||||
namespace manager
|
||||
{
|
||||
|
||||
class ImGuiManagerImpl;
|
||||
|
||||
struct ImGuiContextInfo
|
||||
{
|
||||
size_t id_ {};
|
||||
std::string name_ {};
|
||||
ImGuiContext* context_ {};
|
||||
|
||||
bool operator==(const ImGuiContextInfo& o) const;
|
||||
};
|
||||
|
||||
class ImGuiManager : public QObject
|
||||
{
|
||||
private:
|
||||
Q_OBJECT
|
||||
Q_DISABLE_COPY(ImGuiManager)
|
||||
|
||||
public:
|
||||
explicit ImGuiManager();
|
||||
~ImGuiManager();
|
||||
|
||||
ImGuiContext* CreateContext(const std::string& name);
|
||||
void DestroyContext(const std::string& name);
|
||||
|
||||
std::vector<ImGuiContextInfo> contexts() const;
|
||||
|
||||
static ImGuiManager& Instance();
|
||||
|
||||
signals:
|
||||
void ContextsUpdated();
|
||||
|
||||
private:
|
||||
friend class ImGuiManagerImpl;
|
||||
std::unique_ptr<ImGuiManagerImpl> p;
|
||||
};
|
||||
|
||||
} // namespace manager
|
||||
} // namespace qt
|
||||
} // namespace scwx
|
||||
Loading…
Add table
Add a link
Reference in a new issue