Skip to content

Commit 618dfc5

Browse files
authored
Fix getMousePos to be relative to the GD view (#56)
2 parents 5ee027a + cb71169 commit 618dfc5

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

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
auto& io = ImGui::GetIO();
1639

@@ -32,6 +55,15 @@ void DevTools::setupPlatform() {
3255
m_fontTexture->retain();
3356

3457
io.Fonts->SetTexID(reinterpret_cast<ImTextureID>(static_cast<intptr_t>(m_fontTexture->getName())));
58+
59+
// fixes getMousePos to be relative to the GD view
60+
#ifndef GEODE_IS_MOBILE
61+
(void) Mod::get()->hook(
62+
reinterpret_cast<void*>(addresser::getNonVirtual(&geode::cocos::getMousePos)),
63+
&getMousePos_H,
64+
"geode::cocos::getMousePos"
65+
);
66+
#endif
3567
}
3668

3769
void DevTools::newFrame() {
@@ -50,7 +82,9 @@ void DevTools::newFrame() {
5082
io.DeltaTime = director->getDeltaTime();
5183

5284
#ifdef GEODE_IS_DESKTOP
85+
g_useNormalPos = true;
5386
const auto mousePos = toVec2(geode::cocos::getMousePos());
87+
g_useNormalPos = false;
5488
io.AddMousePosEvent(mousePos.x, mousePos.y);
5589
#endif
5690

0 commit comments

Comments
 (0)