Line data Source code
1 : /* Copyright (C) 2026 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 : #include "precompiled.h"
19 :
20 : #include "gui/CGUI.h"
21 : #include "gui/CGUISprite.h"
22 : #include "gui/SettingTypes/CGUIColor.h"
23 : #include "gui/SettingTypes/CGUISize.h"
24 : #include "gui/SettingTypes/CGUIString.h"
25 : #include "gui/SettingTypes/EAlign.h"
26 : #include "gui/SettingTypes/EScrollOrientation.h"
27 : #include "maths/Rect.h"
28 : #include "maths/Size2D.h"
29 : #include "maths/Vector2D.h"
30 : #include "ps/CLogger.h"
31 : #include "ps/CStr.h"
32 :
33 : #include <sstream>
34 : #include <string>
35 :
36 : class CGUIList;
37 : class CGUISeries;
38 :
39 : template <>
40 0 : bool CGUI::ParseString<bool>(const CGUI*, const CStrW& Value, bool& Output)
41 : {
42 0 : if (Value == L"true")
43 0 : Output = true;
44 0 : else if (Value == L"false")
45 0 : Output = false;
46 : else
47 : return false;
48 :
49 : return true;
50 : }
51 :
52 : template <>
53 0 : bool CGUI::ParseString<std::int32_t>(const CGUI*, const CStrW& Value, int& Output)
54 : {
55 0 : Output = Value.ToInt();
56 0 : return true;
57 : }
58 :
59 : template <>
60 0 : bool CGUI::ParseString<std::uint32_t>(const CGUI*, const CStrW& Value, std::uint32_t& Output)
61 : {
62 0 : Output = Value.ToUInt();
63 0 : return true;
64 : }
65 :
66 : template <>
67 0 : bool CGUI::ParseString<float>(const CGUI*, const CStrW& Value, float& Output)
68 : {
69 0 : Output = Value.ToFloat();
70 0 : return true;
71 : }
72 :
73 : template <>
74 4 : bool CGUI::ParseString<CRect>(const CGUI*, const CStrW& Value, CRect& Output)
75 : {
76 4 : const unsigned int NUM_COORDS = 4;
77 4 : float coords[NUM_COORDS];
78 4 : std::wstringstream stream;
79 4 : stream.str(Value);
80 : // Parse each coordinate
81 16 : for (unsigned int i = 0; i < NUM_COORDS; ++i)
82 : {
83 14 : if (stream.eof())
84 : {
85 3 : LOGWARNING("Too few CRect parameters (min {}). Your input: '{}'", NUM_COORDS,
86 : Value.ToUTF8());
87 1 : return false;
88 : }
89 13 : stream >> coords[i];
90 13 : if ((stream.rdstate() & std::wstringstream::failbit) != 0)
91 : {
92 3 : LOGWARNING("Unable to parse CRect parameters. Your input: '{}'", Value.ToUTF8());
93 1 : return false;
94 : }
95 : }
96 :
97 2 : if (!stream.eof())
98 : {
99 3 : LOGWARNING("Too many CRect parameters (max {}). Your input: '{}'", NUM_COORDS,
100 : Value.ToUTF8());
101 1 : return false;
102 : }
103 :
104 : // Finally the rectangle values
105 1 : Output = CRect(coords[0], coords[1], coords[2], coords[3]);
106 :
107 1 : return true;
108 4 : }
109 :
110 : template <>
111 1 : bool CGUI::ParseString<CGUISize>(const CGUI*, const CStrW& Value, CGUISize& Output)
112 : {
113 1 : return Output.FromString(Value.ToUTF8());
114 : }
115 :
116 : template <>
117 0 : bool CGUI::ParseString<CGUIColor>(const CGUI* pGUI, const CStrW& Value, CGUIColor& Output)
118 : {
119 0 : return Output.ParseString(*pGUI, Value.ToUTF8());
120 : }
121 :
122 : template <>
123 4 : bool CGUI::ParseString<CSize2D>(const CGUI*, const CStrW& Value, CSize2D& Output)
124 : {
125 4 : const unsigned int NUM_COORDS = 2;
126 4 : float coords[NUM_COORDS];
127 4 : std::wstringstream stream;
128 4 : stream.str(Value);
129 : // Parse each coordinate
130 10 : for (unsigned int i = 0; i < NUM_COORDS; ++i)
131 : {
132 8 : if (stream.eof())
133 : {
134 3 : LOGWARNING("Too few CSize2D parameters (min {}). Your input: '{}'", NUM_COORDS,
135 : Value.ToUTF8());
136 1 : return false;
137 : }
138 7 : stream >> coords[i];
139 7 : if ((stream.rdstate() & std::wstringstream::failbit) != 0)
140 : {
141 3 : LOGWARNING("Unable to parse CSize2D parameters. Your input: '{}'", Value.ToUTF8());
142 1 : return false;
143 : }
144 : }
145 :
146 2 : Output.Width = coords[0];
147 2 : Output.Height = coords[1];
148 :
149 2 : if (!stream.eof())
150 : {
151 3 : LOGWARNING("Too many CSize2D parameters (max {}). Your input: '{}'", NUM_COORDS,
152 : Value.ToUTF8());
153 1 : return false;
154 : }
155 :
156 : return true;
157 4 : }
158 :
159 : template <>
160 4 : bool CGUI::ParseString<CVector2D>(const CGUI*, const CStrW& Value, CVector2D& Output)
161 : {
162 4 : const unsigned int NUM_COORDS = 2;
163 4 : float coords[NUM_COORDS];
164 4 : std::wstringstream stream;
165 4 : stream.str(Value);
166 : // Parse each coordinate
167 10 : for (unsigned int i = 0; i < NUM_COORDS; ++i)
168 : {
169 8 : if (stream.eof())
170 : {
171 3 : LOGWARNING("Too few CVector2D parameters (min {}). Your input: '{}'", NUM_COORDS,
172 : Value.ToUTF8());
173 1 : return false;
174 : }
175 7 : stream >> coords[i];
176 7 : if ((stream.rdstate() & std::wstringstream::failbit) != 0)
177 : {
178 3 : LOGWARNING("Unable to parse CVector2D parameters. Your input: '{}'", Value.ToUTF8());
179 1 : return false;
180 : }
181 : }
182 :
183 2 : Output.X = coords[0];
184 2 : Output.Y = coords[1];
185 :
186 2 : if (!stream.eof())
187 : {
188 3 : LOGWARNING("Too many CVector2D parameters (max {}). Your input: '{}'", NUM_COORDS,
189 : Value.ToUTF8());
190 1 : return false;
191 : }
192 :
193 : return true;
194 4 : }
195 :
196 : template <>
197 0 : bool CGUI::ParseString<EAlign>(const CGUI*, const CStrW& Value, EAlign& Output)
198 : {
199 0 : if (Value == L"left")
200 0 : Output = EAlign::LEFT;
201 0 : else if (Value == L"center")
202 0 : Output = EAlign::CENTER;
203 0 : else if (Value == L"right")
204 0 : Output = EAlign::RIGHT;
205 : else
206 : return false;
207 :
208 : return true;
209 : }
210 :
211 : template <>
212 0 : bool CGUI::ParseString<EVAlign>(const CGUI*, const CStrW& Value, EVAlign& Output)
213 : {
214 0 : if (Value == L"top")
215 0 : Output = EVAlign::TOP;
216 0 : else if (Value == L"center")
217 0 : Output = EVAlign::CENTER;
218 0 : else if (Value == L"bottom")
219 0 : Output = EVAlign::BOTTOM;
220 : else
221 : return false;
222 :
223 : return true;
224 : }
225 :
226 : template <>
227 0 : bool CGUI::ParseString<CGUIString>(const CGUI*, const CStrW& Value, CGUIString& Output)
228 : {
229 0 : Output.SetValue(Value);
230 0 : return true;
231 : }
232 :
233 : template <>
234 2 : bool CGUI::ParseString<CStr>(const CGUI*, const CStrW& Value, CStr& Output)
235 : {
236 4 : Output = Value.ToUTF8();
237 2 : return true;
238 : }
239 :
240 : template <>
241 0 : bool CGUI::ParseString<CStrW>(const CGUI*, const CStrW& Value, CStrW& Output)
242 : {
243 0 : Output = Value;
244 0 : return true;
245 : }
246 :
247 : template <>
248 0 : bool CGUI::ParseString<CGUISpriteInstance>(const CGUI*, const CStrW& Value, CGUISpriteInstance& Output)
249 : {
250 0 : Output = CGUISpriteInstance(Value.ToUTF8());
251 0 : return true;
252 : }
253 :
254 : template <>
255 0 : bool CGUI::ParseString<CGUISeries>(const CGUI*, const CStrW& /*Value*/, CGUISeries& /*Output*/)
256 : {
257 0 : return false;
258 : }
259 :
260 : template <>
261 0 : bool CGUI::ParseString<CGUIList>(const CGUI*, const CStrW& /*Value*/, CGUIList& /*Output*/)
262 : {
263 0 : return false;
264 : }
265 :
266 : template <>
267 0 : bool CGUI::ParseString<EScrollOrientation>(const CGUI*, const CStrW& Value, EScrollOrientation& Output)
268 : {
269 0 : if (Value == L"vertical")
270 0 : Output = EScrollOrientation::VERTICAL;
271 0 : else if (Value == L"horizontal")
272 0 : Output = EScrollOrientation::HORIZONTAL;
273 0 : else if (Value == L"both")
274 0 : Output = EScrollOrientation::BOTH;
275 : else
276 : return false;
277 :
278 : return true;
279 : }
|