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