From e01e2f340d94590c97bfb54c83452a5c94c3f7df Mon Sep 17 00:00:00 2001 From: Skyth <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Sun, 26 Jan 2025 17:18:25 +0300 Subject: [PATCH] Fix ImGui mouse position events not working at boxed aspect ratios. --- UnleashedRecomp/gpu/video.cpp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/UnleashedRecomp/gpu/video.cpp b/UnleashedRecomp/gpu/video.cpp index f6814c72..64f6ca70 100644 --- a/UnleashedRecomp/gpu/video.cpp +++ b/UnleashedRecomp/gpu/video.cpp @@ -2044,6 +2044,32 @@ static void DrawProfiler() static void DrawImGui() { ImGui_ImplSDL2_NewFrame(); + + auto& io = ImGui::GetIO(); + io.DisplaySize = { float(Video::s_viewportWidth), float(Video::s_viewportHeight) }; + + // ImGui doesn't know that we center the screen for specific aspect ratio + // settings, which causes mouse events to not work correctly. To fix this, + // we can adjust the mouse events before ImGui processes them. + uint32_t width = g_swapChain->getWidth(); + uint32_t height = g_swapChain->getHeight(); + + if (width != Video::s_viewportWidth || height != Video::s_viewportHeight) + { + float mousePosOffsetX = (width - Video::s_viewportWidth) / 2.0f; + float mousePosOffsetY = (height - Video::s_viewportHeight) / 2.0f; + + for (int i = 0; i < io.Ctx->InputEventsQueue.Size; i++) + { + auto& e = io.Ctx->InputEventsQueue[i]; + if (e.Type == ImGuiInputEventType_MousePos) + { + if (e.MousePos.PosX != -FLT_MAX) e.MousePos.PosX -= mousePosOffsetX; + if (e.MousePos.PosY != -FLT_MAX) e.MousePos.PosY -= mousePosOffsetY; + } + } + } + ImGui::NewFrame(); ResetImGuiCallbacks(); @@ -2064,8 +2090,6 @@ static void DrawImGui() ImGui::End(); #endif - ImGui::GetIO().DisplaySize = { float(Video::s_viewportWidth), float(Video::s_viewportHeight) }; - AchievementMenu::Draw(); OptionsMenu::Draw(); AchievementOverlay::Draw();