guiHyperText.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. Minetest
  3. Copyright (C) 2019 EvicenceBKidscode / Pierre-Yves Rollo <dev@pyrollo.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "config.h" // for USE_FREETYPE
  18. using namespace irr;
  19. class ISimpleTextureSource;
  20. class Client;
  21. #if USE_FREETYPE
  22. #include "irrlicht_changes/CGUITTFont.h"
  23. #endif
  24. class ParsedText
  25. {
  26. public:
  27. ParsedText(const wchar_t *text);
  28. ~ParsedText();
  29. enum ElementType
  30. {
  31. ELEMENT_TEXT,
  32. ELEMENT_SEPARATOR,
  33. ELEMENT_IMAGE,
  34. ELEMENT_ITEM
  35. };
  36. enum BackgroundType
  37. {
  38. BACKGROUND_NONE,
  39. BACKGROUND_COLOR
  40. };
  41. enum FloatType
  42. {
  43. FLOAT_NONE,
  44. FLOAT_RIGHT,
  45. FLOAT_LEFT
  46. };
  47. enum HalignType
  48. {
  49. HALIGN_CENTER,
  50. HALIGN_LEFT,
  51. HALIGN_RIGHT,
  52. HALIGN_JUSTIFY
  53. };
  54. enum ValignType
  55. {
  56. VALIGN_MIDDLE,
  57. VALIGN_TOP,
  58. VALIGN_BOTTOM
  59. };
  60. typedef std::unordered_map<std::string, std::string> StyleList;
  61. typedef std::unordered_map<std::string, std::string> AttrsList;
  62. struct Tag
  63. {
  64. std::string name;
  65. AttrsList attrs;
  66. StyleList style;
  67. };
  68. struct Element
  69. {
  70. std::list<Tag *> tags;
  71. ElementType type;
  72. core::stringw text = "";
  73. core::dimension2d<u32> dim;
  74. core::position2d<s32> pos;
  75. s32 drawwidth;
  76. FloatType floating = FLOAT_NONE;
  77. ValignType valign;
  78. #if USE_FREETYPE
  79. gui::CGUITTFont *font;
  80. #else
  81. gui::IGUIFont *font;
  82. #endif
  83. irr::video::SColor color;
  84. irr::video::SColor hovercolor;
  85. bool underline;
  86. s32 baseline = 0;
  87. // img & item specific attributes
  88. std::string name;
  89. v3s16 angle{0, 0, 0};
  90. v3s16 rotation{0, 0, 0};
  91. s32 margin = 10;
  92. void setStyle(StyleList &style);
  93. };
  94. struct Paragraph
  95. {
  96. std::vector<Element> elements;
  97. HalignType halign;
  98. s32 margin = 10;
  99. void setStyle(StyleList &style);
  100. };
  101. std::vector<Paragraph> m_paragraphs;
  102. // Element style
  103. s32 margin = 3;
  104. ValignType valign = VALIGN_TOP;
  105. BackgroundType background_type = BACKGROUND_NONE;
  106. irr::video::SColor background_color;
  107. Tag m_root_tag;
  108. protected:
  109. // Parser functions
  110. void enterElement(ElementType type);
  111. void endElement();
  112. void enterParagraph();
  113. void endParagraph();
  114. void pushChar(wchar_t c);
  115. ParsedText::Tag *newTag(const std::string &name, const AttrsList &attrs);
  116. ParsedText::Tag *openTag(const std::string &name, const AttrsList &attrs);
  117. bool closeTag(const std::string &name);
  118. void parseGenericStyleAttr(const std::string &name, const std::string &value,
  119. StyleList &style);
  120. void parseStyles(const AttrsList &attrs, StyleList &style);
  121. void globalTag(const ParsedText::AttrsList &attrs);
  122. u32 parseTag(const wchar_t *text, u32 cursor);
  123. void parse(const wchar_t *text);
  124. std::unordered_map<std::string, StyleList> m_elementtags;
  125. std::unordered_map<std::string, StyleList> m_paragraphtags;
  126. std::vector<Tag *> m_tags;
  127. std::list<Tag *> m_active_tags;
  128. // Current values
  129. StyleList m_style;
  130. Element *m_element;
  131. Paragraph *m_paragraph;
  132. };
  133. class TextDrawer
  134. {
  135. public:
  136. TextDrawer(const wchar_t *text, Client *client, gui::IGUIEnvironment *environment,
  137. ISimpleTextureSource *tsrc);
  138. void place(const core::rect<s32> &dest_rect);
  139. inline s32 getHeight() { return m_height; };
  140. void draw(const core::rect<s32> &dest_rect,
  141. const core::position2d<s32> &dest_offset);
  142. ParsedText::Element *getElementAt(core::position2d<s32> pos);
  143. ParsedText::Tag *m_hovertag;
  144. protected:
  145. struct RectWithMargin
  146. {
  147. core::rect<s32> rect;
  148. s32 margin;
  149. };
  150. ParsedText m_text;
  151. Client *m_client;
  152. gui::IGUIEnvironment *m_environment;
  153. s32 m_height;
  154. s32 m_voffset;
  155. std::vector<RectWithMargin> m_floating;
  156. };
  157. class GUIHyperText : public gui::IGUIElement
  158. {
  159. public:
  160. //! constructor
  161. GUIHyperText(const wchar_t *text, gui::IGUIEnvironment *environment,
  162. gui::IGUIElement *parent, s32 id,
  163. const core::rect<s32> &rectangle, Client *client,
  164. ISimpleTextureSource *tsrc);
  165. //! destructor
  166. virtual ~GUIHyperText();
  167. //! draws the element and its children
  168. virtual void draw();
  169. core::dimension2du getTextDimension();
  170. bool OnEvent(const SEvent &event);
  171. protected:
  172. // GUI members
  173. Client *m_client;
  174. GUIScrollBar *m_vscrollbar;
  175. TextDrawer m_drawer;
  176. // Positioning
  177. u32 m_scrollbar_width;
  178. core::rect<s32> m_display_text_rect;
  179. core::position2d<s32> m_text_scrollpos;
  180. ParsedText::Element *getElementAt(s32 X, s32 Y);
  181. void checkHover(s32 X, s32 Y);
  182. };