Game Accessibility Library logo SourceForge.net Logo
Game Accessibility Suite: CAT/CATRect.h Source File

CATRect.h

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 /// \file    CATRect.h
00003 /// \brief   Rectangles, points, size, etc. for GUI
00004 /// \ingroup CAT
00005 ///
00006 /// Copyright (c) 2002-2008 by Michael Ellison.
00007 /// See COPYING.txt for the \ref gaslicense License (MIT License).
00008 ///
00009 // $Author: mikeellison $
00010 // $Date: 2008-01-23 01:43:25 -0600 (Wed, 23 Jan 2008) $
00011 // $Revision:   $
00012 // $NoKeywords: $
00013 
00014 #ifndef CATRect_H_
00015 #define CATRect_H_
00016 
00017 #pragma pack(push)
00018 #pragma pack(1)
00019 /// CATRECT defines the basic rectangle structure.
00020 ///
00021 /// It should be analogous to other rect structs (e.g. RECT from win32).
00022 struct CATRECT
00023 {
00024    CATInt32   left;
00025    CATInt32   top;
00026    CATInt32   right;
00027    CATInt32   bottom;
00028 };
00029 
00030 /// CATPOINT is a simple 2D integer point
00031 struct CATPOINT
00032 {
00033    CATInt32   x;
00034    CATInt32   y;
00035 };
00036 
00037 /// CATSIZE is a 2D integer size (width = cx, height = cy)
00038 struct CATSIZE
00039 {
00040    CATInt32   cx;
00041    CATInt32   cy;
00042 };
00043 #pragma pack(pop)
00044 
00045 
00046 /// \class CATRect
00047 /// \brief Rectangles for GUI.
00048 /// \ingroup CAT
00049 ///
00050 /// Rectangles include their top,left position, but not their bottom,right
00051 /// position.  (top,left) is inclusive, (bottom,right) is exclusive.
00052 ///
00053 /// In other words, a rectangle of (0,0,1,1) has a width of 1, 
00054 /// and a height of 1.  The point (0,0) is inside the rectangle, but the
00055 /// point (1,1) is just outside the rectangle.
00056 class CATRect : public CATRECT
00057 {
00058    public:
00059       /// Default constructor - sets to all 0's.
00060       CATRect()
00061       {
00062          left = top = right = bottom = 0;
00063       }
00064 
00065       /// Copy constructor
00066       CATRect(const CATRECT& rect)
00067       {
00068          this->left     = rect.left;
00069          this->top      = rect.top;
00070          this->right    = rect.right;
00071          this->bottom   = rect.bottom;
00072       }
00073 
00074       /// Initializing constructor
00075       /// \param l - left position (inclusive)
00076       /// \param t - top position (inclusive)
00077       /// \param r - right position (exclusive)
00078       /// \param b - bottom position (exclusive)
00079       CATRect(CATInt32 l, CATInt32 t, CATInt32 r, CATInt32 b)
00080       {
00081          CATASSERT(l <= r, "Left should be <= right.");
00082          CATASSERT(t <= b, "Top should be <= bottom.");
00083 
00084          this->left     = l;
00085          this->top      = t;
00086          this->right    = r;
00087          this->bottom   = b;
00088       }
00089          
00090       /// Destructor
00091       ~CATRect()
00092       {
00093          CATASSERT(left <= right, "Left should be <= right.");
00094          CATASSERT(top <= bottom, "Top should be <= bottom.");
00095       }
00096 
00097       /// Operator= override for CATRECT structs
00098       CATRect& operator=(const CATRECT& rect)
00099       {
00100          this->left     = rect.left;
00101          this->top      = rect.top;
00102          this->right    = rect.right;
00103          this->bottom   = rect.bottom;
00104          return *this;
00105       }
00106 
00107       /// Operator== override for CATRECT structs
00108       bool operator==(const CATRECT& rect)
00109       {
00110          if ((left == rect.left) &&
00111              (top == rect.top) &&
00112              (right == rect.right) &&
00113              (bottom == rect.bottom))
00114          {
00115             return true;
00116          }
00117          return false;
00118       }
00119 
00120       /// Operator!= override for CATRECT structs
00121       bool operator!=(const CATRECT& rect)
00122       {
00123          if ((left == rect.left) &&
00124              (top == rect.top) &&
00125              (right == rect.right) &&
00126              (bottom == rect.bottom))
00127          {
00128             return false;
00129          }
00130          return true;
00131       }
00132 
00133       /// Width() returns the width of the rect
00134       /// \return CATInt32 - width (right - left).
00135       inline CATInt32 Width() const
00136       {
00137          return right - left;
00138       }
00139 
00140       /// Height() returns the height of the rect
00141       /// \return CATInt32 - heigt (bottom - top)
00142       inline CATInt32 Height() const
00143       {
00144          return bottom - top;
00145       }
00146    
00147       /// Size() returns a CATSIZE struct with the
00148       /// width and height.
00149       /// \return CATSIZE - width and height of rectangle      
00150       inline CATSIZE Size() const
00151       {
00152          CATSIZE rsize = {right - left, bottom - top};
00153          return rsize;
00154       }
00155 
00156       /// Origin() returns the current top,left of the
00157       /// rectangle.
00158       /// \return CATPOINT - top left of rectangle
00159       inline CATPOINT Origin() const
00160       {         
00161          CATPOINT point = {left,top};
00162          return point;         
00163       }
00164 
00165       /// CenterX() returns the center X position.
00166       /// If the width is odd, returns the leftmost edge of center 
00167       /// (e.g. rounds down)
00168       inline CATInt32 CenterX() const
00169       {
00170          return (left + right)/2;
00171       }
00172       
00173       /// CenterY() returns the center Y position.
00174       /// If the height is odd, returns the upper edge of center 
00175       /// (e.g. rounds down)
00176       inline CATInt32 CenterY() const
00177       {
00178          return (top + bottom)/2;
00179       }
00180 
00181       /// Center() returns the point in the center of the rectangle.
00182       /// Values are rounded down.
00183       inline CATPOINT Center() const
00184       {
00185          CATPOINT point = {CenterX(), CenterY() };
00186          return point;
00187       }
00188 
00189       /// ZeroOrigin() moves the top-left corner to the
00190       /// 0,0 origin.
00191       void ZeroOrigin()
00192       {
00193          right    -= left;
00194          bottom   -= top;
00195          left     = 0;
00196          top      = 0;         
00197       }
00198 
00199       /// SetOrigin() moves the top-left corner to the specified
00200       /// location
00201       inline void SetOrigin(CATPOINT& point)
00202       {
00203          SetOrigin(point.x, point.y);
00204       }
00205 
00206       void SetOrigin(CATInt32 x, CATInt32 y)
00207       {
00208          ZeroOrigin();
00209          left   += x;
00210          right  += x;
00211          top    += y;
00212          bottom += y;
00213       }
00214 
00215 
00216       /// Offset() offsets the rect by the x,y values
00217       /// in the point.
00218       ///
00219       /// \param point - contains the x,y values to be added to the rectangle
00220       void Offset(const CATPOINT& point)
00221       {
00222          Offset(point.x, point.y);
00223       }
00224 
00225       /// Offset() offsets the rect by the specified x,y.
00226       /// \param x - x offset to be added
00227       /// \param y - y offset to be added
00228       void Offset(CATInt32 x, CATInt32 y)
00229       {
00230          left     += x;
00231          right    += x;
00232          top      += y;
00233          bottom   += y;
00234       }
00235 
00236       /// Resize() resizes the rectangle to the specified size.
00237       /// Only the bottom right corner is moved.
00238       /// \param size - width and height of new rectangle
00239       void Resize(const CATSIZE& size)
00240       {
00241          right = left + size.cx;
00242          bottom = top + size.cy;
00243       }
00244 
00245       /// Resize() resizes the rectangle to the specified size.
00246       ///
00247       /// \param width - new width
00248       /// \param height - new height
00249       void Resize(CATInt32 width, CATInt32 height)
00250       {
00251          right = left + width;
00252          bottom = top + height;
00253       }
00254 
00255       /// Fix() fixes the rectangle if it's broken.
00256       /// Right now, just flips left/right and top/bottom
00257       /// if they are swapped.
00258       void Fix()
00259       {
00260          if (left > right)
00261             CATSwap(left, right);
00262 
00263          if (top > bottom)
00264             CATSwap(top, bottom);
00265       }
00266 
00267       /// Set() sets the rectangle to the specified coordinates
00268       /// \param l - new left position (inclusive)
00269       /// \param t - new top position (inclusive)
00270       /// \param r - new right position (exclusive)
00271       /// \param b - new bottom position (exclusive)
00272       void Set(CATInt32 l, CATInt32 t, CATInt32 r, CATInt32 b)
00273       {
00274          this->left = l;
00275          this->top = t;
00276          this->right = r;
00277          this->bottom = b;
00278       }
00279 
00280       /// Set() sets the rectangle to the specified coordinates
00281       /// \param topLeft - top,left position
00282       /// \param widthHeight -  width/height of rect
00283       void Set(const CATPOINT& topLeft, const CATSIZE& widthHeight)
00284       {
00285          this->left = topLeft.x;
00286          this->top  = topLeft.y;
00287          this->right  = topLeft.x + widthHeight.cx;
00288          this->bottom = topLeft.y + widthHeight.cy;
00289       }
00290 
00291 
00292       /// InRect() returns true if the point is inside the rectangle,
00293       /// and false if it is outside.
00294       ///
00295       /// A point is outside the rectangle if it is on the bottom right
00296       /// corner. e.g. (top,left) is inclusive, (bottom,right) is exclusive.
00297       /// 
00298       bool InRect(const CATPOINT& point) const
00299       {
00300          return InRect(point.x, point.y);
00301       }
00302 
00303       /// InRect() returns true if the point is inside the rectangle,
00304       /// and false if it is outside.
00305       ///
00306       /// A point is outside the rectangle if it is on the bottom right
00307       /// corner. e.g. (top,left) is inclusive, (bottom,right) is exclusive.
00308       /// 
00309       /// \param x - x position of point to check
00310       /// \param y - y position of point to check
00311       /// \return bool - true if point is inside rect, false otherwise
00312       bool InRect(CATInt32 x, CATInt32 y) const
00313       {
00314          if ( ((x >= left) && (x < right)) &&
00315               ((y >= top)  && (y < bottom)) )
00316          {
00317             return true;
00318          }
00319          return false;
00320       }
00321 
00322       /// Intersect() determines if two rectangles intersect, then 
00323       /// calculates the rectangle of intersection between two rectangles
00324       /// if any.
00325       ///
00326       /// \param srcRect - rectangle to check for intersection against
00327       ///        this one.
00328       /// \param intersectRect - ptr to rectangle to receive the
00329       ///        intersecting rectangle between the two.
00330       /// \return bool - true if they intersect, false otherwise.
00331       bool Intersect( const CATRect& srcRect, CATRect* intersectRect = 0) const
00332       {           
00333          // Skip if totally out 
00334          // Remember bottom/right are exclusive, hence >= / <= 
00335          if (  (this->right   <= srcRect.left) || 
00336                (this->left    >= srcRect.right) ||
00337                (this->bottom  <= srcRect.top) ||
00338                (this->top     >= srcRect.bottom) ||
00339                (this->Width() == 0) ||
00340                (this->Height() == 0) ||
00341                (srcRect.Width() == 0) ||
00342                (srcRect.Height() == 0) )
00343          {
00344             // Zero rect if present
00345             if (intersectRect != 0)
00346             {
00347                intersectRect->left = intersectRect->right  = 0;
00348                intersectRect->top  = intersectRect->bottom = 0;
00349             }
00350 
00351             return false;
00352          }
00353 
00354          // If we don't need to know intersect, then bail here         
00355          if (intersectRect == 0)
00356          {
00357             return true;
00358          }
00359          
00360          // Calc intersection between the two
00361          intersectRect->left   = CATMax(left,    srcRect.left);
00362          intersectRect->top    = CATMax(top,     srcRect.top);
00363          intersectRect->right  = CATMin(right,   srcRect.right);
00364          intersectRect->bottom = CATMin(bottom,  srcRect.bottom);
00365          return true;
00366       }
00367       
00368       /// Inside() determines if the inside rect is fully contained
00369       /// by *this rect.
00370       ///
00371       /// \param insideRect - Rect to check
00372       /// \return bool - true if insideRect is fully contained by *this.      
00373       bool Inside(const CATRect& insideRect) const
00374       {
00375          if (  ( this->left   <= insideRect.left   ) &&
00376                ( this->right  >= insideRect.right  ) &&
00377                ( this->top    <= insideRect.top    ) &&
00378                ( this->bottom >= insideRect.bottom ) )
00379          {
00380             return true;
00381          }
00382          return false;
00383       }
00384 
00385       /// Stretch() stretches the rectangle by the specified amount, 
00386       /// optionally keeping it in bounds of the bounds rectangle.
00387       void Stretch(CATInt32 stretchSize = 1, const CATRect* bounds = 0)
00388       {
00389             left   -= stretchSize;
00390             top    -= stretchSize;
00391             right  += stretchSize;
00392             bottom += stretchSize;
00393 
00394             if (bounds)
00395             {
00396                 if ( left < bounds->left)
00397                     left = bounds->left;
00398                 if ( top  < bounds->top)
00399                     top = bounds->top;
00400                 if (right > bounds->right)
00401                     right = bounds->right;
00402                 if (bottom > bounds->bottom)
00403                     right = bounds->bottom;
00404             }            
00405       }
00406 
00407       void Shrink(CATInt32 stretchSize = 1)
00408       {
00409             left   += stretchSize;
00410             top    += stretchSize;
00411             right  -= stretchSize;
00412             bottom -= stretchSize;
00413       }
00414 
00415 };
00416 
00417 #endif // CATRect_H_
00418 
00419 

Generated on Mon Feb 11 04:09:46 2008 for Game Accessibility Suite by doxygen 1.5.4