Ver Fonte

Fixed DeletePixels undo

flabbet há 4 anos atrás
pai
commit
0b8d51244e

+ 3 - 2
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -35,9 +35,10 @@ namespace PixiEditor.Models.Controllers
             LayerChange[] newChange = new LayerChange[layers.Length];
             for (int i = 0; i < layers.Length; i++)
             {
+                int indexOfLayer = Manager.ActiveDocument.Layers.IndexOf(layers[i]);
                 old[i] = new LayerChange(
-                    BitmapPixelChanges.FromArrays(pixels, oldValues[layers[i]]), i);
-                newChange[i] = new LayerChange(changes, i);
+                    BitmapPixelChanges.FromArrays(pixels, oldValues[layers[i]]), indexOfLayer);
+                newChange[i] = new LayerChange(changes, indexOfLayer);
                 layers[i].SetPixels(changes);
             }
 

+ 23 - 0
PixiEditor/Models/DataHolders/BitmapPixelChanges.cs

@@ -1,9 +1,12 @@
 using System;
 using System.Collections.Generic;
+using System.Collections.ObjectModel;
 using System.Linq;
 using System.Windows.Media;
+using System.Windows.Media.Imaging;
 using PixiEditor.Exceptions;
 using PixiEditor.Helpers.Extensions;
+using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 
 namespace PixiEditor.Models.DataHolders
@@ -37,6 +40,26 @@ namespace PixiEditor.Models.DataHolders
             return new BitmapPixelChanges(dict) { WasBuiltAsSingleColored = true };
         }
 
+        /// <summary>
+        ///     Builds BitmapPixelChanges from coordinates in layer.
+        /// </summary>
+        /// <param name="selectedPoints">Pixels coordinates in layer.</param>
+        /// <param name="activeLayer">Layer from pixels are taken.</param>
+        /// <returns>BitmapPixelChanges.</returns>
+        public static BitmapPixelChanges FromSelection(IEnumerable<Coordinates> selectedPoints, Layer activeLayer)
+        {
+            Dictionary<Coordinates, Color> dict = new Dictionary<Coordinates, Color>();
+            using (var ctx = activeLayer.LayerBitmap.GetBitmapContext())
+            {
+                foreach (var point in selectedPoints)
+                {
+                    dict.Add(point, ctx.WriteableBitmap.GetPixel(point.X, point.Y));
+                }
+            }
+
+            return new BitmapPixelChanges(dict);
+        }
+
         /// <summary>
         ///     Combines pixel changes array with overriding values.
         /// </summary>

+ 5 - 7
PixiEditor/ViewModels/SubViewModels/Main/ClipboardViewModel.cs

@@ -6,6 +6,7 @@ using System.Windows.Media;
 using PixiEditor.Helpers;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Position;
 
 namespace PixiEditor.ViewModels.SubViewModels.Main
 {
@@ -37,12 +38,9 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         public void Cut(object parameter)
         {
             Copy(null);
-            Owner.BitmapManager.ActiveDocument.ActiveLayer.SetPixels(
-                BitmapPixelChanges.FromSingleColoredArray(
-                    Owner.SelectionSubViewModel.ActiveSelection.SelectedPoints.ToArray(),
-                    Colors.Transparent));
-            LayerChange[] oldValues = new 
-            UndoManager.AddUndoChange(new Change("UndoChanges", oldValues, newValues, description: "Cut selection"));
+            Owner.BitmapManager.BitmapOperations.DeletePixels(
+                new[] { Owner.BitmapManager.ActiveDocument.ActiveLayer },
+                Owner.SelectionSubViewModel.ActiveSelection.SelectedPoints.ToArray());
         }
 
         public void Paste(object parameter)
@@ -58,7 +56,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         private void Copy(object parameter)
         {
             ClipboardController.CopyToClipboard(
-                Owner.BitmapManager.ActiveDocument.Layers.ToArray(),
+                new[] { Owner.BitmapManager.ActiveDocument.ActiveLayer },
                 Owner.SelectionSubViewModel.ActiveSelection.SelectedPoints.ToArray(),
                 Owner.BitmapManager.ActiveDocument.Width,
                 Owner.BitmapManager.ActiveDocument.Height);