From d399a6427aa3932b24dc4cb236a3eee19ddf88d0 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 11 Jan 2022 23:48:18 +0100 Subject: [PATCH] sys: Use custom literals for scaled values --- .../builtin/include/content/views/view_settings.hpp | 2 +- plugins/builtin/source/content/ui_items.cpp | 4 ++-- plugins/builtin/source/content/views/view_diff.cpp | 2 +- plugins/builtin/source/content/views/view_help.cpp | 2 +- .../builtin/source/content/views/view_hexeditor.cpp | 2 +- plugins/builtin/source/content/views/view_store.cpp | 2 +- plugins/libimhex/include/hex/helpers/utils.hpp | 6 ++++++ plugins/libimhex/source/helpers/utils.cpp | 13 +++++++++++++ plugins/libimhex/source/views/view.cpp | 8 ++++---- source/init/splash_window.cpp | 6 +++--- source/init/tasks.cpp | 8 ++++---- source/window/window.cpp | 12 ++++++------ 12 files changed, 43 insertions(+), 24 deletions(-) diff --git a/plugins/builtin/include/content/views/view_settings.hpp b/plugins/builtin/include/content/views/view_settings.hpp index cd7ddc53f..1ce59d7cb 100644 --- a/plugins/builtin/include/content/views/view_settings.hpp +++ b/plugins/builtin/include/content/views/view_settings.hpp @@ -19,7 +19,7 @@ namespace hex::plugin::builtin { [[nodiscard]] bool hasViewMenuItemEntry() const override { return false; } [[nodiscard]] ImVec2 getMinSize() const override { - return ImVec2(500, 300) * SharedData::globalScale; + return scaled(ImVec2(500, 300)); } }; diff --git a/plugins/builtin/source/content/ui_items.cpp b/plugins/builtin/source/content/ui_items.cpp index a9f0225f7..8a095d171 100644 --- a/plugins/builtin/source/content/ui_items.cpp +++ b/plugins/builtin/source/content/ui_items.cpp @@ -55,7 +55,7 @@ namespace hex::plugin::builtin { ImGui::SameLine(); - ImGui::SmallProgressBar(taskProgress, (ImGui::GetCurrentWindow()->MenuBarHeight() - 10 * SharedData::globalScale) / 2.0); + ImGui::SmallProgressBar(taskProgress, (ImGui::GetCurrentWindow()->MenuBarHeight() - 10_scaled) / 2.0); ImGui::InfoTooltip(taskName.c_str()); } @@ -133,7 +133,7 @@ namespace hex::plugin::builtin { if (ImHexApi::Provider::isValid()) preview = providers[SharedData::currentProvider]->getName(); - ImGui::SetNextItemWidth(200 * SharedData::globalScale); + ImGui::SetNextItemWidth(200_scaled); if (ImGui::BeginCombo("", preview.c_str())) { for (int i = 0; i < providers.size(); i++) { diff --git a/plugins/builtin/source/content/views/view_diff.cpp b/plugins/builtin/source/content/views/view_diff.cpp index e4cfb1b40..2b363302b 100644 --- a/plugins/builtin/source/content/views/view_diff.cpp +++ b/plugins/builtin/source/content/views/view_diff.cpp @@ -48,7 +48,7 @@ namespace hex::plugin::builtin { if (ImHexApi::Provider::isValid() && provider >= 0) preview = providers[provider]->getName(); - ImGui::SetNextItemWidth(200 * SharedData::globalScale); + ImGui::SetNextItemWidth(200_scaled); if (ImGui::BeginCombo("", preview.c_str())) { for (int i = 0; i < providers.size(); i++) { diff --git a/plugins/builtin/source/content/views/view_help.cpp b/plugins/builtin/source/content/views/view_help.cpp index 1ba3a81d2..e15c184fb 100644 --- a/plugins/builtin/source/content/views/view_help.cpp +++ b/plugins/builtin/source/content/views/view_help.cpp @@ -113,7 +113,7 @@ namespace hex::plugin::builtin { } void ViewHelp::drawAboutPopup() { - ImGui::SetNextWindowSize(ImVec2(600, 350) * SharedData::globalScale, ImGuiCond_Always); + ImGui::SetNextWindowSize(scaled(ImVec2(600, 350)), ImGuiCond_Always); if (ImGui::BeginPopupModal(View::toWindowName("hex.builtin.view.help.about.name").c_str(), &this->m_aboutWindowOpen, ImGuiWindowFlags_NoResize)) { if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape))) diff --git a/plugins/builtin/source/content/views/view_hexeditor.cpp b/plugins/builtin/source/content/views/view_hexeditor.cpp index 5aab59198..e6cd79c89 100644 --- a/plugins/builtin/source/content/views/view_hexeditor.cpp +++ b/plugins/builtin/source/content/views/view_hexeditor.cpp @@ -177,7 +177,7 @@ namespace hex::plugin::builtin { if (provider->getPageCount() > 1) { ImGui::NewLine(); - auto linePos = ImGui::GetCursorPosY() - 15 * SharedData::globalScale; + auto linePos = ImGui::GetCursorPosY() - 15_scaled; ImGui::SetCursorPosY(linePos); diff --git a/plugins/builtin/source/content/views/view_store.cpp b/plugins/builtin/source/content/views/view_store.cpp index 2eb9828a8..87d7b4497 100644 --- a/plugins/builtin/source/content/views/view_store.cpp +++ b/plugins/builtin/source/content/views/view_store.cpp @@ -218,7 +218,7 @@ namespace hex::plugin::builtin { } void ViewStore::drawContent() { - ImGui::SetNextWindowSizeConstraints(ImVec2(600, 400) * SharedData::globalScale, ImVec2(FLT_MAX, FLT_MAX)); + ImGui::SetNextWindowSizeConstraints(scaled(ImVec2(600, 400)), ImVec2(FLT_MAX, FLT_MAX)); if (ImGui::BeginPopupModal(View::toWindowName("hex.builtin.view.store.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_AlwaysAutoResize)) { if (this->m_apiRequest.valid()) { if (this->m_apiRequest.wait_for(0s) != std::future_status::ready) diff --git a/plugins/libimhex/include/hex/helpers/utils.hpp b/plugins/libimhex/include/hex/helpers/utils.hpp index 3dab941c4..7f8029f11 100644 --- a/plugins/libimhex/include/hex/helpers/utils.hpp +++ b/plugins/libimhex/include/hex/helpers/utils.hpp @@ -23,8 +23,14 @@ #define TOKEN_CONCAT(x, y) TOKEN_CONCAT_IMPL(x, y) #define ANONYMOUS_VARIABLE(prefix) TOKEN_CONCAT(prefix, __COUNTER__) +struct ImVec2; + namespace hex { + long double operator""_scaled(long double value); + long double operator""_scaled(unsigned long long value); + ImVec2 scaled(const ImVec2 &vector); + std::string to_string(u128 value); std::string to_string(s128 value); diff --git a/plugins/libimhex/source/helpers/utils.cpp b/plugins/libimhex/source/helpers/utils.cpp index fc424a249..df662ff0b 100644 --- a/plugins/libimhex/source/helpers/utils.cpp +++ b/plugins/libimhex/source/helpers/utils.cpp @@ -6,6 +6,7 @@ #include #include +#include #if defined (OS_WINDOWS) #include @@ -18,6 +19,18 @@ namespace hex { + long double operator""_scaled(long double value) { + return value * SharedData::globalScale; + } + + long double operator""_scaled(unsigned long long value) { + return value * SharedData::globalScale; + } + + ImVec2 scaled(const ImVec2 &vector) { + return vector * SharedData::globalScale; + } + std::string to_string(u128 value) { char data[45] = { 0 }; diff --git a/plugins/libimhex/source/views/view.cpp b/plugins/libimhex/source/views/view.cpp index 85361d181..8749745a5 100644 --- a/plugins/libimhex/source/views/view.cpp +++ b/plugins/libimhex/source/views/view.cpp @@ -23,7 +23,7 @@ namespace hex { } void View::drawCommonInterfaces() { - ImGui::SetNextWindowSizeConstraints(ImVec2(400, 100) * SharedData::globalScale, ImVec2(600, 300) * SharedData::globalScale); + ImGui::SetNextWindowSizeConstraints(scaled(ImVec2(400, 100)), scaled(ImVec2(600, 300))); if (ImGui::BeginPopupModal("hex.common.info"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { ImGui::TextWrapped("%s", SharedData::popupMessage.c_str()); ImGui::NewLine(); @@ -35,7 +35,7 @@ namespace hex { ImGui::EndPopup(); } - ImGui::SetNextWindowSizeConstraints(ImVec2(400, 100) * SharedData::globalScale, ImVec2(600, 300) * SharedData::globalScale); + ImGui::SetNextWindowSizeConstraints(scaled(ImVec2(400, 100)), scaled(ImVec2(600, 300))); if (ImGui::BeginPopupModal("hex.common.error"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { ImGui::TextWrapped("%s", SharedData::popupMessage.c_str()); ImGui::NewLine(); @@ -47,7 +47,7 @@ namespace hex { ImGui::EndPopup(); } - ImGui::SetNextWindowSizeConstraints(ImVec2(400, 100) * SharedData::globalScale, ImVec2(600, 300) * SharedData::globalScale); + ImGui::SetNextWindowSizeConstraints(scaled(ImVec2(400, 100)), scaled(ImVec2(600, 300))); if (ImGui::BeginPopupModal("hex.common.fatal"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { ImGui::TextWrapped("%s", SharedData::popupMessage.c_str()); ImGui::NewLine(); @@ -85,7 +85,7 @@ namespace hex { } ImVec2 View::getMinSize() const { - return ImVec2(480, 720) * SharedData::globalScale; + return scaled(ImVec2(480, 720)); } ImVec2 View::getMaxSize() const { diff --git a/source/init/splash_window.cpp b/source/init/splash_window.cpp index 203b64435..3576c9405 100644 --- a/source/init/splash_window.cpp +++ b/source/init/splash_window.cpp @@ -175,7 +175,7 @@ namespace hex::init { } } - this->m_window = glfwCreateWindow(640 * SharedData::globalScale, 400 * SharedData::globalScale, "Starting ImHex...", nullptr, nullptr); + this->m_window = glfwCreateWindow(640_scaled, 400_scaled, "Starting ImHex...", nullptr, nullptr); if (this->m_window == nullptr) { log::fatal("Failed to create GLFW window!"); exit(EXIT_FAILURE); @@ -203,7 +203,7 @@ namespace hex::init { ImFontConfig cfg; cfg.OversampleH = cfg.OversampleV = 1, cfg.PixelSnapH = true; - cfg.SizePixels = 13.0f * SharedData::globalScale; + cfg.SizePixels = 13.0_scaled; io.Fonts->AddFontDefault(&cfg); cfg.MergeMode = true; @@ -214,7 +214,7 @@ namespace hex::init { }; std::uint8_t *px; int w, h; - io.Fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data, font_awesome_compressed_size, 11.0f * SharedData::globalScale, &cfg, fontAwesomeRange); + io.Fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data, font_awesome_compressed_size, 11.0_scaled, &cfg, fontAwesomeRange); io.Fonts->GetTexDataAsRGBA32(&px, &w, &h); // Create new font atlas diff --git a/source/init/tasks.cpp b/source/init/tasks.cpp index 0dc712224..d5b4cd2fd 100644 --- a/source/init/tasks.cpp +++ b/source/init/tasks.cpp @@ -136,7 +136,7 @@ namespace hex::init { fonts->Clear(); cfg.OversampleH = cfg.OversampleV = 1, cfg.PixelSnapH = true; - cfg.SizePixels = 13.0f * SharedData::fontScale; + cfg.SizePixels = 13.0F * SharedData::fontScale; fonts->AddFontDefault(&cfg); } else { // Load custom font @@ -151,9 +151,9 @@ namespace hex::init { cfg.MergeMode = true; - fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data, font_awesome_compressed_size, 13.0f * SharedData::fontScale, &cfg, fontAwesomeRange); - fonts->AddFontFromMemoryCompressedTTF(codicons_compressed_data, codicons_compressed_size, 13.0f * SharedData::fontScale, &cfg, codiconsRange); - fonts->AddFontFromMemoryCompressedTTF(unifont_compressed_data, unifont_compressed_size, 13.0f * SharedData::fontScale, &cfg, unifontRange); + fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data, font_awesome_compressed_size, 13.0F * SharedData::fontScale, &cfg, fontAwesomeRange); + fonts->AddFontFromMemoryCompressedTTF(codicons_compressed_data, codicons_compressed_size, 13.0F * SharedData::fontScale, &cfg, codiconsRange); + fonts->AddFontFromMemoryCompressedTTF(unifont_compressed_data, unifont_compressed_size, 13.0F * SharedData::fontScale, &cfg, unifontRange); ImGuiFreeType::BuildFontAtlas(fonts); diff --git a/source/window/window.cpp b/source/window/window.cpp index c2a819541..685af084b 100644 --- a/source/window/window.cpp +++ b/source/window/window.cpp @@ -417,7 +417,7 @@ namespace hex { static char title[256]; ImFormatString(title, IM_ARRAYSIZE(title), "%s/DockSpace_%08X", ImGui::GetCurrentWindow()->Name, ImGui::GetID("MainDock")); if (ImGui::Begin(title)) { - ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(10 * SharedData::globalScale, 10 * SharedData::globalScale)); + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(10_scaled, 10_scaled)); if (ImGui::BeginChild("Welcome Screen", ImVec2(0, 0), false, ImGuiWindowFlags_AlwaysUseWindowPadding | ImGuiWindowFlags_NoScrollWithMouse)) { this->drawWelcomeScreen(); } @@ -598,7 +598,7 @@ namespace hex { ImGui::UnderlinedText("hex.welcome.header.start"_lang); - ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 5 * SharedData::globalScale); + ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 5_scaled); { if (ImGui::IconHyperlink(ICON_VS_NEW_FILE, "hex.welcome.start.create_file"_lang)) EventManager::post("Create File"); @@ -626,7 +626,7 @@ namespace hex { ImGui::TableNextRow(ImGuiTableRowFlags_None, ImGui::GetTextLineHeightWithSpacing() * 9); ImGui::TableNextColumn(); ImGui::UnderlinedText("hex.welcome.start.recent"_lang); - ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 5 * SharedData::globalScale); + ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 5_scaled); { if (!SharedData::recentFilePaths.empty()) { for (auto &path : SharedData::recentFilePaths) { @@ -651,7 +651,7 @@ namespace hex { ImGui::TableNextRow(ImGuiTableRowFlags_None, ImGui::GetTextLineHeightWithSpacing() * 6); ImGui::TableNextColumn(); ImGui:: UnderlinedText("hex.welcome.header.help"_lang); - ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 5 * SharedData::globalScale); + ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 5_scaled); { if (ImGui::IconHyperlink(ICON_VS_GITHUB, "hex.welcome.help.repo"_lang)) hex::openWebpage("hex.welcome.help.repo.link"_lang); if (ImGui::IconHyperlink(ICON_VS_ORGANIZATION, "hex.welcome.help.gethelp"_lang)) hex::openWebpage("hex.welcome.help.gethelp.link"_lang); @@ -778,7 +778,7 @@ namespace hex { glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); this->m_windowTitle = "ImHex"; - this->m_window = glfwCreateWindow(1280 * SharedData::globalScale, 720 * SharedData::globalScale, this->m_windowTitle.c_str(), nullptr, nullptr); + this->m_window = glfwCreateWindow(1280_scaled, 720_scaled, this->m_windowTitle.c_str(), nullptr, nullptr); glfwSetWindowUserPointer(this->m_window, this); @@ -903,7 +903,7 @@ namespace hex { EventManager::post(window); }); - glfwSetWindowSizeLimits(this->m_window, 720 * SharedData::globalScale, 480 * SharedData::globalScale, GLFW_DONT_CARE, GLFW_DONT_CARE); + glfwSetWindowSizeLimits(this->m_window, 720_scaled, 480_scaled, GLFW_DONT_CARE, GLFW_DONT_CARE); glfwShowWindow(this->m_window); }