Skip to content

Commit 5b31b51

Browse files
committed
add keyboard input via CCIMEDispatcher
1 parent 127b967 commit 5b31b51

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/backend.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <cocos2d.h>
22
#include <Geode/modify/CCTouchDispatcher.hpp>
3+
#include <Geode/modify/CCIMEDispatcher.hpp>
34
#include "platform/platform.hpp"
45
#include "DevTools.hpp"
56
#include "ImGui.hpp"
@@ -197,4 +198,25 @@ class $modify(CCTouchDispatcher) {
197198
}
198199
}
199200
}
201+
};
202+
203+
class $modify(CCIMEDispatcher) {
204+
void dispatchInsertText(const char* text, int len) {
205+
auto& io = ImGui::GetIO();
206+
if (!io.WantCaptureKeyboard) {
207+
CCIMEDispatcher::dispatchInsertText(text, len);
208+
}
209+
std::string str(text, len);
210+
io.AddInputCharactersUTF8(str.c_str());
211+
}
212+
213+
void dispatchDeleteBackward() {
214+
auto& io = ImGui::GetIO();
215+
if (!io.WantCaptureKeyboard) {
216+
CCIMEDispatcher::dispatchDeleteBackward();
217+
}
218+
// is this really how youre supposed to do this
219+
io.AddKeyEvent(ImGuiKey_Backspace, true);
220+
io.AddKeyEvent(ImGuiKey_Backspace, false);
221+
}
200222
};

src/platform/Win32.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,44 @@
66
#include "platform.hpp"
77

88
using namespace cocos2d;
9+
using namespace geode;
10+
11+
ImGuiKey keyFromGLFW(int key) {
12+
if (key >= GLFW_KEY_0 && key <= GLFW_KEY_9) {
13+
return static_cast<ImGuiKey>(ImGuiKey_0 + (key - GLFW_KEY_0));
14+
} else if (key >= GLFW_KEY_A && key <= GLFW_KEY_Z) {
15+
return static_cast<ImGuiKey>(ImGuiKey_A + (key - GLFW_KEY_A));
16+
}
17+
switch (key) {
18+
case GLFW_KEY_SPACE: return ImGuiKey_Space;
19+
case GLFW_KEY_BACKSPACE: return ImGuiKey_Backspace;
20+
case GLFW_KEY_COMMA: return ImGuiKey_Comma;
21+
case GLFW_KEY_LEFT: return ImGuiKey_LeftArrow;
22+
case GLFW_KEY_RIGHT: return ImGuiKey_RightArrow;
23+
case GLFW_KEY_UP: return ImGuiKey_UpArrow;
24+
case GLFW_KEY_DOWN: return ImGuiKey_DownArrow;
25+
case GLFW_KEY_ESCAPE: return ImGuiKey_Escape;
26+
case GLFW_KEY_LEFT_SHIFT: return ImGuiKey_LeftShift;
27+
case GLFW_KEY_RIGHT_SHIFT: return ImGuiKey_RightShift;
28+
case GLFW_KEY_LEFT_CONTROL: return ImGuiKey_LeftCtrl;
29+
case GLFW_KEY_LEFT_ALT: return ImGuiKey_LeftAlt;
30+
// TODO: rest :-)
31+
}
32+
return ImGuiKey_None;
33+
}
934

1035
class $modify(CCEGLView) {
1136
void updateWindow(int width, int height) {
1237
shouldUpdateGDRenderBuffer() = true;
1338
CCEGLView::updateWindow(width, height);
1439
}
40+
41+
void onGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
42+
auto& io = ImGui::GetIO();
43+
CCEGLView::onGLFWKeyCallback(window, key, scancode, action, mods);
44+
// in practice this is only used for arrow keys
45+
io.AddKeyEvent(keyFromGLFW(key), action != GLFW_RELEASE);
46+
}
1547
};
1648

1749
#endif

0 commit comments

Comments
 (0)