Skip to content

Commit

Permalink
Add tuple support for vector, rectangle and color structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Marioalexsan authored and eXpl0it3r committed Aug 24, 2024
1 parent 0b19f8c commit 1aa747b
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/SFML.Graphics/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ public Color(Color color) : this(color.R, color.G, color.B, color.A) { }
////////////////////////////////////////////////////////////
public override string ToString() => $"[Color] R({R}) G({G}) B({B}) A({A})";

/// <summary>
/// Deconstructs a Color into a tuple of bytes
/// </summary>
/// <param name="red">Red component</param>
/// <param name="green">Green component</param>
/// <param name="blue">Blue component</param>
/// <param name="alpha">Alpha (transparency) component</param>
public void Deconstruct(out byte red, out byte green, out byte blue, out byte alpha)
{
red = R;
green = G;
blue = B;
alpha = A;
}

////////////////////////////////////////////////////////////
/// <summary>
/// Compare color and object and checks if they are equal
Expand Down Expand Up @@ -166,6 +181,12 @@ public Color(Color color) : this(color.R, color.G, color.B, color.A) { }
(byte)( left.A * right.A / 255 ));
}

/// <summary>
/// Converts a tuple of bytes to a Color
/// </summary>
/// <param name="tuple">The tuple to convert</param>
public static implicit operator Color((byte R, byte G, byte B, byte A) tuple) => new Color(tuple.R, tuple.G, tuple.B, tuple.A);

/// <summary>Red component of the color</summary>
public byte R;

Expand Down
42 changes: 42 additions & 0 deletions src/SFML.Graphics/Rect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,21 @@ public bool Intersects(IntRect rect, out IntRect overlap)
////////////////////////////////////////////////////////////
public Vector2i Size => new Vector2i(Width, Height);

/// <summary>
/// Deconstructs an IntRect into a tuple of ints
/// </summary>
/// <param name="left">Left coordinate of the rectangle</param>
/// <param name="top">Top coordinate of the rectangle</param>
/// <param name="width">Width of the rectangle</param>
/// <param name="height">Height of the rectangle</param>
public void Deconstruct(out int left, out int top, out int width, out int height)
{
left = Left;
top = Top;
width = Width;
height = Height;
}

////////////////////////////////////////////////////////////
/// <summary>
/// Provide a string describing the object
Expand Down Expand Up @@ -246,6 +261,12 @@ public override int GetHashCode()
////////////////////////////////////////////////////////////
public static bool operator !=(IntRect r1, IntRect r2) => !r1.Equals(r2);

/// <summary>
/// Converts a tuple of ints to an IntRect
/// </summary>
/// <param name="tuple">The tuple to convert</param>
public static implicit operator IntRect((int Left, int Top, int Width, int Height) tuple) => new IntRect(tuple.Left, tuple.Top, tuple.Width, tuple.Height);

////////////////////////////////////////////////////////////
/// <summary>
/// Explicit casting to another rectangle type
Expand Down Expand Up @@ -445,6 +466,21 @@ public bool Intersects(FloatRect rect, out FloatRect overlap)
////////////////////////////////////////////////////////////
public Vector2f Size => new Vector2f(Width, Height);

/// <summary>
/// Deconstructs a FloatRect into a tuple of floats
/// </summary>
/// <param name="left">Left coordinate of the rectangle</param>
/// <param name="top">Top coordinate of the rectangle</param>
/// <param name="width">Width of the rectangle</param>
/// <param name="height">Height of the rectangle</param>
public void Deconstruct(out float left, out float top, out float width, out float height)
{
left = Left;
top = Top;
width = Width;
height = Height;
}

////////////////////////////////////////////////////////////
/// <summary>
/// Provide a string describing the object
Expand Down Expand Up @@ -527,6 +563,12 @@ public override int GetHashCode()
return !r1.Equals(r2);
}

/// <summary>
/// Converts a tuple of floats to a FloatRect
/// </summary>
/// <param name="tuple">The tuple to convert</param>
public static implicit operator FloatRect((float Left, float Top, float Width, float Height) tuple) => new FloatRect(tuple.Left, tuple.Top, tuple.Width, tuple.Height);

////////////////////////////////////////////////////////////
/// <summary>
/// Explicit casting to another rectangle type
Expand Down
51 changes: 51 additions & 0 deletions src/SFML.System/Vector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ public Vector2f(float x, float y)
Y = y;
}

