MyGUI  3.0.1
MyGUI_TRect.h
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #ifndef __MYGUI_TRECT_H__
24 #define __MYGUI_TRECT_H__
25 
26 #include "MyGUI_Prerequest.h"
27 
28 namespace MyGUI
29 {
30  namespace types
31  {
32 
33  template< typename T > struct TRect
34  {
36 
37  TRect() : left( 0 ), top( 0 ), right( 0 ), bottom( 0 ) { }
38  TRect( T const& _left, T const& _top, T const& _right, T const& _bottom ) : left( _left ), top( _top ), right( _right ), bottom( _bottom ) { }
39  TRect( TRect const& o ) : left( o.left ), top( o.top ), right( o.right ), bottom( o.bottom ) { }
40 
41  TRect& operator-=( TRect const& o )
42  {
43  left -= o.left;
44  top -= o.top;
45  right -= o.right;
46  bottom -= o.bottom;
47  return *this;
48  }
49 
50  TRect& operator+=( TRect const& o )
51  {
52  left += o.left;
53  top += o.top;
54  right += o.right;
55  bottom += o.bottom;
56  return *this;
57  }
58 
59  TRect operator-( TRect const& o ) const
60  {
61  return TRect(left - o.left, top - o.top, right - o.right, bottom - o.bottom);
62  }
63 
64  TRect operator+( TRect const& o ) const
65  {
66  return TRect(left + o.left, top + o.top, right + o.right, bottom + o.bottom);
67  }
68 
69  TRect& operator=( TRect const& o )
70  {
71  left = o.left;
72  top = o.top;
73  right = o.right;
74  bottom = o.bottom;
75  return *this;
76  }
77 
78  template< typename U >
79  TRect& operator=( TRect<U> const& o )
80  {
81  left = o.left;
82  top = o.top;
83  right = o.right;
84  bottom = o.bottom;
85  return *this;
86  }
87 
88  bool operator==( TRect const& o ) const
89  {
90  return ((left == o.left) && (top == o.top) && (right == o.right) && (bottom == o.bottom));
91  }
92 
93  bool operator!=( TRect const& o ) const
94  {
95  return ! ((left == o.left) && (top == o.top) && (right == o.right) && (bottom == o.bottom));
96  }
97 
98  T width() const
99  {
100  return right - left;
101  }
102 
103  T height() const
104  {
105  return bottom - top;
106  }
107 
108  void clear()
109  {
110  left = top = right = bottom = 0;
111  }
112 
113  void set( T const& _left, T const& _top, T const& _right, T const& _bottom )
114  {
115  left = _left;
116  top = _top;
117  right = _right;
118  bottom = _bottom;
119  }
120 
121  void swap(TRect& _value)
122  {
123  TRect tmp = _value;
124  _value = *this;
125  *this = tmp;
126  }
127 
128  bool empty() const
129  {
130  return ((left == 0) && (top == 0) && (right == 0) && (bottom == 0));
131  }
132 
133  bool inside(const TRect<T>& _value) const
134  {
135  return ( (_value.left >= left) && (_value.right <= right) && (_value.top >= top) && (_value.bottom <= bottom) );
136  }
137 
138  bool intersect(const TRect<T>& _value) const
139  {
140  return ( (_value.left <= right) && (_value.right >= left) && (_value.top <= bottom) && (_value.bottom >= top) );
141  }
142 
143  bool inside(const TPoint<T>& _value) const
144  {
145  return ( (_value.left >= left) && (_value.left <= right) && (_value.top >= top) && (_value.top <= bottom) );
146  }
147 
148  std::string print() const
149  {
150  std::ostringstream stream;
151  stream << *this;
152  return stream.str();
153  }
154 
155  static TRect<T> parse(const std::string& _value)
156  {
157  TRect<T> result;
158  std::istringstream stream(_value);
159  stream >> result.left >> result.top >> result.right >> result.bottom;
160  if (stream.fail()) return TRect<T>();
161  else
162  {
163  int item = stream.get();
164  while (item != -1)
165  {
166  if (item != ' ' && item != '\t') return TRect<T>();
167  item = stream.get();
168  }
169  }
170  return result;
171  }
172 
173  friend std::ostream& operator << ( std::ostream& _stream, const TRect<T>& _value )
174  {
175  _stream << _value.left << " " << _value.top << " " << _value.right << " " << _value.bottom;
176  return _stream;
177  }
178 
179  friend std::istream& operator >> ( std::istream& _stream, TRect<T>& _value )
180  {
181  _stream >> _value.left >> _value.top >> _value.right >> _value.bottom;
182  if (_stream.fail()) _value.clear();
183  return _stream;
184  }
185 
186  };
187 
188  } // namespace types
189 } // namespace MyGUI
190 
191 #endif // __MYGUI_TRECT_H__