2022-02-25 19:03:09 +08:00
|
|
|
#include "ImGuiPresenter.h"
|
2020-09-08 13:38:16 +08:00
|
|
|
#include <assert.h>
|
2022-10-10 03:53:24 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2023-09-12 19:30:15 +08:00
|
|
|
# include "imgui_impl_ax_android.h"
|
2022-10-10 03:53:24 +08:00
|
|
|
#else
|
2023-09-12 19:30:15 +08:00
|
|
|
# include "imgui_impl_ax.h"
|
2022-10-10 03:53:24 +08:00
|
|
|
#endif
|
2020-09-08 13:38:16 +08:00
|
|
|
#include "imgui_internal.h"
|
2020-09-04 17:19:51 +08:00
|
|
|
|
2022-02-08 12:43:16 +08:00
|
|
|
// TODO: mac metal
|
2023-09-03 13:22:40 +08:00
|
|
|
#if defined(AX_USE_GL) && defined(AX_PLATFORM_PC)
|
2022-07-16 10:43:05 +08:00
|
|
|
# define AX_IMGUI_ENABLE_MULTI_VIEWPORT 1
|
2022-02-08 12:43:16 +08:00
|
|
|
#else
|
2022-07-16 10:43:05 +08:00
|
|
|
# define AX_IMGUI_ENABLE_MULTI_VIEWPORT 0
|
2022-02-08 12:43:16 +08:00
|
|
|
#endif
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_EXT_BEGIN
|
2020-09-04 17:19:51 +08:00
|
|
|
|
2023-10-20 19:22:49 +08:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
uint32_t fourccValue(std::string_view str)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (str.empty() || str[0] != '#')
|
|
|
|
return (uint32_t)-1;
|
2020-09-07 23:21:15 +08:00
|
|
|
uint32_t value = 0;
|
2021-12-31 12:12:40 +08:00
|
|
|
memcpy(&value, str.data() + 1, std::min(sizeof(value), str.size() - 1));
|
2020-09-07 23:21:15 +08:00
|
|
|
return value;
|
2020-09-05 17:10:09 +08:00
|
|
|
}
|
|
|
|
|
2023-10-20 19:22:49 +08:00
|
|
|
std::tuple<ImVec2, ImVec2> getTwoPointTextureUV(Sprite* sp)
|
|
|
|
{
|
|
|
|
ImVec2 uv0, uv1;
|
|
|
|
if (!sp || !sp->getTexture())
|
|
|
|
return std::tuple{uv0, uv1};
|
|
|
|
const auto& rect = sp->getTextureRect();
|
|
|
|
const auto tex = sp->getTexture();
|
|
|
|
const float atlasWidth = (float)tex->getPixelsWide();
|
|
|
|
const float atlasHeight = (float)tex->getPixelsHigh();
|
|
|
|
uv0.x = rect.origin.x / atlasWidth;
|
|
|
|
uv0.y = rect.origin.y / atlasHeight;
|
|
|
|
uv1.x = (rect.origin.x + rect.size.width) / atlasWidth;
|
|
|
|
uv1.y = (rect.origin.y + rect.size.height) / atlasHeight;
|
|
|
|
return std::tuple{uv0, uv1};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::tuple<ImVec2, ImVec2, ImVec2, ImVec2> getFourPointTextureUV(Texture2D* tex, const Rect& rectInPixels, bool rotated)
|
|
|
|
{
|
|
|
|
ImVec2 uv0, uv1, uv2, uv3;
|
|
|
|
|
|
|
|
const auto atlasWidth = (float)tex->getPixelsWide();
|
|
|
|
const auto atlasHeight = (float)tex->getPixelsHigh();
|
|
|
|
|
|
|
|
float rw = rectInPixels.size.width;
|
|
|
|
float rh = rectInPixels.size.height;
|
|
|
|
|
|
|
|
if (rotated)
|
|
|
|
std::swap(rw, rh);
|
|
|
|
|
|
|
|
#if AX_FIX_ARTIFACTS_BY_STRECHING_TEXEL
|
|
|
|
float left = (2 * rectInPixels.origin.x + 1) / (2 * atlasWidth);
|
|
|
|
float right = left + (rw * 2 - 2) / (2 * atlasWidth);
|
|
|
|
float top = (2 * rectInPixels.origin.y + 1) / (2 * atlasHeight);
|
|
|
|
float bottom = top + (rh * 2 - 2) / (2 * atlasHeight);
|
|
|
|
#else
|
|
|
|
float left = rectInPixels.origin.x / atlasWidth;
|
|
|
|
float right = (rectInPixels.origin.x + rw) / atlasWidth;
|
|
|
|
float top = rectInPixels.origin.y / atlasHeight;
|
|
|
|
float bottom = (rectInPixels.origin.y + rh) / atlasHeight;
|
|
|
|
#endif // AX_FIX_ARTIFACTS_BY_STRECHING_TEXEL
|
|
|
|
|
|
|
|
if (rotated)
|
|
|
|
{
|
|
|
|
uv0.x = right;
|
|
|
|
uv0.y = top;
|
|
|
|
uv1.x = right;
|
|
|
|
uv1.y = bottom;
|
|
|
|
uv2.x = left;
|
|
|
|
uv2.y = bottom;
|
|
|
|
uv3.x = left;
|
|
|
|
uv3.y = top;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uv0.x = left;
|
|
|
|
uv0.y = top;
|
|
|
|
uv1.x = right;
|
|
|
|
uv1.y = top;
|
|
|
|
uv2.x = right;
|
|
|
|
uv2.y = bottom;
|
|
|
|
uv3.x = left;
|
|
|
|
uv3.y = bottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::tuple{uv0, uv1, uv2, uv3};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::tuple<ImVec2, ImVec2, ImVec2, ImVec2> getFourPointTextureUV(Sprite* sp)
|
|
|
|
{
|
|
|
|
if (!sp || !sp->getTexture())
|
|
|
|
return std::tuple{ImVec2(), ImVec2(), ImVec2(), ImVec2()};
|
|
|
|
|
|
|
|
const auto rectInPixels = AX_RECT_POINTS_TO_PIXELS(sp->getTextureRect());
|
|
|
|
return getFourPointTextureUV(sp->getTexture(), rectInPixels, sp->isTextureRectRotated());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::tuple<ImVec2, ImVec2, ImVec2, ImVec2> getFourPointTextureUV(SpriteFrame* frame)
|
|
|
|
{
|
|
|
|
if (!frame || !frame->getTexture())
|
|
|
|
return std::tuple{ImVec2(), ImVec2(), ImVec2(), ImVec2()};
|
|
|
|
|
|
|
|
const auto rectInPixels = frame->getRectInPixels();
|
|
|
|
return getFourPointTextureUV(frame->getTexture(), rectInPixels, frame->isRotated());
|
|
|
|
}
|
|
|
|
|
|
|
|
ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs)
|
|
|
|
{
|
|
|
|
return { lhs.x + rhs.x, lhs.y + rhs.y };
|
|
|
|
}
|
|
|
|
|
|
|
|
ImVec2 ImRotate(const ImVec2& v, float cos_a, float sin_a)
|
|
|
|
{
|
|
|
|
return { v.x * cos_a - v.y * sin_a, v.x * sin_a + v.y * cos_a };
|
|
|
|
}
|
|
|
|
|
|
|
|
ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs)
|
|
|
|
{
|
|
|
|
lhs.x += rhs.x;
|
|
|
|
lhs.y += rhs.y;
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
class ImGuiEventTracker
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
public:
|
2023-10-20 19:22:49 +08:00
|
|
|
virtual ~ImGuiEventTracker() = default;
|
2020-09-07 23:21:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Track scene event and check whether routed to the scene graph
|
2022-02-25 19:03:09 +08:00
|
|
|
class ImGuiSceneEventTracker : public ImGuiEventTracker
|
2020-09-07 23:21:15 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool initWithScene(Scene* scene)
|
|
|
|
{
|
2023-09-13 16:22:55 +08:00
|
|
|
#if defined(AX_PLATFORM_PC) || defined(__EMSCRIPTEN__)
|
2022-06-30 20:13:30 +08:00
|
|
|
_trackLayer = utils::newInstance<Node>(&Node::initLayer);
|
2020-09-07 23:21:15 +08:00
|
|
|
|
|
|
|
// note: when at the first click to focus the window, this will not take effect
|
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
|
|
|
listener->setSwallowTouches(true);
|
2021-12-25 10:04:45 +08:00
|
|
|
listener->onTouchBegan = [this](Touch* touch, Event*) -> bool { return ImGui::GetIO().WantCaptureMouse; };
|
2020-09-07 23:21:15 +08:00
|
|
|
_trackLayer->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, _trackLayer);
|
|
|
|
|
|
|
|
// add by halx99
|
|
|
|
auto stopAnyMouse = [=](EventMouse* event) {
|
2021-12-25 10:04:45 +08:00
|
|
|
if (ImGui::GetIO().WantCaptureMouse)
|
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
event->stopPropagation();
|
|
|
|
}
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
auto mouseListener = EventListenerMouse::create();
|
2020-09-07 23:21:15 +08:00
|
|
|
mouseListener->onMouseDown = mouseListener->onMouseUp = stopAnyMouse;
|
|
|
|
_trackLayer->getEventDispatcher()->addEventListenerWithSceneGraphPriority(mouseListener, _trackLayer);
|
|
|
|
scene->addChild(_trackLayer, INT_MAX);
|
|
|
|
#endif
|
2021-06-17 21:16:06 +08:00
|
|
|
// add an empty sprite to avoid render problem
|
|
|
|
// const auto sp = Sprite::create();
|
|
|
|
// sp->setGlobalZOrder(1);
|
|
|
|
// sp->setOpacity(0);
|
|
|
|
// addChild(sp, 1);
|
2020-09-07 23:21:15 +08:00
|
|
|
|
|
|
|
/*
|
2021-12-25 10:04:45 +08:00
|
|
|
* There a 3 choice for schedule frame for ImGui render loop
|
|
|
|
* a. at visit/draw to call beginFrame/endFrame, but at ImGui loop, we can't game object and add to Scene
|
|
|
|
* directly, will cause damage iterator b. scheduleUpdate at onEnter to call beginFrame, at visit/draw to call
|
|
|
|
* endFrame, it's solve iterator damage problem, but when director is paused the director will stop call
|
|
|
|
* 'update' function of Scheduler And need modify engine code to call _scheduler->update(_deltaTime) even
|
|
|
|
* director is paused, pass 0 for update c. Director::EVENT_BEFORE_DRAW call beginFrame, EVENT_AFTER_VISIT call
|
|
|
|
* endFrame
|
|
|
|
*/
|
2020-09-07 23:21:15 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-20 19:22:49 +08:00
|
|
|
~ImGuiSceneEventTracker() override
|
2020-09-07 23:21:15 +08:00
|
|
|
{
|
2023-09-13 16:22:55 +08:00
|
|
|
#if defined(AX_PLATFORM_PC) || defined(__EMSCRIPTEN__)
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_trackLayer)
|
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (_trackLayer->getParent())
|
|
|
|
_trackLayer->removeFromParent();
|
|
|
|
_trackLayer->release();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-06-30 20:13:30 +08:00
|
|
|
Node* _trackLayer = nullptr;
|
2020-09-07 23:21:15 +08:00
|
|
|
};
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
class ImGuiGlobalEventTracker : public ImGuiEventTracker
|
2020-09-05 17:10:09 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
static const int highestPriority = (std::numeric_limits<int>::min)();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-09-07 23:21:15 +08:00
|
|
|
public:
|
|
|
|
bool init()
|
|
|
|
{
|
2023-09-13 16:22:55 +08:00
|
|
|
#if defined(AX_PLATFORM_PC) || defined(__EMSCRIPTEN__)
|
2020-09-07 23:21:15 +08:00
|
|
|
// note: when at the first click to focus the window, this will not take effect
|
|
|
|
|
|
|
|
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
|
|
|
|
|
|
|
|
_touchListener = utils::newInstance<EventListenerTouchOneByOne>();
|
|
|
|
_touchListener->setSwallowTouches(true);
|
2021-12-25 10:04:45 +08:00
|
|
|
_touchListener->onTouchBegan = [this](Touch* touch, Event*) -> bool { return ImGui::GetIO().WantCaptureMouse; };
|
2020-09-07 23:21:15 +08:00
|
|
|
eventDispatcher->addEventListenerWithFixedPriority(_touchListener, highestPriority);
|
|
|
|
|
|
|
|
// add by halx99
|
|
|
|
auto stopAnyMouse = [=](EventMouse* event) {
|
2021-12-25 10:04:45 +08:00
|
|
|
if (ImGui::GetIO().WantCaptureMouse)
|
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
event->stopPropagation();
|
|
|
|
}
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
_mouseListener = utils::newInstance<EventListenerMouse>();
|
2020-09-07 23:21:15 +08:00
|
|
|
_mouseListener->onMouseDown = _mouseListener->onMouseUp = stopAnyMouse;
|
|
|
|
eventDispatcher->addEventListenerWithFixedPriority(_mouseListener, highestPriority);
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-05 17:10:09 +08:00
|
|
|
|
2023-10-20 19:22:49 +08:00
|
|
|
~ImGuiGlobalEventTracker() override
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2023-09-13 16:22:55 +08:00
|
|
|
#if defined(AX_PLATFORM_PC) || defined(__EMSCRIPTEN__)
|
2020-09-07 23:21:15 +08:00
|
|
|
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
|
|
|
|
eventDispatcher->removeEventListener(_mouseListener);
|
|
|
|
eventDispatcher->removeEventListener(_touchListener);
|
|
|
|
|
|
|
|
_mouseListener->release();
|
|
|
|
_touchListener->release();
|
2020-09-05 17:10:09 +08:00
|
|
|
#endif
|
2020-09-07 23:21:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EventListenerTouchOneByOne* _touchListener = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
EventListenerMouse* _mouseListener = nullptr;
|
2020-09-05 17:10:09 +08:00
|
|
|
};
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
static ImGuiPresenter* _instance = nullptr;
|
|
|
|
std::function<void(ImGuiPresenter*)> ImGuiPresenter::_onInit;
|
2020-09-04 17:19:51 +08:00
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
ImGuiPresenter* ImGuiPresenter::getInstance()
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (_instance == nullptr)
|
|
|
|
{
|
2022-02-25 19:03:09 +08:00
|
|
|
_instance = new ImGuiPresenter();
|
2020-09-07 23:21:15 +08:00
|
|
|
_instance->init();
|
|
|
|
if (_onInit)
|
|
|
|
_onInit(_instance);
|
|
|
|
}
|
|
|
|
return _instance;
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::destroyInstance()
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (_instance)
|
|
|
|
{
|
2020-09-08 13:38:16 +08:00
|
|
|
_instance->cleanup();
|
2020-09-07 23:21:15 +08:00
|
|
|
delete _instance;
|
|
|
|
_instance = nullptr;
|
|
|
|
}
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::init()
|
2020-09-08 13:38:16 +08:00
|
|
|
{
|
2022-02-08 12:43:16 +08:00
|
|
|
// Setup Dear ImGui context
|
|
|
|
IMGUI_CHECKVERSION();
|
|
|
|
ImGui::CreateContext();
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
(void)io;
|
|
|
|
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
|
|
|
// io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
|
|
|
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_IMGUI_ENABLE_MULTI_VIEWPORT
|
2022-02-08 12:43:16 +08:00
|
|
|
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
|
|
|
|
#endif
|
|
|
|
// io.ConfigViewportsNoAutoMerge = true;
|
|
|
|
// io.ConfigViewportsNoTaskBarIcon = true;
|
|
|
|
// io.ConfigViewportsNoDefaultParent = true;
|
|
|
|
// io.ConfigDockingAlwaysTabBar = true;
|
|
|
|
// io.ConfigDockingTransparentPayload = true;
|
|
|
|
// io.ConfigFlags |= ImGuiConfigFlags_DpiEnableScaleFonts; // FIXME-DPI: Experimental. THIS CURRENTLY DOESN'T
|
|
|
|
// WORK AS EXPECTED. DON'T USE IN USER APP! io.ConfigFlags |= ImGuiConfigFlags_DpiEnableScaleViewports; //
|
|
|
|
// FIXME-DPI: Experimental.
|
|
|
|
|
|
|
|
// Setup Dear ImGui style
|
|
|
|
ImGui::StyleColorsDark();
|
|
|
|
// ImGui::StyleColorsClassic();
|
|
|
|
|
|
|
|
// When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular
|
|
|
|
// ones.
|
|
|
|
ImGuiStyle& style = ImGui::GetStyle();
|
|
|
|
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
|
|
|
|
{
|
|
|
|
style.WindowRounding = 0.0f;
|
|
|
|
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
|
|
|
|
}
|
|
|
|
|
2022-10-10 03:53:24 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2023-12-01 09:04:51 +08:00
|
|
|
ImGui_ImplAndroid_InitForAx(Director::getInstance()->getGLView(), true);
|
2022-10-10 03:53:24 +08:00
|
|
|
#else
|
2023-12-01 09:04:51 +08:00
|
|
|
auto window = static_cast<GLViewImpl*>(Director::getInstance()->getGLView())->getWindow();
|
2022-10-01 16:24:52 +08:00
|
|
|
ImGui_ImplGlfw_InitForAx(window, true);
|
2022-10-10 03:53:24 +08:00
|
|
|
#endif
|
2022-09-26 22:56:47 +08:00
|
|
|
ImGui_ImplAx_Init();
|
2022-02-08 12:43:16 +08:00
|
|
|
|
2022-09-26 22:56:47 +08:00
|
|
|
ImGui_ImplAx_SetCustomFontLoader(&ImGuiPresenter::loadCustomFonts, this);
|
2020-09-08 13:38:16 +08:00
|
|
|
|
|
|
|
ImGui::StyleColorsClassic();
|
|
|
|
|
|
|
|
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
|
2023-09-05 21:10:25 +08:00
|
|
|
eventDispatcher->addCustomEventListener(Director::EVENT_BEFORE_DRAW, [this](EventCustom*) { beginFrame(); });
|
|
|
|
eventDispatcher->addCustomEventListener(Director::EVENT_AFTER_VISIT, [this](EventCustom*) { endFrame(); });
|
2020-09-08 13:38:16 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::cleanup()
|
2020-09-08 13:38:16 +08:00
|
|
|
{
|
|
|
|
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
|
2021-06-17 21:16:06 +08:00
|
|
|
eventDispatcher->removeCustomEventListeners(Director::EVENT_AFTER_VISIT);
|
|
|
|
eventDispatcher->removeCustomEventListeners(Director::EVENT_BEFORE_DRAW);
|
2020-09-08 13:38:16 +08:00
|
|
|
|
2022-09-26 22:56:47 +08:00
|
|
|
ImGui_ImplAx_SetCustomFontLoader(nullptr, nullptr);
|
|
|
|
ImGui_ImplAx_Shutdown();
|
2022-10-10 03:53:24 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
|
|
|
ImGui_ImplAndroid_Shutdown();
|
|
|
|
#else
|
2022-02-08 12:43:16 +08:00
|
|
|
ImGui_ImplGlfw_Shutdown();
|
2022-10-10 03:53:24 +08:00
|
|
|
#endif
|
2020-09-08 13:38:16 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(_fontsTexture);
|
2022-02-08 12:43:16 +08:00
|
|
|
|
|
|
|
ImGui::DestroyContext();
|
2023-09-10 22:38:54 +08:00
|
|
|
|
2023-09-12 19:30:15 +08:00
|
|
|
if (!_renderLoops.empty())
|
|
|
|
{
|
|
|
|
for (auto item : _renderLoops)
|
|
|
|
{
|
2023-09-10 22:38:54 +08:00
|
|
|
delete item.second.tracker;
|
|
|
|
}
|
|
|
|
_renderLoops.clear();
|
|
|
|
}
|
2020-09-08 13:38:16 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::setOnInit(const std::function<void(ImGuiPresenter*)>& callBack)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
_onInit = callBack;
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::loadCustomFonts(void* ud)
|
2020-09-08 13:38:16 +08:00
|
|
|
{
|
2022-02-25 19:03:09 +08:00
|
|
|
auto thiz = (ImGuiPresenter*)ud;
|
2020-09-08 13:38:16 +08:00
|
|
|
|
|
|
|
auto imFonts = ImGui::GetIO().Fonts;
|
|
|
|
imFonts->Clear();
|
|
|
|
|
|
|
|
auto contentZoomFactor = thiz->_contentZoomFactor;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& fontInfo : thiz->_fontsInfoMap)
|
|
|
|
{
|
2020-09-08 13:38:16 +08:00
|
|
|
const ImWchar* imChars = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (fontInfo.second.glyphRange)
|
|
|
|
{
|
2020-09-08 13:38:16 +08:00
|
|
|
case CHS_GLYPH_RANGE::GENERAL:
|
|
|
|
imChars = imFonts->GetGlyphRangesChineseSimplifiedCommon();
|
|
|
|
break;
|
|
|
|
case CHS_GLYPH_RANGE::FULL:
|
|
|
|
imChars = imFonts->GetGlyphRangesChineseFull();
|
|
|
|
break;
|
2020-09-21 22:10:50 +08:00
|
|
|
default:;
|
2020-09-08 13:38:16 +08:00
|
|
|
}
|
|
|
|
|
2021-05-05 19:49:30 +08:00
|
|
|
auto fontData = FileUtils::getInstance()->getDataFromFile(fontInfo.first);
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(!fontData.isNull(), "Cannot load font for IMGUI");
|
2021-05-05 19:49:30 +08:00
|
|
|
|
2021-08-27 12:34:24 +08:00
|
|
|
ssize_t bufferSize = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* buffer = fontData.takeBuffer(&bufferSize); // Buffer automatically freed by IMGUI
|
2021-05-05 19:49:30 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
imFonts->AddFontFromMemoryTTF(buffer, bufferSize, fontInfo.second.fontSize * contentZoomFactor, nullptr,
|
|
|
|
imChars);
|
2020-09-08 13:38:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-10 22:38:54 +08:00
|
|
|
float ImGuiPresenter::enableDPIScale(float userScale)
|
2020-09-07 21:22:03 +08:00
|
|
|
{
|
2023-11-06 00:19:44 +08:00
|
|
|
float devicePixelRatio = Device::getPixelRatio();
|
2022-10-10 03:53:24 +08:00
|
|
|
|
2023-11-06 00:19:44 +08:00
|
|
|
auto zoomFactor = userScale * devicePixelRatio;
|
2020-09-07 21:22:03 +08:00
|
|
|
|
2020-09-07 23:21:15 +08:00
|
|
|
auto imFonts = ImGui::GetIO().Fonts;
|
2020-09-07 21:22:03 +08:00
|
|
|
|
2020-09-07 23:21:15 +08:00
|
|
|
// clear before add new font
|
2021-12-25 10:04:45 +08:00
|
|
|
auto fontConf = imFonts->ConfigData; // copy font config data
|
2020-09-07 21:22:03 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (zoomFactor != _contentZoomFactor)
|
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
for (auto& fontConf : imFonts->ConfigData)
|
|
|
|
{
|
|
|
|
fontConf.SizePixels = (fontConf.SizePixels / _contentZoomFactor) * zoomFactor;
|
|
|
|
}
|
2020-09-07 21:22:03 +08:00
|
|
|
|
2023-10-20 19:22:49 +08:00
|
|
|
// Destroy font information, let ImplAx recreate at newFrame
|
2022-09-26 22:56:47 +08:00
|
|
|
ImGui_ImplAx_SetDeviceObjectsDirty();
|
2020-09-07 21:22:03 +08:00
|
|
|
|
2020-09-07 23:21:15 +08:00
|
|
|
ImGui::GetStyle().ScaleAllSizes(zoomFactor);
|
2020-09-07 21:22:03 +08:00
|
|
|
|
2020-09-07 23:21:15 +08:00
|
|
|
_contentZoomFactor = zoomFactor;
|
|
|
|
}
|
2020-09-07 21:22:03 +08:00
|
|
|
|
2020-09-07 23:21:15 +08:00
|
|
|
return zoomFactor;
|
2020-09-07 21:22:03 +08:00
|
|
|
}
|
|
|
|
|
2022-10-10 03:53:24 +08:00
|
|
|
void ImGuiPresenter::setViewResolution(float width, float height)
|
|
|
|
{
|
|
|
|
ImGui_ImplAx_SetViewResolution(width, height);
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::addFont(std::string_view fontFile, float fontSize, CHS_GLYPH_RANGE glyphRange)
|
2020-09-07 21:22:03 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (FileUtils::getInstance()->isFileExistInternal(fontFile))
|
|
|
|
{
|
|
|
|
if (_fontsInfoMap.emplace(fontFile, FontInfo{fontSize, glyphRange}).second)
|
2022-09-26 22:56:47 +08:00
|
|
|
ImGui_ImplAx_SetDeviceObjectsDirty();
|
2020-09-07 23:21:15 +08:00
|
|
|
}
|
2020-09-08 13:38:16 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::removeFont(std::string_view fontFile)
|
2020-09-08 13:38:16 +08:00
|
|
|
{
|
|
|
|
auto count = _fontsInfoMap.size();
|
|
|
|
_fontsInfoMap.erase(fontFile);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (count != _fontsInfoMap.size())
|
2022-09-26 22:56:47 +08:00
|
|
|
ImGui_ImplAx_SetDeviceObjectsDirty();
|
2020-09-07 21:22:03 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::clearFonts()
|
2020-09-07 21:22:03 +08:00
|
|
|
{
|
2020-09-08 14:21:26 +08:00
|
|
|
bool haveCustomFonts = !_fontsInfoMap.empty();
|
2020-09-08 13:38:16 +08:00
|
|
|
_fontsInfoMap.clear();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (haveCustomFonts)
|
2022-09-26 22:56:47 +08:00
|
|
|
ImGui_ImplAx_SetDeviceObjectsDirty();
|
2020-09-08 13:38:16 +08:00
|
|
|
|
|
|
|
// auto drawData = ImGui::GetDrawData();
|
|
|
|
// if(drawData) drawData->Clear();
|
2020-09-07 21:22:03 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::end()
|
2020-09-10 17:53:17 +08:00
|
|
|
{
|
|
|
|
_purgeNextLoop = true;
|
|
|
|
}
|
|
|
|
|
2020-09-07 19:15:53 +08:00
|
|
|
/*
|
2021-12-25 10:04:45 +08:00
|
|
|
* begin ImGui frame and draw ImGui stubs
|
|
|
|
*/
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::beginFrame()
|
2021-12-25 10:04:45 +08:00
|
|
|
{ // drived by event Director::EVENT_BEFORE_DRAW from engine mainLoop
|
|
|
|
if (_purgeNextLoop)
|
2020-09-10 17:53:17 +08:00
|
|
|
{
|
|
|
|
Director::getInstance()->end();
|
|
|
|
return;
|
|
|
|
}
|
2023-09-10 22:38:54 +08:00
|
|
|
if (!_renderLoops.empty())
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2020-09-08 13:38:16 +08:00
|
|
|
// create frame
|
2022-09-26 22:56:47 +08:00
|
|
|
ImGui_ImplAx_NewFrame();
|
2022-10-10 03:53:24 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
|
|
|
ImGui_ImplAndroid_NewFrame();
|
|
|
|
#else
|
2022-02-08 12:43:16 +08:00
|
|
|
ImGui_ImplGlfw_NewFrame();
|
2022-10-10 03:53:24 +08:00
|
|
|
#endif
|
2022-02-08 12:43:16 +08:00
|
|
|
ImGui::NewFrame();
|
2020-09-08 13:38:16 +08:00
|
|
|
|
|
|
|
// move to endFrame?
|
2022-09-26 22:56:47 +08:00
|
|
|
_fontsTexture = (Texture2D*)ImGui_ImplAx_GetFontsTexture();
|
2020-09-08 13:38:16 +08:00
|
|
|
assert(_fontsTexture != nullptr);
|
|
|
|
_fontsTexture->retain();
|
2020-09-07 19:15:53 +08:00
|
|
|
|
2020-09-08 13:38:16 +08:00
|
|
|
// draw all gui
|
|
|
|
this->update();
|
2020-09-07 19:15:53 +08:00
|
|
|
|
2020-09-08 13:38:16 +08:00
|
|
|
++_beginFrames;
|
|
|
|
}
|
2020-09-07 19:15:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-12-25 10:04:45 +08:00
|
|
|
* flush ImGui draw data to engine
|
|
|
|
*/
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::endFrame()
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (_beginFrames > 0)
|
|
|
|
{
|
2020-09-08 13:38:16 +08:00
|
|
|
// render
|
|
|
|
ImGui::Render();
|
|
|
|
|
|
|
|
auto drawData = ImGui::GetDrawData();
|
|
|
|
if (drawData)
|
2022-09-26 22:56:47 +08:00
|
|
|
ImGui_ImplAx_RenderDrawData(drawData);
|
2020-09-08 13:38:16 +08:00
|
|
|
|
2022-09-26 22:56:47 +08:00
|
|
|
ImGui_ImplAx_RenderPlatform();
|
2020-09-08 13:38:16 +08:00
|
|
|
--_beginFrames;
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(_fontsTexture);
|
2020-09-08 13:38:16 +08:00
|
|
|
}
|
2020-09-07 19:15:53 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::update()
|
2020-09-08 16:15:40 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
// clear things from last frame
|
|
|
|
usedCCRefIdMap.clear();
|
|
|
|
usedCCRef.clear();
|
|
|
|
// drawing commands
|
2023-09-10 22:38:54 +08:00
|
|
|
|
2023-09-12 19:30:15 +08:00
|
|
|
for (auto iter = _renderLoops.begin(); iter != _renderLoops.end();)
|
|
|
|
{
|
2023-09-10 22:38:54 +08:00
|
|
|
auto& imLoop = iter->second;
|
2023-09-12 19:30:15 +08:00
|
|
|
if (imLoop.removing)
|
|
|
|
{
|
|
|
|
auto tracker = imLoop.tracker;
|
|
|
|
iter = _renderLoops.erase(iter);
|
|
|
|
delete tracker;
|
2023-09-10 22:38:54 +08:00
|
|
|
continue;
|
|
|
|
}
|
2023-09-12 19:30:15 +08:00
|
|
|
imLoop.func(); // invoke ImGui loop func
|
2023-09-10 22:38:54 +08:00
|
|
|
++iter;
|
|
|
|
}
|
2020-09-05 17:10:09 +08:00
|
|
|
|
2020-09-07 23:21:15 +08:00
|
|
|
// commands will be processed after update
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
bool ImGuiPresenter::addRenderLoop(std::string_view id, std::function<void()> func, Scene* target)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-05 17:10:09 +08:00
|
|
|
|
2023-09-12 19:30:15 +08:00
|
|
|
auto tracker = target ? static_cast<ImGuiEventTracker*>(utils::newInstance<ImGuiSceneEventTracker>(
|
|
|
|
&ImGuiSceneEventTracker::initWithScene, target))
|
|
|
|
: static_cast<ImGuiEventTracker*>(utils::newInstance<ImGuiGlobalEventTracker>());
|
2020-09-05 17:10:09 +08:00
|
|
|
|
2023-09-12 19:30:15 +08:00
|
|
|
auto fourccId = fourccValue(id);
|
|
|
|
auto iter = _renderLoops.find(fourccId);
|
|
|
|
if (iter == _renderLoops.end())
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2023-09-10 22:38:54 +08:00
|
|
|
_renderLoops.emplace(fourccId, ImGuiLoop{tracker, std::move(func)});
|
2020-09-07 23:21:15 +08:00
|
|
|
return true;
|
|
|
|
}
|
2023-09-12 19:30:15 +08:00
|
|
|
|
|
|
|
// 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;
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::removeRenderLoop(std::string_view id)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto fourccId = fourccValue(id);
|
2023-09-10 22:38:54 +08:00
|
|
|
const auto iter = _renderLoops.find(fourccId);
|
|
|
|
if (iter != _renderLoops.end())
|
|
|
|
iter->second.removing = true;
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::image(Texture2D* tex,
|
2023-09-12 19:30:15 +08:00
|
|
|
const ImVec2& size,
|
|
|
|
const ImVec2& uv0,
|
|
|
|
const ImVec2& uv1,
|
|
|
|
const ImVec4& tint_col,
|
|
|
|
const ImVec4& border_col)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (!tex)
|
|
|
|
return;
|
|
|
|
auto size_ = size;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (size_.x <= 0.f)
|
|
|
|
size_.x = tex->getPixelsWide();
|
|
|
|
if (size_.y <= 0.f)
|
|
|
|
size_.y = tex->getPixelsHigh();
|
2020-09-07 23:21:15 +08:00
|
|
|
ImGui::PushID(getCCRefId(tex));
|
|
|
|
ImGui::Image((ImTextureID)tex, size_, uv0, uv1, tint_col, border_col);
|
|
|
|
ImGui::PopID();
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2023-10-20 19:22:49 +08:00
|
|
|
void ImGuiPresenter::image(Sprite* sprite,
|
|
|
|
const ImVec2& size,
|
|
|
|
bool keepAspectRatio,
|
|
|
|
const ImVec4& tint_col,
|
|
|
|
const ImVec4& border_col)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (!sprite || !sprite->getTexture())
|
|
|
|
return;
|
2023-10-20 19:22:49 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto size_ = size;
|
2020-09-08 16:15:40 +08:00
|
|
|
const auto& rect = sprite->getTextureRect();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (size_.x <= 0.f)
|
|
|
|
size_.x = rect.size.width;
|
|
|
|
if (size_.y <= 0.f)
|
|
|
|
size_.y = rect.size.height;
|
2023-10-20 19:22:49 +08:00
|
|
|
|
|
|
|
if (keepAspectRatio)
|
|
|
|
{
|
|
|
|
const auto rectInPixels = AX_RECT_POINTS_TO_PIXELS(rect);
|
|
|
|
const auto scale =
|
|
|
|
std::min(1.0f, std::min(size_.x / rectInPixels.size.width, size_.y / rectInPixels.size.height));
|
|
|
|
size_ = ImVec2(rectInPixels.size.width * scale, rectInPixels.size.height * scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto [uv0, uv1, uv2, uv3] = getFourPointTextureUV(sprite);
|
|
|
|
|
|
|
|
const auto* window = ImGui::GetCurrentWindow();
|
|
|
|
|
|
|
|
ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size_);
|
|
|
|
if (border_col.w > 0.0f)
|
|
|
|
bb.Max += ImVec2(2, 2);
|
|
|
|
|
|
|
|
ImGui::ItemSize(bb);
|
|
|
|
if (!ImGui::ItemAdd(bb, 0))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ImVec2 pos[4] = {ImVec2(bb.Min.x, bb.Min.y), ImVec2(bb.Max.x, bb.Min.y), ImVec2(bb.Max.x, bb.Max.y),
|
|
|
|
ImVec2(bb.Min.x, bb.Max.y)};
|
|
|
|
|
2020-09-07 23:21:15 +08:00
|
|
|
ImGui::PushID(getCCRefId(sprite));
|
2023-10-20 19:22:49 +08:00
|
|
|
if (border_col.w > 0.0f)
|
|
|
|
{
|
|
|
|
window->DrawList->AddRect(bb.Min, bb.Max, ImGui::GetColorU32(border_col), 0.0f);
|
|
|
|
window->DrawList->AddImageQuad((ImTextureID)sprite->getTexture(), pos[0] + ImVec2(1, 1), pos[1] + ImVec2(-1, 1),
|
|
|
|
pos[2] + ImVec2(1, -1), pos[3] + ImVec2(-1, -1), uv0, uv1, uv2, uv3,
|
|
|
|
ImGui::GetColorU32(tint_col));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
window->DrawList->AddImageQuad((ImTextureID)sprite->getTexture(), pos[0], pos[1], pos[2], pos[3], uv0, uv1, uv2,
|
|
|
|
uv3, ImGui::GetColorU32(tint_col));
|
|
|
|
}
|
|
|
|
ImGui::PopID();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImGuiPresenter::image(SpriteFrame* spriteFrame,
|
|
|
|
const ImVec2& size,
|
|
|
|
bool keepAspectRatio,
|
|
|
|
const ImVec4& tint_col,
|
|
|
|
const ImVec4& border_col)
|
|
|
|
{
|
|
|
|
if (!spriteFrame || !spriteFrame->getTexture())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto size_ = size;
|
|
|
|
const auto& rect = spriteFrame->getOriginalSizeInPixels();
|
|
|
|
if (size_.x <= 0.f)
|
|
|
|
size_.x = rect.width;
|
|
|
|
if (size_.y <= 0.f)
|
|
|
|
size_.y = rect.height;
|
|
|
|
|
|
|
|
if (keepAspectRatio)
|
|
|
|
{
|
|
|
|
const auto scale = std::min(1.0f, std::min(size_.x / rect.width, size_.y / rect.height));
|
|
|
|
size_ = ImVec2(rect.width * scale, rect.height * scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto [uv0, uv1, uv2, uv3] = getFourPointTextureUV(spriteFrame);
|
|
|
|
|
|
|
|
const auto* window = ImGui::GetCurrentWindow();
|
|
|
|
|
|
|
|
ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size_);
|
|
|
|
if (border_col.w > 0.0f)
|
|
|
|
bb.Max += ImVec2(2, 2);
|
|
|
|
|
|
|
|
ImGui::ItemSize(bb);
|
|
|
|
if (!ImGui::ItemAdd(bb, 0))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ImVec2 pos[4] = {ImVec2(bb.Min.x, bb.Min.y), ImVec2(bb.Max.x, bb.Min.y), ImVec2(bb.Max.x, bb.Max.y),
|
|
|
|
ImVec2(bb.Min.x, bb.Max.y)};
|
|
|
|
|
|
|
|
ImGui::PushID(getCCRefId(spriteFrame));
|
|
|
|
if (border_col.w > 0.0f)
|
|
|
|
{
|
|
|
|
window->DrawList->AddRect(bb.Min, bb.Max, ImGui::GetColorU32(border_col), 0.0f);
|
|
|
|
window->DrawList->AddImageQuad((ImTextureID)spriteFrame->getTexture(), pos[0] + ImVec2(1, 1),
|
|
|
|
pos[1] + ImVec2(-1, 1), pos[2] + ImVec2(1, -1), pos[3] + ImVec2(-1, -1), uv0,
|
|
|
|
uv1, uv2, uv3, ImGui::GetColorU32(tint_col));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
window->DrawList->AddImageQuad((ImTextureID)spriteFrame->getTexture(), pos[0], pos[1], pos[2], pos[3], uv0, uv1,
|
|
|
|
uv2, uv3, ImGui::GetColorU32(tint_col));
|
|
|
|
}
|
2020-09-07 23:21:15 +08:00
|
|
|
ImGui::PopID();
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
bool ImGuiPresenter::imageButton(Texture2D* tex,
|
2023-09-12 19:30:15 +08:00
|
|
|
const ImVec2& size,
|
|
|
|
const ImVec2& uv0,
|
|
|
|
const ImVec2& uv1,
|
|
|
|
int frame_padding,
|
|
|
|
const ImVec4& bg_col,
|
|
|
|
const ImVec4& tint_col)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (!tex)
|
|
|
|
return false;
|
|
|
|
auto size_ = size;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (size_.x <= 0.f)
|
|
|
|
size_.x = tex->getPixelsWide();
|
|
|
|
if (size_.y <= 0.f)
|
|
|
|
size_.y = tex->getPixelsHigh();
|
2020-09-07 23:21:15 +08:00
|
|
|
ImGui::PushID(getCCRefId(tex));
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto ret = ImGui::ImageButton((ImTextureID)tex, size_, uv0, uv1, frame_padding, bg_col, tint_col);
|
2020-09-07 23:21:15 +08:00
|
|
|
ImGui::PopID();
|
|
|
|
return ret;
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
bool ImGuiPresenter::imageButton(Sprite* sprite,
|
2023-09-12 19:30:15 +08:00
|
|
|
const ImVec2& size,
|
|
|
|
int frame_padding,
|
|
|
|
const ImVec4& bg_col,
|
|
|
|
const ImVec4& tint_col)
|
2020-09-07 23:21:15 +08:00
|
|
|
{
|
|
|
|
if (!sprite || !sprite->getTexture())
|
|
|
|
return false;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto size_ = size;
|
2020-09-08 16:15:40 +08:00
|
|
|
const auto& rect = sprite->getTextureRect();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (size_.x <= 0.f)
|
|
|
|
size_.x = rect.size.width;
|
|
|
|
if (size_.y <= 0.f)
|
|
|
|
size_.y = rect.size.height;
|
2023-10-20 19:22:49 +08:00
|
|
|
auto [uv0, uv1] = getTwoPointTextureUV(sprite);
|
2020-09-07 23:21:15 +08:00
|
|
|
ImGui::PushID(getCCRefId(sprite));
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto ret =
|
|
|
|
ImGui::ImageButton((ImTextureID)sprite->getTexture(), size_, uv0, uv1, frame_padding, bg_col, tint_col);
|
2020-09-07 23:21:15 +08:00
|
|
|
ImGui::PopID();
|
|
|
|
return ret;
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::node(Node* node, const ImVec4& tint_col, const ImVec4& border_col)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (!node)
|
|
|
|
return;
|
|
|
|
const auto size = node->getContentSize();
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto pos = ImGui::GetCursorScreenPos();
|
2020-09-07 23:21:15 +08:00
|
|
|
Mat4 tr;
|
2021-12-25 10:04:45 +08:00
|
|
|
tr.m[5] = -1;
|
2020-09-07 23:21:15 +08:00
|
|
|
tr.m[12] = pos.x;
|
|
|
|
tr.m[13] = pos.y + size.height;
|
|
|
|
if (border_col.w > 0.f)
|
|
|
|
{
|
|
|
|
tr.m[12] += 1;
|
|
|
|
tr.m[13] += 1;
|
|
|
|
}
|
|
|
|
node->setNodeToParentTransform(tr);
|
|
|
|
ImGui::PushID(getCCRefId(node));
|
2021-12-25 10:04:45 +08:00
|
|
|
ImGui::Image((ImTextureID)node, ImVec2(size.width, size.height), ImVec2(0, 0), ImVec2(1, 1), tint_col, border_col);
|
2020-09-07 23:21:15 +08:00
|
|
|
ImGui::PopID();
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
bool ImGuiPresenter::nodeButton(Node* node, int frame_padding, const ImVec4& bg_col, const ImVec4& tint_col)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (!node)
|
|
|
|
return false;
|
|
|
|
const auto size = node->getContentSize();
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto pos = ImGui::GetCursorScreenPos();
|
2020-09-07 23:21:15 +08:00
|
|
|
Mat4 tr;
|
2021-12-25 10:04:45 +08:00
|
|
|
tr.m[5] = -1;
|
2020-09-07 23:21:15 +08:00
|
|
|
tr.m[12] = pos.x;
|
|
|
|
tr.m[13] = pos.y + size.height;
|
|
|
|
if (frame_padding >= 0)
|
|
|
|
{
|
|
|
|
tr.m[12] += (float)frame_padding;
|
|
|
|
tr.m[13] += (float)frame_padding;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr.m[12] += ImGui::GetStyle().FramePadding.x;
|
|
|
|
tr.m[13] += ImGui::GetStyle().FramePadding.y;
|
|
|
|
}
|
|
|
|
node->setNodeToParentTransform(tr);
|
|
|
|
ImGui::PushID(getCCRefId(node));
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto ret = ImGui::ImageButton((ImTextureID)node, ImVec2(size.width, size.height), ImVec2(0, 0), ImVec2(1, 1),
|
|
|
|
frame_padding, bg_col, tint_col);
|
2020-09-07 23:21:15 +08:00
|
|
|
ImGui::PopID();
|
|
|
|
return ret;
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
std::tuple<ImTextureID, int> ImGuiPresenter::useTexture(Texture2D* texture)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (!texture)
|
2021-12-25 10:04:45 +08:00
|
|
|
return std::tuple<ImTextureID, int>{nullptr, 0};
|
2023-10-20 19:22:49 +08:00
|
|
|
return std::tuple{(ImTextureID)texture, getCCRefId(texture)};
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
std::tuple<ImTextureID, ImVec2, ImVec2, int> ImGuiPresenter::useSprite(Sprite* sprite)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (!sprite || !sprite->getTexture())
|
2021-12-25 10:04:45 +08:00
|
|
|
return std::tuple<ImTextureID, ImVec2, ImVec2, int>{nullptr, {}, {}, 0};
|
2023-10-20 19:22:49 +08:00
|
|
|
auto [uv0, uv1] = getTwoPointTextureUV(sprite);
|
|
|
|
return std::tuple{(ImTextureID)sprite->getTexture(), uv0, uv1, getCCRefId(sprite)};
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
std::tuple<ImTextureID, ImVec2, ImVec2, int> ImGuiPresenter::useNode(Node* node, const ImVec2& pos)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (!node)
|
2021-12-25 10:04:45 +08:00
|
|
|
return std::tuple<ImTextureID, ImVec2, ImVec2, int>{nullptr, {}, {}, 0};
|
2020-09-07 23:21:15 +08:00
|
|
|
const auto size = node->getContentSize();
|
|
|
|
Mat4 tr;
|
2021-12-25 10:04:45 +08:00
|
|
|
tr.m[5] = -1;
|
2020-09-07 23:21:15 +08:00
|
|
|
tr.m[12] = pos.x;
|
|
|
|
tr.m[13] = pos.y + size.height;
|
|
|
|
node->setNodeToParentTransform(tr);
|
2023-10-20 19:22:49 +08:00
|
|
|
return std::tuple{(ImTextureID)node, pos, ImVec2(pos.x + size.width, pos.y + size.height), getCCRefId(node)};
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::setNodeColor(Node* node, const ImVec4& col)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (node)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
node->setColor({uint8_t(col.x * 255), uint8_t(col.y * 255), uint8_t(col.z * 255)});
|
2020-09-07 23:21:15 +08:00
|
|
|
node->setOpacity(uint8_t(col.w * 255));
|
|
|
|
}
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::setNodeColor(Node* node, ImGuiCol col)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (node && 0 <= col && col < ImGuiCol_COUNT)
|
|
|
|
setNodeColor(node, ImGui::GetStyleColorVec4(col));
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::setLabelColor(Label* label, const ImVec4& col)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (label)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
label->setTextColor({uint8_t(col.x * 255), uint8_t(col.y * 255), uint8_t(col.z * 255), uint8_t(col.w * 255)});
|
2020-09-07 23:21:15 +08:00
|
|
|
}
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::setLabelColor(Label* label, bool disabled)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (label)
|
|
|
|
setLabelColor(label, ImGui::GetStyleColorVec4(disabled ? ImGuiCol_TextDisabled : ImGuiCol_Text));
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::setLabelColor(Label* label, ImGuiCol col)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (label && 0 <= col && col < ImGuiCol_COUNT)
|
|
|
|
setLabelColor(label, ImGui::GetStyleColorVec4(col));
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
ImWchar* ImGuiPresenter::addGlyphRanges(std::string_view key, const std::vector<ImWchar>& ranges)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
auto it = glyphRanges.find(key);
|
|
|
|
// the pointer must be persistant, do not replace
|
|
|
|
if (it != glyphRanges.end())
|
|
|
|
return it->second.data();
|
2021-12-31 12:12:40 +08:00
|
|
|
it = glyphRanges.emplace(key, ranges).first; // glyphRanges[key] = ranges;
|
2020-09-07 23:21:15 +08:00
|
|
|
if (ranges.empty())
|
2021-12-31 12:12:40 +08:00
|
|
|
it->second.push_back(0);
|
|
|
|
return it->second.data();
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
void ImGuiPresenter::mergeFontGlyphs(ImFont* dst, ImFont* src, ImWchar start, ImWchar end)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
if (!dst || !src || start > end)
|
|
|
|
return;
|
|
|
|
for (auto i = start; i <= end; ++i)
|
|
|
|
{
|
|
|
|
const auto g = src->FindGlyphNoFallback(i);
|
|
|
|
if (g)
|
|
|
|
{
|
2020-09-08 16:31:11 +08:00
|
|
|
dst->AddGlyph(nullptr, g->Codepoint, g->X0, g->Y0, g->X1, g->Y1, g->U0, g->V0, g->U1, g->V1, g->AdvanceX);
|
2020-09-07 23:21:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dst->BuildLookupTable();
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:03:09 +08:00
|
|
|
int ImGuiPresenter::getCCRefId(Ref* p)
|
2020-09-04 17:19:51 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
int id = 0;
|
2020-09-07 23:21:15 +08:00
|
|
|
const auto it = usedCCRefIdMap.find(p);
|
|
|
|
if (it == usedCCRefIdMap.end())
|
|
|
|
{
|
|
|
|
usedCCRefIdMap[p] = 0;
|
|
|
|
usedCCRef.pushBack(p);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
id = ++it->second;
|
|
|
|
// BKDR hash
|
|
|
|
constexpr unsigned int seed = 131;
|
2021-12-25 10:04:45 +08:00
|
|
|
unsigned int hash = 0;
|
2020-09-07 23:21:15 +08:00
|
|
|
for (auto i = 0u; i < sizeof(void*); ++i)
|
|
|
|
hash = hash * seed + ((const char*)&p)[i];
|
|
|
|
for (auto i = 0u; i < sizeof(int); ++i)
|
|
|
|
hash = hash * seed + ((const char*)&id)[i];
|
|
|
|
return (int)hash;
|
2020-09-04 17:19:51 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_EXT_END
|