MyGUI  3.2.0
MyGUI_TRect.h
Go to the documentation of this file.
00001 
00006 /*
00007     This file is part of MyGUI.
00008 
00009     MyGUI is free software: you can redistribute it and/or modify
00010     it under the terms of the GNU Lesser General Public License as published by
00011     the Free Software Foundation, either version 3 of the License, or
00012     (at your option) any later version.
00013 
00014     MyGUI 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
00017     GNU Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 #ifndef __MYGUI_TRECT_H__
00023 #define __MYGUI_TRECT_H__
00024 
00025 #include "MyGUI_Prerequest.h"
00026 
00027 namespace MyGUI
00028 {
00029     namespace types
00030     {
00031 
00032         template<typename T>
00033         struct TRect
00034         {
00035             T left;
00036             T top;
00037             T right;
00038             T bottom;
00039 
00040             TRect() :
00041                 left(0),
00042                 top(0),
00043                 right(0),
00044                 bottom(0)
00045             {
00046             }
00047 
00048             TRect(T const& _left, T const& _top, T const& _right, T const& _bottom) :
00049                 left(_left),
00050                 top(_top),
00051                 right(_right),
00052                 bottom(_bottom)
00053             {
00054             }
00055 
00056             TRect(TRect const& _obj) :
00057                 left(_obj.left),
00058                 top(_obj.top),
00059                 right(_obj.right),
00060                 bottom(_obj.bottom)
00061             {
00062             }
00063 
00064             TRect& operator -= (TRect const& _obj)
00065             {
00066                 left -= _obj.left;
00067                 top -= _obj.top;
00068                 right -= _obj.right;
00069                 bottom -= _obj.bottom;
00070                 return *this;
00071             }
00072 
00073             TRect& operator += (TRect const& _obj)
00074             {
00075                 left += _obj.left;
00076                 top += _obj.top;
00077                 right += _obj.right;
00078                 bottom += _obj.bottom;
00079                 return *this;
00080             }
00081 
00082             TRect operator - (TRect const& _obj) const
00083             {
00084                 return TRect(left - _obj.left, top - _obj.top, right - _obj.right, bottom - _obj.bottom);
00085             }
00086 
00087             TRect operator + (TRect const& _obj) const
00088             {
00089                 return TRect(left + _obj.left, top + _obj.top, right + _obj.right, bottom + _obj.bottom);
00090             }
00091 
00092             TRect& operator = (TRect const& _obj)
00093             {
00094                 left = _obj.left;
00095                 top = _obj.top;
00096                 right = _obj.right;
00097                 bottom = _obj.bottom;
00098                 return *this;
00099             }
00100 
00101             template<typename U>
00102             TRect& operator = (TRect<U> const& _obj)
00103             {
00104                 left = _obj.left;
00105                 top = _obj.top;
00106                 right = _obj.right;
00107                 bottom = _obj.bottom;
00108                 return *this;
00109             }
00110 
00111             bool operator == (TRect const& _obj) const
00112             {
00113                 return ((left == _obj.left) && (top == _obj.top) && (right == _obj.right) && (bottom == _obj.bottom));
00114             }
00115 
00116             bool operator != (TRect const& _obj) const
00117             {
00118                 return !((left == _obj.left) && (top == _obj.top) && (right == _obj.right) && (bottom == _obj.bottom));
00119             }
00120 
00121             T width() const
00122             {
00123                 return right - left;
00124             }
00125 
00126             T height() const
00127             {
00128                 return bottom - top;
00129             }
00130 
00131             void clear()
00132             {
00133                 left = top = right = bottom = 0;
00134             }
00135 
00136             void set(T const& _left, T const& _top, T const& _right, T const& _bottom)
00137             {
00138                 left = _left;
00139                 top = _top;
00140                 right = _right;
00141                 bottom = _bottom;
00142             }
00143 
00144             void swap(TRect& _value)
00145             {
00146                 TRect tmp = _value;
00147                 _value = *this;
00148                 *this = tmp;
00149             }
00150 
00151             bool empty() const
00152             {
00153                 return ((left == 0) && (top == 0) && (right == 0) && (bottom == 0));
00154             }
00155 
00156             bool inside(const TRect<T>&  _value) const
00157             {
00158                 return ((_value.left >= left) && (_value.right <= right) && (_value.top >= top) && (_value.bottom <= bottom));
00159             }
00160 
00161             bool intersect(const TRect<T>&  _value) const
00162             {
00163                 return ((_value.left <= right) && (_value.right >= left) && (_value.top <= bottom) && (_value.bottom >= top));
00164             }
00165 
00166             bool inside(const TPoint<T>&  _value) const
00167             {
00168                 return ((_value.left >= left) && (_value.left <= right) && (_value.top >= top) && (_value.top <= bottom));
00169             }
00170 
00171             std::string print() const
00172             {
00173                 std::ostringstream stream;
00174                 stream << *this;
00175                 return stream.str();
00176             }
00177 
00178             static TRect<T> parse(const std::string& _value)
00179             {
00180                 TRect<T> result;
00181                 std::istringstream stream(_value);
00182                 stream >> result.left >> result.top >> result.right >> result.bottom;
00183                 if (stream.fail())
00184                 {
00185                     return TRect<T>();
00186                 }
00187                 else
00188                 {
00189                     int item = stream.get();
00190                     while (item != -1)
00191                     {
00192                         if (item != ' ' && item != '\t')
00193                             return TRect<T>();
00194                         item = stream.get();
00195                     }
00196                 }
00197                 return result;
00198             }
00199 
00200             friend std::ostream& operator << (std::ostream& _stream, const TRect<T>&  _value)
00201             {
00202                 _stream << _value.left << " " << _value.top << " " << _value.right << " " << _value.bottom;
00203                 return _stream;
00204             }
00205 
00206             friend std::istream& operator >> (std::istream& _stream, TRect<T>&  _value)
00207             {
00208                 _stream >> _value.left >> _value.top >> _value.right >> _value.bottom;
00209                 if (_stream.fail())
00210                     _value.clear();
00211                 return _stream;
00212             }
00213         };
00214 
00215     } // namespace types
00216 
00217 } // namespace MyGUI
00218 
00219 #endif // __MYGUI_TRECT_H__