imgui_impl_android.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 arrays indexed using AKEYCODE_* codes, e.g. ImGui::IsKeyPressed(AKEYCODE_SPACE).
  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. // - FIXME: On-screen keyboard currently needs to be enabled by the application (see examples/ and issue #3446)
  11. // - FIXME: Unicode character inputs needs to be passed by Dear ImGui by the application (see examples/ and issue #3446)
  12. // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
  13. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
  14. // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
  15. // Read online: https://github.com/ocornut/imgui/tree/master/docs
  16. // CHANGELOG
  17. // (minor and older changes stripped away, please see git history for details)
  18. // 2021-03-04: Initial version.
  19. #include "imgui.h"
  20. #include "imgui_impl_android.h"
  21. #include <time.h>
  22. #include <map>
  23. #include <queue>
  24. #include <android/native_window.h>
  25. #include <android/input.h>
  26. #include <android/keycodes.h>
  27. #include <android/log.h>
  28. // Android data
  29. static double g_Time = 0.0;
  30. static ANativeWindow* g_Window;
  31. static char g_LogTag[] = "ImGuiExample";
  32. static std::map<int32_t, std::queue<int32_t>> g_KeyEventQueues; // FIXME: Remove dependency on map and queue once we use upcoming input queue.
  33. int32_t ImGui_ImplAndroid_HandleInputEvent(AInputEvent* input_event)
  34. {
  35. ImGuiIO& io = ImGui::GetIO();
  36. int32_t event_type = AInputEvent_getType(input_event);
  37. switch (event_type)
  38. {
  39. case AINPUT_EVENT_TYPE_KEY:
  40. {
  41. int32_t event_key_code = AKeyEvent_getKeyCode(input_event);
  42. int32_t event_action = AKeyEvent_getAction(input_event);
  43. int32_t event_meta_state = AKeyEvent_getMetaState(input_event);
  44. io.KeyCtrl = ((event_meta_state & AMETA_CTRL_ON) != 0);
  45. io.KeyShift = ((event_meta_state & AMETA_SHIFT_ON) != 0);
  46. io.KeyAlt = ((event_meta_state & AMETA_ALT_ON) != 0);
  47. switch (event_action)
  48. {
  49. // FIXME: AKEY_EVENT_ACTION_DOWN and AKEY_EVENT_ACTION_UP occur at once as soon as a touch pointer
  50. // goes up from a key. We use a simple key event queue/ and process one event per key per frame in
  51. // ImGui_ImplAndroid_NewFrame()...or consider using IO queue, if suitable: https://github.com/ocornut/imgui/issues/2787
  52. case AKEY_EVENT_ACTION_DOWN:
  53. case AKEY_EVENT_ACTION_UP:
  54. g_KeyEventQueues[event_key_code].push(event_action);
  55. break;
  56. default:
  57. break;
  58. }
  59. break;
  60. }
  61. case AINPUT_EVENT_TYPE_MOTION:
  62. {
  63. int32_t event_action = AMotionEvent_getAction(input_event);
  64. int32_t event_pointer_index = (event_action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
  65. event_action &= AMOTION_EVENT_ACTION_MASK;
  66. switch (event_action)
  67. {
  68. case AMOTION_EVENT_ACTION_DOWN:
  69. case AMOTION_EVENT_ACTION_UP:
  70. // Physical mouse buttons (and probably other physical devices) also invoke the actions AMOTION_EVENT_ACTION_DOWN/_UP,
  71. // but we have to process them separately to identify the actual button pressed. This is done below via
  72. // AMOTION_EVENT_ACTION_BUTTON_PRESS/_RELEASE. Here, we only process "FINGER" input (and "UNKNOWN", as a fallback).
  73. if((AMotionEvent_getToolType(input_event, event_pointer_index) == AMOTION_EVENT_TOOL_TYPE_FINGER)
  74. || (AMotionEvent_getToolType(input_event, event_pointer_index) == AMOTION_EVENT_TOOL_TYPE_UNKNOWN))
  75. {
  76. io.MouseDown[0] = (event_action == AMOTION_EVENT_ACTION_DOWN);
  77. io.MousePos = ImVec2(AMotionEvent_getX(input_event, event_pointer_index), AMotionEvent_getY(input_event, event_pointer_index));
  78. }
  79. break;
  80. case AMOTION_EVENT_ACTION_BUTTON_PRESS:
  81. case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
  82. {
  83. int32_t button_state = AMotionEvent_getButtonState(input_event);
  84. io.MouseDown[0] = ((button_state & AMOTION_EVENT_BUTTON_PRIMARY) != 0);
  85. io.MouseDown[1] = ((button_state & AMOTION_EVENT_BUTTON_SECONDARY) != 0);
  86. io.MouseDown[2] = ((button_state & AMOTION_EVENT_BUTTON_TERTIARY) != 0);
  87. }
  88. break;
  89. case AMOTION_EVENT_ACTION_HOVER_MOVE: // Hovering: Tool moves while NOT pressed (such as a physical mouse)
  90. case AMOTION_EVENT_ACTION_MOVE: // Touch pointer moves while DOWN
  91. io.MousePos = ImVec2(AMotionEvent_getX(input_event, event_pointer_index), AMotionEvent_getY(input_event, event_pointer_index));
  92. break;
  93. case AMOTION_EVENT_ACTION_SCROLL:
  94. io.MouseWheel = AMotionEvent_getAxisValue(input_event, AMOTION_EVENT_AXIS_VSCROLL, event_pointer_index);
  95. io.MouseWheelH = AMotionEvent_getAxisValue(input_event, AMOTION_EVENT_AXIS_HSCROLL, event_pointer_index);
  96. break;
  97. default:
  98. break;
  99. }
  100. }
  101. return 1;
  102. default:
  103. break;
  104. }
  105. return 0;
  106. }
  107. bool ImGui_ImplAndroid_Init(ANativeWindow* window)
  108. {
  109. g_Window = window;
  110. g_Time = 0.0;
  111. // Setup backend capabilities flags
  112. ImGuiIO& io = ImGui::GetIO();
  113. io.BackendPlatformName = "imgui_impl_android";
  114. // Keyboard mapping. Dear ImGui will use those indices to peek into the io.KeysDown[] array.
  115. io.KeyMap[ImGuiKey_Tab] = AKEYCODE_TAB;
  116. io.KeyMap[ImGuiKey_LeftArrow] = AKEYCODE_DPAD_LEFT; // also covers physical keyboard arrow key
  117. io.KeyMap[ImGuiKey_RightArrow] = AKEYCODE_DPAD_RIGHT; // also covers physical keyboard arrow key
  118. io.KeyMap[ImGuiKey_UpArrow] = AKEYCODE_DPAD_UP; // also covers physical keyboard arrow key
  119. io.KeyMap[ImGuiKey_DownArrow] = AKEYCODE_DPAD_DOWN; // also covers physical keyboard arrow key
  120. io.KeyMap[ImGuiKey_PageUp] = AKEYCODE_PAGE_UP;
  121. io.KeyMap[ImGuiKey_PageDown] = AKEYCODE_PAGE_DOWN;
  122. io.KeyMap[ImGuiKey_Home] = AKEYCODE_MOVE_HOME;
  123. io.KeyMap[ImGuiKey_End] = AKEYCODE_MOVE_END;
  124. io.KeyMap[ImGuiKey_Insert] = AKEYCODE_INSERT;
  125. io.KeyMap[ImGuiKey_Delete] = AKEYCODE_FORWARD_DEL;
  126. io.KeyMap[ImGuiKey_Backspace] = AKEYCODE_DEL;
  127. io.KeyMap[ImGuiKey_Space] = AKEYCODE_SPACE;
  128. io.KeyMap[ImGuiKey_Enter] = AKEYCODE_ENTER;
  129. io.KeyMap[ImGuiKey_Escape] = AKEYCODE_ESCAPE;
  130. io.KeyMap[ImGuiKey_KeyPadEnter] = AKEYCODE_NUMPAD_ENTER;
  131. io.KeyMap[ImGuiKey_A] = AKEYCODE_A;
  132. io.KeyMap[ImGuiKey_C] = AKEYCODE_C;
  133. io.KeyMap[ImGuiKey_V] = AKEYCODE_V;
  134. io.KeyMap[ImGuiKey_X] = AKEYCODE_X;
  135. io.KeyMap[ImGuiKey_Y] = AKEYCODE_Y;
  136. io.KeyMap[ImGuiKey_Z] = AKEYCODE_Z;
  137. return true;
  138. }
  139. void ImGui_ImplAndroid_Shutdown()
  140. {
  141. }
  142. void ImGui_ImplAndroid_NewFrame()
  143. {
  144. ImGuiIO& io = ImGui::GetIO();
  145. // Process queued key events
  146. // 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.
  147. for (auto& key_queue : g_KeyEventQueues)
  148. {
  149. if (key_queue.second.empty())
  150. continue;
  151. io.KeysDown[key_queue.first] = (key_queue.second.front() == AKEY_EVENT_ACTION_DOWN);
  152. key_queue.second.pop();
  153. }
  154. // Setup display size (every frame to accommodate for window resizing)
  155. int32_t window_width = ANativeWindow_getWidth(g_Window);
  156. int32_t window_height = ANativeWindow_getHeight(g_Window);
  157. int display_width = window_width;
  158. int display_height = window_height;
  159. io.DisplaySize = ImVec2((float)window_width, (float)window_height);
  160. if (window_width > 0 && window_height > 0)
  161. io.DisplayFramebufferScale = ImVec2((float)display_width / window_width, (float)display_height / window_height);
  162. // Setup time step
  163. struct timespec current_timespec;
  164. clock_gettime(CLOCK_MONOTONIC, &current_timespec);
  165. double current_time = (double)(current_timespec.tv_sec) + (current_timespec.tv_nsec / 1000000000.0);
  166. io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f / 60.0f);
  167. g_Time = current_time;
  168. }