#pragma once #include #include #include "cocos2d.h" #include "ExtensionMacros.h" #include "imgui/imgui.h" // #define HAVE_IMGUI_MARKDOWN 1 NS_AX_EXT_BEGIN class ImGuiEventTracker; class ImGuiPresenter { friend class ImGuiRenderer; void init(); void cleanup(); public: enum class CHS_GLYPH_RANGE { NONE, GENERAL, FULL }; enum { DEFAULT_FONT_SIZE = 13 // see imgui.cpp }; static ImGuiPresenter* getInstance(); static void destroyInstance(); static void setOnInit(const std::function& callBack); /// /// 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); float getContentZoomFactor() const { return _contentZoomFactor; } void setViewResolution(float width, float height); /// /// Add ImGui font with contentZoomFactor /// /// /// void addFont(std::string_view fontFile, float fontSize = DEFAULT_FONT_SIZE, CHS_GLYPH_RANGE glyphRange = CHS_GLYPH_RANGE::NONE); void removeFont(std::string_view fontFile); void clearFonts(); /// /// Add a ImGui render loop to specific scene /// /// The FOURCC id of render loop, starts with '#', such as "#abcd" /// the ImGui render loop /// The target scene to track event, nullptr for global, useful for global GM tools bool addRenderLoop(std::string_view id, std::function func, Scene* target); /// /// Remove ImGui render loop /// /// FOURCC starts with '#', such as "#abcd" void removeRenderLoop(std::string_view id); void end(); // imgui helper 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)); std::tuple useTexture(Texture2D* texture); std::tuple useSprite(Sprite* sprite); std::tuple 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); ImWchar* addGlyphRanges(std::string_view key, const std::vector& ranges); static void mergeFontGlyphs(ImFont* dst, ImFont* src, ImWchar start, ImWchar end); int getCCRefId(Ref* p); private: static void loadCustomFonts(void*); // perform draw ImGui stubs void beginFrame(); void update(); void endFrame(); static void deactiveImGuiViewports(); private: static std::function _onInit; struct RenderPipline { ImGuiEventTracker* tracker; std::function frame; }; std::unordered_map _renderPiplines; std::unordered_map usedCCRefIdMap; // cocos objects should be retained until next frame Vector usedCCRef; hlookup::string_map> glyphRanges; float _contentZoomFactor = 1.0f; int64_t _beginFrames = 0; Texture2D* _fontsTexture = nullptr; struct FontInfo { float fontSize; CHS_GLYPH_RANGE glyphRange; }; hlookup::string_map _fontsInfoMap; bool _purgeNextLoop = false; }; NS_AX_EXT_END