summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/math/color.cpp8
-rw-r--r--core/math/color.h1
-rw-r--r--core/math/vector2.cpp12
-rw-r--r--core/math/vector2.h2
-rw-r--r--core/math/vector3.cpp7
-rw-r--r--core/math/vector3.h1
-rw-r--r--core/math/vector3i.cpp7
-rw-r--r--core/math/vector3i.h1
-rw-r--r--core/variant/variant_call.cpp5
-rw-r--r--doc/classes/Color.xml11
-rw-r--r--doc/classes/Vector2.xml11
-rw-r--r--doc/classes/Vector2i.xml11
-rw-r--r--doc/classes/Vector3.xml11
-rw-r--r--doc/classes/Vector3i.xml11
-rw-r--r--modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs21
-rw-r--r--modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs17
-rw-r--r--modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2i.cs17
-rw-r--r--modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs18
-rw-r--r--modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3i.cs18
19 files changed, 190 insertions, 0 deletions
diff --git a/core/math/color.cpp b/core/math/color.cpp
index 64abd6dd08..52f029ef4b 100644
--- a/core/math/color.cpp
+++ b/core/math/color.cpp
@@ -211,6 +211,14 @@ bool Color::is_equal_approx(const Color &p_color) const {
return Math::is_equal_approx(r, p_color.r) && Math::is_equal_approx(g, p_color.g) && Math::is_equal_approx(b, p_color.b) && Math::is_equal_approx(a, p_color.a);
}
+Color Color::clamp(const Color &p_min, const Color &p_max) const {
+ return Color(
+ CLAMP(r, p_min.r, p_max.r),
+ CLAMP(g, p_min.g, p_max.g),
+ CLAMP(b, p_min.b, p_max.b),
+ CLAMP(a, p_min.a, p_max.a));
+}
+
void Color::invert() {
r = 1.0 - r;
g = 1.0 - g;
diff --git a/core/math/color.h b/core/math/color.h
index e404d80c8a..a95dbf4f60 100644
--- a/core/math/color.h
+++ b/core/math/color.h
@@ -89,6 +89,7 @@ struct Color {
bool is_equal_approx(const Color &p_color) const;
+ Color clamp(const Color &p_min = Color(0, 0, 0, 0), const Color &p_max = Color(1, 1, 1, 1)) const;
void invert();
Color inverted() const;
diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp
index ab114673d8..ea430b15c4 100644
--- a/core/math/vector2.cpp
+++ b/core/math/vector2.cpp
@@ -122,6 +122,12 @@ Vector2 Vector2::project(const Vector2 &p_to) const {
return p_to * (dot(p_to) / p_to.length_squared());
}
+Vector2 Vector2::clamp(const Vector2 &p_min, const Vector2 &p_max) const {
+ return Vector2(
+ CLAMP(x, p_min.x, p_max.x),
+ CLAMP(y, p_min.y, p_max.y));
+}
+
Vector2 Vector2::snapped(const Vector2 &p_step) const {
return Vector2(
Math::snapped(x, p_step.x),
@@ -189,6 +195,12 @@ bool Vector2::is_equal_approx(const Vector2 &p_v) const {
/* Vector2i */
+Vector2i Vector2i::clamp(const Vector2i &p_min, const Vector2i &p_max) const {
+ return Vector2i(
+ CLAMP(x, p_min.x, p_max.x),
+ CLAMP(y, p_min.y, p_max.y));
+}
+
Vector2i Vector2i::operator+(const Vector2i &p_v) const {
return Vector2i(x + p_v.x, y + p_v.y);
}
diff --git a/core/math/vector2.h b/core/math/vector2.h
index 2a540bd8aa..b0d2049f55 100644
--- a/core/math/vector2.h
+++ b/core/math/vector2.h
@@ -162,6 +162,7 @@ struct Vector2 {
Vector2 ceil() const;
Vector2 round() const;
Vector2 snapped(const Vector2 &p_by) const;
+ Vector2 clamp(const Vector2 &p_min, const Vector2 &p_max) const;
real_t aspect() const { return width / height; }
operator String() const { return String::num(x) + ", " + String::num(y); }
@@ -337,6 +338,7 @@ struct Vector2i {
real_t aspect() const { return width / (real_t)height; }
Vector2i sign() const { return Vector2i(SGN(x), SGN(y)); }
Vector2i abs() const { return Vector2i(ABS(x), ABS(y)); }
+ Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const;
operator String() const { return String::num(x) + ", " + String::num(y); }
diff --git a/core/math/vector3.cpp b/core/math/vector3.cpp
index cc71b1dacf..d5ca985244 100644
--- a/core/math/vector3.cpp
+++ b/core/math/vector3.cpp
@@ -52,6 +52,13 @@ real_t Vector3::get_axis(int p_axis) const {
return operator[](p_axis);
}
+Vector3 Vector3::clamp(const Vector3 &p_min, const Vector3 &p_max) const {
+ return Vector3(
+ CLAMP(x, p_min.x, p_max.x),
+ CLAMP(y, p_min.y, p_max.y),
+ CLAMP(z, p_min.z, p_max.z));
+}
+
void Vector3::snap(Vector3 p_step) {
x = Math::snapped(x, p_step.x);
y = Math::snapped(y, p_step.y);
diff --git a/core/math/vector3.h b/core/math/vector3.h
index 8bab8fd847..d8d3cd3cc0 100644
--- a/core/math/vector3.h
+++ b/core/math/vector3.h
@@ -113,6 +113,7 @@ struct Vector3 {
_FORCE_INLINE_ Vector3 sign() const;
_FORCE_INLINE_ Vector3 ceil() const;
_FORCE_INLINE_ Vector3 round() const;
+ Vector3 clamp(const Vector3 &p_min, const Vector3 &p_max) const;
_FORCE_INLINE_ real_t distance_to(const Vector3 &p_to) const;
_FORCE_INLINE_ real_t distance_squared_to(const Vector3 &p_to) const;
diff --git a/core/math/vector3i.cpp b/core/math/vector3i.cpp
index 167fa3221d..a82db7f7fc 100644
--- a/core/math/vector3i.cpp
+++ b/core/math/vector3i.cpp
@@ -48,6 +48,13 @@ int Vector3i::max_axis() const {
return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0);
}
+Vector3i Vector3i::clamp(const Vector3i &p_min, const Vector3i &p_max) const {
+ return Vector3i(
+ CLAMP(x, p_min.x, p_max.x),
+ CLAMP(y, p_min.y, p_max.y),
+ CLAMP(z, p_min.z, p_max.z));
+}
+
Vector3i::operator String() const {
return (itos(x) + ", " + itos(y) + ", " + itos(z));
}
diff --git a/core/math/vector3i.h b/core/math/vector3i.h
index b0411fb62e..37c7c1c368 100644
--- a/core/math/vector3i.h
+++ b/core/math/vector3i.h
@@ -69,6 +69,7 @@ struct Vector3i {
_FORCE_INLINE_ Vector3i abs() const;
_FORCE_INLINE_ Vector3i sign() const;
+ Vector3i clamp(const Vector3i &p_min, const Vector3i &p_max) const;
/* Operators */
diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp
index 90356435d9..d9cc97154d 100644
--- a/core/variant/variant_call.cpp
+++ b/core/variant/variant_call.cpp
@@ -1443,6 +1443,7 @@ static void _register_variant_builtin_methods() {
bind_method(Vector2, cross, sarray("with"), varray());
bind_method(Vector2, abs, sarray(), varray());
bind_method(Vector2, sign, sarray(), varray());
+ bind_method(Vector2, clamp, sarray("min", "max"), varray());
bind_method(Vector2, snapped, sarray("step"), varray());
/* Vector2i */
@@ -1450,6 +1451,7 @@ static void _register_variant_builtin_methods() {
bind_method(Vector2i, aspect, sarray(), varray());
bind_method(Vector2i, sign, sarray(), varray());
bind_method(Vector2i, abs, sarray(), varray());
+ bind_method(Vector2i, clamp, sarray("min", "max"), varray());
/* Rect2 */
@@ -1498,6 +1500,7 @@ static void _register_variant_builtin_methods() {
bind_method(Vector3, is_normalized, sarray(), varray());
bind_method(Vector3, is_equal_approx, sarray("to"), varray());
bind_method(Vector3, inverse, sarray(), varray());
+ bind_method(Vector3, clamp, sarray("min", "max"), varray());
bind_method(Vector3, snapped, sarray("step"), varray());
bind_method(Vector3, rotated, sarray("by_axis", "phi"), varray());
bind_method(Vector3, lerp, sarray("to", "weight"), varray());
@@ -1526,6 +1529,7 @@ static void _register_variant_builtin_methods() {
bind_method(Vector3i, max_axis, sarray(), varray());
bind_method(Vector3i, sign, sarray(), varray());
bind_method(Vector3i, abs, sarray(), varray());
+ bind_method(Vector3i, clamp, sarray("min", "max"), varray());
/* Plane */
@@ -1563,6 +1567,7 @@ static void _register_variant_builtin_methods() {
bind_method(Color, to_abgr64, sarray(), varray());
bind_method(Color, to_rgba64, sarray(), varray());
+ bind_method(Color, clamp, sarray("min", "max"), varray(Color(0, 0, 0, 0), Color(1, 1, 1, 1)));
bind_method(Color, inverted, sarray(), varray());
bind_method(Color, lerp, sarray("to", "weight"), varray());
bind_method(Color, lightened, sarray("amount"), varray());
diff --git a/doc/classes/Color.xml b/doc/classes/Color.xml
index 6133bb8d8c..ddff92e6fc 100644
--- a/doc/classes/Color.xml
+++ b/doc/classes/Color.xml
@@ -136,6 +136,17 @@
[/codeblocks]
</description>
</method>
+ <method name="clamp" qualifiers="const">
+ <return type="Color">
+ </return>
+ <argument index="0" name="min" type="Color" default="Color( 0, 0, 0, 0 )">
+ </argument>
+ <argument index="1" name="max" type="Color" default="Color( 1, 1, 1, 1 )">
+ </argument>
+ <description>
+ Returns a new color with all components clamped between the components of [code]min[/code] and [code]max[/code], by running [method @GlobalScope.clamp] on each component.
+ </description>
+ </method>
<method name="darkened" qualifiers="const">
<return type="Color">
</return>
diff --git a/doc/classes/Vector2.xml b/doc/classes/Vector2.xml
index 9e47ebfaea..1390a5e45b 100644
--- a/doc/classes/Vector2.xml
+++ b/doc/classes/Vector2.xml
@@ -110,6 +110,17 @@
Returns the vector with all components rounded up (towards positive infinity).
</description>
</method>
+ <method name="clamp" qualifiers="const">
+ <return type="Vector2">
+ </return>
+ <argument index="0" name="min" type="Vector2">
+ </argument>
+ <argument index="1" name="max" type="Vector2">
+ </argument>
+ <description>
+ Returns a new vector with all components clamped between the components of [code]min[/code] and [code]max[/code], by running [method @GlobalScope.clamp] on each component.
+ </description>
+ </method>
<method name="cross" qualifiers="const">
<return type="float">
</return>
diff --git a/doc/classes/Vector2i.xml b/doc/classes/Vector2i.xml
index b38b968ba3..6efb52b712 100644
--- a/doc/classes/Vector2i.xml
+++ b/doc/classes/Vector2i.xml
@@ -64,6 +64,17 @@
Returns the ratio of [member x] to [member y].
</description>
</method>
+ <method name="clamp" qualifiers="const">
+ <return type="Vector2i">
+ </return>
+ <argument index="0" name="min" type="Vector2i">
+ </argument>
+ <argument index="1" name="max" type="Vector2i">
+ </argument>
+ <description>
+ Returns a new vector with all components clamped between the components of [code]min[/code] and [code]max[/code], by running [method @GlobalScope.clamp] on each component.
+ </description>
+ </method>
<method name="operator !=" qualifiers="operator">
<return type="bool">
</return>
diff --git a/doc/classes/Vector3.xml b/doc/classes/Vector3.xml
index 94ecce5e31..170bd78067 100644
--- a/doc/classes/Vector3.xml
+++ b/doc/classes/Vector3.xml
@@ -87,6 +87,17 @@
Returns a new vector with all components rounded up (towards positive infinity).
</description>
</method>
+ <method name="clamp" qualifiers="const">
+ <return type="Vector3">
+ </return>
+ <argument index="0" name="min" type="Vector3">
+ </argument>
+ <argument index="1" name="max" type="Vector3">
+ </argument>
+ <description>
+ Returns a new vector with all components clamped between the components of [code]min[/code] and [code]max[/code], by running [method @GlobalScope.clamp] on each component.
+ </description>
+ </method>
<method name="cross" qualifiers="const">
<return type="Vector3">
</return>
diff --git a/doc/classes/Vector3i.xml b/doc/classes/Vector3i.xml
index ea5945f5b7..6e8a34b692 100644
--- a/doc/classes/Vector3i.xml
+++ b/doc/classes/Vector3i.xml
@@ -58,6 +58,17 @@
<description>
</description>
</method>
+ <method name="clamp" qualifiers="const">
+ <return type="Vector3i">
+ </return>
+ <argument index="0" name="min" type="Vector3i">
+ </argument>
+ <argument index="1" name="max" type="Vector3i">
+ </argument>
+ <description>
+ Returns a new vector with all components clamped between the components of [code]min[/code] and [code]max[/code], by running [method @GlobalScope.clamp] on each component.
+ </description>
+ </method>
<method name="max_axis" qualifiers="const">
<return type="int">
</return>
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
@@ -257,6 +257,27 @@ namespace Godot
}
/// <summary>
+ /// Returns a new color with all components clamped between the
+ /// components of `min` and `max` using
+ /// <see cref="Mathf.Clamp(float, float, float)"/>.
+ /// </summary>
+ /// <param name="min">The color with minimum allowed values.</param>
+ /// <param name="max">The color with maximum allowed values.</param>
+ /// <returns>The color with all components clamped.</returns>
+ 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)
+ );
+ }
+
+ /// <summary>
/// Returns a new color resulting from making this color darker
/// by the specified ratio (on the range of 0 to 1).
/// </summary>
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
@@ -160,6 +160,23 @@ namespace Godot
}
/// <summary>
+ /// Returns a new vector with all components clamped between the
+ /// components of `min` and `max` using
+ /// <see cref="Mathf.Clamp(real_t, real_t, real_t)"/>.
+ /// </summary>
+ /// <param name="min">The vector with minimum allowed values.</param>
+ /// <param name="max">The vector with maximum allowed values.</param>
+ /// <returns>The vector with all components clamped.</returns>
+ public Vector2 Clamp(Vector2 min, Vector2 max)
+ {
+ return new Vector2
+ (
+ Mathf.Clamp(x, min.x, max.x),
+ Mathf.Clamp(y, min.y, max.y)
+ );
+ }
+
+ /// <summary>
/// Returns the cross product of this vector and `b`.
/// </summary>
/// <param name="b">The other vector.</param>
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
@@ -120,6 +120,23 @@ namespace Godot
}
/// <summary>
+ /// Returns a new vector with all components clamped between the
+ /// components of `min` and `max` using
+ /// <see cref="Mathf.Clamp(int, int, int)"/>.
+ /// </summary>
+ /// <param name="min">The vector with minimum allowed values.</param>
+ /// <param name="max">The vector with maximum allowed values.</param>
+ /// <returns>The vector with all components clamped.</returns>
+ public Vector2i Clamp(Vector2i min, Vector2i max)
+ {
+ return new Vector2i
+ (
+ Mathf.Clamp(x, min.x, max.x),
+ Mathf.Clamp(y, min.y, max.y)
+ );
+ }
+
+ /// <summary>
/// Returns the cross product of this vector and `b`.
/// </summary>
/// <param name="b">The other vector.</param>
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
@@ -140,6 +140,24 @@ namespace Godot
}
/// <summary>
+ /// Returns a new vector with all components clamped between the
+ /// components of `min` and `max` using
+ /// <see cref="Mathf.Clamp(real_t, real_t, real_t)"/>.
+ /// </summary>
+ /// <param name="min">The vector with minimum allowed values.</param>
+ /// <param name="max">The vector with maximum allowed values.</param>
+ /// <returns>The vector with all components clamped.</returns>
+ 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)
+ );
+ }
+
+ /// <summary>
/// Returns the cross product of this vector and `b`.
/// </summary>
/// <param name="b">The other vector.</param>
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
@@ -89,6 +89,24 @@ namespace Godot
}
/// <summary>
+ /// Returns a new vector with all components clamped between the
+ /// components of `min` and `max` using
+ /// <see cref="Mathf.Clamp(int, int, int)"/>.
+ /// </summary>
+ /// <param name="min">The vector with minimum allowed values.</param>
+ /// <param name="max">The vector with maximum allowed values.</param>
+ /// <returns>The vector with all components clamped.</returns>
+ 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)
+ );
+ }
+
+ /// <summary>
/// Returns the squared distance between this vector and `b`.
/// This method runs faster than <see cref="DistanceTo"/>, so prefer it if
/// you need to compare vectors or need the squared distance for some formula.