2020-09-04 17:19:51 +08:00
|
|
|
#pragma once
|
|
|
|
|
2020-09-08 13:38:16 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <tuple>
|
|
|
|
|
2020-09-04 17:19:51 +08:00
|
|
|
#include "cocos2d.h"
|
|
|
|
#include "ExtensionMacros.h"
|
|
|
|
|
|
|
|
#include "imgui/imgui.h"
|
|
|
|
|
|
|
|
// #define HAVE_IMGUI_MARKDOWN 1
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2020-09-07 23:21:15 +08:00
|
|
|
class ImGuiEXTEventTracker;
|
2020-09-04 17:19:51 +08:00
|
|
|
class ImGuiEXT
|
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
friend class ImGuiEXTRenderer;
|
|
|
|
void init();
|
2020-09-08 13:38:16 +08:00
|
|
|
void cleanup();
|
2020-09-07 21:22:03 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
public:
|
|
|
|
enum class CHS_GLYPH_RANGE
|
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
NONE,
|
|
|
|
GENERAL,
|
|
|
|
FULL
|
|
|
|
};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
DEFAULT_FONT_SIZE = 13 // see imgui.cpp
|
2020-09-07 23:21:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static ImGuiEXT* getInstance();
|
|
|
|
static void destroyInstance();
|
|
|
|
static void setOnInit(const std::function<void(ImGuiEXT*)>& callBack);
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
float getContentZoomFactor() const { return _contentZoomFactor; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Add ImGui font with contentZoomFactor
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="fontFile"></param>
|
|
|
|
/// <param name="glyphRange"></param>
|
2021-12-28 21:27:32 +08:00
|
|
|
void addFont(std::string_view fontFile,
|
2021-12-25 10:04:45 +08:00
|
|
|
float fontSize = DEFAULT_FONT_SIZE,
|
|
|
|
CHS_GLYPH_RANGE glyphRange = CHS_GLYPH_RANGE::NONE);
|
2021-12-28 21:27:32 +08:00
|
|
|
void removeFont(std::string_view fontFile);
|
2020-09-07 23:21:15 +08:00
|
|
|
void clearFonts();
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Add a ImGui render loop to specific scene
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">The FOURCC id of render loop, starts with '#', such as "#abcd"</id>
|
|
|
|
/// <param name="func">the ImGui render loop</param>
|
|
|
|
/// <param name="target">The target scene to track event, nullptr for global, useful for global GM tools</param>
|
2021-12-28 21:27:32 +08:00
|
|
|
bool addRenderLoop(std::string_view id, std::function<void()> func, Scene* target);
|
2020-09-07 23:21:15 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Remove ImGui render loop
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">FOURCC starts with '#', such as "#abcd"</id>
|
2021-12-28 21:27:32 +08:00
|
|
|
void removeRenderLoop(std::string_view id);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-09-10 17:53:17 +08:00
|
|
|
void end();
|
2020-09-07 23:21:15 +08:00
|
|
|
|
|
|
|
// imgui helper
|
2021-12-25 10:04:45 +08:00
|
|
|
void image(Texture2D* tex,
|
|
|
|
const ImVec2& size,
|
|
|
|
const ImVec2& uv0 = ImVec2(0, 0),
|
|
|
|
const ImVec2& uv1 = ImVec2(1, 1),
|
|
|
|
const ImVec4& tint_col = ImVec4(1, 1, 1, 1),
|
|
|
|
const ImVec4& border_col = ImVec4(0, 0, 0, 0));
|
|
|
|
void image(Sprite* sprite,
|
|
|
|
const ImVec2& size,
|
|
|
|
const ImVec4& tint_col = ImVec4(1, 1, 1, 1),
|
|
|
|
const ImVec4& border_col = ImVec4(0, 0, 0, 0));
|
|
|
|
bool imageButton(Texture2D* tex,
|
|
|
|
const ImVec2& size,
|
|
|
|
const ImVec2& uv0 = ImVec2(0, 0),
|
|
|
|
const ImVec2& uv1 = ImVec2(1, 1),
|
|
|
|
int frame_padding = -1,
|
|
|
|
const ImVec4& bg_col = ImVec4(0, 0, 0, 0),
|
|
|
|
const ImVec4& tint_col = ImVec4(1, 1, 1, 1));
|
|
|
|
bool imageButton(Sprite* sprite,
|
|
|
|
const ImVec2& size,
|
|
|
|
int frame_padding = -1,
|
|
|
|
const ImVec4& bg_col = ImVec4(0, 0, 0, 0),
|
|
|
|
const ImVec4& tint_col = ImVec4(1, 1, 1, 1));
|
|
|
|
|
|
|
|
void node(Node* node, const ImVec4& tint_col = ImVec4(1, 1, 1, 1), const ImVec4& border_col = ImVec4(0, 0, 0, 0));
|
|
|
|
bool nodeButton(Node* node,
|
|
|
|
int frame_padding = -1,
|
|
|
|
const ImVec4& bg_col = ImVec4(0, 0, 0, 0),
|
|
|
|
const ImVec4& tint_col = ImVec4(1, 1, 1, 1));
|
2020-09-07 23:21:15 +08:00
|
|
|
|
|
|
|
std::tuple<ImTextureID, int> useTexture(Texture2D* texture);
|
|
|
|
std::tuple<ImTextureID, ImVec2, ImVec2, int> useSprite(Sprite* sprite);
|
|
|
|
std::tuple<ImTextureID, ImVec2, ImVec2, int> useNode(Node* node, const ImVec2& pos);
|
|
|
|
|
|
|
|
static void setNodeColor(Node* node, const ImVec4& col);
|
|
|
|
static void setNodeColor(Node* node, ImGuiCol col);
|
|
|
|
static void setLabelColor(Label* label, const ImVec4& col);
|
|
|
|
static void setLabelColor(Label* label, bool disabled = false);
|
|
|
|
static void setLabelColor(Label* label, ImGuiCol col);
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
ImWchar* addGlyphRanges(std::string_view key, const std::vector<ImWchar>& ranges);
|
2020-09-07 23:21:15 +08:00
|
|
|
static void mergeFontGlyphs(ImFont* dst, ImFont* src, ImWchar start, ImWchar end);
|
|
|
|
int getCCRefId(Ref* p);
|
2020-09-04 17:19:51 +08:00
|
|
|
|
|
|
|
#if defined(HAVE_IMGUI_MARKDOWN)
|
2020-09-07 23:21:15 +08:00
|
|
|
// markdown
|
2020-09-04 17:19:51 +08:00
|
|
|
|
2020-09-07 23:21:15 +08:00
|
|
|
using MdLinkCallback = std::function<void(const std::string&, const std::string&, bool)>;
|
2021-12-25 10:04:45 +08:00
|
|
|
using MdImageCallback =
|
|
|
|
std::function<std::tuple<Sprite*, ImVec2, ImVec4, ImVec4>(const std::string&, const std::string&)>;
|
2020-09-04 17:19:51 +08:00
|
|
|
|
2020-09-07 23:21:15 +08:00
|
|
|
void setMarkdownLinkCallback(const MdLinkCallback& f);
|
|
|
|
void setMarkdownImageCallback(const MdImageCallback& f);
|
|
|
|
void setMarkdownFont(int index, ImFont* font, bool seperator, float scale = 1.f);
|
2021-12-28 21:27:32 +08:00
|
|
|
void setMarkdownLinkIcon(std::string_view icon);
|
|
|
|
void markdown(std::string_view content);
|
2020-09-04 17:19:51 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
2020-09-08 13:38:16 +08:00
|
|
|
static void loadCustomFonts(void*);
|
|
|
|
|
2020-09-07 23:21:15 +08:00
|
|
|
// perform draw ImGui stubs
|
|
|
|
void beginFrame();
|
|
|
|
void update();
|
|
|
|
void endFrame();
|
2020-09-04 17:19:51 +08:00
|
|
|
|
2020-09-08 13:38:16 +08:00
|
|
|
static void deactiveImGuiViewports();
|
|
|
|
|
2020-09-04 17:19:51 +08:00
|
|
|
private:
|
2020-09-07 23:21:15 +08:00
|
|
|
static std::function<void(ImGuiEXT*)> _onInit;
|
2020-09-05 17:10:09 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
struct RenderPipline
|
|
|
|
{
|
2020-09-07 23:21:15 +08:00
|
|
|
ImGuiEXTEventTracker* tracker;
|
|
|
|
std::function<void()> frame;
|
|
|
|
};
|
2020-09-05 17:10:09 +08:00
|
|
|
|
2020-09-07 23:21:15 +08:00
|
|
|
std::unordered_map<uint32_t, RenderPipline> _renderPiplines;
|
2020-09-07 21:22:03 +08:00
|
|
|
|
2020-09-07 23:21:15 +08:00
|
|
|
std::unordered_map<Ref*, int> usedCCRefIdMap;
|
|
|
|
// cocos objects should be retained until next frame
|
|
|
|
Vector<Ref*> usedCCRef;
|
2021-12-28 21:27:32 +08:00
|
|
|
hlookup::string_map<std::vector<ImWchar>> glyphRanges;
|
2020-09-07 23:21:15 +08:00
|
|
|
|
|
|
|
float _contentZoomFactor = 1.0f;
|
2020-09-08 13:38:16 +08:00
|
|
|
|
|
|
|
int64_t _beginFrames = 0;
|
|
|
|
|
|
|
|
Texture2D* _fontsTexture = nullptr;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
struct FontInfo
|
|
|
|
{
|
2020-09-08 13:38:16 +08:00
|
|
|
float fontSize;
|
|
|
|
CHS_GLYPH_RANGE glyphRange;
|
|
|
|
};
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
hlookup::string_map<FontInfo> _fontsInfoMap;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-09-10 17:53:17 +08:00
|
|
|
bool _purgeNextLoop = false;
|
2020-09-04 17:19:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_EXT_END
|