Browse Source

Cleaned code and wrote test

flabbet 4 years ago
parent
commit
cea66a180c

+ 12 - 6
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -199,6 +199,7 @@ namespace PixiEditor.Models.Controllers
                     Manager.ActiveDocument.ActiveLayer,
                     Manager.ActiveDocument.ActiveLayer,
                     mouseMove.ToArray(),
                     mouseMove.ToArray(),
                     Manager.PrimaryColor);
                     Manager.PrimaryColor);
+
                 BitmapPixelChanges[] changes = modifiedLayers.Select(x => x.PixelChanges).ToArray();
                 BitmapPixelChanges[] changes = modifiedLayers.Select(x => x.PixelChanges).ToArray();
                 Manager.ActiveDocument.PreviewLayer.SetPixels(BitmapPixelChanges.CombineOverride(changes));
                 Manager.ActiveDocument.PreviewLayer.SetPixels(BitmapPixelChanges.CombineOverride(changes));
 
 
@@ -208,14 +209,19 @@ namespace PixiEditor.Models.Controllers
                 }
                 }
                 else
                 else
                 {
                 {
-                    for (int i = 0; i < modifiedLayers.Length; i++)
-                    {
-                        var layer = previewLayerChanges.First(x => x.LayerIndex == modifiedLayers[i].LayerIndex);
-                        layer.PixelChanges.ChangedPixels.AddRangeOverride(modifiedLayers[i].PixelChanges.ChangedPixels);
-                        layer.PixelChanges = layer.PixelChanges.WithoutTransparentPixels();
-                    }
+                    InjectPreviewLayerChanges(modifiedLayers);
                 }
                 }
             }
             }
         }
         }
+
+        private void InjectPreviewLayerChanges(LayerChange[] modifiedLayers)
+        {
+            for (int i = 0; i < modifiedLayers.Length; i++)
+            {
+                var layer = previewLayerChanges.First(x => x.LayerIndex == modifiedLayers[i].LayerIndex);
+                layer.PixelChanges.ChangedPixels.AddRangeOverride(modifiedLayers[i].PixelChanges.ChangedPixels);
+                layer.PixelChanges = layer.PixelChanges.WithoutTransparentPixels();
+            }
+        }
     }
     }
 }
 }

+ 1 - 2
PixiEditor/Models/Tools/ShapeTool.cs

@@ -18,8 +18,7 @@ namespace PixiEditor.Models.Tools
             Toolbar = new BasicShapeToolbar();
             Toolbar = new BasicShapeToolbar();
         }
         }
 
 
-        //TODO: Add cache for lines 31, 32
-
+        // TODO: Add cache for lines 31, 32 (hopefully it would speed up calculation)
         public abstract override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color color);
         public abstract override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color color);
 
 
         protected IEnumerable<Coordinates> GetThickShape(IEnumerable<Coordinates> shape, int thickness)
         protected IEnumerable<Coordinates> GetThickShape(IEnumerable<Coordinates> shape, int thickness)

+ 29 - 19
PixiEditor/Models/Tools/Tools/PenTool.cs

@@ -60,14 +60,7 @@ namespace PixiEditor.Models.Tools.Tools
             }
             }
 
 
             var latestPixels = lineTool.CreateLine(startingCoords, latestCords, 1);
             var latestPixels = lineTool.CreateLine(startingCoords, latestCords, 1);
-            if (latestPixels.Count() == 1)
-            {
-                lastChangedPixels[changedPixelsindex] = latestPixels.First();
-            }
-            else
-            {
-                lastChangedPixels[changedPixelsindex] = latestPixels.ElementAt(1);
-            }
+            SetPixelToCheck(latestPixels);
 
 
             if (changedPixelsindex == 2)
             if (changedPixelsindex == 2)
             {
             {
@@ -78,17 +71,7 @@ namespace PixiEditor.Models.Tools.Tools
                     color,
                     color,
                     toolSize);
                     toolSize);
 
 
-                if (changes.ChangedPixels[lastChangedPixels[1]].A != 0)
-                {
-                    lastChangedPixels[0] = lastChangedPixels[1];
-                    lastChangedPixels[1] = lastChangedPixels[2];
-                    changedPixelsindex = 2;
-                }
-                else
-                {
-                    lastChangedPixels[0] = lastChangedPixels[2];
-                    changedPixelsindex = 1;
-                }
+                MovePixelsToCheck(changes);
 
 
                 return changes;
                 return changes;
             }
             }
@@ -98,6 +81,33 @@ namespace PixiEditor.Models.Tools.Tools
             return BitmapPixelChanges.FromSingleColoredArray(GetThickShape(latestPixels, toolSize), color);
             return BitmapPixelChanges.FromSingleColoredArray(GetThickShape(latestPixels, toolSize), color);
         }
         }
 
 
+        private void MovePixelsToCheck(BitmapPixelChanges changes)
+        {
+            if (changes.ChangedPixels[lastChangedPixels[1]].A != 0)
+            {
+                lastChangedPixels[0] = lastChangedPixels[1];
+                lastChangedPixels[1] = lastChangedPixels[2];
+                changedPixelsindex = 2;
+            }
+            else
+            {
+                lastChangedPixels[0] = lastChangedPixels[2];
+                changedPixelsindex = 1;
+            }
+        }
+
+        private void SetPixelToCheck(IEnumerable<Coordinates> latestPixels)
+        {
+            if (latestPixels.Count() == 1)
+            {
+                lastChangedPixels[changedPixelsindex] = latestPixels.First();
+            }
+            else
+            {
+                lastChangedPixels[changedPixelsindex] = latestPixels.ElementAt(1);
+            }
+        }
+
         private BitmapPixelChanges ApplyPixelPerfectToPixels(Coordinates p1, Coordinates p2, Coordinates p3, Color color, int toolSize)
         private BitmapPixelChanges ApplyPixelPerfectToPixels(Coordinates p1, Coordinates p2, Coordinates p3, Color color, int toolSize)
         {
         {
             if (Math.Abs(p3.X - p1.X) == 1 && Math.Abs(p3.Y - p1.Y) == 1)
             if (Math.Abs(p3.X - p1.X) == 1 && Math.Abs(p3.Y - p1.Y) == 1)

+ 27 - 0
PixiEditorTests/ModelsTests/ToolsTests/PenToolTests.cs

@@ -0,0 +1,27 @@
+using PixiEditor.Models.Position;
+using PixiEditor.Models.Tools.Tools;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.ToolsTests
+{
+    [Collection("Application collection")]
+    public class PenToolTests
+    {
+        [StaFact]
+        public void TestThatPixelPerfectPenReturnsShapeWithoutLShapePixels()
+        {
+            PenTool pen = new PenTool();
+
+            Coordinates start = new Coordinates(0, 0);
+            Coordinates end = new Coordinates(0, 0);
+            Coordinates end2 = new Coordinates(1, 0);
+            Coordinates start2 = new Coordinates(1, 1);
+
+            pen.Draw(start, end, System.Windows.Media.Colors.Black, 1, true);
+            pen.Draw(end, end2, System.Windows.Media.Colors.Black, 1, true);
+            var points = pen.Draw(end2, start2, System.Windows.Media.Colors.Black, 1, true);
+
+            Assert.Contains(points.ChangedPixels, x => x.Value.A == 0);
+        }
+    }
+}