| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/error/error_macros.h" |
| 34 |
#include "core/math/vector2i.h" |
| 35 |
#include "core/templates/hashfuncs.h" |
| 36 |
|
| 37 |
class String; |
| 38 |
struct Rect2; |
| 39 |
|
| 40 |
struct [[nodiscard]] Rect2i { |
| 41 |
Point2i position; |
| 42 |
Size2i size; |
| 43 |
|
| 44 |
const Point2i &get_position() const { return position; } |
| 45 |
void set_position(const Point2i &p_position) { position = p_position; } |
| 46 |
const Size2i &get_size() const { return size; } |
| 47 |
void set_size(const Size2i &p_size) { size = p_size; } |
| 48 |
|
| 49 |
int get_area() const { return size.width * size.height; } |
| 50 |
|
| 51 |
_FORCE_INLINE_ Vector2i get_center() const { return position + (size / 2); } |
| 52 |
|
| 53 |
inline bool intersects(const Rect2i &p_rect) const { |
| 54 |
#ifdef MATH_CHECKS |
| 55 |
if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) { |
| 56 |
ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size."); |
| 57 |
} |
| 58 |
#endif |
| 59 |
if (position.x >= (p_rect.position.x + p_rect.size.width)) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
if ((position.x + size.width) <= p_rect.position.x) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
if (position.y >= (p_rect.position.y + p_rect.size.height)) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
if ((position.y + size.height) <= p_rect.position.y) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
inline bool encloses(const Rect2i &p_rect) const { |
| 76 |
#ifdef MATH_CHECKS |
| 77 |
if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) { |
| 78 |
ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size."); |
| 79 |
} |
| 80 |
#endif |
| 81 |
return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) && |
| 82 |
((p_rect.position.x + p_rect.size.x) <= (position.x + size.x)) && |
| 83 |
((p_rect.position.y + p_rect.size.y) <= (position.y + size.y)); |
| 84 |
} |
| 85 |
|
| 86 |
_FORCE_INLINE_ bool has_area() const { |
| 87 |
return size.x > 0 && size.y > 0; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
inline Rect2i intersection(const Rect2i &p_rect) const { |
| 92 |
Rect2i new_rect = p_rect; |
| 93 |
|
| 94 |
if (!intersects(new_rect)) { |
| 95 |
return Rect2i(); |
| 96 |
} |
| 97 |
|
| 98 |
new_rect.position = p_rect.position.max(position); |
| 99 |
|
| 100 |
Point2i p_rect_end = p_rect.position + p_rect.size; |
| 101 |
Point2i end = position + size; |
| 102 |
|
| 103 |
new_rect.size = p_rect_end.min(end) - new_rect.position; |
| 104 |
|
| 105 |
return new_rect; |
| 106 |
} |
| 107 |
|
| 108 |
inline Rect2i merge(const Rect2i &p_rect) const { |
| 109 |
#ifdef MATH_CHECKS |
| 110 |
if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) { |
| 111 |
ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size."); |
| 112 |
} |
| 113 |
#endif |
| 114 |
Rect2i new_rect; |
| 115 |
|
| 116 |
new_rect.position = p_rect.position.min(position); |
| 117 |
|
| 118 |
new_rect.size = (p_rect.position + p_rect.size).max(position + size); |
| 119 |
|
| 120 |
new_rect.size = new_rect.size - new_rect.position; |
| 121 |
|
| 122 |
return new_rect; |
| 123 |
} |
| 124 |
bool has_point(const Point2i &p_point) const { |
| 125 |
#ifdef MATH_CHECKS |
| 126 |
if (unlikely(size.x < 0 || size.y < 0)) { |
| 127 |
ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size."); |
| 128 |
} |
| 129 |
#endif |
| 130 |
if (p_point.x < position.x) { |
| 131 |
return false; |
| 132 |
} |
| 133 |
if (p_point.y < position.y) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
if (p_point.x >= (position.x + size.x)) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
if (p_point.y >= (position.y + size.y)) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
constexpr bool operator==(const Rect2i &p_rect) const { return position == p_rect.position && size == p_rect.size; } |
| 148 |
constexpr bool operator!=(const Rect2i &p_rect) const { return position != p_rect.position || size != p_rect.size; } |
| 149 |
|
| 150 |
Rect2i grow(int p_amount) const { |
| 151 |
Rect2i g = *this; |
| 152 |
g.position.x -= p_amount; |
| 153 |
g.position.y -= p_amount; |
| 154 |
g.size.width += p_amount * 2; |
| 155 |
g.size.height += p_amount * 2; |
| 156 |
return g; |
| 157 |
} |
| 158 |
|
| 159 |
inline Rect2i grow_side(Side p_side, int p_amount) const { |
| 160 |
Rect2i g = *this; |
| 161 |
g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0, |
| 162 |
(SIDE_TOP == p_side) ? p_amount : 0, |
| 163 |
(SIDE_RIGHT == p_side) ? p_amount : 0, |
| 164 |
(SIDE_BOTTOM == p_side) ? p_amount : 0); |
| 165 |
return g; |
| 166 |
} |
| 167 |
|
| 168 |
inline Rect2i grow_side_bind(uint32_t p_side, int p_amount) const { |
| 169 |
return grow_side(Side(p_side), p_amount); |
| 170 |
} |
| 171 |
|
| 172 |
inline Rect2i grow_individual(int p_left, int p_top, int p_right, int p_bottom) const { |
| 173 |
Rect2i g = *this; |
| 174 |
g.position.x -= p_left; |
| 175 |
g.position.y -= p_top; |
| 176 |
g.size.width += p_left + p_right; |
| 177 |
g.size.height += p_top + p_bottom; |
| 178 |
|
| 179 |
return g; |
| 180 |
} |
| 181 |
|
| 182 |
_FORCE_INLINE_ Rect2i expand(const Vector2i &p_vector) const { |
| 183 |
Rect2i r = *this; |
| 184 |
r.expand_to(p_vector); |
| 185 |
return r; |
| 186 |
} |
| 187 |
|
| 188 |
inline void expand_to(const Point2i &p_vector) { |
| 189 |
#ifdef MATH_CHECKS |
| 190 |
if (unlikely(size.x < 0 || size.y < 0)) { |
| 191 |
ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size."); |
| 192 |
} |
| 193 |
#endif |
| 194 |
Point2i begin = position; |
| 195 |
Point2i end = position + size; |
| 196 |
|
| 197 |
if (p_vector.x < begin.x) { |
| 198 |
begin.x = p_vector.x; |
| 199 |
} |
| 200 |
if (p_vector.y < begin.y) { |
| 201 |
begin.y = p_vector.y; |
| 202 |
} |
| 203 |
|
| 204 |
if (p_vector.x > end.x) { |
| 205 |
end.x = p_vector.x; |
| 206 |
} |
| 207 |
if (p_vector.y > end.y) { |
| 208 |
end.y = p_vector.y; |
| 209 |
} |
| 210 |
|
| 211 |
position = begin; |
| 212 |
size = end - begin; |
| 213 |
} |
| 214 |
|
| 215 |
_FORCE_INLINE_ Rect2i abs() const { |
| 216 |
return Rect2i(position + size.mini(0), size.abs()); |
| 217 |
} |
| 218 |
|
| 219 |
_FORCE_INLINE_ void set_end(const Vector2i &p_end) { |
| 220 |
size = p_end - position; |
| 221 |
} |
| 222 |
|
| 223 |
_FORCE_INLINE_ Vector2i get_end() const { |
| 224 |
return position + size; |
| 225 |
} |
| 226 |
|
| 227 |
explicit operator String() const; |
| 228 |
operator Rect2() const; |
| 229 |
|
| 230 |
uint32_t hash() const { |
| 231 |
uint32_t h = hash_murmur3_one_32(uint32_t(position.x)); |
| 232 |
h = hash_murmur3_one_32(uint32_t(position.y), h); |
| 233 |
h = hash_murmur3_one_32(uint32_t(size.x), h); |
| 234 |
h = hash_murmur3_one_32(uint32_t(size.y), h); |
| 235 |
return hash_fmix32(h); |
| 236 |
} |
| 237 |
|
| 238 |
Rect2i() = default; |
| 239 |
constexpr Rect2i(int p_x, int p_y, int p_width, int p_height) : |
| 240 |
position(Point2i(p_x, p_y)), |
| 241 |
size(Size2i(p_width, p_height)) { |
| 242 |
} |
| 243 |
constexpr Rect2i(const Point2i &p_pos, const Size2i &p_size) : |
| 244 |
position(p_pos), |
| 245 |
size(p_size) { |
| 246 |
} |
| 247 |
}; |
| 248 |
|
| 249 |
template <> |
| 250 |
struct is_zero_constructible<Rect2i> : std::true_type {}; |