From 0a39c7b3546e85eb3221c2ec892d2302bf14c51c Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Fri, 29 Nov 2019 02:20:31 -0500 Subject: [PATCH] [Mono] Basis/Transforms Array operator comments and improvements The behavior for Basis and Transform2D is unchanged, and Transform gets new behavior. All of the behavior is identical to GDScript's behavior. --- .../glue/GodotSharp/GodotSharp/Core/Basis.cs | 58 +++++---------- .../glue/GodotSharp/GodotSharp/Core/Mathf.cs | 6 +- .../GodotSharp/Core/StringExtensions.cs | 3 +- .../GodotSharp/GodotSharp/Core/Transform.cs | 70 +++++++++++++++++++ .../GodotSharp/GodotSharp/Core/Transform2D.cs | 46 +++++------- 5 files changed, 109 insertions(+), 74 deletions(-) diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Basis.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Basis.cs index d38589013ec..55408fecb8a 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Basis.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Basis.cs @@ -93,11 +93,15 @@ namespace Godot } } - public Vector3 this[int columnIndex] + /// + /// Access whole columns in the form of Vector3. + /// + /// Which column vector. + public Vector3 this[int column] { get { - switch (columnIndex) + switch (column) { case 0: return Column0; @@ -111,7 +115,7 @@ namespace Godot } set { - switch (columnIndex) + switch (column) { case 0: Column0 = value; @@ -128,50 +132,22 @@ namespace Godot } } - public real_t this[int columnIndex, int rowIndex] + /// + /// Access matrix elements in column-major order. + /// + /// Which column, the matrix horizontal position. + /// Which row, the matrix vertical position. + public real_t this[int column, int row] { get { - switch (columnIndex) - { - case 0: - return Column0[rowIndex]; - case 1: - return Column1[rowIndex]; - case 2: - return Column2[rowIndex]; - default: - throw new IndexOutOfRangeException(); - } + return this[column][row]; } set { - switch (columnIndex) - { - case 0: - { - var column0 = Column0; - column0[rowIndex] = value; - Column0 = column0; - return; - } - case 1: - { - var column1 = Column1; - column1[rowIndex] = value; - Column1 = column1; - return; - } - case 2: - { - var column2 = Column2; - column2[rowIndex] = value; - Column2 = column2; - return; - } - default: - throw new IndexOutOfRangeException(); - } + Vector3 columnVector = this[column]; + columnVector[row] = value; + this[column] = columnVector; } } diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Mathf.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Mathf.cs index ddfed180b50..4f7aa99df86 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Mathf.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Mathf.cs @@ -130,7 +130,7 @@ namespace Godot public static real_t InverseLerp(real_t from, real_t to, real_t weight) { - return (weight - from) / (to - from); + return (weight - from) / (to - from); } public static bool IsEqualApprox(real_t a, real_t b) @@ -151,12 +151,12 @@ namespace Godot public static bool IsInf(real_t s) { - return real_t.IsInfinity(s); + return real_t.IsInfinity(s); } public static bool IsNaN(real_t s) { - return real_t.IsNaN(s); + return real_t.IsNaN(s); } public static bool IsZeroApprox(real_t s) diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs index e096d37a6fe..b85a00d869c 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs @@ -264,7 +264,8 @@ namespace Godot instanceIndex++; toIndex++; } - } else + } + else { while (true) { diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform.cs index 0b84050f072..aa8815d1aa4 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform.cs @@ -15,6 +15,76 @@ namespace Godot public Basis basis; public Vector3 origin; + /// + /// Access whole columns in the form of Vector3. The fourth column is the origin vector. + /// + /// Which column vector. + public Vector3 this[int column] + { + get + { + switch (column) + { + case 0: + return basis.Column0; + case 1: + return basis.Column1; + case 2: + return basis.Column2; + case 3: + return origin; + default: + throw new IndexOutOfRangeException(); + } + } + set + { + switch (column) + { + case 0: + basis.Column0 = value; + return; + case 1: + basis.Column1 = value; + return; + case 2: + basis.Column2 = value; + return; + case 3: + origin = value; + return; + default: + throw new IndexOutOfRangeException(); + } + } + } + + /// + /// Access matrix elements in column-major order. The fourth column is the origin vector. + /// + /// Which column, the matrix horizontal position. + /// Which row, the matrix vertical position. + public real_t this[int column, int row] + { + get + { + if (column == 3) + { + return origin[row]; + } + return basis[column, row]; + } + set + { + if (column == 3) + { + origin[row] = value; + return; + } + basis[column, row] = value; + } + } + public Transform AffineInverse() { Basis basisInv = basis.Inverse(); diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform2D.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform2D.cs index 77ea3e58302..e72a44809a7 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform2D.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform2D.cs @@ -54,11 +54,15 @@ namespace Godot } } - public Vector2 this[int rowIndex] + /// + /// Access whole columns in the form of Vector2. The third column is the origin vector. + /// + /// Which column vector. + public Vector2 this[int column] { get { - switch (rowIndex) + switch (column) { case 0: return x; @@ -72,7 +76,7 @@ namespace Godot } set { - switch (rowIndex) + switch (column) { case 0: x = value; @@ -89,38 +93,22 @@ namespace Godot } } - public real_t this[int rowIndex, int columnIndex] + /// + /// Access matrix elements in column-major order. The third column is the origin vector. + /// + /// Which column, the matrix horizontal position. + /// Which row, the matrix vertical position. + public real_t this[int column, int row] { get { - switch (rowIndex) - { - case 0: - return x[columnIndex]; - case 1: - return y[columnIndex]; - case 2: - return origin[columnIndex]; - default: - throw new IndexOutOfRangeException(); - } + return this[column][row]; } set { - switch (rowIndex) - { - case 0: - x[columnIndex] = value; - return; - case 1: - y[columnIndex] = value; - return; - case 2: - origin[columnIndex] = value; - return; - default: - throw new IndexOutOfRangeException(); - } + Vector2 columnVector = this[column]; + columnVector[row] = value; + this[column] = columnVector; } }