Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
auto& io = ImGui::GetIO();

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

io.Fonts->SetTexID(reinterpret_cast<ImTextureID>(static_cast<intptr_t>(m_fontTexture->getName())));

// fixes getMousePos to be relative to the GD view
#ifndef GEODE_IS_MOBILE
(void) Mod::get()->hook(
reinterpret_cast<void*>(addresser::getNonVirtual(&geode::cocos::getMousePos)),
&getMousePos_H,
"geode::cocos::getMousePos"
);
#endif
}

void DevTools::newFrame() {
Expand All @@ -50,7 +82,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

Expand Down