@@ -11,9 +11,30 @@ using namespace cocos2d;
11
11
12
12
// based off https://github.com/matcool/gd-imgui-cocos
13
13
14
- void DevTools::setupPlatform () {
15
- ImGui::CreateContext ();
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 ();
16
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
+
37
+ void DevTools::setupPlatform () {
17
38
auto & io = ImGui::GetIO ();
18
39
19
40
io.BackendPlatformUserData = this ;
@@ -29,13 +50,20 @@ void DevTools::setupPlatform() {
29
50
int width, height;
30
51
io.Fonts ->GetTexDataAsRGBA32 (&pixels, &width, &height);
31
52
32
- auto * tex2d = new CCTexture2D;
33
- tex2d->initWithData (pixels, kCCTexture2DPixelFormat_RGBA8888 , width, height, CCSize (width, height));
53
+ m_fontTexture = new CCTexture2D;
54
+ m_fontTexture->initWithData (pixels, kCCTexture2DPixelFormat_RGBA8888 , width, height, CCSize (width, height));
55
+ m_fontTexture->retain ();
34
56
35
- // TODO: not leak this :-)
36
- tex2d->retain ();
57
+ io.Fonts ->SetTexID (reinterpret_cast <ImTextureID>(static_cast <intptr_t >(m_fontTexture->getName ())));
37
58
38
- io.Fonts ->SetTexID (reinterpret_cast <ImTextureID>(static_cast <intptr_t >(tex2d->getName ())));
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
39
67
}
40
68
41
69
void DevTools::newFrame () {
@@ -54,7 +82,9 @@ void DevTools::newFrame() {
54
82
io.DeltaTime = director->getDeltaTime ();
55
83
56
84
#ifdef GEODE_IS_DESKTOP
85
+ g_useNormalPos = true ;
57
86
const auto mousePos = toVec2 (geode::cocos::getMousePos ());
87
+ g_useNormalPos = false ;
58
88
io.AddMousePosEvent (mousePos.x , mousePos.y );
59
89
#endif
60
90
0 commit comments