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