MyGUI 3.4.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
12namespace MyGUI
13{
14 namespace types
15 {
16
17 template<typename T>
18 struct TRect
19 {
21 T top;
24
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>
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_
TRect(TRect const &_obj)
Definition: MyGUI_TRect.h:41
bool inside(const TPoint< T > &_value) const
Definition: MyGUI_TRect.h:151
std::string print() const
Definition: MyGUI_TRect.h:156
void set(T const &_left, T const &_top, T const &_right, T const &_bottom)
Definition: MyGUI_TRect.h:121
bool operator!=(TRect const &_obj) const
Definition: MyGUI_TRect.h:101
TRect & operator=(TRect const &_obj)
Definition: MyGUI_TRect.h:77
TRect & operator-=(TRect const &_obj)
Definition: MyGUI_TRect.h:49
void swap(TRect &_value)
Definition: MyGUI_TRect.h:129
TRect(T const &_left, T const &_top, T const &_right, T const &_bottom)
Definition: MyGUI_TRect.h:33
bool empty() const
Definition: MyGUI_TRect.h:136
bool inside(const TRect< T > &_value) const
Definition: MyGUI_TRect.h:141
TRect operator+(TRect const &_obj) const
Definition: MyGUI_TRect.h:72
bool operator==(TRect const &_obj) const
Definition: MyGUI_TRect.h:96
friend std::istream & operator>>(std::istream &_stream, TRect< T > &_value)
Definition: MyGUI_TRect.h:191
static TRect< T > parse(const std::string &_value)
Definition: MyGUI_TRect.h:163
bool intersect(const TRect< T > &_value) const
Definition: MyGUI_TRect.h:146
TRect operator-(TRect const &_obj) const
Definition: MyGUI_TRect.h:67
friend std::ostream & operator<<(std::ostream &_stream, const TRect< T > &_value)
Definition: MyGUI_TRect.h:185
TRect & operator+=(TRect const &_obj)
Definition: MyGUI_TRect.h:58