1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-10 04:55:42 +00:00

Add RectInt conversions

This commit is contained in:
Oliver Booth 2022-06-01 11:06:24 +01:00
parent 5d63560146
commit 74af813d78
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
7 changed files with 114 additions and 2 deletions

View File

@ -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()`

View File

@ -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;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 18f2e8fbc200475ca5fe7857a457a874
timeCreated: 1654077768

View File

@ -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);

View File

@ -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);

View File

@ -0,0 +1,36 @@
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace X10D.Unity.Drawing;
/// <summary>
/// Drawing-related extension methods for <see cref="RectInt" />.
/// </summary>
public static class RectIntExtensions
{
/// <summary>
/// Converts the current <see cref="RectInt" /> to a <see cref="Rectangle" />.
/// </summary>
/// <param name="rectangle">The rectangle to convert.</param>
/// <returns>The converted rectangle.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle ToSystemRectangle(this RectInt rectangle)
{
return new Rectangle(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}
/// <summary>
/// Converts the current <see cref="RectInt" /> to a <see cref="RectangleF" />.
/// </summary>
/// <param name="rectangle">The rectangle to convert.</param>
/// <returns>The converted rectangle.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RectangleF ToSystemRectangleF(this RectInt rectangle)
{
return new RectangleF(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}
}

View File

@ -21,4 +21,16 @@ public static class RectangleExtensions
{
return new Rect(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
}
/// <summary>
/// Converts the current <see cref="Rectangle" /> to a <see cref="RectInt" />.
/// </summary>
/// <param name="rectangle">The rectangle to convert.</param>
/// <returns>The converted rectangle.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RectInt ToUnityRectInt(this Rectangle rectangle)
{
return new RectInt(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
}
}