wg_rect.cpp

00001 // wg_rect.cpp
00002 //
00003 // CRect class implementation
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 #include "wgui_include_config.h"
00026 #include "wg_rect.h"
00027 #include "wg_error.h"
00028 #include "std_ex.h"
00029 #include <algorithm>
00030 
00031 
00032 namespace wGui
00033 {
00034 
00035 
00036 SDL_Rect CRect::SDLRect(void) const
00037 {
00038      SDL_Rect sdlRect;
00039      //Normalize the rect...
00040      sdlRect.x = stdex::safe_static_cast<short int>(std::min(m_Left, m_Right));
00041      sdlRect.y = stdex::safe_static_cast<short int>(std::min(m_Top, m_Bottom));
00042      sdlRect.w = stdex::safe_static_cast<short int>(Width());
00043      sdlRect.h = stdex::safe_static_cast<short int>(Height());
00044 
00045      return sdlRect;
00046 }
00047 
00048 
00049 // assignment operator
00050 CRect& CRect::operator=(const CRect& r)
00051 {
00052      m_Top = r.Top();
00053      m_Left = r.Left();
00054      m_Right = r.Right();
00055      m_Bottom = r.Bottom();
00056 
00057      return *this;
00058 }
00059 
00060 
00061 CRect CRect::operator+(const CPoint& p) const
00062 {
00063      CRect result(m_Left + p.XPos(), m_Top + p.YPos(), m_Right + p.XPos(), m_Bottom + p.YPos());
00064 
00065      return result;
00066 }
00067 
00068 
00069 CRect CRect::operator-(const CPoint& p) const
00070 {
00071      CRect result(m_Left - p.XPos(), m_Top - p.YPos(), m_Right - p.XPos(), m_Bottom - p.YPos());
00072 
00073      return result;
00074 }
00075 
00076 
00077 CRect& CRect::Grow(int iGrowAmount)
00078 {
00079      m_Top -= iGrowAmount;
00080      m_Left -= iGrowAmount;
00081      m_Right += iGrowAmount;
00082      m_Bottom += iGrowAmount;
00083 
00084      return *this;
00085 }
00086 
00087 
00088 CRect& CRect::Move(int iOffsetX, int iOffsetY)
00089 {
00090      m_Left += iOffsetX;
00091      m_Top += iOffsetY;
00092      m_Right += iOffsetX;
00093      m_Bottom += iOffsetY;
00094 
00095      return *this;
00096 }
00097 
00098 
00099 bool CRect::Overlaps(const CRect& r) const
00100 {
00101      bool bOverlap = false;
00102 
00103      if (m_Right >= r.m_Left && m_Left <= r.m_Right && m_Top <= r.m_Bottom && m_Bottom >= r.m_Top)
00104      {
00105           bOverlap = true;
00106      }
00107 
00108      return bOverlap;
00109 }
00110 
00111 
00112 CRect& CRect::ClipTo(const CRect& r)
00113 {
00114      if (! Overlaps(r))
00115      {
00116           m_Left = 0;
00117           m_Top = 0;
00118           m_Right = 0;
00119           m_Bottom = 0;
00120      }
00121      else
00122      {
00123           if (m_Left < r.m_Left)
00124           {
00125                m_Left = r.m_Left;
00126           }
00127           if (m_Top < r.m_Top)
00128           {
00129                m_Top = r.m_Top;
00130           }
00131           if (m_Right > r.m_Right)
00132           {
00133                m_Right = r.m_Right;
00134           }
00135           if (m_Bottom > r.m_Bottom)
00136           {
00137                m_Bottom = r.m_Bottom;
00138           }
00139      }
00140 
00141      return *this;
00142 }
00143 
00144 
00145 // test to see if the point lies within the rect
00146 unsigned int CRect::HitTest(const CPoint& p) const
00147 {
00148      unsigned int eRelPos = 0;
00149 
00150      eRelPos |= (p.XPos() < m_Left) ? RELPOS_LEFT : 0;
00151      eRelPos |= (p.YPos() < m_Top) ? RELPOS_ABOVE: 0;
00152      eRelPos |= (p.XPos() > m_Right) ? RELPOS_RIGHT : 0;
00153      eRelPos |= (p.YPos() > m_Bottom) ? RELPOS_BELOW: 0;
00154      eRelPos |= (p.XPos() >= m_Left && p.XPos() <= m_Right &&
00155           p.YPos() >= m_Top && p.YPos() <= m_Bottom) ? RELPOS_INSIDE : 0;
00156 
00157      return eRelPos;
00158 }
00159 
00160 }
00161 

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