From 9932fbfb1ea8d62b3480bb5e301bfaf31e69f78c Mon Sep 17 00:00:00 2001 From: Magian Date: Sat, 18 Sep 2021 20:36:15 +0800 Subject: C#, replace the current Xform method with a * operator. --- .../glue/GodotSharp/GodotSharp/Core/Transform2D.cs | 94 +++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) (limited to 'modules/mono/glue') diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform2D.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform2D.cs index 62a6fe6959..ae6ff377ab 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform2D.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform2D.cs @@ -355,6 +355,7 @@ namespace Godot /// /// A vector to transform. /// The transformed vector. + [Obsolete("Xform is deprecated. Use the multiplication operator (Transform2D * Vector2) instead.")] public Vector2 Xform(Vector2 v) { return new Vector2(Tdotx(v), Tdoty(v)) + origin; @@ -365,6 +366,7 @@ namespace Godot /// /// A vector to inversely transform. /// The inversely transformed vector. + [Obsolete("XformInv is deprecated. Use the multiplication operator (Vector2 * Transform2D) instead.")] public Vector2 XformInv(Vector2 v) { Vector2 vInv = v - origin; @@ -439,7 +441,7 @@ namespace Godot public static Transform2D operator *(Transform2D left, Transform2D right) { - left.origin = left.Xform(right.origin); + left.origin = left * right.origin; real_t x0 = left.Tdotx(right.x); real_t x1 = left.Tdoty(right.x); @@ -454,6 +456,96 @@ namespace Godot return left; } + /// + /// Returns a Vector2 transformed (multiplied) by transformation matrix. + /// + /// The transformation to apply. + /// A Vector2 to transform. + /// The transformed Vector2. + public static Vector2 operator *(Transform2D transform, Vector2 vector) + { + return new Vector2(transform.Tdotx(vector), transform.Tdoty(vector)) + transform.origin; + } + + /// + /// Returns a Vector2 transformed (multiplied) by the inverse transformation matrix. + /// + /// A vector to inversely transform. + /// The transformation to apply. + /// The inversely transformed Vector2. + public static Vector2 operator *(Vector2 vector, Transform2D transform) + { + Vector2 vInv = vector - transform.origin; + return new Vector2(transform.x.Dot(vInv), transform.y.Dot(vInv)); + } + + /// + /// Returns a Rect2 transformed (multiplied) by transformation matrix. + /// + /// The transformation to apply. + /// A Rect2 to transform. + /// The transformed Rect2. + public static Rect2 operator *(Transform2D transform, Rect2 rect) + { + Vector2 pos = transform * rect.Position; + Vector2 toX = transform.x * rect.Size.x; + Vector2 toY = transform.y * rect.Size.y; + + return new Rect2(pos, rect.Size).Expand(pos + toX).Expand(pos + toY).Expand(pos + toX + toY); + } + + /// + /// Returns a Rect2 transformed (multiplied) by the inverse transformation matrix. + /// + /// A Rect2 to inversely transform. + /// The transformation to apply. + /// The inversely transformed Rect2. + public static Rect2 operator *(Rect2 rect, Transform2D transform) + { + Vector2 pos = rect.Position * transform; + Vector2 to1 = new Vector2(rect.Position.x, rect.Position.y + rect.Size.y) * transform; + Vector2 to2 = new Vector2(rect.Position.x + rect.Size.x, rect.Position.y + rect.Size.y) * transform; + Vector2 to3 = new Vector2(rect.Position.x + rect.Size.x, rect.Position.y) * transform; + + return new Rect2(pos, rect.Size).Expand(to1).Expand(to2).Expand(to3); + } + + /// + /// Returns a copy of the given Vector2[] transformed (multiplied) by transformation matrix. + /// + /// The transformation to apply. + /// a Vector2[] to transform. + /// The transformed copy of the Vector2[]. + public static Vector2[] operator *(Transform2D transform, Vector2[] array) + { + Vector2[] newArray = new Vector2[array.Length]; + + for (int i = 0; i < array.Length; i++) + { + newArray[i] = transform * array[i]; + } + + return newArray; + } + + /// + /// Returns a copy of the given Vector2[] transformed (multiplied) by the inverse transformation matrix. + /// + /// A Vector2[] to inversely transform. + /// The transformation to apply. + /// The inversely transformed copy of the Vector2[]. + public static Vector2[] operator *(Vector2[] array, Transform2D transform) + { + Vector2[] newArray = new Vector2[array.Length]; + + for (int i = 0; i < array.Length; i++) + { + newArray[i] = array[i] * transform; + } + + return newArray; + } + public static bool operator ==(Transform2D left, Transform2D right) { return left.Equals(right); -- cgit v1.2.3