mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 15:40:06 +00:00 
			
		
		
		
	Use ImGui Manager to create contexts
This commit is contained in:
		
							parent
							
								
									ee2ec329da
								
							
						
					
					
						commit
						0f8b8d73f9
					
				
					 3 changed files with 37 additions and 80 deletions
				
			
		|  | @ -4,12 +4,6 @@ | ||||||
| #include <scwx/qt/util/texture_atlas.hpp> | #include <scwx/qt/util/texture_atlas.hpp> | ||||||
| #include <scwx/util/logger.hpp> | #include <scwx/util/logger.hpp> | ||||||
| 
 | 
 | ||||||
| #include <backends/imgui_impl_opengl3.h> |  | ||||||
| #include <backends/imgui_impl_qt.hpp> |  | ||||||
| #include <imgui.h> |  | ||||||
| #include <QOffscreenSurface> |  | ||||||
| #include <QOpenGLContext> |  | ||||||
| 
 |  | ||||||
| namespace scwx | namespace scwx | ||||||
| { | { | ||||||
| namespace qt | namespace qt | ||||||
|  | @ -18,58 +12,21 @@ namespace manager | ||||||
| { | { | ||||||
| namespace ResourceManager | namespace ResourceManager | ||||||
| { | { | ||||||
| static const std::string logPrefix_ = "scwx::qt::manager::ResourceManager"; | static const std::string logPrefix_ = "scwx::qt::manager::resource_manager"; | ||||||
| static const auto        logger_    = scwx::util::Logger::Create(logPrefix_); | static const auto        logger_    = scwx::util::Logger::Create(logPrefix_); | ||||||
| 
 | 
 | ||||||
| static void InitializeImGui(); |  | ||||||
| static void LoadFonts(); | static void LoadFonts(); | ||||||
| static void LoadTextures(); | static void LoadTextures(); | ||||||
| static void ShutdownImGui(); |  | ||||||
| 
 |  | ||||||
| static std::unique_ptr<QOffscreenSurface> surface_ {}; |  | ||||||
| static ImGuiContext*                      imGuiContext_ {}; |  | ||||||
| 
 | 
 | ||||||
| void Initialize() | void Initialize() | ||||||
| { | { | ||||||
|    config::CountyDatabase::Initialize(); |    config::CountyDatabase::Initialize(); | ||||||
| 
 | 
 | ||||||
|    InitializeImGui(); |  | ||||||
|    LoadFonts(); |    LoadFonts(); | ||||||
|    LoadTextures(); |    LoadTextures(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void Shutdown() | void Shutdown() {} | ||||||
| { |  | ||||||
|    ShutdownImGui(); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| static void InitializeImGui() |  | ||||||
| { |  | ||||||
|    // Create OpenGL Offscreen Surface
 |  | ||||||
|    surface_ = std::make_unique<QOffscreenSurface>(); |  | ||||||
|    surface_->create(); |  | ||||||
|    if (!QOpenGLContext::globalShareContext()->makeCurrent(surface_.get())) |  | ||||||
|    { |  | ||||||
|       logger_->warn("Failed to initialize offscreen surface"); |  | ||||||
|    } |  | ||||||
| 
 |  | ||||||
|    // Initialize ImGui
 |  | ||||||
|    imGuiContext_ = ImGui::CreateContext(); |  | ||||||
|    ImGui_ImplQt_Init(); |  | ||||||
|    ImGui_ImplOpenGL3_Init(); |  | ||||||
|    QOpenGLContext::globalShareContext()->doneCurrent(); |  | ||||||
| 
 |  | ||||||
|    // ImGui Configuration
 |  | ||||||
|    auto& io = ImGui::GetIO(); |  | ||||||
| 
 |  | ||||||
|    // Disable automatic configuration loading/saving
 |  | ||||||
|    io.IniFilename = nullptr; |  | ||||||
| 
 |  | ||||||
|    // Style
 |  | ||||||
|    auto& style = ImGui::GetStyle(); |  | ||||||
| 
 |  | ||||||
|    style.WindowMinSize = {10.0f, 10.0f}; |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| static void LoadFonts() | static void LoadFonts() | ||||||
| { | { | ||||||
|  | @ -87,16 +44,6 @@ static void LoadTextures() | ||||||
|    textureAtlas.BuildAtlas(8, 8); |    textureAtlas.BuildAtlas(8, 8); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static void ShutdownImGui() |  | ||||||
| { |  | ||||||
|    QOpenGLContext::globalShareContext()->makeCurrent(surface_.get()); |  | ||||||
|    ImGui::SetCurrentContext(imGuiContext_); |  | ||||||
|    ImGui_ImplOpenGL3_Shutdown(); |  | ||||||
|    ImGui_ImplQt_Shutdown(); |  | ||||||
|    ImGui::DestroyContext(); |  | ||||||
|    QOpenGLContext::globalShareContext()->doneCurrent(); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| } // namespace ResourceManager
 | } // namespace ResourceManager
 | ||||||
| } // namespace manager
 | } // namespace manager
 | ||||||
| } // namespace qt
 | } // namespace qt
 | ||||||
|  |  | ||||||
|  | @ -1,5 +1,6 @@ | ||||||
| #include <scwx/qt/map/map_widget.hpp> | #include <scwx/qt/map/map_widget.hpp> | ||||||
| #include <scwx/qt/gl/gl.hpp> | #include <scwx/qt/gl/gl.hpp> | ||||||
|  | #include <scwx/qt/manager/imgui_manager.hpp> | ||||||
| #include <scwx/qt/manager/radar_product_manager.hpp> | #include <scwx/qt/manager/radar_product_manager.hpp> | ||||||
| #include <scwx/qt/manager/settings_manager.hpp> | #include <scwx/qt/manager/settings_manager.hpp> | ||||||
| #include <scwx/qt/map/alert_layer.hpp> | #include <scwx/qt/map/alert_layer.hpp> | ||||||
|  | @ -61,7 +62,7 @@ public: | ||||||
|        settings_(settings), |        settings_(settings), | ||||||
|        map_(), |        map_(), | ||||||
|        layerList_ {}, |        layerList_ {}, | ||||||
|        imGuiContext_ {ImGui::CreateContext()}, |        imGuiRendererInitialized_ {false}, | ||||||
|        radarProductManager_ {nullptr}, |        radarProductManager_ {nullptr}, | ||||||
|        radarProductLayer_ {nullptr}, |        radarProductLayer_ {nullptr}, | ||||||
|        alertLayer_ {std::make_shared<AlertLayer>(context_)}, |        alertLayer_ {std::make_shared<AlertLayer>(context_)}, | ||||||
|  | @ -82,14 +83,11 @@ public: | ||||||
|       SetRadarSite(scwx::qt::manager::SettingsManager::general_settings() |       SetRadarSite(scwx::qt::manager::SettingsManager::general_settings() | ||||||
|                       ->default_radar_site()); |                       ->default_radar_site()); | ||||||
| 
 | 
 | ||||||
|       // Set ImGui Context
 |       // Create ImGui Context
 | ||||||
|       ImGui::SetCurrentContext(imGuiContext_); |       static size_t currentMapId_ {0u}; | ||||||
| 
 |       imGuiContextName_ = std::format("Map {}", ++currentMapId_); | ||||||
|       // ImGui Configuration
 |       imGuiContext_ = | ||||||
|       auto& io = ImGui::GetIO(); |          manager::ImGuiManager::Instance().CreateContext(imGuiContextName_); | ||||||
| 
 |  | ||||||
|       // Disable automatic configuration loading/saving
 |  | ||||||
|       io.IniFilename = nullptr; |  | ||||||
| 
 | 
 | ||||||
|       // Initialize ImGui Qt backend
 |       // Initialize ImGui Qt backend
 | ||||||
|       ImGui_ImplQt_Init(); |       ImGui_ImplQt_Init(); | ||||||
|  | @ -102,9 +100,14 @@ public: | ||||||
|       ImGui::SetCurrentContext(imGuiContext_); |       ImGui::SetCurrentContext(imGuiContext_); | ||||||
| 
 | 
 | ||||||
|       // Shutdown ImGui Context
 |       // Shutdown ImGui Context
 | ||||||
|       ImGui_ImplOpenGL3_Shutdown(); |       if (imGuiRendererInitialized_) | ||||||
|  |       { | ||||||
|  |          ImGui_ImplOpenGL3_Shutdown(); | ||||||
|  |       } | ||||||
|       ImGui_ImplQt_Shutdown(); |       ImGui_ImplQt_Shutdown(); | ||||||
|       ImGui::DestroyContext(imGuiContext_); | 
 | ||||||
|  |       // Destroy ImGui Context
 | ||||||
|  |       manager::ImGuiManager::Instance().DestroyContext(imGuiContextName_); | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    void AddLayer(const std::string&            id, |    void AddLayer(const std::string&            id, | ||||||
|  | @ -129,6 +132,8 @@ public: | ||||||
|    std::list<std::string>            layerList_; |    std::list<std::string>            layerList_; | ||||||
| 
 | 
 | ||||||
|    ImGuiContext* imGuiContext_; |    ImGuiContext* imGuiContext_; | ||||||
|  |    std::string   imGuiContextName_; | ||||||
|  |    bool          imGuiRendererInitialized_; | ||||||
| 
 | 
 | ||||||
|    std::shared_ptr<manager::RadarProductManager> radarProductManager_; |    std::shared_ptr<manager::RadarProductManager> radarProductManager_; | ||||||
| 
 | 
 | ||||||
|  | @ -676,6 +681,7 @@ void MapWidget::initializeGL() | ||||||
|    // Initialize ImGui OpenGL3 backend
 |    // Initialize ImGui OpenGL3 backend
 | ||||||
|    ImGui::SetCurrentContext(p->imGuiContext_); |    ImGui::SetCurrentContext(p->imGuiContext_); | ||||||
|    ImGui_ImplOpenGL3_Init(); |    ImGui_ImplOpenGL3_Init(); | ||||||
|  |    p->imGuiRendererInitialized_ = true; | ||||||
| 
 | 
 | ||||||
|    p->map_.reset( |    p->map_.reset( | ||||||
|       new QMapLibreGL::Map(nullptr, p->settings_, size(), pixelRatio())); |       new QMapLibreGL::Map(nullptr, p->settings_, size(), pixelRatio())); | ||||||
|  |  | ||||||
|  | @ -1,4 +1,5 @@ | ||||||
| #include <scwx/qt/ui/imgui_debug_widget.hpp> | #include <scwx/qt/ui/imgui_debug_widget.hpp> | ||||||
|  | #include <scwx/qt/manager/imgui_manager.hpp> | ||||||
| 
 | 
 | ||||||
| #include <imgui.h> | #include <imgui.h> | ||||||
| #include <backends/imgui_impl_opengl3.h> | #include <backends/imgui_impl_opengl3.h> | ||||||
|  | @ -16,17 +17,12 @@ static const std::string logPrefix_ = "scwx::qt::ui::imgui_debug_widget"; | ||||||
| class ImGuiDebugWidgetImpl | class ImGuiDebugWidgetImpl | ||||||
| { | { | ||||||
| public: | public: | ||||||
|    explicit ImGuiDebugWidgetImpl(ImGuiDebugWidget* self) : |    explicit ImGuiDebugWidgetImpl(ImGuiDebugWidget* self) : self_ {self} | ||||||
|        self_ {self}, context_ {ImGui::CreateContext()} |  | ||||||
|    { |    { | ||||||
|       // Set ImGui Context
 |       // Create ImGui Context
 | ||||||
|       ImGui::SetCurrentContext(context_); |       static size_t currentIndex_ {0u}; | ||||||
| 
 |       contextName_ = std::format("ImGui Debug {}", ++currentIndex_); | ||||||
|       // ImGui Configuration
 |       context_ = manager::ImGuiManager::Instance().CreateContext(contextName_); | ||||||
|       auto& io = ImGui::GetIO(); |  | ||||||
| 
 |  | ||||||
|       // Disable automatic configuration loading/saving
 |  | ||||||
|       io.IniFilename = nullptr; |  | ||||||
| 
 | 
 | ||||||
|       // Initialize ImGui Qt backend
 |       // Initialize ImGui Qt backend
 | ||||||
|       ImGui_ImplQt_Init(); |       ImGui_ImplQt_Init(); | ||||||
|  | @ -39,15 +35,22 @@ public: | ||||||
|       ImGui::SetCurrentContext(context_); |       ImGui::SetCurrentContext(context_); | ||||||
| 
 | 
 | ||||||
|       // Shutdown ImGui Context
 |       // Shutdown ImGui Context
 | ||||||
|       ImGui_ImplOpenGL3_Shutdown(); |       if (imGuiRendererInitialized_) | ||||||
|  |       { | ||||||
|  |          ImGui_ImplOpenGL3_Shutdown(); | ||||||
|  |       } | ||||||
|       ImGui_ImplQt_Shutdown(); |       ImGui_ImplQt_Shutdown(); | ||||||
|       ImGui::DestroyContext(context_); | 
 | ||||||
|  |       // Destroy ImGui Context
 | ||||||
|  |       manager::ImGuiManager::Instance().DestroyContext(contextName_); | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    ImGuiDebugWidget* self_; |    ImGuiDebugWidget* self_; | ||||||
|    ImGuiContext*     context_; |    ImGuiContext*     context_; | ||||||
|  |    std::string       contextName_; | ||||||
| 
 | 
 | ||||||
|    bool firstRender_ {true}; |    bool firstRender_ {true}; | ||||||
|  |    bool imGuiRendererInitialized_ {false}; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| ImGuiDebugWidget::ImGuiDebugWidget(QWidget* parent) : | ImGuiDebugWidget::ImGuiDebugWidget(QWidget* parent) : | ||||||
|  | @ -57,6 +60,8 @@ ImGuiDebugWidget::ImGuiDebugWidget(QWidget* parent) : | ||||||
|    setFocusPolicy(Qt::StrongFocus); |    setFocusPolicy(Qt::StrongFocus); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | ImGuiDebugWidget::~ImGuiDebugWidget() {} | ||||||
|  | 
 | ||||||
| void ImGuiDebugWidget::initializeGL() | void ImGuiDebugWidget::initializeGL() | ||||||
| { | { | ||||||
|    makeCurrent(); |    makeCurrent(); | ||||||
|  | @ -64,6 +69,7 @@ void ImGuiDebugWidget::initializeGL() | ||||||
|    // Initialize ImGui OpenGL3 backend
 |    // Initialize ImGui OpenGL3 backend
 | ||||||
|    ImGui::SetCurrentContext(p->context_); |    ImGui::SetCurrentContext(p->context_); | ||||||
|    ImGui_ImplOpenGL3_Init(); |    ImGui_ImplOpenGL3_Init(); | ||||||
|  |    p->imGuiRendererInitialized_ = true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void ImGuiDebugWidget::paintGL() | void ImGuiDebugWidget::paintGL() | ||||||
|  | @ -93,8 +99,6 @@ void ImGuiDebugWidget::paintGL() | ||||||
|    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); |    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| ImGuiDebugWidget::~ImGuiDebugWidget() {} |  | ||||||
| 
 |  | ||||||
| } // namespace ui
 | } // namespace ui
 | ||||||
| } // namespace qt
 | } // namespace qt
 | ||||||
| } // namespace scwx
 | } // namespace scwx
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat