From 444d522558a9fe25548c7dc1925c9f8145fbab9f Mon Sep 17 00:00:00 2001 From: Alphalaneous <38200084+Alphalaneous@users.noreply.github.com> Date: Mon, 28 Apr 2025 10:13:21 -0400 Subject: [PATCH 1/2] Fix getMousePos to be relative to the GD view --- src/backend.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/backend.cpp b/src/backend.cpp index 2650d95..477a6db 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -11,6 +11,29 @@ using namespace cocos2d; // based off https://github.com/matcool/gd-imgui-cocos +static bool g_useNormalPos = false; + +CCPoint getMousePos_H() { + CCPoint mouse = cocos::getMousePos(); + const auto pos = toVec2(mouse); + + if (DevTools::get()->shouldUseGDWindow() && shouldPassEventsToGDButTransformed() && !g_useNormalPos) { + auto win = ImGui::GetMainViewport()->Size; + const auto gdRect = getGDWindowRect(); + + auto relativePos = ImVec2( + pos.x - gdRect.Min.x, + pos.y - gdRect.Min.y + ); + auto x = (relativePos.x / gdRect.GetWidth()) * win.x; + auto y = (relativePos.y / gdRect.GetHeight()) * win.y; + + mouse = toCocos(ImVec2(x, y)); + } + + return mouse; +} + void DevTools::setupPlatform() { ImGui::CreateContext(); @@ -36,6 +59,15 @@ void DevTools::setupPlatform() { tex2d->retain(); io.Fonts->SetTexID(reinterpret_cast(static_cast(tex2d->getName()))); + + // fixes getMousePos to be relative to the GD view + #ifndef GEODE_IS_MOBILE + (void) Mod::get()->hook( + reinterpret_cast(addresser::getNonVirtual(&geode::cocos::getMousePos)), + &getMousePos_H, + "geode::cocos::getMousePos" + ); + #endif } void DevTools::newFrame() { @@ -54,7 +86,9 @@ void DevTools::newFrame() { io.DeltaTime = director->getDeltaTime(); #ifdef GEODE_IS_DESKTOP + g_useNormalPos = true; const auto mousePos = toVec2(geode::cocos::getMousePos()); + g_useNormalPos = false; io.AddMousePosEvent(mousePos.x, mousePos.y); #endif From cb7116923688af302527516be1485e6291f06dc7 Mon Sep 17 00:00:00 2001 From: Alphalaneous <38200084+Alphalaneous@users.noreply.github.com> Date: Fri, 2 May 2025 10:12:26 -0400 Subject: [PATCH 2/2] Oops --- src/backend.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/backend.cpp b/src/backend.cpp index 7552de3..68cd0b0 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -64,6 +64,7 @@ void DevTools::setupPlatform() { "geode::cocos::getMousePos" ); #endif +} void DevTools::newFrame() { auto& io = ImGui::GetIO();