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

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 Rect::Rect(Point pos, Size sz) :
00043     d_top(pos.d_y),
00044     d_bottom(pos.d_y + sz.d_height),
00045     d_left(pos.d_x),
00046     d_right(pos.d_x + sz.d_width)
00047 {
00048 }
00049 
00050 
00051 /*************************************************************************
00052         Return a Rect object that is the intersection of 'this' with 'rect'
00053 *************************************************************************/
00054 Rect Rect::getIntersection(const Rect& rect) const
00055 {
00056         // check for total exclusion
00057         if ((d_right > rect.d_left) &&
00058                 (d_left < rect.d_right) &&
00059                 (d_bottom > rect.d_top) &&
00060                 (d_top < rect.d_bottom))
00061         {
00062                 Rect temp;
00063 
00064                 // fill in temp with the intersection
00065                 temp.d_left = (d_left > rect.d_left) ? d_left : rect.d_left;
00066                 temp.d_right = (d_right < rect.d_right) ? d_right : rect.d_right;
00067                 temp.d_top = (d_top > rect.d_top) ? d_top : rect.d_top;
00068                 temp.d_bottom = (d_bottom < rect.d_bottom) ? d_bottom : rect.d_bottom;
00069 
00070                 return temp;
00071         }
00072         else
00073         {
00074                 return Rect(0.0f, 0.0f, 0.0f, 0.0f);
00075         }
00076 
00077 }
00078 
00079 /*************************************************************************
00080         Apply an offset the the Rect
00081 *************************************************************************/
00082 Rect& Rect::offset(const Point& pt)
00083 {
00084         d_left          += pt.d_x;
00085         d_right         += pt.d_x;
00086         d_top           += pt.d_y;
00087         d_bottom        += pt.d_y;
00088         return *this;
00089 }
00090 
00091 
00092 /*************************************************************************
00093         Check if a given point is within the Rect
00094 *************************************************************************/
00095 bool Rect::isPointInRect(const Point& pt) const
00096 {
00097         if ((d_left > pt.d_x) ||
00098                 (d_right <= pt.d_x) ||
00099                 (d_top > pt.d_y) ||
00100                 (d_bottom <= pt.d_y))
00101         {
00102                 return false;
00103         }
00104 
00105         return true;
00106 }
00107 
00108 /*************************************************************************
00109         Set location of rect retaining current size.
00110 *************************************************************************/
00111 void Rect::setPosition(const Point& pt)
00112 {
00113         Size sz(getSize());
00114 
00115         d_left = pt.d_x;
00116         d_top  = pt.d_y;
00117         setSize(sz);
00118 }
00119 
00120 
00121 /*************************************************************************
00122         check the size of the Rect object and if it is bigger than 'sz', 
00123         resize it so it isn't.  
00124 *************************************************************************/
00125 Rect& Rect::constrainSizeMax(const Size& sz)
00126 {
00127         if (getWidth() > sz.d_width)
00128         {
00129                 setWidth(sz.d_width);
00130         }
00131 
00132         if (getHeight() > sz.d_height)
00133         {
00134                 setHeight(sz.d_height);
00135         }
00136 
00137         return *this;
00138 }
00139 
00140 
00141 /*************************************************************************
00142         check the size of the Rect object and if it is smaller than 'sz',
00143         resize it so it isn't.
00144 *************************************************************************/
00145 Rect& Rect::constrainSizeMin(const Size& sz)
00146 {
00147         if (getWidth() < sz.d_width)
00148         {
00149                 setWidth(sz.d_width);
00150         }
00151 
00152         if (getHeight() < sz.d_height)
00153         {
00154                 setHeight(sz.d_height);
00155         }
00156 
00157         return *this;
00158 }
00159 
00160 
00161 /*************************************************************************
00162         check the size of the Rect object and if it is bigger than 'max_sz'
00163         or smaller than 'min_sz', resize it so it isn't.
00164 *************************************************************************/
00165 Rect& Rect::constrainSize(const Size& max_sz, const Size& min_sz)
00166 {
00167         Size curr_sz(getSize());
00168 
00169         if (curr_sz.d_width > max_sz.d_width)
00170         {
00171                 setWidth(max_sz.d_width);
00172         }
00173         else if (curr_sz.d_width < min_sz.d_width)
00174         {
00175                 setWidth(min_sz.d_width);
00176         }
00177 
00178         if (curr_sz.d_height > max_sz.d_height)
00179         {
00180                 setHeight(max_sz.d_height);
00181         }
00182         else if (curr_sz.d_height < min_sz.d_height)
00183         {
00184                 setHeight(min_sz.d_height);
00185         }
00186 
00187         return *this;
00188 }
00189 
00190 Rect& Rect::operator=(const Rect& rhs)
00191 {
00192         d_left = rhs.d_left;
00193         d_top = rhs.d_top;
00194         d_right = rhs.d_right;
00195         d_bottom = rhs.d_bottom;
00196 
00197         return *this;
00198 }
00199 
00200 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:33 2005 for Crazy Eddies GUI System by  doxygen 1.4.3