/// <summary>
/// Deconstructs a Vector2f into a tuple of floats
/// </summary>
/// <param name="x">X coordinate</param>
/// <param name="y">Y coordinate</param>
public void Deconstruct(out float x, out float y)
{
x = X;
y = Y;
}

////////////////////////////////////////////////////////////
/// <summary>
/// Operator - overload ; returns the opposite of a vector
Expand Down Expand Up @@ -156,6 +167,12 @@ public Vector2f(float x, float y)
////////////////////////////////////////////////////////////
public static explicit operator Vector2u(Vector2f v) => new Vector2u((uint)v.X, (uint)v.Y);

/// <summary>
/// Converts a tuple of floats to a Vector2f
/// </summary>
/// <param name="tuple">The tuple to convert</param>
public static implicit operator Vector2f((float X, float Y) tuple) => new Vector2f(tuple.X, tuple.Y);

/// <summary>X (horizontal) component of the vector</summary>
public float X;

Expand Down Expand Up @@ -185,6 +202,17 @@ public Vector2i(int x, int y)
Y = y;
}

/// <summary>
/// Deconstructs a Vector2i into a tuple of ints
/// </summary>
/// <param name="x">X coordinate</param>
/// <param name="y">Y coordinate</param>
public void Deconstruct(out int x, out int y)
{
x = X;
y = Y;
}

////////////////////////////////////////////////////////////
/// <summary>
/// Operator - overload ; returns the opposite of a vector
Expand Down Expand Up @@ -316,6 +344,12 @@ public Vector2i(int x, int y)
////////////////////////////////////////////////////////////
public static explicit operator Vector2u(Vector2i v) => new Vector2u((uint)v.X, (uint)v.Y);

/// <summary>
/// Converts a tuple of ints to a Vector2i
/// </summary>
/// <param name="tuple">The tuple to convert</param>
public static implicit operator Vector2i((int X, int Y) tuple) => new Vector2i(tuple.X, tuple.Y);

/// <summary>X (horizontal) component of the vector</summary>
public int X;

Expand Down Expand Up @@ -345,6 +379,17 @@ public Vector2u(uint x, uint y)
Y = y;
}

/// <summary>
/// Deconstructs a Vector2u into a tuple of uints
/// </summary>
/// <param name="x">X coordinate</param>
/// <param name="y">Y coordinate</param>
public void Deconstruct(out uint x, out uint y)
{
x = X;
y = Y;
}

////////////////////////////////////////////////////////////
/// <summary>
/// Operator - overload ; subtracts two vectors
Expand Down Expand Up @@ -467,6 +512,12 @@ public Vector2u(uint x, uint y)
////////////////////////////////////////////////////////////
public static explicit operator Vector2f(Vector2u v) => new Vector2f(v.X, v.Y);

/// <summary>
/// Converts a tuple of uints to a Vector2u
/// </summary>
/// <param name="tuple">The tuple to convert</param>
public static implicit operator Vector2u((uint X, uint Y) tuple) => new Vector2u(tuple.X, tuple.Y);

/// <summary>X (horizontal) component of the vector</summary>
public uint X;

Expand Down
19 changes: 19 additions & 0 deletions src/SFML.System/Vector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ public Vector3f(float x, float y, float z)
Z = z;
}

/// <summary>
/// Deconstructs a Vector3f into a tuple of floats
/// </summary>
/// <param name="x">X coordinate</param>
/// <param name="y">Y coordinate</param>
/// <param name="z">Z coordinate</param>
public void Deconstruct(out float x, out float y, out float z)
{
x = X;
y = Y;
z = Z;
}

////////////////////////////////////////////////////////////
/// <summary>
/// Operator - overload ; returns the opposite of a vector
Expand Down Expand Up @@ -140,6 +153,12 @@ public Vector3f(float x, float y, float z)
////////////////////////////////////////////////////////////
public override int GetHashCode() => X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode();

/// <summary>
/// Converts a tuple of floats to a Vector3f
/// </summary>
/// <param name="tuple">The tuple to convert</param>
public static implicit operator Vector3f((float X, float Y, float Z) tuple) => new Vector3f(tuple.X, tuple.Y, tuple.Z);

/// <summary>X (horizontal) component of the vector</summary>
public float X;

Expand Down

0 comments on commit 1aa747b

Please sign in to comment.