Spinning Topp Logo BlackTopp Studios
inc
rect.h
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine 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 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine 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 The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _uirect_h
41 #define _uirect_h
42 
43 #include "vector2.h"
44 #include "exception.h"
45 #include "serialization.h"
46 
47 namespace Mezzanine
48 {
49  namespace UI
50  {
51  ///////////////////////////////////////////////////////////////////////////////
52  /// @brief This class represents a box shaped area on the screen.
53  /// @details
54  ///////////////////////////////////////
55  class MEZZ_LIB Rect
56  {
57  protected:
58  /// @brief Simple check to see if a point is within a given range.
59  /// @param Value The value to compare against the range.
60  /// @param Min The minimum value of the range.
61  /// @param Max The maximum value of the range.
62  /// @return Returns true if the provided value is within the provided range, false otherwise.
63  inline Boole ValueInRange(const Real& Value, const Real& Min, const Real& Max) const
64  {
65  return (Value >= Min) && (Value <= Max);
66  }
67  public:
68  /// @brief Vector2 representing the top-left position of the rect.
70  /// @brief Vector2 representing the width and height of the rect.
72  /// @brief Boole representing whether this rect is represented in relative units or absolute units(pixels).
74 
75  /// @brief Less Detailed Real Constructor.
76  /// @param PosX The position of this rect on the X axis.
77  /// @param PosY The position of this rect on the Y axis.
78  /// @param SizeX The size of this rect on the X axis.
79  /// @param SizeY The size of this rect on the Y axis.
80  Rect(const Real& PosX, const Real& PosY, const Real& SizeX, const Real& SizeY)
81  {
82  this->Position.SetValues(PosX,PosY);
83  this->Size.SetValues(SizeX,SizeY);
84  this->Relative = false;
85  }
86  /// @brief Real Constructor.
87  /// @param PosX The position of this rect on the X axis.
88  /// @param PosY The position of this rect on the Y axis.
89  /// @param SizeX The size of this rect on the X axis.
90  /// @param SizeY The size of this rect on the Y axis.
91  /// @param Relative Whether or not this Rect is expressing relative coordinates (0-1).
92  Rect(const Real& PosX, const Real& PosY, const Real& SizeX, const Real& SizeY, Boole Relative)
93  {
94  this->Position.SetValues(PosX,PosY);
95  this->Size.SetValues(SizeX,SizeY);
96  this->Relative = Relative;
97  }
98  /// @brief Less Detailed Vector2 Constructor.
99  /// @details Sets all the data of the class.
100  /// @param Position The position of the Renderable's Rect.
101  /// @param Size The size of the Renderable's Rect.
102  Rect(const Vector2& Position, const Vector2& Size)
103  {
104  this->Position = Position;
105  this->Size = Size;
106  this->Relative = false;
107  }
108  /// @brief Vector2 Constructor.
109  /// @details Sets all the data of the class.
110  /// @param Position The position of the Renderable's Rect.
111  /// @param Size The size of the Renderable's Rect.
112  /// @param Relative Whether or not this Rect is using relative(0-1) or absolute units(Pixels).
113  Rect(const Vector2& Position, const Vector2& Size, Boole Relative)
114  {
115  this->Position = Position;
116  this->Size = Size;
117  this->Relative = Relative;
118  }
119  /// @brief Copy Constructor.
120  /// @param Other The other Rect to copy.
121  Rect(const Rect& Other)
122  {
123  this->Position = Other.Position;
124  this->Size = Other.Size;
125  this->Relative = Other.Relative;
126  }
127  /// @brief No Initialization Constructor.
128  /// @details Sets all data members to zero's.
130  {
131  this->SetIdentity();
132  this->Relative = false;
133  }
134  /// @brief Class destructor.
135  ~Rect() {}
136 
137  ///////////////////////////////////////////////////////////////////////////////
138  // Utility Methods
139 
140  /// @brief Sets all the values of this rect to zero.
141  inline void SetIdentity()
142  {
143  this->Position.SetIdentity();
144  this->Size.SetIdentity();
145  }
146 
147  /// @brief Gets the value of the top edge of this rect.
148  /// @return Returns a Real that is the point on the vertical axis this rect's top edge is located.
149  inline Real GetTopEdge() const
150  {
151  return this->Position.Y;
152  }
153  /// @brief Gets the value of the bottom edge of this rect.
154  /// @return Returns a Real that is the point on the vertical axis this rect's bottom edge is located.
155  inline Real GetBottomEdge() const
156  {
157  return this->Position.Y + this->Size.Y;
158  }
159  /// @brief Gets the value of the left edge of this rect.
160  /// @return Returns a Real that is the point on the horizontal axis this rect's left edge is located.
161  inline Real GetLeftEdge() const
162  {
163  return this->Position.X;
164  }
165  /// @brief Gets the value of the right edge of this rect.
166  /// @return Returns a Real that is the point on the horizontal axis this rect's right edge is located.
167  inline Real GetRightEdge() const
168  {
169  return this->Position.X + this->Size.X;
170  }
171 
172  /// @brief Gets the value of the horizontal center of this rect.
173  /// @return Returns a Real that is the point on the horizontal axis that is this rect's center.
174  inline Real GetHorizontalCenter() const
175  {
176  return this->Position.X + (this->Size.X * 0.5);
177  }
178  /// @brief Gets the value of the vertical center of this rect.
179  /// @return Returns a Real that is the point on the vertical axis that is this rect's center.
180  inline Real GetVerticalCenter() const
181  {
182  return this->Position.Y + (this->Size.Y * 0.5);
183  }
184  /// @brief Gets the coordinates to the center of this rect.
185  /// @return Returns a vector2 containing the central point of this rect.
186  inline Vector2 GetRectCenter() const
187  {
188  return Vector2(this->GetHorizontalCenter(),this->GetVerticalCenter());
189  }
190 
191  /// @brief Checks to see if another Rect is overlapping with this one.
192  /// @param OtherRect The other rect to compare against.
193  /// @return Returns true if these rects overlap with each other, false otherwise.
194  inline Boole CheckOverlap(const Rect& OtherRect) const
195  {
196  Boole XOverlap = ValueInRange(this->Position.X, OtherRect.Position.X, OtherRect.Position.X + OtherRect.Size.X) ||
197  ValueInRange(OtherRect.Position.X, this->Position.X, this->Position.X + this->Size.X);
198 
199  Boole YOverlap = ValueInRange(this->Position.Y, OtherRect.Position.Y, OtherRect.Position.Y + OtherRect.Size.Y) ||
200  ValueInRange(OtherRect.Position.Y, this->Position.Y, this->Position.Y + this->Size.Y);
201 
202  return (XOverlap && YOverlap);
203  }
204  /// @brief Checks to see if a point in 2D space is inside this rect.
205  /// @param Point The point in 2D space to check.
206  /// @return Returns true if the provided point is within this rect, false otherwise.
207  inline Boole IsInside(const Vector2& Point) const
208  {
209  return ( ValueInRange(Point.X,this->Position.X,this->Position.X + this->Size.X) &&
210  ValueInRange(Point.Y,this->Position.Y,this->Position.Y + this->Size.Y) );
211  }
212  /// @brief Gets whether or point on the X axis is within the limits of this rect or not.
213  /// @param Position The point on the X axis.
214  /// @return Returns true in the provided position is within this rect's limits, false otherwise.
215  inline Boole IsWithinWidth(const Real& Position) const
216  {
217  return ValueInRange(Position,this->Position.X,this->Position.X + this->Size.X);
218  }
219  /// @brief Gets whether or point on the Y axis is within the limits of this rect or not.
220  /// @param Position The point on the Y axis.
221  /// @return Returns true in the provided position is within this rect's limits, false otherwise.
222  inline Boole IsWithinHeight(const Real& Position) const
223  {
224  return ValueInRange(Position,this->Position.Y,this->Position.Y + this->Size.Y);
225  }
226 
227  ///////////////////////////////////////////////////////////////////////////////
228  // Transform Methods
229 
230  /// @brief Scales this rect.
231  /// @note This will scale the rect based on it's center, not it's top-left position. If you want to instead preserve the position of the Rect then apply the scaling manually.
232  /// @param Scaling A Vector2 representing the X and Y scaling to be applied to this rect.
233  /// @return Returns a reference to this.
234  inline Rect& ApplyScaling(const Vector2& Scaling)
235  {
236  Vector2 PrevCenter = this->GetRectCenter();
237  this->Size *= Scaling;
238 
239  this->Position.X = PrevCenter.X - ( this->Size.X * 0.5 );
240  this->Position.Y = PrevCenter.Y - ( this->Size.Y * 0.5 );
241 
242  return *this;
243  }
244 
245  ///////////////////////////////////////////////////////////////////////////////
246  // Overloaded Operators
247 
248  /// @brief Assignment Operator.
249  /// @details Copys the contents of a Rect to another.
250  /// @param Other The other Rect to copy from.
251  /// @return Returns a reference to this.
252  inline Rect& operator=(const Rect& Other)
253  {
254  this->Position = Other.Position;
255  this->Size = Other.Size;
256  this->Relative = Other.Relative;
257  return *this;
258  }
259  /// @brief Equality Comparison Operator.
260  /// @details Checks to see if the two Rects are equal.
261  /// @param Other The other Rect to compare against.
262  /// @return Returns true if this Rect is equal to the Other, false otherwise.
263  inline Boole operator==(const Rect& Other)
264  {
265  return ( this->Position == Other.Position && this->Size == Other.Size && this->Relative == Other.Relative );
266  }
267  /// @brief Inequality Comparison Operator.
268  /// @details Checks to see if the two rects are different.
269  /// @param Other The other Rect to compare against.
270  /// @return Returns false of this Rect is equal to the other, true otherwise.
271  inline Boole operator!=(const Rect& Other)
272  {
273  return ( this->Position != Other.Position || this->Size != Other.Size || this->Relative != Other.Relative );
274  }
275 
276  ///////////////////////////////////////////////////////////////////////////////
277  // Serialization
278 
279  /// @brief Convert this class to an XML::Node ready for serialization.
280  /// @param ParentNode The point in the XML hierarchy that all this renderable should be appended to.
281  void ProtoSerialize(XML::Node& ParentNode) const
282  {
283  XML::Node RectNode = ParentNode.AppendChild( Rect::GetSerializableName() );
284 
285  if( RectNode.AppendAttribute("Version").SetValue("1") &&
286  RectNode.AppendAttribute("PositionX").SetValue(this->Position.X) &&
287  RectNode.AppendAttribute("PositionY").SetValue(this->Position.Y) &&
288  RectNode.AppendAttribute("SizeX").SetValue(this->Size.X) &&
289  RectNode.AppendAttribute("SizeY").SetValue(this->Size.Y) )
290  {
291  return;
292  }else{
293  SerializeError("Create XML Attribute Values",Rect::GetSerializableName(),true);
294  }
295  }
296  /// @brief Take the data stored in an XML Node and overwrite this object with it.
297  /// @param SelfRoot An XML::Node containing the data to populate this class with.
298  void ProtoDeSerialize(const XML::Node& SelfRoot)
299  {
300  XML::Attribute CurrAttrib;
301  XML::Node RectNode = SelfRoot.GetChild( Rect::GetSerializableName() );
302 
303  if( !RectNode.Empty() ) {
304  if(RectNode.GetAttribute("Version").AsInt() == 1) {
305  CurrAttrib = RectNode.GetAttribute("PositionX");
306  if( !CurrAttrib.Empty() )
307  this->Position.X = CurrAttrib.AsReal();
308 
309  CurrAttrib = RectNode.GetAttribute("PositionY");
310  if( !CurrAttrib.Empty() )
311  this->Position.Y = CurrAttrib.AsReal();
312 
313  CurrAttrib = RectNode.GetAttribute("SizeX");
314  if( !CurrAttrib.Empty() )
315  this->Size.X = CurrAttrib.AsReal();
316 
317  CurrAttrib = RectNode.GetAttribute("SizeY");
318  if( !CurrAttrib.Empty() )
319  this->Size.X = CurrAttrib.AsReal();
320  }else{
321  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + Rect::GetSerializableName() + ": Not Version 1.");
322  }
323  }else{
324  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,Rect::GetSerializableName() + " was not found in the provided XML node, which was expected.");
325  }
326  }
327  /// @brief Get the name of the the XML tag the Renderable class will leave behind as its instances are serialized.
328  /// @return A string containing the name of this class.
330  {
331  return "Rect";
332  }
333  };//Rect
334  }//UI
335 }//Mezzanine
336 
337 #endif
Boole Relative
Boole representing whether this rect is represented in relative units or absolute units(pixels)...
Definition: rect.h:73
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
void SetIdentity()
Sets all the values of this rect to zero.
Definition: rect.h:141
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
Boole IsWithinHeight(const Real &Position) const
Gets whether or point on the Y axis is within the limits of this rect or not.
Definition: rect.h:222
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
Real GetVerticalCenter() const
Gets the value of the vertical center of this rect.
Definition: rect.h:180
Vector2 Size
Vector2 representing the width and height of the rect.
Definition: rect.h:71
Real GetTopEdge() const
Gets the value of the top edge of this rect.
Definition: rect.h:149
Thrown when the requested identity could not be found.
Definition: exception.h:94
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
Boole IsWithinWidth(const Real &Position) const
Gets whether or point on the X axis is within the limits of this rect or not.
Definition: rect.h:215
Boole ValueInRange(const Real &Value, const Real &Min, const Real &Max) const
Simple check to see if a point is within a given range.
Definition: rect.h:63
Rect & ApplyScaling(const Vector2 &Scaling)
Scales this rect.
Definition: rect.h:234
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
Rect()
No Initialization Constructor.
Definition: rect.h:129
Boole CheckOverlap(const Rect &OtherRect) const
Checks to see if another Rect is overlapping with this one.
Definition: rect.h:194
Real GetHorizontalCenter() const
Gets the value of the horizontal center of this rect.
Definition: rect.h:174
This class represents a box shaped area on the screen.
Definition: rect.h:55
bool Empty() const
Is this storing anything at all?
This implements the exception hiearchy for Mezzanine.
void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
Definition: rect.h:298
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
static String GetSerializableName()
Get the name of the the XML tag the Renderable class will leave behind as its instances are serialize...
Definition: rect.h:329
The interface for serialization.
bool SetValue(const Char8 *rhs)
Set the value of this.
Boole operator==(const Rect &Other)
Equality Comparison Operator.
Definition: rect.h:263
Real Y
Coordinate on the Y vector.
Definition: vector2.h:69
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
Real X
Coordinate on the X vector.
Definition: vector2.h:67
Rect(const Real &PosX, const Real &PosY, const Real &SizeX, const Real &SizeY)
Less Detailed Real Constructor.
Definition: rect.h:80
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
This is used to represent a point on a 2 dimentional area, such as a screen.
Definition: vector2.h:63
bool Empty() const
Is this storing anything at all?
Real GetRightEdge() const
Gets the value of the right edge of this rect.
Definition: rect.h:167
void SetIdentity()
Sets the values of this vector2 to identity values(0,0).
Definition: vector2.cpp:99
Real AsReal(Real def=0) const
Attempts to convert the value of the attribute to a Real and returns the results. ...
Rect(const Real &PosX, const Real &PosY, const Real &SizeX, const Real &SizeY, Boole Relative)
Real Constructor.
Definition: rect.h:92
void SetValues(const Real &x, const Real &y)
Sets the X and Y values of this vector2.
Definition: vector2.cpp:105
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
Rect(const Rect &Other)
Copy Constructor.
Definition: rect.h:121
Rect(const Vector2 &Position, const Vector2 &Size)
Less Detailed Vector2 Constructor.
Definition: rect.h:102
Rect(const Vector2 &Position, const Vector2 &Size, Boole Relative)
Vector2 Constructor.
Definition: rect.h:113
~Rect()
Class destructor.
Definition: rect.h:135
Real GetLeftEdge() const
Gets the value of the left edge of this rect.
Definition: rect.h:161
Real GetBottomEdge() const
Gets the value of the bottom edge of this rect.
Definition: rect.h:155
Boole IsInside(const Vector2 &Point) const
Checks to see if a point in 2D space is inside this rect.
Definition: rect.h:207
Vector2 Position
Vector2 representing the top-left position of the rect.
Definition: rect.h:69
void ProtoSerialize(XML::Node &ParentNode) const
Convert this class to an XML::Node ready for serialization.
Definition: rect.h:281
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
Boole operator!=(const Rect &Other)
Inequality Comparison Operator.
Definition: rect.h:271
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Rect & operator=(const Rect &Other)
Assignment Operator.
Definition: rect.h:252
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
Vector2 GetRectCenter() const
Gets the coordinates to the center of this rect.
Definition: rect.h:186
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.