MyGUI  3.2.1
MyGUI_TRect.h
Go to the documentation of this file.
1 /*
2  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3  * Distributed under the MIT License
4  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5  */
6 
7 #ifndef __MYGUI_TRECT_H__
8 #define __MYGUI_TRECT_H__
9 
10 #include "MyGUI_Prerequest.h"
11 
12 namespace MyGUI
13 {
14  namespace types
15  {
16 
17  template<typename T>
18  struct TRect
19  {
20  T left;
21  T top;
22  T right;
23  T bottom;
24 
25  TRect() :
26  left(0),
27  top(0),
28  right(0),
29  bottom(0)
30  {
31  }
32 
33  TRect(T const& _left, T const& _top, T const& _right, T const& _bottom) :
34  left(_left),
35  top(_top),
36  right(_right),
37  bottom(_bottom)
38  {
39  }
40 
41  TRect(TRect const& _obj) :
42  left(_obj.left),
43  top(_obj.top),
44  right(_obj.right),
45  bottom(_obj.bottom)
46  {
47  }
48 
49  TRect& operator -= (TRect const& _obj)
50  {
51  left -= _obj.left;
52  top -= _obj.top;
53  right -= _obj.right;
54  bottom -= _obj.bottom;
55  return *this;
56  }
57 
58  TRect& operator += (TRect const& _obj)
59  {
60  left += _obj.left;
61  top += _obj.top;
62  right += _obj.right;
63  bottom += _obj.bottom;
64  return *this;
65  }
66 
67  TRect operator - (TRect const& _obj) const
68  {
69  return TRect(left - _obj.left, top - _obj.top, right - _obj.right, bottom - _obj.bottom);
70  }
71 
72  TRect operator + (TRect const& _obj) const
73  {
74  return TRect(left + _obj.left, top + _obj.top, right + _obj.right, bottom + _obj.bottom);
75  }
76 
77  TRect& operator = (TRect const& _obj)
78  {
79  left = _obj.left;
80  top = _obj.top;
81  right = _obj.right;
82  bottom = _obj.bottom;
83  return *this;
84  }
85 
86  template<typename U>
87  TRect& operator = (TRect<U> const& _obj)
88  {
89  left = _obj.left;
90  top = _obj.top;
91  right = _obj.right;
92  bottom = _obj.bottom;
93  return *this;
94  }
95 
96  bool operator == (TRect const& _obj) const
97  {
98  return ((left == _obj.left) && (top == _obj.top) && (right == _obj.right) && (bottom == _obj.bottom));
99  }
100 
101  bool operator != (TRect const& _obj) const
102  {
103  return !((left == _obj.left) && (top == _obj.top) && (right == _obj.right) && (bottom == _obj.bottom));
104  }
105 
106  T width() const
107  {
108  return right - left;
109  }
110 
111  T height() const
112  {
113  return bottom - top;
114  }
115 
116  void clear()
117  {
118  left = top = right = bottom = 0;
119  }
120 
121  void set(T const& _left, T const& _top, T const& _right, T const& _bottom)
122  {
123  left = _left;
124  top = _top;
125  right = _right;
126  bottom = _bottom;
127  }
128 
129  void swap(TRect& _value)
130  {
131  TRect tmp = _value;
132  _value = *this;
133  *this = tmp;
134  }
135 
136  bool empty() const
137  {
138  return ((left == 0) && (top == 0) && (right == 0) && (bottom == 0));
139  }
140 
141  bool inside(const TRect<T>& _value) const
142  {
143  return ((_value.left >= left) && (_value.right <= right) && (_value.top >= top) && (_value.bottom <= bottom));
144  }
145 
146  bool intersect(const TRect<T>& _value) const
147  {
148  return ((_value.left <= right) && (_value.right >= left) && (_value.top <= bottom) && (_value.bottom >= top));
149  }
150 
151  bool inside(const TPoint<T>& _value) const
152  {
153  return ((_value.left >= left) && (_value.left <= right) && (_value.top >= top) && (_value.top <= bottom));
154  }
155 
156  std::string print() const
157  {
158  std::ostringstream stream;
159  stream << *this;
160  return stream.str();
161  }
162 
163  static TRect<T> parse(const std::string& _value)
164  {
165  TRect<T> result;
166  std::istringstream stream(_value);
167  stream >> result.left >> result.top >> result.right >> result.bottom;
168  if (stream.fail())
169  {
170  return TRect<T>();
171  }
172  else
173  {
174  int item = stream.get();
175  while (item != -1)
176  {
177  if (item != ' ' && item != '\t')
178  return TRect<T>();
179  item = stream.get();
180  }
181  }
182  return result;
183  }
184 
185  friend std::ostream& operator << (std::ostream& _stream, const TRect<T>& _value)
186  {
187  _stream << _value.left << " " << _value.top << " " << _value.right << " " << _value.bottom;
188  return _stream;
189  }
190 
191  friend std::istream& operator >> (std::istream& _stream, TRect<T>& _value)
192  {
193  _stream >> _value.left >> _value.top >> _value.right >> _value.bottom;
194  if (_stream.fail())
195  _value.clear();
196  return _stream;
197  }
198  };
199 
200  } // namespace types
201 
202 } // namespace MyGUI
203 
204 #endif // __MYGUI_TRECT_H__