imgui_impl_android.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // [ ] Platform: Clipboard support.
  6. // [ ] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
  7. // [ ] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. FIXME: Check if this is even possible with Android.
  8. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  9. // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
  10. // https://github.com/ocornut/imgui
  11. // CHANGELOG
  12. // (minor and older changes stripped away, please see git history for details)
  13. // 2021-03-02: Support for physical pointer device input (such as physical mouse)
  14. // 2020-09-13: Support for Unicode characters
  15. // 2020-08-31: On-screen and physical keyboard input (ASCII characters only)
  16. // 2020-03-02: basic draft, touch input
  17. #include "imgui.h"
  18. #include "imgui_impl_android.h"
  19. #include <time.h>
  20. #include <map>
  21. #include <queue>
  22. // Android
  23. #include <android/native_window.h>
  24. #include <android/input.h>
  25. #include <android/keycodes.h>
  26. #include <android/log.h>
  27. static double g_Time = 0.0;
  28. static ANativeWindow* g_Window;
  29. static char g_LogTag[] = "ImguiExample";
  30. static std::map<int32_t, std::queue<int32_t>> g_KeyEventQueues; // FIXME: Remove dependency on map and queue once we use upcoming input queue.
  31. int32_t ImGui_ImplAndroid_HandleInputEvent(AInputEvent* inputEvent)
  32. {
  33. ImGuiIO& io = ImGui::GetIO();
  34. int32_t event_type = AInputEvent_getType(inputEvent);
  35. switch (event_type)
  36. {
  37. case AINPUT_EVENT_TYPE_KEY:
  38. {
  39. int32_t event_key_code = AKeyEvent_getKeyCode(inputEvent);
  40. int32_t event_action = AKeyEvent_getAction(inputEvent);
  41. int32_t event_meta_state = AKeyEvent_getMetaState(inputEvent);
  42. io.KeyCtrl = ((event_meta_state & AMETA_CTRL_ON) != 0);
  43. io.KeyShift = ((event_meta_state & AMETA_SHIFT_ON) != 0);
  44. io.KeyAlt = ((event_meta_state & AMETA_ALT_ON) != 0);
  45. switch (event_action)
  46. {
  47. // FIXME: AKEY_EVENT_ACTION_DOWN and AKEY_EVENT_ACTION_UP occur at once
  48. // as soon as a touch pointer goes up from a key. We use a simple key event queue
  49. // and process one event per key per ImGui frame in ImGui_ImplAndroid_NewFrame().
  50. // ...or consider ImGui 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(inputEvent);
  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(inputEvent, event_pointer_index) == AMOTION_EVENT_TOOL_TYPE_FINGER)
  73. || (AMotionEvent_getToolType(inputEvent, event_pointer_index) == AMOTION_EVENT_TOOL_TYPE_UNKNOWN))
  74. {
  75. io.MouseDown[0] = (event_action == AMOTION_EVENT_ACTION_DOWN) ? true : false;
  76. io.MousePos = ImVec2(
  77. AMotionEvent_getX(inputEvent, event_pointer_index),
  78. AMotionEvent_getY(inputEvent, event_pointer_index));
  79. }
  80. break;
  81. case AMOTION_EVENT_ACTION_BUTTON_PRESS:
  82. case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
  83. {
  84. int32_t button_state = AMotionEvent_getButtonState(inputEvent);
  85. io.MouseDown[0] = (button_state & AMOTION_EVENT_BUTTON_PRIMARY) ? true : false;
  86. io.MouseDown[1] = (button_state & AMOTION_EVENT_BUTTON_SECONDARY) ? true : false;
  87. io.MouseDown[2] = (button_state & AMOTION_EVENT_BUTTON_TERTIARY) ? true : false;
  88. }
  89. break;
  90. case AMOTION_EVENT_ACTION_HOVER_MOVE: // Hovering: Tool moves while NOT pressed (such as a physical mouse)
  91. case AMOTION_EVENT_ACTION_MOVE: // Touch pointer moves while DOWN
  92. io.MousePos = ImVec2(
  93. AMotionEvent_getX(inputEvent, event_pointer_index),
  94. AMotionEvent_getY(inputEvent, event_pointer_index));
  95. break;
  96. case AMOTION_EVENT_ACTION_SCROLL:
  97. io.MouseWheel = AMotionEvent_getAxisValue(inputEvent, AMOTION_EVENT_AXIS_VSCROLL, event_pointer_index);
  98. io.MouseWheelH = AMotionEvent_getAxisValue(inputEvent, AMOTION_EVENT_AXIS_HSCROLL, event_pointer_index);
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. return 1;
  105. default:
  106. break;
  107. }
  108. return 0;
  109. }
  110. bool ImGui_ImplAndroid_Init(ANativeWindow* window)
  111. {
  112. g_Window = window;
  113. g_Time = 0.0;
  114. // Setup back-end capabilities flags
  115. ImGuiIO& io = ImGui::GetIO();
  116. io.BackendPlatformName = "imgui_impl_android";
  117. // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
  118. io.KeyMap[ImGuiKey_Tab] = AKEYCODE_TAB;
  119. io.KeyMap[ImGuiKey_LeftArrow] = AKEYCODE_DPAD_LEFT; // also covers physical keyboard arrow key
  120. io.KeyMap[ImGuiKey_RightArrow] = AKEYCODE_DPAD_RIGHT; // also covers physical keyboard arrow key
  121. io.KeyMap[ImGuiKey_UpArrow] = AKEYCODE_DPAD_UP; // also covers physical keyboard arrow key
  122. io.KeyMap[ImGuiKey_DownArrow] = AKEYCODE_DPAD_DOWN; // also covers physical keyboard arrow key
  123. io.KeyMap[ImGuiKey_PageUp] = AKEYCODE_PAGE_UP;
  124. io.KeyMap[ImGuiKey_PageDown] = AKEYCODE_PAGE_DOWN;
  125. io.KeyMap[ImGuiKey_Home] = AKEYCODE_MOVE_HOME;
  126. io.KeyMap[ImGuiKey_End] = AKEYCODE_MOVE_END;
  127. io.KeyMap[ImGuiKey_Insert] = AKEYCODE_INSERT;
  128. io.KeyMap[ImGuiKey_Delete] = AKEYCODE_FORWARD_DEL;
  129. io.KeyMap[ImGuiKey_Backspace] = AKEYCODE_DEL;
  130. io.KeyMap[ImGuiKey_Space] = AKEYCODE_SPACE;
  131. io.KeyMap[ImGuiKey_Enter] = AKEYCODE_ENTER;
  132. io.KeyMap[ImGuiKey_Escape] = AKEYCODE_ESCAPE;
  133. io.KeyMap[ImGuiKey_KeyPadEnter] = AKEYCODE_NUMPAD_ENTER;
  134. io.KeyMap[ImGuiKey_A] = AKEYCODE_A;
  135. io.KeyMap[ImGuiKey_C] = AKEYCODE_C;
  136. io.KeyMap[ImGuiKey_V] = AKEYCODE_V;
  137. io.KeyMap[ImGuiKey_X] = AKEYCODE_X;
  138. io.KeyMap[ImGuiKey_Y] = AKEYCODE_Y;
  139. io.KeyMap[ImGuiKey_Z] = AKEYCODE_Z;
  140. return true;
  141. }
  142. void ImGui_ImplAndroid_Shutdown()
  143. {
  144. }
  145. void ImGui_ImplAndroid_NewFrame()
  146. {
  147. ImGuiIO& io = ImGui::GetIO();
  148. IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer back-end. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame().");
  149. // Process queued key events
  150. // FIXME: This is a workaround for multiple key event actions occuring at once (see above) and can be removed once we use upcoming input queue.
  151. for (auto& key_queue : g_KeyEventQueues)
  152. {
  153. if (key_queue.second.empty())
  154. continue;
  155. io.KeysDown[key_queue.first] = (key_queue.second.front() == AKEY_EVENT_ACTION_DOWN);
  156. key_queue.second.pop();
  157. }
  158. // Setup display size (every frame to accommodate for window resizing)
  159. int32_t window_width = ANativeWindow_getWidth(g_Window);
  160. int32_t window_height = ANativeWindow_getHeight(g_Window);
  161. int display_width = window_width;
  162. int display_height = window_height;
  163. io.DisplaySize = ImVec2((float)window_width, (float)window_height);
  164. if (window_width > 0 && window_height > 0)
  165. io.DisplayFramebufferScale = ImVec2((float)display_width / window_width, (float)display_height / window_height);
  166. // Setup time step
  167. struct timespec current_timespec;
  168. clock_gettime(CLOCK_MONOTONIC, &current_timespec);
  169. double current_time = (double)(current_timespec.tv_sec) + (current_timespec.tv_nsec / 1000000000.0);
  170. io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f / 60.0f);
  171. g_Time = current_time;
  172. }