MyGUI: MyGUI_TRect.h Source File

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_
MyGUI_Prerequest.h
MyGUI_StringUtility.h
MyGUI::types
Definition MyGUI_TCoord.h:15
MyGUI::utility::parseValue
T parseValue(std::string_view _value)
Definition MyGUI_StringUtility.h:60
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::TRect::inside
bool inside(const TPoint< T > &_value) const
Definition MyGUI_TRect.h:130
MyGUI::types::TRect< int >::right
int right
Definition MyGUI_TRect.h:21
MyGUI::types::TRect::print
std::string print() const
Definition MyGUI_TRect.h:135
MyGUI::types::TRect::set
void set(T const &_left, T const &_top, T const &_right, T const &_bottom)
Definition MyGUI_TRect.h:98
MyGUI::types::TRect::operator!=
bool operator!=(TRect const &_obj) const
Definition MyGUI_TRect.h:78
MyGUI::types::TRect::operator-=
TRect & operator-=(TRect const &_obj)
Definition MyGUI_TRect.h:35
MyGUI::types::TRect::height
T height() const
Definition MyGUI_TRect.h:88
MyGUI::types::TRect::swap
void swap(TRect &_value)
Definition MyGUI_TRect.h:106
MyGUI::types::TRect::TRect
TRect(T const &_left, T const &_top, T const &_right, T const &_bottom)
Definition MyGUI_TRect.h:26
MyGUI::types::TRect::empty
bool empty() const
Definition MyGUI_TRect.h:113
MyGUI::types::TRect::inside
bool inside(const TRect< T > &_value) const
Definition MyGUI_TRect.h:118
MyGUI::types::TRect::operator+
TRect operator+(TRect const &_obj) const
Definition MyGUI_TRect.h:58
MyGUI::types::TRect::operator==
bool operator==(TRect const &_obj) const
Definition MyGUI_TRect.h:73
MyGUI::types::TRect::clear
void clear()
Definition MyGUI_TRect.h:93
MyGUI::types::TRect::operator>>
friend std::istream & operator>>(std::istream &_stream, TRect< T > &_value)
Definition MyGUI_TRect.h:153
MyGUI::types::TRect< int >::left
int left
Definition MyGUI_TRect.h:19
MyGUI::types::TRect::intersect
bool intersect(const TRect< T > &_value) const
Definition MyGUI_TRect.h:124
MyGUI::types::TRect::operator-
TRect operator-(TRect const &_obj) const
Definition MyGUI_TRect.h:53
MyGUI::types::TRect::operator<<
friend std::ostream & operator<<(std::ostream &_stream, const TRect< T > &_value)
Definition MyGUI_TRect.h:147
MyGUI::types::TRect< int >::bottom
int bottom
Definition MyGUI_TRect.h:22
MyGUI::types::TRect::width
T width() const
Definition MyGUI_TRect.h:83
MyGUI::types::TRect::TRect
TRect()=default
MyGUI::types::TRect< int >::top
int top
Definition MyGUI_TRect.h:20
MyGUI::types::TRect::operator+=
TRect & operator+=(TRect const &_obj)
Definition MyGUI_TRect.h:44
MyGUI::types::TRect::operator=
TRect & operator=(TRect< U > const &_obj)
Definition MyGUI_TRect.h:64
MyGUI::types::TRect::parse
static TRect< T > parse(std::string_view _value)
Definition MyGUI_TRect.h:142