2
0

imgui_impl_android.cpp 16 KB

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