Inital code for per map layer ImGui contexts

This commit is contained in:
AdenKoperczak 2025-01-04 13:49:31 -05:00
parent e641c0b0e5
commit 6d107f6c2d
7 changed files with 153 additions and 15 deletions

View file

@ -1,7 +1,14 @@
#include <scwx/qt/manager/font_manager.hpp>
#include <scwx/qt/map/draw_layer.hpp>
#include <scwx/qt/model/imgui_context_model.hpp>
#include <scwx/qt/gl/shader_program.hpp>
#include <scwx/util/logger.hpp>
#include <backends/imgui_impl_opengl3.h>
#include <backends/imgui_impl_qt.hpp>
#include <fmt/format.h>
#include <imgui.h>
namespace scwx
{
namespace qt
@ -16,18 +23,65 @@ class DrawLayerImpl
{
public:
explicit DrawLayerImpl(std::shared_ptr<MapContext> context) :
context_ {context}, drawList_ {}, textureAtlas_ {GL_INVALID_INDEX}
context_ {context},
drawList_ {},
textureAtlas_ {GL_INVALID_INDEX},
imGuiRendererInitialized_ {false}
{
static size_t currentMapId_ {0u};
imGuiContextName_ = fmt::format("Layer {}", ++currentMapId_);
imGuiContext_ =
model::ImGuiContextModel::Instance().CreateContext(imGuiContextName_);
// Initialize ImGui Qt backend
ImGui_ImplQt_Init();
}
~DrawLayerImpl() {}
~DrawLayerImpl()
{
// Set ImGui Context
ImGui::SetCurrentContext(imGuiContext_);
// Shutdown ImGui Context
if (imGuiRendererInitialized_)
{
ImGui_ImplOpenGL3_Shutdown();
}
ImGui_ImplQt_Shutdown();
// Destroy ImGui Context
model::ImGuiContextModel::Instance().DestroyContext(imGuiContextName_);
}
void ImGuiCheckFonts();
std::shared_ptr<MapContext> context_;
std::vector<std::shared_ptr<gl::draw::DrawItem>> drawList_;
GLuint textureAtlas_;
std::uint64_t textureAtlasBuildCount_ {};
std::string imGuiContextName_;
ImGuiContext* imGuiContext_;
bool imGuiRendererInitialized_;
std::uint64_t imGuiFontsBuildCount_ {};
};
void DrawLayerImpl::ImGuiCheckFonts()
{
// Update ImGui Fonts if required
std::uint64_t currentImGuiFontsBuildCount =
manager::FontManager::Instance().imgui_fonts_build_count();
if (imGuiFontsBuildCount_ != currentImGuiFontsBuildCount ||
!model::ImGuiContextModel::Instance().font_atlas()->IsBuilt())
{
ImGui_ImplOpenGL3_DestroyFontsTexture();
ImGui_ImplOpenGL3_CreateFontsTexture();
}
imGuiFontsBuildCount_ = currentImGuiFontsBuildCount;
}
DrawLayer::DrawLayer(const std::shared_ptr<MapContext>& context) :
GenericLayer(context), p(std::make_unique<DrawLayerImpl>(context))
{
@ -42,9 +96,48 @@ void DrawLayer::Initialize()
{
item->Initialize();
}
ImGuiInitialize();
}
void DrawLayer::Render(const QMapLibre::CustomLayerRenderParameters& params)
void DrawLayer::StartImGuiFrame()
{
auto defaultFont = manager::FontManager::Instance().GetImGuiFont(
types::FontCategory::Default);
// Setup ImGui Frame
ImGui::SetCurrentContext(p->imGuiContext_);
// Start ImGui Frame
ImGui_ImplQt_NewFrame(p->context_->widget());
ImGui_ImplOpenGL3_NewFrame();
p->ImGuiCheckFonts();
ImGui::NewFrame();
ImGui::PushFont(defaultFont->font());
}
void DrawLayer::EndImGuiFrame()
{
// Pop default font
ImGui::PopFont();
// Render ImGui Frame
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
void DrawLayer::ImGuiInitialize()
{
ImGui::SetCurrentContext(p->imGuiContext_);
ImGui_ImplQt_RegisterWidget(p->context_->widget());
ImGui_ImplOpenGL3_Init();
p->imGuiFontsBuildCount_ =
manager::FontManager::Instance().imgui_fonts_build_count();
p->imGuiRendererInitialized_ = true;
}
void DrawLayer::RenderWithoutImGui(
const QMapLibre::CustomLayerRenderParameters& params)
{
gl::OpenGLFunctions& gl = p->context_->gl();
p->textureAtlas_ = p->context_->GetTextureAtlas();
@ -69,6 +162,13 @@ void DrawLayer::Render(const QMapLibre::CustomLayerRenderParameters& params)
p->textureAtlasBuildCount_ = newTextureAtlasBuildCount;
}
void DrawLayer::Render(const QMapLibre::CustomLayerRenderParameters& params)
{
StartImGuiFrame();
RenderWithoutImGui(params);
EndImGuiFrame();
}
void DrawLayer::Deinitialize()
{
p->textureAtlas_ = GL_INVALID_INDEX;

View file

@ -32,6 +32,11 @@ public:
protected:
void AddDrawItem(const std::shared_ptr<gl::draw::DrawItem>& drawItem);
void StartImGuiFrame();
void EndImGuiFrame();
void ImGuiInitialize();
void
RenderWithoutImGui(const QMapLibre::CustomLayerRenderParameters& params);
private:
std::unique_ptr<DrawLayerImpl> p;

View file

@ -36,6 +36,8 @@ public:
std::shared_ptr<view::OverlayProductView> overlayProductView_ {nullptr};
std::shared_ptr<view::RadarProductView> radarProductView_;
QWidget* widget_;
};
MapContext::MapContext(
@ -109,6 +111,19 @@ int16_t MapContext::radar_product_code() const
return p->radarProductCode_;
}
<<<<<<< HEAD
=======
QMapLibre::CustomLayerRenderParameters MapContext::render_parameters() const
{
return p->renderParameters_;
}
QWidget* MapContext::widget() const
{
return p->widget_;
}
>>>>>>> 513a41d3 (Inital code for per map layer ImGui contexts)
void MapContext::set_map(const std::shared_ptr<QMapLibre::Map>& map)
{
p->map_ = map;
@ -167,6 +182,11 @@ void MapContext::set_radar_product_code(int16_t radarProductCode)
p->radarProductCode_ = radarProductCode;
}
void MapContext::set_widget(QWidget* widget)
{
p->widget_ = widget;
}
} // namespace map
} // namespace qt
} // namespace scwx

View file

@ -50,6 +50,7 @@ public:
common::RadarProductGroup radar_product_group() const;
std::string radar_product() const;
int16_t radar_product_code() const;
QWidget* widget() const;
void set_map(const std::shared_ptr<QMapLibre::Map>& map);
void set_map_copyrights(const std::string& copyrights);
@ -64,6 +65,7 @@ public:
void set_radar_product_group(common::RadarProductGroup radarProductGroup);
void set_radar_product(const std::string& radarProduct);
void set_radar_product_code(int16_t radarProductCode);
void set_widget(QWidget* widget);
private:
class Impl;

View file

@ -114,6 +114,7 @@ public:
context_->set_map_provider(
GetMapProvider(generalSettings.map_provider().GetValue()));
context_->set_overlay_product_view(overlayProductView);
context_->set_widget(widget);
// Initialize map data
SetRadarSite(generalSettings.default_radar_site().GetValue());
@ -1571,21 +1572,10 @@ void MapWidget::paintGL()
// Handle hotkey updates
p->HandleHotkeyUpdates();
// Setup ImGui Frame
ImGui::SetCurrentContext(p->imGuiContext_);
// Lock ImGui font atlas prior to new ImGui frame
std::shared_lock imguiFontAtlasLock {
manager::FontManager::Instance().imgui_font_atlas_mutex()};
// Start ImGui Frame
ImGui_ImplQt_NewFrame(this);
ImGui_ImplOpenGL3_NewFrame();
p->ImGuiCheckFonts();
ImGui::NewFrame();
// Set default font
ImGui::PushFont(defaultFont->font());
// Update pixel ratio
p->context_->set_pixel_ratio(pixelRatio());
@ -1596,6 +1586,19 @@ void MapWidget::paintGL()
size() * pixelRatio());
p->map_->render();
// ImGui tool tip code
// Setup ImGui Frame
ImGui::SetCurrentContext(p->imGuiContext_);
// Start ImGui Frame
ImGui_ImplQt_NewFrame(this);
ImGui_ImplOpenGL3_NewFrame();
p->ImGuiCheckFonts();
ImGui::NewFrame();
// Set default font
ImGui::PushFont(defaultFont->font());
// Perform mouse picking
if (p->hasMouse_)
{

View file

@ -292,6 +292,8 @@ void OverlayLayer::Render(const QMapLibre::CustomLayerRenderParameters& params)
auto& settings = context()->settings();
const float pixelRatio = context()->pixel_ratio();
StartImGuiFrame();
p->sweepTimePicked_ = false;
if (radarProductView != nullptr)
@ -457,7 +459,7 @@ void OverlayLayer::Render(const QMapLibre::CustomLayerRenderParameters& params)
p->icons_->SetIconVisible(p->mapLogoIcon_,
generalSettings.show_map_logo().GetValue());
DrawLayer::Render(params);
DrawLayer::RenderWithoutImGui(params);
auto mapCopyrights = context()->map_copyrights();
if (mapCopyrights.length() > 0 &&
@ -491,6 +493,8 @@ void OverlayLayer::Render(const QMapLibre::CustomLayerRenderParameters& params)
p->lastFontSize_ = ImGui::GetFontSize();
p->lastColorTableMargins_ = colorTableMargins;
EndImGuiFrame();
SCWX_GL_CHECK_ERROR();
}

View file

@ -55,6 +55,8 @@ void RadarSiteLayer::Initialize()
logger_->debug("Initialize()");
p->radarSites_ = config::RadarSite::GetAll();
ImGuiInitialize();
}
void RadarSiteLayer::Render(
@ -84,6 +86,7 @@ void RadarSiteLayer::Render(
p->halfWidth_ = params.width * 0.5f;
p->halfHeight_ = params.height * 0.5f;
StartImGuiFrame();
// Radar site ImGui windows shouldn't have padding
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2 {0.0f, 0.0f});
@ -93,6 +96,7 @@ void RadarSiteLayer::Render(
}
ImGui::PopStyleVar();
EndImGuiFrame();
SCWX_GL_CHECK_ERROR();
}