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/types/qt_types.hpp>
#include <scwx/util/logger.hpp>
#include <imgui.h>
@ -54,6 +55,28 @@ QVariant ImGuiContextModel::data(const QModelIndex& index, int role) const
return QString("%1: %2")
.arg(p->contexts_[row].id_)
.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 {};
@ -107,11 +130,6 @@ void ImGuiContextModel::DestroyContext(const std::string& name)
}
}
std::vector<ImGuiContextInfo> ImGuiContextModel::contexts() const
{
return p->contexts_;
}
ImGuiContextModel& ImGuiContextModel::Instance()
{
static ImGuiContextModel instance_ {};

View file

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

View file

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

View file

@ -2,6 +2,7 @@
#include "ui_imgui_debug_dialog.h"
#include <scwx/qt/model/imgui_context_model.hpp>
#include <scwx/qt/types/qt_types.hpp>
#include <scwx/qt/ui/imgui_debug_widget.hpp>
namespace scwx
@ -36,7 +37,26 @@ ImGuiDebugDialog::ImGuiDebugDialog(QWidget* parent) :
ui->verticalLayout->insertWidget(0, p->imGuiDebugWidget_);
// 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()

View file

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

View file

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