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