Skip to content

Commit 418c28b

Browse files
authored
Make the ImGui extension work on all axmol supported platforms (#3083)
1 parent 09aae8e commit 418c28b

File tree

11 files changed

+63
-112
lines changed

11 files changed

+63
-112
lines changed

axmol/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,12 @@ option(AX_ENABLE_EXT_SPINE "Build extension spine" ${AX_EXT_HINT})
204204
option(AX_ENABLE_EXT_DRAGONBONES "Build extension DragonBones" ${AX_EXT_HINT})
205205
option(AX_ENABLE_EXT_COCOSTUDIO "Build extension cocostudio" ${AX_EXT_HINT})
206206
option(AX_ENABLE_EXT_FAIRYGUI "Build extension FairyGUI" ${AX_EXT_HINT})
207+
option(AX_ENABLE_EXT_IMGUI "Build extension ImGui" ${AX_EXT_HINT})
207208
option(AX_ENABLE_EXT_LIVE2D "Build extension Live2D" OFF)
208209
option(AX_ENABLE_EXT_EFFEKSEER "Build extension Effekseer" OFF)
209210

210211
cmake_dependent_option(AX_ENABLE_EXT_PARTICLE3D "Build extension Particle3D" ${AX_EXT_HINT} "AX_ENABLE_3D" OFF)
211212
cmake_dependent_option(AX_ENABLE_EXT_PHYSICS_NODE "Build extension physics-nodes" ${AX_EXT_HINT} "AX_ENABLE_PHYSICS" OFF)
212-
cmake_dependent_option(AX_ENABLE_EXT_IMGUI "Build extension ImGui" ${AX_EXT_HINT} "NOT IOS AND NOT WINRT" OFF)
213213
cmake_dependent_option(AX_ENABLE_EXT_INSPECTOR "Build extension Inspector" ${AX_EXT_HINT} "AX_ENABLE_EXT_IMGUI" OFF)
214214
cmake_dependent_option(AX_ENABLE_EXT_SDFGEN "Build extension SDFGen" ${AX_EXT_HINT} "AX_ENABLE_EXT_IMGUI" OFF)
215215

extensions/ImGui/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ set(SOURCE
2525
list(APPEND SOURCE src/ImGui/backends/imgui_impl_axmol.cpp)
2626
list(APPEND HEADER src/ImGui/backends/imgui_impl_axmol.h)
2727

28-
if(ANDROID)
29-
list(APPEND SOURCE src/ImGui/backends/imgui_impl_android.cpp)
30-
list(APPEND HEADER src/ImGui/backends/imgui_impl_android.h)
28+
if(WINRT OR ANDROID OR IOS)
29+
list(APPEND SOURCE src/ImGui/backends/imgui_impl_axmol_sw.cpp)
30+
list(APPEND HEADER src/ImGui/backends/imgui_impl_axmol_sw.h)
3131
else()
3232
list(APPEND SOURCE src/ImGui/backends/imgui_impl_glfw.cpp)
3333
list(APPEND HEADER src/ImGui/backends/imgui_impl_glfw.h)

extensions/ImGui/src/ImGui/ImGuiPresenter.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ THE SOFTWARE.
2424

2525
#include "ImGuiPresenter.h"
2626
#include <assert.h>
27-
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
28-
# include "backends/imgui_impl_android.h"
29-
#else
27+
#if defined(AX_PLATFORM_PC)
3028
# include "backends/imgui_impl_glfw.h"
29+
# include "backends/imgui_impl_axmol.h"
30+
#else
31+
# include "backends/imgui_impl_axmol_sw.h"
3132
#endif
32-
33-
#include "backends/imgui_impl_axmol.h"
34-
3533
#include "imgui_internal.h"
3634
#include "misc/freetype/imgui_freetype.h"
3735

@@ -165,7 +163,7 @@ class ImGuiSceneEventTracker : public ImGuiEventTracker
165163
public:
166164
bool initWithScene(Scene* scene)
167165
{
168-
#if defined(AX_PLATFORM_PC) || defined(__EMSCRIPTEN__)
166+
#if defined(AX_PLATFORM_PC)
169167
_trackLayer = utils::newInstance<Node>(&Node::initLayer);
170168

171169
// note: when at the first click to focus the window, this will not take effect
@@ -206,7 +204,7 @@ class ImGuiSceneEventTracker : public ImGuiEventTracker
206204

207205
~ImGuiSceneEventTracker() override
208206
{
209-
#if defined(AX_PLATFORM_PC) || defined(__EMSCRIPTEN__)
207+
#if defined(AX_PLATFORM_PC)
210208
if (_trackLayer)
211209
{
212210
if (_trackLayer->getParent())
@@ -227,7 +225,7 @@ class ImGuiGlobalEventTracker : public ImGuiEventTracker
227225
public:
228226
bool init()
229227
{
230-
#if defined(AX_PLATFORM_PC) || defined(__EMSCRIPTEN__)
228+
#if defined(AX_PLATFORM_PC)
231229
// note: when at the first click to focus the window, this will not take effect
232230

233231
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
@@ -252,7 +250,7 @@ class ImGuiGlobalEventTracker : public ImGuiEventTracker
252250

253251
~ImGuiGlobalEventTracker() override
254252
{
255-
#if defined(AX_PLATFORM_PC) || defined(__EMSCRIPTEN__)
253+
#if defined(AX_PLATFORM_PC)
256254
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
257255
eventDispatcher->removeEventListener(_mouseListener);
258256
eventDispatcher->removeEventListener(_touchListener);
@@ -325,11 +323,11 @@ void ImGuiPresenter::init()
325323
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
326324
}
327325

328-
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
329-
ImGui_ImplAndroid_InitForAxmol(Director::getInstance()->getRenderView(), true);
330-
#else
326+
#if defined(AX_PLATFORM_PC)
331327
auto window = static_cast<RenderViewImpl*>(Director::getInstance()->getRenderView())->getWindow();
332328
ImGui_ImplGlfw_InitForAxmol(window, true);
329+
#else
330+
ImGui_ImplAxmolSW_InitForAxmol(Director::getInstance()->getRenderView(), true);
333331
#endif
334332
ImGui_ImplAxmol_Init();
335333

@@ -360,10 +358,10 @@ void ImGuiPresenter::cleanup()
360358

361359
ImGui_ImplAxmol_SetUpdateFontsFunc(nullptr, nullptr);
362360
ImGui_ImplAxmol_Shutdown();
363-
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
364-
ImGui_ImplAndroid_Shutdown();
365-
#else
361+
#if defined(AX_PLATFORM_PC)
366362
ImGui_ImplGlfw_Shutdown();
363+
#else
364+
ImGui_ImplAxmolSW_Shutdown();
367365
#endif
368366

369367
ImGui::DestroyContext();
@@ -481,10 +479,10 @@ void ImGuiPresenter::beginFrame()
481479
{
482480
// create frame
483481
ImGui_ImplAxmol_NewFrame();
484-
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
485-
ImGui_ImplAndroid_NewFrame();
486-
#else
482+
#if defined(AX_PLATFORM_PC)
487483
ImGui_ImplGlfw_NewFrame();
484+
#else
485+
ImGui_ImplAxmolSW_NewFrame();
488486
#endif
489487
ImGui::NewFrame();
490488

extensions/ImGui/src/ImGui/backends/imgui_impl_android.h

Lines changed: 0 additions & 16 deletions
This file was deleted.

extensions/ImGui/src/ImGui/backends/imgui_impl_axmol.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "axmol/base/Director.h"
44
#include "axmol/base/Data.h"
5-
#if !defined(__ANDROID__)
5+
#if defined(AX_PLATFORM_PC)
66
# include "axmol/platform/RenderViewImpl.h"
77
#endif
88
#include "axmol/rhi/Program.h"
@@ -470,7 +470,7 @@ IMGUI_IMPL_API void ImGui_ImplAxmol_RenderPlatform()
470470
ImGui::UpdatePlatformWindows();
471471
ImGui::RenderPlatformWindowsDefault();
472472

473-
#if !defined(__ANDROID__)
473+
#if defined(AX_PLATFORM_PC)
474474
// restore context
475475
if (rhi::DriverContext::isOpenGL())
476476
{
@@ -638,7 +638,7 @@ static void ImGui_ImplAxmol_ShutdownMultiViewportSupport()
638638
ImGui::DestroyPlatformWindows();
639639
}
640640

641-
#if !defined(__ANDROID__)
641+
#if defined(AX_PLATFORM_PC)
642642
IMGUI_IMPL_API void ImGui_ImplAxmol_SetViewResolution(float width, float height)
643643
{
644644
// Resize (expand) window

extensions/ImGui/src/ImGui/backends/imgui_impl_android.cpp renamed to extensions/ImGui/src/ImGui/backends/imgui_impl_axmol_sw.cpp

Lines changed: 23 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
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

7671
static 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)
110105
static 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, &current_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
297259
IMGUI_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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
#include "imgui_impl_axmol.h"
3+
#include "axmol/platform/RenderView.h"
4+
5+
/// ImGui Axmol SingleWindow platform spec APIs
6+
IMGUI_IMPL_API bool ImGui_ImplAxmolSW_InitForAxmol(ax::RenderView* window, bool install_callbacks);
7+
IMGUI_IMPL_API void ImGui_ImplAxmolSW_Shutdown();
8+
IMGUI_IMPL_API void ImGui_ImplAxmolSW_NewFrame();

tests/cpp-tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ if(AX_ENABLE_EXT_IMGUI)
395395
list(APPEND GAME_HEADER Source/ImGuiTest/ImGuiTest.h)
396396
list(APPEND GAME_SOURCE Source/ImGuiTest/ImGuiTest.cpp)
397397

398+
# Box2DTestBed is only works on PC platforms, because it requires glfw which is not available on mobile platforms
398399
if((WINDOWS OR MACOSX OR LINUX OR WASM) AND(NOT WINRT))
399400
file(GLOB_RECURSE BOX2D_TESTBED_SOURCES Source/Box2DTestBed/*.h;Source/Box2DTestBed/*.cpp;Source/Box2DTestBed/*.c)
400401
list(APPEND GAME_SOURCE ${BOX2D_TESTBED_SOURCES})

tests/cpp-tests/Source/controller.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ class RootTests : public TestList
6161
addTest("AudioEngine", []() { return new AudioEngineTests(); });
6262

6363
addTest("Box2D - Basic", []() { return new Box2DTests(); });
64-
#if AX_ENABLE_EXT_IMGUI && AX_TARGET_PLATFORM != AX_PLATFORM_ANDROID
64+
65+
// Box2DTestBed is only works on PC platforms, because it requires glfw which is not available on mobile platforms
66+
#if AX_ENABLE_EXT_IMGUI && defined(AX_PLATFORM_PC)
6567
addTest("Box2D - TestBed", []() { return new Box2DTestBedTests(); });
6668
#endif
6769
addTest("Bugs", []() { return new BugsTests(); });

0 commit comments

Comments
 (0)