Browse Source

Even more small changes

Krzysztof Krysiński 4 years ago
parent
commit
c7c4dcbf68

+ 5 - 5
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -39,12 +39,12 @@ namespace PixiEditor.Models.Controllers
                 return;
                 return;
             }
             }
 
 
-            for (int i = 0; i < layers.Length; i++)
-            {
-                layers[i].SetPixels(changes);
-            }
+            //TODO: Fix for (int i = 0; i < layers.Length; i++)
+            //{
+            //    layers[i].SetPixels(changes);
+            //}
 
 
-            Manager.ActiveDocument.UndoManager.AddUndoChange(new Change("UndoChanges", old, newChange, "Deleted pixels"));
+            //Manager.ActiveDocument.UndoManager.AddUndoChange(new Change("UndoChanges", old, newChange, "Deleted pixels"));
         }
         }
 
 
         /// <summary>
         /// <summary>

+ 21 - 10
PixiEditor/Models/Tools/Tools/CircleTool.cs

@@ -41,12 +41,12 @@ namespace PixiEditor.Models.Tools.Tools
         {
         {
             int thickness = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
             int thickness = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
             DoubleCords fixedCoordinates = CalculateCoordinatesForShapeRotation(coordinates[^1], coordinates[0]);
             DoubleCords fixedCoordinates = CalculateCoordinatesForShapeRotation(coordinates[^1], coordinates[0]);
-            CreateEllipse(fixedCoordinates.Coords1, fixedCoordinates.Coords2, thickness);
+            var outline = CreateEllipse(layer, color, fixedCoordinates.Coords1, fixedCoordinates.Coords2, thickness);
 
 
             if (Toolbar.GetSetting<BoolSetting>("Fill").Value)
             if (Toolbar.GetSetting<BoolSetting>("Fill").Value)
             {
             {
                 Color fillColor = Toolbar.GetSetting<ColorSetting>("FillColor").Value;
                 Color fillColor = Toolbar.GetSetting<ColorSetting>("FillColor").Value;
-                DrawEllipseFill(outline);
+                DrawEllipseFill(layer, color, outline);
             }
             }
         }
         }
 
 
@@ -62,7 +62,7 @@ namespace PixiEditor.Models.Tools.Tools
             IEnumerable<Coordinates> outline = CreateEllipse(layer, color, startCoordinates, endCoordinates, thickness);
             IEnumerable<Coordinates> outline = CreateEllipse(layer, color, startCoordinates, endCoordinates, thickness);
             if (filled)
             if (filled)
             {
             {
-                DrawEllipseFill(outline);
+                DrawEllipseFill(layer, color, outline);
             }
             }
         }
         }
 
 
@@ -72,25 +72,29 @@ namespace PixiEditor.Models.Tools.Tools
         /// <param name="startCoordinates">Top left coordinate of ellipse.</param>
         /// <param name="startCoordinates">Top left coordinate of ellipse.</param>
         /// <param name="endCoordinates">Bottom right coordinate of ellipse.</param>
         /// <param name="endCoordinates">Bottom right coordinate of ellipse.</param>
         /// <param name="thickness">Thickness of ellipse.</param>
         /// <param name="thickness">Thickness of ellipse.</param>
