[Gameplay] Reduce loom cost
[0ad.git] / source / gui / GUIStringConversions.cpp
blob6a93d195227f7d9b278ce93e9aaf04d319511063
1 /* Copyright (C) 2021 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #include "precompiled.h"
20 #include "gui/CGUI.h"
21 #include "gui/SettingTypes/CGUIString.h"
22 #include "ps/CLogger.h"
24 class CGUIList;
25 class CGUISeries;
27 template <>
28 bool CGUI::ParseString<bool>(const CGUI* UNUSED(pGUI), const CStrW& Value, bool& Output)
30 if (Value == L"true")
31 Output = true;
32 else if (Value == L"false")
33 Output = false;
34 else
35 return false;
37 return true;
40 template <>
41 bool CGUI::ParseString<i32>(const CGUI* UNUSED(pGUI), const CStrW& Value, int& Output)
43 Output = Value.ToInt();
44 return true;
47 template <>
48 bool CGUI::ParseString<u32>(const CGUI* UNUSED(pGUI), const CStrW& Value, u32& Output)
50 Output = Value.ToUInt();
51 return true;
54 template <>
55 bool CGUI::ParseString<float>(const CGUI* UNUSED(pGUI), const CStrW& Value, float& Output)
57 Output = Value.ToFloat();
58 return true;
61 template <>
62 bool CGUI::ParseString<CRect>(const CGUI* UNUSED(pGUI), const CStrW& Value, CRect& Output)
64 const unsigned int NUM_COORDS = 4;
65 float coords[NUM_COORDS];
66 std::wstringstream stream;
67 stream.str(Value);
68 // Parse each coordinate
69 for (unsigned int i = 0; i < NUM_COORDS; ++i)
71 if (stream.eof())
73 LOGWARNING("Too few CRect parameters (min %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str());
74 return false;
76 stream >> coords[i];
77 if ((stream.rdstate() & std::wstringstream::failbit) != 0)
79 LOGWARNING("Unable to parse CRect parameters. Your input: '%s'", Value.ToUTF8().c_str());
80 return false;
84 if (!stream.eof())
86 LOGWARNING("Too many CRect parameters (max %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str());
87 return false;
90 // Finally the rectangle values
91 Output = CRect(coords[0], coords[1], coords[2], coords[3]);
93 return true;
96 template <>
97 bool CGUI::ParseString<CGUISize>(const CGUI* UNUSED(pGUI), const CStrW& Value, CGUISize& Output)
99 return Output.FromString(Value.ToUTF8());
102 template <>
103 bool CGUI::ParseString<CGUIColor>(const CGUI* pGUI, const CStrW& Value, CGUIColor& Output)
105 return Output.ParseString(*pGUI, Value.ToUTF8());
108 template <>
109 bool CGUI::ParseString<CSize2D>(const CGUI* UNUSED(pGUI), const CStrW& Value, CSize2D& Output)
111 const unsigned int NUM_COORDS = 2;
112 float coords[NUM_COORDS];
113 std::wstringstream stream;
114 stream.str(Value);
115 // Parse each coordinate
116 for (unsigned int i = 0; i < NUM_COORDS; ++i)
118 if (stream.eof())
120 LOGWARNING("Too few CSize2D parameters (min %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str());
121 return false;
123 stream >> coords[i];
124 if ((stream.rdstate() & std::wstringstream::failbit) != 0)
126 LOGWARNING("Unable to parse CSize2D parameters. Your input: '%s'", Value.ToUTF8().c_str());
127 return false;
131 Output.Width = coords[0];
132 Output.Height = coords[1];
134 if (!stream.eof())
136 LOGWARNING("Too many CSize2D parameters (max %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str());
137 return false;
140 return true;
143 template <>
144 bool CGUI::ParseString<CVector2D>(const CGUI* UNUSED(pGUI), const CStrW& Value, CVector2D& Output)
146 const unsigned int NUM_COORDS = 2;
147 float coords[NUM_COORDS];
148 std::wstringstream stream;
149 stream.str(Value);
150 // Parse each coordinate
151 for (unsigned int i = 0; i < NUM_COORDS; ++i)
153 if (stream.eof())
155 LOGWARNING("Too few CVector2D parameters (min %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str());
156 return false;
158 stream >> coords[i];
159 if ((stream.rdstate() & std::wstringstream::failbit) != 0)
161 LOGWARNING("Unable to parse CVector2D parameters. Your input: '%s'", Value.ToUTF8().c_str());
162 return false;
166 Output.X = coords[0];
167 Output.Y = coords[1];
169 if (!stream.eof())
171 LOGWARNING("Too many CVector2D parameters (max %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str());
172 return false;
175 return true;
178 template <>
179 bool CGUI::ParseString<EAlign>(const CGUI* UNUSED(pGUI), const CStrW& Value, EAlign& Output)
181 if (Value == L"left")
182 Output = EAlign::LEFT;
183 else if (Value == L"center")
184 Output = EAlign::CENTER;
185 else if (Value == L"right")
186 Output = EAlign::RIGHT;
187 else
188 return false;
190 return true;
193 template <>
194 bool CGUI::ParseString<EVAlign>(const CGUI* UNUSED(pGUI), const CStrW& Value, EVAlign& Output)
196 if (Value == L"top")
197 Output = EVAlign::TOP;
198 else if (Value == L"center")
199 Output = EVAlign::CENTER;
200 else if (Value == L"bottom")
201 Output = EVAlign::BOTTOM;
202 else
203 return false;
205 return true;
208 template <>
209 bool CGUI::ParseString<CGUIString>(const CGUI* UNUSED(pGUI), const CStrW& Value, CGUIString& Output)
211 Output.SetValue(Value);
212 return true;
215 template <>
216 bool CGUI::ParseString<CStr>(const CGUI* UNUSED(pGUI), const CStrW& Value, CStr& Output)
218 Output = Value.ToUTF8();
219 return true;
222 template <>
223 bool CGUI::ParseString<CStrW>(const CGUI* UNUSED(pGUI), const CStrW& Value, CStrW& Output)
225 Output = Value;
226 return true;
229 template <>
230 bool CGUI::ParseString<CGUISpriteInstance>(const CGUI* UNUSED(pGUI), const CStrW& Value, CGUISpriteInstance& Output)
232 Output = CGUISpriteInstance(Value.ToUTF8());
233 return true;
236 template <>
237 bool CGUI::ParseString<CGUISeries>(const CGUI* UNUSED(pGUI), const CStrW& UNUSED(Value), CGUISeries& UNUSED(Output))
239 return false;
242 template <>
243 bool CGUI::ParseString<CGUIList>(const CGUI* UNUSED(pGUI), const CStrW& UNUSED(Value), CGUIList& UNUSED(Output))
245 return false;