UIRichText.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __UIRICHTEXT_H__
  22. #define __UIRICHTEXT_H__
  23. #include "ui/UIWidget.h"
  24. #include "ui/GUIExport.h"
  25. #include "base/CCValue.h"
  26. NS_CC_BEGIN
  27. /**
  28. * @addtogroup ui
  29. * @{
  30. */
  31. class Label;
  32. namespace ui {
  33. /**
  34. *@brief Rich text element base class.
  35. * It defines the basic common properties for all rich text element.
  36. */
  37. class CC_GUI_DLL RichElement : public Ref
  38. {
  39. public:
  40. /**
  41. *@brief Rich element type.
  42. */
  43. enum class Type
  44. {
  45. TEXT, /*!< RichElementText */
  46. IMAGE, /*!< RichElementImage */
  47. CUSTOM, /*!< RichElementCustomNode */
  48. NEWLINE /*!< RichElementNewLine */
  49. };
  50. /**
  51. * @brief Default constructor.
  52. * @js ctor
  53. * @lua new
  54. */
  55. RichElement(){};
  56. /**
  57. * @brief Default destructor.
  58. * @js NA
  59. * @lua NA
  60. */
  61. virtual ~RichElement(){};
  62. /**
  63. * @brief Initialize a rich element with different arguments.
  64. *
  65. * @param tag A integer tag value.
  66. * @param color A color in @see `Color3B`.
  67. * @param opacity A opacity value in `GLubyte`.
  68. * @return True if initialize success, false otherwise.
  69. */
  70. bool init(int tag, const Color3B& color, GLubyte opacity);
  71. bool equalType(Type type);
  72. void setColor(const Color3B& color);
  73. protected:
  74. Type _type; /*!< Rich element type. */
  75. int _tag; /*!< A integer tag value. */
  76. Color3B _color; /*!< A color in `Color3B`. */
  77. GLubyte _opacity; /*!< A opacity value in `GLubyte`. */
  78. friend class RichText;
  79. };
  80. /**
  81. *@brief Rich element for displaying text.
  82. */
  83. class CC_GUI_DLL RichElementText : public RichElement
  84. {
  85. public:
  86. /**
  87. *@brief Default constructor.
  88. * @js ctor
  89. * @lua new
  90. */
  91. RichElementText()
  92. {_type = Type::TEXT;};
  93. enum {
  94. ITALICS_FLAG = 1 << 0, /*!< italic text */
  95. BOLD_FLAG = 1 << 1, /*!< bold text */
  96. UNDERLINE_FLAG = 1 << 2, /*!< underline */
  97. STRIKETHROUGH_FLAG = 1 << 3, /*!< strikethrough */
  98. URL_FLAG = 1 << 4, /*!< url of anchor */
  99. OUTLINE_FLAG = 1 << 5, /*!< outline effect */
  100. SHADOW_FLAG = 1 << 6, /*!< shadow effect */
  101. GLOW_FLAG = 1 << 7 /*!< glow effect */
  102. };
  103. /**
  104. *@brief Default destructor.
  105. * @js NA
  106. * @lua NA
  107. */
  108. virtual ~RichElementText(){};
  109. /**
  110. * @brief Initialize a RichElementText with various arguments.
  111. *
  112. * @param tag A integer tag value.
  113. * @param color A color in Color3B.
  114. * @param opacity A opacity in GLubyte.
  115. * @param text Content string.
  116. * @param fontName Content font name.
  117. * @param fontSize Content font size.
  118. * @param flags italics, bold, underline, strikethrough, url, outline, shadow or glow
  119. * @param url uniform resource locator
  120. * @param outlineColor the color of the outline
  121. * @param outlineSize the outline effect size value
  122. * @param shadowColor the shadow effect color value
  123. * @param shadowOffset shadow effect offset value
  124. * @param shadowBlurRadius the shadow effect blur radius
  125. * @param glowColor glow color
  126. * @return True if initialize success, false otherwise.
  127. */
  128. bool init(int tag, const Color3B& color, GLubyte opacity, const std::string& text,
  129. const std::string& fontName, float fontSize, uint32_t flags, const std::string& url,
  130. const Color3B& outlineColor = Color3B::WHITE, int outlineSize = -1,
  131. const Color3B& shadowColor = Color3B::BLACK, const cocos2d::Size& shadowOffset = Size(2.0, -2.0), int shadowBlurRadius = 0,
  132. const Color3B& glowColor = Color3B::WHITE);
  133. /**
  134. * @brief Create a RichElementText with various arguments.
  135. *
  136. * @param tag A integer tag value.
  137. * @param color A color in Color3B.
  138. * @param opacity A opacity in GLubyte.
  139. * @param text Content string.
  140. * @param fontName Content font name.
  141. * @param fontSize Content font size.
  142. * @param flags italics, bold, underline, strikethrough, url, outline, shadow or glow
  143. * @param url uniform resource locator
  144. * @param outlineColor the color of the outline
  145. * @param outlineSize the outline effect size value
  146. * @param shadowColor the shadow effect color value
  147. * @param shadowOffset shadow effect offset value
  148. * @param shadowBlurRadius the shadow effect blur radius
  149. * @param glowColor glow color
  150. * @return RichElementText instance.
  151. */
  152. static RichElementText* create(int tag, const Color3B& color, GLubyte opacity, const std::string& text,
  153. const std::string& fontName, float fontSize, uint32_t flags=0, const std::string& url="",
  154. const Color3B& outlineColor = Color3B::WHITE, int outlineSize = -1,
  155. const Color3B& shadowColor = Color3B::BLACK, const cocos2d::Size& shadowOffset = Size(2.0, -2.0), int shadowBlurRadius = 0,
  156. const Color3B& glowColor = Color3B::WHITE);
  157. void setRichIsNewLine(bool isnewline) {
  158. _isNewLine = isnewline;
  159. }
  160. void setMaxNewLine(int maxnum) {
  161. _maxNewLine = maxnum;
  162. }
  163. protected:
  164. std::string _text;
  165. std::string _fontName;
  166. float _fontSize;
  167. uint32_t _flags;
  168. std::string _url;
  169. Color3B _outlineColor; /*!< the color of the outline */
  170. int _outlineSize; /*!< the outline effect size value */
  171. Color3B _shadowColor; /*!< the shadow effect color value */
  172. cocos2d::Size _shadowOffset; /*!< shadow effect offset value */
  173. int _shadowBlurRadius; /*!< the shadow effect blur radius */
  174. Color3B _glowColor; /*!< attributes of glow tag */
  175. friend class RichText;
  176. bool _isNewLine;
  177. int _maxNewLine;
  178. };
  179. /**
  180. *@brief Rich element for displaying images.
  181. */
  182. class CC_GUI_DLL RichElementImage : public RichElement
  183. {
  184. public:
  185. /**
  186. * @brief Default constructor.
  187. * @js ctor
  188. * @lua new
  189. *
  190. */
  191. RichElementImage(){_type = Type::IMAGE;};
  192. /**
  193. * @brief Default destructor.
  194. * @js NA
  195. * @lua NA
  196. */
  197. virtual ~RichElementImage(){};
  198. /**
  199. * @brief Initialize a RichElementImage with various arguments.
  200. *
  201. * @param tag A integer tag value.
  202. * @param color A color in Color3B.
  203. * @param opacity A opacity in GLubyte.
  204. * @param filePath A image file name.
  205. * @param url uniform resource locator
  206. * @param texType texture type, may be a valid file path, or a sprite frame name
  207. * @return True if initialize success, false otherwise.
  208. */
  209. bool init(int tag, const Color3B& color, GLubyte opacity, const std::string& filePath, const std::string& url = "", Widget::TextureResType texType = Widget::TextureResType::LOCAL);
  210. /**
  211. * @brief Create a RichElementImage with various arguments.
  212. *
  213. * @param tag A integer tag value.
  214. * @param color A color in Color3B.
  215. * @param opacity A opacity in GLubyte.
  216. * @param filePath A image file name.
  217. * @param url uniform resource locator
  218. * @param texType texture type, may be a valid file path, or a sprite frame name
  219. * @return A RichElementImage instance.
  220. */
  221. static RichElementImage* create(int tag, const Color3B& color, GLubyte opacity, const std::string& filePath, const std::string& url = "", Widget::TextureResType texType = Widget::TextureResType::LOCAL);
  222. void setWidth(int width);
  223. void setHeight(int height);
  224. void setUrl(const std::string& url);
  225. protected:
  226. std::string _filePath;
  227. Rect _textureRect;
  228. Widget::TextureResType _textureType;
  229. friend class RichText;
  230. int _width;
  231. int _height;
  232. std::string _url; /*!< attributes of anchor tag */
  233. };
  234. /**
  235. *@brief Rich element for displaying custom node type.
  236. */
  237. class CC_GUI_DLL RichElementCustomNode : public RichElement
  238. {
  239. public:
  240. /**
  241. * @brief Default constructor.
  242. * @js ctor
  243. * @lua new
  244. */
  245. RichElementCustomNode(){_type = Type::CUSTOM;};
  246. /**
  247. * @brief Default destructor.
  248. * @js NA
  249. * @lua NA
  250. */
  251. virtual ~RichElementCustomNode(){CC_SAFE_RELEASE(_customNode);};
  252. /**
  253. * @brief Initialize a RichElementCustomNode with various arguments.
  254. *
  255. * @param tag A integer tag value.
  256. * @param color A color in Color3B.
  257. * @param opacity A opacity in GLubyte.
  258. * @param customNode A custom node pointer.
  259. * @return True if initialize success, false otherwise.
  260. */
  261. bool init(int tag, const Color3B& color, GLubyte opacity, Node* customNode);
  262. /**
  263. * @brief Create a RichElementCustomNode with various arguments.
  264. *
  265. * @param tag A integer tag value.
  266. * @param color A color in Color3B.
  267. * @param opacity A opacity in GLubyte.
  268. * @param customNode A custom node pointer.
  269. * @return A RichElementCustomNode instance.
  270. */
  271. static RichElementCustomNode* create(int tag, const Color3B& color, GLubyte opacity, Node* customNode);
  272. protected:
  273. Node* _customNode;
  274. friend class RichText;
  275. };
  276. /**
  277. *@brief Rich element for new line.
  278. */
  279. class CC_GUI_DLL RichElementNewLine : public RichElement
  280. {
  281. public:
  282. /**
  283. * @brief Default constructor.
  284. * @js ctor
  285. * @lua new
  286. *
  287. */
  288. RichElementNewLine(){_type = Type::NEWLINE;};
  289. /**
  290. * @brief Default destructor.
  291. * @js NA
  292. * @lua NA
  293. */
  294. virtual ~RichElementNewLine(){};
  295. /**
  296. * @brief Create a RichElementNewLine with various arguments.
  297. *
  298. * @param tag A integer tag value.
  299. * @param color A color in Color3B.
  300. * @param opacity A opacity in GLubyte.
  301. * @return A RichElementNewLine instance.
  302. */
  303. static RichElementNewLine* create(int tag, const Color3B& color, GLubyte opacity);
  304. protected:
  305. friend class RichText;
  306. };
  307. /**
  308. *@brief A container for displaying various RichElements.
  309. * We could use it to display texts with images easily.
  310. */
  311. class CC_GUI_DLL RichText : public Widget
  312. {
  313. public:
  314. enum WrapMode {
  315. WRAP_PER_WORD,
  316. WRAP_PER_CHAR
  317. };
  318. enum class HorizontalAlignment {
  319. LEFT,
  320. CENTER,
  321. RIGHT,
  322. };
  323. /**
  324. * @brief call to open a resource specified by a URL
  325. * @param url a URL
  326. */
  327. typedef std::function<void(const std::string& url)> OpenUrlHandler;
  328. /**
  329. * @brief called on the specified tag
  330. * @param tagAttrValueMap the attributes of a tag
  331. * @result text attributes and RichElement
  332. */
  333. typedef std::function<std::pair<ValueMap, RichElement*>(const ValueMap& tagAttrValueMap)> VisitEnterHandler;
  334. static const std::string KEY_VERTICAL_SPACE; /*!< key of vertical space */
  335. static const std::string KEY_WRAP_MODE; /*!< key of per word, or per char */
  336. static const std::string KEY_HORIZONTAL_ALIGNMENT; /*!< key of left, right, or center */
  337. static const std::string KEY_FONT_COLOR_STRING; /*!< key of font color */
  338. static const std::string KEY_FONT_SIZE; /*!< key of font size */
  339. static const std::string KEY_FONT_SMALL; /*!< key of font size small */
  340. static const std::string KEY_FONT_BIG; /*!< key of font size big */
  341. static const std::string KEY_FONT_FACE; /*!< key of font name */
  342. static const std::string KEY_TEXT_BOLD; /*!< key of text bold */
  343. static const std::string KEY_TEXT_ITALIC; /*!< key of text italic */
  344. static const std::string KEY_TEXT_LINE; /*!< key of line style */
  345. static const std::string VALUE_TEXT_LINE_NONE; /*!< value of none */
  346. static const std::string VALUE_TEXT_LINE_DEL; /*!< value of strikethrough line */
  347. static const std::string VALUE_TEXT_LINE_UNDER; /*!< value of underline */
  348. static const std::string KEY_TEXT_STYLE; /*!< key of effect style */
  349. static const std::string VALUE_TEXT_STYLE_NONE; /*!< value of none */
  350. static const std::string VALUE_TEXT_STYLE_OUTLINE; /*!< value of outline */
  351. static const std::string VALUE_TEXT_STYLE_SHADOW; /*!< value of shadow */
  352. static const std::string VALUE_TEXT_STYLE_GLOW; /*!< value of glow */
  353. static const std::string KEY_TEXT_OUTLINE_COLOR; /*!< key of outline color */
  354. static const std::string KEY_TEXT_OUTLINE_SIZE; /*!< key of outline size */
  355. static const std::string KEY_TEXT_SHADOW_COLOR; /*!< key of shadow color */
  356. static const std::string KEY_TEXT_SHADOW_OFFSET_WIDTH; /*!< key of shadow offset (width) */
  357. static const std::string KEY_TEXT_SHADOW_OFFSET_HEIGHT; /*!< key of shadow offset (height) */
  358. static const std::string KEY_TEXT_SHADOW_BLUR_RADIUS; /*!< key of shadow blur radius */
  359. static const std::string KEY_TEXT_GLOW_COLOR; /*!< key of glow color */
  360. static const std::string KEY_URL; /*!< key of url */
  361. static const std::string KEY_ANCHOR_FONT_COLOR_STRING; /*!< key of font color of anchor tag */
  362. static const std::string KEY_ANCHOR_TEXT_BOLD; /*!< key of text bold of anchor tag */
  363. static const std::string KEY_ANCHOR_TEXT_ITALIC; /*!< key of text italic of anchor tag */
  364. static const std::string KEY_ANCHOR_TEXT_LINE; /*!< key of line style of anchor tag */
  365. static const std::string KEY_ANCHOR_TEXT_STYLE; /*!< key of effect style of anchor tag */
  366. static const std::string KEY_ANCHOR_TEXT_OUTLINE_COLOR; /*!< key of outline color of anchor tag */
  367. static const std::string KEY_ANCHOR_TEXT_OUTLINE_SIZE; /*!< key of outline size of anchor tag */
  368. static const std::string KEY_ANCHOR_TEXT_SHADOW_COLOR; /*!< key of shadow color of anchor tag */
  369. static const std::string KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH; /*!< key of shadow offset (width) of anchor tag */
  370. static const std::string KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT; /*!< key of shadow offset (height) of anchor tag */
  371. static const std::string KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS; /*!< key of shadow blur radius of anchor tag */
  372. static const std::string KEY_ANCHOR_TEXT_GLOW_COLOR; /*!< key of glow color of anchor tag */
  373. /**
  374. * @brief Default constructor.
  375. * @js ctor
  376. * @lua new
  377. */
  378. RichText();
  379. /**
  380. * @brief Default destructor.
  381. * @js NA
  382. * @lua NA
  383. */
  384. virtual ~RichText();
  385. /**
  386. * @brief Create a empty RichText.
  387. *
  388. * @return RichText instance.
  389. */
  390. static RichText* create();
  391. /**
  392. * @brief Create a RichText from an XML
  393. *
  394. * @return RichText instance.
  395. */
  396. static RichText* createWithXML(const std::string& xml, const ValueMap& defaults = ValueMap(), const OpenUrlHandler& handleOpenUrl = nullptr);
  397. /**
  398. * @brief Insert a RichElement at a given index.
  399. *
  400. * @param element A RichElement type.
  401. * @param index A given index.
  402. */
  403. void insertElement(RichElement* element, int index);
  404. /**
  405. * @brief Add a RichElement at the end of RichText.
  406. *
  407. * @param element A RichElement instance.
  408. */
  409. void pushBackElement(RichElement* element);
  410. /**
  411. * @brief Remove a RichElement at a given index.
  412. *
  413. * @param index A integer index value.
  414. */
  415. void removeElement(int index);
  416. /**
  417. * @brief Remove specific RichElement.
  418. *
  419. * @param element A RichElement type.
  420. */
  421. void removeElement(RichElement* element);
  422. /**
  423. * @brief Set vertical space between each RichElement.
  424. *
  425. * @param space Point in float.
  426. */
  427. void setVerticalSpace(float space);
  428. /**
  429. * @brief Rearrange all RichElement in the RichText.
  430. * It's usually called internally.
  431. */
  432. void formatText();
  433. //override functions.
  434. virtual void ignoreContentAdaptWithSize(bool ignore) override;
  435. virtual std::string getDescription() const override;
  436. void setWrapMode(WrapMode wrapMode); /*!< sets the wrapping mode: WRAP_PER_CHAR or WRAP_PER_WORD */
  437. WrapMode getWrapMode() const; /*!< returns the current wrapping mode */
  438. void setHorizontalAlignment(HorizontalAlignment a); /*!< sets the horizontal alignment mode: LEFT, CENTER, or RIGHT */
  439. HorizontalAlignment getHorizontalAlignment() const; /*!< returns the current horizontal alignment mode */
  440. void setFontColor(const std::string& color); /*!< Set the font color. @param color the #RRGGBB hexadecimal notation. */
  441. std::string getFontColor(); /*!< return the current font color */
  442. Color3B getFontColor3B(); /*!< return the current font color */
  443. void setFontSize(float size); /*!< Set the font size. @param size the font size. */
  444. float getFontSize(); /*!< return the current font size */
  445. void setFontFace(const std::string& face); /*!< Set the font face. @param face the font face. */
  446. std::string getFontFace(); /*!< return the current font face */
  447. void setAnchorFontColor(const std::string& color); /*!< Set the font color of a-tag. @param face the font color. */
  448. std::string getAnchorFontColor(); /*!< return the current font color of a-tag */
  449. cocos2d::Color3B getAnchorFontColor3B(); /*!< return the current font color of a-tag */
  450. void setAnchorTextBold(bool enable); /*!< enable bold text of a-tag */
  451. bool isAnchorTextBoldEnabled(); /*!< valid style is bold text of a-tag? */
  452. void setAnchorTextItalic(bool enable); /*!< enable italic text of a-tag */
  453. bool isAnchorTextItalicEnabled(); /*!< valid style is italic text of a-tag? */
  454. void setAnchorTextDel(bool enable); /*!< enable the strikethrough of a-tag */
  455. bool isAnchorTextDelEnabled(); /*!< valid strikethrough of a-tag? */
  456. void setAnchorTextUnderline(bool enable); /*!< enable the underline of a-tag */
  457. bool isAnchorTextUnderlineEnabled(); /*!< valid underline of a-tag? */
  458. /** @brief enable the outline of a-tag */
  459. void setAnchorTextOutline(bool enable, const Color3B& outlineColor = Color3B::WHITE, int outlineSize = -1);
  460. bool isAnchorTextOutlineEnabled(); /*!< valid outline of a-tag? */
  461. Color3B getAnchorTextOutlineColor3B(); /*!< return the current text outline color of a-tag */
  462. int getAnchorTextOutlineSize(); /*!< return the current text outline size of a-tag */
  463. /** @brief enable the shadow of a-tag */
  464. void setAnchorTextShadow(bool enable, const Color3B& shadowColor = Color3B::BLACK, const Size& offset = Size(2.0, -2.0), int blurRadius = 0);
  465. bool isAnchorTextShadowEnabled(); /*!< valid shadow of a-tag? */
  466. Color3B getAnchorTextShadowColor3B(); /*!< return the current text shadow color of a-tag */
  467. Size getAnchorTextShadowOffset(); /*!< return the current text shadow offset of a-tag */
  468. int getAnchorTextShadowBlurRadius(); /*!< return the current text shadow blur radius of a-tag */
  469. void setAnchorTextGlow(bool enable, const Color3B& glowColor = Color3B::WHITE); /*!< enable the glow of a-tag */
  470. bool isAnchorTextGlowEnabled(); /*!< valid glow of a-tag? */
  471. Color3B getAnchorTextGlowColor3B(); /*!< return the current text glow color of a-tag */
  472. void setDefaults(const ValueMap& defaults); /*!< set the default values */
  473. ValueMap getDefaults() const; /*!< returns the default values */
  474. cocos2d::Color3B color3BWithString(const std::string& color); /*!< convert a color string into a Color3B. */
  475. std::string stringWithColor3B(const cocos2d::Color3B& color3b); /*!< convert a Color3B into a color string. */
  476. std::string stringWithColor4B(const cocos2d::Color4B& color4b); /*!< convert a Color4B into a color string. */
  477. /**
  478. * @brief add a callback to own tag.
  479. * @param tag tag's name
  480. * @param isFontElement use attributes of text tag
  481. * @param handleVisitEnter callback
  482. */
  483. static void setTagDescription(const std::string& tag, bool isFontElement, VisitEnterHandler handleVisitEnter);
  484. /**
  485. * @brief remove a callback to own tag.
  486. * @param tag tag's name
  487. */
  488. static void removeTagDescription(const std::string& tag);
  489. void openUrl(const std::string& url);
  490. /**
  491. * @brief Asks the callback to open a resource specified by a URL.
  492. * @discussion If you set up your own URL in the anchor tag, it is used to intercept the URL open.
  493. * @param handleOpenUrl the callback
  494. */
  495. void setOpenUrlHandler(const OpenUrlHandler& handleOpenUrl);
  496. CC_CONSTRUCTOR_ACCESS:
  497. virtual bool init() override;
  498. bool initWithXML(const std::string& xml, const ValueMap& defaults = ValueMap(), const OpenUrlHandler& handleOpenUrl = nullptr);
  499. protected:
  500. virtual void adaptRenderers() override;
  501. virtual void initRenderer() override;
  502. void pushToContainer(Node* renderer);
  503. void handleTextRenderer(const std::string& text, const std::string& fontName, float fontSize, const Color3B& color,
  504. GLubyte opacity, uint32_t flags, const std::string& url = "",
  505. const Color3B& outlineColor = Color3B::WHITE, int outlineSize = -1,
  506. const Color3B& shadowColor = Color3B::BLACK, const Size& shadowOffset = Size(2.0, -2.0), int shadowBlurRadius = 0,
  507. const Color3B& glowColor = Color3B::WHITE);
  508. void handleImageRenderer(const std::string& filePath, const Color3B& color, GLubyte opacity, int width, int height, const std::string& url);
  509. void handleCustomRenderer(Node* renderer);
  510. void formatRenderers();
  511. void addNewLine();
  512. void doHorizontalAlignment(const Vector<Node*>& row, float rowWidth);
  513. float stripTrailingWhitespace(const Vector<Node*>& row);
  514. bool _formatTextDirty;
  515. Vector<RichElement*> _richElements;
  516. std::vector<Vector<Node*>> _elementRenders;
  517. std::vector<float> _lineHeights;
  518. float _leftSpaceWidth;
  519. ValueMap _defaults; /*!< default values */
  520. OpenUrlHandler _handleOpenUrl; /*!< the callback for open URL */
  521. };
  522. }
  523. // end of ui group
  524. /// @}
  525. NS_CC_END
  526. #endif /* defined(__UIRichText__) */