mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 02:40:05 +00:00 
			
		
		
		
	Get ImGui font by font category
This commit is contained in:
		
							parent
							
								
									e4ab1e8c19
								
							
						
					
					
						commit
						acc782b2bc
					
				
					 4 changed files with 60 additions and 10 deletions
				
			
		|  | @ -64,6 +64,8 @@ public: | |||
| 
 | ||||
|    std::shared_mutex imguiFontAtlasMutex_ {}; | ||||
| 
 | ||||
|    std::uint64_t imguiFontsBuildCount_ {}; | ||||
| 
 | ||||
|    boost::unordered_flat_map<FontRecordPair, | ||||
|                              std::shared_ptr<types::ImGuiFont>, | ||||
|                              FontRecordHash<FontRecordPair>> | ||||
|  | @ -73,6 +75,12 @@ public: | |||
|    boost::unordered_flat_map<std::string, std::vector<std::uint8_t>> | ||||
|               rawFontData_ {}; | ||||
|    std::mutex rawFontDataMutex_ {}; | ||||
| 
 | ||||
|    std::shared_ptr<types::ImGuiFont> defaultFont_ {}; | ||||
|    boost::unordered_flat_map<types::FontCategory, | ||||
|                              std::shared_ptr<types::ImGuiFont>> | ||||
|               fontCategoryMap_ {}; | ||||
|    std::mutex fontCategoryMutex_ {}; | ||||
| }; | ||||
| 
 | ||||
| FontManager::FontManager() : p(std::make_unique<Impl>()) {} | ||||
|  | @ -84,11 +92,30 @@ std::shared_mutex& FontManager::imgui_font_atlas_mutex() | |||
|    return p->imguiFontAtlasMutex_; | ||||
| } | ||||
| 
 | ||||
| std::uint64_t FontManager::imgui_fonts_build_count() const | ||||
| { | ||||
|    return p->imguiFontsBuildCount_; | ||||
| } | ||||
| 
 | ||||
| std::shared_ptr<types::ImGuiFont> | ||||
| FontManager::GetImGuiFont(const std::string&               family, | ||||
|                           const std::vector<std::string>&  styles, | ||||
|                           units::font_size::points<double> size, | ||||
|                           bool                             loadIfNotFound) | ||||
| FontManager::GetImGuiFont(types::FontCategory fontCategory) | ||||
| { | ||||
|    std::unique_lock lock {p->fontCategoryMutex_}; | ||||
| 
 | ||||
|    auto it = p->fontCategoryMap_.find(fontCategory); | ||||
|    if (it != p->fontCategoryMap_.cend()) | ||||
|    { | ||||
|       return it->second; | ||||
|    } | ||||
| 
 | ||||
|    return p->defaultFont_; | ||||
| } | ||||
| 
 | ||||
| std::shared_ptr<types::ImGuiFont> | ||||
| FontManager::LoadImGuiFont(const std::string&               family, | ||||
|                            const std::vector<std::string>&  styles, | ||||
|                            units::font_size::points<double> size, | ||||
|                            bool                             loadIfNotFound) | ||||
| { | ||||
|    const std::string styleString = fmt::format("{}", fmt::join(styles, " ")); | ||||
|    const std::string fontString = | ||||
|  | @ -148,6 +175,9 @@ FontManager::GetImGuiFont(const std::string&               family, | |||
|    // Store the ImGui font
 | ||||
|    p->imguiFonts_.insert_or_assign(imguiFontKey, imguiFont); | ||||
| 
 | ||||
|    // Increment ImGui font build count
 | ||||
|    ++p->imguiFontsBuildCount_; | ||||
| 
 | ||||
|    // Return the ImGui font
 | ||||
|    return imguiFont; | ||||
| } | ||||
|  | @ -181,7 +211,7 @@ FontManager::Impl::GetRawFontData(const std::string& filename) | |||
|    auto result = rawFontData_.emplace(filename, std::move(buffer)); | ||||
| 
 | ||||
|    // Return the cached buffer
 | ||||
|    return it->second; | ||||
|    return result.first->second; | ||||
| } | ||||
| 
 | ||||
