mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 06:10:04 +00:00 
			
		
		
		
	ImGui unique contexts
This commit is contained in:
		
							parent
							
								
									2347eff04d
								
							
						
					
					
						commit
						fef3c597d0
					
				
					 4 changed files with 89 additions and 10 deletions
				
			
		|  | @ -27,6 +27,7 @@ static void LoadTextures(); | ||||||
| static void ShutdownImGui(); | static void ShutdownImGui(); | ||||||
| 
 | 
 | ||||||
| static std::unique_ptr<QOffscreenSurface> surface_ {}; | static std::unique_ptr<QOffscreenSurface> surface_ {}; | ||||||
|  | static ImGuiContext*                      imGuiContext_ {}; | ||||||
| 
 | 
 | ||||||
| void Initialize() | void Initialize() | ||||||
| { | { | ||||||
|  | @ -53,7 +54,7 @@ static void InitializeImGui() | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    // Initialize ImGui
 |    // Initialize ImGui
 | ||||||
|    ImGui::CreateContext(); |    imGuiContext_ = ImGui::CreateContext(); | ||||||
|    ImGui_ImplQt_Init(); |    ImGui_ImplQt_Init(); | ||||||
|    ImGui_ImplOpenGL3_Init(); |    ImGui_ImplOpenGL3_Init(); | ||||||
|    QOpenGLContext::globalShareContext()->doneCurrent(); |    QOpenGLContext::globalShareContext()->doneCurrent(); | ||||||
|  | @ -89,6 +90,7 @@ static void LoadTextures() | ||||||
| static void ShutdownImGui() | static void ShutdownImGui() | ||||||
| { | { | ||||||
|    QOpenGLContext::globalShareContext()->makeCurrent(surface_.get()); |    QOpenGLContext::globalShareContext()->makeCurrent(surface_.get()); | ||||||
|  |    ImGui::SetCurrentContext(imGuiContext_); | ||||||
|    ImGui_ImplOpenGL3_Shutdown(); |    ImGui_ImplOpenGL3_Shutdown(); | ||||||
|    ImGui_ImplQt_Shutdown(); |    ImGui_ImplQt_Shutdown(); | ||||||
|    ImGui::DestroyContext(); |    ImGui::DestroyContext(); | ||||||
|  |  | ||||||
|  | @ -14,6 +14,7 @@ | ||||||
| #include <scwx/util/time.hpp> | #include <scwx/util/time.hpp> | ||||||
| 
 | 
 | ||||||
| #include <backends/imgui_impl_opengl3.h> | #include <backends/imgui_impl_opengl3.h> | ||||||
|  | #include <backends/imgui_impl_qt.hpp> | ||||||
| #include <imgui.h> | #include <imgui.h> | ||||||
| #include <QApplication> | #include <QApplication> | ||||||
| #include <QColor> | #include <QColor> | ||||||
|  | @ -60,6 +61,7 @@ public: | ||||||
|        settings_(settings), |        settings_(settings), | ||||||
|        map_(), |        map_(), | ||||||
|        layerList_ {}, |        layerList_ {}, | ||||||
|  |        imGuiContext_ {ImGui::CreateContext()}, | ||||||
|        radarProductManager_ {nullptr}, |        radarProductManager_ {nullptr}, | ||||||
|        radarProductLayer_ {nullptr}, |        radarProductLayer_ {nullptr}, | ||||||
|        alertLayer_ {std::make_shared<AlertLayer>(context_)}, |        alertLayer_ {std::make_shared<AlertLayer>(context_)}, | ||||||
|  | @ -79,8 +81,31 @@ public: | ||||||
|    { |    { | ||||||
|       SetRadarSite(scwx::qt::manager::SettingsManager::general_settings() |       SetRadarSite(scwx::qt::manager::SettingsManager::general_settings() | ||||||
|                       ->default_radar_site()); |                       ->default_radar_site()); | ||||||
|  | 
 | ||||||
|  |       // Set ImGui Context
 | ||||||
|  |       ImGui::SetCurrentContext(imGuiContext_); | ||||||
|  | 
 | ||||||
|  |       // ImGui Configuration
 | ||||||
|  |       auto& io = ImGui::GetIO(); | ||||||
|  | 
 | ||||||
|  |       // Disable automatic configuration loading/saving
 | ||||||
|  |       io.IniFilename = nullptr; | ||||||
|  | 
 | ||||||
|  |       // Initialize ImGui Qt backend
 | ||||||
|  |       ImGui_ImplQt_Init(); | ||||||
|  |       ImGui_ImplQt_RegisterWidget(widget_); | ||||||
|  |    } | ||||||
|  | 
 | ||||||
|  |    ~MapWidgetImpl() | ||||||
|  |    { | ||||||
|  |       // Set ImGui Context
 | ||||||
|  |       ImGui::SetCurrentContext(imGuiContext_); | ||||||
|  | 
 | ||||||
|  |       // Shutdown ImGui Context
 | ||||||
|  |       ImGui_ImplOpenGL3_Shutdown(); | ||||||
|  |       ImGui_ImplQt_Shutdown(); | ||||||
|  |       ImGui::DestroyContext(imGuiContext_); | ||||||
|    } |    } | ||||||
|    ~MapWidgetImpl() = default; |  | ||||||
| 
 | 
 | ||||||
|    void AddLayer(const std::string&            id, |    void AddLayer(const std::string&            id, | ||||||
|                  std::shared_ptr<GenericLayer> layer, |                  std::shared_ptr<GenericLayer> layer, | ||||||
|  | @ -103,6 +128,8 @@ public: | ||||||
|    std::shared_ptr<QMapLibreGL::Map> map_; |    std::shared_ptr<QMapLibreGL::Map> map_; | ||||||
|    std::list<std::string>            layerList_; |    std::list<std::string>            layerList_; | ||||||
| 
 | 
 | ||||||
|  |    ImGuiContext* imGuiContext_; | ||||||
|  | 
 | ||||||
|    std::shared_ptr<manager::RadarProductManager> radarProductManager_; |    std::shared_ptr<manager::RadarProductManager> radarProductManager_; | ||||||
| 
 | 
 | ||||||
|    std::shared_ptr<common::ColorTable> colorTable_; |    std::shared_ptr<common::ColorTable> colorTable_; | ||||||
|  | @ -136,6 +163,8 @@ MapWidget::MapWidget(const QMapLibreGL::Settings& settings) : | ||||||
|     p(std::make_unique<MapWidgetImpl>(this, settings)) |     p(std::make_unique<MapWidgetImpl>(this, settings)) | ||||||
| { | { | ||||||
|    setFocusPolicy(Qt::StrongFocus); |    setFocusPolicy(Qt::StrongFocus); | ||||||
|  | 
 | ||||||
|  |    ImGui_ImplQt_RegisterWidget(this); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| MapWidget::~MapWidget() | MapWidget::~MapWidget() | ||||||
|  | @ -644,6 +673,10 @@ void MapWidget::initializeGL() | ||||||
|    makeCurrent(); |    makeCurrent(); | ||||||
|    p->context_->gl().initializeOpenGLFunctions(); |    p->context_->gl().initializeOpenGLFunctions(); | ||||||
| 
 | 
 | ||||||
|  |    // Initialize ImGui OpenGL3 backend
 | ||||||
|  |    ImGui::SetCurrentContext(p->imGuiContext_); | ||||||
|  |    ImGui_ImplOpenGL3_Init(); | ||||||
|  | 
 | ||||||
|    p->map_.reset( |    p->map_.reset( | ||||||
|       new QMapLibreGL::Map(nullptr, p->settings_, size(), pixelRatio())); |       new QMapLibreGL::Map(nullptr, p->settings_, size(), pixelRatio())); | ||||||
|    p->context_->set_map(p->map_); |    p->context_->set_map(p->map_); | ||||||
|  | @ -686,10 +719,10 @@ void MapWidget::paintGL() | ||||||
|    p->frameDraws_++; |    p->frameDraws_++; | ||||||
| 
 | 
 | ||||||
|    // Setup ImGui Frame
 |    // Setup ImGui Frame
 | ||||||
|    ImGui::GetIO().DisplaySize = {static_cast<float>(size().width()), |    ImGui::SetCurrentContext(p->imGuiContext_); | ||||||
|                                  static_cast<float>(size().height())}; |  | ||||||
| 
 | 
 | ||||||
|    // Start ImGui Frame
 |    // Start ImGui Frame
 | ||||||
|  |    ImGui_ImplQt_NewFrame(this); | ||||||
|    ImGui_ImplOpenGL3_NewFrame(); |    ImGui_ImplOpenGL3_NewFrame(); | ||||||
|    ImGui::NewFrame(); |    ImGui::NewFrame(); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -6,8 +6,8 @@ | ||||||
|    <rect> |    <rect> | ||||||
|     <x>0</x> |     <x>0</x> | ||||||
|     <y>0</y> |     <y>0</y> | ||||||
|     <width>570</width> |     <width>592</width> | ||||||
|     <height>760</height> |     <height>754</height> | ||||||
|    </rect> |    </rect> | ||||||
|   </property> |   </property> | ||||||
|   <property name="windowTitle"> |   <property name="windowTitle"> | ||||||
|  |  | ||||||
|  | @ -16,33 +16,77 @@ static const std::string logPrefix_ = "scwx::qt::ui::imgui_debug_widget"; | ||||||
| class ImGuiDebugWidgetImpl | class ImGuiDebugWidgetImpl | ||||||
| { | { | ||||||
| public: | public: | ||||||
|    explicit ImGuiDebugWidgetImpl(ImGuiDebugWidget* self) : self_ {self} |    explicit ImGuiDebugWidgetImpl(ImGuiDebugWidget* self) : | ||||||
|  |        self_ {self}, context_ {ImGui::CreateContext()} | ||||||
|    { |    { | ||||||
|  |       // Set ImGui Context
 | ||||||
|  |       ImGui::SetCurrentContext(context_); | ||||||
|  | 
 | ||||||
|       // ImGui Configuration
 |       // ImGui Configuration
 | ||||||
|       auto& io = ImGui::GetIO(); |       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_); |       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_; |    ImGuiDebugWidget* self_; | ||||||
|  |    ImGuiContext*     context_; | ||||||
|  | 
 | ||||||
|  |    bool firstRender_ {true}; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| ImGuiDebugWidget::ImGuiDebugWidget(QWidget* parent) : | ImGuiDebugWidget::ImGuiDebugWidget(QWidget* parent) : | ||||||
|     QOpenGLWidget(parent), p {std::make_unique<ImGuiDebugWidgetImpl>(this)} |     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() | void ImGuiDebugWidget::paintGL() | ||||||
| { | { | ||||||
|  |    ImGui::SetCurrentContext(p->context_); | ||||||
|  | 
 | ||||||
|    ImGui_ImplQt_NewFrame(this); |    ImGui_ImplQt_NewFrame(this); | ||||||
|    ImGui_ImplOpenGL3_NewFrame(); |    ImGui_ImplOpenGL3_NewFrame(); | ||||||
| 
 | 
 | ||||||
|    ImGui::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::ShowDemoWindow(); | ||||||
| 
 | 
 | ||||||
|    ImGui::Render(); |    ImGui::Render(); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat