mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 06:20:06 +00:00 
			
		
		
		
	Use fonts defined by text settings for rendering
This commit is contained in:
		
							parent
							
								
									e37e64b3f2
								
							
						
					
					
						commit
						11ea4676cf
					
				
					 5 changed files with 47 additions and 79 deletions
				
			
		|  | @ -189,6 +189,22 @@ FontManager::GetImGuiFont(types::FontCategory fontCategory) | ||||||
|    return p->defaultFont_; |    return p->defaultFont_; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | QFont FontManager::GetQFont(types::FontCategory fontCategory) | ||||||
|  | { | ||||||
|  |    auto& textSettings = settings::TextSettings::Instance(); | ||||||
|  | 
 | ||||||
|  |    auto family = textSettings.font_family(fontCategory).GetValue(); | ||||||
|  |    auto styles = textSettings.font_style(fontCategory).GetValue(); | ||||||
|  |    units::font_size::points<double> size { | ||||||
|  |       textSettings.font_point_size(fontCategory).GetValue()}; | ||||||
|  | 
 | ||||||
|  |    QFont font(QString::fromStdString(family)); | ||||||
|  |    font.setStyleName(QString::fromStdString(styles)); | ||||||
|  |    font.setPointSizeF(size.value()); | ||||||
|  | 
 | ||||||
|  |    return font; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| std::shared_ptr<types::ImGuiFont> | std::shared_ptr<types::ImGuiFont> | ||||||
| FontManager::LoadImGuiFont(const std::string&               family, | FontManager::LoadImGuiFont(const std::string&               family, | ||||||
|                            const std::vector<std::string>&  styles, |                            const std::vector<std::string>&  styles, | ||||||
|  |  | ||||||
|  | @ -5,6 +5,7 @@ | ||||||
| 
 | 
 | ||||||
| #include <shared_mutex> | #include <shared_mutex> | ||||||
| 
 | 
 | ||||||
|  | #include <QFont> | ||||||
| #include <QObject> | #include <QObject> | ||||||
| 
 | 
 | ||||||
| namespace scwx | namespace scwx | ||||||
|  | @ -37,6 +38,8 @@ public: | ||||||
|    void LoadApplicationFont(const std::string& filename); |    void LoadApplicationFont(const std::string& filename); | ||||||
|    void InitializeFonts(); |    void InitializeFonts(); | ||||||
| 
 | 
 | ||||||
|  |    static QFont GetQFont(types::FontCategory fontCategory); | ||||||
|  | 
 | ||||||
|    static FontManager& Instance(); |    static FontManager& Instance(); | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|  |  | ||||||
|  | @ -1032,6 +1032,9 @@ void MapWidget::initializeGL() | ||||||
| 
 | 
 | ||||||
| void MapWidget::paintGL() | void MapWidget::paintGL() | ||||||
| { | { | ||||||
|  |    auto defaultFont = manager::FontManager::Instance().GetImGuiFont( | ||||||
|  |       types::FontCategory::Default); | ||||||
|  | 
 | ||||||
|    p->frameDraws_++; |    p->frameDraws_++; | ||||||
| 
 | 
 | ||||||
|    // Setup ImGui Frame
 |    // Setup ImGui Frame
 | ||||||
|  | @ -1047,6 +1050,9 @@ void MapWidget::paintGL() | ||||||
|    p->ImGuiCheckFonts(); |    p->ImGuiCheckFonts(); | ||||||
|    ImGui::NewFrame(); |    ImGui::NewFrame(); | ||||||
| 
 | 
 | ||||||
|  |    // Set default font
 | ||||||
|  |    ImGui::PushFont(defaultFont->font()); | ||||||
|  | 
 | ||||||
|    // Update pixel ratio
 |    // Update pixel ratio
 | ||||||
|    p->context_->set_pixel_ratio(pixelRatio()); |    p->context_->set_pixel_ratio(pixelRatio()); | ||||||
| 
 | 
 | ||||||
|  | @ -1069,6 +1075,9 @@ void MapWidget::paintGL() | ||||||
|       p->lastItemPicked_ = false; |       p->lastItemPicked_ = false; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|  |    // Pop default font
 | ||||||
|  |    ImGui::PopFont(); | ||||||
|  | 
 | ||||||
|    // Render ImGui Frame
 |    // Render ImGui Frame
 | ||||||
|    ImGui::Render(); |    ImGui::Render(); | ||||||
|    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); |    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); | ||||||
|  |  | ||||||
|  | @ -1,10 +1,7 @@ | ||||||
| #include <scwx/qt/util/imgui.hpp> | #include <scwx/qt/util/imgui.hpp> | ||||||
| #include <scwx/qt/manager/resource_manager.hpp> | #include <scwx/qt/manager/font_manager.hpp> | ||||||
| #include <scwx/qt/settings/general_settings.hpp> |  | ||||||
| #include <scwx/util/logger.hpp> | #include <scwx/util/logger.hpp> | ||||||
| 
 | 
 | ||||||
| #include <mutex> |  | ||||||
| 
 |  | ||||||
| #include <imgui.h> | #include <imgui.h> | ||||||
| 
 | 
 | ||||||
| namespace scwx | namespace scwx | ||||||
|  | @ -22,13 +19,6 @@ class ImGui::Impl | ||||||
| public: | public: | ||||||
|    explicit Impl() {} |    explicit Impl() {} | ||||||
|    ~Impl() {} |    ~Impl() {} | ||||||
| 
 |  | ||||||
|    void Initialize(); |  | ||||||
|    void UpdateMonospaceFont(); |  | ||||||
| 
 |  | ||||||
|    bool initialized_ {false}; |  | ||||||
| 
 |  | ||||||
|    ImFont* monospaceFont_ {nullptr}; |  | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| ImGui::ImGui() : p(std::make_unique<Impl>()) {} | ImGui::ImGui() : p(std::make_unique<Impl>()) {} | ||||||
|  | @ -37,58 +27,13 @@ ImGui::~ImGui() = default; | ||||||
| ImGui::ImGui(ImGui&&) noexcept            = default; | ImGui::ImGui(ImGui&&) noexcept            = default; | ||||||
| ImGui& ImGui::operator=(ImGui&&) noexcept = default; | ImGui& ImGui::operator=(ImGui&&) noexcept = default; | ||||||
| 
 | 
 | ||||||
| void ImGui::Impl::Initialize() |  | ||||||
| { |  | ||||||
|    if (initialized_) |  | ||||||
|    { |  | ||||||
|       return; |  | ||||||
|    } |  | ||||||
| 
 |  | ||||||
|    logger_->debug("Initialize"); |  | ||||||
| 
 |  | ||||||
|    // Configure monospace font
 |  | ||||||
|    UpdateMonospaceFont(); |  | ||||||
|    settings::GeneralSettings::Instance() |  | ||||||
|       .font_sizes() |  | ||||||
|       .RegisterValueChangedCallback([this](const std::vector<std::int64_t>&) |  | ||||||
|                                     { UpdateMonospaceFont(); }); |  | ||||||
| 
 |  | ||||||
|    initialized_ = true; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| void ImGui::Impl::UpdateMonospaceFont() |  | ||||||
| { |  | ||||||
|    // Get monospace font size
 |  | ||||||
|    std::size_t fontSize = 16; |  | ||||||
|    auto        fontSizes = |  | ||||||
|       settings::GeneralSettings::Instance().font_sizes().GetValue(); |  | ||||||
|    if (fontSizes.size() > 1) |  | ||||||
|    { |  | ||||||
|       fontSize = fontSizes[1]; |  | ||||||
|    } |  | ||||||
|    else if (fontSizes.size() > 0) |  | ||||||
|    { |  | ||||||
|       fontSize = fontSizes[0]; |  | ||||||
|    } |  | ||||||
| 
 |  | ||||||
|    // Get monospace font pointer
 |  | ||||||
|    auto monospace = |  | ||||||
|       manager::ResourceManager::Font(types::Font::Inconsolata_Regular); |  | ||||||
|    auto monospaceFont = monospace->ImGuiFont(fontSize); |  | ||||||
| 
 |  | ||||||
|    // Store monospace font pointer if not null
 |  | ||||||
|    if (monospaceFont != nullptr) |  | ||||||
|    { |  | ||||||
|       monospaceFont_ = monospace->ImGuiFont(fontSize); |  | ||||||
|    } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| void ImGui::DrawTooltip(const std::string& hoverText) | void ImGui::DrawTooltip(const std::string& hoverText) | ||||||
| { | { | ||||||
|    p->Initialize(); |    auto tooltipFont = manager::FontManager::Instance().GetImGuiFont( | ||||||
|  |       types::FontCategory::Tooltip); | ||||||
| 
 | 
 | ||||||
|    ::ImGui::BeginTooltip(); |    ::ImGui::BeginTooltip(); | ||||||
|    ::ImGui::PushFont(p->monospaceFont_); |    ::ImGui::PushFont(tooltipFont->font()); | ||||||
|    ::ImGui::TextUnformatted(hoverText.c_str()); |    ::ImGui::TextUnformatted(hoverText.c_str()); | ||||||
|    ::ImGui::PopFont(); |    ::ImGui::PopFont(); | ||||||
|    ::ImGui::EndTooltip(); |    ::ImGui::EndTooltip(); | ||||||
|  |  | ||||||
|  | @ -1,8 +1,6 @@ | ||||||
| #include <scwx/qt/util/tooltip.hpp> | #include <scwx/qt/util/tooltip.hpp> | ||||||
| #include <scwx/qt/settings/general_settings.hpp> | #include <scwx/qt/manager/font_manager.hpp> | ||||||
| #include <scwx/qt/settings/text_settings.hpp> | #include <scwx/qt/settings/text_settings.hpp> | ||||||
| #include <scwx/qt/types/font_types.hpp> |  | ||||||
| #include <scwx/qt/types/text_types.hpp> |  | ||||||
| #include <scwx/qt/util/imgui.hpp> | #include <scwx/qt/util/imgui.hpp> | ||||||
| #include <scwx/util/logger.hpp> | #include <scwx/util/logger.hpp> | ||||||
| 
 | 
 | ||||||
|  | @ -84,12 +82,22 @@ void Show(const std::string& text, const QPointF& mouseGlobalPos) | ||||||
|    } |    } | ||||||
|    else if (tooltipMethod == types::TooltipMethod::QToolTip) |    else if (tooltipMethod == types::TooltipMethod::QToolTip) | ||||||
|    { |    { | ||||||
|  |       QString fontFamily = QString::fromStdString( | ||||||
|  |          textSettings.font_family(types::FontCategory::Tooltip).GetValue()); | ||||||
|  |       QString fontStyle = QString::fromStdString( | ||||||
|  |          textSettings.font_style(types::FontCategory::Tooltip).GetValue()); | ||||||
|  |       double fontPointSize = | ||||||
|  |          textSettings.font_point_size(types::FontCategory::Tooltip).GetValue(); | ||||||
|  | 
 | ||||||
|       static std::size_t id = 0; |       static std::size_t id = 0; | ||||||
|       QToolTip::showText( |       QToolTip::showText( | ||||||
|          mouseGlobalPos.toPoint(), |          mouseGlobalPos.toPoint(), | ||||||
|          QString("<span id='%1' style='font-family:\"%2\"'>%3</span>") |          QString("<span id='%1' style='font-family:\"%2\"; font-style:\"%3\"; " | ||||||
|  |                  "font-size:\"%4pt\";'>%5</span>") | ||||||
|             .arg(++id) |             .arg(++id) | ||||||
|             .arg("Inconsolata") |             .arg(fontFamily) | ||||||
|  |             .arg(fontStyle) | ||||||
|  |             .arg(fontPointSize) | ||||||
|             .arg(QString::fromStdString(displayText).replace("\n", "<br/>")), |             .arg(QString::fromStdString(displayText).replace("\n", "<br/>")), | ||||||
|          tooltipParent_.get(), |          tooltipParent_.get(), | ||||||
|          {}, |          {}, | ||||||
|  | @ -97,22 +105,9 @@ void Show(const std::string& text, const QPointF& mouseGlobalPos) | ||||||
|    } |    } | ||||||
|    else if (tooltipMethod == types::TooltipMethod::QLabel) |    else if (tooltipMethod == types::TooltipMethod::QLabel) | ||||||
|    { |    { | ||||||
|       // Get monospace font size
 |  | ||||||
|       units::font_size::pixels<double> fontSize {16}; |  | ||||||
|       auto                             fontSizes = |  | ||||||
|          settings::GeneralSettings::Instance().font_sizes().GetValue(); |  | ||||||
|       if (fontSizes.size() > 1) |  | ||||||
|       { |  | ||||||
|          fontSize = units::font_size::pixels<double> {fontSizes[1]}; |  | ||||||
|       } |  | ||||||
|       else if (fontSizes.size() > 0) |  | ||||||
|       { |  | ||||||
|          fontSize = units::font_size::pixels<double> {fontSizes[0]}; |  | ||||||
|       } |  | ||||||
| 
 |  | ||||||
|       // Configure the label
 |       // Configure the label
 | ||||||
|       QFont font("Inconsolata"); |       QFont font = manager::FontManager::Instance().GetQFont( | ||||||
|       font.setPointSizeF(units::font_size::points<double>(fontSize).value()); |          types::FontCategory::Tooltip); | ||||||
|       tooltipLabel_->setFont(font); |       tooltipLabel_->setFont(font); | ||||||
|       tooltipLabel_->setText(QString::fromStdString(displayText)); |       tooltipLabel_->setText(QString::fromStdString(displayText)); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat