MyGUI  3.2.0
MyGUI_TCoord.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_TCOORD_H__
23 #define __MyGUI_TCOORD_H__
24 
25 #include "MyGUI_Prerequest.h"
26 #include "MyGUI_TPoint.h"
27 #include "MyGUI_TSize.h"
28 
29 namespace MyGUI
30 {
31  namespace types
32  {
33 
34  template<typename T>
35  struct TCoord
36  {
37  T left;
38  T top;
39  T width;
40  T height;
41 
42  TCoord() :
43  left(0),
44  top(0),
45  width(0),
46  height(0)
47  {
48  }
49 
50  TCoord(T const& _left, T const& _top, T const& _width, T const& _height) :
51  left(_left),
52  top(_top),
53  width(_width),
54  height(_height)
55  {
56  }
57 
58  TCoord(TCoord const& _obj) :
59  left(_obj.left),
60  top(_obj.top),
61  width(_obj.width),
62  height(_obj.height)
63  {
64  }
65 
66  TCoord(TPoint<T> const& _point, TSize<T> const& _size) :
67  left(_point.left),
68  top(_point.top),
69  width(_size.width),
70  height(_size.height)
71  {
72  }
73 
74  TCoord& operator -= (TCoord const& _obj)
75  {
76  left -= _obj.left;
77  top -= _obj.top;
78  width -= _obj.width;
79  height -= _obj.height;
80  return *this;
81  }
82 
83  TCoord& operator += (TCoord const& _obj)
84  {
85  left += _obj.left;
86  top += _obj.top;
87  width += _obj.width;
88  height += _obj.height;
89  return *this;
90  }
91 
92  TCoord operator - (TCoord const& _obj) const
93  {
94  return TCoord(left - _obj.left, top - _obj.top, width - _obj.width, height - _obj.height);
95  }
96 
97  TCoord operator - (TPoint<T> const& _obj) const
98  {
99  return TCoord(left - _obj.left, top - _obj.top, width, height);
100  }
101 
102  TCoord operator - (TSize<T> const& _obj) const
103  {
104  return TCoord(left, top, width - _obj.width, height - _obj.height);
105  }
106 
107  TCoord operator + (TCoord const& _obj) const
108  {
109  return TCoord(left + _obj.left, top + _obj.top, width + _obj.width, height + _obj.height);
110  }
111 
112  TCoord operator + (TPoint<T> const& _obj) const
113  {
114  return TCoord(left + _obj.left, top + _obj.top, width, height);
115  }
116 
117  TCoord operator + (TSize<T> const& _obj) const
118  {
119  return TCoord(left, top, width + _obj.width, height + _obj.height);
120  }
121 
122  TCoord& operator = (TCoord const& _obj)
123  {
124  left = _obj.left;
125  top = _obj.top;
126  width = _obj.width;
127  height = _obj.height;
128  return *this;
129  }
130 
131  template< typename U >
133  {
134  left = _obj.left;
135  top = _obj.top;
136  width = _obj.width;
137  height = _obj.height;
138  return *this;
139  }
140 
142  {
143  left = _obj.left;
144  top = _obj.top;
145  return *this;
146  }
147 
149  {
150  width = _obj.width;
151  height = _obj.height;
152  return *this;
153  }
154 
155  bool operator == (TCoord const& _obj) const
156  {
157  return ((left == _obj.left) && (top == _obj.top) && (width == _obj.width) && (height == _obj.height));
158  }
159 
160  bool operator != (TCoord const& _obj) const
161  {
162  return !((left == _obj.left) && (top == _obj.top) && (width == _obj.width) && (height == _obj.height));
163  }
164 
165  T right() const
166  {
167  return left + width;
168  }
169 
170  T bottom() const
171  {
172  return top + height;
173  }
174 
175  void clear()
176  {
177  left = top = width = height = 0;
178  }
179 
180  void set(T const& _left, T const& _top, T const& _width, T const& _height)
181  {
182  left = _left;
183  top = _top;
184  width = _width;
185  height = _height;
186  }
187 
188  void swap(TCoord& _value)
189  {
190  TCoord tmp = _value;
191  _value = *this;
192  *this = tmp;
193  }
194 
195  bool empty() const
196  {
197  return ((left == 0) && (top == 0) && (width == 0) && (height == 0));
198  }
199 
200  TPoint<T> point() const
201  {
202  return TPoint<T>(left, top);
203  }
204 
205  TSize<T> size() const
206  {
207  return TSize<T>(width, height);
208  }
209 
210  bool inside(const TPoint<T>& _value) const
211  {
212  return ((_value.left >= left) && (_value.left <= right()) && (_value.top >= top) && (_value.top <= bottom()));
213  }
214 
215  std::string print() const
216  {
217  std::ostringstream stream;
218  stream << *this;
219  return stream.str();
220  }
221 
222  static TCoord<T> parse(const std::string& _value)
223  {
224  TCoord<T> result;
225  std::istringstream stream(_value);
226  stream >> result.left >> result.top >> result.width >> result.height;
227  if (stream.fail())
228  {
229  return TCoord<T>();
230  }
231  else
232  {
233  int item = stream.get();
234  while (item != -1)
235  {
236  if (item != ' ' && item != '\t')
237  return TCoord<T>();
238  item = stream.get();
239  }
240  }
241  return result;
242  }
243 
244  friend std::ostream& operator << (std::ostream& _stream, const TCoord<T>& _value)
245  {
246  _stream << _value.left << " " << _value.top << " " << _value.width << " " << _value.height;
247  return _stream;
248  }
249 
250  friend std::istream& operator >> (std::istream& _stream, TCoord<T>& _value)
251  {
252  _stream >> _value.left >> _value.top >> _value.width >> _value.height;
253  if (_stream.fail())
254  _value.clear();
255  return _stream;
256  }
257  };
258 
259  } // namespace types
260 
261 } // namespace MyGUI
262 
263 #endif // __MyGUI_TCOORD_H__