From ba37e3009df79a01d8e3a8126b7fa5a09ccac91b Mon Sep 17 00:00:00 2001 From: halx99 Date: Tue, 12 Sep 2023 19:30:15 +0800 Subject: [PATCH] Improve ImGui loop managment --- extensions/ImGui/ImGuiPresenter.cpp | 87 ++++++++++++++++------------- extensions/ImGui/ImGuiPresenter.h | 7 ++- 2 files changed, 54 insertions(+), 40 deletions(-) diff --git a/extensions/ImGui/ImGuiPresenter.cpp b/extensions/ImGui/ImGuiPresenter.cpp index e0922fa917..14c91921a6 100644 --- a/extensions/ImGui/ImGuiPresenter.cpp +++ b/extensions/ImGui/ImGuiPresenter.cpp @@ -1,9 +1,9 @@ #include "ImGuiPresenter.h" #include #if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID) - #include "imgui_impl_ax_android.h" +# include "imgui_impl_ax_android.h" #else - #include "imgui_impl_ax.h" +# include "imgui_impl_ax.h" #endif #include "imgui_internal.h" @@ -236,8 +236,10 @@ void ImGuiPresenter::cleanup() ImGui::DestroyContext(); - if(!_renderLoops.empty()) { - for(auto item : _renderLoops) { + if (!_renderLoops.empty()) + { + for (auto item : _renderLoops) + { delete item.second.tracker; } _renderLoops.clear(); @@ -288,6 +290,9 @@ float ImGuiPresenter::enableDPIScale(float userScale) #if (AX_TARGET_PLATFORM != AX_PLATFORM_ANDROID) // Gets scale glfwGetMonitorContentScale(glfwGetPrimaryMonitor(), &xscale, nullptr); +#else + // refer to: https://developer.android.com/training/multiscreen/screendensities?hl=en-us + xscale = Device::getDPI() / 160.0f; #endif auto zoomFactor = userScale * xscale; @@ -414,14 +419,17 @@ void ImGuiPresenter::update() usedCCRef.clear(); // drawing commands - for (auto iter = _renderLoops.begin(); iter != _renderLoops.end(); ) { + for (auto iter = _renderLoops.begin(); iter != _renderLoops.end();) + { auto& imLoop = iter->second; - if(imLoop.removing) { - delete imLoop.tracker; - iter = _renderLoops.erase(iter); + if (imLoop.removing) + { + auto tracker = imLoop.tracker; + iter = _renderLoops.erase(iter); + delete tracker; continue; } - imLoop.func(); // invoke ImGui loop func + imLoop.func(); // invoke ImGui loop func ++iter; } @@ -430,25 +438,28 @@ void ImGuiPresenter::update() bool ImGuiPresenter::addRenderLoop(std::string_view id, std::function func, Scene* target) { - // TODO: check whether exist + + auto tracker = target ? static_cast(utils::newInstance( + &ImGuiSceneEventTracker::initWithScene, target)) + : static_cast(utils::newInstance()); + auto fourccId = fourccValue(id); - if (_renderLoops.find(fourccId) != _renderLoops.end()) - { - return false; - } - - ImGuiEventTracker* tracker; - if (target) - tracker = utils::newInstance(&ImGuiSceneEventTracker::initWithScene, target); - else - tracker = utils::newInstance(); - - if (tracker) + auto iter = _renderLoops.find(fourccId); + if (iter == _renderLoops.end()) { _renderLoops.emplace(fourccId, ImGuiLoop{tracker, std::move(func)}); return true; } - return false; + + // allow reuse imLoop, update func, tracker, removing status + auto& imLoop = iter->second; + imLoop.func = std::move(func); + imLoop.tracker = tracker; + + if (imLoop.removing) + imLoop.removing = false; + + return true; } void ImGuiPresenter::removeRenderLoop(std::string_view id) @@ -476,11 +487,11 @@ static std::tuple getTextureUV(Sprite* sp) } void ImGuiPresenter::image(Texture2D* tex, - const ImVec2& size, - const ImVec2& uv0, - const ImVec2& uv1, - const ImVec4& tint_col, - const ImVec4& border_col) + const ImVec2& size, + const ImVec2& uv0, + const ImVec2& uv1, + const ImVec4& tint_col, + const ImVec4& border_col) { if (!tex) return; @@ -512,12 +523,12 @@ void ImGuiPresenter::image(Sprite* sprite, const ImVec2& size, const ImVec4& tin } bool ImGuiPresenter::imageButton(Texture2D* tex, - const ImVec2& size, - const ImVec2& uv0, - const ImVec2& uv1, - int frame_padding, - const ImVec4& bg_col, - const ImVec4& tint_col) + const ImVec2& size, + const ImVec2& uv0, + const ImVec2& uv1, + int frame_padding, + const ImVec4& bg_col, + const ImVec4& tint_col) { if (!tex) return false; @@ -533,10 +544,10 @@ bool ImGuiPresenter::imageButton(Texture2D* tex, } bool ImGuiPresenter::imageButton(Sprite* sprite, - const ImVec2& size, - int frame_padding, - const ImVec4& bg_col, - const ImVec4& tint_col) + const ImVec2& size, + int frame_padding, + const ImVec4& bg_col, + const ImVec4& tint_col) { if (!sprite || !sprite->getTexture()) return false; diff --git a/extensions/ImGui/ImGuiPresenter.h b/extensions/ImGui/ImGuiPresenter.h index 1792925900..5200ea5b33 100644 --- a/extensions/ImGui/ImGuiPresenter.h +++ b/extensions/ImGui/ImGuiPresenter.h @@ -3,7 +3,7 @@ #include #include -#include "cocos2d.h" +#include "axmol.h" #include "ExtensionMacros.h" #include "imgui/imgui.h" @@ -36,14 +36,17 @@ public: static void destroyInstance(); static void setOnInit(const std::function& callBack); + /// deprecated use enableDPIScale instead + float scaleAllByDPI(float userScale = 1.0f) { return enableDPIScale(userScale); } + /// /// Scale ImGui with majorMoniter DPI scaling /// /// Usually is 1.0 /// The full path of .ttc/.ttf file /// The final contentZoomFactor = userScale * dpiScale - float scaleAllByDPI(float userScale = 1.0f) { return enableDPIScale(userScale); } float enableDPIScale(float userScale = 1.0f); + float getContentZoomFactor() const { return _contentZoomFactor; } void setViewResolution(float width, float height);