Line data Source code
1 : /* Copyright (C) 2025 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_CGUISTRING
19 : #define INCLUDED_CGUISTRING
20 :
21 : #include "gui/CGUIText.h"
22 : #include "lib/code_annotation.h"
23 : #include "maths/Size2D.h"
24 : #include "ps/CStr.h"
25 :
26 : #include <array>
27 : #include <list>
28 : #include <string>
29 : #include <vector>
30 :
31 : class CGUI;
32 : class CStrIntern;
33 : class IGUIObject;
34 :
35 : /**
36 : * String class, substitute for CStr, but that parses
37 : * the tags and builds up a list of all text that will
38 : * be different when outputted.
39 : *
40 : * The difference between CGUIString and CGUIText is that
41 : * CGUIString is a string-class that parses the tags
42 : * when the value is set. The CGUIText is just a container
43 : * which stores the positions and settings of all text-calls
44 : * that will have to be made to the Renderer.
45 : */
46 5 : class CGUIString
47 : {
48 : public:
49 : /**
50 : * A chunk of text that represents one call to the renderer.
51 : * In other words, all text in one chunk, will be drawn
52 : * exactly with the same settings.
53 : */
54 28 : struct TextChunk
55 : {
56 : /**
57 : * A tag looks like this "Hello [b]there[/b] little"
58 : */
59 304 : struct Tag
60 : {
61 : /**
62 : * Tag Type
63 : */
64 : enum TagType
65 : {
66 : TAG_B,
67 : TAG_I,
68 : TAG_FONT,
69 : TAG_LOCALE,
70 : TAG_SIZE,
71 : TAG_COLOR,
72 : TAG_IMGLEFT,
73 : TAG_IMGRIGHT,
74 : TAG_ICON,
75 : TAG_TOOLTIP,
76 : TAG_INVALID
77 : };
78 :
79 0 : struct TagAttribute
80 : {
81 : std::wstring attrib;
82 : std::wstring value;
83 : };
84 :
85 : /**
86 : * Set tag from string
87 : *
88 : * @param tagtype TagType by string, like 'img' for [img]
89 : * @return True if m_TagType was set.
90 : */
91 : bool SetTagType(const CStrW& tagtype);
92 : TagType GetTagType(const CStrW& tagtype) const;
93 :
94 :
95 : /**
96 : * In [b="Hello"][/b]
97 : * m_TagType is TAG_B
98 : */
99 : TagType m_TagType;
100 :
101 : /**
102 : * In [b="Hello"][/b]
103 : * m_TagValue is 'Hello'
104 : */
105 : std::wstring m_TagValue;
106 :
107 : /**
108 : * Some tags need an additional attributes
109 : */
110 : std::vector<TagAttribute> m_TagAttributes;
111 : };
112 :
113 : /**
114 : * m_From and m_To is the range of the string
115 : */
116 : int m_From, m_To;
117 :
118 : /**
119 : * Tags that are present. [a][b]
120 : */
121 : std::vector<Tag> m_Tags;
122 : };
123 :
124 : /**
125 : * All data generated in GenerateTextCall()
126 : */
127 : struct SFeedback
128 : {
129 : // Avoid copying the vector and list containers.
130 : NONCOPYABLE(SFeedback);
131 : MOVABLE(SFeedback);
132 1668 : SFeedback() = default;
133 :
134 : // Constants
135 : static const int Left = 0;
136 : static const int Right = 1;
137 :
138 : /**
139 : * Reset all member data.
140 : */
141 : void Reset();
142 :
143 : /**
144 : * Image stacks, for left and right floating images.
145 : */
146 : std::array<std::vector<CStr>, 2> m_Images; // left and right
147 :
148 : /**
149 : * Text and Sprite Calls.
150 : */
151 : std::vector<CGUIText::STextCall> m_TextCalls;
152 :
153 : // list for consistent mem addresses so that we can point to elements.
154 : std::list<CGUIText::SSpriteCall> m_SpriteCalls;
155 :
156 : /**
157 : * Width and Height *feedback*
158 : */
159 : CSize2D m_Size;
160 :
161 : /**
162 : * If the word inputted was a new line.
163 : */
164 : bool m_NewLine;
165 :
166 : /**
167 : * If the word inputted ends with a space that can be collapsed when aligning.
168 : */
169 : bool m_EndsWithSpace;
170 : };
171 :
172 : /**
173 : * Set the value, the string will automatically
174 : * be parsed when set.
175 : */
176 : void SetValue(const CStrW& str);
177 :
178 : /**
179 : * Get String, with tags
180 : */
181 0 : const CStrW& GetOriginalString() const { return m_OriginalString; }
182 :
183 : /**
184 : * Get String, stripped of tags
185 : */
186 0 : const CStrW& GetRawString() const { return m_RawString; }
187 :
188 : /**
189 : * Generate Text Call from specified range. The range
190 : * must span only within ONE TextChunk though. Otherwise
191 : * it can't be fit into a single Text Call
192 : *
193 : * Notice it won't make it complete, you will have to add
194 : * X/Y values and such.
195 : *
196 : * @param pGUI Pointer to CGUI object making this call, for e.g. icon retrieval.
197 : * @param Feedback contains all info that is generated.
198 : * @param DefaultFont Default Font
199 : * @param from From character n,
200 : * @param to to character n.
201 : * @param FirstLine Whether this is the first line of text, to calculate its height correctly
202 : * @param pObject Only for Error outputting, optional! If nullptr
203 : * then no Errors will be reported! Useful when you need
204 : * to make several GenerateTextCall in different phases,
205 : * it avoids duplicates.
206 : */
207 : void GenerateTextCall(const CGUI& pGUI, SFeedback& Feedback, CStrIntern DefaultFont, const int& from, const int& to, const bool FirstLine, const IGUIObject* pObject = nullptr) const;
208 :
209 : /**
210 : * Words
211 : */
212 : std::vector<int> m_Words;
213 :
214 : private:
215 : /**
216 : * TextChunks
217 : */
218 : std::vector<TextChunk> m_TextChunks;
219 :
220 : /**
221 : * The full raw string. Stripped of tags.
222 : */
223 : CStrW m_RawString;
224 :
225 : /**
226 : * The original string value passed to SetValue.
227 : */
228 : CStrW m_OriginalString;
229 : };
230 :
231 : #endif // INCLUDED_CGUISTRING
|