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