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