TGUI  0.9-dev
Rect.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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 #include <SFML/Graphics/Rect.hpp>
32 
34 
35 namespace tgui
36 {
37  template <typename T>
38  class Rect
39  {
40  public:
41 
47  TGUI_CONSTEXPR Rect() = default;
48 
49 
55  template <typename U>
56  explicit TGUI_CONSTEXPR Rect(const Rect<U>& rect) :
57  left {static_cast<T>(rect.left)},
58  top {static_cast<T>(rect.top)},
59  width {static_cast<T>(rect.width)},
60  height{static_cast<T>(rect.height)}
61  {
62  }
63 
64 
70  explicit TGUI_CONSTEXPR Rect(sf::Rect<T> rect) :
71  left {rect.left},
72  top {rect.top},
73  width {rect.width},
74  height{rect.height}
75  {
76  }
77 
78 
87  TGUI_CONSTEXPR Rect(T rectLeft, T rectTop, T rectWidth, T rectHeight) :
88  left {rectLeft},
89  top {rectTop},
90  width {rectWidth},
91  height{rectHeight}
92  {
93  }
94 
95 
102  TGUI_CONSTEXPR Rect(Vector2<T> position, Vector2<T> size) :
103  left {position.x},
104  top {position.y},
105  width {size.x},
106  height{size.y}
107  {
108  }
109 
110 
116  TGUI_CONSTEXPR void setPosition(Vector2<T> position)
117  {
118  left = position.x;
119  top = position.y;
120  }
121 
122 
128  TGUI_CONSTEXPR Vector2<T> getPosition() const
129  {
130  return {left, top};
131  }
132 
133 
139  TGUI_CONSTEXPR void setSize(Vector2<T> size)
140  {
141  width = size.x;
142  height = size.y;
143  }
144 
145 
151  TGUI_CONSTEXPR Vector2<T> getSize() const
152  {
153  return {width, height};
154  }
155 
156 
160  explicit operator sf::Rect<T>() const
161  {
162  return sf::Rect<T>{left, top, width, height};
163  }
164 
165 
177  TGUI_CONSTEXPR bool contains(const Vector2<T>& pos) const
178  {
179  return (pos.x >= left) && (pos.x < left + width) && (pos.y >= top) && (pos.y < top + height);
180  }
181 
182 
192  TGUI_CONSTEXPR bool intersects(const Rect<T>& rect) const
193  {
194  // Compute the intersection boundaries
195  const T interLeft = std::max(left, rect.left);
196  const T interTop = std::max(top, rect.top);
197  const T interRight = std::min(left + width, rect.left + rect.width);
198  const T interBottom = std::min(top + height, rect.top + rect.height);
199 
200  // If the intersection is valid (positive non zero area), then there is an intersection
201  return (interLeft < interRight) && (interTop < interBottom);
202  }
203 
204 
206  public:
207 
208  T left = 0;
209  T top = 0;
210  T width = 0;
211  T height = 0;
212  };
213 
214 
218  template <typename T>
219  TGUI_CONSTEXPR bool operator==(const Rect<T>& left, const Rect<T>& right)
220  {
221  return (left.left == right.left) && (left.width == right.width)
222  && (left.top == right.top) && (left.height == right.height);
223  }
224 
225 
229  template <typename T>
230  TGUI_CONSTEXPR bool operator!=(const Rect<T>& left, const Rect<T>& right)
231  {
232  return !(left == right);
233  }
234 
235 
237 
238  using FloatRect = Rect<float>;
239  using IntRect = Rect<int>;
240  using UIntRect = Rect<unsigned int>;
241 }
242 
244 
245 #endif // TGUI_RECT_HPP
TGUI_CONSTEXPR Rect()=default
Default constructor.
T width
Width of the rectangle.
Definition: Rect.hpp:210
TGUI_CONSTEXPR Rect(sf::Rect< T > rect)
Constructs the rectangle from an sf::Rect.
Definition: Rect.hpp:70
TGUI_CONSTEXPR Rect(const Rect< U > &rect)
Constructs the rectangle from an another Rect with a different type.
Definition: Rect.hpp:56
TGUI_CONSTEXPR Vector2< T > getPosition() const
Returns the position of the rectangle.
Definition: Rect.hpp:128
T top
Top coordinate of the rectangle.
Definition: Rect.hpp:209
T y
Y coordinate of the vector.
Definition: Vector2.hpp:141
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
T x
X coordinate of the vector.
Definition: Vector2.hpp:140
T left
Left coordinate of the rectangle.
Definition: Rect.hpp:208
TGUI_CONSTEXPR void setPosition(Vector2< T > position)
Sets the position of the rectangle.
Definition: Rect.hpp:116
TGUI_CONSTEXPR bool contains(const Vector2< T > &pos) const
Check if a point is inside the rectangle's area.
Definition: Rect.hpp:177
TGUI_CONSTEXPR bool intersects(const Rect< T > &rect) const
Check the intersection between two rectangles.
Definition: Rect.hpp:192
TGUI_CONSTEXPR Rect(Vector2< T > position, Vector2< T > size)
Constructs the rectangle from its position and size.
Definition: Rect.hpp:102
Definition: Rect.hpp:38
TGUI_CONSTEXPR Rect(T rectLeft, T rectTop, T rectWidth, T rectHeight)
Constructs the rectangle from its position and size.
Definition: Rect.hpp:87
TGUI_CONSTEXPR Vector2< T > getSize() const
Returns the size of the rectangle.
Definition: Rect.hpp:151
Definition: Vector2.hpp:40
T height
Height of the rectangle.
Definition: Rect.hpp:211
TGUI_CONSTEXPR void setSize(Vector2< T > size)
Sets the size of the rectangle.
Definition: Rect.hpp:139