guiHyperText.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 <vector>
  18. #include <list>
  19. #include <unordered_map>
  20. #include <string>
  21. #include "irrlichttypes_extrabloated.h"
  22. using namespace irr;
  23. class ISimpleTextureSource;
  24. class Client;
  25. class GUIScrollBar;
  26. class ParsedText
  27. {
  28. public:
  29. ParsedText(const wchar_t *text);
  30. ~ParsedText();
  31. enum ElementType
  32. {
  33. ELEMENT_TEXT,
  34. ELEMENT_SEPARATOR,
  35. ELEMENT_IMAGE,
  36. ELEMENT_ITEM
  37. };
  38. enum BackgroundType
  39. {
  40. BACKGROUND_NONE,
  41. BACKGROUND_COLOR
  42. };
  43. enum FloatType
  44. {
  45. FLOAT_NONE,
  46. FLOAT_RIGHT,
  47. FLOAT_LEFT
  48. };
  49. enum HalignType
  50. {
  51. HALIGN_CENTER,
  52. HALIGN_LEFT,
  53. HALIGN_RIGHT,
  54. HALIGN_JUSTIFY
  55. };
  56. enum ValignType
  57. {
  58. VALIGN_MIDDLE,
  59. VALIGN_TOP,
  60. VALIGN_BOTTOM
  61. };
  62. typedef std::unordered_map<std::string, std::string> StyleList;
  63. typedef std::unordered_map<std::string, std::string> AttrsList;
  64. struct Tag
  65. {
  66. std::string name;
  67. AttrsList attrs;
  68. StyleList style;
  69. };
  70. struct Element
  71. {
  72. std::list<Tag *> tags;
  73. ElementType type;
  74. core::stringw text = "";
  75. core::dimension2d<u32> dim;
  76. core::position2d<s32> pos;
  77. s32 drawwidth;
  78. FloatType floating = FLOAT_NONE;
  79. ValignType valign;
  80. gui::IGUIFont *font;
  81. irr::video::SColor color;
  82. irr::video::SColor hovercolor;
  83. bool underline;
  84. s32 baseline = 0;
  85. // img & item specific attributes
  86. std::string name;
  87. v3s16 angle{0, 0, 0};
  88. v3s16 rotation{0, 0, 0};
  89. s32 margin = 10;
  90. void setStyle(StyleList &style);
  91. };
  92. struct Paragraph
  93. {
  94. std::vector<Element> elements;
  95. HalignType halign;
  96. s32 margin = 10;
  97. void setStyle(StyleList &style);
  98. };
  99. std::vector<Paragraph> m_paragraphs;
  100. // Element style
  101. s32 margin = 3;
  102. ValignType valign = VALIGN_TOP;
  103. BackgroundType background_type = BACKGROUND_NONE;
  104. irr::video::SColor background_color;
  105. Tag m_root_tag;
  106. protected:
  107. typedef enum { ER_NONE, ER_TAG, ER_NEWLINE } EndReason;
  108. // Parser functions
  109. void enterElement(ElementType type);
  110. void endElement();
  111. void enterParagraph();
  112. void endParagraph(EndReason reason);
  113. void pushChar(wchar_t c);
  114. ParsedText::Tag *newTag(const std::string &name, const AttrsList &attrs);
  115. ParsedText::Tag *openTag(const std::string &name, const AttrsList &attrs);
  116. bool closeTag(const std::string &name);
  117. void parseGenericStyleAttr(const std::string &name, const std::string &value,
  118. StyleList &style);
  119. void parseStyles(const AttrsList &attrs, StyleList &style);
  120. void globalTag(const ParsedText::AttrsList &attrs);
  121. u32 parseTag(const wchar_t *text, u32 cursor);
  122. void parse(const wchar_t *text);
  123. std::unordered_map<std::string, StyleList> m_elementtags;
  124. std::unordered_map<std::string, StyleList> m_paragraphtags;
  125. std::vector<Tag *> m_not_root_tags;
  126. std::list<Tag *> m_active_tags;
  127. // Current values
  128. StyleList m_style;
  129. Element *m_element;
  130. Paragraph *m_paragraph;
  131. bool m_empty_paragraph;
  132. EndReason m_end_paragraph_reason;
  133. };
  134. class TextDrawer
  135. {
  136. public:
  137. TextDrawer(const wchar_t *text, Client *client, gui::IGUIEnvironment *environment,
  138. ISimpleTextureSource *tsrc);
  139. void place(const core::rect<s32> &dest_rect);
  140. inline s32 getHeight() { return m_height; };
  141. void draw(const core::rect<s32> &clip_rect,
  142. const core::position2d<s32> &dest_offset);
  143. ParsedText::Element *getElementAt(core::position2d<s32> pos);
  144. ParsedText::Tag *m_hovertag;
  145. protected:
  146. struct RectWithMargin
  147. {
  148. core::rect<s32> rect;
  149. s32 margin;
  150. };
  151. ParsedText m_text;
  152. Client *m_client; ///< null in the mainmenu
  153. ISimpleTextureSource *m_tsrc;
  154. gui::IGUIEnvironment *m_guienv;
  155. s32 m_height;
  156. s32 m_voffset;
  157. std::vector<RectWithMargin> m_floating;
  158. };
  159. class GUIHyperText : public gui::IGUIElement
  160. {
  161. public:
  162. //! constructor
  163. GUIHyperText(const wchar_t *text, gui::IGUIEnvironment *environment,
  164. gui::IGUIElement *parent, s32 id,
  165. const core::rect<s32> &rectangle, Client *client,
  166. ISimpleTextureSource *tsrc);
  167. //! destructor
  168. virtual ~GUIHyperText();
  169. //! draws the element and its children
  170. virtual void draw();
  171. core::dimension2du getTextDimension();
  172. bool OnEvent(const SEvent &event);
  173. protected:
  174. // GUI members
  175. ISimpleTextureSource *m_tsrc;
  176. GUIScrollBar *m_vscrollbar;
  177. TextDrawer m_drawer;
  178. // Positioning
  179. u32 m_scrollbar_width;
  180. core::rect<s32> m_display_text_rect;
  181. core::position2d<s32> m_text_scrollpos;
  182. ParsedText::Element *getElementAt(s32 X, s32 Y);
  183. void checkHover(s32 X, s32 Y);
  184. };