MyGUI  3.2.1
MyGUI_TCoord.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_TCOORD_H__
00008 #define __MyGUI_TCOORD_H__
00009 
00010 #include "MyGUI_Prerequest.h"
00011 #include "MyGUI_TPoint.h"
00012 #include "MyGUI_TSize.h"
00013 
00014 namespace MyGUI
00015 {
00016     namespace types
00017     {
00018 
00019         template<typename T>
00020         struct TCoord
00021         {
00022             T left;
00023             T top;
00024             T width;
00025             T height;
00026 
00027             TCoord() :
00028                 left(0),
00029                 top(0),
00030                 width(0),
00031                 height(0)
00032             {
00033             }
00034 
00035             TCoord(T const& _left, T const& _top, T const& _width, T const& _height) :
00036                 left(_left),
00037                 top(_top),
00038                 width(_width),
00039                 height(_height)
00040             {
00041             }
00042 
00043             TCoord(TCoord const& _obj) :
00044                 left(_obj.left),
00045                 top(_obj.top),
00046                 width(_obj.width),
00047                 height(_obj.height)
00048             {
00049             }
00050 
00051             TCoord(TPoint<T> const& _point, TSize<T> const& _size) :
00052                 left(_point.left),
00053                 top(_point.top),
00054                 width(_size.width),
00055                 height(_size.height)
00056             {
00057             }
00058 
00059             TCoord& operator -= (TCoord const& _obj)
00060             {
00061                 left -= _obj.left;
00062                 top -= _obj.top;
00063                 width -= _obj.width;
00064                 height -= _obj.height;
00065                 return *this;
00066             }
00067 
00068             TCoord& operator += (TCoord const& _obj)
00069             {
00070                 left += _obj.left;
00071                 top += _obj.top;
00072                 width += _obj.width;
00073                 height += _obj.height;
00074                 return *this;
00075             }
00076 
00077             TCoord operator - (TCoord const& _obj) const
00078             {
00079                 return TCoord(left - _obj.left, top - _obj.top, width - _obj.width, height - _obj.height);
00080             }
00081 
00082             TCoord operator - (TPoint<T> const& _obj) const
00083             {
00084                 return TCoord(left - _obj.left, top - _obj.top, width, height);
00085             }
00086 
00087             TCoord operator - (TSize<T> const& _obj) const
00088             {
00089                 return TCoord(left, top, width - _obj.width, height - _obj.height);
00090             }
00091 
00092             TCoord operator + (TCoord const& _obj) const
00093             {
00094                 return TCoord(left + _obj.left, top + _obj.top, width + _obj.width, height + _obj.height);
00095             }
00096 
00097             TCoord operator + (TPoint<T> const& _obj) const
00098             {
00099                 return TCoord(left + _obj.left, top + _obj.top, width, height);
00100             }
00101 
00102             TCoord operator + (TSize<T> const& _obj) const
00103             {
00104                 return TCoord(left, top, width + _obj.width, height + _obj.height);
00105             }
00106 
00107             TCoord& operator = (TCoord const& _obj)
00108             {
00109                 left = _obj.left;
00110                 top = _obj.top;
00111                 width = _obj.width;
00112                 height = _obj.height;
00113                 return *this;
00114             }
00115 
00116             template< typename U >
00117             TCoord& operator = (TCoord<U> const& _obj)
00118             {
00119                 left = _obj.left;
00120                 top = _obj.top;
00121                 width = _obj.width;
00122                 height = _obj.height;
00123                 return *this;
00124             }
00125 
00126             TCoord& operator = (TPoint<T> const& _obj)
00127             {
00128                 left = _obj.left;
00129                 top = _obj.top;
00130                 return *this;
00131             }
00132 
00133             TCoord& operator = (TSize<T> const& _obj)
00134             {
00135                 width = _obj.width;
00136                 height = _obj.height;
00137                 return *this;
00138             }
00139 
00140             bool operator == (TCoord const& _obj) const
00141             {
00142                 return ((left == _obj.left) && (top == _obj.top) && (width == _obj.width) && (height == _obj.height));
00143             }
00144 
00145             bool operator != (TCoord const& _obj) const
00146             {
00147                 return !((left == _obj.left) && (top == _obj.top) && (width == _obj.width) && (height == _obj.height));
00148             }
00149 
00150             T right() const
00151             {
00152                 return left + width;
00153             }
00154 
00155             T bottom() const
00156             {
00157                 return top + height;
00158             }
00159 
00160             void clear()
00161             {
00162                 left = top = width = height = 0;
00163             }
00164 
00165             void set(T const& _left, T const& _top, T const& _width, T const& _height)
00166             {
00167                 left = _left;
00168                 top = _top;
00169                 width = _width;
00170                 height = _height;
00171             }
00172 
00173             void swap(TCoord& _value)
00174             {
00175                 TCoord tmp = _value;
00176                 _value = *this;
00177                 *this = tmp;
00178             }
00179 
00180             bool empty() const
00181             {
00182                 return ((left == 0) && (top == 0) && (width == 0) && (height == 0));
00183             }
00184 
00185             TPoint<T> point() const
00186             {
00187                 return TPoint<T>(left, top);
00188             }
00189 
00190             TSize<T> size() const
00191             {
00192                 return TSize<T>(width, height);
00193             }
00194 
00195             bool inside(const TPoint<T>&  _value) const
00196             {
00197                 return ((_value.left >= left) && (_value.left <= right()) && (_value.top >= top) && (_value.top <= bottom()));
00198             }
00199 
00200             std::string print() const
00201             {
00202                 std::ostringstream stream;
00203                 stream << *this;
00204                 return stream.str();
00205             }
00206 
00207             static TCoord<T> parse(const std::string& _value)
00208             {
00209                 TCoord<T> result;
00210                 std::istringstream stream(_value);
00211                 stream >> result.left >> result.top >> result.width >> result.height;
00212                 if (stream.fail())
00213                 {
00214                     return TCoord<T>();
00215                 }
00216                 else
00217                 {
00218                     int item = stream.get();
00219                     while (item != -1)
00220                     {
00221                         if (item != ' ' && item != '\t')
00222                             return TCoord<T>();
00223                         item = stream.get();
00224                     }
00225                 }
00226                 return result;
00227             }
00228 
00229             friend std::ostream& operator << (std::ostream& _stream, const TCoord<T>&  _value)
00230             {
00231                 _stream << _value.left << " " << _value.top << " " << _value.width << " " << _value.height;
00232                 return _stream;
00233             }
00234 
00235             friend std::istream& operator >> (std::istream& _stream, TCoord<T>&  _value)
00236             {
00237                 _stream >> _value.left >> _value.top >> _value.width >> _value.height;
00238                 if (_stream.fail())
00239                     _value.clear();
00240                 return _stream;
00241             }
00242         };
00243 
00244     } // namespace types
00245 
00246 } // namespace MyGUI
00247 
00248 #endif // __MyGUI_TCOORD_H__