MyGUI 3.4.3
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#include "MyGUI_StringUtility.h"
12
13namespace MyGUI::types
14{
15
16 template<typename T>
17 struct TRect
18 {
19 T left{};
20 T top{};
21 T right{};
22 T bottom{};
23
24 TRect() = default;
25
26 TRect(T const& _left, T const& _top, T const& _right, T const& _bottom) :
27 left(_left),
28 top(_top),
29 right(_right),
30 bottom(_bottom)
31 {
32 }
33
34
35 TRect& operator-=(TRect const& _obj)
36 {
37 left -= _obj.left;
38 top -= _obj.top;
39 right -= _obj.right;
40 bottom -= _obj.bottom;
41 return *this;
42 }
43
44 TRect& operator+=(TRect const& _obj)
45 {
46 left += _obj.left;
47 top += _obj.top;
48 right += _obj.right;
49 bottom += _obj.bottom;
50 return *this;
51 }
52
53 TRect operator-(TRect const& _obj) const
54 {
55 return TRect(left - _obj.left, top - _obj.top, right - _obj.right, bottom - _obj.bottom);
56 }
57
58 TRect operator+(TRect const& _obj) const
59 {
60 return TRect(left + _obj.left, top + _obj.top, right + _obj.right, bottom + _obj.bottom);
61 }
62
63 template<typename U>
64 TRect& operator=(TRect<U> const& _obj)
65 {
66 left = _obj.left;
67 top = _obj.top;
68 right = _obj.right;
69 bottom = _obj.bottom;
70 return *this;
71 }
72
73 bool operator==(TRect const& _obj) const
74 {
75 return ((left == _obj.left) && (top == _obj.top) && (right == _obj.right) && (bottom == _obj.bottom));
76 }
77
78 bool operator!=(TRect const& _obj) const
79 {
80 return !((left == _obj.left) && (top == _obj.top) && (right == _obj.right) && (bottom == _obj.bottom));
81 }
82
83 T width() const
84 {
85 return right - left;
86 }
87
88 T height() const
89 {
90 return bottom - top;
91 }
92
93 void clear()
94 {
95 left = top = right = bottom = 0;
96 }
97
98 void set(T const& _left, T const& _top, T const& _right, T const& _bottom)
99 {
100 left = _left;
101 top = _top;
102 right = _right;
103 bottom = _bottom;
104 }
105
106 void swap(TRect& _value)
107 {
108 TRect tmp = _value;
109 _value = *this;
110 *this = tmp;
111 }
112
113 bool empty() const
114 {
115 return ((left == 0) && (top == 0) && (right == 0) && (bottom == 0));
116 }
117
118 bool inside(const TRect<T>& _value) const
119 {
120 return (
121 (_value.left >= left) && (_value.right <= right) && (_value.top >= top) && (_value.bottom <= bottom));
122 }
123
124 bool intersect(const TRect<T>& _value) const
125 {
126 return (
127 (_value.left <= right) && (_value.right >= left) && (_value.top <= bottom) && (_value.bottom >= top));
128 }
129
130 bool inside(const TPoint<T>& _value) const
131 {
132 return ((_value.left >= left) && (_value.left <= right) && (_value.top >= top) && (_value.top <= bottom));
133 }
134
135 std::string print() const
136 {
137 std::ostringstream stream;
138 stream << *this;
139 return stream.str();
140 }
141
142 static TRect<T> parse(std::string_view _value)
143 {
144 return utility::parseValue<TRect<T>>(_value);
145 }
146
147 friend std::ostream& operator<<(std::ostream& _stream, const TRect<T>& _value)
148 {
149 _stream << _value.left << " " << _value.top << " " << _value.right << " " << _value.bottom;
150 return _stream;
151 }
152
153 friend std::istream& operator>>(std::istream& _stream, TRect<T>& _value)
154 {
155 _stream >> _value.left >> _value.top >> _value.right >> _value.bottom;
156 if (_stream.fail())
157 _value.clear();
158 return _stream;
159 }
160 };
161
162} // namespace MyGUI
163
164#endif // MYGUI_TRECT_H_
T parseValue(std::string_view _value)
bool inside(const TPoint< T > &_value) const
std::string print() const
void set(T const &_left, T const &_top, T const &_right, T const &_bottom)
Definition MyGUI_TRect.h:98
bool operator!=(TRect const &_obj) const
Definition MyGUI_TRect.h:78
TRect & operator-=(TRect const &_obj)
Definition MyGUI_TRect.h:35
void swap(TRect &_value)
TRect(T const &_left, T const &_top, T const &_right, T const &_bottom)
Definition MyGUI_TRect.h:26
bool empty() const
bool inside(const TRect< T > &_value) const
TRect operator+(TRect const &_obj) const
Definition MyGUI_TRect.h:58
bool operator==(TRect const &_obj) const
Definition MyGUI_TRect.h:73
friend std::istream & operator>>(std::istream &_stream, TRect< T > &_value)
bool intersect(const TRect< T > &_value) const
TRect operator-(TRect const &_obj) const
Definition MyGUI_TRect.h:53
friend std::ostream & operator<<(std::ostream &_stream, const TRect< T > &_value)
TRect & operator+=(TRect const &_obj)
Definition MyGUI_TRect.h:44
TRect & operator=(TRect< U > const &_obj)
Definition MyGUI_TRect.h:64
static TRect< T > parse(std::string_view _value)