Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/ext/imgui/imgui_impl_platform.cpp
3187 views
1
#include "ext/imgui/imgui.h"
2
#include "Common/File/Path.h"
3
#include "Common/Input/KeyCodes.h"
4
#include "Common/Data/Encoding/Utf8.h"
5
#include "Common/System/Display.h"
6
#include "Common/TimeUtil.h"
7
#include "Common/StringUtils.h"
8
#include "Common/Log.h"
9
10
#include "imgui_impl_platform.h"
11
12
static ImGuiMouseCursor g_cursor = ImGuiMouseCursor_Arrow;
13
Bounds g_imguiCentralNodeBounds;
14
15
void ImGui_ImplPlatform_KeyEvent(const KeyInput &key) {
16
ImGuiIO &io = ImGui::GetIO();
17
18
if (key.flags & KEY_DOWN) {
19
// Specially handle scroll events and any other special keys.
20
switch (key.keyCode) {
21
case NKCODE_EXT_MOUSEWHEEL_UP:
22
io.AddMouseWheelEvent(0, key.Delta() * 0.010f);
23
break;
24
case NKCODE_EXT_MOUSEWHEEL_DOWN:
25
io.AddMouseWheelEvent(0, -key.Delta() * 0.010f);
26
break;
27
default:
28
{
29
ImGuiKey keyCode = KeyCodeToImGui(key.keyCode);
30
if (keyCode != ImGuiKey_None) {
31
io.AddKeyEvent(keyCode, true);
32
}
33
break;
34
}
35
}
36
}
37
if (key.flags & KEY_UP) {
38
ImGuiKey keyCode = KeyCodeToImGui(key.keyCode);
39
if (keyCode != ImGuiKey_None) {
40
io.AddKeyEvent(keyCode, false);
41
}
42
}
43
if (key.flags & KEY_CHAR) {
44
const int unichar = key.unicodeChar;
45
46
if (unichar >= 0x20) {
47
// Insert it! (todo: do it with a string insert)
48
char buf[16];
49
buf[u8_wc_toutf8(buf, unichar)] = '\0';
50
io.AddInputCharactersUTF8(buf);
51
}
52
}
53
}
54
55
void ImGui_ImplPlatform_TouchEvent(const TouchInput &touch) {
56
ImGuiIO& io = ImGui::GetIO();
57
58
// We use real pixels in the imgui, no DPI adjustment yet.
59
float x = touch.x / g_display.dpi_scale_x;
60
float y = touch.y / g_display.dpi_scale_y;
61
62
if (touch.flags & TOUCH_MOVE) {
63
io.AddMousePosEvent(x, y);
64
}
65
if (touch.flags & TOUCH_DOWN) {
66
io.AddMousePosEvent(x, y);
67
if (touch.flags & TOUCH_MOUSE) {
68
if (touch.buttons & 1)
69
io.AddMouseButtonEvent(0, true);
70
if (touch.buttons & 2)
71
io.AddMouseButtonEvent(1, true);
72
} else {
73
io.AddMouseButtonEvent(0, true);
74
}
75
}
76
if (touch.flags & TOUCH_UP) {
77
io.AddMousePosEvent(x, y);
78
if (touch.flags & TOUCH_MOUSE) {
79
if (touch.buttons & 1)
80
io.AddMouseButtonEvent(0, false);
81
if (touch.buttons & 2)
82
io.AddMouseButtonEvent(1, false);
83
} else {
84
io.AddMouseButtonEvent(0, false);
85
}
86
}
87
}
88
89
void ImGui_ImplPlatform_Init(const Path &configPath) {
90
static char path[1024];
91
truncate_cpy(path, configPath.ToString());
92
ImGui::GetIO().IniFilename = path;
93
ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_DockingEnable;
94
}
95
96
void ImGui_ImplPlatform_AxisEvent(const AxisInput &axis) {
97
// Ignore for now.
98
}
99
100
void ImGui_ImplPlatform_NewFrame() {
101
static double lastTime = 0.0;
102
if (lastTime == 0.0) {
103
lastTime = time_now_d();
104
}
105
106
double now = time_now_d();
107
108
g_cursor = ImGui::GetMouseCursor();
109
ImGuiIO &io = ImGui::GetIO();
110
io.DisplaySize = ImVec2(g_display.pixel_xres, g_display.pixel_yres);
111
io.DisplayFramebufferScale = ImVec2(1.0f, 1.0f);
112
io.DeltaTime = now - lastTime;
113
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
114
lastTime = now;
115
}
116
117
ImGuiMouseCursor ImGui_ImplPlatform_GetCursor() {
118
return g_cursor;
119
}
120
121
// Written by chatgpt
122
ImGuiKey KeyCodeToImGui(InputKeyCode keyCode) {
123
switch (keyCode) {
124
case NKCODE_DPAD_UP: return ImGuiKey_UpArrow;
125
case NKCODE_DPAD_DOWN: return ImGuiKey_DownArrow;
126
case NKCODE_DPAD_LEFT: return ImGuiKey_LeftArrow;
127
case NKCODE_DPAD_RIGHT: return ImGuiKey_RightArrow;
128
case NKCODE_ENTER: return ImGuiKey_Enter;
129
case NKCODE_ESCAPE: return ImGuiKey_Escape;
130
case NKCODE_SHIFT_LEFT: return ImGuiKey_LeftShift;
131
case NKCODE_SHIFT_RIGHT: return ImGuiKey_RightShift;
132
case NKCODE_ALT_LEFT: return ImGuiKey_LeftAlt;
133
case NKCODE_ALT_RIGHT: return ImGuiKey_RightAlt;
134
case NKCODE_CTRL_LEFT: return ImGuiKey_LeftCtrl;
135
case NKCODE_CTRL_RIGHT: return ImGuiKey_RightCtrl;
136
case NKCODE_TAB: return ImGuiKey_Tab;
137
case NKCODE_MENU: return ImGuiKey_Menu;
138
case NKCODE_DEL: return ImGuiKey_Backspace;
139
case NKCODE_FORWARD_DEL: return ImGuiKey_Delete;
140
case NKCODE_CAPS_LOCK: return ImGuiKey_CapsLock;
141
case NKCODE_SPACE: return ImGuiKey_Space;
142
case NKCODE_PAGE_UP: return ImGuiKey_PageUp;
143
case NKCODE_PAGE_DOWN: return ImGuiKey_PageDown;
144
case NKCODE_MOVE_HOME: return ImGuiKey_Home;
145
case NKCODE_MOVE_END: return ImGuiKey_End;
146
case NKCODE_INSERT: return ImGuiKey_Insert;
147
148
// Numeric keys
149
case NKCODE_0: return ImGuiKey_0;
150
case NKCODE_1: return ImGuiKey_1;
151
case NKCODE_2: return ImGuiKey_2;
152
case NKCODE_3: return ImGuiKey_3;
153
case NKCODE_4: return ImGuiKey_4;
154
case NKCODE_5: return ImGuiKey_5;
155
case NKCODE_6: return ImGuiKey_6;
156
case NKCODE_7: return ImGuiKey_7;
157
case NKCODE_8: return ImGuiKey_8;
158
case NKCODE_9: return ImGuiKey_9;
159
160
// Letter keys
161
case NKCODE_A: return ImGuiKey_A;
162
case NKCODE_B: return ImGuiKey_B;
163
case NKCODE_C: return ImGuiKey_C;
164
case NKCODE_D: return ImGuiKey_D;
165
case NKCODE_E: return ImGuiKey_E;
166
case NKCODE_F: return ImGuiKey_F;
167
case NKCODE_G: return ImGuiKey_G;
168
case NKCODE_H: return ImGuiKey_H;
169
case NKCODE_I: return ImGuiKey_I;
170
case NKCODE_J: return ImGuiKey_J;
171
case NKCODE_K: return ImGuiKey_K;
172
case NKCODE_L: return ImGuiKey_L;
173
case NKCODE_M: return ImGuiKey_M;
174
case NKCODE_N: return ImGuiKey_N;
175
case NKCODE_O: return ImGuiKey_O;
176
case NKCODE_P: return ImGuiKey_P;
177
case NKCODE_Q: return ImGuiKey_Q;
178
case NKCODE_R: return ImGuiKey_R;
179
case NKCODE_S: return ImGuiKey_S;
180
case NKCODE_T: return ImGuiKey_T;
181
case NKCODE_U: return ImGuiKey_U;
182
case NKCODE_V: return ImGuiKey_V;
183
case NKCODE_W: return ImGuiKey_W;
184
case NKCODE_X: return ImGuiKey_X;
185
case NKCODE_Y: return ImGuiKey_Y;
186
case NKCODE_Z: return ImGuiKey_Z;
187
188
// Symbols
189
case NKCODE_COMMA: return ImGuiKey_Comma;
190
case NKCODE_PERIOD: return ImGuiKey_Period;
191
case NKCODE_MINUS: return ImGuiKey_Minus;
192
case NKCODE_PLUS: return ImGuiKey_Equal; // Hmm
193
case NKCODE_EQUALS: return ImGuiKey_Equal;
194
case NKCODE_LEFT_BRACKET: return ImGuiKey_LeftBracket;
195
case NKCODE_RIGHT_BRACKET: return ImGuiKey_RightBracket;
196
case NKCODE_BACKSLASH: return ImGuiKey_Backslash;
197
case NKCODE_SEMICOLON: return ImGuiKey_Semicolon;
198
case NKCODE_APOSTROPHE: return ImGuiKey_Apostrophe;
199
case NKCODE_SLASH: return ImGuiKey_Slash;
200
case NKCODE_GRAVE: return ImGuiKey_GraveAccent;
201
202
// Function keys
203
case NKCODE_F1: return ImGuiKey_F1;
204
case NKCODE_F2: return ImGuiKey_F2;
205
case NKCODE_F3: return ImGuiKey_F3;
206
case NKCODE_F4: return ImGuiKey_F4;
207
case NKCODE_F5: return ImGuiKey_F5;
208
case NKCODE_F6: return ImGuiKey_F6;
209
case NKCODE_F7: return ImGuiKey_F7;
210
case NKCODE_F8: return ImGuiKey_F8;
211
case NKCODE_F9: return ImGuiKey_F9;
212
case NKCODE_F10: return ImGuiKey_F10;
213
case NKCODE_F11: return ImGuiKey_F11;
214
case NKCODE_F12: return ImGuiKey_F12;
215
216
// Keypad
217
case NKCODE_NUMPAD_0: return ImGuiKey_Keypad0;
218
case NKCODE_NUMPAD_1: return ImGuiKey_Keypad1;
219
case NKCODE_NUMPAD_2: return ImGuiKey_Keypad2;
220
case NKCODE_NUMPAD_3: return ImGuiKey_Keypad3;
221
case NKCODE_NUMPAD_4: return ImGuiKey_Keypad4;
222
case NKCODE_NUMPAD_5: return ImGuiKey_Keypad5;
223
case NKCODE_NUMPAD_6: return ImGuiKey_Keypad6;
224
case NKCODE_NUMPAD_7: return ImGuiKey_Keypad7;
225
case NKCODE_NUMPAD_8: return ImGuiKey_Keypad8;
226
case NKCODE_NUMPAD_9: return ImGuiKey_Keypad9;
227
case NKCODE_NUMPAD_DIVIDE: return ImGuiKey_KeypadDivide;
228
case NKCODE_NUMPAD_MULTIPLY: return ImGuiKey_KeypadMultiply;
229
case NKCODE_NUMPAD_SUBTRACT: return ImGuiKey_KeypadSubtract;
230
case NKCODE_NUMPAD_ADD: return ImGuiKey_KeypadAdd;
231
case NKCODE_NUMPAD_ENTER: return ImGuiKey_KeypadEnter;
232
case NKCODE_NUMPAD_EQUALS: return ImGuiKey_KeypadEqual;
233
234
// Lock keys
235
case NKCODE_NUM_LOCK: return ImGuiKey_NumLock;
236
case NKCODE_SCROLL_LOCK: return ImGuiKey_ScrollLock;
237
238
case NKCODE_EXT_MOUSEBUTTON_1:
239
case NKCODE_EXT_MOUSEBUTTON_2:
240
case NKCODE_EXT_MOUSEBUTTON_3:
241
case NKCODE_EXT_MOUSEBUTTON_4:
242
case NKCODE_EXT_MOUSEBUTTON_5:
243
case NKCODE_EXT_MOUSEWHEEL_DOWN:
244
case NKCODE_EXT_MOUSEWHEEL_UP:
245
// Keys ignored for imgui
246
return ImGuiKey_None;
247
case NKCODE_PRINTSCREEN:
248
return ImGuiKey_PrintScreen;
249
250
case NKCODE_EXT_PIPE:
251
// No valid mapping exists.
252
return ImGuiKey_None;
253
254
default:
255
WARN_LOG(Log::System, "Unmapped ImGui keycode conversion from %d", keyCode);
256
return ImGuiKey_None;
257
}
258
}
259
260