Skip to content

Commit 1aa747b

Browse files
MarioalexsaneXpl0it3r
authored andcommitted
Add tuple support for vector, rectangle and color structs
1 parent 0b19f8c commit 1aa747b

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

src/SFML.Graphics/Color.cs

+21
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ public Color(Color color) : this(color.R, color.G, color.B, color.A) { }
7979
////////////////////////////////////////////////////////////
8080
public override string ToString() => $"[Color] R({R}) G({G}) B({B}) A({A})";
8181

82+
/// <summary>
83+
/// Deconstructs a Color into a tuple of bytes
84+
/// </summary>
85+
/// <param name="red">Red component</param>
86+
/// <param name="green">Green component</param>
87+
/// <param name="blue">Blue component</param>
88+
/// <param name="alpha">Alpha (transparency) component</param>
89+
public void Deconstruct(out byte red, out byte green, out byte blue, out byte alpha)
90+
{
91+
red = R;
92+
green = G;
93+
blue = B;
94+
alpha = A;
95+
}
96+
8297
////////////////////////////////////////////////////////////
8398
/// <summary>
8499
/// Compare color and object and checks if they are equal
@@ -166,6 +181,12 @@ public Color(Color color) : this(color.R, color.G, color.B, color.A) { }
166181
(byte)( left.A * right.A / 255 ));
167182
}
168183

184+
/// <summary>
185+
/// Converts a tuple of bytes to a Color
186+
/// </summary>
187+
/// <param name="tuple">The tuple to convert</param>
188+
public static implicit operator Color((byte R, byte G, byte B, byte A) tuple) => new Color(tuple.R, tuple.G, tuple.B, tuple.A);
189+
169190
/// <summary>Red component of the color</summary>
170191
public byte R;
171192

src/SFML.Graphics/Rect.cs

+42
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,21 @@ public bool Intersects(IntRect rect, out IntRect overlap)
177177
////////////////////////////////////////////////////////////
178178
public Vector2i Size => new Vector2i(Width, Height);
179179

180+
/// <summary>
181+
/// Deconstructs an IntRect into a tuple of ints
182+
/// </summary>
183+
/// <param name="left">Left coordinate of the rectangle</param>
184+
/// <param name="top">Top coordinate of the rectangle</param>
185+
/// <param name="width">Width of the rectangle</param>
186+
/// <param name="height">Height of the rectangle</param>
187+
public void Deconstruct(out int left, out int top, out int width, out int height)
188+
{
189+
left = Left;
190+
top = Top;
191+
width = Width;
192+
height = Height;
193+
}
194+
180195
////////////////////////////////////////////////////////////
181196
/// <summary>
182197
/// Provide a string describing the object
@@ -246,6 +261,12 @@ public override int GetHashCode()
246261
////////////////////////////////////////////////////////////
247262
public static bool operator !=(IntRect r1, IntRect r2) => !r1.Equals(r2);
248263

264+
/// <summary>
265+
/// Converts a tuple of ints to an IntRect
266+
/// </summary>
267+
/// <param name="tuple">The tuple to convert</param>
268+
public static implicit operator IntRect((int Left, int Top, int Width, int Height) tuple) => new IntRect(tuple.Left, tuple.Top, tuple.Width, tuple.Height);
269+
249270
////////////////////////////////////////////////////////////
250271
/// <summary>
251272
/// Explicit casting to another rectangle type
@@ -445,6 +466,21 @@ public bool Intersects(FloatRect rect, out FloatRect overlap)
445466
////////////////////////////////////////////////////////////
446467
public Vector2f Size => new Vector2f(Width, Height);
447468

469+
/// <summary>
470+
/// Deconstructs a FloatRect into a tuple of floats
471+
/// </summary>
472+
/// <param name="left">Left coordinate of the rectangle</param>
473+
/// <param name="top">Top coordinate of the rectangle</param>
474+
/// <param name="width">Width of the rectangle</param>
475+
/// <param name="height">Height of the rectangle</param>
476+
public void Deconstruct(out float left, out float top, out float width, out float height)
477+
{
478+
left = Left;
479+
top = Top;
480+
width = Width;
481+
height = Height;
482+
}
483+
448484
////////////////////////////////////////////////////////////
449485
/// <summary>
450486
/// Provide a string describing the object
@@ -527,6 +563,12 @@ public override int GetHashCode()
527563
return !r1.Equals(r2);
528564
}
529565

566+
/// <summary>
567+
/// Converts a tuple of floats to a FloatRect
568+
/// </summary>
569+
/// <param name="tuple">The tuple to convert</param>
570+
public static implicit operator FloatRect((float Left, float Top, float Width, float Height) tuple) => new FloatRect(tuple.Left, tuple.Top, tuple.Width, tuple.Height);
571+
530572
////////////////////////////////////////////////////////////
531573
/// <summary>
532574
/// Explicit casting to another rectangle type

src/SFML.System/Vector2.cs

+51
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ public Vector2f(float x, float y)
2525
Y = y;
2626
}
2727

