1- #include " imgui_impl_android.h"
2- #include " imgui_impl_axmol.h"
1+ #include " imgui_impl_axmol_sw.h"
32#include " axmol/base/Director.h"
43#include " axmol/base/EventListenerTouch.h"
54#include " axmol/base/IMEDelegate.h"
65#include " axmol/rhi/axmol-rhi.h"
76#include < functional>
8- #include < android/native_window.h>
9- #include < android/input.h>
10- #include < android/keycodes.h>
11- #include < android/log.h>
127#include < imgui_internal.h>
138#include " axmol/base/IMEDelegate.h"
149#include " axmol/base/EventDispatcher.h"
@@ -49,15 +44,15 @@ class KeyboardInputDelegate : public IMEDelegate
4944 }
5045};
5146
52- // Android data
47+ // Axmol SingleWindow Platform data
5348
54- struct ImGui_ImplAndroid_Data
49+ struct ImGui_ImplAxmolSW_Data
5550{
5651 RenderView* Window{nullptr };
5752 double Time{0 };
5853 bool InstalledCallbacks{false };
5954
60- // ImGui_ImplAndroid_Data () { memset(this, 0, sizeof(*this)); }
55+ // ImGui_ImplAxmolSW_Data () { memset(this, 0, sizeof(*this)); }
6156
6257 // axmol spec data
6358 Vec2 ViewResolution = Vec2(1920 , 1080 );
@@ -68,14 +63,14 @@ struct ImGui_ImplAndroid_Data
6863};
6964
7065// Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts
71- static ImGui_ImplAndroid_Data* ImGui_ImplAndroid_GetBackendData ()
66+ static ImGui_ImplAxmolSW_Data* ImGui_ImplAxmolSW_GetBackendData ()
7267{
73- return ImGui::GetCurrentContext () ? (ImGui_ImplAndroid_Data *)ImGui::GetIO ().BackendPlatformUserData : nullptr ;
68+ return ImGui::GetCurrentContext () ? (ImGui_ImplAxmolSW_Data *)ImGui::GetIO ().BackendPlatformUserData : nullptr ;
7469}
7570
7671static ax::Vec2 convertToScreen (const Vec2& pos)
7772{
78- auto * bd = ImGui_ImplAndroid_GetBackendData ();
73+ auto * bd = ImGui_ImplAxmolSW_GetBackendData ();
7974 ImGuiIO& io = ImGui::GetIO ();
8075 auto origin = bd->Window ->getViewportRect ().origin ;
8176 auto uiX = (pos.x * bd->Window ->getScaleX () + origin.x ) / io.DisplayFramebufferScale .x ;
@@ -110,15 +105,15 @@ static bool ImGui_ImplAxmol_HitTest(const ImVec2& p)
110105static int s_CapturedTouchId = -1 ;
111106
112107// Functions
113- bool ImGui_ImplAndroid_InitForAxmol (RenderView* window, bool install_callbacks)
108+ bool ImGui_ImplAxmolSW_InitForAxmol (RenderView* window, bool install_callbacks)
114109{
115110 ImGuiIO& io = ImGui::GetIO ();
116111 IM_ASSERT (io.BackendPlatformUserData == nullptr && " Already initialized a platform backend!" );
117112
118113 // Setup backend capabilities flags
119- ImGui_ImplAndroid_Data * bd = IM_NEW (ImGui_ImplAndroid_Data )();
114+ ImGui_ImplAxmolSW_Data * bd = IM_NEW (ImGui_ImplAxmolSW_Data )();
120115 io.BackendPlatformUserData = (void *)bd;
121- io.BackendPlatformName = " imgui_impl_android " ;
116+ io.BackendPlatformName = " imgui_impl_axmol_sw " ;
122117 // io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
123118 io.BackendFlags |=
124119 ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
@@ -143,7 +138,7 @@ bool ImGui_ImplAndroid_InitForAxmol(RenderView* window, bool install_callbacks)
143138
144139 touchListener->onTouchBegan = [](Touch* touch, Event* event) -> bool {
145140 ImGuiIO& io = ImGui::GetIO ();
146- ImGui_ImplAndroid_Data * bd = ImGui_ImplAndroid_GetBackendData ();
141+ ImGui_ImplAxmolSW_Data * bd = ImGui_ImplAxmolSW_GetBackendData ();
147142 auto location = convertToScreen (touch->getLocationInView ());
148143 auto touchPos = ImVec2 (location.x , location.y );
149144
@@ -169,7 +164,7 @@ bool ImGui_ImplAndroid_InitForAxmol(RenderView* window, bool install_callbacks)
169164 if (touch->getID () != s_CapturedTouchId)
170165 return ;
171166 ImGuiIO& io = ImGui::GetIO ();
172- ImGui_ImplAndroid_Data * bd = ImGui_ImplAndroid_GetBackendData ();
167+ ImGui_ImplAxmolSW_Data * bd = ImGui_ImplAxmolSW_GetBackendData ();
173168 auto location = convertToScreen (touch->getLocationInView ());
174169 io.AddMousePosEvent (location.x , location.y );
175170 bd->LastValidMousePos = ImVec2 (location.x , location.y );
@@ -179,7 +174,7 @@ bool ImGui_ImplAndroid_InitForAxmol(RenderView* window, bool install_callbacks)
179174 if (touch->getID () != s_CapturedTouchId)
180175 return ;
181176 ImGuiIO& io = ImGui::GetIO ();
182- ImGui_ImplAndroid_Data * bd = ImGui_ImplAndroid_GetBackendData ();
177+ ImGui_ImplAxmolSW_Data * bd = ImGui_ImplAxmolSW_GetBackendData ();
183178 auto location = convertToScreen (touch->getLocationInView ());
184179 io.AddMousePosEvent (location.x , location.y );
185180 bd->LastValidMousePos = ImVec2 (location.x , location.y );
@@ -204,7 +199,7 @@ bool ImGui_ImplAndroid_InitForAxmol(RenderView* window, bool install_callbacks)
204199 return ;
205200
206201 ImGuiIO& io = ImGui::GetIO ();
207- ImGui_ImplAndroid_Data * bd = ImGui_ImplAndroid_GetBackendData ();
202+ ImGui_ImplAxmolSW_Data * bd = ImGui_ImplAxmolSW_GetBackendData ();
208203 auto location = convertToScreen (touch->getLocationInView ());
209204 io.AddMousePosEvent (location.x , location.y );
210205 bd->LastValidMousePos = ImVec2 (location.x , location.y );
@@ -219,30 +214,28 @@ bool ImGui_ImplAndroid_InitForAxmol(RenderView* window, bool install_callbacks)
219214 return true ;
220215}
221216
222- void ImGui_ImplAndroid_Shutdown ()
217+ void ImGui_ImplAxmolSW_Shutdown ()
223218{
224- ImGui_ImplAndroid_Data * bd = ImGui_ImplAndroid_GetBackendData ();
219+ ImGui_ImplAxmolSW_Data * bd = ImGui_ImplAxmolSW_GetBackendData ();
225220 IM_ASSERT (bd != nullptr && " No platform backend to shutdown, or already shutdown?" );
226221 ImGuiIO& io = ImGui::GetIO ();
227222
228223 io.BackendPlatformName = nullptr ;
229224 io.BackendPlatformUserData = nullptr ;
230225 io.BackendRendererUserData = nullptr ;
231226
232- #if defined(__ANDROID__)
233227 Director::getInstance ()->getEventDispatcher ()->removeEventListener (bd->TouchListener );
234228 AX_SAFE_RELEASE_NULL (bd->TouchListener );
235- #endif
236229
237230 IM_DELETE (bd);
238231}
239232
240- void ImGui_ImplAndroid_NewFrame ()
233+ void ImGui_ImplAxmolSW_NewFrame ()
241234{
242235 ImGuiIO& io = ImGui::GetIO ();
243236
244- ImGui_ImplAndroid_Data * bd = ImGui_ImplAndroid_GetBackendData ();
245- IM_ASSERT (bd != nullptr && " Did you call ImGui_ImplAndroid_InitForXXX ()?" );
237+ ImGui_ImplAxmolSW_Data * bd = ImGui_ImplAxmolSW_GetBackendData ();
238+ IM_ASSERT (bd != nullptr && " Did you call ImGui_ImplAxmolSW_InitForXXX ()?" );
246239
247240 // Setup display size (every frame to accommodate for window resizing)
248241 int32_t window_width = bd->ViewResolution .width ;
@@ -255,48 +248,17 @@ void ImGui_ImplAndroid_NewFrame()
255248 io.DisplayFramebufferScale = ImVec2 ((float )display_width / window_width, (float )display_height / window_height);
256249
257250 // Setup time step
258- struct timespec current_timespec ;
259- clock_gettime (CLOCK_MONOTONIC, ¤t_timespec );
260- double current_time = ( double )(current_timespec. tv_sec ) + (current_timespec. tv_nsec / 1000000000.0 ) ;
251+ auto now = std::chrono::high_resolution_clock::now () ;
252+ auto duration = now. time_since_epoch ( );
253+ double current_time = std::chrono::duration_cast<std::chrono::nanoseconds>(duration). count () / 1e9 ;
261254 io.DeltaTime = bd->Time > 0.0 ? (float )(current_time - bd->Time ) : (float )(1 .0f / 60 .0f );
262255 bd->Time = current_time;
263256}
264257
265- // --------------------------------------------------------------------------------------------------------
266- // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
267- // This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports
268- // simultaneously. If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you
269- // completely ignore this section first..
270- // --------------------------------------------------------------------------------------------------------
271-
272- // Helper structure we store in the void* RenderUserData field of each ImGuiViewport to easily retrieve our backend
273- // data.
274- struct ImGui_ImplAndroid_ViewportData
275- {
276- RenderView* Window;
277- bool WindowOwned;
278- int IgnoreWindowPosEventFrame;
279- int IgnoreWindowSizeEventFrame;
280-
281- ImGui_ImplAndroid_ViewportData ()
282- {
283- Window = nullptr ;
284- WindowOwned = false ;
285- IgnoreWindowSizeEventFrame = IgnoreWindowPosEventFrame = -1 ;
286- }
287- ~ImGui_ImplAndroid_ViewportData () { IM_ASSERT (Window == nullptr ); }
288- };
289-
290- static void ImGui_ImplAndroid_WindowCloseCallback (RenderView* window)
291- {
292- if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle (window))
293- viewport->PlatformRequestClose = true ;
294- }
295-
296258// @imgui_impl_axmol.h
297259IMGUI_IMPL_API void ImGui_ImplAxmol_SetViewResolution (float width, float height)
298260{
299- auto bd = ImGui_ImplAndroid_GetBackendData ();
261+ auto bd = ImGui_ImplAxmolSW_GetBackendData ();
300262 bd->ViewResolution .set (width, height);
301263}
302264
0 commit comments