MyGUI: MyGUI_TCoord.h Source File

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
14 namespace 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>
92 TCoord& operator=(TCoord<U> 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 TCoord& operator=(TPoint<T> const& _obj)
102 {
103 left = _obj.left;
104 top = _obj.top;
105 return *this;
106 }
107
108 TCoord& operator=(TSize<T> const& _obj)
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
160 TPoint<T> point() const
161 {
162 return TPoint<T>(left, top);
163 }
164
165 TSize<T> size() const
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_
MyGUI_Prerequest.h
MyGUI_TPoint.h
MyGUI_TSize.h
MyGUI::types
Definition MyGUI_TCoord.h:15
MyGUI::utility::parseValue
T parseValue(std::string_view _value)
Definition MyGUI_StringUtility.h:60
MyGUI::types::TCoord< int >::height
int height
Definition MyGUI_TCoord.h:23
MyGUI::types::TCoord::operator=
TCoord & operator=(TCoord< U > const &_obj)
Definition MyGUI_TCoord.h:92
MyGUI::types::TCoord::operator=
TCoord & operator=(TPoint< T > const &_obj)
Definition MyGUI_TCoord.h:101
MyGUI::types::TCoord::right
T right() const
Definition MyGUI_TCoord.h:125
MyGUI::types::TCoord::empty
bool empty() const
Definition MyGUI_TCoord.h:155
MyGUI::types::TCoord::operator=
TCoord & operator=(TSize< T > const &_obj)
Definition MyGUI_TCoord.h:108
MyGUI::types::TCoord::operator+=
TCoord & operator+=(TCoord const &_obj)
Definition MyGUI_TCoord.h:52
MyGUI::types::TCoord::TCoord
TCoord(TPoint< T > const &_point, TSize< T > const &_size)
Definition MyGUI_TCoord.h:35
MyGUI::types::TCoord::operator<<
friend std::ostream & operator<<(std::ostream &_stream, const TCoord< T > &_value)
Definition MyGUI_TCoord.h:188
MyGUI::types::TCoord< int >::width
int width
Definition MyGUI_TCoord.h:22
MyGUI::types::TCoord::TCoord
TCoord()=default
MyGUI::types::TCoord::operator==
bool operator==(TCoord const &_obj) const
Definition MyGUI_TCoord.h:115
MyGUI::types::TCoord::operator-=
TCoord & operator-=(TCoord const &_obj)
Definition MyGUI_TCoord.h:43
MyGUI::types::TCoord::TCoord
TCoord(T const &_left, T const &_top, T const &_width, T const &_height)
Definition MyGUI_TCoord.h:27
MyGUI::types::TCoord::set
void set(T const &_left, T const &_top, T const &_width, T const &_height)
Definition MyGUI_TCoord.h:140
MyGUI::types::TCoord< int >::top
int top
Definition MyGUI_TCoord.h:21
MyGUI::types::TCoord::parse
static TCoord< T > parse(std::string_view _value)
Definition MyGUI_TCoord.h:183
MyGUI::types::TCoord::point
TPoint< T > point() const
Definition MyGUI_TCoord.h:160
MyGUI::types::TCoord::swap
void swap(TCoord &_value)
Definition MyGUI_TCoord.h:148
MyGUI::types::TCoord::operator!=
bool operator!=(TCoord const &_obj) const
Definition MyGUI_TCoord.h:120
MyGUI::types::TCoord::operator-
TCoord operator-(TCoord const &_obj) const
Definition MyGUI_TCoord.h:61
MyGUI::types::TCoord::operator+
TCoord operator+(TSize< T > const &_obj) const
Definition MyGUI_TCoord.h:86
MyGUI::types::TCoord::operator>>
friend std::istream & operator>>(std::istream &_stream, TCoord< T > &_value)
Definition MyGUI_TCoord.h:194
MyGUI::types::TCoord::operator+
TCoord operator+(TPoint< T > const &_obj) const
Definition MyGUI_TCoord.h:81
MyGUI::types::TCoord::size
TSize< T > size() const
Definition MyGUI_TCoord.h:165
MyGUI::types::TCoord::operator-
TCoord operator-(TSize< T > const &_obj) const
Definition MyGUI_TCoord.h:71
MyGUI::types::TCoord::print
std::string print() const
Definition MyGUI_TCoord.h:176
MyGUI::types::TCoord::inside
bool inside(const TPoint< T > &_value) const
Definition MyGUI_TCoord.h:170
MyGUI::types::TCoord< int >::left
int left
Definition MyGUI_TCoord.h:20
MyGUI::types::TCoord::operator-
TCoord operator-(TPoint< T > const &_obj) const
Definition MyGUI_TCoord.h:66
MyGUI::types::TCoord::operator+
TCoord operator+(TCoord const &_obj) const
Definition MyGUI_TCoord.h:76
MyGUI::types::TCoord::bottom
T bottom() const
Definition MyGUI_TCoord.h:130
MyGUI::types::TCoord::clear
void clear()
Definition MyGUI_TCoord.h:135
MyGUI::types::TPoint
Definition MyGUI_TPoint.h:18
MyGUI::types::TPoint::left
T left
Definition MyGUI_TPoint.h:19
MyGUI::types::TPoint::top
T top
Definition MyGUI_TPoint.h:20
MyGUI::types::TSize
Definition MyGUI_TSize.h:18
MyGUI::types::TSize::width
T width
Definition MyGUI_TSize.h:19
MyGUI::types::TSize::height
T height
Definition MyGUI_TSize.h:20