Pyrogenesis  trunk
GUIutil.h
Go to the documentation of this file.
1 /* Copyright (C) 2015 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 util
20 
21 --Overview--
22 
23  Contains help class GUI<>, which gives us templated
24  parameter to all functions within GUI.
25 
26 --More info--
27 
28  Check GUI.h
29 
30 */
31 
32 #ifndef INCLUDED_GUIUTIL
33 #define INCLUDED_GUIUTIL
34 
35 #include "CGUI.h"
36 #include "CGUISprite.h"
37 #include "GUIbase.h"
38 #include "IGUIObject.h"
39 
40 class CClientArea;
41 class CGUIString;
42 class CMatrix3D;
43 
44 template <typename T>
45 bool __ParseString(const CStrW& Value, T& tOutput);
46 
47 // Model-view-projection matrix with (0,0) in top-left of screen
49 
50 struct SGUIMessage;
51 
52 /**
53  * Base class to only the class GUI. This superclass is
54  * kind of a templateless extention of the class GUI.
55  * Used for other functions to friend with, because it
56  * can't friend with GUI since it's templated (at least
57  * not on all compilers we're using).
58  */
60 {
61 protected:
62  /// Get object pointer
63  static IGUIObject* GetObjectPointer(CGUI& GUIinstance, const CStr& Object);
64 
65  /// const version
66  static const IGUIObject* GetObjectPointer(const CGUI& GUIinstance, const CStr& Object);
67 
68  /// Wrapper for ResetStates
69  static void QueryResetting(IGUIObject* pObject);
70 
71  static void HandleMessage(IGUIObject* pObject, SGUIMessage& message);
72 };
73 
74 
75 #ifndef NDEBUG
76 // Used to ensure type-safety, sort of
77 template<typename T> void CheckType(const IGUIObject* obj, const CStr& setting);
78 #endif
79 
80 
81 /**
82  * Includes static functions that needs one template
83  * argument.
84  *
85  * int is only to please functions that doesn't even use T
86  * and are only within this class because it's convenient
87  */
88 template <typename T=int>
90 {
91  // Private functions further ahead
92  friend class CGUI;
93  friend class IGUIObject;
95 
96 public:
97 
98  // Like GetSetting (below), but doesn't make a copy of the value
99  // (so it can be modified later)
100  static PSRETURN GetSettingPointer(const IGUIObject* pObject, const CStr& Setting, T*& Value);
101 
102  /**
103  * Retrieves a setting by name from object pointer
104  *
105  * @param pObject Object pointer
106  * @param Setting Setting by name
107  * @param Value Stores value here, note type T!
108  */
109  static PSRETURN GetSetting(const IGUIObject* pObject, const CStr& Setting, T& Value);
110 
111  /**
112  * Sets a value by name using a real datatype as input.
113  *
114  * This is the official way of setting a setting, no other
115  * way should only cautiously be used!
116  *
117  * @param pObject Object pointer
118  * @param Setting Setting by name
119  * @param Value Sets value to this, note type T!
120  * @param SkipMessage Does not send a GUIM_SETTINGS_UPDATED if true
121  */
122  static PSRETURN SetSetting(IGUIObject* pObject, const CStr& Setting, const T& Value, const bool& SkipMessage = false);
123 
124  /**
125  * Retrieves a setting by settings name and object name
126  *
127  * @param GUIinstance GUI Object const ref
128  * @param Object Object name
129  * @param Setting Setting by name
130  * @param Value Stores value here, note type T!
131  */
132  static PSRETURN GetSetting(const CGUI& GUIinstance, const CStr& Object, const CStr& Setting, T& Value)
133  {
134  if (!GUIinstance.ObjectExists(Object))
136 
137  // Retrieve pointer and call sibling function
138  const IGUIObject* pObject = GetObjectPointer(GUIinstance, Object);
139 
140  return GetSetting(pObject, Setting, Value);
141  }
142 
143  /**
144  * Sets a value by setting and object name using a real
145  * datatype as input
146  *
147  * This is just a wrapper so that we can type the object name
148  * and not input the actual pointer.
149  *
150  * @param GUIinstance GUI Object, reference since we'll be changing values
151  * @param Object Object name
152  * @param Setting Setting by name
153  * @param Value Sets value to this, note type T!
154  * @param SkipMessage Does not send a GUIM_SETTINGS_UPDATED if true
155  */
156  static PSRETURN SetSetting(CGUI& GUIinstance, const CStr& Object, const CStr& Setting, const T& Value, const bool& SkipMessage = false)
157  {
158  if (!GUIinstance.ObjectExists(Object))
160 
161  // Retrieve pointer and call sibling function
162 
163  // Important, we don't want to use this T, we want
164  // to use the standard T, since that will be the
165  // one with the friend relationship
166  IGUIObject* pObject = GetObjectPointer(GUIinstance, Object);
167 
168  return SetSetting(pObject, Setting, Value, SkipMessage);
169  }
170 
171  /**
172  * This will return the value of the first sprite if it's not null,
173  * if it is null, it will return the value of the second sprite, if
174  * that one is null, then null it is.
175  *
176  * @param prim Primary sprite that should be used
177  * @param sec Secondary sprite if Primary should fail
178  * @return Resulting string
179  */
181  {
182  return (prim.IsEmpty() ? sec : prim);
183  }
184 
185  /**
186  * Same principle as FallBackSprite
187  *
188  * @param prim Primary color that should be used
189  * @param sec Secondary color if Primary should fail
190  * @return Resulting color
191  * @see FallBackSprite
192  */
193  static CColor FallBackColor(const CColor& prim, const CColor& sec)
194  {
195  // CColor() == null.
196  return ((prim!=CColor())?(prim):(sec));
197  }
198 
199  /**
200  * Sets a value by setting and object name using a real
201  * datatype as input.
202  *
203  * This is just a wrapper for __ParseString() which really
204  * works the magic.
205  *
206  * @param Value The value in string form, like "0 0 100% 100%"
207  * @param tOutput Parsed value of type T
208  * @return True at success.
209  *
210  * @see __ParseString()
211  */
212  static bool ParseString(const CStrW& Value, T& tOutput)
213  {
214  return __ParseString<T>(Value, tOutput);
215  }
216 
217  static bool ParseColor(const CStrW& Value, CColor& tOutput, int DefaultAlpha);
218 
219 private:
220 
221  // templated typedef of function pointer
222  typedef void (IGUIObject::*void_Object_pFunction_argT)(const T& arg);
223  typedef void (IGUIObject::*void_Object_pFunction_argRefT)(T& arg);
224  typedef void (IGUIObject::*void_Object_pFunction)();
225 
226  /**
227  * If you want to call a IGUIObject-function
228  * on not just an object, but also on ALL of their children
229  * you want to use this recursion system.
230  * It recurses an object calling a function on itself
231  * and all children (and so forth).
232  *
233  * <b>Restrictions:</b>\n
234  * You can also set restrictions, so that if the recursion
235  * reaches an objects with certain setup, it just doesn't
236  * call the function on the object, nor it's children for
237  * that matter. i.e. it cuts that object off from the
238  * recursion tree. What setups that can cause restrictions
239  * are hardcoded and specific. Check out the defines
240  * GUIRR_* for all different setups.
241  *
242  * Error reports are either logged or thrown out of RecurseObject.
243  * Always use it with try/catch!
244  *
245  * @param RR Recurse Restrictions, set to 0 if no restrictions
246  * @param pObject Top object, this is where the iteration starts
247  * @param pFunc Function to recurse
248  * @param Argument Argument for pFunc of type T
249  * @throws PSERROR Depends on what pFunc might throw. PSERROR is standard.
250  * Itself doesn't throw anything.
251  */
252  static void RecurseObject(int RR, IGUIObject* pObject, void_Object_pFunction_argT pFunc, const T& Argument)
253  {
254  // TODO Gee: Don't run this for the base object.
255  if (CheckIfRestricted(RR, pObject))
256  return;
257 
258  (pObject->*pFunc)(Argument);
259 
260  // Iterate children
261  for (IGUIObject* const& obj : *pObject)
262  RecurseObject(RR, obj, pFunc, Argument);
263  }
264 
265  /**
266  * Argument is reference.
267  *
268  * @see RecurseObject()
269  */
270  static void RecurseObject(int RR, IGUIObject* pObject, void_Object_pFunction_argRefT pFunc, T& Argument)
271  {
272  if (CheckIfRestricted(RR, pObject))
273  return;
274 
275  (pObject->*pFunc)(Argument);
276 
277  // Iterate children
278  for (IGUIObject* const& obj : *pObject)
279  RecurseObject(RR, obj, pFunc, Argument);
280  }
281 
282  /**
283  * With no argument.
284  *
285  * @see RecurseObject()
286  */
287  static void RecurseObject(int RR, IGUIObject* pObject, void_Object_pFunction pFunc)
288  {
289  if (CheckIfRestricted(RR, pObject))
290  return;
291 
292  (pObject->*pFunc)();
293 
294  // Iterate children
295  for (IGUIObject* const& obj : *pObject)
296  RecurseObject(RR, obj, pFunc);
297  }
298 
299 private:
300  /**
301  * Checks restrictions for the iteration, for instance if
302  * you tell the recursor to avoid all hidden objects, it
303  * will, and this function checks a certain object's
304  * restriction values.
305  *
306  * @param RR What kind of restriction, for instance hidden or disabled
307  * @param pObject Object
308  * @return true if restricted
309  */
310  static bool CheckIfRestricted(int RR, IGUIObject* pObject)
311  {
312  // Statically initialise some strings, so we don't have to do
313  // lots of allocation every time this function is called
314  static const CStr strHidden("hidden");
315  static const CStr strEnabled("enabled");
316  static const CStr strGhost("ghost");
317 
318  if (RR & GUIRR_HIDDEN)
319  {
320  bool hidden = true;
321  GUI<bool>::GetSetting(pObject, strHidden, hidden);
322 
323  if (hidden)
324  return true;
325  }
326  if (RR & GUIRR_DISABLED)
327  {
328  bool enabled = false;
329  GUI<bool>::GetSetting(pObject, strEnabled, enabled);
330 
331  if (!enabled)
332  return true;
333  }
334  if (RR & GUIRR_GHOST)
335  {
336  bool ghost = true;
337  GUI<bool>::GetSetting(pObject, strGhost, ghost);
338 
339  if (ghost)
340  return true;
341  }
342 
343  // false means not restricted
344  return false;
345  }
346 };
347 
348 #endif // INCLUDED_GUIUTIL
bool ObjectExists(const CStr &Name) const
Checks if object exists and return true or false accordingly.
Definition: CGUI.cpp:432
Definition: GUIbase.h:129
static IGUIObject * GetObjectPointer(CGUI &GUIinstance, const CStr &Object)
Get object pointer.
Definition: GUIutil.cpp:284
Base class to only the class GUI.
Definition: GUIutil.h:59
static PSRETURN GetSetting(const CGUI &GUIinstance, const CStr &Object, const CStr &Setting, T &Value)
Retrieves a setting by settings name and object name.
Definition: GUIutil.h:132
Definition: Shapes.h:36
static void HandleMessage(IGUIObject *pObject, SGUIMessage &message)
Definition: GUIutil.cpp:299
static bool ParseString(const CStrW &Value, T &tOutput)
Sets a value by setting and object name using a real datatype as input.
Definition: GUIutil.h:212
static PSRETURN GetSetting(const IGUIObject *pObject, const CStr &Setting, T &Value)
Retrieves a setting by name from object pointer.
Definition: GUIutil.cpp:352
const PSRETURN PSRETURN_GUI_NullObjectProvided
static void RecurseObject(int RR, IGUIObject *pObject, void_Object_pFunction pFunc)
With no argument.
Definition: GUIutil.h:287
static CColor FallBackColor(const CColor &prim, const CColor &sec)
Same principle as FallBackSprite.
Definition: GUIutil.h:193
Base settings, all objects possess these settings in their m_BaseSettings Instructions can be found i...
Definition: IGUIObject.h:117
bool __ParseString(const CStrW &Value, T &tOutput)
static const CGUISpriteInstance & FallBackSprite(const CGUISpriteInstance &prim, const CGUISpriteInstance &sec)
This will return the value of the first sprite if it&#39;s not null, if it is null, it will return the va...
Definition: GUIutil.h:180
Config::Object_type Object
Definition: json_spirit_value.h:183
The main object that represents a whole GUI page.
Definition: CGUI.h:75
static void QueryResetting(IGUIObject *pObject)
Wrapper for ResetStates.
Definition: GUIutil.cpp:294
Definition: Matrix3D.h:33
Includes static functions that needs one template argument.
Definition: GUIutil.h:89
Config::Value_type Value
Definition: json_spirit_value.h:181
u32 PSRETURN
Definition: Errors.h:75
#define T(string_literal)
Definition: secure_crt.cpp:76
static PSRETURN SetSetting(CGUI &GUIinstance, const CStr &Object, const CStr &Setting, const T &Value, const bool &SkipMessage=false)
Sets a value by setting and object name using a real datatype as input.
Definition: GUIutil.h:156
bool IsEmpty() const
Definition: CGUISprite.cpp:50
CMatrix3D GetDefaultGuiMatrix()
Definition: GUIutil.cpp:264
Definition: CGUISprite.h:158
Definition: GUIbase.h:130
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:252
Message send to IGUIObject::HandleMessage() in order to give life to Objects manually with a derived ...
Definition: GUIbase.h:91
static bool CheckIfRestricted(int RR, IGUIObject *pObject)
Checks restrictions for the iteration, for instance if you tell the recursor to avoid all hidden obje...
Definition: GUIutil.h:310
Definition: GUIbase.h:131
static void RecurseObject(int RR, IGUIObject *pObject, void_Object_pFunction_argRefT pFunc, T &Argument)
Argument is reference.
Definition: GUIutil.h:270
String class, substitute for CStr, but that parses the tags and builds up a list of all text that wil...
Definition: GUItext.h:169
Client Area is a rectangle relative to a parent rectangle.
Definition: GUIbase.h:164
void CheckType(const IGUIObject *obj, const CStr &setting)