MyGUI 3.4.3
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::types
15{
16
17 template<typename T>
18 struct TCoord
19 {
20 T left{};
21 T top{};
22 T width{};
23 T height{};
24
25 TCoord() = default;
26
27 TCoord(T const& _left, T const& _top, T const& _width, T const& _height) :
28 left(_left),
29 top(_top),
30 width(_width),
31 height(_height)
32 {
33 }
34
35 TCoord(TPoint<T> const& _point, TSize<T> const& _size) :
36 left(_point.left),
37 top(_point.top),
38 width(_size.width),
39 height(_size.height)
40 {
41 }
42
43 TCoord& operator-=(TCoord const& _obj)
44 {
45 left -= _obj.left;
46 top -= _obj.top;
47 width -= _obj.width;
48 height -= _obj.height;
49 return *this;
50 }
51
52 TCoord& operator+=(TCoord const& _obj)
53 {
54 left += _obj.left;
55 top += _obj.top;
56 width += _obj.width;
57 height += _obj.height;
58 return *this;
59 }
60
61 TCoord operator-(TCoord const& _obj) const
62 {
63 return TCoord(left - _obj.left, top - _obj.top, width - _obj.width, height - _obj.height);
64 }
65
66 TCoord operator-(TPoint<T> const& _obj) const
67 {
68 return TCoord(left - _obj.left, top - _obj.top, width, height);
69 }
70
71 TCoord operator-(TSize<T> const& _obj) const
72 {
73 return TCoord(left, top, width - _obj.width, height - _obj.height);
74 }
75
76 TCoord operator+(TCoord const& _obj) const
77 {
78 return TCoord(left + _obj.left, top + _obj.top, width + _obj.width, height + _obj.height);
79 }
80
81 TCoord operator+(TPoint<T> const& _obj) const
82 {
83 return TCoord(left + _obj.left, top + _obj.top, width, height);
84 }
85
86 TCoord operator+(TSize<T> const& _obj) const
87 {
88 return TCoord(left, top, width + _obj.width, height + _obj.height);
89 }
90
91 template<typename U>
93 {
94 left = _obj.left;
95 top = _obj.top;
96 width = _obj.width;
97 height = _obj.height;
98 return *this;
99 }
100
102 {
103 left = _obj.left;
104 top = _obj.top;
105 return *this;
106 }
107
109 {
110 width = _obj.width;
111 height = _obj.height;
112 return *this;
113 }
114
115 bool operator==(TCoord const& _obj) const
116 {
117 return ((left == _obj.left) && (top == _obj.top) && (width == _obj.width) && (height == _obj.height));
118 }
119
120 bool operator!=(TCoord const& _obj) const
121 {
122 return !((left == _obj.left) && (top == _obj.top) && (width == _obj.width) && (height == _obj.height));
123 }
124
125 T right() const
126 {
127 return left + width;
128 }
129
130 T bottom() const
131 {
132 return top + height;
133 }
134
135 void clear()
136 {
137 left = top = width = height = 0;
138 }
139
140 void set(T const& _left, T const& _top, T const& _width, T const& _height)
141 {
142 left = _left;
143 top = _top;
144 width = _width;
145 height = _height;
146 }
147
148 void swap(TCoord& _value)
149 {
150 TCoord tmp = _value;
151 _value = *this;
152 *this = tmp;
153 }
154
155 bool empty() const
156 {
157 return ((left == 0) && (top == 0) && (width == 0) && (height == 0));
158 }
159
161 {
162 return TPoint<T>(left, top);
163 }
164
166 {
167 return TSize<T>(width, height);
168 }
169
170 bool inside(const TPoint<T>& _value) const
171 {
172 return (
173 (_value.left >= left) && (_value.left <= right()) && (_value.top >= top) && (_value.top <= bottom()));
174 }
175
176 std::string print() const
177 {
178 std::ostringstream stream;
179 stream << *this;
180 return stream.str();
181 }
182
183 static TCoord<T> parse(std::string_view _value)
184 {
185 return utility::parseValue<TCoord<T>>(_value);
186 }
187
188 friend std::ostream& operator<<(std::ostream& _stream, const TCoord<T>& _value)
189 {
190 _stream << _value.left << " " << _value.top << " " << _value.width << " " << _value.height;
191 return _stream;
192 }
193
194 friend std::istream& operator>>(std::istream& _stream, TCoord<T>& _value)
195 {
196 _stream >> _value.left >> _value.top >> _value.width >> _value.height;
197 if (_stream.fail())
198 _value.clear();
199 return _stream;
200 }
201 };
202
203} // namespace MyGUI
204
205#endif // MyGUI_TCOORD_H_
T parseValue(std::string_view _value)
TCoord & operator=(TCoord< U > const &_obj)
TCoord & operator=(TPoint< T > const &_obj)
TCoord & operator=(TSize< T > const &_obj)
TCoord & operator+=(TCoord const &_obj)
TCoord(TPoint< T > const &_point, TSize< T > const &_size)
friend std::ostream & operator<<(std::ostream &_stream, const TCoord< T > &_value)
bool operator==(TCoord const &_obj) const
TCoord & operator-=(TCoord const &_obj)
TCoord(T const &_left, T const &_top, T const &_width, T const &_height)
void set(T const &_left, T const &_top, T const &_width, T const &_height)
static TCoord< T > parse(std::string_view _value)
TPoint< T > point() const
void swap(TCoord &_value)
bool operator!=(TCoord const &_obj) const
TCoord operator-(TCoord const &_obj) const
TCoord operator+(TSize< T > const &_obj) const
friend std::istream & operator>>(std::istream &_stream, TCoord< T > &_value)
TCoord operator+(TPoint< T > const &_obj) const
TSize< T > size() const
TCoord operator-(TSize< T > const &_obj) const
std::string print() const
bool inside(const TPoint< T > &_value) const
TCoord operator-(TPoint< T > const &_obj) const
TCoord operator+(TCoord const &_obj) const