Skip to content

Commit 444d522

Browse files
committed
Fix getMousePos to be relative to the GD view
1 parent e605e96 commit 444d522

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

src/backend.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@ using namespace cocos2d;
1111

1212
// based off https://github.com/matcool/gd-imgui-cocos
1313

14+
static bool g_useNormalPos = false;
15+
16+
CCPoint getMousePos_H() {
17+
CCPoint mouse = cocos::getMousePos();
18+
const auto pos = toVec2(mouse);
19+
20+
if (DevTools::get()->shouldUseGDWindow() && shouldPassEventsToGDButTransformed() && !g_useNormalPos) {
21+
auto win = ImGui::GetMainViewport()->Size;
22+
const auto gdRect = getGDWindowRect();
23+
24+
auto relativePos = ImVec2(
25+
pos.x - gdRect.Min.x,
26+
pos.y - gdRect.Min.y
27+
);
28+
auto x = (relativePos.x / gdRect.GetWidth()) * win.x;
29+
auto y = (relativePos.y / gdRect.GetHeight()) * win.y;
30+
31+
mouse = toCocos(ImVec2(x, y));
32+
}
33+
34+
return mouse;
35+
}
36+
1437
void DevTools::setupPlatform() {
1538
ImGui::CreateContext();
1639

@@ -36,6 +59,15 @@ void DevTools::setupPlatform() {
3659
tex2d->retain();
3760

3861
io.Fonts->SetTexID(reinterpret_cast<ImTextureID>(static_cast<intptr_t>(tex2d->getName())));
62+
63+
// fixes getMousePos to be relative to the GD view
64+
#ifndef GEODE_IS_MOBILE
65+
(void) Mod::get()->hook(
66+
reinterpret_cast<void*>(addresser::getNonVirtual(&geode::cocos::getMousePos)),
67+
&getMousePos_H,
68+
"geode::cocos::getMousePos"
69+
);
70+
#endif
3971
}
4072

4173
void DevTools::newFrame() {
@@ -54,7 +86,9 @@ void DevTools::newFrame() {
5486
io.DeltaTime = director->getDeltaTime();
5587

5688
#ifdef GEODE_IS_DESKTOP
89+
g_useNormalPos = true;
5790
const auto mousePos = toVec2(geode::cocos::getMousePos());
91+
g_useNormalPos = false;
5892
io.AddMousePosEvent(mousePos.x, mousePos.y);
5993
#endif
6094

0 commit comments

Comments
 (0)