Explorar el Código

Added delete layer undo

flabbet hace 4 años
padre
commit
24a765c690

+ 13 - 7
PixiEditor/Models/DataHolders/Document/Document.Layers.cs

@@ -149,7 +149,7 @@ namespace PixiEditor.Models.DataHolders
 
             if (Layers.Count > 1)
             {
-                StorageBasedChange storageChange = new StorageBasedChange(this, new[] { Layers[^1] }, false);
+                StorageBasedChange storageChange = new(this, new[] { Layers[^1] }, false);
                 UndoManager.AddUndoChange(
                     storageChange.ToChange(
                         RemoveLayerProcess,
@@ -238,7 +238,7 @@ namespace PixiEditor.Models.DataHolders
             }
         }
 
-        public void RemoveLayer(int layerIndex, bool addToUndo)
+        public void RemoveLayer(int layerIndex, bool addToUndo = true)
         {
             if (Layers.Count == 0)
             {
@@ -249,12 +249,11 @@ namespace PixiEditor.Models.DataHolders
 
             bool wasActive = Layers[layerIndex].IsActive;
 
-            StorageBasedChange change = new StorageBasedChange(this, new[] { Layers[layerIndex] });
+            StorageBasedChange change = new(this, new[] { Layers[layerIndex] });
             if (addToUndo)
             {
                 UndoManager.AddUndoChange(
-                    change.ToChange(RestoreLayersProcess, RemoveLayerProcess, new object[] { Layers[layerIndex].LayerGuid }, "Remove layer"));
-
+                    change.ToChange(RestoreLayersProcess, RemoveLayerProcess, new object[] { Layers[layerIndex].LayerGuid }));
             }
 
             Layers.RemoveAt(layerIndex);
@@ -276,17 +275,24 @@ namespace PixiEditor.Models.DataHolders
                 return;
             }
 
+            var oldLayerStructure = LayerStructure.Clone();
+
             Layer[] layers = Layers.Where(x => x.IsActive).ToArray();
             int firstIndex = Layers.IndexOf(layers[0]);
 
             object[] guidArgs = new object[] { layers.Select(x => x.LayerGuid).ToArray() };
 
-            StorageBasedChange change = new StorageBasedChange(this, layers);
+            StorageBasedChange change = new(this, layers);
 
             RemoveLayersProcess(guidArgs);
 
+            UndoManager.AddUndoChange(
+                new Change(nameof(LayerStructure), oldLayerStructure, LayerStructure.Clone(), root: this));
+
             InjectRemoveActiveLayersUndo(guidArgs, change);
 
+            UndoManager.SquashUndoChanges(2, "Removed active layers");
+
             SetNextLayerAsActive(firstIndex);
         }
 
@@ -389,7 +395,7 @@ namespace PixiEditor.Models.DataHolders
 
             if (Layers.Count == 0)
             {
-                Layer layer = new Layer("Base Layer");
+                Layer layer = new("Base Layer");
                 Layers.Add(layer);
                 undoAction = (Layer[] layers, UndoLayer[] undoData) =>
                 {

+ 4 - 4
PixiEditor/Models/Layers/GuidStructureItem.cs

@@ -25,9 +25,9 @@ namespace PixiEditor.Models.Layers
             }
         }
 
-        private Guid? startLayerGuid = null;
+        private Guid startLayerGuid;
 
-        public Guid? StartLayerGuid
+        public Guid StartLayerGuid
         {
             get => startLayerGuid;
             set
@@ -37,9 +37,9 @@ namespace PixiEditor.Models.Layers
             }
         }
 
-        private Guid? endLayerGuid = null;
+        private Guid endLayerGuid;
 
-        public Guid? EndLayerGuid
+        public Guid EndLayerGuid
         {
             get => endLayerGuid;
             set

+ 29 - 15
PixiEditor/Models/Layers/LayerStructure.cs

@@ -32,6 +32,30 @@ namespace PixiEditor.Models.Layers
             return GetGroupByLayer(layerGuid, Groups);
         }
 
