瀏覽代碼

Add extension methods for rects

Equbuxu 3 年之前
父節點
當前提交
a5ed89e5cc

+ 8 - 2
PixiEditor/Helpers/Extensions/Int32RectEx.cs

@@ -1,9 +1,10 @@
-using System;
+using SkiaSharp;
+using System;
 using System.Windows;
 
 namespace PixiEditor.Helpers.Extensions
 {
-    static class Int32RectEx
+    public static class Int32RectEx
     {
         public static Int32Rect Intersect(this Int32Rect rect, Int32Rect other)
         {
@@ -50,5 +51,10 @@ namespace PixiEditor.Helpers.Extensions
 
             return new Int32Rect(minX1, minY1, width, height);
         }
+
+        public static SKRectI ToSKRectI(this Int32Rect rect)
+        {
+            return new SKRectI(rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height);
+        }
     }
 }

+ 13 - 0
PixiEditor/Helpers/Extensions/SKRectIEx.cs

@@ -0,0 +1,13 @@
+using SkiaSharp;
+using System.Windows;
+
+namespace PixiEditor.Helpers.Extensions
+{
+    public static class SKRectIEx
+    {
+        public static Int32Rect ToInt32Rect(this SKRectI rect)
+        {
+            return new Int32Rect(rect.Left, rect.Top, rect.Width, rect.Height);
+        }
+    }
+}

+ 32 - 0
PixiEditorTests/ModelsTests/PositionTests/RectTests.cs

@@ -0,0 +1,32 @@
+using PixiEditor.Helpers.Extensions;
+using SkiaSharp;
+using System.Windows;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.PositionTests
+{
+    public class RectTests
+    {
+        [Fact]
+        public void TestThatInt32RectToSKRectIWorks()
+        {
+            Int32Rect rect = new Int32Rect(5, 2, 8, 10);
+            SKRectI converted = rect.ToSKRectI();
+            Assert.Equal(rect.X, converted.Left);
+            Assert.Equal(rect.Y, converted.Top);
+            Assert.Equal(rect.Width, converted.Width);
+            Assert.Equal(rect.Height, converted.Height);
+        }
+
+        [Fact]
+        public void TestThatSKRectIToInt32RectWorks()
+        {
+            SKRectI rect = new SKRectI(5, 2, 8, 10);
+            Int32Rect converted = rect.ToInt32Rect();
+            Assert.Equal(rect.Left, converted.X);
+            Assert.Equal(rect.Top, converted.Y);
+            Assert.Equal(rect.Width, converted.Width);
+            Assert.Equal(rect.Height, converted.Height);
+        }
+    }
+}