MyGUI  3.2.1
MyGUI_TRect.h
Go to the documentation of this file.
00001 /*
00002  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
00003  * Distributed under the MIT License
00004  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
00005  */
00006 
00007 #ifndef __MYGUI_TRECT_H__
00008 #define __MYGUI_TRECT_H__
00009 
00010 #include "MyGUI_Prerequest.h"
00011 
00012 namespace MyGUI
00013 {
00014     namespace types
00015     {
00016 
00017         template<typename T>
00018         struct TRect
00019         {
00020             T left;
00021             T top;
00022             T right;
00023             T bottom;
00024 
00025             TRect() :
00026                 left(0),
00027                 top(0),
00028                 right(0),
00029                 bottom(0)
00030             {
00031             }
00032 
00033             TRect(T const& _left, T const& _top, T const& _right, T const& _bottom) :
00034                 left(_left),
00035                 top(_top),
00036                 right(_right),
00037                 bottom(_bottom)
00038             {
00039             }
00040 
00041             TRect(TRect const& _obj) :
00042                 left(_obj.left),
00043                 top(_obj.top),
00044                 right(_obj.right),
00045                 bottom(_obj.bottom)
00046             {
00047             }
00048 
00049             TRect& operator -= (TRect const& _obj)
00050             {
00051                 left -= _obj.left;
00052                 top -= _obj.top;
00053                 right -= _obj.right;
00054                 bottom -= _obj.bottom;
00055                 return *this;
00056             }
00057 
00058             TRect& operator += (TRect const& _obj)
00059             {
00060                 left += _obj.left;
00061                 top += _obj.top;
00062                 right += _obj.right;
00063                 bottom += _obj.bottom;
00064                 return *this;
00065             }
00066 
00067             TRect operator - (TRect const& _obj) const
00068             {
00069                 return TRect(left - _obj.left, top - _obj.top, right - _obj.right, bottom - _obj.bottom);
00070             }
00071 
00072             TRect operator + (TRect const& _obj) const
00073             {
00074                 return TRect(left + _obj.left, top + _obj.top, right + _obj.right, bottom + _obj.bottom);
00075             }
00076 
00077             TRect& operator = (TRect const& _obj)
00078             {
00079                 left = _obj.left;
00080                 top = _obj.top;
00081                 right = _obj.right;
00082                 bottom = _obj.bottom;
00083                 return *this;
00084             }
00085 
00086             template<typename U>
00087             TRect& operator = (TRect<U> const& _obj)
00088             {
00089                 left = _obj.left;
00090                 top = _obj.top;
00091                 right = _obj.right;
00092                 bottom = _obj.bottom;
00093                 return *this;
00094             }
00095 
00096             bool operator == (TRect const& _obj) const
00097             {
00098                 return ((left == _obj.left) && (top == _obj.top) && (right == _obj.right) && (bottom == _obj.bottom));
00099             }
00100 
00101             bool operator != (TRect const& _obj) const
00102             {
00103                 return !((left == _obj.left) && (top == _obj.top) && (right == _obj.right) && (bottom == _obj.bottom));
00104             }
00105 
00106             T width() const
00107             {
00108                 return right - left;
00109             }
00110 
00111             T height() const
00112             {
00113                 return bottom - top;
00114             }
00115 
00116             void clear()
00117             {
00118                 left = top = right = bottom = 0;
00119             }
00120 
00121             void set(T const& _left, T const& _top, T const& _right, T const& _bottom)
00122             {
00123                 left = _left;
00124                 top = _top;
00125                 right = _right;
00126                 bottom = _bottom;
00127             }
00128 
00129             void swap(TRect& _value)
00130             {
00131                 TRect tmp = _value;
00132                 _value = *this;
00133                 *this = tmp;
00134             }
00135 
00136             bool empty() const
00137             {
00138                 return ((left == 0) && (top == 0) && (right == 0) && (bottom == 0));
00139             }
00140 
00141             bool inside(const TRect<T>&  _value) const
00142             {
00143                 return ((_value.left >= left) && (_value.right <= right) && (_value.top >= top) && (_value.bottom <= bottom));
00144             }
00145 
00146             bool intersect(const TRect<T>&  _value) const
00147             {
00148                 return ((_value.left <= right) && (_value.right >= left) && (_value.top <= bottom) && (_value.bottom >= top));
00149             }
00150 
00151             bool inside(const TPoint<T>&  _value) const
00152             {
00153                 return ((_value.left >= left) && (_value.left <= right) && (_value.top >= top) && (_value.top <= bottom));
00154             }
00155 
00156             std::string print() const
00157             {
00158                 std::ostringstream stream;
00159                 stream << *this;
00160                 return stream.str();
00161             }
00162 
00163             static TRect<T> parse(const std::string& _value)
00164             {
00165                 TRect<T> result;
00166                 std::istringstream stream(_value);
00167                 stream >> result.left >> result.top >> result.right >> result.bottom;
00168                 if (stream.fail())
00169                 {
00170                     return TRect<T>();
00171                 }
00172                 else
00173                 {
00174                     int item = stream.get();
00175                     while (item != -1)
00176                     {
00177                         if (item != ' ' && item != '\t')
00178                             return TRect<T>();
00179                         item = stream.get();
00180                     }
00181                 }
00182                 return result;
00183             }
00184 
00185             friend std::ostream& operator << (std::ostream& _stream, const TRect<T>&  _value)
00186             {
00187                 _stream << _value.left << " " << _value.top << " " << _value.right << " " << _value.bottom;
00188                 return _stream;
00189             }
00190 
00191             friend std::istream& operator >> (std::istream& _stream, TRect<T>&  _value)
00192             {
00193                 _stream >> _value.left >> _value.top >> _value.right >> _value.bottom;
00194                 if (_stream.fail())
00195                     _value.clear();
00196                 return _stream;
00197             }
00198         };
00199 
00200     } // namespace types
00201 
00202 } // namespace MyGUI
00203 
00204 #endif // __MYGUI_TRECT_H__