2
0

imgui_impl_android.cpp 16 KB

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