Skip to content

Commit 1f08426

Browse files
update imgui, set mouse cursor, drag on empty space on mobile (#67)
Co-authored-by: matcool <[email protected]>
1 parent a42efd5 commit 1f08426

File tree

8 files changed

+100
-8
lines changed

8 files changed

+100
-8
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ imgui/**
2020
imgui
2121

2222
**/.DS_Store
23-
.cache/
23+
.cache/
24+
/CMakeSettings.json
25+
/.vs

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ endif()
2929

3030
add_subdirectory($ENV{GEODE_SDK} ${CMAKE_CURRENT_BINARY_DIR}/geode)
3131

32-
CPMAddPackage("gh:ocornut/imgui@1.91.0-docking")
32+
CPMAddPackage("gh:ocornut/imgui@1.92.4-docking")
3333

3434
target_include_directories(${PROJECT_NAME} PRIVATE ${imgui_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/include)
3535

src/DevTools.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,36 @@ void DevTools::addCustomCallback(std::function<void(CCNode*)> callback) {
106106
m_customCallbacks.push_back(std::move(callback));
107107
}
108108

109+
// Scroll when dragging empty space
110+
void mobileScrollBehavior() {
111+
auto* ctx = ImGui::GetCurrentContext();
112+
auto* window = ctx->CurrentWindow;
113+
if (!window) return;
114+
115+
bool hovered = false;
116+
bool held = false;
117+
ImGuiID id = window->GetID("##scroll_dragging_overlay");
118+
ImGui::KeepAliveID(id);
119+
// If nothing hovered so far in the frame (not same as IsAnyItemHovered()!)
120+
if (ctx->HoveredId == 0) {
121+
ImGui::ButtonBehavior(window->Rect(), id, &hovered, &held, ImGuiButtonFlags_MouseButtonLeft);
122+
}
123+
if (held) {
124+
ImVec2 delta = ImGui::GetIO().MouseDownDuration[0] > 0.1 ? -ImGui::GetIO().MouseDelta : ImVec2(0, 0);
125+
if (std::abs(delta.x) >= 0.1f || std::abs(delta.y) >= 0.1f) {
126+
ImGui::SetScrollX(window, window->Scroll.x + delta.x);
127+
ImGui::SetScrollY(window, window->Scroll.y + delta.y);
128+
}
129+
}
130+
}
131+
109132
void DevTools::drawPage(const char* name, void(DevTools::*pageFun)()) {
110133
if (ImGui::Begin(name, nullptr, ImGuiWindowFlags_HorizontalScrollbar)) {
111134
(this->*pageFun)();
135+
136+
#ifdef GEODE_IS_MOBILE
137+
mobileScrollBehavior();
138+
#endif
112139
}
113140
ImGui::End();
114141
}
@@ -208,6 +235,10 @@ void DevTools::draw(GLRenderCtx* ctx) {
208235
if (this->shouldUseGDWindow()) this->drawGD(ctx);
209236
ImGui::PopFont();
210237
}
238+
239+
#ifdef GEODE_IS_WINDOWS
240+
setMouseCursor();
241+
#endif
211242
}
212243

213244
void DevTools::setupFonts() {
@@ -301,4 +332,5 @@ void DevTools::sceneChanged() {
301332

302333
bool DevTools::shouldUseGDWindow() const {
303334
return Mod::get()->getSettingValue<bool>("should-use-gd-window");
304-
}
335+
336+
}

src/backend.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void DevTools::setupPlatform() {
5757
m_fontTexture->initWithData(pixels, kCCTexture2DPixelFormat_RGBA8888, width, height, CCSize(width, height));
5858
m_fontTexture->retain();
5959

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

6262
// fixes getMousePos to be relative to the GD view
6363
#ifndef GEODE_IS_MOBILE
@@ -219,7 +219,7 @@ void DevTools::renderDrawDataFallback(ImDrawData* draw_data) {
219219
auto* idxBuffer = list->IdxBuffer.Data;
220220
auto* vtxBuffer = list->VtxBuffer.Data;
221221
for (auto& cmd : list->CmdBuffer) {
222-
ccGLBindTexture2D(static_cast<GLuint>(reinterpret_cast<intptr_t>(cmd.GetTexID())));
222+
ccGLBindTexture2D(static_cast<GLuint>(cmd.GetTexID()));
223223

224224
const auto rect = cmd.ClipRect;
225225
const auto orig = toCocos(ImVec2(rect.x, rect.y));
@@ -306,7 +306,7 @@ void DevTools::renderDrawData(ImDrawData* draw_data) {
306306
glBufferData(GL_ELEMENT_ARRAY_BUFFER, list->IdxBuffer.Size * sizeof(ImDrawIdx), list->IdxBuffer.Data, GL_STREAM_DRAW);
307307

308308
for (auto& cmd : list->CmdBuffer) {
309-
ccGLBindTexture2D(static_cast<GLuint>(reinterpret_cast<intptr_t>(cmd.GetTexID())));
309+
ccGLBindTexture2D(static_cast<GLuint>(cmd.GetTexID()));
310310

311311
const auto rect = cmd.ClipRect;
312312
const auto orig = toCocos(ImVec2(rect.x, rect.y));

src/platform/Win32.cpp

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,53 @@ std::string formatAddressIntoOffsetImpl(uintptr_t addr, bool module) {
4444
return fmt::format("{:#x}", addr - reinterpret_cast<uintptr_t>(mod));
4545
}
4646

47-
#endif
47+
// mostly copied from gd-imgui-cocos
48+
void setMouseCursor() {
49+
// Shows imgui's cursor instead of hidden cursor if out of GD Window
50+
bool isCursorVisible = false;
51+
CURSORINFO ci = { sizeof(ci) };
52+
if (GetCursorInfo(&ci)) {
53+
isCursorVisible = (ci.flags & CURSOR_SHOWING) != 0;
54+
}
55+
// whether to draw a fake cursor
56+
ImGui::GetIO().MouseDrawCursor = DevTools::get()->isVisible() && !isCursorVisible && !shouldPassEventsToGDButTransformed();
57+
58+
struct GLFWCursorData {
59+
void* next = nullptr;
60+
HCURSOR cursor;
61+
};
62+
auto& cursorField = *reinterpret_cast<GLFWCursorData**>(reinterpret_cast<uintptr_t>(
63+
CCEGLView::get()->getWindow()) + 0x50);
64+
65+
auto cursor = ImGui::GetIO().MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor();
66+
static ImGuiMouseCursor lastCursor = ImGuiMouseCursor_COUNT;
67+
if (cursor != lastCursor) {
68+
lastCursor = cursor;
69+
70+
auto winCursor = IDC_ARROW;
71+
switch (cursor) {
72+
case ImGuiMouseCursor_Arrow: winCursor = IDC_ARROW; break;
73+
case ImGuiMouseCursor_TextInput: winCursor = IDC_IBEAM; break;
74+
case ImGuiMouseCursor_ResizeAll: winCursor = IDC_SIZEALL; break;
75+
case ImGuiMouseCursor_ResizeEW: winCursor = IDC_SIZEWE; break;
76+
case ImGuiMouseCursor_ResizeNS: winCursor = IDC_SIZENS; break;
77+
case ImGuiMouseCursor_ResizeNESW: winCursor = IDC_SIZENESW; break;
78+
case ImGuiMouseCursor_ResizeNWSE: winCursor = IDC_SIZENWSE; break;
79+
case ImGuiMouseCursor_Hand: winCursor = IDC_HAND; break;
80+
case ImGuiMouseCursor_NotAllowed: winCursor = IDC_NO; break;
81+
}
82+
if (cursorField) {
83+
cursorField->cursor = LoadCursor(NULL, winCursor);
84+
}
85+
else {
86+
// must be heap allocated
87+
cursorField = new GLFWCursorData {
88+
.next = nullptr,
89+
.cursor = LoadCursor(NULL, winCursor)
90+
};
91+
}
92+
}
93+
}
94+
95+
#endif
96+

src/platform/platform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void GLRenderCtx::cleanup() {
4343
GLRenderCtx::GLRenderCtx(ImVec2 const& size) : m_size(size) {}
4444

4545
ImTextureID GLRenderCtx::texture() const {
46-
return reinterpret_cast<ImTextureID>(static_cast<uintptr_t>(m_texture));
46+
return static_cast<ImTextureID>(m_texture);
4747
}
4848

4949
ImVec2 GLRenderCtx::size() const {

src/platform/platform.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#elif defined(GEODE_IS_IOS)
99
#include <OpenGLES/ES2/gl.h>
1010
#endif
11+
#include <Geode/platform/cplatform.h>
1112

1213
ImRect& getGDWindowRect();
1314
bool& shouldPassEventsToGDButTransformed();
@@ -35,3 +36,7 @@ class GLRenderCtx final {
3536
bool begin();
3637
void end();
3738
};
39+
40+
#ifdef GEODE_IS_WINDOWS
41+
void setMouseCursor();
42+
#endif

src/themes.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,10 @@ void applyCommon(ImGuiStyle& style) {
311311
// style.FrameRounding = 2.0f;
312312
// style.WindowPadding = { 3.f, 3.f };
313313
// style.ColorButtonPosition = ImGuiDir_Left;
314+
315+
//special sets for imgui 1.92.4 to keep old look of devtools
316+
style.TabRounding = 0.f;
317+
style.TabBarOverlineSize = 2.f;
314318
}
315319

316320
ThemeDef getThemeDef(std::string const& name) {

0 commit comments

Comments
 (0)