Pyrogenesis  trunk
GUItext.h
Go to the documentation of this file.
1 /* Copyright (C) 2015 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 /*
19  * GUI text, handles text stuff
20  *
21  * --Overview--
22  * Mainly contains struct SGUIText and friends.
23  * Actual text processing is made in CGUI::GenerateText()
24  *
25  * --More info--
26  * Check GUI.h
27  *
28  */
29 
30 #ifndef INCLUDED_GUITEXT
31 #define INCLUDED_GUITEXT
32 
33 #include <list>
34 
35 #include "CGUISprite.h"
36 #include "ps/CStrIntern.h"
37 
38 class CGUI;
39 
40 /**
41  * An SGUIText object is a parsed string, divided into
42  * text-rendering components. Each component, being a
43  * call to the Renderer. For instance, if you by tags
44  * change the color, then the GUI will have to make
45  * individual calls saying it want that color on the
46  * text.
47  *
48  * For instance:
49  * "Hello [b]there[/b] bunny!"
50  *
51  * That without word-wrapping would mean 3 components.
52  * i.e. 3 calls to CRenderer. One drawing "Hello",
53  * one drawing "there" in bold, and one drawing "bunny!".
54  */
55 struct SGUIText
56 {
57  /**
58  * A sprite call to the CRenderer
59  */
60  struct SSpriteCall
61  {
63 
64  /**
65  * Size and position of sprite
66  */
68 
69  /**
70  * Sprite from global GUI sprite database.
71  */
73 
74  int m_CellID;
75 
76  /**
77  * Tooltip text
78  */
79  CStrW m_Tooltip;
80 
81  /**
82  * Tooltip style
83  */
85  };
86 
87  /**
88  * A text call to the CRenderer
89  */
90  struct STextCall
91  {
93  m_UseCustomColor(false),
94  m_Bold(false), m_Italic(false), m_Underlined(false),
95  m_pSpriteCall(NULL) {}
96 
97  /**
98  * Position
99  */
101 
102  /**
103  * Size
104  */
106 
107  /**
108  * The string that is suppose to be rendered.
109  */
110  CStrW m_String;
111 
112  /**
113  * Use custom color? If true then m_Color is used,
114  * else the color inputted will be used.
115  */
117 
118  /**
119  * Color setup
120  */
122 
123  /**
124  * Font name
125  */
127 
128  /**
129  * Settings
130  */
131  bool m_Bold, m_Italic, m_Underlined;
132 
133  /**
134  * *IF* an icon, then this is not NULL.
135  */
136  std::list<SSpriteCall>::pointer m_pSpriteCall;
137  };
138 
139  /**
140  * List of TextCalls, for instance "Hello", "there!"
141  */
142  std::vector<STextCall> m_TextCalls;
143 
144  /**
145  * List of sprites, or "icons" that should be rendered
146  * along with the text.
147  */
148  std::list<SSpriteCall> m_SpriteCalls; // list for consistent mem addresses
149  // so that we can point to elements.
150 
151  /**
152  * Width and height of the whole output, used when setting up
153  * scrollbars and such.
154  */
156 };
157 
158 /**
159  * String class, substitute for CStr, but that parses
160  * the tags and builds up a list of all text that will
161  * be different when outputted.
162  *
163  * The difference between CGUIString and SGUIText is that
164  * CGUIString is a string-class that parses the tags
165  * when the value is set. The SGUIText is just a container
166  * which stores the positions and settings of all text-calls
167  * that will have to be made to the Renderer.
168  */
170 {
171 public:
172  /**
173  * A chunk of text that represents one call to the renderer.
174  * In other words, all text in one chunk, will be drawn
175  * exactly with the same settings.
176  */
177  struct TextChunk
178  {
179  /**
180  * A tag looks like this "Hello [b]there[/b] little"
181  */
182  struct Tag
183  {
184  /**
185  * Tag Type
186  */
187  enum TagType
188  {
197  TAG_INVALID
198  };
199 
201  {
202  std::wstring attrib;
203  std::wstring value;
204  };
205 
206  /**
207  * Set tag from string
208  *
209  * @param tagtype TagType by string, like 'img' for [img]
210  * @return True if m_TagType was set.
211  */
212  bool SetTagType(const CStrW& tagtype);
213  TagType GetTagType(const CStrW& tagtype) const;
214 
215 
216  /**
217  * In [b="Hello"][/b]
218  * m_TagType is TAG_B
219  */
221 
222  /**
223  * In [b="Hello"][/b]
224  * m_TagValue is 'Hello'
225  */
226  std::wstring m_TagValue;
227 
228  /**
229  * Some tags need an additional attributes
230  */
231  std::vector<TagAttribute> m_TagAttributes;
232  };
233 
234  /**
235  * m_From and m_To is the range of the string
236  */
237  int m_From, m_To;
238 
239  /**
240  * Tags that are present. [a][b]
241  */
242  std::vector<Tag> m_Tags;
243  };
244 
245  /**
246  * All data generated in GenerateTextCall()
247  */
248  struct SFeedback
249  {
250  // Constants
251  static const int Left = 0;
252  static const int Right = 1;
253 
254  /**
255  * Reset all member data.
256  */
257  void Reset();
258 
259  /**
260  * Image stacks, for left and right floating images.
261  */
262  std::vector<CStr> m_Images[2]; // left and right
263 
264  /**
265  * Text and Sprite Calls.
266  */
267  std::vector<SGUIText::STextCall> m_TextCalls;
268  std::list<SGUIText::SSpriteCall> m_SpriteCalls; // list for consistent mem addresses
269  // so that we can point to elements.
270 
271  /**
272  * Width and Height *feedback*
273  */
275 
276  /**
277  * If the word inputted was a new line.
278  */
279  bool m_NewLine;
280  };
281 
282  /**
283  * Set the value, the string will automatically
284  * be parsed when set.
285  */
286  void SetValue(const CStrW& str);
287 
288  /**
289  * Get String, with tags
290  */
291  const CStrW& GetOriginalString() const { return m_OriginalString; }
292 
293  /**
294  * Generate Text Call from specified range. The range
295  * must span only within ONE TextChunk though. Otherwise
296  * it can't be fit into a single Text Call
297  *
298  * Notice it won't make it complete, you will have to add
299  * X/Y values and such.
300  *
301  * @param pGUI Pointer to CGUI object making this call, for e.g. icon retrieval.
302  * @param Feedback contains all info that is generated.
303  * @param DefaultFont Default Font
304  * @param from From character n,
305  * @param to to character n.
306  * @param FirstLine Whether this is the first line of text, to calculate its height correctly
307  * @param pObject Only for Error outputting, optional! If NULL
308  * then no Errors will be reported! Useful when you need
309  * to make several GenerateTextCall in different phases,
310  * it avoids duplicates.
311  */
312  void GenerateTextCall(const CGUI* pGUI, SFeedback& Feedback, CStrIntern DefaultFont, const int& from, const int& to, const bool FirstLine, const IGUIObject* pObject = NULL) const;
313 
314  /**
315  * Words
316  */
317  std::vector<int> m_Words;
318 
319 private:
320  /**
321  * TextChunks
322  */
323  std::vector<TextChunk> m_TextChunks;
324 
325  /**
326  * The full raw string. Stripped of tags.
327  */
328  CStrW m_RawString;
329 
330  /**
331  * The original string value passed to SetValue.
332  */
334 };
335 
336 #endif // INCLUDED_GUITEXT
A chunk of text that represents one call to the renderer.
Definition: GUItext.h:177
std::wstring value
Definition: GUItext.h:203
int m_CellID
Definition: GUItext.h:74
Made to represent a screen size, should in philosophy be made of unsigned ints, but for the sake of c...
Definition: Shapes.h:207
std::list< SSpriteCall >::pointer m_pSpriteCall
IF an icon, then this is not NULL.
Definition: GUItext.h:136
std::wstring attrib
Definition: GUItext.h:202
Definition: Shapes.h:36
bool m_Underlined
Definition: GUItext.h:131
All data generated in GenerateTextCall()
Definition: GUItext.h:248
std::vector< SGUIText::STextCall > m_TextCalls
Text and Sprite Calls.
Definition: GUItext.h:267
std::vector< Tag > m_Tags
Tags that are present.
Definition: GUItext.h:242
CSize m_Size
Width and Height feedback
Definition: GUItext.h:274
CStrW m_TooltipStyle
Tooltip style.
Definition: GUItext.h:84
CRect m_Area
Size and position of sprite.
Definition: GUItext.h:67
SSpriteCall()
Definition: GUItext.h:62
Base settings, all objects possess these settings in their m_BaseSettings Instructions can be found i...
Definition: IGUIObject.h:117
A sprite call to the CRenderer.
Definition: GUItext.h:60
The main object that represents a whole GUI page.
Definition: CGUI.h:75
std::vector< TextChunk > m_TextChunks
TextChunks.
Definition: GUItext.h:323
Definition: GUItext.h:192
Interned 8-bit strings.
Definition: CStrIntern.h:37
bool m_UseCustomColor
Use custom color? If true then m_Color is used, else the color inputted will be used.
Definition: GUItext.h:116
CSize m_Size
Width and height of the whole output, used when setting up scrollbars and such.
Definition: GUItext.h:155
TagType
Tag Type.
Definition: GUItext.h:187
CPos m_Pos
Position.
Definition: GUItext.h:100
std::vector< STextCall > m_TextCalls
List of TextCalls, for instance "Hello", "there!".
Definition: GUItext.h:142
CStrW m_String
The string that is suppose to be rendered.
Definition: GUItext.h:110
CColor m_Color
Color setup.
Definition: GUItext.h:121
Made to represent screen positions and delta values.
Definition: Shapes.h:169
Definition: GUItext.h:196
CStrW m_RawString
The full raw string.
Definition: GUItext.h:328
Definition: GUItext.h:193
STextCall()
Definition: GUItext.h:92
std::vector< int > m_Words
Words.
Definition: GUItext.h:317
Definition: GUItext.h:189
std::wstring m_TagValue
In [b="Hello"][/b] m_TagValue is &#39;Hello&#39;.
Definition: GUItext.h:226
CStrW m_OriginalString
The original string value passed to SetValue.
Definition: GUItext.h:333
Definition: CGUISprite.h:158
std::list< SGUIText::SSpriteCall > m_SpriteCalls
Definition: GUItext.h:268
TagType m_TagType
In [b="Hello"][/b] m_TagType is TAG_B.
Definition: GUItext.h:220
bool m_NewLine
If the word inputted was a new line.
Definition: GUItext.h:279
A tag looks like this "Hello [b]there[/b] little".
Definition: GUItext.h:182
int m_To
Definition: GUItext.h:237
CStrIntern m_Font
Font name.
Definition: GUItext.h:126
CSize m_Size
Size.
Definition: GUItext.h:105
Definition: GUItext.h:191
std::list< SSpriteCall > m_SpriteCalls
List of sprites, or "icons" that should be rendered along with the text.
Definition: GUItext.h:148
A text call to the CRenderer.
Definition: GUItext.h:90
Definition: GUItext.h:190
CGUISpriteInstance m_Sprite
Sprite from global GUI sprite database.
Definition: GUItext.h:72
const CStrW & GetOriginalString() const
Get String, with tags.
Definition: GUItext.h:291
CStrW m_Tooltip
Tooltip text.
Definition: GUItext.h:79
std::vector< TagAttribute > m_TagAttributes
Some tags need an additional attributes.
Definition: GUItext.h:231
String class, substitute for CStr, but that parses the tags and builds up a list of all text that wil...
Definition: GUItext.h:169
An SGUIText object is a parsed string, divided into text-rendering components.
Definition: GUItext.h:55
Rectangle class used for screen rectangles.
Definition: Shapes.h:73