Enable ImGui context selection

This commit is contained in:
Dan Paulat 2022-11-28 00:20:31 -06:00
parent 9684aa4cdc
commit f27e2534a7
6 changed files with 76 additions and 14 deletions

View file

@ -1,4 +1,5 @@
#include <scwx/qt/model/imgui_context_model.hpp> #include <scwx/qt/model/imgui_context_model.hpp>
#include <scwx/qt/types/qt_types.hpp>
#include <scwx/util/logger.hpp> #include <scwx/util/logger.hpp>
#include <imgui.h> #include <imgui.h>
@ -54,6 +55,28 @@ QVariant ImGuiContextModel::data(const QModelIndex& index, int role) const
return QString("%1: %2") return QString("%1: %2")
.arg(p->contexts_[row].id_) .arg(p->contexts_[row].id_)
.arg(p->contexts_[row].name_.c_str()); .arg(p->contexts_[row].name_.c_str());
case qt::types::ItemDataRole::RawDataRole:
QVariant variant {};
variant.setValue(p->contexts_[row]);
return variant;
}
return {};
}
QModelIndex ImGuiContextModel::IndexOf(const std::string& contextName) const
{
// Find context from registry
auto it =
std::find_if(p->contexts_.begin(),
p->contexts_.end(),
[&](auto& info) { return info.name_ == contextName; });
if (it != p->contexts_.end())
{
const int row = it - p->contexts_.begin();
return createIndex(row, 0, nullptr);
} }
return {}; return {};
@ -107,11 +130,6 @@ void ImGuiContextModel::DestroyContext(const std::string& name)
} }
} }
std::vector<ImGuiContextInfo> ImGuiContextModel::contexts() const
{
return p->contexts_;
}
ImGuiContextModel& ImGuiContextModel::Instance() ImGuiContextModel& ImGuiContextModel::Instance()
{ {
static ImGuiContextModel instance_ {}; static ImGuiContextModel instance_ {};

View file

@ -40,11 +40,11 @@ public:
QVariant data(const QModelIndex& index, QVariant data(const QModelIndex& index,
int role = Qt::DisplayRole) const override; int role = Qt::DisplayRole) const override;
QModelIndex IndexOf(const std::string& contextName) const;
ImGuiContext* CreateContext(const std::string& name); ImGuiContext* CreateContext(const std::string& name);
void DestroyContext(const std::string& name); void DestroyContext(const std::string& name);
std::vector<ImGuiContextInfo> contexts() const;
static ImGuiContextModel& Instance(); static ImGuiContextModel& Instance();
signals: signals:

View file

@ -12,7 +12,8 @@ namespace types
enum ItemDataRole enum ItemDataRole
{ {
SortRole = Qt::UserRole, SortRole = Qt::UserRole,
TimePointRole TimePointRole,
RawDataRole
}; };
} // namespace types } // namespace types

View file

@ -2,6 +2,7 @@
#include "ui_imgui_debug_dialog.h" #include "ui_imgui_debug_dialog.h"
#include <scwx/qt/model/imgui_context_model.hpp> #include <scwx/qt/model/imgui_context_model.hpp>
#include <scwx/qt/types/qt_types.hpp>
#include <scwx/qt/ui/imgui_debug_widget.hpp> #include <scwx/qt/ui/imgui_debug_widget.hpp>
namespace scwx namespace scwx
@ -36,7 +37,26 @@ ImGuiDebugDialog::ImGuiDebugDialog(QWidget* parent) :
ui->verticalLayout->insertWidget(0, p->imGuiDebugWidget_); ui->verticalLayout->insertWidget(0, p->imGuiDebugWidget_);
// Context Combo Box // Context Combo Box
ui->contextComboBox->setModel(&model::ImGuiContextModel::Instance()); auto& contextModel = model::ImGuiContextModel::Instance();
auto index = contextModel.IndexOf(p->imGuiDebugWidget_->context_name());
ui->contextComboBox->setModel(&contextModel);
ui->contextComboBox->setCurrentIndex(index.row());
connect(
ui->contextComboBox,
&QComboBox::currentIndexChanged,
[=](int row)
{
auto& contextModel = model::ImGuiContextModel::Instance();
auto index = contextModel.index(row, 0);
if (index.isValid())
{
QVariant data = contextModel.data(index, qt::types::RawDataRole);
auto contextInfo = data.value<model::ImGuiContextInfo>();
p->imGuiDebugWidget_->set_current_context(contextInfo.context_);
}
});
} }
ImGuiDebugDialog::~ImGuiDebugDialog() ImGuiDebugDialog::~ImGuiDebugDialog()

