Added abs helper method to Rect2 and Rect2i.

This commit is contained in:
Relintai 2022-02-04 13:21:15 +01:00
parent e34079ee46
commit 2c16563d97
2 changed files with 13 additions and 1 deletions

View File

@ -22,6 +22,8 @@ public:
void expand_to(const Vector2 &p_vector); void expand_to(const Vector2 &p_vector);
inline Rect2 clip(const Rect2 &p_rect) const; inline Rect2 clip(const Rect2 &p_rect) const;
inline Rect2 abs() const;
Vector2 position() const; Vector2 position() const;
Vector2 size() const; Vector2 size() const;
@ -51,7 +53,7 @@ public:
// Taken from the Godot Engine (MIT License) // Taken from the Godot Engine (MIT License)
// Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. // Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.
// Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). // Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).
inline Rect2 Rect2::clip(const Rect2 &p_rect) const { /// return a clipped rect Rect2 Rect2::clip(const Rect2 &p_rect) const { /// return a clipped rect
Rect2 new_rect = p_rect; Rect2 new_rect = p_rect;
if (!intersects(new_rect)) { if (!intersects(new_rect)) {
@ -70,4 +72,8 @@ inline Rect2 Rect2::clip(const Rect2 &p_rect) const { /// return a clipped rect
return new_rect; return new_rect;
} }
Rect2 Rect2::abs() const {
return Rect2(x + MIN(w, 0), y + MIN(h, 0), ABS(w), ABS(h));
}
#endif #endif

View File

@ -23,6 +23,8 @@ public:
void expand_to(const Vector2i &p_vector); void expand_to(const Vector2i &p_vector);
inline Rect2i clip(const Rect2i &p_rect) const; inline Rect2i clip(const Rect2i &p_rect) const;
inline Rect2i abs() const;
Vector2i position() const; Vector2i position() const;
Vector2i size() const; Vector2i size() const;
@ -74,4 +76,8 @@ inline Rect2i Rect2i::clip(const Rect2i &p_rect) const { /// return a clipped re
return new_rect; return new_rect;
} }
Rect2i Rect2i::abs() const {
return Rect2i(x + MIN(w, 0), y + MIN(h, 0), ABS(w), ABS(h));
}
#endif #endif