wg_rect.h

00001 // wg_rect.h
00002 //
00003 // CRect class interface
00004 //
00005 //
00006 // Copyright (c) 2002-2004 Rob Wiskow
00007 // rob-dev@boxedchaos.com
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 //
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017 // Lesser General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00022 //
00023 
00024 
00025 #ifndef _WG_RECT_H_
00026 #define _WG_RECT_H_
00027 
00028 #include "wg_point.h"
00029 #include "std_ex.h"
00030 #include "SDL.h"
00031 #include <stdlib.h>
00032 #include <math.h>
00033 
00034 namespace wGui
00035 {
00036 
00038 
00039 class CRect
00040 {
00041 public:
00043      CRect() : m_Left(0), m_Right(0), m_Top(0), m_Bottom(0) { }
00044 
00049      CRect(const int left, const int top, const int right, const int bottom) :
00050           m_Left(left), m_Right(right), m_Top(top), m_Bottom(bottom) { }  // constructor
00051 
00055      CRect(const CPoint& p1, const CPoint& p2) :
00056           m_Left(p1.XPos()), m_Right(p2.XPos()), m_Top(p1.YPos()), m_Bottom(p2.YPos()) { }  // constructor
00057 
00060      CRect(const CRect& r) :
00061           m_Left(r.m_Left), m_Right(r.m_Right), m_Top(r.m_Top), m_Bottom(r.m_Bottom) { }  // constructor
00062 
00064      virtual ~CRect() { }
00065 
00068      void SetTop(const int top) { m_Top = top; }
00069 
00072      void SetLeft(const int left) { m_Left = left; }
00073 
00076      void SetRight(const int right) { m_Right = right; }
00077 
00080      void SetBottom(const int bottom) { m_Bottom = bottom; }
00081 
00084      int Top(void) const { return m_Top; }
00085 
00088      int Left(void) const { return m_Left; }
00089 
00092      int Right(void) const { return m_Right; }
00093 
00096      int Bottom(void) const { return m_Bottom; }
00097 
00100      CPoint TopLeft(void) const { return CPoint(m_Left, m_Top); }
00101 
00104      CPoint TopRight(void) const { return CPoint(m_Right, m_Top); }
00105 
00108      CPoint BottomLeft(void) const { return CPoint(m_Left, m_Bottom); }
00109 
00112      CPoint BottomRight(void) const { return CPoint(m_Right, m_Bottom); }
00113 
00116      CPoint Center(void) const { return CPoint((m_Left + m_Right) / 2, (m_Top + m_Bottom) / 2); }
00117 
00120      CPoint CenterLeft(void) const { return CPoint( m_Left, (m_Top + m_Bottom) / 2); }
00121 
00124      CPoint CenterTop(void) const { return CPoint( (m_Left + m_Right) / 2, m_Top ); }
00125 
00128      CPoint CenterBottom(void) const { return CPoint( (m_Left + m_Right) / 2, m_Bottom ); }
00129 
00132      CPoint CenterRight(void) const { return CPoint( m_Right, (m_Top + m_Bottom) / 2); }
00133 
00134 
00137      SDL_Rect SDLRect(void) const;
00138 
00141      int Width(void) const { return abs(m_Right - m_Left + 1); }
00142 
00145      int Height(void) const { return abs(m_Bottom - m_Top + 1); }
00146 
00149      CRect SizeRect(void) const { return CRect(0, 0, abs(m_Right - m_Left), abs(m_Bottom - m_Top)); }
00150 
00152      CRect& operator=(const CRect& r);  // assignment operator
00153 
00156      CRect operator+(const CPoint& p) const;
00157 
00160      CRect operator-(const CPoint& p) const;
00161 
00164      bool operator==(const CRect& r) const
00165           { return (m_Left == r.m_Left && m_Top == r.m_Top && m_Right == r.m_Right && m_Bottom == r.m_Bottom); }
00166 
00169      bool operator!=(const CRect& r) const
00170           { return (m_Left != r.m_Left || m_Top != r.m_Top || m_Right != r.m_Right || m_Bottom != r.m_Bottom); }
00171 
00177      CRect& Grow(int iGrowAmount);
00178 
00183      CRect& Move(int iOffsetX, int iOffsetY);
00184 
00188      bool Overlaps(const CRect& r) const;
00189 
00192      CRect& ClipTo(const CRect& r);
00193 
00195      enum ERelativePosition
00196      {
00197           RELPOS_INVALID = 0, 
00198           RELPOS_ABOVE = 1, 
00199           RELPOS_BELOW = 2, 
00200           RELPOS_LEFT = 4, 
00201           RELPOS_RIGHT = 8, 
00202           RELPOS_INSIDE = 16 
00203      };
00204 
00208      unsigned int HitTest(const CPoint& p) const;
00209 
00212      std::wstring ToString(void) const
00213           { return stdex::itoa(m_Left) + L"," + stdex::itoa(m_Top) + L"," + stdex::itoa(m_Right) + L"," + stdex::itoa(m_Bottom); }
00214 
00215 protected:
00217      int m_Left;
00218 
00220      int m_Right;
00221 
00223      int m_Top;
00224 
00226      int m_Bottom;
00227 };
00228 
00229 }
00230 
00231 
00232 #include "unit_tests/wg_rect_unittests.h"
00233 
00234 #endif // _WG_RECT_H_
00235 

Generated on Wed May 16 23:11:26 2007 for wGui by  doxygen 1.5.1