28+
/// <summary>
29+
/// Deconstructs a Vector2f into a tuple of floats
30+
/// </summary>
31+
/// <param name="x">X coordinate</param>
32+
/// <param name="y">Y coordinate</param>
33+
public void Deconstruct(out float x, out float y)
34+
{
35+
x = X;
36+
y = Y;
37+
}
38+
2839
////////////////////////////////////////////////////////////
2940
/// <summary>
3041
/// Operator - overload ; returns the opposite of a vector
@@ -156,6 +167,12 @@ public Vector2f(float x, float y)
156167
////////////////////////////////////////////////////////////
157168
public static explicit operator Vector2u(Vector2f v) => new Vector2u((uint)v.X, (uint)v.Y);
158169

170+
/// <summary>
171+
/// Converts a tuple of floats to a Vector2f
172+
/// </summary>
173+
/// <param name="tuple">The tuple to convert</param>
174+
public static implicit operator Vector2f((float X, float Y) tuple) => new Vector2f(tuple.X, tuple.Y);
175+
159176
/// <summary>X (horizontal) component of the vector</summary>
160177
public float X;
161178

@@ -185,6 +202,17 @@ public Vector2i(int x, int y)
185202
Y = y;
186203
}
187204

205+
/// <summary>
206+
/// Deconstructs a Vector2i into a tuple of ints
207+
/// </summary>
208+
/// <param name="x">X coordinate</param>
209+
/// <param name="y">Y coordinate</param>
210+
public void Deconstruct(out int x, out int y)
211+
{
212+
x = X;
213+
y = Y;
214+
}
215+
188216
////////////////////////////////////////////////////////////
189217
/// <summary>
190218
/// Operator - overload ; returns the opposite of a vector
@@ -316,6 +344,12 @@ public Vector2i(int x, int y)
316344
////////////////////////////////////////////////////////////
317345
public static explicit operator Vector2u(Vector2i v) => new Vector2u((uint)v.X, (uint)v.Y);
318346

347+
/// <summary>
348+
/// Converts a tuple of ints to a Vector2i
349+
/// </summary>
350+
/// <param name="tuple">The tuple to convert</param>
351+
public static implicit operator Vector2i((int X, int Y) tuple) => new Vector2i(tuple.X, tuple.Y);
352+
319353
/// <summary>X (horizontal) component of the vector</summary>
320354
public int X;
321355

@@ -345,6 +379,17 @@ public Vector2u(uint x, uint y)
345379
Y = y;
346380
}
347381

382+
/// <summary>
383+
/// Deconstructs a Vector2u into a tuple of uints
384+
/// </summary>
385+
/// <param name="x">X coordinate</param>
386+
/// <param name="y">Y coordinate</param>
387+
public void Deconstruct(out uint x, out uint y)
388+
{
389+
x = X;
390+
y = Y;
391+
}
392+
348393
////////////////////////////////////////////////////////////
349394
/// <summary>
350395
/// Operator - overload ; subtracts two vectors
@@ -467,6 +512,12 @@ public Vector2u(uint x, uint y)
467512
////////////////////////////////////////////////////////////
468513
public static explicit operator Vector2f(Vector2u v) => new Vector2f(v.X, v.Y);
469514

515+
/// <summary>
516+
/// Converts a tuple of uints to a Vector2u
517+
/// </summary>
518+
/// <param name="tuple">The tuple to convert</param>
519+
public static implicit operator Vector2u((uint X, uint Y) tuple) => new Vector2u(tuple.X, tuple.Y);
520+
470521
/// <summary>X (horizontal) component of the vector</summary>
471522
public uint X;
472523

src/SFML.System/Vector3.cs

+19
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ public Vector3f(float x, float y, float z)
2727
Z = z;
2828
}
2929

30+
/// <summary>
31+
/// Deconstructs a Vector3f into a tuple of floats
32+
/// </summary>
33+
/// <param name="x">X coordinate</param>
34+
/// <param name="y">Y coordinate</param>
35+
/// <param name="z">Z coordinate</param>
36+
public void Deconstruct(out float x, out float y, out float z)
37+
{
38+
x = X;
39+
y = Y;
40+
z = Z;
41+
}
42+
3043
////////////////////////////////////////////////////////////
3144
/// <summary>
3245
/// Operator - overload ; returns the opposite of a vector
@@ -140,6 +153,12 @@ public Vector3f(float x, float y, float z)
140153
////////////////////////////////////////////////////////////
141154
public override int GetHashCode() => X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode();
142155

156+
/// <summary>
157+
/// Converts a tuple of floats to a Vector3f
158+
/// </summary>
159+
/// <param name="tuple">The tuple to convert</param>
160+
public static implicit operator Vector3f((float X, float Y, float Z) tuple) => new Vector3f(tuple.X, tuple.Y, tuple.Z);
161+
143162
/// <summary>X (horizontal) component of the vector</summary>
144163
public float X;
145164

0 commit comments

Comments
 (0)