Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

CEGUIRect.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUIRect.cpp
00003         created:        8/3/2004
00004         author:         Paul D Turner
00005         
00006         purpose:        Implements 'Rect' class
00007 *************************************************************************/
00008 /*************************************************************************
00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2.1 of the License, or (at your option) any later version.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021 
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 *************************************************************************/
00026 #include "CEGUIRect.h"
00027 
00028 // Start of CEGUI namespace section
00029 namespace CEGUI
00030 {
00031 /*************************************************************************
00032         Constructor
00033 *************************************************************************/
00034 Rect::Rect(float left, float top, float right, float bottom) :
00035         d_top(top),
00036         d_bottom(bottom),
00037         d_left(left),
00038         d_right(right)
00039 {
00040 }
00041 
00042 /*************************************************************************
00043         Return a Rect object that is the intersection of 'this' with 'rect'
00044 *************************************************************************/
00045 Rect Rect::getIntersection(const Rect& rect) const
00046 {
00047         // check for total exclusion
00048         if ((d_right > rect.d_left) &&
00049                 (d_left < rect.d_right) &&
00050                 (d_bottom > rect.d_top) &&
00051                 (d_top < rect.d_bottom))
00052         {
00053                 Rect temp;
00054 
00055                 // fill in temp with the intersection
00056                 temp.d_left = (d_left > rect.d_left) ? d_left : rect.d_left;
00057                 temp.d_right = (d_right < rect.d_right) ? d_right : rect.d_right;
00058                 temp.d_top = (d_top > rect.d_top) ? d_top : rect.d_top;
00059                 temp.d_bottom = (d_bottom < rect.d_bottom) ? d_bottom : rect.d_bottom;
00060 
00061                 return temp;
00062         }
00063         else
00064         {
00065                 return Rect(0.0f, 0.0f, 0.0f, 0.0f);
00066         }
00067 
00068 }
00069 
00070 /*************************************************************************
00071         Apply an offset the the Rect
00072 *************************************************************************/
00073 Rect& Rect::offset(const Point& pt)
00074 {
00075         d_left          += pt.d_x;
00076         d_right         += pt.d_x;
00077         d_top           += pt.d_y;
00078         d_bottom        += pt.d_y;
00079         return *this;
00080 }
00081 
00082 
00083 /*************************************************************************
00084         Check if a given point is within the Rect
00085 *************************************************************************/
00086 bool Rect::isPointInRect(const Point& pt) const
00087 {
00088         if ((d_left > pt.d_x) ||
00089                 (d_right <= pt.d_x) ||
00090                 (d_top > pt.d_y) ||
00091                 (d_bottom <= pt.d_y))
00092         {
00093                 return false;
00094         }
00095 
00096         return true;
00097 }
00098 
00099 /*************************************************************************
00100         Set location of rect retaining current size.
00101 *************************************************************************/
00102 void Rect::setPosition(const Point& pt)
00103 {
00104         Size sz(getSize());
00105 
00106         d_left = pt.d_x;
00107         d_top  = pt.d_y;
00108         setSize(sz);
00109 }
00110 
00111 
00112 /*************************************************************************
00113         check the size of the Rect object and if it is bigger than 'sz', 
00114         resize it so it isn't.  
00115 *************************************************************************/
00116 Rect& Rect::constrainSizeMax(const Size& sz)
00117 {
00118         if (getWidth() > sz.d_width)
00119         {
00120                 setWidth(sz.d_width);
00121         }
00122 
00123         if (getHeight() > sz.d_height)
00124         {
00125                 setHeight(sz.d_height);
00126         }
00127 
00128         return *this;
00129 }
00130 
00131 
00132 /*************************************************************************
00133         check the size of the Rect object and if it is smaller than 'sz',
00134         resize it so it isn't.
00135 *************************************************************************/
00136 Rect& Rect::constrainSizeMin(const Size& sz)
00137 {
00138         if (getWidth() < sz.d_width)
00139         {
00140                 setWidth(sz.d_width);
00141         }
00142 
00143         if (getHeight() < sz.d_height)
00144         {
00145                 setHeight(sz.d_height);
00146         }
00147 
00148         return *this;
00149 }
00150 
00151 
00152 /*************************************************************************
00153         check the size of the Rect object and if it is bigger than 'max_sz'
00154         or smaller than 'min_sz', resize it so it isn't.
00155 *************************************************************************/
00156 Rect& Rect::constrainSize(const Size& max_sz, const Size& min_sz)
00157 {
00158         Size curr_sz(getSize());
00159 
00160         if (curr_sz.d_width > max_sz.d_width)
00161         {
00162                 setWidth(max_sz.d_width);
00163         }
00164         else if (curr_sz.d_width < min_sz.d_width)
00165         {
00166                 setWidth(min_sz.d_width);
00167         }
00168 
00169         if (curr_sz.d_height > max_sz.d_height)
00170         {
00171                 setHeight(max_sz.d_height);
00172         }
00173         else if (curr_sz.d_height < min_sz.d_height)
00174         {
00175                 setHeight(min_sz.d_height);
00176         }
00177 
00178         return *this;
00179 }
00180 
00181 Rect& Rect::operator=(const Rect& rhs)
00182 {
00183         d_left = rhs.d_left;
00184         d_top = rhs.d_top;
00185         d_right = rhs.d_right;
00186         d_bottom = rhs.d_bottom;
00187 
00188         return *this;
00189 }
00190 
00191 } // End of  CEGUI namespace section

Generated on Wed Feb 16 12:41:07 2005 for Crazy Eddies GUI System by  doxygen 1.3.9.1