Crazy Eddie's GUI System  0.8.7
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:
76  typedef return_type safe_method_return_type;
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:
154  typedef return_type safe_method_return_type;
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 
558  {
559  return d_horizontalAlignment;
560  }
561 
572  virtual void setVerticalAlignment(const VerticalAlignment alignment);
573 
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_
static const String EventVerticalAlignmentChanged
Definition: Element.h:278
void setSize(const USize &size)
Set the element's size.
Definition: Element.h:600
const URect & getArea() const
Return the element's area.
Definition: Element.h:467
bool d_pixelAligned
If true, the position and size are pixel aligned.
Definition: Element.h:1320
AspectMode
How aspect ratio should be maintained.
Definition: Size.h:45
VerticalAlignment d_verticalAlignment
Specifies the base for vertical alignment.
Definition: Element.h:1310
HorizontalAlignment getHorizontalAlignment() const
Get the horizontal alignment.
Definition: Element.h:557
const Quaternion & getRotation() const
retrieves rotation of this widget
Definition: Element.h:869
static const String EventParentSized
Definition: Element.h:261
Definition: MemoryAllocatedObject.h:109
bool d_nonClient
true if element is in non-client (outside InnerRect) area of parent.
Definition: Element.h:1303
static const String EventZOrderChanged
Definition: Element.h:299
URect d_area
This element objects area as defined by a URect.
Definition: Element.h:1306
Main namespace for Crazy Eddie's GUI Library.
Definition: arch_overview.dox:1
bool isPixelAligned() const
Checks whether this Element is pixel aligned.
Definition: Element.h:798
USize getSize() const
Get the element's size.
Definition: Element.h:628
A positioned and sized rectangular node in a tree graph.
Definition: Element.h:242
void setWidth(const UDim &width)
Definition: Element.h:606
Definition: Element.h:146
Quaternion d_rotation
Rotation of this element (relative to the parent)
Definition: Element.h:1324
static const String EventNamespace
Namespace for global events.
Definition: Element.h:249
Base class used as the argument to all subscribers Event object.
Definition: EventArgs.h:49
void setXPosition(const UDim &pos)
Definition: Element.h:493
Class that holds the size (width & height) of something.
Definition: ForwardRefs.h:112
ChildList d_children
The list of child element objects attached to this.
Definition: Element.h:1298
const CachedRectf & getUnclippedRect(const bool inner) const
Return a Rect that describes the unclipped area covered by the Element.
Definition: Element.h:1028
static const String EventRotated
Definition: Element.h:283
const CachedRectf & getChildContentArea(const bool non_client=false) const
Return a Rect that is used to position and size child elements.
Definition: Element.h:1074
Size< T > getSize() const
return the size of the Rect area
Definition: Rect.h:149
Interface providing introspection capabilities.
Definition: PropertySet.h:107
const Sizef & getPixelSize() const
Return the element's size in pixels.
Definition: Element.h:822
Rectf getFresh(bool skipAllPixelAlignment=false) const
Skips all caching and calls the generator.
Definition: Element.h:352
const CachedRectf & getUnclippedOuterRect() const
Return a Rect that describes the unclipped outer rect area of the Element.
Definition: Element.h:992
void setPosition(const UVector2 &pos)
Set the element's position.
Definition: Element.h:487
Definition: Element.h:68
Definition: Element.h:136
const USize & getMinSize() const
Get the element's minimum size.
Definition: Element.h:677
USize d_maxSize
current maximum size for the element.
Definition: Element.h:1314
CachedRectf d_unclippedInnerRect
inner area rect in screen pixels
Definition: Element.h:1329
static const String EventSized
Definition: Element.h:255
UDim getWidth() const
Definition: Element.h:634
AspectMode getAspectMode() const
Retrieves currently used aspect mode.
Definition: Element.h:737
const UVector2 & getPosition() const
Get the element's position.
Definition: Element.h:518
AspectMode d_aspectMode
How to satisfy current aspect ratio.
Definition: Element.h:1316
USize d_minSize
current minimum size for the element.
Definition: Element.h:1312
static const String EventHorizontalAlignmentChanged
Definition: Element.h:272
bool isNonClient() const
Checks whether this element was set to be non client.
Definition: Element.h:974
const UDim & getXPosition() const
Definition: Element.h:524
static const String EventNonClientChanged
Definition: Element.h:305
const UDim & getYPosition() const
Definition: Element.h:530
Helper class used to convert various data types to and from the format expected in Property strings...
Definition: ForwardRefs.h:84
CachedRectf d_unclippedOuterRect
outer area rect in screen pixels
Definition: Element.h:1327
std::vector< Element *CEGUI_VECTOR_ALLOC(Element *)> ChildList
definition of type used for the list of attached child elements.
Definition: Element.h:1295
size_t getChildCount() const
Returns number of child elements attached to this Element.
Definition: Element.h:929
Definition: Element.h:141
Interface providing event signaling and handling.
Definition: EventSet.h:166
VerticalAlignment getVerticalAlignment() const
Get the vertical alignment.
Definition: Element.h:584
static const String EventChildRemoved
Definition: Element.h:293
const USize & getMaxSize() const
Get the element's maximum size.
Definition: Element.h:716
EventArgs based class that is used for objects passed to handlers triggered for events concerning som...
Definition: Element.h:210
const Vector2f & getPixelPosition() const
Return the element's absolute (or screen, depending on the type of the element) position in pixels...
Definition: Element.h:810
UDim getHeight() const
Definition: Element.h:640
static const String EventChildAdded
Definition: Element.h:288
Definition: Element.h:63
HorizontalAlignment d_horizontalAlignment
Specifies the base for horizontal alignment.
Definition: Element.h:1308
void setArea(const UDim &xpos, const UDim &ypos, const UDim &width, const UDim &height)
Definition: Element.h:441
A tiny wrapper to hide some of the dirty work of rect caching.
Definition: Element.h:314
bool isInnerRectSizeChanged() const
helper to return whether the inner rect size has changed
Definition: Element.h:1146
Element * element
pointer to an Element object of relevance to the event.
Definition: Element.h:218
Dimension that has both a relative 'scale' portion and and absolute 'offset' portion.
Definition: UDim.h:92
Class to represent rotation, avoids Gimbal lock.
Definition: Quaternion.h:67
Definition: Element.h:58
float d_aspectRatio
The target aspect ratio.
Definition: Element.h:1318
void invalidateCache() const
Invalidates the cached Rectf causing it to be regenerated.
Definition: Element.h:370
float getAspectRatio() const
Retrieves target aspect ratio.
Definition: Element.h:763
Sizef d_pixelSize
Current constrained pixel size of the element.
Definition: Element.h:1322
void setArea(const URect &area)
Definition: Element.h:448
void setHeight(const UDim &height)
Definition: Element.h:612
VerticalAlignment
Enumerated type used when specifying vertical alignments for Element.
Definition: Element.h:130
void setYPosition(const UDim &pos)
Definition: Element.h:499
const CachedRectf & getUnclippedInnerRect() const
Return a Rect that describes the unclipped inner rect area of the Element.
Definition: Element.h:1010
Element * d_parent
Holds pointer to the parent element.
Definition: Element.h:1300
Element * getParentElement() const
Retrieves parent of this element.
Definition: Element.h:414
Element * getChildElementAtIdx(size_t idx) const
return a pointer to the child element that is attached to 'this' at the given index.
Definition: Element.h:921
HorizontalAlignment
Enumerated type used when specifying horizontal alignments for Element.
Definition: Element.h:52
static const String EventMoved
Definition: Element.h:266
String class used within the GUI system.
Definition: String.h:62