mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 20:10:06 +00:00
Add ImGui Font class and dedicated Font Manager
This commit is contained in:
parent
d1a478ad12
commit
190bd95781
5 changed files with 205 additions and 2 deletions
|
|
@ -74,7 +74,8 @@ set(SRC_GL_DRAW source/scwx/qt/gl/draw/draw_item.cpp
|
|||
source/scwx/qt/gl/draw/placefile_text.cpp
|
||||
source/scwx/qt/gl/draw/placefile_triangles.cpp
|
||||
source/scwx/qt/gl/draw/rectangle.cpp)
|
||||
set(HDR_MANAGER source/scwx/qt/manager/placefile_manager.hpp
|
||||
set(HDR_MANAGER source/scwx/qt/manager/font_manager.hpp
|
||||
source/scwx/qt/manager/placefile_manager.hpp
|
||||
source/scwx/qt/manager/radar_product_manager.hpp
|
||||
source/scwx/qt/manager/radar_product_manager_notifier.hpp
|
||||
source/scwx/qt/manager/resource_manager.hpp
|
||||
|
|
@ -82,7 +83,8 @@ set(HDR_MANAGER source/scwx/qt/manager/placefile_manager.hpp
|
|||
source/scwx/qt/manager/text_event_manager.hpp
|
||||
source/scwx/qt/manager/timeline_manager.hpp
|
||||
source/scwx/qt/manager/update_manager.hpp)
|
||||
set(SRC_MANAGER source/scwx/qt/manager/placefile_manager.cpp
|
||||
set(SRC_MANAGER source/scwx/qt/manager/font_manager.cpp
|
||||
source/scwx/qt/manager/placefile_manager.cpp
|
||||
source/scwx/qt/manager/radar_product_manager.cpp
|
||||
source/scwx/qt/manager/radar_product_manager_notifier.cpp
|
||||
source/scwx/qt/manager/resource_manager.cpp
|
||||
|
|
@ -158,6 +160,7 @@ set(SRC_SETTINGS source/scwx/qt/settings/general_settings.cpp
|
|||
set(HDR_TYPES source/scwx/qt/types/alert_types.hpp
|
||||
source/scwx/qt/types/font_types.hpp
|
||||
source/scwx/qt/types/github_types.hpp
|
||||
source/scwx/qt/types/imgui_font.hpp
|
||||
source/scwx/qt/types/map_types.hpp
|
||||
source/scwx/qt/types/qt_types.hpp
|
||||
source/scwx/qt/types/radar_product_record.hpp
|
||||
|
|
@ -165,6 +168,7 @@ set(HDR_TYPES source/scwx/qt/types/alert_types.hpp
|
|||
source/scwx/qt/types/text_types.hpp)
|
||||
set(SRC_TYPES source/scwx/qt/types/alert_types.cpp
|
||||
source/scwx/qt/types/github_types.cpp
|
||||
source/scwx/qt/types/imgui_font.cpp
|
||||
source/scwx/qt/types/map_types.cpp
|
||||
source/scwx/qt/types/radar_product_record.cpp
|
||||
source/scwx/qt/types/text_event_key.cpp
|
||||
|
|
|
|||
45
scwx-qt/source/scwx/qt/manager/font_manager.cpp
Normal file
45
scwx-qt/source/scwx/qt/manager/font_manager.cpp
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include <scwx/qt/manager/font_manager.hpp>
|
||||
#include <scwx/util/logger.hpp>
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
namespace qt
|
||||
{
|
||||
namespace manager
|
||||
{
|
||||
|
||||
static const std::string logPrefix_ = "scwx::qt::manager::font_manager";
|
||||
static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
|
||||
|
||||
class FontManager::Impl
|
||||
{
|
||||
public:
|
||||
explicit Impl() {}
|
||||
~Impl() {}
|
||||
};
|
||||
|
||||
FontManager::FontManager() : p(std::make_unique<Impl>()) {}
|
||||
|
||||
FontManager::~FontManager() {};
|
||||
|
||||
std::shared_ptr<FontManager> FontManager::Instance()
|
||||
{
|
||||
static std::weak_ptr<FontManager> fontManagerReference_ {};
|
||||
static std::mutex instanceMutex_ {};
|
||||
|
||||
std::unique_lock lock(instanceMutex_);
|
||||
|
||||
std::shared_ptr<FontManager> fontManager = fontManagerReference_.lock();
|
||||
|
||||
if (fontManager == nullptr)
|
||||
{
|
||||
fontManager = std::make_shared<FontManager>();
|
||||
fontManagerReference_ = fontManager;
|
||||
}
|
||||
|
||||
return fontManager;
|
||||
}
|
||||
|
||||
} // namespace manager
|
||||
} // namespace qt
|
||||
} // namespace scwx
|
||||
31
scwx-qt/source/scwx/qt/manager/font_manager.hpp
Normal file
31
scwx-qt/source/scwx/qt/manager/font_manager.hpp
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#pragma once
|
||||
|
||||
#include <scwx/qt/types/imgui_font.hpp>
|
||||
|
||||
#include <QObject>
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
namespace qt
|
||||
{
|
||||
namespace manager
|
||||
{
|
||||
|
||||
class FontManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit FontManager();
|
||||
~FontManager();
|
||||
|
||||
static std::shared_ptr<FontManager> Instance();
|
||||
|
||||
private:
|
||||
class Impl;
|
||||
std::unique_ptr<Impl> p;
|
||||
};
|
||||
|
||||
} // namespace manager
|
||||
} // namespace qt
|
||||
} // namespace scwx
|
||||
83
scwx-qt/source/scwx/qt/types/imgui_font.cpp
Normal file
83
scwx-qt/source/scwx/qt/types/imgui_font.cpp
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
// Disable strncpy warning
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
|
||||
#include <scwx/qt/types/imgui_font.hpp>
|
||||
#include <scwx/qt/model/imgui_context_model.hpp>
|
||||
#include <scwx/util/logger.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
namespace qt
|
||||
{
|
||||
namespace types
|
||||
{
|
||||
|
||||
static const std::string logPrefix_ = "scwx::qt::types::imgui_font";
|
||||
static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
|
||||
|
||||
class ImGuiFont::Impl
|
||||
{
|
||||
public:
|
||||
explicit Impl(const std::string& fontName,
|
||||
const std::string& fontData,
|
||||
units::pixels<double> size) :
|
||||
fontName_ {fontName}, size_ {size}
|
||||
{
|
||||
CreateImGuiFont(fontData);
|
||||
}
|
||||
|
||||
~Impl() {}
|
||||
|
||||
void CreateImGuiFont(const std::string& fontData);
|
||||
|
||||
const std::string fontName_;
|
||||
const units::pixels<double> size_;
|
||||
|
||||
ImFont* imFont_ {nullptr};
|
||||
};
|
||||
|
||||
ImGuiFont::ImGuiFont(const std::string& fontName,
|
||||
const std::string& fontData,
|
||||
units::pixels<double> size) :
|
||||
p(std::make_unique<Impl>(fontName, fontData, size))
|
||||
{
|
||||
}
|
||||
ImGuiFont::~ImGuiFont() = default;
|
||||
|
||||
void ImGuiFont::Impl::CreateImGuiFont(const std::string& fontData)
|
||||
{
|
||||
logger_->debug("Creating Font: {}", fontName_);
|
||||
|
||||
ImFontAtlas* fontAtlas = model::ImGuiContextModel::Instance().font_atlas();
|
||||
ImFontConfig fontConfig {};
|
||||
|
||||
const float sizePixels = static_cast<float>(size_.value());
|
||||
|
||||
// Do not transfer ownership of font data to ImGui, makes const_cast safe
|
||||
fontConfig.FontDataOwnedByAtlas = false;
|
||||
|
||||
// Assign name to font
|
||||
strncpy(fontConfig.Name, fontName_.c_str(), sizeof(fontConfig.Name) - 1);
|
||||
fontConfig.Name[sizeof(fontConfig.Name) - 1] = 0;
|
||||
|
||||
imFont_ = fontAtlas->AddFontFromMemoryTTF(
|
||||
const_cast<void*>(static_cast<const void*>(fontData.c_str())),
|
||||
static_cast<int>(std::clamp<std::size_t>(
|
||||
fontData.size(), 0, std::numeric_limits<int>::max())),
|
||||
sizePixels,
|
||||
&fontConfig);
|
||||
}
|
||||
|
||||
ImFont* ImGuiFont::font()
|
||||
{
|
||||
return p->imFont_;
|
||||
}
|
||||
|
||||
} // namespace types
|
||||
} // namespace qt
|
||||
} // namespace scwx
|
||||
40
scwx-qt/source/scwx/qt/types/imgui_font.hpp
Normal file
40
scwx-qt/source/scwx/qt/types/imgui_font.hpp
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <scwx/qt/types/font_types.hpp>
|
||||
|
||||
struct ImFont;
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
namespace qt
|
||||
{
|
||||
namespace types
|
||||
{
|
||||
|
||||
class ImGuiFont
|
||||
{
|
||||
public:
|
||||
explicit ImGuiFont(const std::string& fontName,
|
||||
const std::string& fontData,
|
||||
units::pixels<double> size);
|
||||
~ImGuiFont();
|
||||
|
||||
ImGuiFont(const ImGuiFont&) = delete;
|
||||
ImGuiFont& operator=(const ImGuiFont&) = delete;
|
||||
|
||||
ImGuiFont(ImGuiFont&&) = delete;
|
||||
ImGuiFont& operator=(ImGuiFont&&) = delete;
|
||||
|
||||
ImFont* font();
|
||||
|
||||
private:
|
||||
class Impl;
|
||||
std::unique_ptr<Impl> p;
|
||||
};
|
||||
|
||||
} // namespace types
|
||||
} // namespace qt
|
||||
} // namespace scwx
|
||||
Loading…
Add table
Add a link
Reference in a new issue