| 1 |
// dear imgui: Platform Binding for Android native app |
| 2 |
// This needs to be used along with the OpenGL 3 Renderer (imgui_impl_opengl3) |
| 3 |
|
| 4 |
// Implemented features: |
| 5 |
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy AKEYCODE_* values are obsolete since 1.87 and not supported since 1.91.5] |
| 6 |
// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen/Pen. |
| 7 |
// Missing features or Issues: |
| 8 |
// [ ] Platform: Clipboard support. |
| 9 |
// [ ] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. |
| 10 |
// [ ] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. FIXME: Check if this is even possible with Android. |
| 11 |
// Important: |
| 12 |
// - Consider using SDL or GLFW backend on Android, which will be more full-featured than this. |
| 13 |
// - FIXME: On-screen keyboard currently needs to be enabled by the application (see examples/ and issue #3446) |
| 14 |
// - FIXME: Unicode character inputs needs to be passed by Dear ImGui by the application (see examples/ and issue #3446) |
| 15 |
|
| 16 |
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. |
| 17 |
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. |
| 18 |
// Learn about Dear ImGui: |
| 19 |
// - FAQ https://dearimgui.com/faq |
| 20 |
// - Getting Started https://dearimgui.com/getting-started |
| 21 |
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). |
| 22 |
// - Introduction, links and more at the top of imgui.cpp |
| 23 |
|
| 24 |
// CHANGELOG |
| 25 |
// (minor and older changes stripped away, please see git history for details) |
| 26 |
// 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported). |
| 27 |
// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion. |
| 28 |
// 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+). |
| 29 |
// 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range. |
| 30 |
// 2021-03-04: Initial version. |
| 31 |
|
| 32 |
#include "imgui.h" |
| 33 |
#ifndef IMGUI_DISABLE |
| 34 |
#include "imgui_impl_android.h" |
| 35 |
#include <time.h> |
| 36 |
#include <android/native_window.h> |
| 37 |
#include <android/input.h> |
| 38 |
#include <android/keycodes.h> |
| 39 |
#include <android/log.h> |
| 40 |
|
| 41 |
// Android data |
| 42 |
static double g_Time = 0.0; |
| 43 |
static ANativeWindow* g_Window; |
| 44 |
static char g_LogTag[] = "ImGuiExample"; |
| 45 |
|
| 46 |
static ImGuiKey ImGui_ImplAndroid_KeyCodeToImGuiKey(int32_t key_code) |
| 47 |
{ |
| 48 |
switch (key_code) |
| 49 |
{ |
| 50 |
case AKEYCODE_TAB: return ImGuiKey_Tab; |
| 51 |
case AKEYCODE_DPAD_LEFT: return ImGuiKey_LeftArrow; |
| 52 |
case AKEYCODE_DPAD_RIGHT: return ImGuiKey_RightArrow; |
| 53 |
case AKEYCODE_DPAD_UP: return ImGuiKey_UpArrow; |
| 54 |
case AKEYCODE_DPAD_DOWN: return ImGuiKey_DownArrow; |
| 55 |
case AKEYCODE_PAGE_UP: return ImGuiKey_PageUp; |
| 56 |
case AKEYCODE_PAGE_DOWN: return ImGuiKey_PageDown; |
| 57 |
case AKEYCODE_MOVE_HOME: return ImGuiKey_Home; |
| 58 |
case AKEYCODE_MOVE_END: return ImGuiKey_End; |
| 59 |
case AKEYCODE_INSERT: return ImGuiKey_Insert; |
| 60 |
case AKEYCODE_FORWARD_DEL: return ImGuiKey_Delete; |
| 61 |
case AKEYCODE_DEL: return ImGuiKey_Backspace; |
| 62 |
case AKEYCODE_SPACE: return ImGuiKey_Space; |
| 63 |
case AKEYCODE_ENTER: return ImGuiKey_Enter; |
| 64 |
case AKEYCODE_ESCAPE: return ImGuiKey_Escape; |
| 65 |
case AKEYCODE_APOSTROPHE: return ImGuiKey_Apostrophe; |
| 66 |
case AKEYCODE_COMMA: return ImGuiKey_Comma; |
| 67 |
case AKEYCODE_MINUS: return ImGuiKey_Minus; |
| 68 |
case AKEYCODE_PERIOD: return ImGuiKey_Period; |
| 69 |
case AKEYCODE_SLASH: return ImGuiKey_Slash; |
| 70 |
case AKEYCODE_SEMICOLON: return ImGuiKey_Semicolon; |
| 71 |
case AKEYCODE_EQUALS: return ImGuiKey_Equal; |
| 72 |
case AKEYCODE_LEFT_BRACKET: return ImGuiKey_LeftBracket; |
| 73 |
case AKEYCODE_BACKSLASH: return ImGuiKey_Backslash; |
| 74 |
case AKEYCODE_RIGHT_BRACKET: return ImGuiKey_RightBracket; |
| 75 |
case AKEYCODE_GRAVE: return ImGuiKey_GraveAccent; |
| 76 |
case AKEYCODE_CAPS_LOCK: return ImGuiKey_CapsLock; |
| 77 |
case AKEYCODE_SCROLL_LOCK: return ImGuiKey_ScrollLock; |
| 78 |
case AKEYCODE_NUM_LOCK: return ImGuiKey_NumLock; |
| 79 |
case AKEYCODE_SYSRQ: return ImGuiKey_PrintScreen; |
| 80 |
case AKEYCODE_BREAK: return ImGuiKey_Pause; |
| 81 |
case AKEYCODE_NUMPAD_0: return ImGuiKey_Keypad0; |
| 82 |
case AKEYCODE_NUMPAD_1: return ImGuiKey_Keypad1; |
| 83 |
case AKEYCODE_NUMPAD_2: return ImGuiKey_Keypad2; |
| 84 |
case AKEYCODE_NUMPAD_3: return ImGuiKey_Keypad3; |
| 85 |
case AKEYCODE_NUMPAD_4: return ImGuiKey_Keypad4; |
| 86 |
case AKEYCODE_NUMPAD_5: return ImGuiKey_Keypad5; |
| 87 |
case AKEYCODE_NUMPAD_6: return ImGuiKey_Keypad6; |
| 88 |
case AKEYCODE_NUMPAD_7: return ImGuiKey_Keypad7; |
| 89 |
case AKEYCODE_NUMPAD_8: return ImGuiKey_Keypad8; |
| 90 |
case AKEYCODE_NUMPAD_9: return ImGuiKey_Keypad9; |
| 91 |
case AKEYCODE_NUMPAD_DOT: return ImGuiKey_KeypadDecimal; |
| 92 |
case AKEYCODE_NUMPAD_DIVIDE: return ImGuiKey_KeypadDivide; |
| 93 |
case AKEYCODE_NUMPAD_MULTIPLY: return ImGuiKey_KeypadMultiply; |
| 94 |
case AKEYCODE_NUMPAD_SUBTRACT: return ImGuiKey_KeypadSubtract; |
| 95 |
case AKEYCODE_NUMPAD_ADD: return ImGuiKey_KeypadAdd; |
| 96 |
case AKEYCODE_NUMPAD_ENTER: return ImGuiKey_KeypadEnter; |
| 97 |
case AKEYCODE_NUMPAD_EQUALS: return ImGuiKey_KeypadEqual; |
| 98 |
case AKEYCODE_CTRL_LEFT: return ImGuiKey_LeftCtrl; |
| 99 |
case AKEYCODE_SHIFT_LEFT: return ImGuiKey_LeftShift; |
| 100 |
case AKEYCODE_ALT_LEFT: return ImGuiKey_LeftAlt; |
| 101 |
case AKEYCODE_META_LEFT: return ImGuiKey_LeftSuper; |
| 102 |
case AKEYCODE_CTRL_RIGHT: return ImGuiKey_RightCtrl; |
| 103 |
case AKEYCODE_SHIFT_RIGHT: return ImGuiKey_RightShift; |
| 104 |
case AKEYCODE_ALT_RIGHT: return ImGuiKey_RightAlt; |
| 105 |
case AKEYCODE_META_RIGHT: return ImGuiKey_RightSuper; |
| 106 |
case AKEYCODE_MENU: return ImGuiKey_Menu; |
| 107 |
case AKEYCODE_0: return ImGuiKey_0; |
| 108 |
case AKEYCODE_1: return ImGuiKey_1; |
| 109 |
case AKEYCODE_2: return ImGuiKey_2; |
| 110 |
case AKEYCODE_3: return ImGuiKey_3; |
| 111 |
case AKEYCODE_4: return ImGuiKey_4; |
| 112 |
case AKEYCODE_5: return ImGuiKey_5; |
| 113 |
case AKEYCODE_6: return ImGuiKey_6; |
| 114 |
case AKEYCODE_7: return ImGuiKey_7; |
| 115 |
case AKEYCODE_8: return ImGuiKey_8; |
| 116 |
case AKEYCODE_9: return ImGuiKey_9; |
| 117 |
case AKEYCODE_A: return ImGuiKey_A; |
| 118 |
case AKEYCODE_B: return ImGuiKey_B; |
| 119 |
case AKEYCODE_C: return ImGuiKey_C; |
| 120 |
case AKEYCODE_D: return ImGuiKey_D; |
| 121 |
case AKEYCODE_E: return ImGuiKey_E; |
| 122 |
case AKEYCODE_F: return ImGuiKey_F; |
| 123 |
case AKEYCODE_G: return ImGuiKey_G; |
| 124 |
case AKEYCODE_H: return ImGuiKey_H; |
| 125 |
case AKEYCODE_I: return ImGuiKey_I; |
| 126 |
case AKEYCODE_J: return ImGuiKey_J; |
| 127 |
case AKEYCODE_K: return ImGuiKey_K; |
| 128 |
case AKEYCODE_L: return ImGuiKey_L; |
| 129 |
case AKEYCODE_M: return ImGuiKey_M; |
| 130 |
case AKEYCODE_N: return ImGuiKey_N; |
| 131 |
case AKEYCODE_O: return ImGuiKey_O; |
| 132 |
case AKEYCODE_P: return ImGuiKey_P; |
| 133 |
case AKEYCODE_Q: return ImGuiKey_Q; |
| 134 |
case AKEYCODE_R: return ImGuiKey_R; |
| 135 |
case AKEYCODE_S: return ImGuiKey_S; |
| 136 |
case AKEYCODE_T: return ImGuiKey_T; |
| 137 |
case AKEYCODE_U: return ImGuiKey_U; |
| 138 |
case AKEYCODE_V: return ImGuiKey_V; |
| 139 |
case AKEYCODE_W: return ImGuiKey_W; |
| 140 |
case AKEYCODE_X: return ImGuiKey_X; |
| 141 |
case AKEYCODE_Y: return ImGuiKey_Y; |
| 142 |
case AKEYCODE_Z: return ImGuiKey_Z; |
| 143 |
case AKEYCODE_F1: return ImGuiKey_F1; |
| 144 |
case AKEYCODE_F2: return ImGuiKey_F2; |
| 145 |
case AKEYCODE_F3: return ImGuiKey_F3; |
| 146 |
case AKEYCODE_F4: return ImGuiKey_F4; |
| 147 |
case AKEYCODE_F5: return ImGuiKey_F5; |
| 148 |
case AKEYCODE_F6: return ImGuiKey_F6; |
| 149 |
case AKEYCODE_F7: return ImGuiKey_F7; |
| 150 |
case AKEYCODE_F8: return ImGuiKey_F8; |
| 151 |
case AKEYCODE_F9: return ImGuiKey_F9; |
| 152 |
case AKEYCODE_F10: return ImGuiKey_F10; |
| 153 |
case AKEYCODE_F11: return ImGuiKey_F11; |
| 154 |
case AKEYCODE_F12: return ImGuiKey_F12; |
| 155 |
default: return ImGuiKey_None; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
int32_t ImGui_ImplAndroid_HandleInputEvent(const AInputEvent* input_event) |
| 160 |
{ |
| 161 |
ImGuiIO& io = ImGui::GetIO(); |
| 162 |
int32_t event_type = AInputEvent_getType(input_event); |
| 163 |
switch (event_type) |
| 164 |
{ |
| 165 |
case AINPUT_EVENT_TYPE_KEY: |
| 166 |
{ |
| 167 |
int32_t event_key_code = AKeyEvent_getKeyCode(input_event); |
| 168 |
int32_t event_scan_code = AKeyEvent_getScanCode(input_event); |
| 169 |
int32_t event_action = AKeyEvent_getAction(input_event); |
| 170 |
int32_t event_meta_state = AKeyEvent_getMetaState(input_event); |
| 171 |
|
| 172 |
io.AddKeyEvent(ImGuiMod_Ctrl, (event_meta_state & AMETA_CTRL_ON) != 0); |
| 173 |
io.AddKeyEvent(ImGuiMod_Shift, (event_meta_state & AMETA_SHIFT_ON) != 0); |
| 174 |
io.AddKeyEvent(ImGuiMod_Alt, (event_meta_state & AMETA_ALT_ON) != 0); |
| 175 |
io.AddKeyEvent(ImGuiMod_Super, (event_meta_state & AMETA_META_ON) != 0); |
| 176 |
|
| 177 |
switch (event_action) |
| 178 |
{ |
| 179 |
// FIXME: AKEY_EVENT_ACTION_DOWN and AKEY_EVENT_ACTION_UP occur at once as soon as a touch pointer |
| 180 |
// goes up from a key. We use a simple key event queue/ and process one event per key per frame in |
| 181 |
// ImGui_ImplAndroid_NewFrame()...or consider using IO queue, if suitable: https://github.com/ocornut/imgui/issues/2787 |
| 182 |
case AKEY_EVENT_ACTION_DOWN: |
| 183 |
case AKEY_EVENT_ACTION_UP: |
| 184 |
{ |
| 185 |
ImGuiKey key = ImGui_ImplAndroid_KeyCodeToImGuiKey(event_key_code); |
| 186 |
if (key != ImGuiKey_None) |
| 187 |
{ |
| 188 |
io.AddKeyEvent(key, event_action == AKEY_EVENT_ACTION_DOWN); |
| 189 |
io.SetKeyEventNativeData(key, event_key_code, event_scan_code); |
| 190 |
} |
| 191 |
|
| 192 |
break; |
| 193 |
} |
| 194 |
default: |
| 195 |
break; |
| 196 |
} |
| 197 |
break; |
| 198 |
} |
| 199 |
case AINPUT_EVENT_TYPE_MOTION: |
| 200 |
{ |
| 201 |
int32_t event_action = AMotionEvent_getAction(input_event); |
| 202 |
int32_t event_pointer_index = (event_action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 203 |
event_action &= AMOTION_EVENT_ACTION_MASK; |
| 204 |
|
| 205 |
switch (AMotionEvent_getToolType(input_event, event_pointer_index)) |
| 206 |
{ |
| 207 |
case AMOTION_EVENT_TOOL_TYPE_MOUSE: |
| 208 |
io.AddMouseSourceEvent(ImGuiMouseSource_Mouse); |
| 209 |
break; |
| 210 |
case AMOTION_EVENT_TOOL_TYPE_STYLUS: |
| 211 |
case AMOTION_EVENT_TOOL_TYPE_ERASER: |
| 212 |
io.AddMouseSourceEvent(ImGuiMouseSource_Pen); |
| 213 |
break; |
| 214 |
case AMOTION_EVENT_TOOL_TYPE_FINGER: |
| 215 |
default: |
| 216 |
io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen); |
| 217 |
break; |
| 218 |
} |
| 219 |
|
| 220 |
switch (event_action) |
| 221 |
{ |
| 222 |
case AMOTION_EVENT_ACTION_DOWN: |
| 223 |
case AMOTION_EVENT_ACTION_UP: |
| 224 |
{ |
| 225 |
// Physical mouse buttons (and probably other physical devices) also invoke the actions AMOTION_EVENT_ACTION_DOWN/_UP, |
| 226 |
// but we have to process them separately to identify the actual button pressed. This is done below via |
| 227 |
// AMOTION_EVENT_ACTION_BUTTON_PRESS/_RELEASE. Here, we only process "FINGER" input (and "UNKNOWN", as a fallback). |
| 228 |
int tool_type = AMotionEvent_getToolType(input_event, event_pointer_index); |
| 229 |
if (tool_type == AMOTION_EVENT_TOOL_TYPE_FINGER || tool_type == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) |
| 230 |
{ |
| 231 |
io.AddMousePosEvent(AMotionEvent_getX(input_event, event_pointer_index), AMotionEvent_getY(input_event, event_pointer_index)); |
| 232 |
io.AddMouseButtonEvent(0, event_action == AMOTION_EVENT_ACTION_DOWN); |
| 233 |
} |
| 234 |
break; |
| 235 |
} |
| 236 |
case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 237 |
case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 238 |
{ |
| 239 |
int32_t button_state = AMotionEvent_getButtonState(input_event); |
| 240 |
io.AddMouseButtonEvent(0, (button_state & AMOTION_EVENT_BUTTON_PRIMARY) != 0); |
| 241 |
io.AddMouseButtonEvent(1, (button_state & AMOTION_EVENT_BUTTON_SECONDARY) != 0); |
| 242 |
io.AddMouseButtonEvent(2, (button_state & AMOTION_EVENT_BUTTON_TERTIARY) != 0); |
| 243 |
break; |
| 244 |
} |
| 245 |
case AMOTION_EVENT_ACTION_HOVER_MOVE: // Hovering: Tool moves while NOT pressed (such as a physical mouse) |
| 246 |
case AMOTION_EVENT_ACTION_MOVE: // Touch pointer moves while DOWN |
| 247 |
io.AddMousePosEvent(AMotionEvent_getX(input_event, event_pointer_index), AMotionEvent_getY(input_event, event_pointer_index)); |
| 248 |
break; |
| 249 |
case AMOTION_EVENT_ACTION_SCROLL: |
| 250 |
io.AddMouseWheelEvent(AMotionEvent_getAxisValue(input_event, AMOTION_EVENT_AXIS_HSCROLL, event_pointer_index), AMotionEvent_getAxisValue(input_event, AMOTION_EVENT_AXIS_VSCROLL, event_pointer_index)); |
| 251 |
break; |
| 252 |
default: |
| 253 |
break; |
| 254 |
} |
| 255 |
} |
| 256 |
return 1; |
| 257 |
default: |
| 258 |
break; |
| 259 |
} |
| 260 |
|
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
bool ImGui_ImplAndroid_Init(ANativeWindow* window) |
| 265 |
{ |
| 266 |
IMGUI_CHECKVERSION(); |
| 267 |
|
| 268 |
g_Window = window; |
| 269 |
g_Time = 0.0; |
| 270 |
|
| 271 |
// Setup backend capabilities flags |
| 272 |
ImGuiIO& io = ImGui::GetIO(); |
| 273 |
io.BackendPlatformName = "imgui_impl_android"; |
| 274 |
|
| 275 |
return true; |
| 276 |
} |
| 277 |
|
| 278 |
void ImGui_ImplAndroid_Shutdown() |
| 279 |
{ |
| 280 |
ImGuiIO& io = ImGui::GetIO(); |
| 281 |
io.BackendPlatformName = nullptr; |
| 282 |
} |
| 283 |
|
| 284 |
void ImGui_ImplAndroid_NewFrame() |
| 285 |
{ |
| 286 |
ImGuiIO& io = ImGui::GetIO(); |
| 287 |
|
| 288 |
// Setup display size (every frame to accommodate for window resizing) |
| 289 |
int32_t window_width = ANativeWindow_getWidth(g_Window); |
| 290 |
int32_t window_height = ANativeWindow_getHeight(g_Window); |
| 291 |
int display_width = window_width; |
| 292 |
int display_height = window_height; |
| 293 |
|
| 294 |
io.DisplaySize = ImVec2((float)window_width, (float)window_height); |
| 295 |
if (window_width > 0 && window_height > 0) |
| 296 |
io.DisplayFramebufferScale = ImVec2((float)display_width / window_width, (float)display_height / window_height); |
| 297 |
|
| 298 |
// Setup time step |
| 299 |
struct timespec current_timespec; |
| 300 |
clock_gettime(CLOCK_MONOTONIC, ¤t_timespec); |
| 301 |
double current_time = (double)(current_timespec.tv_sec) + (current_timespec.tv_nsec / 1000000000.0); |
| 302 |
io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f / 60.0f); |
| 303 |
g_Time = current_time; |
| 304 |
} |
| 305 |
|
| 306 |
//----------------------------------------------------------------------------- |
| 307 |
|
| 308 |
#endif // #ifndef IMGUI_DISABLE |