From a8248f4a265d26f77999aa54992ab4352ab22c2f Mon Sep 17 00:00:00 2001 From: Grinch_ Date: Wed, 26 Jun 2024 16:32:00 -0600 Subject: [PATCH] FIx window sizes not scaling properly when resolution changes --- src/hook.cpp | 2 ++ src/opcodemgr.cpp | 19 +++++++++++++++---- src/scriptextender.hpp | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/hook.cpp b/src/hook.cpp index 58cfb95..1c4f509 100644 --- a/src/hook.cpp +++ b/src/hook.cpp @@ -9,6 +9,7 @@ #include "injector.hpp" #include "font.h" #include "kiero.h" +#include "scriptextender.hpp" extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); @@ -134,6 +135,7 @@ void Hook::ProcessFrame(void* ptr) { style->Colors[ImGuiCol_ResizeGrip] = ImVec4(0.0f, 0.0f, 0.0f, 0.00f); style->WindowTitleAlign = ImVec2(0.5f, 0.5f); fScreenSize = ImVec2((float)width, (float)height); + ScriptExData::SetScaling({scaleX, scaleY}); } ImGui_ImplWin32_NewFrame(); diff --git a/src/opcodemgr.cpp b/src/opcodemgr.cpp index 44a907d..a08e56b 100644 --- a/src/opcodemgr.cpp +++ b/src/opcodemgr.cpp @@ -4,6 +4,7 @@ #include "texturemgr.h" #include "wrapper.hpp" #include "imgui_internal.h" +#include "hook.h" static RTN_TYPE RUNTIME_API ImGuiBegin(RUNTIME_CONTEXT ctx) { char label[RUNTIME_STR_LEN]; @@ -336,8 +337,13 @@ static RTN_TYPE RUNTIME_API ImGuiSetNextWindowSize(RUNTIME_CONTEXT ctx) { int cond = wGetIntParam(ctx); ScriptExData* data = ScriptExData::Get(); + cond = data->imgui.m_bNeedToUpdateScaling ? ImGuiCond_Always : cond; data->imgui += [=]() { - ImGui::SetNextWindowSize(size, cond); + ImGui::SetNextWindowSize({size.x * data->imgui.m_vecScaling.x, size.y * data->imgui.m_vecScaling.y}, cond); + + if (data->imgui.m_bNeedToUpdateScaling) { + data->imgui.m_bWasScalingUpdatedThisFrame = true; + } }; return RTN_CONTINUE; @@ -350,8 +356,13 @@ static RTN_TYPE RUNTIME_API ImGuiSetWindowSize(RUNTIME_CONTEXT ctx) { int cond = wGetIntParam(ctx); ScriptExData* data = ScriptExData::Get(); + cond = data->imgui.m_bNeedToUpdateScaling ? ImGuiCond_Always : cond; data->imgui += [=]() { - ImGui::SetWindowSize(size, cond); + ImGui::SetWindowSize({size.x * data->imgui.m_vecScaling.x, size.y * data->imgui.m_vecScaling.y}, cond); + + if (data->imgui.m_bNeedToUpdateScaling) { + data->imgui.m_bWasScalingUpdatedThisFrame = true; + } }; return RTN_CONTINUE; @@ -487,8 +498,8 @@ static RTN_TYPE RUNTIME_API ImGuiGetWindowSize(RUNTIME_CONTEXT ctx) { ScriptExData* data = ScriptExData::Get(); data->imgui += [=]() { ImVec2 size = ImGui::GetWindowSize(); - data->SetData(buf, 0, size.x); - data->SetData(buf, 1, size.y); + data->SetData(buf, 0, size.x / data->imgui.m_vecScaling.x); + data->SetData(buf, 1, size.y / data->imgui.m_vecScaling.y); }; ImVec2 size = { data->GetData(buf, 0, 0.0f), data->GetData(buf, 1, 0.0f) }; diff --git a/src/scriptextender.hpp b/src/scriptextender.hpp index bf8b9cb..eb93d7a 100644 --- a/src/scriptextender.hpp +++ b/src/scriptextender.hpp @@ -38,6 +38,9 @@ class ScriptExData } m_ImGCol; bool m_bRender; // is backBuffer ready for render + ImVec2 m_vecScaling = ImVec2(1, 1); + bool m_bWasScalingUpdatedThisFrame; // was scaling updated + bool m_bNeedToUpdateScaling; // does imgui scaling needs updating for this script long long lastScriptCall; // last time script called ImGui::Begin(), hide if no script call std::vector> buf; // finished buffer for render @@ -86,6 +89,11 @@ class ScriptExData if (curTime-lastScriptCall > 2 || scriptsPaused) { ClearFrames(); } + + if (m_bWasScalingUpdatedThisFrame) { + m_bNeedToUpdateScaling = false; + m_bWasScalingUpdatedThisFrame = false; + } } void ClearFrames() @@ -210,6 +218,14 @@ class ScriptExData m_nFramerate = (size_t)ImGui::GetIO().Framerate; } + static void SetScaling(ImVec2 scaling) { + for (auto it = scripts.begin(); it != scripts.end(); ++it) + { + (*it)->imgui.m_vecScaling = scaling; + (*it)->imgui.m_bNeedToUpdateScaling = true; + } + } + // Clears all the internal stuff static void Clear() { scripts.clear();