MyGUI 3.4.1
MyGUI_TCoord.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_TCOORD_H_
8#define MyGUI_TCOORD_H_
9
10#include "MyGUI_Prerequest.h"
11#include "MyGUI_TPoint.h"
12#include "MyGUI_TSize.h"
13
14namespace MyGUI
15{
16 namespace types
17 {
18
19 template<typename T>
20 struct TCoord
21 {
23 T top;
26
28 left(0),
29 top(0),
30 width(0),
31 height(0)
32 {
33 }
34
35 TCoord(T const& _left, T const& _top, T const& _width, T const& _height) :
36 left(_left),
37 top(_top),
38 width(_width),
39 height(_height)
40 {
41 }
42
43 TCoord(TCoord const& _obj) :
44 left(_obj.left),
45 top(_obj.top),
46 width(_obj.width),
47 height(_obj.height)
48 {
49 }
50
51 TCoord(TPoint<T> const& _point, TSize<T> const& _size) :
52 left(_point.left),
53 top(_point.top),
54 width(_size.width),
55 height(_size.height)
56 {
57 }
58
60 {
61 left -= _obj.left;
62 top -= _obj.top;
63 width -= _obj.width;
64 height -= _obj.height;
65 return *this;
66 }
67
69 {
70 left += _obj.left;
71 top += _obj.top;
72 width += _obj.width;
73 height += _obj.height;
74 return *this;
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) 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
108 {
109 left = _obj.left;
110 top = _obj.top;
111 width = _obj.width;
112 height = _obj.height;
113 return *this;
114 }
115
116 template< typename U >
118 {
119 left = _obj.left;
120 top = _obj.top;
121 width = _obj.width;
122 height = _obj.height;
123 return *this;
124 }
125
127 {
128 left = _obj.left;
129 top = _obj.top;
130 return *this;
131 }
132
134 {
135 width = _obj.width;
136 height = _obj.height;
137 return *this;
138 }
139
140 bool operator == (TCoord const& _obj) const
141 {
142 return ((left == _obj.left) && (top == _obj.top) && (width == _obj.width) && (height == _obj.height));
143 }
144
145 bool operator != (TCoord const& _obj) const
146 {
147 return !((left == _obj.left) && (top == _obj.top) && (width == _obj.width) && (height == _obj.height));
148 }
149
150 T right() const
151 {
152 return left + width;
153 }
154
155 T bottom() const
156 {
157 return top + height;
158 }
159
160 void clear()
161 {
162 left = top = width = height = 0;
163 }
164
165 void set(T const& _left, T const& _top, T const& _width, T const& _height)
166 {
167 left = _left;
168 top = _top;
169 width = _width;
170 height = _height;
171 }
172
173 void swap(TCoord& _value)
174 {
175 TCoord tmp = _value;
176 _value = *this;
177 *this = tmp;
178 }
179
180 bool empty() const
181 {
182 return ((left == 0) && (top == 0) && (width == 0) && (height == 0));
183 }
184
186 {
187 return TPoint<T>(left, top);
188 }
189
191 {
192 return TSize<T>(width, height);
193 }
194
195 bool inside(const TPoint<T>& _value) const
196 {
197 return ((_value.left >= left) && (_value.left <= right()) && (_value.top >= top) && (_value.top <= bottom()));
198 }
199
200 std::string print() const
201 {
202 std::ostringstream stream;
203 stream << *this;
204 return stream.str();
205 }
206
207 static TCoord<T> parse(const std::string& _value)
208 {
209 TCoord<T> result;
210 std::istringstream stream(_value);
211 stream >> result.left >> result.top >> result.width >> result.height;
212 if (stream.fail())
213 {
214 return TCoord<T>();
215 }
216 else
217 {
218 int item = stream.get();
219 while (item != -1)
220 {
221 if (item != ' ' && item != '\t')
222 return TCoord<T>();
223 item = stream.get();
224 }
225 }
226 return result;
227 }
228
229 friend std::ostream& operator << (std::ostream& _stream, const TCoord<T>& _value)
230 {
231 _stream << _value.left << " " << _value.top << " " << _value.width << " " << _value.height;
232 return _stream;
233 }
234
235 friend std::istream& operator >> (std::istream& _stream, TCoord<T>& _value)
236 {
237 _stream >> _value.left >> _value.top >> _value.width >> _value.height;
238 if (_stream.fail())
239 _value.clear();
240 return _stream;
241 }
242 };
243
244 } // namespace types
245
246} // namespace MyGUI
247
248#endif // MyGUI_TCOORD_H_
bool empty() const
Definition: MyGUI_TCoord.h:180
TCoord & operator+=(TCoord const &_obj)
Definition: MyGUI_TCoord.h:68
TCoord(TPoint< T > const &_point, TSize< T > const &_size)
Definition: MyGUI_TCoord.h:51
friend std::ostream & operator<<(std::ostream &_stream, const TCoord< T > &_value)
Definition: MyGUI_TCoord.h:229
TCoord(TCoord const &_obj)
Definition: MyGUI_TCoord.h:43
bool operator==(TCoord const &_obj) const
Definition: MyGUI_TCoord.h:140
TCoord & operator-=(TCoord const &_obj)
Definition: MyGUI_TCoord.h:59
TCoord(T const &_left, T const &_top, T const &_width, T const &_height)
Definition: MyGUI_TCoord.h:35
void set(T const &_left, T const &_top, T const &_width, T const &_height)
Definition: MyGUI_TCoord.h:165
TCoord & operator=(TCoord const &_obj)
Definition: MyGUI_TCoord.h:107
TPoint< T > point() const
Definition: MyGUI_TCoord.h:185
void swap(TCoord &_value)
Definition: MyGUI_TCoord.h:173
bool operator!=(TCoord const &_obj) const
Definition: MyGUI_TCoord.h:145
TCoord operator-(TCoord const &_obj) const
Definition: MyGUI_TCoord.h:77
friend std::istream & operator>>(std::istream &_stream, TCoord< T > &_value)
Definition: MyGUI_TCoord.h:235
TSize< T > size() const
Definition: MyGUI_TCoord.h:190
static TCoord< T > parse(const std::string &_value)
Definition: MyGUI_TCoord.h:207
std::string print() const
Definition: MyGUI_TCoord.h:200
bool inside(const TPoint< T > &_value) const
Definition: MyGUI_TCoord.h:195
TCoord operator+(TCoord const &_obj) const
Definition: MyGUI_TCoord.h:92