MyGUI 3.2.2

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
14 namespace MyGUI
15 {
16 namespace types
17 {
18
19 template<typename T>
20 struct TCoord
21 {
22 T left;
23 T top;
24 T width;
25 T height;
26
27 TCoord() :
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
59 TCoord& operator -= (TCoord const& _obj)
60 {
61 left -= _obj.left;
62 top -= _obj.top;
63 width -= _obj.width;
64 height -= _obj.height;
65 return *this;
66 }
67
68 TCoord& operator += (TCoord const& _obj)
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
107 TCoord& operator = (TCoord const& _obj)
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 >
117 TCoord& operator = (TCoord<U> const& _obj)
118 {
119 left = _obj.left;
120 top = _obj.top;
121 width = _obj.width;
122 height = _obj.height;
123 return *this;
124 }
125
126 TCoord& operator = (TPoint<T> const& _obj)
127 {
128 left = _obj.left;
129 top = _obj.top;
130 return *this;
131 }
132
133 TCoord& operator = (TSize<T> const& _obj)
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
185 TPoint<T> point() const
186 {
187 return TPoint<T>(left, top);
188 }
189
190 TSize<T> size() const
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_
MyGUI::types::TCoord
Definition: MyGUI_TCoord.h:20
MyGUI_TSize.h
MyGUI::types::TCoord::operator==
bool operator==(TCoord const &_obj) const
Definition: MyGUI_TCoord.h:140
MyGUI::types::TPoint
Definition: MyGUI_TPoint.h:18
MyGUI::types::TCoord::operator+
TCoord operator+(TCoord const &_obj) const
Definition: MyGUI_TCoord.h:92
MyGUI_Prerequest.h
MyGUI::types::TCoord::swap
void swap(TCoord &_value)
Definition: MyGUI_TCoord.h:173
MyGUI::types::TCoord::bottom
T bottom() const
Definition: MyGUI_TCoord.h:155
MyGUI::types::TSize
Definition: MyGUI_TSize.h:18
MyGUI::types::TCoord::right
T right() const
Definition: MyGUI_TCoord.h:150
MyGUI::types::TCoord::point
TPoint< T > point() const
Definition: MyGUI_TCoord.h:185
MyGUI::types::TCoord::TCoord
TCoord(TPoint< T > const &_point, TSize< T > const &_size)
Definition: MyGUI_TCoord.h:51
MyGUI::types::TCoord::operator!=
bool operator!=(TCoord const &_obj) const
Definition: MyGUI_TCoord.h:145
MyGUI::types::TPoint::top
T top
Definition: MyGUI_TPoint.h:21
MyGUI::types::TCoord::width
T width
Definition: MyGUI_TCoord.h:24
MyGUI::types::TCoord::TCoord
TCoord(TCoord const &_obj)
Definition: MyGUI_TCoord.h:43
MyGUI::types::TCoord::clear
void clear()
Definition: MyGUI_TCoord.h:160
MyGUI::types::TCoord::operator=
TCoord & operator=(TCoord const &_obj)
Definition: MyGUI_TCoord.h:107
MyGUI::types::TCoord::top
T top
Definition: MyGUI_TCoord.h:23
MyGUI_TPoint.h
MyGUI
Definition: MyGUI_ActionController.h:14
MyGUI::types::TPoint::left
T left
Definition: MyGUI_TPoint.h:20
MyGUI::types::TCoord::operator-=
TCoord & operator-=(TCoord const &_obj)
Definition: MyGUI_TCoord.h:59
MyGUI::types::TCoord::inside
bool inside(const TPoint< T > &_value) const
Definition: MyGUI_TCoord.h:195
MyGUI::types::TCoord::TCoord
TCoord(T const &_left, T const &_top, T const &_width, T const &_height)
Definition: MyGUI_TCoord.h:35
MyGUI::types::TSize::width
T width
Definition: MyGUI_TSize.h:20
MyGUI::types::TCoord::print
std::string print() const
Definition: MyGUI_TCoord.h:200
MyGUI::types::TCoord::size
TSize< T > size() const
Definition: MyGUI_TCoord.h:190
MyGUI::types::TCoord::empty
bool empty() const
Definition: MyGUI_TCoord.h:180
MyGUI::types::TCoord::parse
static TCoord< T > parse(const std::string &_value)
Definition: MyGUI_TCoord.h:207
MyGUI::types::TCoord::operator-
TCoord operator-(TCoord const &_obj) const
Definition: MyGUI_TCoord.h:77
MyGUI::types::TSize::height
T height
Definition: MyGUI_TSize.h:21
MyGUI::types::TCoord::operator>>
friend std::istream & operator>>(std::istream &_stream, TCoord< T > &_value)
Definition: MyGUI_TCoord.h:235
MyGUI::types::TCoord::TCoord
TCoord()
Definition: MyGUI_TCoord.h:27
MyGUI::types::TCoord::left
T left
Definition: MyGUI_TCoord.h:22
MyGUI::types::TCoord::height
T height
Definition: MyGUI_TCoord.h:25
MyGUI::types::TCoord::operator+=
TCoord & operator+=(TCoord const &_obj)
Definition: MyGUI_TCoord.h:68