Crazy Eddie's GUI System  0.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
PropertyHelper.h
1 /***********************************************************************
2  filename: CEGUIPropertyHelper.h
3  created: 21/11/2010
4  author: Martin Preisler (reworked from code by Paul D Turner)
5 
6  purpose: Interface to the PropertyHelper class
7 *************************************************************************/
8 /***************************************************************************
9  * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining
12  * a copy of this software and associated documentation files (the
13  * "Software"), to deal in the Software without restriction, including
14  * without limitation the rights to use, copy, modify, merge, publish,
15  * distribute, sublicense, and/or sell copies of the Software, and to
16  * permit persons to whom the Software is furnished to do so, subject to
17  * the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be
20  * included in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  ***************************************************************************/
30 #ifndef _CEGUIPropertyHelper_h_
31 #define _CEGUIPropertyHelper_h_
32 
33 #include "CEGUI/String.h"
34 #include "CEGUI/Size.h"
35 #include "CEGUI/Vector.h"
36 #include "CEGUI/Quaternion.h"
37 #include "CEGUI/Colour.h"
38 #include "CEGUI/ColourRect.h"
39 #include "CEGUI/UDim.h"
40 #include "CEGUI/Rect.h"
41 
42 
43 #include <cstdio>
44 
45 #include <sstream>
46 
47 #ifdef _MSC_VER
48 #define snprintf _snprintf
49 #endif
50 
51 // Start of CEGUI namespace section
52 namespace CEGUI
53 {
54 
55 
66 template<typename T>
67 class PropertyHelper;
68 
69 // this redirects PropertyHelper<const T> to PropertyHelper<T>
70 template<typename T>
71 class PropertyHelper<const T>
72 {
73 public:
74  typedef typename PropertyHelper<T>::return_type return_type;
75  typedef typename PropertyHelper<T>::safe_method_return_type safe_method_return_type;
76  typedef typename PropertyHelper<T>::pass_type pass_type;
77  typedef typename PropertyHelper<T>::string_return_type string_return_type;
78 
79  static inline const String& getDataTypeName()
80  {
82  }
83 
84  static inline return_type fromString(const String& str)
85  {
87  }
88 
89  static inline String toString(pass_type val)
90  {
91  return PropertyHelper<T>::toString(val);
92  }
93 };
94 
95 // this redirects PropertyHelper<const T&> to PropertyHelper<T>
96 template<typename T>
97 class PropertyHelper<const T&>
98 {
99 public:
100  typedef typename PropertyHelper<T>::return_type return_type;
101  typedef typename PropertyHelper<T>::safe_method_return_type safe_method_return_type;
102  typedef typename PropertyHelper<T>::pass_type pass_type;
103  typedef typename PropertyHelper<T>::string_return_type string_return_type;
104 
105  static inline const String& getDataTypeName()
106  {
108  }
109 
110  static inline return_type fromString(const String& str)
111  {
112  return PropertyHelper<T>::fromString(str);
113  }
114 
115  static inline String toString(pass_type val)
116  {
117  return PropertyHelper<T>::toString(val);
118  }
119 };
120 
121 // this redirects PropertyHelper<const T*> to PropertyHelper<T*>
122 template<typename T>
123 class PropertyHelper<const T*>
124 {
125 public:
126  typedef typename PropertyHelper<T*>::return_type return_type;
127  typedef typename PropertyHelper<T*>::safe_method_return_type safe_method_return_type;
128  typedef typename PropertyHelper<T*>::pass_type pass_type;
129  typedef typename PropertyHelper<T*>::string_return_type string_return_type;
130 
131  static inline const String& getDataTypeName()
132  {
134  }
135 
136  static inline return_type fromString(const String& str)
137  {
138  return PropertyHelper<T*>::fromString(str);
139  }
140 
141  static inline String toString(pass_type val)
142  {
143  return PropertyHelper<T*>::toString(val);
144  }
145 };
146 
147 template<>
149 {
150 public:
151  typedef const String& return_type;
153  typedef const String& pass_type;
154  typedef const String& string_return_type;
155 
156  static const String& getDataTypeName()
157  {
158  static String type("String");
159 
160  return type;
161  }
162 
163  static inline return_type fromString(const String& str)
164  {
165  return str;
166  }
167 
168  static inline string_return_type toString(pass_type val)
169  {
170  return val;
171  }
172 };
173 
174 template<>
175 class PropertyHelper<float>
176 {
177 public:
178  typedef float return_type;
179  typedef return_type safe_method_return_type;
180  typedef const float pass_type;
181  typedef String string_return_type;
182 
183  static const String& getDataTypeName()
184  {
185  static String type("float");
186 
187  return type;
188  }
189 
190  static inline return_type fromString(const String& str)
191  {
192  float val = 0;
193  sscanf(str.c_str(), " %g", &val);
194 
195  return val;
196  }
197 
198  static inline string_return_type toString(pass_type val)
199  {
200  char buff[64];
201  snprintf(buff, sizeof(buff), "%g", val);
202 
203  return String(buff);
204  }
205 };
206 template<>
207 class PropertyHelper<double>
208 {
209 public:
210  typedef double return_type;
211  typedef return_type safe_method_return_type;
212  typedef const double pass_type;
213  typedef String string_return_type;
214 
215  static const String& getDataTypeName()
216  {
217  static String type("double");
218 
219  return type;
220  }
221 
222  static inline return_type fromString(const String& str)
223  {
224  double val = 0;
225  sscanf(str.c_str(), " %lg", &val);
226 
227  return val;
228  }
229 
230  static inline string_return_type toString(pass_type val)
231  {
232  char buff[64];
233  snprintf(buff, sizeof(buff), "%g", val);
234 
235  return String(buff);
236  }
237 };
238 
239 template<>
240 class PropertyHelper<int>
241 {
242 public:
243  typedef int return_type;
244  typedef return_type safe_method_return_type;
245  typedef const int pass_type;
246  typedef String string_return_type;
247 
248  static const String& getDataTypeName()
249  {
250  static String type("int");
251 
252  return type;
253  }
254 
255  static inline return_type fromString(const String& str)
256  {
257  int val = 0;
258  sscanf(str.c_str(), " %d", &val);
259 
260  return val;
261  }
262 
263  static inline string_return_type toString(pass_type val)
264  {
265  char buff[64];
266  snprintf(buff, sizeof(buff), "%d", val);
267 
268  return String(buff);
269  }
270 };
271 
272 template<>
273 class PropertyHelper<uint>
274 {
275 public:
276  typedef uint return_type;
277  typedef return_type safe_method_return_type;
278  typedef const uint pass_type;
279  typedef String string_return_type;
280 
281  static const String& getDataTypeName()
282  {
283  static String type("uint");
284 
285  return type;
286  }
287 
288  static return_type fromString(const String& str)
289  {
290  uint val = 0;
291  sscanf(str.c_str(), " %u", &val);
292 
293  return val;
294  }
295 
296  static string_return_type toString(pass_type val)
297  {
298  char buff[64];
299  snprintf(buff, sizeof(buff), "%u", val);
300 
301  return String(buff);
302  }
303 };
304 
305 template<>
306 class PropertyHelper<uint64>
307 {
308 public:
309  typedef uint64 return_type;
310  typedef return_type safe_method_return_type;
311  typedef const uint64 pass_type;
312  typedef String string_return_type;
313 
314  static const String& getDataTypeName()
315  {
316  static String type("uint64");
317 
318  return type;
319  }
320 
321  static return_type fromString(const String& str)
322  {
323  uint64 val = 0;
324  sscanf(str.c_str(), " %llu", &val);
325 
326  return val;
327  }
328 
329  static string_return_type toString(pass_type val)
330  {
331  char buff[64];
332  snprintf(buff, sizeof(buff), "%llu", val);
333 
334  return String(buff);
335  }
336 };
337 
338 #if CEGUI_STRING_CLASS != CEGUI_STRING_CLASS_UNICODE
339 
340 template<>
341 class PropertyHelper<String::value_type>
342 {
343 public:
344  typedef String::value_type return_type;
345  typedef return_type safe_method_return_type;
346  typedef const String::value_type pass_type;
347  typedef String string_return_type;
348 
349  static const String& getDataTypeName()
350  {
351  static String type("char");
352 
353  return type;
354  }
355 
356  static return_type fromString(const String& str)
357  {
358  return str[0];
359  }
360 
361  static string_return_type toString(pass_type val)
362  {
363  return String("") + val;
364  }
365 };
366 
367 #endif
368 
369 template<>
370 class PropertyHelper<unsigned long>
371 {
372 public:
373  typedef unsigned long return_type;
374  typedef return_type safe_method_return_type;
375  typedef const unsigned long pass_type;
376  typedef String string_return_type;
377 
378  static const String& getDataTypeName()
379  {
380  static String type("unsigned long");
381 
382  return type;
383  }
384 
385  static return_type fromString(const String& str)
386  {
387  unsigned long val = 0;
388  sscanf(str.c_str(), " %lu", &val);
389 
390  return val;
391  }
392 
393  static string_return_type toString(pass_type val)
394  {
395  char buff[64];
396  snprintf(buff, sizeof(buff), "%lu", val);
397 
398  return String(buff);
399  }
400 };
401 
402 template<>
403 class PropertyHelper<bool>
404 {
405 public:
406  typedef bool return_type;
407  typedef return_type safe_method_return_type;
408  typedef const bool pass_type;
409  typedef const String& string_return_type;
410 
411  static const String& getDataTypeName()
412  {
413  static String type("bool");
414 
415  return type;
416  }
417 
418  static return_type fromString(const String& str)
419  {
420  return (str == "True" || str == "true");
421  }
422 
423  static string_return_type toString(pass_type val)
424  {
425  // yeah I am that awesome ;-D
426 
427  static String True("True");
428  static String False("False");
429 
430  return val ? True : False;
431  }
432 };
433 
434 template<>
436 {
437 public:
438  typedef AspectMode return_type;
440  typedef AspectMode pass_type;
441  typedef String string_return_type;
442 
443  static const String& getDataTypeName()
444  {
445  static String type("AspectMode");
446 
447  return type;
448  }
449 
450  static return_type fromString(const String& str)
451  {
452  if (str == "Shrink")
453  {
454  return AM_SHRINK;
455  }
456  else if (str == "Expand")
457  {
458  return AM_EXPAND;
459  }
460  else
461  {
462  return AM_IGNORE;
463  }
464  }
465 
466  static string_return_type toString(pass_type val)
467  {
468  if (val == AM_IGNORE)
469  {
470  return "Ignore";
471  }
472  else if (val == AM_SHRINK)
473  {
474  return "Shrink";
475  }
476  else if (val == AM_EXPAND)
477  {
478  return "Expand";
479  }
480  else
481  {
482  assert(false && "Invalid aspect mode");
483  return "Ignore";
484  }
485  }
486 };
487 
488 template<>
490 {
491 public:
492  typedef Sizef return_type;
494  typedef const Sizef& pass_type;
495  typedef String string_return_type;
496 
497  static const String& getDataTypeName()
498  {
499  static String type("Sizef");
500 
501  return type;
502  }
503 
504  static return_type fromString(const String& str)
505  {
506  Sizef val(0, 0);
507  sscanf(str.c_str(), " w:%g h:%g", &val.d_width, &val.d_height);
508 
509  return val;
510  }
511 
512  static string_return_type toString(pass_type val)
513  {
514  char buff[128];
515  snprintf(buff, sizeof(buff), "w:%g h:%g", val.d_width, val.d_height);
516 
517  return String(buff);
518  }
519 };
520 
521 template<>
523 {
524 public:
525  typedef Vector2f return_type;
527  typedef const Vector2f& pass_type;
528  typedef String string_return_type;
529 
530  static const String& getDataTypeName()
531  {
532  static String type("Vector2f");
533 
534  return type;
535  }
536 
537  static return_type fromString(const String& str)
538  {
539  Vector2f val(0, 0) ;
540  sscanf(str.c_str(), " x:%g y:%g", &val.d_x, &val.d_y);
541 
542  return val;
543  }
544 
545  static string_return_type toString(pass_type val)
546  {
547  char buff[128];
548  snprintf(buff, sizeof(buff), "x:%g y:%g", val.d_x, val.d_y);
549 
550  return String(buff);
551  }
552 };
553 
554 template<>
556 {
557 public:
558  typedef Vector3f return_type;
560  typedef const Vector3f& pass_type;
561  typedef String string_return_type;
562 
563  static const String& getDataTypeName()
564  {
565  static String type("Vector3f");
566 
567  return type;
568  }
569 
570  static return_type fromString(const String& str)
571  {
572  Vector3f val(0, 0, 0);
573  sscanf(str.c_str(), " x:%g y:%g z:%g", &val.d_x, &val.d_y, &val.d_z);
574 
575  return val;
576  }
577 
578  static string_return_type toString(pass_type val)
579  {
580  char buff[128];
581  snprintf(buff, sizeof(buff), "x:%g y:%g z:%g", val.d_x, val.d_y, val.d_z);
582 
583  return String(buff);
584  }
585 };
586 
587 template<>
589 {
590 public:
591  typedef Quaternion return_type;
593  typedef const Quaternion& pass_type;
594  typedef String string_return_type;
595 
596  static const String& getDataTypeName()
597  {
598  static String type("Quaternion");
599 
600  return type;
601  }
602 
603  static return_type fromString(const String& str)
604  {
605  if (strchr(str.c_str(), 'w') || strchr(str.c_str(), 'W'))
606  {
607  Quaternion val(1, 0, 0, 0);
608  sscanf(str.c_str(), " w:%g x:%g y:%g z:%g", &val.d_w, &val.d_x, &val.d_y, &val.d_z);
609 
610  return val;
611  }
612  else
613  {
614  float x, y, z;
615  sscanf(str.c_str(), " x:%g y:%g z:%g", &x, &y, &z);
616  return Quaternion::eulerAnglesDegrees(x, y, z);
617  }
618  }
619 
620  static string_return_type toString(pass_type val)
621  {
622  char buff[128];
623  snprintf(buff, sizeof(buff), "w:%g x:%g y:%g z:%g", val.d_w, val.d_x, val.d_y, val.d_z);
624 
625  return String(buff);
626  }
627 };
628 
629 template<>
631 {
632 public:
633  typedef Rectf return_type;
635  typedef const Rectf& pass_type;
636  typedef String string_return_type;
637 
638  static const String& getDataTypeName()
639  {
640  static String type("Rectf");
641 
642  return type;
643  }
644 
645  static return_type fromString(const String& str)
646  {
647  Rectf val(0, 0, 0, 0);
648  sscanf(str.c_str(), " l:%g t:%g r:%g b:%g", &val.d_min.d_x, &val.d_min.d_y, &val.d_max.d_x, &val.d_max.d_y);
649 
650  return val;
651  }
652 
653  static string_return_type toString(pass_type val)
654  {
655  char buff[256];
656  snprintf(buff, sizeof(buff), "l:%g t:%g r:%g b:%g",
657  val.d_min.d_x, val.d_min.d_y, val.d_max.d_x, val.d_max.d_y);
658 
659  return String(buff);
660  }
661 };
662 
663 template<>
664 class CEGUIEXPORT PropertyHelper<Image*>
665 {
666 public:
667  typedef const Image* return_type;
668  typedef return_type safe_method_return_type;
669  typedef const Image* const pass_type;
670  typedef String string_return_type;
671 
672  static const String& getDataTypeName()
673  {
674  static String type("Image");
675 
676  return type;
677  }
678 
679  static return_type fromString(const String& str);
680 
681  static string_return_type toString(pass_type val);
682 };
683 
684 template<>
686 {
687 public:
688  typedef Colour return_type;
690  typedef const Colour& pass_type;
691  typedef String string_return_type;
692 
693  static const String& getDataTypeName()
694  {
695  static String type("Colour");
696 
697  return type;
698  }
699 
700  static return_type fromString(const String& str)
701  {
702  argb_t val = 0xFF000000;
703  sscanf(str.c_str(), " %8X", &val);
704 
705  return Colour(val);
706  }
707 
708  static string_return_type toString(pass_type val)
709  {
710  char buff[16];
711  sprintf(buff, "%.8X", val.getARGB());
712 
713  return String(buff);
714  }
715 };
716 
717 template<>
719 {
720 public:
721  typedef ColourRect return_type;
723  typedef const ColourRect& pass_type;
724  typedef String string_return_type;
725 
726  static const String& getDataTypeName()
727  {
728  static String type("ColourRect");
729 
730  return type;
731  }
732 
733  static return_type fromString(const String& str)
734  {
735  if (str.length() == 8)
736  {
737  argb_t all = 0xFF000000;
738  sscanf(str.c_str(), "%8X", &all);
739  return ColourRect(all);
740  }
741 
742  argb_t topLeft = 0xFF000000, topRight = 0xFF000000, bottomLeft = 0xFF000000, bottomRight = 0xFF000000;
743  sscanf(str.c_str(), "tl:%8X tr:%8X bl:%8X br:%8X", &topLeft, &topRight, &bottomLeft, &bottomRight);
744 
745  return ColourRect(topLeft, topRight, bottomLeft, bottomRight);
746  }
747 
748  static string_return_type toString(pass_type val)
749  {
750  char buff[64];
751  sprintf(buff, "tl:%.8X tr:%.8X bl:%.8X br:%.8X", val.d_top_left.getARGB(), val.d_top_right.getARGB(), val.d_bottom_left.getARGB(), val.d_bottom_right.getARGB());
752 
753  return String(buff);
754  }
755 };
756 
757 template<>
759 {
760 public:
761  typedef UDim return_type;
763  typedef const UDim& pass_type;
764  typedef String string_return_type;
765 
766  static const String& getDataTypeName()
767  {
768  static String type("UDim");
769 
770  return type;
771  }
772 
773  static return_type fromString(const String& str)
774  {
775  UDim ud;
776  sscanf(str.c_str(), " { %g , %g }", &ud.d_scale, &ud.d_offset);
777 
778  return ud;
779  }
780 
781  static string_return_type toString(pass_type val)
782  {
783  char buff[128];
784  snprintf(buff, sizeof(buff), "{%g,%g}", val.d_scale, val.d_offset);
785 
786  return String(buff);
787  }
788 };
789 
790 template<>
792 {
793 public:
794  typedef UVector2 return_type;
796  typedef const UVector2& pass_type;
797  typedef String string_return_type;
798 
799  static const String& getDataTypeName()
800  {
801  static String type("UVector2");
802 
803  return type;
804  }
805 
806  static return_type fromString(const String& str)
807  {
808  UVector2 uv;
809  sscanf(str.c_str(), " { { %g , %g } , { %g , %g } }",
810  &uv.d_x.d_scale, &uv.d_x.d_offset,
811  &uv.d_y.d_scale, &uv.d_y.d_offset);
812 
813  return uv;
814  }
815 
816  static string_return_type toString(pass_type val)
817  {
818  char buff[256];
819  snprintf(buff, sizeof(buff), "{{%g,%g},{%g,%g}}",
820  val.d_x.d_scale, val.d_x.d_offset, val.d_y.d_scale, val.d_y.d_offset);
821 
822  return String(buff);
823  }
824 };
825 
826 template<>
828 {
829 public:
830  typedef USize return_type;
832  typedef const USize& pass_type;
833  typedef String string_return_type;
834 
835  static const String& getDataTypeName()
836  {
837  static String type("USize");
838 
839  return type;
840  }
841 
842  static return_type fromString(const String& str)
843  {
844  USize uv;
845  sscanf(str.c_str(), " { { %g , %g } , { %g , %g } }",
846  &uv.d_width.d_scale, &uv.d_width.d_offset,
847  &uv.d_height.d_scale, &uv.d_height.d_offset);
848 
849  return uv;
850  }
851 
852  static string_return_type toString(pass_type val)
853  {
854  char buff[256];
855  snprintf(buff, sizeof(buff), "{{%g,%g},{%g,%g}}",
856  val.d_width.d_scale, val.d_width.d_offset, val.d_height.d_scale, val.d_height.d_offset);
857 
858  return String(buff);
859  }
860 };
861 
862 template<>
864 {
865 public:
866  typedef URect return_type;
868  typedef const URect& pass_type;
869  typedef String string_return_type;
870 
871  static const String& getDataTypeName()
872  {
873  static String type("URect");
874 
875  return type;
876  }
877 
878  static return_type fromString(const String& str)
879  {
880  URect ur;
881  sscanf(
882  str.c_str(),
883  " { { %g , %g } , { %g , %g } , { %g , %g } , { %g , %g } }",
884  &ur.d_min.d_x.d_scale, &ur.d_min.d_x.d_offset,
885  &ur.d_min.d_y.d_scale, &ur.d_min.d_y.d_offset,
886  &ur.d_max.d_x.d_scale, &ur.d_max.d_x.d_offset,
887  &ur.d_max.d_y.d_scale, &ur.d_max.d_y.d_offset
888  );
889 
890  return ur;
891  }
892 
893  static string_return_type toString(pass_type val)
894  {
895  char buff[512];
896  snprintf(buff, sizeof(buff), "{{%g,%g},{%g,%g},{%g,%g},{%g,%g}}",
897  val.d_min.d_x.d_scale, val.d_min.d_x.d_offset,
898  val.d_min.d_y.d_scale, val.d_min.d_y.d_offset,
899  val.d_max.d_x.d_scale, val.d_max.d_x.d_offset,
900  val.d_max.d_y.d_scale, val.d_max.d_y.d_offset);
901 
902  return String(buff);
903  }
904 };
905 
906 template<>
908 {
909 public:
910  typedef UBox return_type;
912  typedef const UBox& pass_type;
913  typedef String string_return_type;
914 
915  static const String& getDataTypeName()
916  {
917  static String type("UBox");
918 
919  return type;
920  }
921 
922  static return_type fromString(const String& str)
923  {
924  UBox ret;
925  sscanf(
926  str.c_str(),
927  " { top: { %g , %g } , left: { %g , %g } , bottom: { %g , %g } , right: { %g , %g } }",
928  &ret.d_top.d_scale, &ret.d_top.d_offset,
929  &ret.d_left.d_scale, &ret.d_left.d_offset,
930  &ret.d_bottom.d_scale, &ret.d_bottom.d_offset,
931  &ret.d_right.d_scale, &ret.d_right.d_offset
932  );
933 
934  return ret;
935  }
936 
937  static string_return_type toString(pass_type val)
938  {
939  char buff[512];
940  snprintf(buff, sizeof(buff), "{top:{%g,%g},left:{%g,%g},bottom:{%g,%g},right:{%g,%g}}",
941  val.d_top.d_scale, val.d_top.d_offset,
942  val.d_left.d_scale, val.d_left.d_offset,
943  val.d_bottom.d_scale, val.d_bottom.d_offset,
944  val.d_right.d_scale, val.d_right.d_offset);
945 
946  return String(buff);
947  }
948 };
949 
950 
951 template<>
952 class CEGUIEXPORT PropertyHelper<Font*>
953 {
954 public:
955  typedef const Font* return_type;
956  typedef return_type safe_method_return_type;
957  typedef const Font* const pass_type;
958  typedef String string_return_type;
959 
960  static const String& getDataTypeName()
961  {
962  static String type("Font");
963 
964  return type;
965  }
966 
967  static return_type fromString(const String& str);
968  static string_return_type toString(pass_type val);
969 };
970 
971 } // End of CEGUI namespace section
972 
973 #endif // end of guard _CEGUIPropertyHelper_h_