Crazy Eddie's GUI System  0.8.4
Rect.h
00001 /***********************************************************************
00002         created:        14/2/2011
00003         author:         Martin Preisler (reworked from code by Paul D Turner)
00004         
00005         purpose:        Defines 'Rect' class
00006 *************************************************************************/
00007 /***************************************************************************
00008  *   Copyright (C) 2004 - 2011 Paul D Turner & The CEGUI Development Team
00009  *
00010  *   Permission is hereby granted, free of charge, to any person obtaining
00011  *   a copy of this software and associated documentation files (the
00012  *   "Software"), to deal in the Software without restriction, including
00013  *   without limitation the rights to use, copy, modify, merge, publish,
00014  *   distribute, sublicense, and/or sell copies of the Software, and to
00015  *   permit persons to whom the Software is furnished to do so, subject to
00016  *   the following conditions:
00017  *
00018  *   The above copyright notice and this permission notice shall be
00019  *   included in all copies or substantial portions of the Software.
00020  *
00021  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00022  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00023  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00024  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
00025  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00026  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00027  *   OTHER DEALINGS IN THE SOFTWARE.
00028  ***************************************************************************/
00029 #ifndef _CEGUIRect_h_
00030 #define _CEGUIRect_h_
00031 
00032 #include "CEGUI/Vector.h"
00033 #include "CEGUI/Size.h"
00034 
00035 // Start of CEGUI namespace section
00036 namespace CEGUI
00037 {
00042 template<typename T>
00043 class Rect:
00044     public AllocatedObject<Rect<T> >
00045 {
00046 public:
00047     typedef T value_type;
00048 
00049         inline Rect()
00050     {}
00051 
00052         inline Rect(const T& left, const T& top, const T& right, const T& bottom):
00053         d_min(left, top),
00054         d_max(right, bottom)
00055     {}
00056 
00057     inline Rect(const Vector2<T>& min, const Vector2<T>& max):
00058         d_min(min),
00059         d_max(max)
00060     {}
00061 
00062     inline Rect(const Vector2<T>& pos, const Size<T>& size):
00063         d_min(pos),
00064         d_max(pos + Vector2<T>(size.d_width, size.d_height))
00065     {}
00066 
00067     inline Rect(const Rect& r):
00068         d_min(r.d_min),
00069         d_max(r.d_max)
00070     {}
00071 
00072     inline Rect& operator=(const Rect& rhs)
00073     {
00074         d_min = rhs.d_min;
00075         d_max = rhs.d_max;
00076 
00077         return *this;
00078     }
00079 
00080     inline void left(const T& v)
00081     {
00082         d_min.d_x = v;
00083     }
00084 
00085     inline const T& left() const
00086     {
00087         return d_min.d_x;
00088     }
00089 
00090     inline void top(const T& v)
00091     {
00092         d_min.d_y = v;
00093     }
00094 
00095     inline const T& top() const
00096     {
00097         return d_min.d_y;
00098     }
00099 
00100     inline void right(const T& v)
00101     {
00102         d_max.d_x = v;
00103     }
00104 
00105     inline const T& right() const
00106     {
00107         return d_max.d_x;
00108     }
00109 
00110     inline void bottom(const T& v)
00111     {
00112         d_max.d_y = v;
00113     }
00114 
00115     inline const T& bottom() const
00116     {
00117         return d_max.d_y;
00118     }
00119 
00124         void setPosition(const Vector2<T>& min)
00125     {
00126         const Size<T> size = getSize();
00127         d_min = min;
00128         setSize(size);
00129     }
00130 
00135         const Vector2<T>& getPosition() const
00136     {
00137         return d_min;
00138     }
00139 
00140     void setSize(const Size<T>& size)
00141     {
00142         d_max = d_min + Vector2<T>(size.d_width, size.d_height);
00143     }
00144 
00149         inline Size<T> getSize() const
00150     {
00151         return Size<T>(getWidth(), getHeight());
00152     }
00153 
00154     void setWidth(const T& w)
00155     {
00156         d_max.d_x = d_min.d_x + w;
00157     }
00158 
00163         inline T getWidth() const
00164     {
00165         return d_max.d_x - d_min.d_x;
00166     }
00167 
00168     void setHeight(const T& h)
00169     {
00170         d_max.d_y = d_min.d_y + h;
00171     }
00172 
00177         inline T getHeight() const
00178     {
00179         return d_max.d_y - d_min.d_y;
00180     }
00181 
00190         inline Rect getIntersection(const Rect& rect) const
00191     {
00192         if ((d_max.d_x > rect.d_min.d_x) &&
00193                     (d_min.d_x < rect.d_max.d_x) &&
00194                     (d_max.d_y > rect.d_min.d_y) &&
00195                     (d_min.d_y < rect.d_max.d_y))
00196             {
00197                     Rect ret;
00198 
00199                     // fill in ret with the intersection
00200                     ret.d_min.d_x = (d_min.d_x > rect.d_min.d_x) ? d_min.d_x : rect.d_min.d_x;
00201                     ret.d_max.d_x = (d_max.d_x < rect.d_max.d_x) ? d_max.d_x : rect.d_max.d_x;
00202                     ret.d_min.d_y = (d_min.d_y > rect.d_min.d_y) ? d_min.d_y : rect.d_min.d_y;
00203                     ret.d_max.d_y = (d_max.d_y < rect.d_max.d_y) ? d_max.d_y : rect.d_max.d_y;
00204 
00205                     return ret;
00206             }
00207             else
00208             {
00209                     return Rect(0.0f, 0.0f, 0.0f, 0.0f);
00210             }
00211     }
00212 
00223         inline void offset(const Vector2<T>& v)
00224     {
00225         d_min += v;
00226         d_max += v;
00227     }
00228 
00239         inline bool isPointInRect(const Vector2<T>& v) const
00240     {
00241                 if ((d_min.d_x >  v.d_x) ||
00242                     (d_max.d_x <= v.d_x) ||
00243                     (d_min.d_y >  v.d_y) ||
00244                     (d_max.d_y <= v.d_y))
00245             {
00246                     return false;
00247             }
00248 
00249             return true;
00250     }
00251 
00252 
00263         void constrainSizeMax(const Size<T>& size)
00264     {
00265         if (getWidth() > size.d_width)
00266             {
00267                     setWidth(size.d_width);
00268             }
00269 
00270             if (getHeight() > size.d_height)
00271             {
00272                     setHeight(size.d_height);
00273             }
00274     }
00275 
00276 
00287         void constrainSizeMin(const Size<T>& size)
00288     {
00289         if (getWidth() < size.d_width)
00290             {
00291                     setWidth(size.d_width);
00292             }
00293 
00294             if (getHeight() < size.d_height)
00295             {
00296                     setHeight(size.d_height);
00297             }
00298     }
00299 
00300 
00314         void constrainSize(const Size<T>& max_sz, const Size<T>& min_sz)
00315     {
00316         Size<T> curr_sz(getSize());
00317 
00318             if (curr_sz.d_width > max_sz.d_width)
00319             {
00320                     setWidth(max_sz.d_width);
00321             }
00322             else if (curr_sz.d_width < min_sz.d_width)
00323             {
00324                     setWidth(min_sz.d_width);
00325             }
00326 
00327             if (curr_sz.d_height > max_sz.d_height)
00328             {
00329                     setHeight(max_sz.d_height);
00330             }
00331             else if (curr_sz.d_height < min_sz.d_height)
00332             {
00333                     setHeight(min_sz.d_height);
00334             }
00335     }
00336 
00337 
00338         /*************************************************************************
00339                 Operators
00340         *************************************************************************/
00341         inline bool operator==(const Rect& rhs) const
00342         {
00343                 return ((d_min == rhs.d_min) && (d_max == rhs.d_max));
00344         }
00345 
00346         inline bool operator!=(const Rect& rhs) const
00347     {
00348         return !operator==(rhs);
00349     }
00350 
00351     inline Rect operator*(T scalar) const
00352     {
00353         return Rect(d_min * scalar, d_max * scalar);
00354     }
00355 
00356     const Rect& operator*=(T scalar)
00357     {
00358         d_min *= scalar;
00359         d_max *= scalar;
00360         return *this;
00361     }
00362 
00363         Rect operator+(const Rect& r) const
00364     {
00365         return Rect(d_min + r.d_min, d_max + r.d_max);
00366     }
00367     
00368     inline friend std::ostream& operator << (std::ostream& s, const Rect& v)
00369     {
00370         s << "CEGUI::Rect<" << typeid(T).name() << ">(" << v.d_min.d_x << ", " << v.d_min.d_y << ", " << v.d_max.d_x << ", " << v.d_max.d_y << ")";
00371         return s;
00372     }
00373     
00375     inline static Rect zero()
00376     {
00377         return Rect(Vector2<T>::zero(), Size<T>::zero());
00378     }
00379     
00380     /*************************************************************************
00381         Data Fields
00382     *************************************************************************/
00383     Vector2<T> d_min;
00384     Vector2<T> d_max;
00385 
00386     // d_min.d_x is former d_left
00387     // d_min.d_y is former d_top
00388     // d_max.d_x is former d_right
00389     // d_max.d_y is former d_bottom
00390 };
00391 
00392 // the main reason for this is to keep C++ API in sync with other languages
00393 typedef Rect<float> Rectf;
00394 
00395 // we need to allow URect to be multiplied by floats, this is the most elegant way to do that
00396 inline Rect<UDim> operator * (const Rect<UDim>& v, const float c)
00397 {
00398     return Rect<UDim>(v.d_min * c, v.d_max * c);
00399 }
00400 
00401 typedef Rect<UDim> URect;
00402 
00403 } // End of  CEGUI namespace section
00404 
00405 
00406 #endif  // end of guard _CEGUIRect_h_
00407 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends