From 810b61f8f93f66d4e28234a6c7108d9f4addbce2 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Sun, 8 Oct 2023 22:05:17 -0500 Subject: [PATCH] Use fonts defined in placefiles --- .../source/scwx/qt/gl/draw/placefile_text.cpp | 34 ++++++++++++++ .../source/scwx/qt/gl/draw/placefile_text.hpp | 13 ++++++ .../scwx/qt/manager/placefile_manager.cpp | 44 ++++++++++++++++--- .../scwx/qt/manager/placefile_manager.hpp | 4 ++ .../source/scwx/qt/map/placefile_layer.cpp | 2 + 5 files changed, 92 insertions(+), 5 deletions(-) diff --git a/scwx-qt/source/scwx/qt/gl/draw/placefile_text.cpp b/scwx-qt/source/scwx/qt/gl/draw/placefile_text.cpp index 18f2efee..b318e19f 100644 --- a/scwx-qt/source/scwx/qt/gl/draw/placefile_text.cpp +++ b/scwx-qt/source/scwx/qt/gl/draw/placefile_text.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #include #include @@ -37,6 +39,7 @@ public: const std::string& text, const std::string& hoverText, boost::gil::rgba8_pixel_t color, + std::size_t fontNumber, float x, float y); @@ -62,6 +65,9 @@ public: std::mutex listMutex_ {}; std::vector> textList_ {}; std::vector> newList_ {}; + + std::vector> fonts_ {}; + std::vector> newFonts_ {}; }; PlacefileText::PlacefileText(const std::shared_ptr& context, @@ -155,6 +161,7 @@ void PlacefileText::Impl::RenderTextDrawItem( di->text_, di->hoverText_, di->color_, + std::clamp(di->fontNumber_, 1, 8), rotatedX + di->x_ + halfWidth_, rotatedY + di->y_ + halfHeight_); } @@ -165,6 +172,7 @@ void PlacefileText::Impl::RenderText( const std::string& text, const std::string& hoverText, boost::gil::rgba8_pixel_t color, + std::size_t fontNumber, float x, float y) { @@ -184,10 +192,12 @@ void PlacefileText::Impl::RenderText( ImGuiWindowFlags_NoBackground); // Render text + ImGui::PushFont(fonts_[fontNumber - 1]->font()); ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(color[0], color[1], color[2], color[3])); ImGui::TextUnformatted(text.c_str()); ImGui::PopStyleColor(); + ImGui::PopFont(); // Store hover text for mouse picking pass if (!hoverText.empty() && ImGui::IsItemHovered()) @@ -231,6 +241,28 @@ void PlacefileText::StartText() p->newList_.clear(); } +void PlacefileText::SetFonts( + const boost::unordered_flat_map>& fonts) +{ + auto defaultFont = manager::FontManager::Instance().GetImGuiFont( + types::FontCategory::Default); + + // Valid font numbers are from 1 to 8, place in 0-based font vector + for (std::size_t i = 1; i <= 8; ++i) + { + auto it = fonts.find(i); + if (it != fonts.cend()) + { + p->newFonts_.push_back(it->second); + } + else + { + p->newFonts_.push_back(defaultFont); + } + } +} + void PlacefileText::AddText( const std::shared_ptr& di) { @@ -246,9 +278,11 @@ void PlacefileText::FinishText() // Swap text lists p->textList_.swap(p->newList_); + p->fonts_.swap(p->newFonts_); // Clear the new list p->newList_.clear(); + p->newFonts_.clear(); } } // namespace draw diff --git a/scwx-qt/source/scwx/qt/gl/draw/placefile_text.hpp b/scwx-qt/source/scwx/qt/gl/draw/placefile_text.hpp index 979a802a..a8d23bc3 100644 --- a/scwx-qt/source/scwx/qt/gl/draw/placefile_text.hpp +++ b/scwx-qt/source/scwx/qt/gl/draw/placefile_text.hpp @@ -2,8 +2,11 @@ #include #include +#include #include +#include + namespace scwx { namespace qt @@ -44,6 +47,16 @@ public: */ void StartText(); + /** + * Configures the fonts for drawing the placefile text. + * + * @param [in] fonts A map of ImGui fonts + */ + void + SetFonts(const boost::unordered_flat_map>& + fonts); + /** * Adds placefile text to the internal draw list. * diff --git a/scwx-qt/source/scwx/qt/manager/placefile_manager.cpp b/scwx-qt/source/scwx/qt/manager/placefile_manager.cpp index 52414031..67bc3f1c 100644 --- a/scwx-qt/source/scwx/qt/manager/placefile_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/placefile_manager.cpp @@ -52,7 +52,8 @@ public: void ReadPlacefileSettings(); void WritePlacefileSettings(); - static void + static boost::unordered_flat_map> LoadFontResources(const std::shared_ptr& placefile); static std::vector> LoadImageResources(const std::shared_ptr& placefile); @@ -66,7 +67,7 @@ public: std::shared_ptr radarSite_ {}; std::vector> placefileRecords_ {}; - std::unordered_map> + boost::unordered_flat_map> placefileRecordMap_ {}; std::shared_mutex placefileRecordLock_ {}; }; @@ -137,6 +138,10 @@ public: std::mutex refreshMutex_ {}; std::mutex timerMutex_ {}; + boost::unordered_flat_map> + fonts_ {}; + std::mutex fontsMutex_ {}; + std::vector> images_ {}; std::string lastRadarSite_ {}; @@ -211,6 +216,20 @@ PlacefileManager::placefile(const std::string& name) return nullptr; } +boost::unordered_flat_map> +PlacefileManager::placefile_fonts(const std::string& name) +{ + std::shared_lock lock(p->placefileRecordLock_); + + auto it = p->placefileRecordMap_.find(name); + if (it != p->placefileRecordMap_.cend()) + { + std::unique_lock fontsLock {it->second->fontsMutex_}; + return it->second->fonts_; + } + return {}; +} + void PlacefileManager::set_placefile_enabled(const std::string& name, bool enabled) { @@ -281,6 +300,7 @@ void PlacefileManager::set_placefile_url(const std::string& name, auto placefileRecord = it->second; placefileRecord->name_ = normalizedUrl; placefileRecord->placefile_ = nullptr; + placefileRecord->fonts_.clear(); placefileRecord->images_.clear(); p->placefileRecordMap_.erase(it); p->placefileRecordMap_.insert_or_assign(normalizedUrl, placefileRecord); @@ -590,7 +610,7 @@ void PlacefileManager::Impl::PlacefileRecord::Update() if (updatedPlacefile != nullptr) { // Load placefile resources - Impl::LoadFontResources(updatedPlacefile); + auto newFonts = Impl::LoadFontResources(updatedPlacefile); auto newImages = Impl::LoadImageResources(updatedPlacefile); // Check the name matches, in case the name updated @@ -601,6 +621,13 @@ void PlacefileManager::Impl::PlacefileRecord::Update() title_ = placefile_->title(); lastUpdateTime_ = std::chrono::system_clock::now(); + // Update font resources + { + std::unique_lock fontsLock {fontsMutex_}; + fonts_.swap(newFonts); + newFonts.clear(); + } + // Update image resources images_.swap(newImages); newImages.clear(); @@ -688,9 +715,12 @@ std::shared_ptr PlacefileManager::Instance() return placefileManager; } -void PlacefileManager::Impl::LoadFontResources( +boost::unordered_flat_map> +PlacefileManager::Impl::LoadFontResources( const std::shared_ptr& placefile) { + boost::unordered_flat_map> + imGuiFonts {}; auto fonts = placefile->fonts(); for (auto& font : fonts) @@ -707,8 +737,12 @@ void PlacefileManager::Impl::LoadFontResources( styles.push_back("italic"); } - FontManager::Instance().LoadImGuiFont(font.second->face_, styles, size); + auto imGuiFont = FontManager::Instance().LoadImGuiFont( + font.second->face_, styles, size); + imGuiFonts.emplace(font.first, std::move(imGuiFont)); } + + return imGuiFonts; } std::vector> diff --git a/scwx-qt/source/scwx/qt/manager/placefile_manager.hpp b/scwx-qt/source/scwx/qt/manager/placefile_manager.hpp index 0567265f..ee9bf3a4 100644 --- a/scwx-qt/source/scwx/qt/manager/placefile_manager.hpp +++ b/scwx-qt/source/scwx/qt/manager/placefile_manager.hpp @@ -2,8 +2,10 @@ #include #include +#include #include +#include namespace scwx { @@ -24,6 +26,8 @@ public: bool placefile_thresholded(const std::string& name); std::string placefile_title(const std::string& name); std::shared_ptr placefile(const std::string& name); + boost::unordered_flat_map> + placefile_fonts(const std::string& name); void set_placefile_enabled(const std::string& name, bool enabled); void set_placefile_thresholded(const std::string& name, bool thresholded); diff --git a/scwx-qt/source/scwx/qt/map/placefile_layer.cpp b/scwx-qt/source/scwx/qt/map/placefile_layer.cpp index f29d63e4..3f6e5961 100644 --- a/scwx-qt/source/scwx/qt/map/placefile_layer.cpp +++ b/scwx-qt/source/scwx/qt/map/placefile_layer.cpp @@ -197,6 +197,8 @@ void PlacefileLayer::ReloadData() p->placefileIcons_->SetIconFiles(placefile->icon_files(), placefile->name()); + p->placefileText_->SetFonts( + placefileManager->placefile_fonts(p->placefileName_)); for (auto& drawItem : placefile->GetDrawItems()) {