ImGui unique contexts

This commit is contained in:
Dan Paulat 2022-11-26 21:46:57 -06:00
parent 2347eff04d
commit fef3c597d0
4 changed files with 89 additions and 10 deletions

View file

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>570</width>
<height>760</height>
<width>592</width>
<height>754</height>
</rect>
</property>
<property name="windowTitle">

View file

@ -16,33 +16,77 @@ static const std::string logPrefix_ = "scwx::qt::ui::imgui_debug_widget";
class ImGuiDebugWidgetImpl
{
public:
explicit ImGuiDebugWidgetImpl(ImGuiDebugWidget* self) : self_ {self}
explicit ImGuiDebugWidgetImpl(ImGuiDebugWidget* self) :
self_ {self}, context_ {ImGui::CreateContext()}
{
// Set ImGui Context
ImGui::SetCurrentContext(context_);
// ImGui Configuration
auto& io = ImGui::GetIO();
// Initialize Qt backend
// Disable automatic configuration loading/saving
io.IniFilename = nullptr;
// Initialize ImGui Qt backend
ImGui_ImplQt_Init();
ImGui_ImplQt_RegisterWidget(self_);
}
~ImGuiDebugWidgetImpl() {}
~ImGuiDebugWidgetImpl()
{
// Set ImGui Context
ImGui::SetCurrentContext(context_);
// Shutdown ImGui Context
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplQt_Shutdown();
ImGui::DestroyContext(context_);
}
ImGuiDebugWidget* self_;
ImGuiContext* context_;
bool firstRender_ {true};
};
ImGuiDebugWidget::ImGuiDebugWidget(QWidget* parent) :
QOpenGLWidget(parent), p {std::make_unique<ImGuiDebugWidgetImpl>(this)}
{
// Accept focus for keyboard events
setFocusPolicy(Qt::StrongFocus);
}
void ImGuiDebugWidget::initializeGL() {}
void ImGuiDebugWidget::initializeGL()
{
makeCurrent();
// Initialize ImGui OpenGL3 backend
ImGui::SetCurrentContext(p->context_);
ImGui_ImplOpenGL3_Init();
}
void ImGuiDebugWidget::paintGL()
{
ImGui::SetCurrentContext(p->context_);
ImGui_ImplQt_NewFrame(this);
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
if (p->firstRender_)
{
// Set initial position of demo window
ImGui::SetNextWindowPos(ImVec2 {width() / 2.0f, height() / 2.0f},
ImGuiCond_FirstUseEver,
ImVec2 {0.5f, 0.5f});
ImGui::Begin("Dear ImGui Demo");
ImGui::End();
p->firstRender_ = false;
}
ImGui::ShowDemoWindow();
ImGui::Render();