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 _rect_h
41 #define _rect_h
42 
43 #include "datatypes.h"
44 
45 namespace Mezzanine
46 {
47  /// @brief A class for tracking a rectangle by tracking two corners Top, Left and
48  /// Right, Bottom.
49  template<typename NumType>
50  class RectBase
51  {
52  public:
53  ///////////////////////////////////////////////////////////////////////////////
54  // Public Types
55 
56  /// @brief Convenience typedef for describing the type you are working with.
58 
59  ///////////////////////////////////////////////////////////////////////////////
60  // Public Data Members
61 
62  /// @brief The Left edge of the rect.
63  NumType Left;
64  /// @brief The Top edge of the rect.
65  NumType Top;
66  /// @brief The Right edge of the rect.
67  NumType Right;
68  /// @brief The Bottom edge of the rect.
69  NumType Bottom;
70 
71  ///////////////////////////////////////////////////////////////////////////////
72  // Construction and Destruction
73 
74  /// @brief Blank constructor.
76  Left(0),
77  Top(0),
78  Right(0),
79  Bottom(0)
80  { }
81  /// @brief Descriptive constructor.
82  /// @param RectLeft The Left edge of the Rect.
83  /// @param RectTop The Top edge of the Rect.
84  /// @param RectRight The Right edge of the Rect.
85  /// @param RectBottom The Bottom edge of the Rect.
86  RectBase(const NumType RectLeft, const NumType RectTop, const NumType RectRight, const NumType RectBottom) :
87  Left(RectLeft),
88  Top(RectTop),
89  Right(RectRight),
90  Bottom(RectBottom)
91  { }
92  /// @brief Class destructor.
94  { }
95 
96  ///////////////////////////////////////////////////////////////////////////////
97  // Utility
98 
99  /// @brief Sets all values of this Rect to zero.
100  void SetZero()
101  {
102  this->Left = 0;
103  this->Top = 0;
104  this->Right = 0;
105  this->Bottom = 0;
106  }
107  /// @brief Sets the value of each member of this Rect.
108  /// @param RectLeft The Left edge of the Rect.
109  /// @param RectTop The Top edge of the Rect.
110  /// @param RectRight The Right edge of the Rect.
111  /// @param RectBottom The Bottom edge of the Rect.
112  void SetValues(const NumType RectLeft, const NumType RectTop, const NumType RectRight, const NumType RectBottom)
113  {
114  this->Left = RectLeft;
115  this->Top = RectTop;
116  this->Right = RectRight;
117  this->Bottom = RectBottom;
118  }
119 
120  /// @brief Clamps the position and size of this Rect to the provided size.
121  /// @param Width The width of the area this Rect will be placed in.
122  /// @param Height The height of the area this Rect will be placed in.
123  void ClampToSize(const NumType Width, const NumType Height)
124  {
125  this->Left = std::max(NumType(0),this->Left);
126  this->Top = std::max(NumType(0),this->Top);
127  this->Right = std::min(Width,this->Right);
128  this->Bottom = std::min(Height,this->Bottom);
129  }
130 
131  /// @brief Gets the width of this Rect.
132  /// @return Returns the width of this Rect.
133  NumType GetWidth() const
134  { return this->Right - this->Left; }
135  /// @brief Gets the height of this Rect.
136  /// @return Returns the height of this Rect.
137  NumType GetHeight() const
138  { return this->Bottom - this->Top; }
139 
140  ///////////////////////////////////////////////////////////////////////////////
141  // Operators
142 
143  /// @brief Assignment Operator.
144  /// @param Other The other Rect to copy data from.
145  void operator=(const SelfType& Other)
146  {
147  this->Left = Other.Left;
148  this->Top = Other.Top;
149  this->Right = Other.Right;
150  this->Bottom = Other.Bottom;
151  }
152 
153  /// @brief Equality Operator.
154  /// @param Other The other Rect to compare with.
155  /// @return Returns true if both Rects express the same area.
156  Boole operator==(const SelfType& Other)
157  { return ( this->Left == Other.Left && this->Top == Other.Top && this->Right == Other.Right && this->Bottom == Other.Bottom ); }
158  /// @brief Inequality Operator.
159  /// @param Other The other Rect to compare with.
160  /// @return Returns true if the two Rects express different areas.
161  Boole operator!=(const SelfType& Other)
162  { return ( this->Left != Other.Left || this->Top != Other.Top || this->Right != Other.Right || this->Bottom != Other.Bottom ); }
163  };//RectBase
164 
165  /// @brief Convenience type for a Rect using Whole numbers.
167  /// @brief Convenience type for a Rect using Real numbers.
169 }//Mezzanine
170 
171 #endif
RectBase< Whole > WholeRect
Convenience type for a Rect using Whole numbers.
Definition: rect.h:166
RectBase(const NumType RectLeft, const NumType RectTop, const NumType RectRight, const NumType RectBottom)
Descriptive constructor.
Definition: rect.h:86
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
void SetValues(const NumType RectLeft, const NumType RectTop, const NumType RectRight, const NumType RectBottom)
Sets the value of each member of this Rect.
Definition: rect.h:112
void SetZero()
Sets all values of this Rect to zero.
Definition: rect.h:100
RectBase()
Blank constructor.
Definition: rect.h:75
All the definitions for datatypes as well as some basic conversion functions are defined here...
Boole operator==(const SelfType &Other)
Equality Operator.
Definition: rect.h:156
NumType GetWidth() const
Gets the width of this Rect.
Definition: rect.h:133
RectBase< Real > RealRect
Convenience type for a Rect using Real numbers.
Definition: rect.h:168
Boole operator!=(const SelfType &Other)
Inequality Operator.
Definition: rect.h:161
~RectBase()
Class destructor.
Definition: rect.h:93
NumType Bottom
The Bottom edge of the rect.
Definition: rect.h:69
void operator=(const SelfType &Other)
Assignment Operator.
Definition: rect.h:145
NumType Right
The Right edge of the rect.
Definition: rect.h:67
RectBase< NumType > SelfType
Convenience typedef for describing the type you are working with.
Definition: rect.h:57
NumType Left
The Left edge of the rect.
Definition: rect.h:63
A class for tracking a rectangle by tracking two corners Top, Left and Right, Bottom.
Definition: rect.h:50
void ClampToSize(const NumType Width, const NumType Height)
Clamps the position and size of this Rect to the provided size.
Definition: rect.h:123
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
NumType Top
The Top edge of the rect.
Definition: rect.h:65
NumType GetHeight() const
Gets the height of this Rect.
Definition: rect.h:137