cleanup
[waspsaliva.git] / src / gui / guiHyperText.h
blob5b936262e88ab4aa256836fff75a31c4ef05518d
1 /*
2 Minetest
3 Copyright (C) 2019 EvicenceBKidscode / Pierre-Yves Rollo <dev@pyrollo.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
22 #include "config.h" // for USE_FREETYPE
24 using namespace irr;
26 class ISimpleTextureSource;
27 class Client;
29 #if USE_FREETYPE
30 #include "irrlicht_changes/CGUITTFont.h"
31 #endif
33 class ParsedText
35 public:
36 ParsedText(const wchar_t *text);
37 ~ParsedText();
39 enum ElementType
41 ELEMENT_TEXT,
42 ELEMENT_SEPARATOR,
43 ELEMENT_IMAGE,
44 ELEMENT_ITEM
47 enum BackgroundType
49 BACKGROUND_NONE,
50 BACKGROUND_COLOR
53 enum FloatType
55 FLOAT_NONE,
56 FLOAT_RIGHT,
57 FLOAT_LEFT
60 enum HalignType
62 HALIGN_CENTER,
63 HALIGN_LEFT,
64 HALIGN_RIGHT,
65 HALIGN_JUSTIFY
68 enum ValignType
70 VALIGN_MIDDLE,
71 VALIGN_TOP,
72 VALIGN_BOTTOM
75 typedef std::unordered_map<std::string, std::string> StyleList;
76 typedef std::unordered_map<std::string, std::string> AttrsList;
78 struct Tag
80 std::string name;
81 AttrsList attrs;
82 StyleList style;
85 struct Element
87 std::list<Tag *> tags;
88 ElementType type;
89 core::stringw text = "";
91 core::dimension2d<u32> dim;
92 core::position2d<s32> pos;
93 s32 drawwidth;
95 FloatType floating = FLOAT_NONE;
97 ValignType valign;
99 gui::IGUIFont *font;
101 irr::video::SColor color;
102 irr::video::SColor hovercolor;
103 bool underline;
105 s32 baseline = 0;
107 // img & item specific attributes
108 std::string name;
109 v3s16 angle{0, 0, 0};
110 v3s16 rotation{0, 0, 0};
112 s32 margin = 10;
114 void setStyle(StyleList &style);
117 struct Paragraph
119 std::vector<Element> elements;
120 HalignType halign;
121 s32 margin = 10;
123 void setStyle(StyleList &style);
126 std::vector<Paragraph> m_paragraphs;
128 // Element style
129 s32 margin = 3;
130 ValignType valign = VALIGN_TOP;
131 BackgroundType background_type = BACKGROUND_NONE;
132 irr::video::SColor background_color;
134 Tag m_root_tag;
136 protected:
137 typedef enum { ER_NONE, ER_TAG, ER_NEWLINE } EndReason;
139 // Parser functions
140 void enterElement(ElementType type);
141 void endElement();
142 void enterParagraph();
143 void endParagraph(EndReason reason);
144 void pushChar(wchar_t c);
145 ParsedText::Tag *newTag(const std::string &name, const AttrsList &attrs);
146 ParsedText::Tag *openTag(const std::string &name, const AttrsList &attrs);
147 bool closeTag(const std::string &name);
148 void parseGenericStyleAttr(const std::string &name, const std::string &value,
149 StyleList &style);
150 void parseStyles(const AttrsList &attrs, StyleList &style);
151 void globalTag(const ParsedText::AttrsList &attrs);
152 u32 parseTag(const wchar_t *text, u32 cursor);
153 void parse(const wchar_t *text);
155 std::unordered_map<std::string, StyleList> m_elementtags;
156 std::unordered_map<std::string, StyleList> m_paragraphtags;
158 std::vector<Tag *> m_not_root_tags;
159 std::list<Tag *> m_active_tags;
161 // Current values
162 StyleList m_style;
163 Element *m_element;
164 Paragraph *m_paragraph;
165 bool m_empty_paragraph;
166 EndReason m_end_paragraph_reason;
169 class TextDrawer
171 public:
172 TextDrawer(const wchar_t *text, Client *client, gui::IGUIEnvironment *environment,
173 ISimpleTextureSource *tsrc);
175 void place(const core::rect<s32> &dest_rect);
176 inline s32 getHeight() { return m_height; };
177 void draw(const core::rect<s32> &clip_rect,
178 const core::position2d<s32> &dest_offset);
179 ParsedText::Element *getElementAt(core::position2d<s32> pos);
180 ParsedText::Tag *m_hovertag;
182 protected:
183 struct RectWithMargin
185 core::rect<s32> rect;
186 s32 margin;
189 ParsedText m_text;
190 Client *m_client;
191 gui::IGUIEnvironment *m_environment;
192 s32 m_height;
193 s32 m_voffset;
194 std::vector<RectWithMargin> m_floating;
197 class GUIHyperText : public gui::IGUIElement
199 public:
200 //! constructor
201 GUIHyperText(const wchar_t *text, gui::IGUIEnvironment *environment,
202 gui::IGUIElement *parent, s32 id,
203 const core::rect<s32> &rectangle, Client *client,
204 ISimpleTextureSource *tsrc);
206 //! destructor
207 virtual ~GUIHyperText();
209 //! draws the element and its children
210 virtual void draw();
212 core::dimension2du getTextDimension();
214 bool OnEvent(const SEvent &event);
216 protected:
217 // GUI members
218 Client *m_client;
219 GUIScrollBar *m_vscrollbar;
220 TextDrawer m_drawer;
222 // Positioning
223 u32 m_scrollbar_width;
224 core::rect<s32> m_display_text_rect;
225 core::position2d<s32> m_text_scrollpos;
227 ParsedText::Element *getElementAt(s32 X, s32 Y);
228 void checkHover(s32 X, s32 Y);