From c75659db82d152719fd6130ba4cb975a441009d5 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 4 Aug 2021 18:57:53 +0200 Subject: [PATCH] ui: Make splash screen DPI aware --- include/init/splash_window.hpp | 2 ++ source/init/splash_window.cpp | 31 +++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/include/init/splash_window.hpp b/include/init/splash_window.hpp index 0d026cf8b..885d6b95a 100644 --- a/include/init/splash_window.hpp +++ b/include/init/splash_window.hpp @@ -27,6 +27,8 @@ namespace hex::init { float m_progress = 0; std::string m_currTaskName; + float m_globalScale = 1.0F; + void initGLFW(); void initImGui(); diff --git a/source/init/splash_window.cpp b/source/init/splash_window.cpp index 2f862aa2c..8044a1fbe 100644 --- a/source/init/splash_window.cpp +++ b/source/init/splash_window.cpp @@ -5,6 +5,7 @@ #include #include +#define IMGUI_DEFINE_MATH_OPERATORS #include #include #include @@ -92,17 +93,18 @@ namespace hex::init { auto drawList = ImGui::GetOverlayDrawList(); - drawList->AddImage(splashTexture, ImVec2(0, 0), ImVec2(splashWidth, splashHeight)); + drawList->AddImage(splashTexture, ImVec2(0, 0), ImVec2(splashWidth, splashHeight) * this->m_globalScale); + + drawList->AddText(ImVec2(15, 120) * this->m_globalScale, ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("WerWolv 2020 - {0}", &__DATE__[7]).c_str()); #if defined(DEBUG) - drawList->AddText(ImVec2(15, 120), ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("WerWolv 2020 - {0}", &__DATE__[7]).c_str()); - drawList->AddText(ImVec2(15, 140), ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("{0} : {1} {2}@{3}", IMHEX_VERSION, ICON_FA_CODE_BRANCH, GIT_BRANCH, GIT_COMMIT_HASH).c_str()); + drawList->AddText(ImVec2(15, 140) * this->m_globalScale, ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("{0} : {1} {2}@{3}", IMHEX_VERSION, ICON_FA_CODE_BRANCH, GIT_BRANCH, GIT_COMMIT_HASH).c_str()); #else - drawList->AddText(ImVec2(15, 120), ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("WerWolv 2020 - {0}", &__DATE__[7]).c_str()); - drawList->AddText(ImVec2(15, 140), ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("{0}", IMHEX_VERSION).c_str()); + drawList->AddText(ImVec2(15, 140) * this->m_globalScale, ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("{0}", IMHEX_VERSION).c_str()); #endif - drawList->AddRectFilled(ImVec2(0, splashHeight - 5), ImVec2(splashWidth * this->m_progress, splashHeight), 0xFFFFFFFF); - drawList->AddText(ImVec2(15, splashHeight - 25), ImColor(0xFF, 0xFF, 0xFF, 0xFF), + + drawList->AddRectFilled(ImVec2(0, splashHeight - 5) * this->m_globalScale, ImVec2(splashWidth * this->m_progress, splashHeight) * this->m_globalScale, 0xFFFFFFFF); + drawList->AddText(ImVec2(15, splashHeight - 25) * this->m_globalScale, ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("[{}] {}", "|/-\\"[ImU32(ImGui::GetTime() * 15) % 4], this->m_currTaskName).c_str()); } @@ -164,7 +166,14 @@ namespace hex::init { glfwWindowHint(GLFW_DECORATED, GLFW_FALSE); glfwWindowHint(GLFW_FLOATING, GLFW_TRUE); - this->m_window = glfwCreateWindow(640, 400, "ImHex", nullptr, nullptr); + if (GLFWmonitor *monitor = glfwGetPrimaryMonitor(); monitor != nullptr) { + float xscale, yscale; + glfwGetMonitorContentScale(monitor, &xscale, &yscale); + + this->m_globalScale = std::midpoint(xscale, yscale); + } + + this->m_window = glfwCreateWindow(640 * this->m_globalScale, 400 * this->m_globalScale, "ImHex", nullptr, nullptr); if (this->m_window == nullptr) { log::fatal("Failed to create GLFW window!"); exit(EXIT_FAILURE); @@ -191,11 +200,13 @@ namespace hex::init { auto &io = ImGui::GetIO(); + ImGui::GetStyle().ScaleAllSizes(this->m_globalScale); + io.Fonts->Clear(); ImFontConfig cfg; cfg.OversampleH = cfg.OversampleV = 1, cfg.PixelSnapH = true; - cfg.SizePixels = 13.0f; + cfg.SizePixels = 13.0f * this->m_globalScale; io.Fonts->AddFontDefault(&cfg); cfg.MergeMode = true; @@ -206,7 +217,7 @@ namespace hex::init { }; std::uint8_t *px; int w, h; - io.Fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data, font_awesome_compressed_size, 11.0f, &cfg, fontAwesomeRange); + io.Fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data, font_awesome_compressed_size, 11.0f * this->m_globalScale, &cfg, fontAwesomeRange); io.Fonts->GetTexDataAsRGBA32(&px, &w, &h); // Create new font atlas