From 3e347fb6d44ae7a082441ec2077f276f5bf93cc4 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 18 Jun 2024 22:10:02 +0200 Subject: [PATCH] impr: Improve icon scaling a bit --- .../source/ui/imgui_imhex_extensions.cpp | 6 +++-- plugins/fonts/source/font_loader.cpp | 23 ++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/libimhex/source/ui/imgui_imhex_extensions.cpp b/lib/libimhex/source/ui/imgui_imhex_extensions.cpp index 73c4ad08a..2733ccbf5 100644 --- a/lib/libimhex/source/ui/imgui_imhex_extensions.cpp +++ b/lib/libimhex/source/ui/imgui_imhex_extensions.cpp @@ -574,7 +574,7 @@ namespace ImGuiExt { if (IsItemHovered() && (currTime - lastMoveTime) >= 0.5 && hoveredID == lastHoveredID) { if (!std::string_view(text).empty()) { const auto width = 300 * hex::ImHexApi::System::getGlobalScale(); - ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(width, FLT_MAX)); + ImGui::SetNextWindowSizeConstraints(ImVec2(width / 2, 0), ImVec2(width, FLT_MAX)); if (BeginTooltip()) { if (isSeparator) SeparatorText(text); @@ -754,6 +754,8 @@ namespace ImGuiExt { ImVec2 size = CalcItemSize(ImVec2(1, 1) * GetCurrentWindow()->MenuBarHeight, label_size.x + style.FramePadding.x * 2.0F, label_size.y + style.FramePadding.y * 2.0F); + ImVec2 padding = (size - label_size) / 2; + const ImRect bb(pos, pos + size); ItemSize(size, style.FramePadding.y); if (!ItemAdd(bb, id)) @@ -769,7 +771,7 @@ namespace ImGuiExt { : ImGuiCol_MenuBarBg); RenderNavHighlight(bb, id); RenderFrame(bb.Min, bb.Max, col, false, style.FrameRounding); - RenderTextClipped(bb.Min + style.FramePadding, bb.Max - style.FramePadding, symbol, nullptr, &label_size, style.ButtonTextAlign, &bb); + RenderTextClipped(bb.Min + padding, bb.Max - padding, symbol, nullptr, &size, style.ButtonTextAlign, &bb); PopStyleColor(); diff --git a/plugins/fonts/source/font_loader.cpp b/plugins/fonts/source/font_loader.cpp index dd6544422..4c8057b7f 100644 --- a/plugins/fonts/source/font_loader.cpp +++ b/plugins/fonts/source/font_loader.cpp @@ -4,14 +4,16 @@ #include #include + #include +#include +#include +#include +#include +#include #include -#include "wolv/io/file.hpp" -#include "hex/api/content_registry.hpp" -#include "hex/helpers/logger.hpp" -#include "wolv/utils/string.hpp" -#include "hex/helpers/fs.hpp" +#include namespace hex::fonts { @@ -158,7 +160,7 @@ namespace hex::fonts { // Calculate the expected font size auto size = fontSize; if (font.defaultSize.has_value()) - size = font.defaultSize.value() * std::floor(ImHexApi::Fonts::getFontSize() / ImHexApi::Fonts::DefaultFontSize); + size = font.defaultSize.value() * std::max(1.0F, std::floor(ImHexApi::Fonts::getFontSize() / ImHexApi::Fonts::DefaultFontSize)); else size = std::max(1.0F, std::floor(size / ImHexApi::Fonts::DefaultFontSize)) * ImHexApi::Fonts::DefaultFontSize; @@ -227,7 +229,12 @@ namespace hex::fonts { } float getFontSize() { - float fontSize = ImHexApi::Fonts::DefaultFontSize * ImHexApi::System::getGlobalScale(); + float fontSize = ImHexApi::Fonts::DefaultFontSize; + + if (auto scaling = ImHexApi::System::getGlobalScale(); u32(scaling) * 10 == u32(scaling * 10)) + fontSize *= scaling; + else + fontSize *= scaling * 0.75F; // Fall back to the default font if the global scale is 0 if (fontSize == 0.0F) @@ -310,7 +317,7 @@ namespace hex::fonts { glyphRanges.push_back(glyphRange); // Calculate the glyph offset for the font - ImVec2 offset = { font.offset.x, font.offset.y - defaultFont->getDescent() + fontAtlas.calculateFontDescend(font, fontSize) }; + ImVec2 offset = { font.offset.x, font.offset.y - (defaultFont->getDescent() - fontAtlas.calculateFontDescend(font, fontSize)) }; // Load the font fontAtlas.addFontFromMemory(font.fontData, font.defaultSize.value_or(fontSize), offset, false, glyphRanges.back());