imgui_impl_android.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  13. // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
  14. // https://github.com/ocornut/imgui
  15. // CHANGELOG
  16. // (minor and older changes stripped away, please see git history for details)
  17. // 2021-03-04: Initial version.
  18. #include "imgui.h"
  19. #include "imgui_impl_android.h"
  20. #include <time.h>
  21. #include <map>
  22. #include <queue>
  23. #include <android/native_window.h>
  24. #include <android/input.h>
  25. #include <android/keycodes.h>
  26. #include <android/log.h>
  27. // Android data
  28. static double g_Time = 0.0;
  29. static ANativeWindow* g_Window;
  30. static char g_LogTag[] = "ImGuiExample";
  31. static std::map<int32_t, std::queue<int32_t>> g_KeyEventQueues; // FIXME: Remove dependency on map and queue once we use upcoming input queue.
  32. int32_t ImGui_ImplAndroid_HandleInputEvent(AInputEvent* input_event)
  33. {
  34. ImGuiIO& io = ImGui::GetIO();
  35. int32_t event_type = AInputEvent_getType(input_event);
  36. switch (event_type)
  37. {
  38. case AINPUT_EVENT_TYPE_KEY:
  39. {
  40. int32_t event_key_code = AKeyEvent_getKeyCode(input_event);
  41. int32_t event_action = AKeyEvent_getAction(input_event);
  42. int32_t event_meta_state = AKeyEvent_getMetaState(input_event);
  43. io.KeyCtrl = ((event_meta_state & AMETA_CTRL_ON) != 0);
  44. io.KeyShift = ((event_meta_state & AMETA_SHIFT_ON) != 0);
  45. io.KeyAlt = ((event_meta_state & AMETA_ALT_ON) != 0);
  46. switch (event_action)
  47. {
  48. // FIXME: AKEY_EVENT_ACTION_DOWN and AKEY_EVENT_ACTION_UP occur at once as soon as a touch pointer
  49. // goes up from a key. We use a simple key event queue/ and process one event per key per frame in
  50. // ImGui_ImplAndroid_NewFrame()...or consider using IO queue, if suitable: https://github.com/ocornut/imgui/issues/2787
  51. case AKEY_EVENT_ACTION_DOWN:
  52. case AKEY_EVENT_ACTION_UP:
  53. g_KeyEventQueues[event_key_code].push(event_action);
  54. break;
  55. default:
  56. break;
  57. }
  58. break;
  59. }
  60. case AINPUT_EVENT_TYPE_MOTION:
  61. {
  62. int32_t event_action = AMotionEvent_getAction(input_event);
  63. int32_t event_pointer_index = (event_action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
  64. event_action &= AMOTION_EVENT_ACTION_MASK;
  65. switch (event_action)
  66. {
  67. case AMOTION_EVENT_ACTION_DOWN:
  68. case AMOTION_EVENT_ACTION_UP:
  69. // Physical mouse buttons (and probably other physical devices) also invoke the actions AMOTION_EVENT_ACTION_DOWN/_UP,
  70. // but we have to process them separately to identify the actual button pressed. This is done below via
  71. // AMOTION_EVENT_ACTION_BUTTON_PRESS/_RELEASE. Here, we only process "FINGER" input (and "UNKNOWN", as a fallback).
  72. if((AMotionEvent_getToolType(input_event, event_pointer_index) == AMOTION_EVENT_TOOL_TYPE_FINGER)
  73. || (AMotionEvent_getToolType(input_event, event_pointer_index) == AMOTION_EVENT_TOOL_TYPE_UNKNOWN))
  74. {
  75. io.MouseDown[0] = (event_action == AMOTION_EVENT_ACTION_DOWN);
  76. io.MousePos = ImVec2(AMotionEvent_getX(input_event, event_pointer_index), AMotionEvent_getY(input_event, event_pointer_index));
  77. }
  78. break;
  79. case AMOTION_EVENT_ACTION_BUTTON_PRESS:
  80. case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
  81. {
  82. int32_t button_state = AMotionEvent_getButtonState(input_event);
  83. io.MouseDown[0] = ((button_state & AMOTION_EVENT_BUTTON_PRIMARY) != 0);
  84. io.MouseDown[1] = ((button_state & AMOTION_EVENT_BUTTON_SECONDARY) != 0);
  85. io.MouseDown[2] = ((button_state & AMOTION_EVENT_BUTTON_TERTIARY) != 0);
  86. }
  87. break;
  88. case AMOTION_EVENT_ACTION_HOVER_MOVE: // Hovering: Tool moves while NOT pressed (such as a physical mouse)
  89. case AMOTION_EVENT_ACTION_MOVE: // Touch pointer moves while DOWN
  90. io.MousePos = ImVec2(AMotionEvent_getX(input_event, event_pointer_index), AMotionEvent_getY(input_event, event_pointer_index));
  91. break;
  92. case AMOTION_EVENT_ACTION_SCROLL:
  93. io.MouseWheel = AMotionEvent_getAxisValue(input_event, AMOTION_EVENT_AXIS_VSCROLL, event_pointer_index);
  94. io.MouseWheelH = AMotionEvent_getAxisValue(input_event, AMOTION_EVENT_AXIS_HSCROLL, event_pointer_index);
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. return 1;
  101. default:
  102. break;
  103. }
  104. return 0;
  105. }
  106. bool ImGui_ImplAndroid_Init(ANativeWindow* window)
  107. {
  108. g_Window = window;
  109. g_Time = 0.0;
  110. // Setup backend capabilities flags
  111. ImGuiIO& io = ImGui::GetIO();
  112. io.BackendPlatformName = "imgui_impl_android";
  113. // Keyboard mapping. Dear ImGui will use those indices to peek into the io.KeysDown[] array.
  114. io.KeyMap[ImGuiKey_Tab] = AKEYCODE_TAB;
  115. io.KeyMap[ImGuiKey_LeftArrow] = AKEYCODE_DPAD_LEFT; // also covers physical keyboard arrow key
  116. io.KeyMap[ImGuiKey_RightArrow] = AKEYCODE_DPAD_RIGHT; // also covers physical keyboard arrow key
  117. io.KeyMap[ImGuiKey_UpArrow] = AKEYCODE_DPAD_UP; // also covers physical keyboard arrow key
  118. io.KeyMap[ImGuiKey_DownArrow] = AKEYCODE_DPAD_DOWN; // also covers physical keyboard arrow key
  119. io.KeyMap[ImGuiKey_PageUp] = AKEYCODE_PAGE_UP;
  120. io.KeyMap[ImGuiKey_PageDown] = AKEYCODE_PAGE_DOWN;
  121. io.KeyMap[ImGuiKey_Home] = AKEYCODE_MOVE_HOME;
  122. io.KeyMap[ImGuiKey_End] = AKEYCODE_MOVE_END;
  123. io.KeyMap[ImGuiKey_Insert] = AKEYCODE_INSERT;
  124. io.KeyMap[ImGuiKey_Delete] = AKEYCODE_FORWARD_DEL;
  125. io.KeyMap[ImGuiKey_Backspace] = AKEYCODE_DEL;
  126. io.KeyMap[ImGuiKey_Space] = AKEYCODE_SPACE;
  127. io.KeyMap[ImGuiKey_Enter] = AKEYCODE_ENTER;
  128. io.KeyMap[ImGuiKey_Escape] = AKEYCODE_ESCAPE;
  129. io.KeyMap[ImGuiKey_KeyPadEnter] = AKEYCODE_NUMPAD_ENTER;
  130. io.KeyMap[ImGuiKey_A] = AKEYCODE_A;
  131. io.KeyMap[ImGuiKey_C] = AKEYCODE_C;
  132. io.KeyMap[ImGuiKey_V] = AKEYCODE_V;
  133. io.KeyMap[ImGuiKey_X] = AKEYCODE_X;
  134. io.KeyMap[ImGuiKey_Y] = AKEYCODE_Y;
  135. io.KeyMap[ImGuiKey_Z] = AKEYCODE_Z;
  136. return true;
  137. }
  138. void ImGui_ImplAndroid_Shutdown()
  139. {
  140. }
  141. void ImGui_ImplAndroid_NewFrame()
  142. {
  143. ImGuiIO& io = ImGui::GetIO();
  144. IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer backend. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame().");
  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. }