diff options
Diffstat (limited to 'include/godot_cpp/variant/rect2i.hpp')
-rw-r--r-- | include/godot_cpp/variant/rect2i.hpp | 59 |
1 files changed, 42 insertions, 17 deletions
diff --git a/include/godot_cpp/variant/rect2i.hpp b/include/godot_cpp/variant/rect2i.hpp index 30c7d2d..3bf104e 100644 --- a/include/godot_cpp/variant/rect2i.hpp +++ b/include/godot_cpp/variant/rect2i.hpp @@ -32,7 +32,6 @@ #define GODOT_RECT2I_HPP #include <godot_cpp/classes/global_constants.hpp> -#include <godot_cpp/core/math.hpp> #include <godot_cpp/variant/vector2i.hpp> namespace godot { @@ -51,17 +50,24 @@ struct _NO_DISCARD_ Rect2i { int get_area() const { return size.width * size.height; } + _FORCE_INLINE_ Vector2i get_center() const { return position + (size / 2); } + inline bool intersects(const Rect2i &p_rect) const { - if (position.x > (p_rect.position.x + p_rect.size.width)) { +#ifdef MATH_CHECKS + if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) { + ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size."); + } +#endif + if (position.x >= (p_rect.position.x + p_rect.size.width)) { return false; } - if ((position.x + size.width) < p_rect.position.x) { + if ((position.x + size.width) <= p_rect.position.x) { return false; } - if (position.y > (p_rect.position.y + p_rect.size.height)) { + if (position.y >= (p_rect.position.y + p_rect.size.height)) { return false; } - if ((position.y + size.height) < p_rect.position.y) { + if ((position.y + size.height) <= p_rect.position.y) { return false; } @@ -69,13 +75,18 @@ struct _NO_DISCARD_ Rect2i { } inline bool encloses(const Rect2i &p_rect) const { +#ifdef MATH_CHECKS + if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) { + ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size."); + } +#endif return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) && - ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) && - ((p_rect.position.y + p_rect.size.y) < (position.y + size.y)); + ((p_rect.position.x + p_rect.size.x) <= (position.x + size.x)) && + ((p_rect.position.y + p_rect.size.y) <= (position.y + size.y)); } - inline bool has_no_area() const { - return (size.x <= 0 || size.y <= 0); + _FORCE_INLINE_ bool has_area() const { + return size.x > 0 && size.y > 0; } // Returns the instersection between two Rect2is or an empty Rect2i if there is no intersection @@ -92,14 +103,18 @@ struct _NO_DISCARD_ Rect2i { Point2i p_rect_end = p_rect.position + p_rect.size; Point2i end = position + size; - new_rect.size.x = (int)(Math::min(p_rect_end.x, end.x) - new_rect.position.x); - new_rect.size.y = (int)(Math::min(p_rect_end.y, end.y) - new_rect.position.y); + new_rect.size.x = Math::min(p_rect_end.x, end.x) - new_rect.position.x; + new_rect.size.y = Math::min(p_rect_end.y, end.y) - new_rect.position.y; return new_rect; } inline Rect2i merge(const Rect2i &p_rect) const { ///< return a merged rect - +#ifdef MATH_CHECKS + if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) { + ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size."); + } +#endif Rect2i new_rect; new_rect.position.x = Math::min(p_rect.position.x, position.x); @@ -108,11 +123,16 @@ struct _NO_DISCARD_ Rect2i { new_rect.size.x = Math::max(p_rect.position.x + p_rect.size.x, position.x + size.x); new_rect.size.y = Math::max(p_rect.position.y + p_rect.size.y, position.y + size.y); - new_rect.size = new_rect.size - new_rect.position; // make relative again + new_rect.size = new_rect.size - new_rect.position; // Make relative again. return new_rect; } bool has_point(const Point2i &p_point) const { +#ifdef MATH_CHECKS + if (unlikely(size.x < 0 || size.y < 0)) { + ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size."); + } +#endif if (p_point.x < position.x) { return false; } @@ -165,13 +185,18 @@ struct _NO_DISCARD_ Rect2i { return g; } - inline Rect2i expand(const Vector2i &p_vector) const { + _FORCE_INLINE_ Rect2i expand(const Vector2i &p_vector) const { Rect2i r = *this; r.expand_to(p_vector); return r; } inline void expand_to(const Point2i &p_vector) { +#ifdef MATH_CHECKS + if (unlikely(size.x < 0 || size.y < 0)) { + ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size."); + } +#endif Point2i begin = position; Point2i end = position + size; @@ -193,15 +218,15 @@ struct _NO_DISCARD_ Rect2i { size = end - begin; } - inline Rect2i abs() const { + _FORCE_INLINE_ Rect2i abs() const { return Rect2i(Point2i(position.x + Math::min(size.x, 0), position.y + Math::min(size.y, 0)), size.abs()); } - inline void set_end(const Vector2i &p_end) { + _FORCE_INLINE_ void set_end(const Vector2i &p_end) { size = p_end - position; } - inline Vector2i get_end() const { + _FORCE_INLINE_ Vector2i get_end() const { return position + size; } |