Crazy Eddie's GUI System  0.8.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Element.h
1 /***********************************************************************
2  created: 18/8/2011
3  author: Martin Preisler
4 
5  purpose: Defines a class representing an item in a graph
6  (deals with relative positions, relative dimensions, ...)
7 *************************************************************************/
8 /***************************************************************************
9  * Copyright (C) 2004 - 2011 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 
31 #ifndef _CEGUIElement_h_
32 #define _CEGUIElement_h_
33 
34 #include "CEGUI/Base.h"
35 #include "CEGUI/PropertySet.h"
36 #include "CEGUI/EventSet.h"
37 #include "CEGUI/EventArgs.h"
38 
39 #if defined(_MSC_VER)
40 # pragma warning(push)
41 # pragma warning(disable : 4251)
42 #endif
43 
44 namespace CEGUI
45 {
46 
53 {
69 };
70 
71 template<>
73 {
74 public:
78  typedef String string_return_type;
79 
80  static const String& getDataTypeName()
81  {
82  static String type("HorizontalAlignment");
83 
84  return type;
85  }
86 
87  static return_type fromString(const String& str)
88  {
89  if (str == "Centre")
90  {
91  return HA_CENTRE;
92  }
93  else if (str == "Right")
94  {
95  return HA_RIGHT;
96  }
97  else
98  {
99  return HA_LEFT;
100  }
101  }
102 
103  static string_return_type toString(pass_type val)
104  {
105  if (val == HA_CENTRE)
106  {
107  return "Centre";
108  }
109  else if (val == HA_RIGHT)
110  {
111  return "Right";
112  }
113  else if (val == HA_LEFT)
114  {
115  return "Left";
116  }
117  else
118  {
119  assert(false && "Invalid horizontal alignment");
120  return "Centre";
121  }
122  }
123 };
124 
131 {
147 };
148 
149 template<>
151 {
152 public:
156  typedef String string_return_type;
157 
158  static const String& getDataTypeName()
159  {
160  static String type("VerticalAlignment");
161 
162  return type;
163  }
164 
165  static return_type fromString(const String& str)
166  {
167  if (str == "Centre")
168  {
169  return VA_CENTRE;
170  }
171  else if (str == "Bottom")
172  {
173  return VA_BOTTOM;
174  }
175  else
176  {
177  return VA_TOP;
178  }
179  }
180 
181  static string_return_type toString(pass_type val)
182  {
183  if (val == VA_CENTRE)
184  {
185  return "Centre";
186  }
187  else if (val == VA_BOTTOM)
188  {
189  return "Bottom";
190  }
191  else if (val == VA_TOP)
192  {
193  return "Top";
194  }
195  else
196  {
197  assert(false && "Invalid vertical alignment");
198  return "Centre";
199  }
200  }
201 };
202 
210 class CEGUIEXPORT ElementEventArgs : public EventArgs
211 {
212 public:
213  ElementEventArgs(Element* element):
214  element(element)
215  {}
216 
219 };
220 
242 class CEGUIEXPORT Element :
243  public PropertySet,
244  public EventSet,
245  public AllocatedObject<Element>
246 {
247 public:
249  static const String EventNamespace;
250 
255  static const String EventSized;
261  static const String EventParentSized;
266  static const String EventMoved;
283  static const String EventRotated;
288  static const String EventChildAdded;
293  static const String EventChildRemoved;
306 
315  {
316  public:
323  typedef Rectf (Element::*DataGenerator)(bool) const;
324 
325  CachedRectf(Element const* element, DataGenerator generator):
326  d_element(element),
327  d_generator(generator),
328  // we don't have to initialise d_cachedData here, it will get
329  // regenerated and reset anyways
330  d_cacheValid(false)
331  {}
332 
336  inline const Rectf& get() const
337  {
338  if (!d_cacheValid)
339  {
340  regenerateCache();
341  }
342 
343  return d_cachedData;
344  }
345 
352  inline Rectf getFresh(bool skipAllPixelAlignment = false) const
353  {
354  // if the cache is not valid we will use this chance to regenerate it
355  // of course this is only applicable if we are allowed to use pixel alignment where applicable
356  if (!d_cacheValid && !skipAllPixelAlignment)
357  {
358  return get();
359  }
360 
361  return CEGUI_CALL_MEMBER_FN(*d_element, d_generator)(skipAllPixelAlignment);
362  }
363 
370  inline void invalidateCache() const
371  {
372  d_cacheValid = false;
373  }
374 
375  inline bool isCacheValid() const
376  {
377  return d_cacheValid;
378  }
379 
380  inline void regenerateCache() const
381  {
382  // false, since when we are caching we don't want to skip anything, we want everything to act
383  // exactly as it was setup
384  d_cachedData = CEGUI_CALL_MEMBER_FN(*d_element, d_generator)(false);
385 
386  d_cacheValid = true;
387  }
388 
389  private:
390  Element const* d_element;
391  const DataGenerator d_generator;
392 
393  mutable Rectf d_cachedData;
394  mutable bool d_cacheValid;
395  };
396 
400  Element();
401 
405  virtual ~Element();
406 
414  inline Element* getParentElement() const
415  {
416  return d_parent;
417  }
418 
438  virtual void setArea(const UVector2& pos, const USize& size);
439 
441  inline void setArea(const UDim& xpos, const UDim& ypos,
442  const UDim& width, const UDim& height)
443  {
444  setArea(UVector2(xpos, ypos), USize(width, height));
445  }
446 
448  inline void setArea(const URect& area)
449  {
450  setArea(area.d_min, area.getSize());
451  }
452 
467  inline const URect& getArea() const
468  {
469  return d_area;
470  }
471 
487  inline void setPosition(const UVector2& pos)
488  {
489  setArea_impl(pos, d_area.getSize());
490  }
491 
493  inline void setXPosition(const UDim& pos)
494  {
495  setPosition(UVector2(pos, getYPosition()));
496  }
497 
499  inline void setYPosition(const UDim& pos)
500  {
501  setPosition(UVector2(getXPosition(), pos));
502  }
503 
518  inline const UVector2& getPosition() const
519  {
520  return d_area.getPosition();
521  }
522 
524  inline const UDim& getXPosition() const
525  {
526  return getPosition().d_x;
527  }
528 
530  inline const UDim& getYPosition() const
531  {
532  return getPosition().d_y;
533  }
534 
545  virtual void setHorizontalAlignment(const HorizontalAlignment alignment);
546 
557  inline HorizontalAlignment getHorizontalAlignment() const
558  {
559  return d_horizontalAlignment;
560  }
561 
572  virtual void setVerticalAlignment(const VerticalAlignment alignment);
573 
584  inline VerticalAlignment getVerticalAlignment() const
585  {
586  return d_verticalAlignment;
587  }
588 
600  inline void setSize(const USize& size)
601  {
602  setArea(d_area.getPosition(), size);
603  }
604 
606  inline void setWidth(const UDim& width)
607  {
608  setSize(USize(width, getSize().d_height));
609  }
610 
612  inline void setHeight(const UDim& height)
613  {
614  setSize(USize(getSize().d_width, height));
615  }
616 
628  inline USize getSize() const
629  {
630  return d_area.getSize();
631  }
632 
634  inline UDim getWidth() const
635  {
636  return getSize().d_width;
637  }
638 
640  inline UDim getHeight() const
641  {
642  return getSize().d_height;
643  }
644 
662  void setMinSize(const USize& size);
663 
677  inline const USize& getMinSize() const
678  {
679  return d_minSize;
680  }
681 
701  void setMaxSize(const USize& size);
702 
716  inline const USize& getMaxSize() const
717  {
718  return d_maxSize;
719  }
720 
729  void setAspectMode(const AspectMode mode);
730 
737  inline AspectMode getAspectMode() const
738  {
739  return d_aspectMode;
740  }
741 
755  void setAspectRatio(const float ratio);
756 
763  inline float getAspectRatio() const
764  {
765  return d_aspectRatio;
766  }
767 
789  void setPixelAligned(const bool setting);
790 
798  inline bool isPixelAligned() const
799  {
800  return d_pixelAligned;
801  }
802 
810  inline const Vector2f& getPixelPosition() const
811  {
812  return getUnclippedOuterRect().get().d_min;
813  }
814 
822  inline const Sizef& getPixelSize() const
823  {
824  return d_pixelSize;
825  }
826 
837  Sizef calculatePixelSize(bool skipAllPixelAlignment = false) const;
838 
848  Sizef getParentPixelSize(bool skipAllPixelAlignment = false) const;
849 
862  void setRotation(const Quaternion& rotation);
863 
869  inline const Quaternion& getRotation() const
870  {
871  return d_rotation;
872  }
873 
894  void addChild(Element* element);
895 
906  void removeChild(Element* element);
907 
921  inline Element* getChildElementAtIdx(size_t idx) const
922  {
923  return d_children[idx];
924  }
925 
929  inline size_t getChildCount() const
930  {
931  return d_children.size();
932  }
933 
937  bool isChild(const Element* element) const;
938 
952  bool isAncestor(const Element* element) const;
953 
967  void setNonClient(const bool setting);
968 
974  inline bool isNonClient() const
975  {
976  return d_nonClient;
977  }
978 
992  inline const CachedRectf& getUnclippedOuterRect() const
993  {
994  return d_unclippedOuterRect;
995  }
996 
1010  inline const CachedRectf& getUnclippedInnerRect() const
1011  {
1012  return d_unclippedInnerRect;
1013  }
1014 
1028  inline const CachedRectf& getUnclippedRect(const bool inner) const
1029  {
1030  return inner ? getUnclippedInnerRect() : getUnclippedOuterRect();
1031  }
1032 
1041  virtual const CachedRectf& getClientChildContentArea() const;
1042 
1051  virtual const CachedRectf& getNonClientChildContentArea() const;
1052 
1074  inline const CachedRectf& getChildContentArea(const bool non_client = false) const
1075  {
1076  return non_client ? getNonClientChildContentArea() : getClientChildContentArea();
1077  }
1078 
1092  virtual void notifyScreenAreaChanged(bool recursive = true);
1093 
1103  virtual const Sizef& getRootContainerSize() const;
1104 
1105 protected:
1110  void addElementProperties();
1111 
1142  virtual void setArea_impl(const UVector2& pos, const USize& size,
1143  bool topLeftSizing = false, bool fireEvents = true);
1144 
1146  inline bool isInnerRectSizeChanged() const
1147  {
1148  const Sizef old_sz(d_unclippedInnerRect.get().getSize());
1149  d_unclippedInnerRect.invalidateCache();
1150  return old_sz != d_unclippedInnerRect.get().getSize();
1151  }
1152 
1164  virtual void setParent(Element* parent);
1165 
1170  virtual void addChild_impl(Element* element);
1171 
1176  virtual void removeChild_impl(Element* element);
1177 
1179  virtual Rectf getUnclippedOuterRect_impl(bool skipAllPixelAlignment) const;
1181  virtual Rectf getUnclippedInnerRect_impl(bool skipAllPixelAlignment) const;
1182 
1184  void fireAreaChangeEvents(const bool moved, const bool sized);
1185  void notifyChildrenOfSizeChange(const bool non_client,
1186  const bool client);
1187 
1188  /*************************************************************************
1189  Event trigger methods
1190  *************************************************************************/
1199  virtual void onSized(ElementEventArgs& e);
1200 
1212  virtual void onParentSized(ElementEventArgs& e);
1213 
1222  virtual void onMoved(ElementEventArgs& e);
1223 
1234  virtual void onHorizontalAlignmentChanged(ElementEventArgs& e);
1235 
1246  virtual void onVerticalAlignmentChanged(ElementEventArgs& e);
1247 
1256  virtual void onRotated(ElementEventArgs& e);
1257 
1266  virtual void onChildAdded(ElementEventArgs& e);
1267 
1276  virtual void onChildRemoved(ElementEventArgs& e);
1277 
1288  virtual void onNonClientChanged(ElementEventArgs& e);
1289 
1290  /*************************************************************************
1291  Implementation Data
1292  *************************************************************************/
1294  typedef std::vector<Element*
1295  CEGUI_VECTOR_ALLOC(Element*)> ChildList;
1296 
1301 
1304 
1325 
1330 
1331 private:
1332  /*************************************************************************
1333  May not copy or assign Element objects
1334  *************************************************************************/
1335  Element(const Element&);
1336 
1337  Element& operator=(const Element&) {return *this;}
1338 };
1339 
1340 } // End of CEGUI namespace section
1341 
1342 
1343 #if defined(_MSC_VER)
1344 # pragma warning(pop)
1345 #endif
1346 
1347 #endif // end of guard _CEGUIElement_h_