MyGUI 3.0.1

MyGUI_TRect.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_TRECT_H__
00024 #define __MYGUI_TRECT_H__
00025 
00026 #include "MyGUI_Prerequest.h"
00027 
00028 namespace MyGUI
00029 {
00030     namespace types
00031     {
00032 
00033         template< typename T > struct TRect
00034         {
00035             T left, top, right, bottom;
00036 
00037             TRect() : left( 0 ), top( 0 ), right( 0 ), bottom( 0 ) { }
00038             TRect( T const& _left, T const& _top, T const& _right, T const& _bottom ) : left( _left ), top( _top ), right( _right ), bottom( _bottom ) { }
00039             TRect( TRect const& o ) : left( o.left ), top( o.top ), right( o.right ), bottom( o.bottom ) { }
00040 
00041             TRect& operator-=( TRect const& o )
00042             {
00043                 left -= o.left;
00044                 top -= o.top;
00045                 right -= o.right;
00046                 bottom -= o.bottom;
00047                 return *this;
00048             }
00049 
00050             TRect& operator+=( TRect const& o )
00051             {
00052                 left += o.left;
00053                 top += o.top;
00054                 right += o.right;
00055                 bottom += o.bottom;
00056                 return *this;
00057             }
00058 
00059             TRect operator-( TRect const& o ) const
00060             {
00061                 return TRect(left - o.left, top - o.top, right - o.right, bottom - o.bottom);
00062             }
00063 
00064             TRect operator+( TRect const& o ) const
00065             {
00066                 return TRect(left + o.left, top + o.top, right + o.right, bottom + o.bottom);
00067             }
00068 
00069             TRect& operator=( TRect const& o )
00070             {
00071                 left = o.left;
00072                 top = o.top;
00073                 right = o.right;
00074                 bottom = o.bottom;
00075                 return *this;
00076             }
00077 
00078             template< typename U >
00079             TRect& operator=( TRect<U> const& o )
00080             {
00081                 left = o.left;
00082                 top = o.top;
00083                 right = o.right;
00084                 bottom = o.bottom;
00085                 return *this;
00086             }
00087 
00088             bool operator==( TRect const& o ) const
00089             {
00090                 return ((left == o.left) && (top == o.top) && (right == o.right) && (bottom == o.bottom));
00091             }
00092 
00093             bool operator!=( TRect const& o ) const
00094             {
00095                 return ! ((left == o.left) && (top == o.top) && (right == o.right) && (bottom == o.bottom));
00096             }
00097 
00098             T width() const
00099             {
00100                 return right - left;
00101             }
00102 
00103             T height() const
00104             {
00105                 return bottom - top;
00106             }
00107 
00108             void clear()
00109             {
00110                 left = top = right = bottom = 0;
00111             }
00112 
00113             void set( T const& _left, T const& _top, T const& _right, T const& _bottom )
00114             {
00115                 left = _left;
00116                 top = _top;
00117                 right = _right;
00118                 bottom = _bottom;
00119             }
00120 
00121             void swap(TRect& _value)
00122             {
00123                 TRect tmp = _value;
00124                 _value = *this;
00125                 *this = tmp;
00126             }
00127 
00128             bool empty() const
00129             {
00130                 return ((left == 0) && (top == 0) && (right == 0) && (bottom == 0));
00131             }
00132 
00133             bool inside(const TRect<T>&  _value) const
00134             {
00135                 return ( (_value.left >= left) && (_value.right <= right) && (_value.top >= top) && (_value.bottom <= bottom) );
00136             }
00137 
00138             bool intersect(const TRect<T>&  _value) const
00139             {
00140                 return ( (_value.left <= right) && (_value.right >= left) && (_value.top <= bottom) && (_value.bottom >= top) );
00141             }
00142 
00143             bool inside(const TPoint<T>&  _value) const
00144             {
00145                 return ( (_value.left >= left) && (_value.left <= right) && (_value.top >= top) && (_value.top <= bottom) );
00146             }
00147 
00148             std::string print() const
00149             {
00150                 std::ostringstream stream;
00151                 stream << *this;
00152                 return stream.str();
00153             }
00154 
00155             static TRect<T> parse(const std::string& _value)
00156             {
00157                 TRect<T> result;
00158                 std::istringstream stream(_value);
00159                 stream >> result.left >> result.top >> result.right >> result.bottom;
00160                 if (stream.fail()) return TRect<T>();
00161                 else
00162                 {
00163                     int item = stream.get();
00164                     while (item != -1)
00165                     {
00166                         if (item != ' ' && item != '\t') return TRect<T>();
00167                         item = stream.get();
00168                     }
00169                 }
00170                 return result;
00171             }
00172 
00173             friend std::ostream& operator << ( std::ostream& _stream, const TRect<T>&  _value )
00174             {
00175                 _stream << _value.left << " " << _value.top << " " << _value.right << " " << _value.bottom;
00176                 return _stream;
00177             }
00178 
00179             friend std::istream& operator >> ( std::istream& _stream, TRect<T>&  _value )
00180             {
00181                 _stream >> _value.left >> _value.top >> _value.right >> _value.bottom;
00182                 if (_stream.fail()) _value.clear();
00183                 return _stream;
00184             }
00185 
00186         };
00187 
00188     } // namespace types
00189 } // namespace MyGUI
00190 
00191 #endif // __MYGUI_TRECT_H__
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines