Krzysztof Krysiński 4 years ago
parent
commit
36fef27900

+ 16 - 28
PixiEditor/Models/Tools/Tools/CircleTool.cs

@@ -41,37 +41,29 @@ namespace PixiEditor.Models.Tools.Tools
         {
             int thickness = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
             DoubleCords fixedCoordinates = CalculateCoordinatesForShapeRotation(coordinates[^1], coordinates[0]);
-            IEnumerable<Coordinates> outline = CreateEllipse(fixedCoordinates.Coords1, fixedCoordinates.Coords2, thickness);
-            BitmapPixelChanges pixels = BitmapPixelChanges.FromSingleColoredArray(outline, color);
+            CreateEllipse(fixedCoordinates.Coords1, fixedCoordinates.Coords2, thickness);
+
             if (Toolbar.GetSetting<BoolSetting>("Fill").Value)
             {
                 Color fillColor = Toolbar.GetSetting<ColorSetting>("FillColor").Value;
-                pixels.ChangedPixels.AddRangeNewOnly(
-                    BitmapPixelChanges.FromSingleColoredArray(CalculateFillForEllipse(outline), fillColor)
-                        .ChangedPixels);
+                DrawEllipseFill(outline);
             }
         }
 
         /// <summary>
-        ///     Calculates ellipse points for specified coordinates and thickness.
+        ///     Draws ellipse for specified coordinates and thickness.
         /// </summary>
         /// <param name="startCoordinates">Top left coordinate of ellipse.</param>
         /// <param name="endCoordinates">Bottom right coordinate of ellipse.</param>
         /// <param name="thickness">Thickness of ellipse.</param>
         /// <param name="filled">Should ellipse be filled.</param>
-        /// <returns>Coordinates for ellipse.</returns>
-        public IEnumerable<Coordinates> CreateEllipse(Coordinates startCoordinates, Coordinates endCoordinates, int thickness, bool filled)
+        public void CreateEllipse(Layer layer, Color color, Coordinates startCoordinates, Coordinates endCoordinates, int thickness, bool filled)
         {
-            List<Coordinates> output = new List<Coordinates>();
-            IEnumerable<Coordinates> outline = CreateEllipse(startCoordinates, endCoordinates, thickness);
-            output.AddRange(outline);
+            IEnumerable<Coordinates> outline = CreateEllipse(layer, color, startCoordinates, endCoordinates, thickness);
             if (filled)
             {
-                output.AddRange(CalculateFillForEllipse(outline));
-                return output.Distinct();
+                DrawEllipseFill(outline);
             }
-
-            return output;
         }
 
         /// <summary>
@@ -98,7 +90,7 @@ namespace PixiEditor.Models.Tools.Tools
         {
             if (halfWidth < 1 || halfHeight < 1)
             {
-                return FallbackRectangle(layer, color, halfWidth, halfHeight, centerX, centerY);
+                DrawFallbackRectangle(layer, color, halfWidth, halfHeight, centerX, centerY);
             }
 
             // ellipse formula: halfHeight^2 * x^2 + halfWidth^2 * y^2 - halfHeight^2 * halfWidth^2 = 0
@@ -114,7 +106,7 @@ namespace PixiEditor.Models.Tools.Tools
             // from PI/2 to middle
             do
             {
-                outputCoordinates.AddRange(GetRegionPoints(currentX, centerX, currentY, centerY));
+                DrawRegionPoints(currentX, centerX, currentY, centerY);
 
                 // calculate next pixel coords
                 currentX++;
@@ -136,7 +128,7 @@ namespace PixiEditor.Models.Tools.Tools
             // from middle to 0
             while (currentY - centerY >= 0)
             {
-                outputCoordinates.AddRange(GetRegionPoints(currentX, centerX, currentY, centerY));
+                outputCoordinates.AddRange(DrawRegionPoints(currentX, centerX, currentY, centerY));
 
                 currentY--;
                 if ((Math.Pow(halfHeight, 2) * Math.Pow(currentX - centerX + 0.5, 2)) +
@@ -150,13 +142,13 @@ namespace PixiEditor.Models.Tools.Tools
             return outputCoordinates;
         }
 
-        public IEnumerable<Coordinates> CalculateFillForEllipse(IEnumerable<Coordinates> outlineCoordinates)
+        public void DrawEllipseFill(Layer layer, Color color, IEnumerable<Coordinates> outlineCoordinates)
         {
-            List<Coordinates> finalCoordinates = new List<Coordinates>();
+            using var ctx = layer.LayerBitmap.GetBitmapContext();
 
             if (!outlineCoordinates.Any())
             {
-                return finalCoordinates;
+                return;
             }
 
             int bottom = outlineCoordinates.Max(x => x.Y);
@@ -168,14 +160,12 @@ namespace PixiEditor.Models.Tools.Tools
                 int left = rowCords.Min(x => x.X);
                 for (int j = left + 1; j < right; j++)
                 {
-                    finalCoordinates.Add(new Coordinates(j, i));
+                    layer.SetPixel(new Coordinates(j, i), color);
                 }
             }
-
-            return finalCoordinates;
         }
 
-        private List<Coordinates> FallbackRectangle(Layer layer, Color color, double halfWidth, double halfHeight, double centerX, double centerY)
+        private void DrawFallbackRectangle(Layer layer, Color color, double halfWidth, double halfHeight, double centerX, double centerY)
         {
             using var ctx = layer.LayerBitmap.GetBitmapContext();
 
@@ -202,11 +192,9 @@ namespace PixiEditor.Models.Tools.Tools
                 coordinates.Add(cords);
                 layer.SetPixel(cords, color);
             }
-
-            return coordinates;
         }
 
-        private Coordinates[] GetRegionPoints(double x, double xc, double y, double yc)
+        private void DrawRegionPoints(double x, double xc, double y, double yc)
         {
             Coordinates[] outputCoordinates = new Coordinates[4];
             outputCoordinates[0] = new Coordinates((int)Math.Floor(x), (int)Math.Floor(y));

+ 22 - 33
PixiEditor/Models/Tools/Tools/RectangleTool.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Windows.Input;
 using System.Windows.Media;
+using System.Windows.Media.Imaging;
 using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
@@ -41,48 +42,43 @@ namespace PixiEditor.Models.Tools.Tools
         public override void Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
             int thickness = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
-            BitmapPixelChanges pixels =
-                BitmapPixelChanges.FromSingleColoredArray(CreateRectangle(coordinates, thickness), color);
+            CreateRectangle(layer, color, coordinates, thickness);
             if (Toolbar.GetSetting<BoolSetting>("Fill").Value)
             {
                 Color fillColor = Toolbar.GetSetting<ColorSetting>("FillColor").Value;
-                pixels.ChangedPixels.AddRangeOverride(
-                    BitmapPixelChanges.FromSingleColoredArray(
-                            CalculateFillForRectangle(coordinates[^1], coordinates[0], thickness), fillColor)
-                        .ChangedPixels);
+                DrawRectangleFill(layer, color, coordinates[^1], coordinates[0], thickness);
             }
         }
 
-        public IEnumerable<Coordinates> CreateRectangle(List<Coordinates> coordinates, int thickness)
+        public void CreateRectangle(Layer layer, Color color, List<Coordinates> coordinates, int thickness)
         {
             DoubleCords fixedCoordinates = CalculateCoordinatesForShapeRotation(coordinates[^1], coordinates[0]);
-            List<Coordinates> output = new List<Coordinates>();
-            IEnumerable<Coordinates> rectangle = CalculateRectanglePoints(fixedCoordinates);
-            output.AddRange(rectangle);
+
+            using var ctx = layer.LayerBitmap.GetBitmapContext();
+
+            DrawRectangle(layer, color, fixedCoordinates);
 
             for (int i = 1; i < (int)Math.Floor(thickness / 2f) + 1; i++)
             {
-                output.AddRange(CalculateRectanglePoints(new DoubleCords(
+                DrawRectangle(layer, color, new DoubleCords(
                     new Coordinates(fixedCoordinates.Coords1.X - i, fixedCoordinates.Coords1.Y - i),
-                    new Coordinates(fixedCoordinates.Coords2.X + i, fixedCoordinates.Coords2.Y + i))));
+                    new Coordinates(fixedCoordinates.Coords2.X + i, fixedCoordinates.Coords2.Y + i)));
             }
 
             for (int i = 1; i < (int)Math.Ceiling(thickness / 2f); i++)
             {
-                output.AddRange(CalculateRectanglePoints(new DoubleCords(
+                DrawRectangle(layer, color, new DoubleCords(
                     new Coordinates(fixedCoordinates.Coords1.X + i, fixedCoordinates.Coords1.Y + i),
-                    new Coordinates(fixedCoordinates.Coords2.X - i, fixedCoordinates.Coords2.Y - i))));
+                    new Coordinates(fixedCoordinates.Coords2.X - i, fixedCoordinates.Coords2.Y - i)));
             }
-
-            return output.Distinct();
         }
 
-        public IEnumerable<Coordinates> CreateRectangle(Coordinates start, Coordinates end, int thickness)
+        public void CreateRectangle(Layer layer, Color color, Coordinates start, Coordinates end, int thickness)
         {
-            return CreateRectangle(new() { end, start }, thickness);
+            CreateRectangle(layer, color, new() { end, start }, thickness);
         }
 
-        public IEnumerable<Coordinates> CalculateFillForRectangle(Coordinates start, Coordinates end, int thickness)
+        public void DrawRectangleFill(Layer layer, Color color, Coordinates start, Coordinates end, int thickness)
         {
             int offset = (int)Math.Ceiling(thickness / 2f);
             DoubleCords fixedCords = CalculateCoordinatesForShapeRotation(start, end);
@@ -98,40 +94,33 @@ namespace PixiEditor.Models.Tools.Tools
 
             if (height < 1 || width < 1)
             {
-                return Array.Empty<Coordinates>();
+                return;
             }
 
-            Coordinates[] filledCoordinates = new Coordinates[width * height];
             int i = 0;
             for (int y = 0; y < height; y++)
             {
                 for (int x = 0; x < width; x++)
                 {
-                    filledCoordinates[i] = new Coordinates(innerCords.Coords1.X + x, innerCords.Coords1.Y + y);
+                    layer.SetPixel(new Coordinates(innerCords.Coords1.X + x, innerCords.Coords1.Y + y), color);
                     i++;
                 }
             }
-
-            return filledCoordinates.Distinct();
         }
 
-        private IEnumerable<Coordinates> CalculateRectanglePoints(DoubleCords coordinates)
+        private void DrawRectangle(Layer layer, Color color, DoubleCords coordinates)
         {
-            List<Coordinates> finalCoordinates = new List<Coordinates>();
-
             for (int i = coordinates.Coords1.X; i < coordinates.Coords2.X + 1; i++)
             {
-                finalCoordinates.Add(new Coordinates(i, coordinates.Coords1.Y));
-                finalCoordinates.Add(new Coordinates(i, coordinates.Coords2.Y));
+                layer.SetPixel(new Coordinates(i, coordinates.Coords1.Y), color);
+                layer.SetPixel(new Coordinates(i, coordinates.Coords2.Y), color);
             }
 
             for (int i = coordinates.Coords1.Y + 1; i <= coordinates.Coords2.Y - 1; i++)
             {
-                finalCoordinates.Add(new Coordinates(coordinates.Coords1.X, i));
-                finalCoordinates.Add(new Coordinates(coordinates.Coords2.X, i));
+                layer.SetPixel(new Coordinates(coordinates.Coords1.X, i), color);
+                layer.SetPixel(new Coordinates(coordinates.Coords2.X, i), color);
             }
-
-            return finalCoordinates;
         }
     }
 }

+ 2 - 2
PixiEditor/Models/Tools/Tools/SelectTool.cs

@@ -67,7 +67,7 @@ namespace PixiEditor.Models.Tools.Tools
         public IEnumerable<Coordinates> GetRectangleSelectionForPoints(Coordinates start, Coordinates end)
         {
             List<Coordinates> selection = rectangleTool.CreateRectangle(start, end, 1).ToList();
-            selection.AddRange(rectangleTool.CalculateFillForRectangle(start, end, 1));
+            selection.AddRange(rectangleTool.DrawRectangleFill(start, end, 1));
             return selection;
         }
 
@@ -75,7 +75,7 @@ namespace PixiEditor.Models.Tools.Tools
         {
             DoubleCords fixedCoordinates = ShapeTool.CalculateCoordinatesForShapeRotation(start, end);
             List<Coordinates> selection = circleTool.CreateEllipse(fixedCoordinates.Coords1, fixedCoordinates.Coords2, 1).ToList();
-            selection.AddRange(circleTool.CalculateFillForEllipse(selection));
+            selection.AddRange(circleTool.DrawEllipseFill(selection));
             return selection;
         }