View file

@ -1,6 +1,8 @@
#include <scwx/qt/ui/imgui_debug_widget.hpp> #include <scwx/qt/ui/imgui_debug_widget.hpp>
#include <scwx/qt/model/imgui_context_model.hpp> #include <scwx/qt/model/imgui_context_model.hpp>
#include <set>
#include <imgui.h> #include <imgui.h>
#include <backends/imgui_impl_opengl3.h> #include <backends/imgui_impl_opengl3.h>
#include <backends/imgui_impl_qt.hpp> #include <backends/imgui_impl_qt.hpp>
@ -24,6 +26,7 @@ public:
contextName_ = std::format("ImGui Debug {}", ++currentIndex_); contextName_ = std::format("ImGui Debug {}", ++currentIndex_);
context_ = context_ =
model::ImGuiContextModel::Instance().CreateContext(contextName_); model::ImGuiContextModel::Instance().CreateContext(contextName_);
currentContext_ = context_;
// Initialize ImGui Qt backend // Initialize ImGui Qt backend
ImGui_ImplQt_Init(); ImGui_ImplQt_Init();
@ -50,7 +53,9 @@ public:
ImGuiContext* context_; ImGuiContext* context_;
std::string contextName_; std::string contextName_;
bool firstRender_ {true}; ImGuiContext* currentContext_;
std::set<ImGuiContext*> renderedSet_ {};
bool imGuiRendererInitialized_ {false}; bool imGuiRendererInitialized_ {false};
}; };
@ -63,6 +68,17 @@ ImGuiDebugWidget::ImGuiDebugWidget(QWidget* parent) :
ImGuiDebugWidget::~ImGuiDebugWidget() {} ImGuiDebugWidget::~ImGuiDebugWidget() {}
std::string ImGuiDebugWidget::context_name() const
{
return p->contextName_;
}
void ImGuiDebugWidget::set_current_context(ImGuiContext* context)
{
p->currentContext_ = context;
update();
}
void ImGuiDebugWidget::initializeGL() void ImGuiDebugWidget::initializeGL()
{ {
makeCurrent(); makeCurrent();
@ -75,14 +91,14 @@ void ImGuiDebugWidget::initializeGL()
void ImGuiDebugWidget::paintGL() void ImGuiDebugWidget::paintGL()
{ {
ImGui::SetCurrentContext(p->context_); ImGui::SetCurrentContext(p->currentContext_);
ImGui_ImplQt_NewFrame(this); ImGui_ImplQt_NewFrame(this);
ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame(); ImGui::NewFrame();
if (p->firstRender_) if (!p->renderedSet_.contains(p->currentContext_))
{ {
// Set initial position of demo window // Set initial position of demo window
ImGui::SetNextWindowPos(ImVec2 {width() / 2.0f, height() / 2.0f}, ImGui::SetNextWindowPos(ImVec2 {width() / 2.0f, height() / 2.0f},
@ -91,7 +107,8 @@ void ImGuiDebugWidget::paintGL()
ImGui::Begin("Dear ImGui Demo"); ImGui::Begin("Dear ImGui Demo");
ImGui::End(); ImGui::End();
p->firstRender_ = false; p->renderedSet_.insert(p->currentContext_);
update();
} }
ImGui::ShowDemoWindow(); ImGui::ShowDemoWindow();

View file

@ -2,6 +2,8 @@
#include <QOpenGLWidget> #include <QOpenGLWidget>
struct ImGuiContext;
namespace Ui namespace Ui
{ {
class ImGuiDebugWidget; class ImGuiDebugWidget;
@ -25,6 +27,10 @@ public:
explicit ImGuiDebugWidget(QWidget* parent = nullptr); explicit ImGuiDebugWidget(QWidget* parent = nullptr);
~ImGuiDebugWidget(); ~ImGuiDebugWidget();
std::string context_name() const;
void set_current_context(ImGuiContext* context);
void initializeGL() override; void initializeGL() override;
void paintGL() override; void paintGL() override;