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