123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578 |
- #ifndef __UIRICHTEXT_H__
- #define __UIRICHTEXT_H__
- #include "ui/UIWidget.h"
- #include "ui/GUIExport.h"
- #include "base/CCValue.h"
- NS_CC_BEGIN
- class Label;
- namespace ui {
- class CC_GUI_DLL RichElement : public Ref
- {
- public:
-
- enum class Type
- {
- TEXT,
- IMAGE,
- CUSTOM,
- NEWLINE
- };
-
-
- RichElement(){};
-
-
- virtual ~RichElement(){};
-
-
- bool init(int tag, const Color3B& color, GLubyte opacity);
-
- bool equalType(Type type);
- void setColor(const Color3B& color);
- protected:
- Type _type;
- int _tag;
- Color3B _color;
- GLubyte _opacity;
- friend class RichText;
- };
-
- class CC_GUI_DLL RichElementText : public RichElement
- {
- public:
-
-
- RichElementText()
- {_type = Type::TEXT;};
- enum {
- ITALICS_FLAG = 1 << 0,
- BOLD_FLAG = 1 << 1,
- UNDERLINE_FLAG = 1 << 2,
- STRIKETHROUGH_FLAG = 1 << 3,
- URL_FLAG = 1 << 4,
- OUTLINE_FLAG = 1 << 5,
- SHADOW_FLAG = 1 << 6,
- GLOW_FLAG = 1 << 7
- };
-
-
- virtual ~RichElementText(){};
-
-
- bool init(int tag, const Color3B& color, GLubyte opacity, const std::string& text,
- const std::string& fontName, float fontSize, uint32_t flags, const std::string& url,
- const Color3B& outlineColor = Color3B::WHITE, int outlineSize = -1,
- const Color3B& shadowColor = Color3B::BLACK, const cocos2d::Size& shadowOffset = Size(2.0, -2.0), int shadowBlurRadius = 0,
- const Color3B& glowColor = Color3B::WHITE);
-
-
- static RichElementText* create(int tag, const Color3B& color, GLubyte opacity, const std::string& text,
- const std::string& fontName, float fontSize, uint32_t flags=0, const std::string& url="",
- const Color3B& outlineColor = Color3B::WHITE, int outlineSize = -1,
- const Color3B& shadowColor = Color3B::BLACK, const cocos2d::Size& shadowOffset = Size(2.0, -2.0), int shadowBlurRadius = 0,
- const Color3B& glowColor = Color3B::WHITE);
- protected:
- std::string _text;
- std::string _fontName;
- float _fontSize;
- uint32_t _flags;
- std::string _url;
- Color3B _outlineColor;
- int _outlineSize;
- Color3B _shadowColor;
- cocos2d::Size _shadowOffset;
- int _shadowBlurRadius;
- Color3B _glowColor;
- friend class RichText;
- };
-
- class CC_GUI_DLL RichElementImage : public RichElement
- {
- public:
-
-
- RichElementImage(){_type = Type::IMAGE;};
-
-
- virtual ~RichElementImage(){};
-
-
- bool init(int tag, const Color3B& color, GLubyte opacity, const std::string& filePath, const std::string& url = "");
-
-
- static RichElementImage* create(int tag, const Color3B& color, GLubyte opacity, const std::string& filePath, const std::string& url = "");
- void setWidth(int width);
- void setHeight(int height);
- void setUrl(const std::string& url);
- protected:
- std::string _filePath;
- Rect _textureRect;
- int _textureType;
- friend class RichText;
- int _width;
- int _height;
- std::string _url;
- };
-
- class CC_GUI_DLL RichElementCustomNode : public RichElement
- {
- public:
-
-
- RichElementCustomNode(){_type = Type::CUSTOM;};
-
-
- virtual ~RichElementCustomNode(){CC_SAFE_RELEASE(_customNode);};
-
-
- bool init(int tag, const Color3B& color, GLubyte opacity, Node* customNode);
-
-
- static RichElementCustomNode* create(int tag, const Color3B& color, GLubyte opacity, Node* customNode);
- protected:
- Node* _customNode;
- friend class RichText;
- };
-
- class CC_GUI_DLL RichElementNewLine : public RichElement
- {
- public:
-
-
- RichElementNewLine(){_type = Type::NEWLINE;};
-
-
- virtual ~RichElementNewLine(){};
-
-
- static RichElementNewLine* create(int tag, const Color3B& color, GLubyte opacity);
- protected:
- friend class RichText;
- };
- class CC_GUI_DLL RichText : public Widget
- {
- public:
- enum WrapMode {
- WRAP_PER_WORD,
- WRAP_PER_CHAR
- };
-
-
- typedef std::function<void(const std::string& url)> OpenUrlHandler;
-
-
- typedef std::function<std::pair<ValueMap, RichElement*>(const ValueMap& tagAttrValueMap)> VisitEnterHandler;
-
- static const std::string KEY_VERTICAL_SPACE;
- static const std::string KEY_WRAP_MODE;
- static const std::string KEY_FONT_COLOR_STRING;
- static const std::string KEY_FONT_SIZE;
- static const std::string KEY_FONT_SMALL;
- static const std::string KEY_FONT_BIG;
- static const std::string KEY_FONT_FACE;
- static const std::string KEY_TEXT_BOLD;
- static const std::string KEY_TEXT_ITALIC;
- static const std::string KEY_TEXT_LINE;
- static const std::string VALUE_TEXT_LINE_NONE;
- static const std::string VALUE_TEXT_LINE_DEL;
- static const std::string VALUE_TEXT_LINE_UNDER;
- static const std::string KEY_TEXT_STYLE;
- static const std::string VALUE_TEXT_STYLE_NONE;
- static const std::string VALUE_TEXT_STYLE_OUTLINE;
- static const std::string VALUE_TEXT_STYLE_SHADOW;
- static const std::string VALUE_TEXT_STYLE_GLOW;
- static const std::string KEY_TEXT_OUTLINE_COLOR;
- static const std::string KEY_TEXT_OUTLINE_SIZE;
- static const std::string KEY_TEXT_SHADOW_COLOR;
- static const std::string KEY_TEXT_SHADOW_OFFSET_WIDTH;
- static const std::string KEY_TEXT_SHADOW_OFFSET_HEIGHT;
- static const std::string KEY_TEXT_SHADOW_BLUR_RADIUS;
- static const std::string KEY_TEXT_GLOW_COLOR;
- static const std::string KEY_URL;
- static const std::string KEY_ANCHOR_FONT_COLOR_STRING;
- static const std::string KEY_ANCHOR_TEXT_BOLD;
- static const std::string KEY_ANCHOR_TEXT_ITALIC;
- static const std::string KEY_ANCHOR_TEXT_LINE;
- static const std::string KEY_ANCHOR_TEXT_STYLE;
- static const std::string KEY_ANCHOR_TEXT_OUTLINE_COLOR;
- static const std::string KEY_ANCHOR_TEXT_OUTLINE_SIZE;
- static const std::string KEY_ANCHOR_TEXT_SHADOW_COLOR;
- static const std::string KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH;
- static const std::string KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT;
- static const std::string KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS;
- static const std::string KEY_ANCHOR_TEXT_GLOW_COLOR;
-
-
- RichText();
-
-
- virtual ~RichText();
-
-
- static RichText* create();
-
- static RichText* createWithXML(const std::string& xml, const ValueMap& defaults = ValueMap(), const OpenUrlHandler& handleOpenUrl = nullptr);
-
- void insertElement(RichElement* element, int index);
-
-
- void pushBackElement(RichElement* element);
-
-
- void removeElement(int index);
-
-
- void removeElement(RichElement* element);
-
-
- void setVerticalSpace(float space);
-
-
- void formatText();
-
- virtual void ignoreContentAdaptWithSize(bool ignore) override;
- virtual std::string getDescription() const override;
- void setWrapMode(WrapMode wrapMode);
- WrapMode getWrapMode() const;
- void setFontColor(const std::string& color);
- std::string getFontColor();
- Color3B getFontColor3B();
- void setFontSize(float size);
- float getFontSize();
- void setFontFace(const std::string& face);
- std::string getFontFace();
- void setAnchorFontColor(const std::string& color);
- std::string getAnchorFontColor();
- cocos2d::Color3B getAnchorFontColor3B();
- void setAnchorTextBold(bool enable);
- bool isAnchorTextBoldEnabled();
- void setAnchorTextItalic(bool enable);
- bool isAnchorTextItalicEnabled();
- void setAnchorTextDel(bool enable);
- bool isAnchorTextDelEnabled();
- void setAnchorTextUnderline(bool enable);
- bool isAnchorTextUnderlineEnabled();
-
- void setAnchorTextOutline(bool enable, const Color3B& outlineColor = Color3B::WHITE, int outlineSize = -1);
- bool isAnchorTextOutlineEnabled();
- Color3B getAnchorTextOutlineColor3B();
- int getAnchorTextOutlineSize();
-
- void setAnchorTextShadow(bool enable, const Color3B& shadowColor = Color3B::BLACK, const Size& offset = Size(2.0, -2.0), int blurRadius = 0);
- bool isAnchorTextShadowEnabled();
- Color3B getAnchorTextShadowColor3B();
- Size getAnchorTextShadowOffset();
- int getAnchorTextShadowBlurRadius();
- void setAnchorTextGlow(bool enable, const Color3B& glowColor = Color3B::WHITE);
- bool isAnchorTextGlowEnabled();
- Color3B getAnchorTextGlowColor3B();
- void setDefaults(const ValueMap& defaults);
- ValueMap getDefaults() const;
- cocos2d::Color3B color3BWithString(const std::string& color);
- std::string stringWithColor3B(const cocos2d::Color3B& color3b);
- std::string stringWithColor4B(const cocos2d::Color4B& color4b);
-
- static void setTagDescription(const std::string& tag, bool isFontElement, VisitEnterHandler handleVisitEnter);
-
- static void removeTagDescription(const std::string& tag);
-
- void openUrl(const std::string& url);
-
- void setOpenUrlHandler(const OpenUrlHandler& handleOpenUrl);
- CC_CONSTRUCTOR_ACCESS:
- virtual bool init() override;
- bool initWithXML(const std::string& xml, const ValueMap& defaults = ValueMap(), const OpenUrlHandler& handleOpenUrl = nullptr);
- protected:
- virtual void adaptRenderers() override;
- virtual void initRenderer() override;
- void pushToContainer(Node* renderer);
- void handleTextRenderer(const std::string& text, const std::string& fontName, float fontSize, const Color3B& color,
- GLubyte opacity, uint32_t flags, const std::string& url="",
- const Color3B& outlineColor = Color3B::WHITE, int outlineSize = -1,
- const Color3B& shadowColor = Color3B::BLACK, const cocos2d::Size& shadowOffset = Size(2.0, -2.0), int shadowBlurRadius = 0,
- const Color3B& glowColor = Color3B::WHITE);
- void handleImageRenderer(const std::string& filePath, const Color3B& color, GLubyte opacity, int width, int height, const std::string& url);
- void handleCustomRenderer(Node* renderer);
- void formarRenderers();
- void addNewLine();
- int findSplitPositionForWord(cocos2d::Label* label, const std::string& text);
- int findSplitPositionForChar(cocos2d::Label* label, const std::string& text);
- bool _formatTextDirty;
- Vector<RichElement*> _richElements;
- std::vector<Vector<Node*>*> _elementRenders;
- float _leftSpaceWidth;
- ValueMap _defaults;
- OpenUrlHandler _handleOpenUrl;
- };
-
- }
- NS_CC_END
- #endif
|