File: CGUIText.h

package info (click to toggle)
0ad 0.0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,460 kB
  • sloc: cpp: 261,824; ansic: 198,392; javascript: 19,067; python: 14,557; sh: 7,629; perl: 4,072; xml: 849; makefile: 741; java: 533; ruby: 229; php: 190; pascal: 30; sql: 21; tcl: 4
file content (280 lines) | stat: -rw-r--r-- 6,951 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* Copyright (C) 2022 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_GUITEXT
#define INCLUDED_GUITEXT

#include "gui/CGUISprite.h"
#include "gui/SettingTypes/CGUIColor.h"
#include "gui/SettingTypes/EAlign.h"
#include "maths/Rect.h"
#include "maths/Size2D.h"
#include "maths/Vector2D.h"
#include "ps/CStrIntern.h"

#include <array>
#include <list>
#include <vector>

class CCanvas2D;
class CGUI;
class CGUIString;
class IGUIObject;
struct SGenerateTextImage;

using SGenerateTextImages = std::array<std::vector<SGenerateTextImage>, 2>;

/**
 * An CGUIText object is a parsed string, divided into
 * text-rendering components. Each component, being a
 * call to the Renderer. For instance, if you by tags
 * change the color, then the GUI will have to make
 * individual calls saying it want that color on the
 * text.
 *
 * For instance:
 * "Hello [b]there[/b] bunny!"
 *
 * That without word-wrapping would mean 3 components.
 * i.e. 3 calls to CRenderer. One drawing "Hello",
 * one drawing "there" in bold, and one drawing "bunny!".
 */
class CGUIText
{
public:
	/**
	 * A sprite call to the CRenderer
	 */
	struct SSpriteCall
	{
		// The CGUISpriteInstance makes this uncopyable to avoid invalidating its draw cache
		NONCOPYABLE(SSpriteCall);
		MOVABLE(SSpriteCall);

		SSpriteCall() {}

		/**
		 * Size and position of sprite
		 */
		CRect m_Area;

		/**
		 * Sprite from global GUI sprite database.
		 */
		CGUISpriteInstance m_Sprite;
	};

	/**
	 * A text call to the CRenderer
	 */
	struct STextCall
	{
		NONCOPYABLE(STextCall);
		MOVABLE(STextCall);

		STextCall() :
			m_UseCustomColor(false),
			m_Bold(false), m_Italic(false), m_Underlined(false),
			m_pSpriteCall(nullptr) {}

		/**
		 * Position
		 */
		CVector2D m_Pos;

		/**
		 * Size
		 */
		CSize2D m_Size;

		/**
		 * The string that is suppose to be rendered.
		 */
		CStrW m_String;

		/**
		 * Use custom color? If true then m_Color is used,
		 * else the color inputted will be used.
		 */
		bool m_UseCustomColor;

		/**
		 * Color setup
		 */
		CGUIColor m_Color;

		/**
		 * Font name
		 */
		CStrIntern m_Font;

		/**
		 * Settings
		 */
		bool m_Bold, m_Italic, m_Underlined;

		/**
		 * Tooltip text
		 */
		CStrW m_Tooltip;

		/**
		 * *IF* an icon, then this is not nullptr.
		 */
		std::list<SSpriteCall>::pointer m_pSpriteCall;
	};

	// The SSpriteCall CGUISpriteInstance makes this uncopyable to avoid invalidating its draw cache.
	// Also take advantage of exchanging the containers directly with move semantics.
	NONCOPYABLE(CGUIText);
	MOVABLE(CGUIText);

	/**
	 * Generates empty text.
	 */
	CGUIText() = default;

	/**
	 * Generate a CGUIText object from the inputted string.
	 * The function will break down the string and its
	 * tags to calculate exactly which rendering queries
	 * will be sent to the Renderer. Also, horizontal alignment
	 * is taken into acount in this method but NOT vertical alignment.
	 *
	 * @param string Text to generate CGUIText object from.
	 * @param font Default font, notice both Default color and default font
	 *		  can be changed by tags.
	 * @param width Width, 0 if no word-wrapping.
	 * @param bufferZone Space between text and edge, and space between text and images.
	 * @param align Horizontal alignment (left / center / right).
	 * @param pObject Optional parameter for error output. Used *only* if error parsing fails,
	 *		  and we need to be able to output which object the error occurred in to aid the user.
	 */
	CGUIText(const CGUI& pGUI, const CGUIString& string, const CStrW& fontW, const float width, const float bufferZone, const EAlign align, const IGUIObject* pObject);

	/**
	 * Draw this CGUIText object
	 */
	void Draw(CGUI& pGUI, CCanvas2D& canvas, const CGUIColor& DefaultColor, const CVector2D& pos, CRect clipping) const;

	const CSize2D& GetSize() const { return m_Size; }

	const std::list<SSpriteCall>& GetSpriteCalls() const { return m_SpriteCalls; }

	const std::vector<STextCall>& GetTextCalls() const { return m_TextCalls; }

	// Helper functions of the constructor
	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);

	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);

	float GetLineOffset(
		const EAlign align,
		const float widthRangeFrom,
		const float widthRangeTo,
		const CSize2D& lineSize) const;

	void ComputeLineRange(
		const SGenerateTextImages& images,
		const float y,
		const float width,
		const float prelimLineHeight,
		float& widthRangeFrom,
		float& widthRangeTo) const;

	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;

	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);

	/**
	 * List of TextCalls, for instance "Hello", "there!"
	 */
	std::vector<STextCall> m_TextCalls;

	/**
	 * List of sprites, or "icons" that should be rendered
	 * along with the text.
	 */
	std::list<SSpriteCall> m_SpriteCalls; // list for consistent mem addresses
										  // so that we can point to elements.
	/**
	 * Width and height of the whole output, used when setting up
	 * scrollbars and such.
	 */
	CSize2D m_Size;
};

struct SGenerateTextImage
{
	// The image's starting location in Y
	float m_YFrom;

	// The image's end location in Y
	float m_YTo;

	// The image width in other words
	float m_Indentation;

	void SetupSpriteCall(
		const bool left, CGUIText::SSpriteCall& spriteCall, const float width, const float y,
		const CSize2D& size, const CStr& textureName, const float bufferZone);
};

#endif // INCLUDED_GUITEXT