imgui_impl_android.cpp 16 KB

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