Luanti 5.17.0-dev
Loading...
Searching...
No Matches
guiHyperText.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2019 EvicenceBKidscode / Pierre-Yves Rollo <dev@pyrollo.com>
4
5#pragma once
6
7#include <vector>
8#include <list>
9#include <unordered_map>
10#include <string>
11#include <IGUIElement.h>
12#include <IGUIEnvironment.h>
13#include "irr_v3d.h"
14#include "StyleSpec.h"
15
17class Client;
18class GUIScrollBar;
19
21{
22public:
23 ParsedText(const wchar_t *text, video::SColor default_color);
25
33
39
46
54
61
62 typedef std::unordered_map<std::string, std::string> StyleList;
63 typedef std::unordered_map<std::string, std::string> AttrsList;
64
65 struct Tag
66 {
67 std::string name;
70 };
71
72 struct Element
73 {
74 std::list<Tag *> tags;
76 core::stringw text = "";
77
78 core::dimension2d<u32> dim;
79 core::position2d<s32> pos;
81
83
85
86 gui::IGUIFont *font;
87
88 video::SColor color;
89 video::SColor hovercolor;
91
92 s32 baseline = 0;
93
94 // img & item specific attributes
95 std::string name;
96 v3s16 angle{0, 0, 0};
97 v3s16 rotation{0, 0, 0};
98
99 s32 margin = 10;
100
101 void setStyle(StyleList &style);
102 };
103
105 {
106 std::vector<Element> elements;
108 s32 margin = 10;
109
110 void setStyle(StyleList &style);
111 };
112
113 std::vector<Paragraph> m_paragraphs;
114
115 // Element style
116 s32 margin = 3;
119 video::SColor background_color;
120 video::ITexture *background_image = nullptr;
121 core::rect<s32> background_middle;
122 bool border = false;
123
125
126protected:
127 typedef enum { ER_NONE, ER_TAG, ER_NEWLINE } EndReason;
128
129 // Parser functions
130 void enterElement(ElementType type);
131 void endElement();
132 void enterParagraph();
133 void endParagraph(EndReason reason);
134 void pushChar(wchar_t c);
135 ParsedText::Tag *newTag(const std::string &name, const AttrsList &attrs);
136 ParsedText::Tag *openTag(const std::string &name, const AttrsList &attrs);
137 bool closeTag(const std::string &name);
138 void parseGenericStyleAttr(const std::string &name, const std::string &value,
139 StyleList &style);
140 void parseStyles(const AttrsList &attrs, StyleList &style);
141 void globalTag(const ParsedText::AttrsList &attrs);
142 u32 parseTag(const wchar_t *text, u32 cursor);
143 void parse(const wchar_t *text);
144
145 std::unordered_map<std::string, StyleList> m_elementtags;
146 std::unordered_map<std::string, StyleList> m_paragraphtags;
147
148 std::vector<Tag *> m_not_root_tags;
149 std::list<Tag *> m_active_tags;
150
151 // Current values
157};
158
160{
161public:
162 TextDrawer(const wchar_t *text, Client *client, gui::IGUIEnvironment *environment,
164 video::SColor default_background_color,
165 video::SColor default_color);
166
167 void place(const core::rect<s32> &dest_rect);
168 inline s32 getHeight() { return m_height; };
169 void draw(const core::rect<s32> &clip_rect,
170 const core::position2d<s32> &dest_offset);
171 void drawBackgroundImage(video::IVideoDriver *driver, const core::rect<s32> &clip_rect);
172 void applyStyleSpecToText(const StyleSpec &style);
173 ParsedText::Element *getElementAt(core::position2d<s32> pos);
175
176protected:
178 {
179 core::rect<s32> rect;
181 };
182
186 gui::IGUIEnvironment *m_guienv;
187 s32 m_height = 0;
189 std::vector<RectWithMargin> m_floating;
191};
192
193class GUIHyperText : public gui::IGUIElement
194{
195public:
197 GUIHyperText(const wchar_t *text, gui::IGUIEnvironment *environment,
198 gui::IGUIElement *parent, s32 id,
199 const core::rect<s32> &rectangle, Client *client,
201 video::SColor default_background_color,
202 video::SColor default_color,
203 bool is_hypertip = false);
204
206 virtual ~GUIHyperText();
207
209 virtual void draw();
210
212 s32 getTextHeight() { return m_drawer.getHeight(); }
213
214 bool OnEvent(const SEvent &event);
215
216 void setStyles(const std::array<StyleSpec, StyleSpec::NUM_STATES> &styles);
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
232 // Counter variable to be increased every iteration of draw(),
233 // if it reached a certain number, drawing is allowed.
235
237 video::SColor m_default_color;
238};
Definition client.h:107
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:1212
u32 m_scrollbar_width
Definition guiHyperText.h:225
void checkHover(s32 X, s32 Y)
Definition guiHyperText.cpp:1108
video::SColor m_default_background_color
Definition guiHyperText.h:236
ParsedText::Element * getElementAt(s32 X, s32 Y)
Definition guiHyperText.cpp:1100
bool OnEvent(const SEvent &event)
Definition guiHyperText.cpp:1140
GUIScrollBar * m_vscrollbar
Definition guiHyperText.h:221
video::SColor m_default_color
Definition guiHyperText.h:237
s32 getTextHeight()
Returns the height of the text in pixels when it is drawn.
Definition guiHyperText.h:212
core::rect< s32 > m_display_text_rect
Definition guiHyperText.h:226
GUIHyperText(const wchar_t *text, gui::IGUIEnvironment *environment, gui::IGUIElement *parent, s32 id, const core::rect< s32 > &rectangle, Client *client, ISimpleTextureSource *tsrc, video::SColor default_background_color, video::SColor default_color, bool is_hypertip=false)
constructor
Definition guiHyperText.cpp:1064
virtual ~GUIHyperText()
destructor
Definition guiHyperText.cpp:1094
void setStyles(const std::array< StyleSpec, StyleSpec::NUM_STATES > &styles)
Definition guiHyperText.cpp:1131
TextDrawer m_drawer
Definition guiHyperText.h:222
int m_draw_state
Definition guiHyperText.h:234
Definition guiScrollBar.h:16
Definition texturesource.h:34
Definition guiHyperText.h:21
void enterParagraph()
Definition guiHyperText.cpp:251
ParsedText(const wchar_t *text, video::SColor default_color)
Definition guiHyperText.cpp:89
void enterElement(ElementType type)
Definition guiHyperText.cpp:261
video::ITexture * background_image
Definition guiHyperText.h:120
bool border
Definition guiHyperText.h:122
void endElement()
Definition guiHyperText.cpp:228
EndReason
Definition guiHyperText.h:127
@ ER_NONE
Definition guiHyperText.h:127
@ ER_TAG
Definition guiHyperText.h:127
@ ER_NEWLINE
Definition guiHyperText.h:127
std::unordered_map< std::string, std::string > AttrsList
Definition guiHyperText.h:63
video::SColor background_color
Definition guiHyperText.h:119
bool closeTag(const std::string &name)
Definition guiHyperText.cpp:306
ParsedText::Tag * openTag(const std::string &name, const AttrsList &attrs)
Definition guiHyperText.cpp:299
std::vector< Tag * > m_not_root_tags
Definition guiHyperText.h:148
void globalTag(const ParsedText::AttrsList &attrs)
Definition guiHyperText.cpp:346
BackgroundType
Definition guiHyperText.h:35
@ BACKGROUND_NONE
Definition guiHyperText.h:36
@ BACKGROUND_COLOR
Definition guiHyperText.h:37
void endParagraph(EndReason reason)
Definition guiHyperText.cpp:233
std::unordered_map< std::string, std::string > StyleList
Definition guiHyperText.h:62
ValignType
Definition guiHyperText.h:56
@ VALIGN_MIDDLE
Definition guiHyperText.h:57
@ VALIGN_BOTTOM
Definition guiHyperText.h:59
@ VALIGN_TOP
Definition guiHyperText.h:58
void parse(const wchar_t *text)
Definition guiHyperText.cpp:170
Paragraph * m_paragraph
Definition guiHyperText.h:154
u32 parseTag(const wchar_t *text, u32 cursor)
Definition guiHyperText.cpp:386
StyleList m_style
Definition guiHyperText.h:152
~ParsedText()
Definition guiHyperText.cpp:164
core::rect< s32 > background_middle
Definition guiHyperText.h:121
Tag m_root_tag
Definition guiHyperText.h:124
void parseGenericStyleAttr(const std::string &name, const std::string &value, StyleList &style)
Definition guiHyperText.cpp:318
void pushChar(wchar_t c)
Definition guiHyperText.cpp:274
FloatType
Definition guiHyperText.h:41
@ FLOAT_NONE
Definition guiHyperText.h:42
@ FLOAT_LEFT
Definition guiHyperText.h:44
@ FLOAT_RIGHT
Definition guiHyperText.h:43
std::list< Tag * > m_active_tags
Definition guiHyperText.h:149
EndReason m_end_paragraph_reason
Definition guiHyperText.h:156
HalignType
Definition guiHyperText.h:48
@ HALIGN_RIGHT
Definition guiHyperText.h:51
@ HALIGN_LEFT
Definition guiHyperText.h:50
@ HALIGN_JUSTIFY
Definition guiHyperText.h:52
@ HALIGN_CENTER
Definition guiHyperText.h:49
ValignType valign
Definition guiHyperText.h:117
std::unordered_map< std::string, StyleList > m_paragraphtags
Definition guiHyperText.h:146
void parseStyles(const AttrsList &attrs, StyleList &style)
Definition guiHyperText.cpp:340
std::unordered_map< std::string, StyleList > m_elementtags
Definition guiHyperText.h:145
s32 margin
Definition guiHyperText.h:116
ElementType
Definition guiHyperText.h:27
@ ELEMENT_SEPARATOR
Definition guiHyperText.h:29
@ ELEMENT_TEXT
Definition guiHyperText.h:28
@ ELEMENT_IMAGE
Definition guiHyperText.h:30
@ ELEMENT_ITEM
Definition guiHyperText.h:31
bool m_empty_paragraph
Definition guiHyperText.h:155
Element * m_element
Definition guiHyperText.h:153
std::vector< Paragraph > m_paragraphs
Definition guiHyperText.h:113
BackgroundType background_type
Definition guiHyperText.h:118
ParsedText::Tag * newTag(const std::string &name, const AttrsList &attrs)
Definition guiHyperText.cpp:289
Definition StyleSpec.h:18
Definition guiHyperText.h:160
s32 getHeight()
Definition guiHyperText.h:168
void drawBackgroundImage(video::IVideoDriver *driver, const core::rect< s32 > &clip_rect)
Definition guiHyperText.cpp:939
std::vector< RectWithMargin > m_floating
Definition guiHyperText.h:189
void draw(const core::rect< s32 > &clip_rect, const core::position2d< s32 > &dest_offset)
Definition guiHyperText.cpp:958
ParsedText::Element * getElementAt(core::position2d< s32 > pos)
Definition guiHyperText.cpp:680
ParsedText::Tag * m_hovertag
Definition guiHyperText.h:174
TextDrawer(const wchar_t *text, Client *client, gui::IGUIEnvironment *environment, ISimpleTextureSource *tsrc, video::SColor default_background_color, video::SColor default_color)
Definition guiHyperText.cpp:621
Client * m_client
null in the mainmenu
Definition guiHyperText.h:184
s32 m_voffset
Definition guiHyperText.h:188
void place(const core::rect< s32 > &dest_rect)
Definition guiHyperText.cpp:701
void applyStyleSpecToText(const StyleSpec &style)
Definition guiHyperText.cpp:1046
ISimpleTextureSource * m_tsrc
Definition guiHyperText.h:185
s32 m_height
Definition guiHyperText.h:187
video::SColor m_default_background_color
Definition guiHyperText.h:190
gui::IGUIEnvironment * m_guienv
Definition guiHyperText.h:186
ParsedText m_text
Definition guiHyperText.h:183
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
Definition activeobjectmgr.cpp:11
Definition guiHyperText.h:73
video::SColor hovercolor
Definition guiHyperText.h:89
s32 baseline
Definition guiHyperText.h:92
std::string name
Definition guiHyperText.h:95
s32 margin
Definition guiHyperText.h:99
video::SColor color
Definition guiHyperText.h:88
bool underline
Definition guiHyperText.h:90
v3s16 rotation
Definition guiHyperText.h:97
v3s16 angle
Definition guiHyperText.h:96
core::position2d< s32 > pos
Definition guiHyperText.h:79
void setStyle(StyleList &style)
Definition guiHyperText.cpp:42
std::list< Tag * > tags
Definition guiHyperText.h:74
s32 drawwidth
Definition guiHyperText.h:80
gui::IGUIFont * font
Definition guiHyperText.h:86
ElementType type
Definition guiHyperText.h:75
ValignType valign
Definition guiHyperText.h:84
FloatType floating
Definition guiHyperText.h:82
core::dimension2d< u32 > dim
Definition guiHyperText.h:78
core::stringw text
Definition guiHyperText.h:76
Definition guiHyperText.h:105
void setStyle(StyleList &style)
Definition guiHyperText.cpp:77
std::vector< Element > elements
Definition guiHyperText.h:106
HalignType halign
Definition guiHyperText.h:107
s32 margin
Definition guiHyperText.h:108
Definition guiHyperText.h:66
AttrsList attrs
Definition guiHyperText.h:68
std::string name
Definition guiHyperText.h:67
StyleList style
Definition guiHyperText.h:69
Definition guiHyperText.h:178
s32 margin
Definition guiHyperText.h:180
core::rect< s32 > rect
Definition guiHyperText.h:179