From 2c16563d97bc015a65f9e3dc8b7a0dd803f1ff22 Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 4 Feb 2022 13:21:15 +0100 Subject: [PATCH] Added abs helper method to Rect2 and Rect2i. --- core/math/rect2.h | 8 +++++++- core/math/rect2i.h | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/core/math/rect2.h b/core/math/rect2.h index 48cbaf4..7f244be 100644 --- a/core/math/rect2.h +++ b/core/math/rect2.h @@ -22,6 +22,8 @@ public: void expand_to(const Vector2 &p_vector); inline Rect2 clip(const Rect2 &p_rect) const; + inline Rect2 abs() const; + Vector2 position() const; Vector2 size() const; @@ -51,7 +53,7 @@ public: // Taken from the Godot Engine (MIT License) // Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. // 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; if (!intersects(new_rect)) { @@ -70,4 +72,8 @@ inline Rect2 Rect2::clip(const Rect2 &p_rect) const { /// return a clipped rect return new_rect; } +Rect2 Rect2::abs() const { + return Rect2(x + MIN(w, 0), y + MIN(h, 0), ABS(w), ABS(h)); +} + #endif \ No newline at end of file diff --git a/core/math/rect2i.h b/core/math/rect2i.h index 58690bf..0ff581a 100644 --- a/core/math/rect2i.h +++ b/core/math/rect2i.h @@ -23,6 +23,8 @@ public: void expand_to(const Vector2i &p_vector); inline Rect2i clip(const Rect2i &p_rect) const; + inline Rect2i abs() const; + Vector2i position() const; Vector2i size() const; @@ -74,4 +76,8 @@ inline Rect2i Rect2i::clip(const Rect2i &p_rect) const { /// return a clipped re return new_rect; } +Rect2i Rect2i::abs() const { + return Rect2i(x + MIN(w, 0), y + MIN(h, 0), ABS(w), ABS(h)); +} + #endif \ No newline at end of file