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

@ -1,9 +1,9 @@
#include "ImGuiPresenter.h"
#include <assert.h>
#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,11 +419,14 @@ 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;
if (imLoop.removing)
{
auto tracker = imLoop.tracker;
iter = _renderLoops.erase(iter);
delete tracker;
continue;
}
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)
{
// 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);
if (_renderLoops.find(fourccId) != _renderLoops.end())
{
return false;
}
ImGuiEventTracker* tracker;
if (target)
tracker = utils::newInstance<ImGuiSceneEventTracker>(&ImGuiSceneEventTracker::initWithScene, target);
else
tracker = utils::newInstance<ImGuiGlobalEventTracker>();
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)

View File

@ -3,7 +3,7 @@
#include <stdint.h>
#include <tuple>
#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<void(ImGuiPresenter*)>& callBack);
/// deprecated use enableDPIScale instead
float scaleAllByDPI(float userScale = 1.0f) { return enableDPIScale(userScale); }
/// <summary>
/// Scale ImGui with majorMoniter DPI scaling
/// </summary>
/// <param name="userScale">Usually is 1.0</param>
/// <param name="fontFile">The full path of .ttc/.ttf file</param>
/// <returns>The final contentZoomFactor = userScale * dpiScale</returns>
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);