-        public void CreateEllipse(Layer layer, Color color, Coordinates startCoordinates, Coordinates endCoordinates, int thickness)
+        public IEnumerable<Coordinates> CreateEllipse(Layer layer, Color color, Coordinates startCoordinates, Coordinates endCoordinates, int thickness)
         {
         {
             double radiusX = (endCoordinates.X - startCoordinates.X) / 2.0;
             double radiusX = (endCoordinates.X - startCoordinates.X) / 2.0;
             double radiusY = (endCoordinates.Y - startCoordinates.Y) / 2.0;
             double radiusY = (endCoordinates.Y - startCoordinates.Y) / 2.0;
             double centerX = (startCoordinates.X + endCoordinates.X + 1) / 2.0;
             double centerX = (startCoordinates.X + endCoordinates.X + 1) / 2.0;
             double centerY = (startCoordinates.Y + endCoordinates.Y + 1) / 2.0;
             double centerY = (startCoordinates.Y + endCoordinates.Y + 1) / 2.0;
 
 
-            IEnumerable<Coordinates> ellipse = GenerateMidpointEllipse(radiusX, radiusY, centerX, centerY);
+            IEnumerable<Coordinates> ellipse = GenerateMidpointEllipse(layer, color, radiusX, radiusY, centerX, centerY);
             if (thickness > 1)
             if (thickness > 1)
             {
             {
                 ThickenShape(layer, color, ellipse, thickness);
                 ThickenShape(layer, color, ellipse, thickness);
             }
             }
+
+            return ellipse;
         }
         }
 
 
         public List<Coordinates> GenerateMidpointEllipse(Layer layer, Color color, double halfWidth, double halfHeight, double centerX, double centerY)
         public List<Coordinates> GenerateMidpointEllipse(Layer layer, Color color, double halfWidth, double halfHeight, double centerX, double centerY)
         {
         {
+            using var ctx = layer.LayerBitmap.GetBitmapContext();
+
             if (halfWidth < 1 || halfHeight < 1)
             if (halfWidth < 1 || halfHeight < 1)
             {
             {
-                DrawFallbackRectangle(layer, color, halfWidth, halfHeight, centerX, centerY);
+                return DrawFallbackRectangle(layer, color, halfWidth, halfHeight, centerX, centerY);
             }
             }
 
 
             // ellipse formula: halfHeight^2 * x^2 + halfWidth^2 * y^2 - halfHeight^2 * halfWidth^2 = 0
             // ellipse formula: halfHeight^2 * x^2 + halfWidth^2 * y^2 - halfHeight^2 * halfWidth^2 = 0
@@ -106,7 +110,7 @@ namespace PixiEditor.Models.Tools.Tools
             // from PI/2 to middle
             // from PI/2 to middle
             do
             do
             {
             {
-                DrawRegionPoints(currentX, centerX, currentY, centerY);
+                outputCoordinates.AddRange(DrawRegionPoints(layer, color, currentX, centerX, currentY, centerY));
 
 
                 // calculate next pixel coords
                 // calculate next pixel coords
                 currentX++;
                 currentX++;
@@ -128,7 +132,7 @@ namespace PixiEditor.Models.Tools.Tools
             // from middle to 0
             // from middle to 0
             while (currentY - centerY >= 0)
             while (currentY - centerY >= 0)
             {
             {
-                outputCoordinates.AddRange(DrawRegionPoints(currentX, centerX, currentY, centerY));
+                outputCoordinates.AddRange(DrawRegionPoints(layer, color, currentX, centerX, currentY, centerY));
 
 
                 currentY--;
                 currentY--;
                 if ((Math.Pow(halfHeight, 2) * Math.Pow(currentX - centerX + 0.5, 2)) +
                 if ((Math.Pow(halfHeight, 2) * Math.Pow(currentX - centerX + 0.5, 2)) +
@@ -165,7 +169,7 @@ namespace PixiEditor.Models.Tools.Tools
             }
             }
         }
         }
 
 
-        private void DrawFallbackRectangle(Layer layer, Color color, double halfWidth, double halfHeight, double centerX, double centerY)
+        private List<Coordinates> DrawFallbackRectangle(Layer layer, Color color, double halfWidth, double halfHeight, double centerX, double centerY)
         {
         {
             using var ctx = layer.LayerBitmap.GetBitmapContext();
             using var ctx = layer.LayerBitmap.GetBitmapContext();
 
 
@@ -192,15 +196,22 @@ namespace PixiEditor.Models.Tools.Tools
                 coordinates.Add(cords);
                 coordinates.Add(cords);
                 layer.SetPixel(cords, color);
                 layer.SetPixel(cords, color);
             }
             }
+
+            return coordinates;
         }
         }
 
 
-        private void DrawRegionPoints(double x, double xc, double y, double yc)
+        private Coordinates[] DrawRegionPoints(Layer layer, Color color, double x, double xc, double y, double yc)
         {
         {
             Coordinates[] outputCoordinates = new Coordinates[4];
             Coordinates[] outputCoordinates = new Coordinates[4];
             outputCoordinates[0] = new Coordinates((int)Math.Floor(x), (int)Math.Floor(y));
             outputCoordinates[0] = new Coordinates((int)Math.Floor(x), (int)Math.Floor(y));
+            layer.SetPixel(outputCoordinates[0], color);
             outputCoordinates[1] = new Coordinates((int)Math.Floor(-(x - xc) + xc), (int)Math.Floor(y));
             outputCoordinates[1] = new Coordinates((int)Math.Floor(-(x - xc) + xc), (int)Math.Floor(y));
+            layer.SetPixel(outputCoordinates[1], color);
             outputCoordinates[2] = new Coordinates((int)Math.Floor(x), (int)Math.Floor(-(y - yc) + yc));
             outputCoordinates[2] = new Coordinates((int)Math.Floor(x), (int)Math.Floor(-(y - yc) + yc));
+            layer.SetPixel(outputCoordinates[2], color);
             outputCoordinates[3] = new Coordinates((int)Math.Floor(-(x - xc) + xc), (int)Math.Floor(-(y - yc) + yc));
             outputCoordinates[3] = new Coordinates((int)Math.Floor(-(x - xc) + xc), (int)Math.Floor(-(y - yc) + yc));
+            layer.SetPixel(outputCoordinates[3], color);
+
             return outputCoordinates;
             return outputCoordinates;
         }
         }
     }
     }

+ 3 - 3
PixiEditor/Models/Tools/Tools/LineTool.cs

@@ -109,7 +109,7 @@ namespace PixiEditor.Models.Tools.Tools
             switch (cap)
             switch (cap)
             {
             {
                 case CapType.Round:
                 case CapType.Round:
-                    ApplyRoundCap(position, thickness); // Round cap is not working very well, circle tool must be improved
+                    ApplyRoundCap(layer, color, position, thickness); // Round cap is not working very well, circle tool must be improved
                     break;
                     break;
 
 
                 default:
                 default:
@@ -123,11 +123,11 @@ namespace PixiEditor.Models.Tools.Tools
         /// </summary>
         /// </summary>
         /// <param name="position">Starting position of cap.</param>
         /// <param name="position">Starting position of cap.</param>
         /// <param name="thickness">Thickness of cap.</param>
         /// <param name="thickness">Thickness of cap.</param>
-        private void ApplyRoundCap(Coordinates position, int thickness)
+        private void ApplyRoundCap(Layer layer, Color color, Coordinates position, int thickness)
         {
         {
             IEnumerable<Coordinates> rectangleCords = CoordinatesCalculator.RectangleToCoordinates(
             IEnumerable<Coordinates> rectangleCords = CoordinatesCalculator.RectangleToCoordinates(
                 CoordinatesCalculator.CalculateThicknessCenter(position, thickness));
                 CoordinatesCalculator.CalculateThicknessCenter(position, thickness));
-            circleTool.CreateEllipse(rectangleCords.First(), rectangleCords.Last(), 1, true);
+            circleTool.CreateEllipse(layer, color, rectangleCords.First(), rectangleCords.Last(), 1, true);
         }
         }
 
 
         private List<Coordinates> BresenhamLine(Layer layer, Color color, int x1, int y1, int x2, int y2)
         private List<Coordinates> BresenhamLine(Layer layer, Color color, int x1, int y1, int x2, int y2)

+ 54 - 54
PixiEditor/Models/Tools/Tools/MoveTool.cs

@@ -65,34 +65,34 @@ namespace PixiEditor.Models.Tools.Tools
 
 
         public override void AfterAddedUndo(UndoManager undoManager)
         public override void AfterAddedUndo(UndoManager undoManager)
         {
         {
-            if (currentSelection == null || currentSelection.Length == 0)
-            {
-                return;
-            }
-
-            Change changes = undoManager.UndoStack.Peek();
-
-            // Inject to default undo system change custom changes made by this tool
-            foreach (var item in startPixelColors)
-            {
-                BitmapPixelChanges beforeMovePixels = BitmapPixelChanges.FromArrays(startSelection, item.Value);
-                BitmapPixelChanges afterMovePixels = BitmapPixelChanges.FromArrays(currentSelection, endPixelColors[item.Key]);
-                Guid layerGuid = item.Key;
-                var oldValue = (LayerChange[])changes.OldValue;
-
-                if (oldValue.Any(x => x.LayerGuid == layerGuid))
-                {
-                    var layer = oldValue.First(x => x.LayerGuid == layerGuid);
-                    layer.PixelChanges.ChangedPixels.AddRangeOverride(afterMovePixels.ChangedPixels);
-                    layer.PixelChanges.ChangedPixels
-                        .AddRangeOverride(beforeMovePixels.ChangedPixels);
-
-                    ((LayerChange[])changes.NewValue).First(x => x.LayerGuid == layerGuid).PixelChanges.ChangedPixels
-                        .AddRangeNewOnly(BitmapPixelChanges
-                            .FromSingleColoredArray(startSelection, System.Windows.Media.Colors.Transparent)
-                            .ChangedPixels);
-                }
-            }
+            //if (currentSelection == null || currentSelection.Length == 0)
+            //{
+            //    return;
+            //}
+
+            //Change changes = undoManager.UndoStack.Peek();
+
+            //// Inject to default undo system change custom changes made by this tool
+            //foreach (var item in startPixelColors)
+            //{
+            //    BitmapPixelChanges beforeMovePixels = BitmapPixelChanges.FromArrays(startSelection, item.Value);
+            //    BitmapPixelChanges afterMovePixels = BitmapPixelChanges.FromArrays(currentSelection, endPixelColors[item.Key]);
+            //    Guid layerGuid = item.Key;
+            //    var oldValue = (LayerChange[])changes.OldValue;
+
+            //    if (oldValue.Any(x => x.LayerGuid == layerGuid))
+            //    {
+            //        var layer = oldValue.First(x => x.LayerGuid == layerGuid);
+            //        layer.PixelChanges.ChangedPixels.AddRangeOverride(afterMovePixels.ChangedPixels);
+            //        layer.PixelChanges.ChangedPixels
+            //            .AddRangeOverride(beforeMovePixels.ChangedPixels);
+
+            //        ((LayerChange[])changes.NewValue).First(x => x.LayerGuid == layerGuid).PixelChanges.ChangedPixels
+            //            .AddRangeNewOnly(BitmapPixelChanges
+            //                .FromSingleColoredArray(startSelection, System.Windows.Media.Colors.Transparent)
+            //                .ChangedPixels);
+            //    }
+            //}
         }
         }
 
 
         // This adds undo if there is no selection, reason why this isn't in AfterUndoAdded,
         // This adds undo if there is no selection, reason why this isn't in AfterUndoAdded,
@@ -143,32 +143,32 @@ namespace PixiEditor.Models.Tools.Tools
 
 
         public override void Use(Layer layer, List<Coordinates> mouseMove, Color color)
         public override void Use(Layer layer, List<Coordinates> mouseMove, Color color)
         {
         {
-            LayerChange[] result = new LayerChange[affectedLayers.Length];
-            var end = mouseMove[0];
-            var lastSelection = currentSelection.ToArray();
-            for (int i = 0; i < affectedLayers.Length; i++)
-            {
-                if (currentSelection.Length > 0)
-                {
-                    endPixelColors = BitmapUtils.GetPixelsForSelection(affectedLayers, currentSelection);
-                    var changes = MoveSelection(affectedLayers[i], mouseMove);
-                    ClearSelectedPixels(affectedLayers[i], lastSelection);
-
-                    changes = RemoveTransparentPixels(changes);
-
-                    result[i] = new LayerChange(changes, affectedLayers[i]);
-                }
-                else
-                {
-                    var vector = Transform.GetTranslation(lastMouseMove, end);
-                    affectedLayers[i].Offset = new Thickness(affectedLayers[i].OffsetX + vector.X, affectedLayers[i].OffsetY + vector.Y, 0, 0);
-                    result[i] = new LayerChange(BitmapPixelChanges.Empty, affectedLayers[i]);
-                }
-            }
-
-            lastMouseMove = end;
-
-            return result;
+            //LayerChange[] result = new LayerChange[affectedLayers.Length];
+            //var end = mouseMove[0];
+            //var lastSelection = currentSelection.ToArray();
+            //for (int i = 0; i < affectedLayers.Length; i++)
+            //{
+            //    if (currentSelection.Length > 0)
+            //    {
+            //        endPixelColors = BitmapUtils.GetPixelsForSelection(affectedLayers, currentSelection);
+            //        var changes = MoveSelection(affectedLayers[i], mouseMove);
+            //        ClearSelectedPixels(affectedLayers[i], lastSelection);
+
+            //        changes = RemoveTransparentPixels(changes);
+
+            //        result[i] = new LayerChange(changes, affectedLayers[i]);
+            //    }
+            //    else
+            //    {
+            //        var vector = Transform.GetTranslation(lastMouseMove, end);
+            //        affectedLayers[i].Offset = new Thickness(affectedLayers[i].OffsetX + vector.X, affectedLayers[i].OffsetY + vector.Y, 0, 0);
+            //        result[i] = new LayerChange(BitmapPixelChanges.Empty, affectedLayers[i]);
+            //    }
+            //}
+
+            //lastMouseMove = end;
+
+            // return result;
         }
         }
 
 
         public BitmapPixelChanges MoveSelection(Layer layer, IEnumerable<Coordinates> mouseMove)
         public BitmapPixelChanges MoveSelection(Layer layer, IEnumerable<Coordinates> mouseMove)

+ 1 - 0
PixiEditor/Models/Tools/Tools/PenTool.cs

@@ -136,6 +136,7 @@ namespace PixiEditor.Models.Tools.Tools
             }
             }
 
 
             ThickenShape(layer, color, new Coordinates[] { p2, p3 }.Distinct(), toolSize);
             ThickenShape(layer, color, new Coordinates[] { p2, p3 }.Distinct(), toolSize);
+            return BitmapPixelChanges.Empty;
         }
         }
 
 
         private void PixelPerfectSettingValueChanged(object sender, SettingValueChangedEventArgs<bool> e)
         private void PixelPerfectSettingValueChanged(object sender, SettingValueChangedEventArgs<bool> e)