File: CGUIString.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 (225 lines) | stat: -rw-r--r-- 5,165 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
/* 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_CGUISTRING
#define INCLUDED_CGUISTRING

#include "gui/CGUIText.h"
#include "ps/CStrIntern.h"

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

class CGUI;

/**
 * String class, substitute for CStr, but that parses
 * the tags and builds up a list of all text that will
 * be different when outputted.
 *
 * The difference between CGUIString and CGUIText is that
 * CGUIString is a string-class that parses the tags
 * when the value is set. The CGUIText is just a container
 * which stores the positions and settings of all text-calls
 * that will have to be made to the Renderer.
 */
class CGUIString
{
public:
	/**
	 * A chunk of text that represents one call to the renderer.
	 * In other words, all text in one chunk, will be drawn
	 * exactly with the same settings.
	 */
	struct TextChunk
	{
		/**
		 * A tag looks like this "Hello [b]there[/b] little"
		 */
		struct Tag
		{
			/**
			 * Tag Type
			 */
			enum TagType
			{
				TAG_B,
				TAG_I,
				TAG_FONT,
				TAG_SIZE,
				TAG_COLOR,
				TAG_IMGLEFT,
				TAG_IMGRIGHT,
				TAG_ICON,
				TAG_TOOLTIP,
				TAG_INVALID
			};

			struct TagAttribute
			{
				std::wstring attrib;
				std::wstring value;
			};

			/**
			 * Set tag from string
			 *
			 * @param tagtype TagType by string, like 'img' for [img]
			 * @return True if m_TagType was set.
			 */
			bool SetTagType(const CStrW& tagtype);
			TagType GetTagType(const CStrW& tagtype) const;


			/**
			 * In [b="Hello"][/b]
			 * m_TagType is TAG_B
			 */
			TagType m_TagType;

			/**
			 * In [b="Hello"][/b]
			 * m_TagValue is 'Hello'
			 */
			std::wstring m_TagValue;

			/**
			 * Some tags need an additional attributes
			 */
			std::vector<TagAttribute> m_TagAttributes;
		};

		/**
		 * m_From and m_To is the range of the string
		 */
		int m_From, m_To;

		/**
		 * Tags that are present. [a][b]
		 */
		std::vector<Tag> m_Tags;
	};

	/**
	 * All data generated in GenerateTextCall()
	 */
	struct SFeedback
	{
		// Avoid copying the vector and list containers.
		NONCOPYABLE(SFeedback);
		MOVABLE(SFeedback);
		SFeedback() = default;

		// Constants
		static const int Left = 0;
		static const int Right = 1;

		/**
		 * Reset all member data.
		 */
		void Reset();

		/**
		 * Image stacks, for left and right floating images.
		 */
		std::array<std::vector<CStr>, 2> m_Images; // left and right

		/**
		 * Text and Sprite Calls.
		 */
		std::vector<CGUIText::STextCall> m_TextCalls;

		// list for consistent mem addresses so that we can point to elements.
		std::list<CGUIText::SSpriteCall> m_SpriteCalls;

		/**
		 * Width and Height *feedback*
		 */
		CSize2D m_Size;

		/**
		 * If the word inputted was a new line.
		 */
		bool m_NewLine;

		/**
		 * If the word inputted ends with a space that can be collapsed when aligning.
		 */
		bool m_EndsWithSpace;
	};

	/**
	 * Set the value, the string will automatically
	 * be parsed when set.
	 */
	void SetValue(const CStrW& str);

	/**
	 * Get String, with tags
	 */
	const CStrW& GetOriginalString() const { return m_OriginalString; }

	/**
	 * Get String, stripped of tags
	 */
	const CStrW& GetRawString() const { return m_RawString; }

	/**
	 * Generate Text Call from specified range. The range
	 * must span only within ONE TextChunk though. Otherwise
	 * it can't be fit into a single Text Call
	 *
	 * Notice it won't make it complete, you will have to add
	 * X/Y values and such.
	 *
	 * @param pGUI Pointer to CGUI object making this call, for e.g. icon retrieval.
	 * @param Feedback contains all info that is generated.
	 * @param DefaultFont Default Font
	 * @param from From character n,
	 * @param to to character n.
	 * @param FirstLine Whether this is the first line of text, to calculate its height correctly
	 * @param pObject Only for Error outputting, optional! If nullptr
	 *		  then no Errors will be reported! Useful when you need
	 *		  to make several GenerateTextCall in different phases,
	 *		  it avoids duplicates.
	 */
	void GenerateTextCall(const CGUI& pGUI, SFeedback& Feedback, CStrIntern DefaultFont, const int& from, const int& to, const bool FirstLine, const IGUIObject* pObject = nullptr) const;

	/**
	 * Words
	 */
	std::vector<int> m_Words;

private:
	/**
	 * TextChunks
	 */
	std::vector<TextChunk> m_TextChunks;

	/**
	 * The full raw string. Stripped of tags.
	 */
	CStrW m_RawString;

	/**
	 * The original string value passed to SetValue.
	 */
	CStrW m_OriginalString;
};

#endif // INCLUDED_CGUISTRING