Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GUIutil.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 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 /*
19 GUI utilities
20 */
21 
22 #include "precompiled.h"
23 #include "GUI.h"
24 #include "GUIManager.h"
25 #include "maths/Matrix3D.h"
26 #include "ps/Parser.h"
27 
28 extern int g_xres, g_yres;
29 
30 #include "ps/CLogger.h"
31 
32 
33 template <>
34 bool __ParseString<bool>(const CStrW& Value, bool &Output)
35 {
36  if (Value == L"true")
37  Output = true;
38  else
39  if (Value == L"false")
40  Output = false;
41  else
42  return false;
43 
44  return true;
45 }
46 
47 template <>
48 bool __ParseString<int>(const CStrW& Value, int &Output)
49 {
50  Output = Value.ToInt();
51  return true;
52 }
53 
54 template <>
55 bool __ParseString<float>(const CStrW& Value, float &Output)
56 {
57  Output = Value.ToFloat();
58  return true;
59 }
60 
61 template <>
62 bool __ParseString<CRect>(const CStrW& Value, CRect &Output)
63 {
64  // Use the parser to parse the values
65  CParser& parser (CParserCache::Get("_$value_$value_$value_$value_"));
66 
67  CParserLine line;
68  line.ParseString(parser, Value.ToUTF8());
69  if (!line.m_ParseOK)
70  {
71  // Parsing failed
72  return false;
73  }
74  float values[4];
75  for (int i=0; i<4; ++i)
76  {
77  if (!line.GetArgFloat(i, values[i]))
78  {
79  // Parsing failed
80  return false;
81  }
82  }
83 
84  // Finally the rectangle values
85  Output = CRect(values[0], values[1], values[2], values[3]);
86  return true;
87 }
88 
89 template <>
90 bool __ParseString<CClientArea>(const CStrW& Value, CClientArea &Output)
91 {
92  return Output.SetClientArea(Value.ToUTF8());
93 }
94 
95 template <>
96 bool GUI<int>::ParseColor(const CStrW& Value, CColor &Output, float DefaultAlpha)
97 {
98  // First, check our database in g_GUI for pre-defined colors
99  // If we find anything, we'll ignore DefaultAlpha
100  // If it fails, it won't do anything with Output
101  if (g_GUI->GetPreDefinedColor(Value.ToUTF8(), Output))
102  return true;
103 
104  return Output.ParseString(Value.ToUTF8(), DefaultAlpha);
105 }
106 
107 
108 template <>
109 bool __ParseString<CColor>(const CStrW& Value, CColor &Output)
110 {
111  // First, check our database in g_GUI for pre-defined colors
112  // If it fails, it won't do anything with Output
113  if (g_GUI->GetPreDefinedColor(Value.ToUTF8(), Output))
114  return true;
115 
116  return Output.ParseString(Value.ToUTF8(), 255.f);
117 }
118 
119 template <>
120 bool __ParseString<CSize>(const CStrW& Value, CSize &Output)
121 {
122  // Use the parser to parse the values
123  CParser& parser (CParserCache::Get("_$value_$value_"));
124 
125  CParserLine line;
126  line.ParseString(parser, Value.ToUTF8());
127  if (!line.m_ParseOK)
128  {
129  // Parsing failed
130  return false;
131  }
132 
133  float x, y;
134 
135  // x
136  if (!line.GetArgFloat(0, x))
137  {
138  // TODO Gee: Parsing failed
139  return false;
140  }
141 
142  // y
143  if (!line.GetArgFloat(1, y))
144  {
145  // TODO Gee: Parsing failed
146  return false;
147  }
148 
149  Output.cx = x;
150  Output.cy = y;
151 
152  return true;
153 }
154 
155 template <>
156 bool __ParseString<CPos>(const CStrW& Value, CPos &Output)
157 {
158  CParser& parser (CParserCache::Get("_[-$arg(_minus)]$value_[-$arg(_minus)]$value_"));
159 
160  CParserLine line;
161  line.ParseString(parser, Value.ToUTF8());
162  if (!line.m_ParseOK)
163  return false;
164 
165  float x, y;
166  if (!line.GetArgFloat(0, x))
167  return false;
168  if (!line.GetArgFloat(1, y))
169  return false;
170 
171  Output.x = x;
172  Output.y = y;
173 
174  return true;
175 }
176 
177 template <>
178 bool __ParseString<EAlign>(const CStrW& Value, EAlign &Output)
179 {
180  if (Value == L"left")
181  Output = EAlign_Left;
182  else
183  if (Value == L"center")
184  Output = EAlign_Center;
185  else
186  if (Value == L"right")
187  Output = EAlign_Right;
188  else
189  return false;
190 
191  return true;
192 }
193 
194 template <>
195 bool __ParseString<EVAlign>(const CStrW& Value, EVAlign &Output)
196 {
197  if (Value == L"top")
198  Output = EVAlign_Top;
199  else
200  if (Value == L"center")
201  Output = EVAlign_Center;
202  else
203  if (Value == L"bottom")
204  Output = EVAlign_Bottom;
205  else
206  return false;
207 
208  return true;
209 }
210 
211 template <>
212 bool __ParseString<CGUIString>(const CStrW& Value, CGUIString &Output)
213 {
214  // TODO: i18n: Might want to translate the Value perhaps
215 
216  Output.SetValue(Value);
217  return true;
218 }
219 
220 template <>
221 bool __ParseString<CStr>(const CStrW& Value, CStr& Output)
222 {
223  // Do very little.
224  Output = Value.ToUTF8();
225  return true;
226 }
227 
228 template <>
229 bool __ParseString<CStrW>(const CStrW& Value, CStrW& Output)
230 {
231  // TODO: i18n: Might want to translate the Value perhaps
232 
233  Output = Value;
234  return true;
235 }
236 
237 template <>
238 bool __ParseString<CGUISpriteInstance>(const CStrW& Value, CGUISpriteInstance &Output)
239 {
240  Output = CGUISpriteInstance(Value.ToUTF8());
241  return true;
242 }
243 
244 template <>
245 bool __ParseString<CGUIList>(const CStrW& UNUSED(Value), CGUIList& UNUSED(Output))
246 {
247  return false;
248 }
249 
250 
251 //--------------------------------------------------------
252 
254 {
255  CMatrix3D m;
256  m.SetIdentity();
257  m.Scale(1.0f, -1.f, 1.0f);
258  m.Translate(0.0f, (float)g_yres, -1000.0f);
259 
260  CMatrix3D proj;
261  proj.SetOrtho(0.f, (float)g_xres, 0.f, (float)g_yres, -1.f, 1000.f);
262  m = proj * m;
263 
264  return m;
265 }
266 
267 //--------------------------------------------------------
268 // Utilities implementation
269 //--------------------------------------------------------
271 {
272 // if (!GUIinstance.ObjectExists(Object))
273 // return NULL;
274 
275  return GUIinstance.m_pAllObjects.find(Object)->second;
276 }
277 
278 const IGUIObject * CInternalCGUIAccessorBase::GetObjectPointer(const CGUI &GUIinstance, const CStr& Object)
279 {
280 // if (!GUIinstance.ObjectExists(Object))
281 // return NULL;
282 
283  return GUIinstance.m_pAllObjects.find(Object)->second;
284 }
285 
287 {
289 }
290 
292 {
293  pObject->HandleMessage(message);
294 }
295 
296 
297 
298 #ifndef NDEBUG
299  #define TYPE(T) \
300  template<> void CheckType<T>(const IGUIObject* obj, const CStr& setting) { \
301  std::map<CStr, SGUISetting>::const_iterator it = obj->m_Settings.find(setting); \
302  if (it == obj->m_Settings.end() || it->second.m_Type != GUIST_##T) \
303  { \
304  /* Abort now, to avoid corrupting everything by invalidly \
305  casting pointers */ \
306  DEBUG_DISPLAY_ERROR(L"FATAL ERROR: Inconsistent types in GUI"); \
307  } \
308  }
309  #include "GUItypes.h"
310  #undef TYPE
311 #endif
312 
313 
314 //--------------------------------------------------------------------
315 
316 template <typename T>
317 PSRETURN GUI<T>::GetSettingPointer(const IGUIObject *pObject, const CStr& Setting, T* &Value)
318 {
319  ENSURE(pObject != NULL);
320 
321  std::map<CStr, SGUISetting>::const_iterator it = pObject->m_Settings.find(Setting);
322  if (it == pObject->m_Settings.end())
323  {
324  LOGWARNING(L"setting %hs was not found on object %hs",
325  Setting.c_str(),
326  pObject->GetPresentableName().c_str());
328  }
329 
330  if (it->second.m_pSetting == NULL)
332 
333 #ifndef NDEBUG
334  CheckType<T>(pObject, Setting);
335 #endif
336 
337  // Get value
338  Value = (T*)(it->second.m_pSetting);
339 
340  return PSRETURN_OK;
341 }
342 
343 template <typename T>
344 PSRETURN GUI<T>::GetSetting(const IGUIObject *pObject, const CStr& Setting, T &Value)
345 {
346  T* v = NULL;
347  PSRETURN ret = GetSettingPointer(pObject, Setting, v);
348  if (ret == PSRETURN_OK)
349  Value = *v;
350  return ret;
351 }
352 
353 // Helper function for SetSetting
354 template <typename T>
355 bool IsBoolTrue(const T&)
356 {
357  return false;
358 }
359 template <>
360 bool IsBoolTrue<bool>(const bool& v)
361 {
362  return v;
363 }
364 
365 template <typename T>
366 PSRETURN GUI<T>::SetSetting(IGUIObject *pObject, const CStr& Setting,
367  const T &Value, const bool& SkipMessage)
368 {
369  ENSURE(pObject != NULL);
370 
371  if (!pObject->SettingExists(Setting))
372  {
373  LOGWARNING(L"setting %hs was not found on object %hs",
374  Setting.c_str(),
375  pObject->GetPresentableName().c_str());
377  }
378 
379 #ifndef NDEBUG
380  CheckType<T>(pObject, Setting);
381 #endif
382 
383  // Set value
384  *(T*)pObject->m_Settings[Setting].m_pSetting = Value;
385 
386  //
387  // Some settings needs special attention at change
388  //
389 
390  // If setting was "size", we need to re-cache itself and all children
391  if (Setting == "size")
392  {
393  RecurseObject(0, pObject, &IGUIObject::UpdateCachedSize);
394  }
395  else
396  if (Setting == "hidden")
397  {
398  // Hiding an object requires us to reset it and all children
399  if (IsBoolTrue(Value))
400  QueryResetting(pObject);
401  }
402 
403  if (!SkipMessage)
404  {
405  SGUIMessage msg(GUIM_SETTINGS_UPDATED, Setting);
406  HandleMessage(pObject, msg);
407  }
408 
409  return PSRETURN_OK;
410 }
411 
412 // Instantiate templated functions:
413 #define TYPE(T) \
414  template PSRETURN GUI<T>::GetSettingPointer(const IGUIObject *pObject, const CStr& Setting, T* &Value); \
415  template PSRETURN GUI<T>::GetSetting(const IGUIObject *pObject, const CStr& Setting, T &Value); \
416  template PSRETURN GUI<T>::SetSetting(IGUIObject *pObject, const CStr& Setting, const T &Value, const bool& SkipMessage);
417 #define GUITYPE_IGNORE_CGUISpriteInstance
418 #include "GUItypes.h"
419 #undef GUITYPE_IGNORE_CGUISpriteInstance
420 #undef TYPE
421 
422 // Don't instantiate GetSetting<CGUISpriteInstance> - this will cause linker errors if
423 // you attempt to retrieve a sprite using GetSetting, since that copies the sprite
424 // and will mess up the caching performed by DrawSprite. You have to use GetSettingPointer
425 // instead. (This is mainly useful to stop me accidentally using the wrong function.)
426 template PSRETURN GUI<CGUISpriteInstance>::GetSettingPointer(const IGUIObject *pObject, const CStr& Setting, CGUISpriteInstance* &Value);
427 template PSRETURN GUI<CGUISpriteInstance>::SetSetting(IGUIObject *pObject, const CStr& Setting, const CGUISpriteInstance &Value, const bool& SkipMessage);
void Translate(float x, float y, float z)
Definition: Matrix3D.cpp:172
bool __ParseString< CGUIList >(const CStrW &Value, CGUIList &Output)
Definition: GUIutil.cpp:245
virtual void ResetStates()
Reset internal state of this object.
Definition: IGUIObject.h:381
bool __ParseString< CGUIString >(const CStrW &Value, CGUIString &Output)
Definition: GUIutil.cpp:212
Made to represent a screen size, should in philosophy be made of unsigned ints, but for the sake of c...
Definition: Overlay.h:205
bool __ParseString< EVAlign >(const CStrW &Value, EVAlign &Output)
Definition: GUIutil.cpp:195
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
static IGUIObject * GetObjectPointer(CGUI &GUIinstance, const CStr &Object)
Get object pointer.
Definition: GUIutil.cpp:270
CMatrix3D GetDefaultGuiMatrix()
Definition: GUIutil.cpp:253
bool __ParseString< CRect >(const CStrW &Value, CRect &Output)
Definition: GUIutil.cpp:62
const PSRETURN PSRETURN_OK
Definition: Errors.h:103
Definition: Overlay.h:34
bool IsBoolTrue(const T &)
Definition: GUIutil.cpp:355
bool __ParseString< CColor >(const CStrW &Value, CColor &Output)
Definition: GUIutil.cpp:109
static void HandleMessage(IGUIObject *pObject, SGUIMessage &message)
Definition: GUIutil.cpp:291
int g_xres
Definition: Config.cpp:58
static PSRETURN GetSetting(const IGUIObject *pObject, const CStr &Setting, T &Value)
Retrieves a setting by name from object pointer.
Definition: GUIutil.cpp:344
bool __ParseString< CStr >(const CStrW &Value, CStr &Output)
Definition: GUIutil.cpp:221
bool __ParseString< CGUISpriteInstance >(const CStrW &Value, CGUISpriteInstance &Output)
Definition: GUIutil.cpp:238
bool IsBoolTrue< bool >(const bool &v)
Definition: GUIutil.cpp:360
bool GetArgFloat(size_t arg, float &ret)
bool GetPreDefinedColor(const CStr &name, CColor &output)
See CGUI::GetPreDefinedColor; applies to the currently active page.
Definition: GUIManager.cpp:231
Base settings, all objects possess these settings in their m_BaseSettings Instructions can be found i...
Definition: IGUIObject.h:140
bool __ParseString< CPos >(const CStrW &Value, CPos &Output)
Definition: GUIutil.cpp:156
CStr GetPresentableName() const
Definition: IGUIObject.cpp:536
CGUIManager * g_GUI
Definition: GUIManager.cpp:32
bool ParseString(const CStr8 &Value, float DefaultAlpha)
Definition: Overlay.cpp:30
The main object that represents a whole GUI page.
Definition: CGUI.h:97
#define LOGWARNING
Definition: CLogger.h:34
static void QueryResetting(IGUIObject *pObject)
Wrapper for ResetStates.
Definition: GUIutil.cpp:286
virtual void UpdateCachedSize()
All sizes are relative to resolution, and the calculation is not wanted in real time, therefore it is cached, update the cached size with this function.
Definition: IGUIObject.cpp:336
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
bool __ParseString< int >(const CStrW &Value, int &Output)
Definition: GUIutil.cpp:48
u32 PSRETURN
Definition: Errors.h:75
bool __ParseString< EAlign >(const CStrW &Value, EAlign &Output)
Definition: GUIutil.cpp:178
bool __ParseString< CSize >(const CStrW &Value, CSize &Output)
Definition: GUIutil.cpp:120
static bool ParseColor(const CStrW &Value, CColor &tOutput, float DefaultAlpha)
std::map< CStr, SGUISetting > m_Settings
Settings pool, all an object&#39;s settings are located here If a derived object has got more settings th...
Definition: IGUIObject.h:550
int g_yres
Definition: Config.cpp:58
void Scale(float x_scale, float y_scale, float z_scale)
Definition: Matrix3D.cpp:210
void SetIdentity()
Definition: Matrix3D.cpp:30
bool __ParseString< float >(const CStrW &Value, float &Output)
Definition: GUIutil.cpp:55
#define T(string_literal)
Definition: secure_crt.cpp:70
bool __ParseString< bool >(const CStrW &Value, bool &Output)
Definition: GUIutil.cpp:34
Made to represent screen positions and delta values.
Definition: Overlay.h:167
bool ParseString(const CParser &parser, const std::string &line)
Definition: Parser.cpp:325
bool __ParseString< CClientArea >(const CStrW &Value, CClientArea &Output)
Definition: GUIutil.cpp:90
static CParser & Get(const char *str)
Definition: Parser.cpp:1069
map_pObjects m_pAllObjects
Just pointers for fast name access, each object is really constructed within its parent for easy recu...
Definition: CGUI.h:642
bool m_ParseOK
Definition: Parser.h:194
bool SettingExists(const CStr &Setting) const
Checks if settings exists, only available for derived classes that has this set up, that&#39;s why the base class just returns false.
Definition: IGUIObject.cpp:239
const PSRETURN PSRETURN_GUI_InvalidSetting
bool __ParseString< CStrW >(const CStrW &Value, CStrW &Output)
Definition: GUIutil.cpp:229
static PSRETURN SetSetting(IGUIObject *pObject, const CStr &Setting, const T &Value, const bool &SkipMessage=false)
Sets a value by name using a real datatype as input.
Definition: GUIutil.cpp:366
EAlign
Definition: GUIbase.h:150
void SetOrtho(float l, float r, float b, float t, float n, float f)
Definition: Matrix3D.cpp:47
static void RecurseObject(int RR, IGUIObject *pObject, void_Object_pFunction_argT pFunc, const T &Argument)
If you want to call a IGUIObject-function on not just an object, but also on ALL of their children yo...
Definition: GUIutil.h:274
EVAlign
Definition: GUIbase.h:151
Message send to IGUIObject::HandleMessage() in order to give life to Objects manually with a derived ...
Definition: GUIbase.h:106
virtual void HandleMessage(SGUIMessage &Message)
This function is called with different messages for instance when the mouse enters the object...
Definition: IGUIObject.h:322
String class, substitute for CStr, but that parses the tags and builds up a list of all text that wil...
Definition: GUItext.h:176
static PSRETURN GetSettingPointer(const IGUIObject *pObject, const CStr &Setting, T *&Value)
Definition: GUIutil.cpp:317
Rectangle class used for screen rectangles.
Definition: Overlay.h:71
Client Area is a rectangle relative to a parent rectangle.
Definition: GUIbase.h:179