TGUI  0.9-dev
Rect.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2021 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25
26#ifndef TGUI_RECT_HPP
27#define TGUI_RECT_HPP
28
29#include <TGUI/Vector2.hpp>
30
31#if TGUI_HAS_BACKEND_SFML_GRAPHICS
32 #include <SFML/Graphics/Rect.hpp>
33#endif
34
36
37namespace tgui
38{
39 template <typename T>
40 class Rect
41 {
42 public:
43
49 TGUI_CONSTEXPR Rect() = default;
50
51
57 template <typename U>
58 explicit TGUI_CONSTEXPR Rect(const Rect<U>& rect) :
59 left {static_cast<T>(rect.left)},
60 top {static_cast<T>(rect.top)},
61 width {static_cast<T>(rect.width)},
62 height{static_cast<T>(rect.height)}
63 {
64 }
65
66#if TGUI_HAS_BACKEND_SFML_GRAPHICS
72 explicit TGUI_CONSTEXPR Rect(sf::Rect<T> rect) :
73 left {rect.left},
74 top {rect.top},
75 width {rect.width},
76 height{rect.height}
77 {
78 }
79#endif
80
89 TGUI_CONSTEXPR Rect(T rectLeft, T rectTop, T rectWidth, T rectHeight) :
90 left {rectLeft},
91 top {rectTop},
92 width {rectWidth},
93 height{rectHeight}
94 {
95 }
96
97
104 TGUI_CONSTEXPR Rect(Vector2<T> position, Vector2<T> size) :
105 left {position.x},
106 top {position.y},
107 width {size.x},
108 height{size.y}
109 {
110 }
111
112
118 TGUI_CONSTEXPR void setPosition(Vector2<T> position)
119 {
120 left = position.x;
121 top = position.y;
122 }
123
124
130 TGUI_CONSTEXPR Vector2<T> getPosition() const
131 {
132 return {left, top};
133 }
134
135
141 TGUI_CONSTEXPR void setSize(Vector2<T> size)
142 {
143 width = size.x;
144 height = size.y;
145 }
146
147
153 TGUI_CONSTEXPR Vector2<T> getSize() const
154 {
155 return {width, height};
156 }
157
158#if TGUI_HAS_BACKEND_SFML_GRAPHICS
162 explicit operator sf::Rect<T>() const
163 {
164 return sf::Rect<T>{left, top, width, height};
165 }
166#endif
167
179 TGUI_CONSTEXPR bool contains(const Vector2<T>& pos) const
180 {
181 return (pos.x >= left) && (pos.x < left + width) && (pos.y >= top) && (pos.y < top + height);
182 }
183
184
194 TGUI_CONSTEXPR bool intersects(const Rect<T>& rect) const
195 {
196 // Compute the intersection boundaries
197 const T interLeft = std::max(left, rect.left);
198 const T interTop = std::max(top, rect.top);
199 const T interRight = std::min(left + width, rect.left + rect.width);
200 const T interBottom = std::min(top + height, rect.top + rect.height);
201
202 // If the intersection is valid (positive non zero area), then there is an intersection
203 return (interLeft < interRight) && (interTop < interBottom);
204 }
205
206
208 public:
209
210 T left = 0;
211 T top = 0;
212 T width = 0;
213 T height = 0;
214 };
215
216
220 template <typename T>
221 TGUI_CONSTEXPR bool operator==(const Rect<T>& left, const Rect<T>& right)
222 {
223 return (left.left == right.left) && (left.width == right.width)
224 && (left.top == right.top) && (left.height == right.height);
225 }
226
227
231 template <typename T>
232 TGUI_CONSTEXPR bool operator!=(const Rect<T>& left, const Rect<T>& right)
233 {
234 return !(left == right);
235 }
236
237
239
240 using FloatRect = Rect<float>;
241 using IntRect = Rect<int>;
242 using UIntRect = Rect<unsigned int>;
243}
244
246
247#endif // TGUI_RECT_HPP
Definition: Rect.hpp:41
TGUI_CONSTEXPR Rect()=default
Default constructor.
TGUI_CONSTEXPR Rect(T rectLeft, T rectTop, T rectWidth, T rectHeight)
Constructs the rectangle from its position and size.
Definition: Rect.hpp:89
TGUI_CONSTEXPR Rect(Vector2< T > position, Vector2< T > size)
Constructs the rectangle from its position and size.
Definition: Rect.hpp:104
T width
Width of the rectangle.
Definition: Rect.hpp:212
TGUI_CONSTEXPR Vector2< T > getSize() const
Returns the size of the rectangle.
Definition: Rect.hpp:153
T height
Height of the rectangle.
Definition: Rect.hpp:213
TGUI_CONSTEXPR Vector2< T > getPosition() const
Returns the position of the rectangle.
Definition: Rect.hpp:130
TGUI_CONSTEXPR void setPosition(Vector2< T > position)
Sets the position of the rectangle.
Definition: Rect.hpp:118
T left
Left coordinate of the rectangle.
Definition: Rect.hpp:210
T top
Top coordinate of the rectangle.
Definition: Rect.hpp:211
TGUI_CONSTEXPR Rect(const Rect< U > &rect)
Constructs the rectangle from an another Rect with a different type.
Definition: Rect.hpp:58
TGUI_CONSTEXPR bool contains(const Vector2< T > &pos) const
Check if a point is inside the rectangle's area.
Definition: Rect.hpp:179
TGUI_CONSTEXPR bool intersects(const Rect< T > &rect) const
Check the intersection between two rectangles.
Definition: Rect.hpp:194
TGUI_CONSTEXPR void setSize(Vector2< T > size)
Sets the size of the rectangle.
Definition: Rect.hpp:141
Definition: Vector2.hpp:42
T y
Y coordinate of the vector.
Definition: Vector2.hpp:142
T x
X coordinate of the vector.
Definition: Vector2.hpp:141
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36