TGUI 1.13
Loading...
Searching...
No Matches
Rect.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 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#ifndef TGUI_RECT_HPP
26#define TGUI_RECT_HPP
27
28#include <TGUI/Vector2.hpp>
29
30#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
31 #include <SFML/Graphics/Rect.hpp>
32#endif
33
35
36namespace tgui
37{
38 template <typename T>
39 class Rect
40 {
41 public:
47 constexpr Rect() = default;
48
54 template <typename U>
55 explicit constexpr Rect(const Rect<U>& rect) :
56 left{static_cast<T>(rect.left)},
57 top{static_cast<T>(rect.top)},
58 width{static_cast<T>(rect.width)},
59 height{static_cast<T>(rect.height)}
60 {
61 }
62
63#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
69 explicit constexpr Rect(sf::Rect<T> rect) :
70 left{rect.left},
71 top{rect.top},
72 width{rect.width},
73 height{rect.height}
74 {
75 }
76#endif
77
86 constexpr Rect(T rectLeft, T rectTop, T rectWidth, T rectHeight) :
87 left{rectLeft},
88 top{rectTop},
89 width{rectWidth},
90 height{rectHeight}
91 {
92 }
93
100 constexpr Rect(Vector2<T> position, Vector2<T> size) :
101 left{position.x},
102 top{position.y},
103 width{size.x},
104 height{size.y}
105 {
106 }
107
113 constexpr void setPosition(Vector2<T> position)
114 {
115 left = position.x;
116 top = position.y;
117 }
118
124 [[nodiscard]] constexpr Vector2<T> getPosition() const
125 {
126 return {left, top};
127 }
128
134 constexpr void setSize(Vector2<T> size)
135 {
136 width = size.x;
137 height = size.y;
138 }
139
145 [[nodiscard]] constexpr Vector2<T> getSize() const
146 {
147 return {width, height};
148 }
149
150#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
154 explicit operator sf::Rect<T>() const
155 {
156 return sf::Rect<T>{left, top, width, height};
157 }
158#endif
159
171 [[nodiscard]] constexpr bool contains(const Vector2<T>& pos) const
172 {
173 return (pos.x >= left) && (pos.x < left + width) && (pos.y >= top) && (pos.y < top + height);
174 }
175
185 [[nodiscard]] constexpr bool intersects(const Rect<T>& rect) const
186 {
187 // Compute the intersection boundaries
188 const T interLeft = std::max(left, rect.left);
189 const T interTop = std::max(top, rect.top);
190 const T interRight = std::min(left + width, rect.left + rect.width);
191 const T interBottom = std::min(top + height, rect.top + rect.height);
192
193 // If the intersection is valid (positive non zero area), then there is an intersection
194 return (interLeft < interRight) && (interTop < interBottom);
195 }
196
198
199 public:
200 T left = 0;
201 T top = 0;
202 T width = 0;
203 T height = 0;
204 };
205
209 template <typename T>
210 [[nodiscard]] constexpr bool operator==(const Rect<T>& left, const Rect<T>& right)
211 {
212 return (left.left == right.left) && (left.width == right.width) && (left.top == right.top) && (left.height == right.height);
213 }
214
218 template <typename T>
219 [[nodiscard]] constexpr bool operator!=(const Rect<T>& left, const Rect<T>& right)
220 {
221 return !(left == right);
222 }
223
225
226 using FloatRect = Rect<float>;
227 using IntRect = Rect<int>;
228 using UIntRect = Rect<unsigned int>;
229} // namespace tgui
230
231#endif // TGUI_RECT_HPP
Definition Rect.hpp:40
constexpr Rect()=default
Default constructor.
float width
Definition Rect.hpp:202
float height
Definition Rect.hpp:203
constexpr Rect(sf::Rect< T > rect)
Constructs the rectangle from an sf::Rect.
Definition Rect.hpp:69
constexpr bool intersects(const Rect< T > &rect) const
Check the intersection between two rectangles.
Definition Rect.hpp:185
constexpr Vector2< T > getSize() const
Returns the size of the rectangle.
Definition Rect.hpp:145
constexpr Rect(const Rect< U > &rect)
Constructs the rectangle from an another Rect with a different type.
Definition Rect.hpp:55
float left
Definition Rect.hpp:200
constexpr void setPosition(Vector2< T > position)
Sets the position of the rectangle.
Definition Rect.hpp:113
constexpr Vector2< T > getPosition() const
Returns the position of the rectangle.
Definition Rect.hpp:124
constexpr bool contains(const Vector2< T > &pos) const
Check if a point is inside the rectangle's area.
Definition Rect.hpp:171
float top
Definition Rect.hpp:201
constexpr void setSize(Vector2< T > size)
Sets the size of the rectangle.
Definition Rect.hpp:134
constexpr Rect(T rectLeft, T rectTop, T rectWidth, T rectHeight)
Constructs the rectangle from its position and size.
Definition Rect.hpp:86
constexpr Rect(Vector2< T > position, Vector2< T > size)
Constructs the rectangle from its position and size.
Definition Rect.hpp:100
Definition Vector2.hpp:42
T y
Y coordinate of the vector.
Definition Vector2.hpp:136
T x
X coordinate of the vector.
Definition Vector2.hpp:135
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37