00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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 }
00227 }
00228
00229 #endif // __MyGUI_TCOORD_H__