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