Minetest 5.9.0-dev
 
Loading...
Searching...
No Matches
guiHyperText.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2019 EvicenceBKidscode / Pierre-Yves Rollo <dev@pyrollo.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#pragma once
21
22#include <vector>
23#include <list>
24#include <unordered_map>
25#include <string>
27
28using namespace irr;
29
31class Client;
32class GUIScrollBar;
33
35{
36public:
37 ParsedText(const wchar_t *text);
39
41 {
46 };
47
49 {
52 };
53
55 {
59 };
60
62 {
67 };
68
70 {
74 };
75
76 typedef std::unordered_map<std::string, std::string> StyleList;
77 typedef std::unordered_map<std::string, std::string> AttrsList;
78
79 struct Tag
80 {
81 std::string name;
84 };
85
86 struct Element
87 {
88 std::list<Tag *> tags;
90 core::stringw text = "";
91
92 core::dimension2d<u32> dim;
93 core::position2d<s32> pos;
95
97
99
100 gui::IGUIFont *font;
101
102 irr::video::SColor color;
103 irr::video::SColor hovercolor;
105
106 s32 baseline = 0;
107
108 // img & item specific attributes
109 std::string name;
110 v3s16 angle{0, 0, 0};
111 v3s16 rotation{0, 0, 0};
112
113 s32 margin = 10;
114
115 void setStyle(StyleList &style);
116 };
117
119 {
120 std::vector<Element> elements;
122 s32 margin = 10;
123
124 void setStyle(StyleList &style);
125 };
126
127 std::vector<Paragraph> m_paragraphs;
128
129 // Element style
130 s32 margin = 3;
133 irr::video::SColor background_color;
134
136
137protected:
138 typedef enum { ER_NONE, ER_TAG, ER_NEWLINE } EndReason;
139
140 // Parser functions
141 void enterElement(ElementType type);
142 void endElement();
143 void enterParagraph();
144 void endParagraph(EndReason reason);
145 void pushChar(wchar_t c);
146 ParsedText::Tag *newTag(const std::string &name, const AttrsList &attrs);
147 ParsedText::Tag *openTag(const std::string &name, const AttrsList &attrs);
148 bool closeTag(const std::string &name);
149 void parseGenericStyleAttr(const std::string &name, const std::string &value,
150 StyleList &style);
151 void parseStyles(const AttrsList &attrs, StyleList &style);
152 void globalTag(const ParsedText::AttrsList &attrs);
153 u32 parseTag(const wchar_t *text, u32 cursor);
154 void parse(const wchar_t *text);
155
156 std::unordered_map<std::string, StyleList> m_elementtags;
157 std::unordered_map<std::string, StyleList> m_paragraphtags;
158
159 std::vector<Tag *> m_not_root_tags;
160 std::list<Tag *> m_active_tags;
161
162 // Current values
168};
169
171{
172public:
173 TextDrawer(const wchar_t *text, Client *client, gui::IGUIEnvironment *environment,
175
176 void place(const core::rect<s32> &dest_rect);
177 inline s32 getHeight() { return m_height; };
178 void draw(const core::rect<s32> &clip_rect,
179 const core::position2d<s32> &dest_offset);
180 ParsedText::Element *getElementAt(core::position2d<s32> pos);
182
183protected:
185 {
186 core::rect<s32> rect;
188 };
189
193 gui::IGUIEnvironment *m_guienv;
196 std::vector<RectWithMargin> m_floating;
197};
198
199class GUIHyperText : public gui::IGUIElement
200{
201public:
203 GUIHyperText(const wchar_t *text, gui::IGUIEnvironment *environment,
204 gui::IGUIElement *parent, s32 id,
205 const core::rect<s32> &rectangle, Client *client,
207
209 virtual ~GUIHyperText();
210
212 virtual void draw();
213
214 core::dimension2du getTextDimension();
215
216 bool OnEvent(const SEvent &event);
217
218protected:
219 // GUI members
223
224 // Positioning
226 core::rect<s32> m_display_text_rect;
227 core::position2d<s32> m_text_scrollpos;
228
229 ParsedText::Element *getElementAt(s32 X, s32 Y);
230 void checkHover(s32 X, s32 Y);
231};
Definition: client.h:119
Definition: guiHyperText.h:200
ISimpleTextureSource * m_tsrc
Definition: guiHyperText.h:220
core::position2d< s32 > m_text_scrollpos
Definition: guiHyperText.h:227
virtual void draw()
draws the element and its children
Definition: guiHyperText.cpp:1134
u32 m_scrollbar_width
Definition: guiHyperText.h:225
void checkHover(s32 X, s32 Y)
Definition: guiHyperText.cpp:1039
core::dimension2du getTextDimension()
ParsedText::Element * getElementAt(s32 X, s32 Y)
Definition: guiHyperText.cpp:1031
bool OnEvent(const SEvent &event)
Definition: guiHyperText.cpp:1062
GUIScrollBar * m_vscrollbar
Definition: guiHyperText.h:221
core::rect< s32 > m_display_text_rect
Definition: guiHyperText.h:226
virtual ~GUIHyperText()
destructor
Definition: guiHyperText.cpp:1025
TextDrawer m_drawer
Definition: guiHyperText.h:222
Definition: guiScrollBar.h:24
Definition: texturesource.h:34
Definition: guiHyperText.h:35
void enterParagraph()
Definition: guiHyperText.cpp:264
void enterElement(ElementType type)
Definition: guiHyperText.cpp:274
void endElement()
Definition: guiHyperText.cpp:241
EndReason
Definition: guiHyperText.h:138
@ ER_NONE
Definition: guiHyperText.h:138
@ ER_TAG
Definition: guiHyperText.h:138
@ ER_NEWLINE
Definition: guiHyperText.h:138
std::unordered_map< std::string, std::string > AttrsList
Definition: guiHyperText.h:77
bool closeTag(const std::string &name)
Definition: guiHyperText.cpp:319
ParsedText::Tag * openTag(const std::string &name, const AttrsList &attrs)
Definition: guiHyperText.cpp:312
std::vector< Tag * > m_not_root_tags
Definition: guiHyperText.h:159
void globalTag(const ParsedText::AttrsList &attrs)
Definition: guiHyperText.cpp:359
BackgroundType
Definition: guiHyperText.h:49
@ BACKGROUND_NONE
Definition: guiHyperText.h:50
@ BACKGROUND_COLOR
Definition: guiHyperText.h:51
void endParagraph(EndReason reason)
Definition: guiHyperText.cpp:246
std::unordered_map< std::string, std::string > StyleList
Definition: guiHyperText.h:76
ValignType
Definition: guiHyperText.h:70
@ VALIGN_MIDDLE
Definition: guiHyperText.h:71
@ VALIGN_BOTTOM
Definition: guiHyperText.h:73
@ VALIGN_TOP
Definition: guiHyperText.h:72
void parse(const wchar_t *text)
Definition: guiHyperText.cpp:183
Paragraph * m_paragraph
Definition: guiHyperText.h:165
u32 parseTag(const wchar_t *text, u32 cursor)
Definition: guiHyperText.cpp:399
StyleList m_style
Definition: guiHyperText.h:163
irr::video::SColor background_color
Definition: guiHyperText.h:133
~ParsedText()
Definition: guiHyperText.cpp:177
Tag m_root_tag
Definition: guiHyperText.h:135
void parseGenericStyleAttr(const std::string &name, const std::string &value, StyleList &style)
Definition: guiHyperText.cpp:331
void pushChar(wchar_t c)
Definition: guiHyperText.cpp:287
FloatType
Definition: guiHyperText.h:55
@ FLOAT_NONE
Definition: guiHyperText.h:56
@ FLOAT_LEFT
Definition: guiHyperText.h:58
@ FLOAT_RIGHT
Definition: guiHyperText.h:57
std::list< Tag * > m_active_tags
Definition: guiHyperText.h:160
EndReason m_end_paragraph_reason
Definition: guiHyperText.h:167
HalignType
Definition: guiHyperText.h:62
@ HALIGN_RIGHT
Definition: guiHyperText.h:65
@ HALIGN_LEFT
Definition: guiHyperText.h:64
@ HALIGN_JUSTIFY
Definition: guiHyperText.h:66
@ HALIGN_CENTER
Definition: guiHyperText.h:63
ValignType valign
Definition: guiHyperText.h:131
std::unordered_map< std::string, StyleList > m_paragraphtags
Definition: guiHyperText.h:157
void parseStyles(const AttrsList &attrs, StyleList &style)
Definition: guiHyperText.cpp:353
std::unordered_map< std::string, StyleList > m_elementtags
Definition: guiHyperText.h:156
s32 margin
Definition: guiHyperText.h:130
ElementType
Definition: guiHyperText.h:41
@ ELEMENT_SEPARATOR
Definition: guiHyperText.h:43
@ ELEMENT_TEXT
Definition: guiHyperText.h:42
@ ELEMENT_IMAGE
Definition: guiHyperText.h:44
@ ELEMENT_ITEM
Definition: guiHyperText.h:45
bool m_empty_paragraph
Definition: guiHyperText.h:166
Element * m_element
Definition: guiHyperText.h:164
std::vector< Paragraph > m_paragraphs
Definition: guiHyperText.h:127
BackgroundType background_type
Definition: guiHyperText.h:132
ParsedText::Tag * newTag(const std::string &name, const AttrsList &attrs)
Definition: guiHyperText.cpp:302
Definition: guiHyperText.h:171
s32 getHeight()
Definition: guiHyperText.h:177
std::vector< RectWithMargin > m_floating
Definition: guiHyperText.h:196
void draw(const core::rect< s32 > &clip_rect, const core::position2d< s32 > &dest_offset)
Definition: guiHyperText.cpp:920
ParsedText::Element * getElementAt(core::position2d< s32 > pos)
Definition: guiHyperText.cpp:664
ParsedText::Tag * m_hovertag
Definition: guiHyperText.h:181
Client * m_client
null in the mainmenu
Definition: guiHyperText.h:191
s32 m_voffset
Definition: guiHyperText.h:195
void place(const core::rect< s32 > &dest_rect)
Definition: guiHyperText.cpp:685
ISimpleTextureSource * m_tsrc
Definition: guiHyperText.h:192
s32 m_height
Definition: guiHyperText.h:194
gui::IGUIEnvironment * m_guienv
Definition: guiHyperText.h:193
ParsedText m_text
Definition: guiHyperText.h:190
core::vector3d< s16 > v3s16
Definition: irr_v3d.h:28
Definition: activeobjectmgr.cpp:26
Definition: shader.h:61
Definition: guiHyperText.h:87
s32 baseline
Definition: guiHyperText.h:106
irr::video::SColor hovercolor
Definition: guiHyperText.h:103
std::string name
Definition: guiHyperText.h:109
s32 margin
Definition: guiHyperText.h:113
bool underline
Definition: guiHyperText.h:104
v3s16 rotation
Definition: guiHyperText.h:111
v3s16 angle
Definition: guiHyperText.h:110
core::position2d< s32 > pos
Definition: guiHyperText.h:93
void setStyle(StyleList &style)
Definition: guiHyperText.cpp:55
std::list< Tag * > tags
Definition: guiHyperText.h:88
irr::video::SColor color
Definition: guiHyperText.h:102
s32 drawwidth
Definition: guiHyperText.h:94
gui::IGUIFont * font
Definition: guiHyperText.h:100
ElementType type
Definition: guiHyperText.h:89
ValignType valign
Definition: guiHyperText.h:98
FloatType floating
Definition: guiHyperText.h:96
core::dimension2d< u32 > dim
Definition: guiHyperText.h:92
core::stringw text
Definition: guiHyperText.h:90
Definition: guiHyperText.h:119
void setStyle(StyleList &style)
Definition: guiHyperText.cpp:90
std::vector< Element > elements
Definition: guiHyperText.h:120
HalignType halign
Definition: guiHyperText.h:121
s32 margin
Definition: guiHyperText.h:122
Definition: guiHyperText.h:80
AttrsList attrs
Definition: guiHyperText.h:82
std::string name
Definition: guiHyperText.h:81
StyleList style
Definition: guiHyperText.h:83
Definition: guiHyperText.h:185
s32 margin
Definition: guiHyperText.h:187
core::rect< s32 > rect
Definition: guiHyperText.h:186