+        public LayerStructure Clone()
+        {
+            return new(CloneGroups(Groups), Owner);
+        }
+
+        private ObservableCollection<GuidStructureItem> CloneGroups(ObservableCollection<GuidStructureItem> groups)
+        {
+            ObservableCollection<GuidStructureItem> outputGroups = new();
+            foreach (var group in groups.ToArray())
+            {
+                outputGroups.Add(new(group.Name, group.StartLayerGuid, group.EndLayerGuid, group.Subgroups, group.Parent)
+                {
+                    GroupGuid = group.GroupGuid,
+                    IsExpanded = group.IsExpanded,
+                });
+                if(group.Subgroups.Count > 0)
+                {
+                    outputGroups[^1].Subgroups = CloneGroups(group.Subgroups);
+                }
+            }
+
+            return outputGroups;
+        }
+
         // This will allow to add new group with multiple layers and groups at once. Not working well, todo fix
         /*public GuidStructureItem AddNewGroup(string groupName, IEnumerable<Layer> layers, Guid activeLayer)
         {
@@ -420,7 +444,7 @@ namespace PixiEditor.Models.Layers
             }
         }
 
-        private Guid? FindBoundLayer(Guid layerGuid, int parentFolderTopIndex, int parentFolderBottomIndex, bool above)
+        private Guid FindBoundLayer(Guid layerGuid, int parentFolderTopIndex, int parentFolderBottomIndex, bool above)
         {
             return GetNextLayerGuid(
                    layerGuid,
@@ -428,7 +452,7 @@ namespace PixiEditor.Models.Layers
                    above);
         }
 
-        private Guid? FindBoundLayer(GuidStructureItem parentFolder, Guid layerGuid, bool above)
+        private Guid FindBoundLayer(GuidStructureItem parentFolder, Guid layerGuid, bool above)
         {
             int parentFolderTopIndex = Owner.Layers.IndexOf(Owner.Layers.First(x => x.LayerGuid == parentFolder.EndLayerGuid));
             int parentFolderBottomIndex = Owner.Layers.IndexOf(Owner.Layers.First(x => x.LayerGuid == parentFolder.StartLayerGuid));
@@ -436,23 +460,13 @@ namespace PixiEditor.Models.Layers
             return FindBoundLayer(layerGuid, parentFolderTopIndex, parentFolderBottomIndex, above);
         }
 
-        private Guid? GetNextLayerGuid(Guid? layer, List<Guid> allLayers, bool above)
+        private Guid GetNextLayerGuid(Guid layer, List<Guid> allLayers, bool above)
         {
-            if (layer == null)
-            {
-                return null;
-            }
-
-            int indexOfLayer = allLayers.IndexOf(layer.Value);
+            int indexOfLayer = allLayers.IndexOf(layer);
 
             int modifier = above ? 1 : -1;
 
-            int newIndex = indexOfLayer + modifier;
-
-            if (newIndex < 0 || newIndex >= allLayers.Count)
-            {
-                return null;
-            }
+            int newIndex = Math.Clamp(indexOfLayer + modifier, 0, allLayers.Count - 1);
 
             return allLayers[newIndex];
         }

+ 1 - 9
PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs

@@ -168,16 +168,8 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         public void DeleteLayer(object parameter)
         {
-            int index = (int)parameter;
             var doc = Owner.BitmapManager.ActiveDocument;
-            if (!doc.Layers[index].IsActive)
-            {
-                doc.RemoveLayer(index, true);
-            }
-            else
-            {
-                doc.RemoveActiveLayers();
-            }
+            doc.RemoveActiveLayers();
         }
 
         public bool CanDeleteLayer(object property)

+ 29 - 0
PixiEditorTests/ModelsTests/LayerStructureTests/LayerStructureCloneTests.cs

@@ -0,0 +1,29 @@
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Layers;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.LayerStructureTests
+{
+    public class LayerStructureCloneTests
+    {
+        public void TestThatCloneReturnsSameLayerStructure()
+        {
+            Document doc = new(0, 0);
+            doc.Layers.Add(new("Test"));
+            doc.Layers.Add(new("Test2"));
+            LayerStructure structure = new(doc);
+            structure.AddNewGroup("Test group", doc.Layers[0].LayerGuid);
+
+            var clone = structure.Clone();
+
+            Assert.Equal(doc, clone.Owner);
+            Assert.Single(clone.Groups);
+            Assert.Equal(structure.Groups[0].GroupGuid, clone.Groups[0].GroupGuid);
+        }
+    }
+}