Pyrogenesis  trunk
CGUIText.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_GUITEXT
19 #define INCLUDED_GUITEXT
20 
21 #include "gui/CGUISprite.h"
24 #include "maths/Rect.h"
25 #include "maths/Size2D.h"
26 #include "maths/Vector2D.h"
27 #include "ps/CStrIntern.h"
28 
29 #include <array>
30 #include <list>
31 #include <vector>
32 
33 class CCanvas2D;
34 class CGUI;
35 class CGUIString;
36 class IGUIObject;
37 struct SGenerateTextImage;
38 
39 using SGenerateTextImages = std::array<std::vector<SGenerateTextImage>, 2>;
40 
41 /**
42  * An CGUIText object is a parsed string, divided into
43  * text-rendering components. Each component, being a
44  * call to the Renderer. For instance, if you by tags
45  * change the color, then the GUI will have to make
46  * individual calls saying it want that color on the
47  * text.
48  *
49  * For instance:
50  * "Hello [b]there[/b] bunny!"
51  *
52  * That without word-wrapping would mean 3 components.
53  * i.e. 3 calls to CRenderer. One drawing "Hello",
54  * one drawing "there" in bold, and one drawing "bunny!".
55  */
56 class CGUIText
57 {
58 public:
59  /**
60  * A sprite call to the CRenderer
61  */
62  struct SSpriteCall
63  {
64  // The CGUISpriteInstance makes this uncopyable to avoid invalidating its draw cache
67 
69 
70  /**
71  * Size and position of sprite
72  */
74 
75  /**
76  * Sprite from global GUI sprite database.
77  */
79  };
80 
81  /**
82  * A text call to the CRenderer
83  */
84  struct STextCall
85  {
88 
90  m_UseCustomColor(false),
91  m_Bold(false), m_Italic(false), m_Underlined(false),
92  m_pSpriteCall(nullptr) {}
93 
94  /**
95  * Position
96  */
98 
99  /**
100  * Size
101  */
103 
104  /**
105  * The string that is suppose to be rendered.
106  */
107  CStrW m_String;
108 
109  /**
110  * Use custom color? If true then m_Color is used,
111  * else the color inputted will be used.
112  */
114 
115  /**
116  * Color setup
117  */
119 
120  /**
121  * Font name
122  */
124 
125  /**
126  * Settings
127  */
128  bool m_Bold, m_Italic, m_Underlined;
129 
130  /**
131  * Tooltip text
132  */
133  CStrW m_Tooltip;
134 
135  /**
136  * *IF* an icon, then this is not nullptr.
137  */
138  std::list<SSpriteCall>::pointer m_pSpriteCall;
139  };
140 
141  // The SSpriteCall CGUISpriteInstance makes this uncopyable to avoid invalidating its draw cache.
142  // Also take advantage of exchanging the containers directly with move semantics.
144  MOVABLE(CGUIText);
145 
146  /**
147  * Generates empty text.
148  */
149  CGUIText() = default;
150 
151  /**
152  * Generate a CGUIText object from the inputted string.
153  * The function will break down the string and its
154  * tags to calculate exactly which rendering queries
155  * will be sent to the Renderer. Also, horizontal alignment
156  * is taken into acount in this method but NOT vertical alignment.
157  *
158  * @param string Text to generate CGUIText object from.
159  * @param font Default font, notice both Default color and default font
160  * can be changed by tags.
161  * @param width Width, 0 if no word-wrapping.
162  * @param bufferZone Space between text and edge, and space between text and images.
163  * @param align Horizontal alignment (left / center / right).
164  * @param pObject Optional parameter for error output. Used *only* if error parsing fails,
165  * and we need to be able to output which object the error occurred in to aid the user.
166  */
167  CGUIText(const CGUI& pGUI, const CGUIString& string, const CStrW& fontW, const float width, const float bufferZone, const EAlign align, const IGUIObject* pObject);
168 
169  /**
170  * Draw this CGUIText object
171  */
172  void Draw(CGUI& pGUI, CCanvas2D& canvas, const CGUIColor& DefaultColor, const CVector2D& pos, CRect clipping) const;
173 
174  const CSize2D& GetSize() const { return m_Size; }
175 
176  const std::list<SSpriteCall>& GetSpriteCalls() const { return m_SpriteCalls; }
177 
178  const std::vector<STextCall>& GetTextCalls() const { return m_TextCalls; }
179 
180  // Helper functions of the constructor
181  bool ProcessLine(
182  const CGUI& pGUI,
183  const CGUIString& string,
184  const CStrIntern& font,
185  const IGUIObject* pObject,
186  const SGenerateTextImages& images,
187  const EAlign align,
188  const float prelimLineHeight,
189  const float width,
190  const float bufferZone,
191  bool& firstLine,
192  float& y,
193  int& i,
194  int& from);
195 
196  void SetupSpriteCalls(
197  const CGUI& pGUI,
198  const std::array<std::vector<CStr>, 2>& feedbackImages,
199  const float y,
200  const float width,
201  const float bufferZone,
202  const int i,
203  const int posLastImage,
204  SGenerateTextImages& images);
205 
206  float GetLineOffset(
207  const EAlign align,
208  const float widthRangeFrom,
209  const float widthRangeTo,
210  const CSize2D& lineSize) const;
211 
212  void ComputeLineRange(
213  const SGenerateTextImages& images,
214  const float y,
215  const float width,
216  const float prelimLineHeight,
217  float& widthRangeFrom,
218  float& widthRangeTo) const;
219 
220  void ComputeLineSize(
221  const CGUI& pGUI,
222  const CGUIString& string,
223  const CStrIntern& font,
224  const bool firstLine,
225  const float width,
226  const float widthRangeFrom,
227  const float widthRangeTo,
228  const int i,
229  const int tempFrom,
230  CSize2D& lineSize) const;
231 
232  bool AssembleCalls(
233  const CGUI& pGUI,
234  const CGUIString& string,
235  const CStrIntern& font,
236  const IGUIObject* pObject,
237  const bool firstLine,
238  const float width,
239  const float widthRangeTo,
240  const float dx,
241  const float y,
242  const int tempFrom,
243  const int i,
244  int& from);
245 
246  /**
247  * List of TextCalls, for instance "Hello", "there!"
248  */
249  std::vector<STextCall> m_TextCalls;
250 
251  /**
252  * List of sprites, or "icons" that should be rendered
253  * along with the text.
254  */
255  std::list<SSpriteCall> m_SpriteCalls; // list for consistent mem addresses
256  // so that we can point to elements.
257  /**
258  * Width and height of the whole output, used when setting up
259  * scrollbars and such.
260  */
262 };
263 
265 {
266  // The image's starting location in Y
267  float m_YFrom;
268 
269  // The image's end location in Y
270  float m_YTo;
271 
272  // The image width in other words
274 
275  void SetupSpriteCall(
276  const bool left, CGUIText::SSpriteCall& spriteCall, const float width, const float y,
277  const CSize2D& size, const CStr& textureName, const float bufferZone);
278 };
279 
280 #endif // INCLUDED_GUITEXT
CStrW m_String
The string that is suppose to be rendered.
Definition: CGUIText.h:107
float GetLineOffset(const EAlign align, const float widthRangeFrom, const float widthRangeTo, const CSize2D &lineSize) const
Definition: CGUIText.cpp:322
CSize2D m_Size
Size.
Definition: CGUIText.h:102
void SetupSpriteCalls(const CGUI &pGUI, const std::array< std::vector< CStr >, 2 > &feedbackImages, const float y, const float width, const float bufferZone, const int i, const int posLastImage, SGenerateTextImages &images)
Definition: CGUIText.cpp:125
MOVABLE(SSpriteCall)
bool ProcessLine(const CGUI &pGUI, const CGUIString &string, const CStrIntern &font, const IGUIObject *pObject, const SGenerateTextImages &images, const EAlign align, const float prelimLineHeight, const float width, const float bufferZone, bool &firstLine, float &y, int &i, int &from)
Definition: CGUIText.cpp:233
std::array< std::vector< SGenerateTextImage >, 2 > SGenerateTextImages
Definition: CGUIText.h:39
Same as the CColor class, but this one can also parse colors predefined in the GUI page (such as "yel...
Definition: CGUIColor.h:29
CGUIColor m_Color
Color setup.
Definition: CGUIText.h:118
float m_YTo
Definition: CGUIText.h:270
CSize2D m_Size
Width and height of the whole output, used when setting up scrollbars and such.
Definition: CGUIText.h:261
bool AssembleCalls(const CGUI &pGUI, const CGUIString &string, const CStrIntern &font, const IGUIObject *pObject, const bool firstLine, const float width, const float widthRangeTo, const float dx, const float y, const int tempFrom, const int i, int &from)
Definition: CGUIText.cpp:345
CStrIntern m_Font
Font name.
Definition: CGUIText.h:123
GUI object such as a button or an input-box.
Definition: IGUIObject.h:59
CGUISpriteInstance m_Sprite
Sprite from global GUI sprite database.
Definition: CGUIText.h:78
bool m_UseCustomColor
Use custom color? If true then m_Color is used, else the color inputted will be used.
Definition: CGUIText.h:113
STextCall()
Definition: CGUIText.h:89
float m_Indentation
Definition: CGUIText.h:273
float m_YFrom
Definition: CGUIText.h:267
The main object that represents a whole GUI page.
Definition: CGUI.h:60
CVector2D m_Pos
Position.
Definition: CGUIText.h:97
Definition: Size2D.h:24
CStrW m_Tooltip
Tooltip text.
Definition: CGUIText.h:133
const CSize2D & GetSize() const
Definition: CGUIText.h:174
Interned 8-bit strings.
Definition: CStrIntern.h:37
std::list< SSpriteCall >::pointer m_pSpriteCall
IF an icon, then this is not nullptr.
Definition: CGUIText.h:138
std::vector< STextCall > m_TextCalls
List of TextCalls, for instance "Hello", "there!".
Definition: CGUIText.h:249
void ComputeLineSize(const CGUI &pGUI, const CGUIString &string, const CStrIntern &font, const bool firstLine, const float width, const float widthRangeFrom, const float widthRangeTo, const int i, const int tempFrom, CSize2D &lineSize) const
Definition: CGUIText.cpp:173
Definition: Canvas2D.h:35
CGUIText()=default
Generates empty text.
const std::list< SSpriteCall > & GetSpriteCalls() const
Definition: CGUIText.h:176
const std::vector< STextCall > & GetTextCalls() const
Definition: CGUIText.h:178
Definition: Vector2D.h:31
bool m_Underlined
Definition: CGUIText.h:128
void ComputeLineRange(const SGenerateTextImages &images, const float y, const float width, const float prelimLineHeight, float &widthRangeFrom, float &widthRangeTo) const
Definition: CGUIText.cpp:288
std::list< SSpriteCall > m_SpriteCalls
List of sprites, or "icons" that should be rendered along with the text.
Definition: CGUIText.h:255
A sprite call to the CRenderer.
Definition: CGUIText.h:62
Definition: CGUISprite.h:133
CRect m_Area
Size and position of sprite.
Definition: CGUIText.h:73
A text call to the CRenderer.
Definition: CGUIText.h:84
void Draw(CGUI &pGUI, CCanvas2D &canvas, const CGUIColor &DefaultColor, const CVector2D &pos, CRect clipping) const
Draw this CGUIText object.
Definition: CGUIText.cpp:451
An CGUIText object is a parsed string, divided into text-rendering components.
Definition: CGUIText.h:56
Definition: CGUIText.h:264
SSpriteCall()
Definition: CGUIText.h:68
String class, substitute for CStr, but that parses the tags and builds up a list of all text that wil...
Definition: CGUIString.h:41
EAlign
Definition: EAlign.h:21
NONCOPYABLE(SSpriteCall)
Rectangle class used for screen rectangles.
Definition: Rect.h:30