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