Skip to content

Commit

Permalink
FIx window sizes not scaling properly when resolution changes
Browse files Browse the repository at this point in the history
  • Loading branch information
user-grinch committed Jun 26, 2024
1 parent 3c422de commit a8248f4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down
19 changes: 15 additions & 4 deletions src/opcodemgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) };
Expand Down
16 changes: 16 additions & 0 deletions src/scriptextender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::function<void()>> buf; // finished buffer for render
Expand Down Expand Up @@ -86,6 +89,11 @@ class ScriptExData
if (curTime-lastScriptCall > 2 || scriptsPaused) {
ClearFrames();
}

if (m_bWasScalingUpdatedThisFrame) {
m_bNeedToUpdateScaling = false;
m_bWasScalingUpdatedThisFrame = false;
}
}

void ClearFrames()
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit a8248f4

Please sign in to comment.