From 74af813d7883c446689b38fe9b0565373e04894a Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 1 Jun 2022 11:06:24 +0100 Subject: [PATCH] Add RectInt conversions --- CHANGELOG.md | 3 ++ .../Assets/Tests/Drawing/RectIntTests.cs | 43 +++++++++++++++++++ .../Assets/Tests/Drawing/RectIntTests.cs.meta | 3 ++ .../Assets/Tests/Drawing/RectangleFTests.cs | 2 +- .../Assets/Tests/Drawing/RectangleTests.cs | 17 +++++++- X10D.Unity/src/Drawing/RectIntExtensions.cs | 36 ++++++++++++++++ X10D.Unity/src/Drawing/RectangleExtensions.cs | 12 ++++++ 7 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 X10D.Unity.Tests/Assets/Tests/Drawing/RectIntTests.cs create mode 100644 X10D.Unity.Tests/Assets/Tests/Drawing/RectIntTests.cs.meta create mode 100644 X10D.Unity/src/Drawing/RectIntExtensions.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cb10e2..0de03ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,7 +24,10 @@ - X10D.Unity: Added `Point.ToUnityVector2Int()` - X10D.Unity: Added `PointF.ToUnityVector2()` - X10D.Unity: Added `Rect.ToSystemRectangleF()` +- X10D.Unity: Added `RectInt.ToSystemRectangle()` +- X10D.Unity: Added `RectInt.ToSystemRectangleF()` - X10D.Unity: Added `Rectangle.ToUnityRect()` +- X10D.Unity: Added `Rectangle.ToUnityRectInt()` - X10D.Unity: Added `RectangleF.ToUnityRect()` - X10D.Unity: Added `Size.ToUnityVector2()` - X10D.Unity: Added `Size.ToUnityVector2Int()` diff --git a/X10D.Unity.Tests/Assets/Tests/Drawing/RectIntTests.cs b/X10D.Unity.Tests/Assets/Tests/Drawing/RectIntTests.cs new file mode 100644 index 0000000..e58b3d2 --- /dev/null +++ b/X10D.Unity.Tests/Assets/Tests/Drawing/RectIntTests.cs @@ -0,0 +1,43 @@ +using System.Collections; +using NUnit.Framework; +using UnityEngine; +using UnityEngine.TestTools; +using X10D.Core; +using X10D.Unity.Drawing; +using Random = System.Random; + +namespace X10D.Unity.Tests.Drawing +{ + public class RectIntTests + { + [UnityTest] + public IEnumerator ToSystemRectangle_ShouldReturnRectangleF_WithEquivalentMembers() + { + var random = new Random(); + var rect = new RectInt(random.Next(), random.Next(), random.Next(), random.Next()); + var rectangle = rect.ToSystemRectangle(); + + Assert.AreEqual(rect.x, rectangle.X); + Assert.AreEqual(rect.y, rectangle.Y); + Assert.AreEqual(rect.width, rectangle.Width); + Assert.AreEqual(rect.height, rectangle.Height); + + yield break; + } + + [UnityTest] + public IEnumerator ToSystemRectangleF_ShouldReturnRectangleF_WithEquivalentMembers() + { + var random = new Random(); + var rect = new RectInt(random.Next(), random.Next(), random.Next(), random.Next()); + var rectangle = rect.ToSystemRectangleF(); + + Assert.AreEqual(rect.x, rectangle.X); + Assert.AreEqual(rect.y, rectangle.Y); + Assert.AreEqual(rect.width, rectangle.Width); + Assert.AreEqual(rect.height, rectangle.Height); + + yield break; + } + } +} diff --git a/X10D.Unity.Tests/Assets/Tests/Drawing/RectIntTests.cs.meta b/X10D.Unity.Tests/Assets/Tests/Drawing/RectIntTests.cs.meta new file mode 100644 index 0000000..462b6b5 --- /dev/null +++ b/X10D.Unity.Tests/Assets/Tests/Drawing/RectIntTests.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 18f2e8fbc200475ca5fe7857a457a874 +timeCreated: 1654077768 \ No newline at end of file diff --git a/X10D.Unity.Tests/Assets/Tests/Drawing/RectangleFTests.cs b/X10D.Unity.Tests/Assets/Tests/Drawing/RectangleFTests.cs index b49312c..32676c4 100644 --- a/X10D.Unity.Tests/Assets/Tests/Drawing/RectangleFTests.cs +++ b/X10D.Unity.Tests/Assets/Tests/Drawing/RectangleFTests.cs @@ -16,7 +16,7 @@ namespace X10D.Unity.Tests.Drawing var random = new Random(); var rectangle = new RectangleF(random.NextSingle(), random.NextSingle(), random.NextSingle(), random.NextSingle()); var rect = rectangle.ToUnityRect(); - + Assert.AreEqual(rectangle.X, rect.x, 1e-6f); Assert.AreEqual(rectangle.Y, rect.y, 1e-6f); Assert.AreEqual(rectangle.Width, rect.width, 1e-6f); diff --git a/X10D.Unity.Tests/Assets/Tests/Drawing/RectangleTests.cs b/X10D.Unity.Tests/Assets/Tests/Drawing/RectangleTests.cs index 4a7eb2d..632e5d6 100644 --- a/X10D.Unity.Tests/Assets/Tests/Drawing/RectangleTests.cs +++ b/X10D.Unity.Tests/Assets/Tests/Drawing/RectangleTests.cs @@ -15,7 +15,22 @@ namespace X10D.Unity.Tests.Drawing var random = new Random(); var rectangle = new Rectangle(random.Next(), random.Next(), random.Next(), random.Next()); var rect = rectangle.ToUnityRect(); - + + Assert.AreEqual(rectangle.X, rect.x); + Assert.AreEqual(rectangle.Y, rect.y); + Assert.AreEqual(rectangle.Width, rect.width); + Assert.AreEqual(rectangle.Height, rect.height); + + yield break; + } + + [UnityTest] + public IEnumerator ToUnityRectInt_ShouldReturnRect_WithEquivalentMembers() + { + var random = new Random(); + var rectangle = new Rectangle(random.Next(), random.Next(), random.Next(), random.Next()); + var rect = rectangle.ToUnityRectInt(); + Assert.AreEqual(rectangle.X, rect.x); Assert.AreEqual(rectangle.Y, rect.y); Assert.AreEqual(rectangle.Width, rect.width); diff --git a/X10D.Unity/src/Drawing/RectIntExtensions.cs b/X10D.Unity/src/Drawing/RectIntExtensions.cs new file mode 100644 index 0000000..51b6fc4 --- /dev/null +++ b/X10D.Unity/src/Drawing/RectIntExtensions.cs @@ -0,0 +1,36 @@ +using System.Diagnostics.Contracts; +using System.Drawing; +using System.Runtime.CompilerServices; +using UnityEngine; + +namespace X10D.Unity.Drawing; + +/// +/// Drawing-related extension methods for . +/// +public static class RectIntExtensions +{ + /// + /// Converts the current to a . + /// + /// The rectangle to convert. + /// The converted rectangle. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Rectangle ToSystemRectangle(this RectInt rectangle) + { + return new Rectangle(rectangle.x, rectangle.y, rectangle.width, rectangle.height); + } + + /// + /// Converts the current to a . + /// + /// The rectangle to convert. + /// The converted rectangle. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static RectangleF ToSystemRectangleF(this RectInt rectangle) + { + return new RectangleF(rectangle.x, rectangle.y, rectangle.width, rectangle.height); + } +} diff --git a/X10D.Unity/src/Drawing/RectangleExtensions.cs b/X10D.Unity/src/Drawing/RectangleExtensions.cs index 371ccae..5c5977d 100644 --- a/X10D.Unity/src/Drawing/RectangleExtensions.cs +++ b/X10D.Unity/src/Drawing/RectangleExtensions.cs @@ -21,4 +21,16 @@ public static class RectangleExtensions { return new Rect(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); } + + /// + /// Converts the current to a . + /// + /// The rectangle to convert. + /// The converted rectangle. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static RectInt ToUnityRectInt(this Rectangle rectangle) + { + return new RectInt(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); + } }