| void FontManager::LoadApplicationFont(const std::string& filename) | ||||
|  |  | |||
|  | @ -1,6 +1,7 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <scwx/qt/types/imgui_font.hpp> | ||||
| #include <scwx/qt/types/text_types.hpp> | ||||
| 
 | ||||
| #include <shared_mutex> | ||||
| 
 | ||||
|  | @ -22,12 +23,15 @@ public: | |||
|    ~FontManager(); | ||||
| 
 | ||||
|    std::shared_mutex& imgui_font_atlas_mutex(); | ||||
|    std::uint64_t      imgui_fonts_build_count() const; | ||||
| 
 | ||||
|    std::shared_ptr<types::ImGuiFont> | ||||
|    GetImGuiFont(const std::string&               family, | ||||
|                 const std::vector<std::string>&  styles, | ||||
|                 units::font_size::points<double> size, | ||||
|                 bool                             loadIfNotFound = false); | ||||
|    GetImGuiFont(types::FontCategory fontCategory); | ||||
|    std::shared_ptr<types::ImGuiFont> | ||||
|    LoadImGuiFont(const std::string&               family, | ||||
|                  const std::vector<std::string>&  styles, | ||||
|                  units::font_size::points<double> size, | ||||
|                  bool                             loadIfNotFound = true); | ||||
| 
 | ||||
|    void LoadApplicationFont(const std::string& filename); | ||||
| 
 | ||||
|  |  | |||
|  | @ -707,7 +707,7 @@ void PlacefileManager::Impl::LoadFontResources( | |||
|          styles.push_back("italic"); | ||||
|       } | ||||
| 
 | ||||
|       FontManager::Instance().GetImGuiFont(font.second->face_, styles, size); | ||||
|       FontManager::Instance().LoadImGuiFont(font.second->face_, styles, size); | ||||
|    } | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -155,6 +155,7 @@ public: | |||
|    ImGuiContext* imGuiContext_; | ||||
|    std::string   imGuiContextName_; | ||||
|    bool          imGuiRendererInitialized_; | ||||
|    std::uint64_t imGuiFontsBuildCount_ {}; | ||||
| 
 | ||||
|    std::shared_ptr<manager::PlacefileManager> placefileManager_ { | ||||
|       manager::PlacefileManager::Instance()}; | ||||
|  | @ -981,9 +982,15 @@ void MapWidget::initializeGL() | |||
|    makeCurrent(); | ||||
|    p->context_->gl().initializeOpenGLFunctions(); | ||||
| 
 | ||||
|    // Lock ImGui font atlas prior to new ImGui frame
 | ||||
|    std::shared_lock imguiFontAtlasLock { | ||||
|       manager::FontManager::Instance().imgui_font_atlas_mutex()}; | ||||
| 
 | ||||
|    // Initialize ImGui OpenGL3 backend
 | ||||
|    ImGui::SetCurrentContext(p->imGuiContext_); | ||||
|    ImGui_ImplOpenGL3_Init(); | ||||
|    p->imGuiFontsBuildCount_ = | ||||
|       manager::FontManager::Instance().imgui_fonts_build_count(); | ||||
|    p->imGuiRendererInitialized_ = true; | ||||
| 
 | ||||
|    p->map_.reset( | ||||
|  | @ -1038,6 +1045,15 @@ void MapWidget::paintGL() | |||
|    ImGui_ImplOpenGL3_NewFrame(); | ||||
|    ImGui::NewFrame(); | ||||
| 
 | ||||
|    // Update ImGui Fonts if required
 | ||||
|    std::uint64_t currentImGuiFontsBuildCount = | ||||
|       manager::FontManager::Instance().imgui_fonts_build_count(); | ||||
|    if (p->imGuiFontsBuildCount_ != currentImGuiFontsBuildCount) | ||||
|    { | ||||
|       ImGui_ImplOpenGL3_DestroyFontsTexture(); | ||||
|       ImGui_ImplOpenGL3_CreateFontsTexture(); | ||||
|    } | ||||
| 
 | ||||
|    // Update pixel ratio
 | ||||
|    p->context_->set_pixel_ratio(pixelRatio()); | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat