Crazy Eddie's GUI System  0.8.3
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Element.h
1 /***********************************************************************
2  filename: CEGUI/Element.h
3  created: 18/8/2011
4  author: Martin Preisler
5 
6  purpose: Defines a class representing an item in a graph
7  (deals with relative positions, relative dimensions, ...)
8 *************************************************************************/
9 /***************************************************************************
10  * Copyright (C) 2004 - 2011 Paul D Turner & The CEGUI Development Team
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining
13  * a copy of this software and associated documentation files (the
14  * "Software"), to deal in the Software without restriction, including
15  * without limitation the rights to use, copy, modify, merge, publish,
16  * distribute, sublicense, and/or sell copies of the Software, and to
17  * permit persons to whom the Software is furnished to do so, subject to
18  * the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be
21  * included in all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
27  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29  * OTHER DEALINGS IN THE SOFTWARE.
30  ***************************************************************************/
31 
32 #ifndef _CEGUIElement_h_
33 #define _CEGUIElement_h_
34 
35 #include "CEGUI/Base.h"
36 #include "CEGUI/PropertySet.h"
37 #include "CEGUI/EventSet.h"
38 #include "CEGUI/EventArgs.h"
39 
40 #if defined(_MSC_VER)
41 # pragma warning(push)
42 # pragma warning(disable : 4251)
43 #endif
44 
45 namespace CEGUI
46 {
47 
54 {
70 };
71 
72 template<>
74 {
75 public:
79  typedef String string_return_type;
80 
81  static const String& getDataTypeName()
82  {
83  static String type("HorizontalAlignment");
84 
85  return type;
86  }
87 
88  static return_type fromString(const String& str)
89  {
90  if (str == "Centre")
91  {
92  return HA_CENTRE;
93  }
94  else if (str == "Right")
95  {
96  return HA_RIGHT;
97  }
98  else
99  {
100  return HA_LEFT;
101  }
102  }
103 
104  static string_return_type toString(pass_type val)
105  {
106  if (val == HA_CENTRE)
107  {
108  return "Centre";
109  }
110  else if (val == HA_RIGHT)
111  {
112  return "Right";
113  }
114  else if (val == HA_LEFT)
115  {
116  return "Left";
117  }
118  else
119  {
120  assert(false && "Invalid horizontal alignment");
121  return "Centre";
122  }
123  }
124 };
125 
132 {
148 };
149 
150 template<>
152 {
153 public:
157  typedef String string_return_type;
158 
159  static const String& getDataTypeName()
160  {
161  static String type("VerticalAlignment");
162 
163  return type;
164  }
165 
166  static return_type fromString(const String& str)
167  {
168  if (str == "Centre")
169  {
170  return VA_CENTRE;
171  }
172  else if (str == "Bottom")
173  {
174  return VA_BOTTOM;
175  }
176  else
177  {
178  return VA_TOP;
179  }
180  }
181 
182  static string_return_type toString(pass_type val)
183  {
184  if (val == VA_CENTRE)
185  {
186  return "Centre";
187  }
188  else if (val == VA_BOTTOM)
189  {
190  return "Bottom";
191  }
192  else if (val == VA_TOP)
193  {
194  return "Top";
195  }
196  else
197  {
198  assert(false && "Invalid vertical alignment");
199  return "Centre";
200  }
201  }
202 };
203 
211 class CEGUIEXPORT ElementEventArgs : public EventArgs
212 {
213 public:
214  ElementEventArgs(Element* element):
215  element(element)
216  {}
217 
220 };
221 
243 class CEGUIEXPORT Element :
244  public PropertySet,
245  public EventSet,
246  public AllocatedObject<Element>
247 {
248 public:
250  static const String EventNamespace;
251 
256  static const String EventSized;
262  static const String EventParentSized;
267  static const String EventMoved;
284  static const String EventRotated;
289  static const String EventChildAdded;
294  static const String EventChildRemoved;
307 
316  {
317  public:
324  typedef Rectf (Element::*DataGenerator)(bool) const;
325 
326  CachedRectf(Element const* element, DataGenerator generator):
327  d_element(element),
328  d_generator(generator),
329  // we don't have to initialise d_cachedData here, it will get
330  // regenerated and reset anyways
331  d_cacheValid(false)
332  {}
333 
337  inline const Rectf& get() const
338  {
339  if (!d_cacheValid)
340  {
341  regenerateCache();
342  }
343 
344  return d_cachedData;
345  }
346 
353  inline Rectf getFresh(bool skipAllPixelAlignment = false) const
354  {
355  // if the cache is not valid we will use this chance to regenerate it
356  // of course this is only applicable if we are allowed to use pixel alignment where applicable
357  if (!d_cacheValid && !skipAllPixelAlignment)
358  {
359  return get();
360  }
361 
362  return CEGUI_CALL_MEMBER_FN(*d_element, d_generator)(skipAllPixelAlignment);
363  }
364 
371  inline void invalidateCache() const
372  {
373  d_cacheValid = false;
374  }
375 
376  inline bool isCacheValid() const
377  {
378  return d_cacheValid;
379  }
380 
381  inline void regenerateCache() const
382  {
383  // false, since when we are caching we don't want to skip anything, we want everything to act
384  // exactly as it was setup
385  d_cachedData = CEGUI_CALL_MEMBER_FN(*d_element, d_generator)(false);
386 
387  d_cacheValid = true;
388  }
389 
390  private:
391  Element const* d_element;
392  const DataGenerator d_generator;
393 
394  mutable Rectf d_cachedData;
395  mutable bool d_cacheValid;
396  };
397 
401  Element();
402 
406  virtual ~Element();
407 
415  inline Element* getParentElement() const
416  {
417  return d_parent;
418  }
419 
439  virtual void setArea(const UVector2& pos, const USize& size);
440 
442  inline void setArea(const UDim& xpos, const UDim& ypos,
443  const UDim& width, const UDim& height)
444  {
445  setArea(UVector2(xpos, ypos), USize(width, height));
446  }
447 
449  inline void setArea(const URect& area)
450  {
451  setArea(area.d_min, area.getSize());
452  }
453 
468  inline const URect& getArea() const
469  {
470  return d_area;
471  }
472 
488  inline void setPosition(const UVector2& pos)
489  {
490  setArea_impl(pos, d_area.getSize());
491  }
492 
494  inline void setXPosition(const UDim& pos)
495  {
496  setPosition(UVector2(pos, getYPosition()));
497  }
498 
500  inline void setYPosition(const UDim& pos)
501  {
502  setPosition(UVector2(getXPosition(), pos));
503  }
504 
519  inline const UVector2& getPosition() const
520  {
521  return d_area.getPosition();
522  }
523 
525  inline const UDim& getXPosition() const
526  {
527  return getPosition().d_x;
528  }
529 
531  inline const UDim& getYPosition() const
532  {
533  return getPosition().d_y;
534  }
535 
546  virtual void setHorizontalAlignment(const HorizontalAlignment alignment);
547 
558  inline HorizontalAlignment getHorizontalAlignment() const
559  {
560  return d_horizontalAlignment;
561  }
562 
573  virtual void setVerticalAlignment(const VerticalAlignment alignment);
574 
585  inline VerticalAlignment getVerticalAlignment() const
586  {
587  return d_verticalAlignment;
588  }
589 
601  inline void setSize(const USize& size)
602  {
603  setArea(d_area.getPosition(), size);
604  }
605 
607  inline void setWidth(const UDim& width)
608  {
609  setSize(USize(width, getSize().d_height));
610  }
611 
613  inline void setHeight(const UDim& height)
614  {
615  setSize(USize(getSize().d_width, height));
616  }
617 
629  inline USize getSize() const
630  {
631  return d_area.getSize();
632  }
633 
635  inline UDim getWidth() const
636  {
637  return getSize().d_width;
638  }
639 
641  inline UDim getHeight() const
642  {
643  return getSize().d_height;
644  }
645 
663  void setMinSize(const USize& size);
664 
678  inline const USize& getMinSize() const
679  {
680  return d_minSize;
681  }
682 
702  void setMaxSize(const USize& size);
703 
717  inline const USize& getMaxSize() const
718  {
719  return d_maxSize;
720  }
721 
730  void setAspectMode(const AspectMode mode);
731 
738  inline AspectMode getAspectMode() const
739  {
740  return d_aspectMode;
741  }
742 
756  void setAspectRatio(const float ratio);
757 
764  inline float getAspectRatio() const
765  {
766  return d_aspectRatio;
767  }
768 
790  void setPixelAligned(const bool setting);
791 
799  inline bool isPixelAligned() const
800  {
801  return d_pixelAligned;
802  }
803 
811  inline const Vector2f& getPixelPosition() const
812  {
813  return getUnclippedOuterRect().get().d_min;
814  }
815 
823  inline const Sizef& getPixelSize() const
824  {
825  return d_pixelSize;
826  }
827 
838  Sizef calculatePixelSize(bool skipAllPixelAlignment = false) const;
839 
849  Sizef getParentPixelSize(bool skipAllPixelAlignment = false) const;
850 
863  void setRotation(const Quaternion& rotation);
864 
870  inline const Quaternion& getRotation() const
871  {
872  return d_rotation;
873  }
874 
895  void addChild(Element* element);
896 
907  void removeChild(Element* element);
908 
922  inline Element* getChildElementAtIdx(size_t idx) const
923  {
924  return d_children[idx];
925  }
926 
930  inline size_t getChildCount() const
931  {
932  return d_children.size();
933  }
934 
938  bool isChild(const Element* element) const;
939 
953  bool isAncestor(const Element* element) const;
954 
968  void setNonClient(const bool setting);
969 
975  inline bool isNonClient() const
976  {
977  return d_nonClient;
978  }
979 
993  inline const CachedRectf& getUnclippedOuterRect() const
994  {
995  return d_unclippedOuterRect;
996  }
997 
1011  inline const CachedRectf& getUnclippedInnerRect() const
1012  {
1013  return d_unclippedInnerRect;
1014  }
1015 
1029  inline const CachedRectf& getUnclippedRect(const bool inner) const
1030  {
1031  return inner ? getUnclippedInnerRect() : getUnclippedOuterRect();
1032  }
1033 
1042  virtual const CachedRectf& getClientChildContentArea() const;
1043 
1052  virtual const CachedRectf& getNonClientChildContentArea() const;
1053 
1075  inline const CachedRectf& getChildContentArea(const bool non_client = false) const
1076  {
1077  return non_client ? getNonClientChildContentArea() : getClientChildContentArea();
1078  }
1079 
1093  virtual void notifyScreenAreaChanged(bool recursive = true);
1094 
1104  virtual const Sizef& getRootContainerSize() const;
1105 
1106 protected:
1111  void addElementProperties();
1112 
1143  virtual void setArea_impl(const UVector2& pos, const USize& size,
1144  bool topLeftSizing = false, bool fireEvents = true);
1145 
1147  inline bool isInnerRectSizeChanged() const
1148  {
1149  const Sizef old_sz(d_unclippedInnerRect.get().getSize());
1150  d_unclippedInnerRect.invalidateCache();
1151  return old_sz != d_unclippedInnerRect.get().getSize();
1152  }
1153 
1165  virtual void setParent(Element* parent);
1166 
1171  virtual void addChild_impl(Element* element);
1172 
1177  virtual void removeChild_impl(Element* element);
1178 
1180  virtual Rectf getUnclippedOuterRect_impl(bool skipAllPixelAlignment) const;
1182  virtual Rectf getUnclippedInnerRect_impl(bool skipAllPixelAlignment) const;
1183 
1185  void fireAreaChangeEvents(const bool moved, const bool sized);
1186  void notifyChildrenOfSizeChange(const bool non_client,
1187  const bool client);
1188 
1189  /*************************************************************************
1190  Event trigger methods
1191  *************************************************************************/
1200  virtual void onSized(ElementEventArgs& e);
1201 
1213  virtual void onParentSized(ElementEventArgs& e);
1214 
1223  virtual void onMoved(ElementEventArgs& e);
1224 
1235  virtual void onHorizontalAlignmentChanged(ElementEventArgs& e);
1236 
1247  virtual void onVerticalAlignmentChanged(ElementEventArgs& e);
1248 
1257  virtual void onRotated(ElementEventArgs& e);
1258 
1267  virtual void onChildAdded(ElementEventArgs& e);
1268 
1277  virtual void onChildRemoved(ElementEventArgs& e);
1278 
1289  virtual void onNonClientChanged(ElementEventArgs& e);
1290 
1291  /*************************************************************************
1292  Implementation Data
1293  *************************************************************************/
1295  typedef std::vector<Element*
1296  CEGUI_VECTOR_ALLOC(Element*)> ChildList;
1297 
1302 
1305 
1326 
1331 
1332 private:
1333  /*************************************************************************
1334  May not copy or assign Element objects
1335  *************************************************************************/
1336  Element(const Element&);
1337 
1338  Element& operator=(const Element&) {return *this;}
1339 };
1340 
1341 } // End of CEGUI namespace section
1342 
1343 
1344 #if defined(_MSC_VER)
1345 # pragma warning(pop)
1346 #endif
1347 
1348 #endif // end of guard _CEGUIElement_h_