From 2e13e3ed4a0f94e5f868cc827d1ba6ad4bed8b35 Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Mon, 1 Feb 2021 00:10:52 -0500 Subject: Allow clamping vectors and colors --- .../mono/glue/GodotSharp/GodotSharp/Core/Color.cs | 21 +++++++++++++++++++++ .../mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs | 17 +++++++++++++++++ .../glue/GodotSharp/GodotSharp/Core/Vector2i.cs | 17 +++++++++++++++++ .../mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs | 18 ++++++++++++++++++ .../glue/GodotSharp/GodotSharp/Core/Vector3i.cs | 18 ++++++++++++++++++ 5 files changed, 91 insertions(+) (limited to 'modules/mono/glue') diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs index 2a9f834aac..24b9218197 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs @@ -256,6 +256,27 @@ namespace Godot return res; } + /// + /// Returns a new color with all components clamped between the + /// components of `min` and `max` using + /// . + /// + /// The color with minimum allowed values. + /// The color with maximum allowed values. + /// The color with all components clamped. + public Color Clamp(Color? min = null, Color? max = null) + { + Color minimum = min ?? new Color(0, 0, 0, 0); + Color maximum = max ?? new Color(1, 1, 1, 1); + return new Color + ( + (float)Mathf.Clamp(r, minimum.r, maximum.r), + (float)Mathf.Clamp(g, minimum.g, maximum.g), + (float)Mathf.Clamp(b, minimum.b, maximum.b), + (float)Mathf.Clamp(a, minimum.a, maximum.a) + ); + } + /// /// Returns a new color resulting from making this color darker /// by the specified ratio (on the range of 0 to 1). diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs index c9bf415622..334c7cd510 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs @@ -159,6 +159,23 @@ namespace Godot return new Vector2(Mathf.Ceil(x), Mathf.Ceil(y)); } + /// + /// Returns a new vector with all components clamped between the + /// components of `min` and `max` using + /// . + /// + /// The vector with minimum allowed values. + /// The vector with maximum allowed values. + /// The vector with all components clamped. + public Vector2 Clamp(Vector2 min, Vector2 max) + { + return new Vector2 + ( + Mathf.Clamp(x, min.x, max.x), + Mathf.Clamp(y, min.y, max.y) + ); + } + /// /// Returns the cross product of this vector and `b`. /// diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2i.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2i.cs index f605ba8fd0..9068593fd8 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2i.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2i.cs @@ -119,6 +119,23 @@ namespace Godot return x / (real_t)y; } + /// + /// Returns a new vector with all components clamped between the + /// components of `min` and `max` using + /// . + /// + /// The vector with minimum allowed values. + /// The vector with maximum allowed values. + /// The vector with all components clamped. + public Vector2i Clamp(Vector2i min, Vector2i max) + { + return new Vector2i + ( + Mathf.Clamp(x, min.x, max.x), + Mathf.Clamp(y, min.y, max.y) + ); + } + /// /// Returns the cross product of this vector and `b`. /// diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs index 14aeac41d0..fe1b3ad3c0 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs @@ -139,6 +139,24 @@ namespace Godot return new Vector3(Mathf.Ceil(x), Mathf.Ceil(y), Mathf.Ceil(z)); } + /// + /// Returns a new vector with all components clamped between the + /// components of `min` and `max` using + /// . + /// + /// The vector with minimum allowed values. + /// The vector with maximum allowed values. + /// The vector with all components clamped. + public Vector3 Clamp(Vector3 min, Vector3 max) + { + return new Vector3 + ( + Mathf.Clamp(x, min.x, max.x), + Mathf.Clamp(y, min.y, max.y), + Mathf.Clamp(z, min.z, max.z) + ); + } + /// /// Returns the cross product of this vector and `b`. /// diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3i.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3i.cs index bf25ba9cb3..e727afa3ff 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3i.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3i.cs @@ -88,6 +88,24 @@ namespace Godot return new Vector3i(Mathf.Abs(x), Mathf.Abs(y), Mathf.Abs(z)); } + /// + /// Returns a new vector with all components clamped between the + /// components of `min` and `max` using + /// . + /// + /// The vector with minimum allowed values. + /// The vector with maximum allowed values. + /// The vector with all components clamped. + public Vector3i Clamp(Vector3i min, Vector3i max) + { + return new Vector3i + ( + Mathf.Clamp(x, min.x, max.x), + Mathf.Clamp(y, min.y, max.y), + Mathf.Clamp(z, min.z, max.z) + ); + } + /// /// Returns the squared distance between this vector and `b`. /// This method runs faster than , so prefer it if -- cgit v1.2.3