Improve ImGui loop managment

This commit is contained in:
halx99 2023-09-12 19:30:15 +08:00
parent 23ea89c8e6
commit ba37e3009d
2 changed files with 54 additions and 40 deletions

View File

@ -236,8 +236,10 @@ void ImGuiPresenter::cleanup()
ImGui::DestroyContext(); ImGui::DestroyContext();
if(!_renderLoops.empty()) { if (!_renderLoops.empty())
for(auto item : _renderLoops) { {
for (auto item : _renderLoops)
{
delete item.second.tracker; delete item.second.tracker;
} }
_renderLoops.clear(); _renderLoops.clear();
@ -288,6 +290,9 @@ float ImGuiPresenter::enableDPIScale(float userScale)
#if (AX_TARGET_PLATFORM != AX_PLATFORM_ANDROID) #if (AX_TARGET_PLATFORM != AX_PLATFORM_ANDROID)
// Gets scale // Gets scale
glfwGetMonitorContentScale(glfwGetPrimaryMonitor(), &xscale, nullptr); glfwGetMonitorContentScale(glfwGetPrimaryMonitor(), &xscale, nullptr);
#else
// refer to: https://developer.android.com/training/multiscreen/screendensities?hl=en-us
xscale = Device::getDPI() / 160.0f;
#endif #endif
auto zoomFactor = userScale * xscale; auto zoomFactor = userScale * xscale;
@ -414,11 +419,14 @@ void ImGuiPresenter::update()
usedCCRef.clear(); usedCCRef.clear();
// drawing commands // drawing commands
for (auto iter = _renderLoops.begin(); iter != _renderLoops.end(); ) { for (auto iter = _renderLoops.begin(); iter != _renderLoops.end();)
{
auto& imLoop = iter->second; auto& imLoop = iter->second;
if(imLoop.removing) { if (imLoop.removing)
delete imLoop.tracker; {
auto tracker = imLoop.tracker;
iter = _renderLoops.erase(iter); iter = _renderLoops.erase(iter);
delete tracker;
continue; continue;
} }
imLoop.func(); // invoke ImGui loop func imLoop.func(); // invoke ImGui loop func
@ -430,25 +438,28 @@ void ImGuiPresenter::update()
bool ImGuiPresenter::addRenderLoop(std::string_view id, std::function<void()> func, Scene* target) bool ImGuiPresenter::addRenderLoop(std::string_view id, std::function<void()> func, Scene* target)
{ {
// TODO: check whether exist
auto tracker = target ? static_cast<ImGuiEventTracker*>(utils::newInstance<ImGuiSceneEventTracker>(
&ImGuiSceneEventTracker::initWithScene, target))
: static_cast<ImGuiEventTracker*>(utils::newInstance<ImGuiGlobalEventTracker>());
auto fourccId = fourccValue(id); auto fourccId = fourccValue(id);
if (_renderLoops.find(fourccId) != _renderLoops.end()) auto iter = _renderLoops.find(fourccId);
{ if (iter == _renderLoops.end())
return false;
}
ImGuiEventTracker* tracker;
if (target)
tracker = utils::newInstance<ImGuiSceneEventTracker>(&ImGuiSceneEventTracker::initWithScene, target);
else
tracker = utils::newInstance<ImGuiGlobalEventTracker>();
if (tracker)
{ {
_renderLoops.emplace(fourccId, ImGuiLoop{tracker, std::move(func)}); _renderLoops.emplace(fourccId, ImGuiLoop{tracker, std::move(func)});
return true; 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) void ImGuiPresenter::removeRenderLoop(std::string_view id)

View File

@ -3,7 +3,7 @@
#include <stdint.h> #include <stdint.h>
#include <tuple> #include <tuple>
#include "cocos2d.h" #include "axmol.h"
#include "ExtensionMacros.h" #include "ExtensionMacros.h"
#include "imgui/imgui.h" #include "imgui/imgui.h"
@ -36,14 +36,17 @@ public:
static void destroyInstance(); static void destroyInstance();
static void setOnInit(const std::function<void(ImGuiPresenter*)>& callBack); static void setOnInit(const std::function<void(ImGuiPresenter*)>& callBack);
/// deprecated use enableDPIScale instead
float scaleAllByDPI(float userScale = 1.0f) { return enableDPIScale(userScale); }
/// <summary> /// <summary>
/// Scale ImGui with majorMoniter DPI scaling /// Scale ImGui with majorMoniter DPI scaling
/// </summary> /// </summary>
/// <param name="userScale">Usually is 1.0</param> /// <param name="userScale">Usually is 1.0</param>
/// <param name="fontFile">The full path of .ttc/.ttf file</param> /// <param name="fontFile">The full path of .ttc/.ttf file</param>
/// <returns>The final contentZoomFactor = userScale * dpiScale</returns> /// <returns>The final contentZoomFactor = userScale * dpiScale</returns>
float scaleAllByDPI(float userScale = 1.0f) { return enableDPIScale(userScale); }
float enableDPIScale(float userScale = 1.0f); float enableDPIScale(float userScale = 1.0f);
float getContentZoomFactor() const { return _contentZoomFactor; } float getContentZoomFactor() const { return _contentZoomFactor; }
void setViewResolution(float width, float height); void setViewResolution(float width, float height);