Browse Source

Fixed groups bug

flabbet 3 years ago
parent
commit
54c3ae19f5

+ 13 - 5
PixiEditor/Models/Controllers/UndoManager.cs

@@ -1,4 +1,4 @@
-using System;
+using System;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Linq;
@@ -103,10 +103,16 @@ namespace PixiEditor.Models.Controllers
         /// Merges multiple undo changes into one.
         /// </summary>
         /// <param name="amount">Amount of changes to squash.</param>
-        public void SquashUndoChanges(int amount)
+        /// <param name="reverseOrderInReverseProcess">Reverses order of execution changes in reverseProcess (undo)</param>
+        public void SquashUndoChanges(int amount, bool reverseOrderInReverseProcess = false)
         {
             string description = UndoStack.ElementAt(UndoStack.Count - amount).Description;
-            SquashUndoChanges(amount, description);
+            if (string.IsNullOrEmpty(description))
+            {
+                description = $"Squash {amount} undo changes.";
+            }
+
+            SquashUndoChanges(amount, description, reverseOrderInReverseProcess);
         }
 
         /// <summary>
@@ -114,7 +120,8 @@ namespace PixiEditor.Models.Controllers
         /// </summary>
         /// <param name="amount">Amount of changes to squash.</param>
         /// <param name="description">Final change description.</param>
-        public void SquashUndoChanges(int amount, string description)
+        /// <param name="reverseOrderInReverseProcess">Reverses order of execution changes in reverseProcess (undo)</param>
+        public void SquashUndoChanges(int amount, string description, bool reverseOrderInReverseProcess = false)
         {
             Change[] changes = new Change[amount];
             for (int i = 0; i < amount; i++)
@@ -124,7 +131,8 @@ namespace PixiEditor.Models.Controllers
 
             Action<object[]> reverseProcess = (object[] props) =>
             {
-                foreach (var prop in props)
+                IEnumerable<object> enumerable = reverseOrderInReverseProcess ? props.Reverse() : props;
+                foreach (var prop in enumerable)
                 {
                     Change change = (Change)prop;
                     if (change.ReverseProcess == null)

+ 1 - 1
PixiEditor/Models/DataHolders/Document/Document.Layers.cs

@@ -416,7 +416,7 @@ namespace PixiEditor.Models.DataHolders
                     BuildLayerStructureProcess,
                     new object[] { oldLayerStructureGroups },
                     BuildLayerStructureProcess,
-                    new object[] { LayerStructure.CloneGroups() }));
+                    new object[] { LayerStructure.CloneGroups() }, "Reload LayerStructure"));
         }
 
         public Layer MergeLayers(Layer[] layersToMerge, bool nameOfLast, int index)

+ 2 - 3
PixiEditor/Models/Tools/BitmapOperationTool.cs

@@ -31,7 +31,6 @@ namespace PixiEditor.Models.Tools
         /// <summary>
         /// Executes undo adding procedure.
         /// </summary>
-        /// <param name="document">Active document</param>
         /// <remarks>When overriding, set UseDefaultUndoMethod to false.</remarks>
         public override void AfterUse()
         {
@@ -40,8 +39,8 @@ namespace PixiEditor.Models.Tools
             var document = ViewModels.ViewModelMain.Current.BitmapManager.ActiveDocument;
             var args = new object[] { _change.Document };
             document.UndoManager.AddUndoChange(_change.ToChange(StorageBasedChange.BasicUndoProcess, args));
-            document.AddLayerStructureToUndo(document.LayerStructure.Groups);
-            document.UndoManager.SquashUndoChanges(2);
+            document.AddLayerStructureToUndo(document.LayerStructure.CloneGroups());
+            document.UndoManager.SquashUndoChanges(2, true);
             _change = null;
         }
     }

+ 1 - 3
PixiEditor/Models/Undo/StorageBasedChange.cs

@@ -21,7 +21,6 @@ namespace PixiEditor.Models.Undo
         public UndoLayer[] StoredLayers { get; set; }
 
         private List<Guid> layersToStore;
-
         public Document Document { get; }
 
         public StorageBasedChange(Document doc, IEnumerable<Layer> layers, bool saveOnStartup = true)
@@ -260,11 +259,10 @@ namespace PixiEditor.Models.Undo
                 for (int i = 0; i < layers.Length; i++)
                 {
                     Layer layer = layers[i];
-                    document.RemoveLayer(data[i].LayerIndex, false);
 
+                    document.RemoveLayer(data[i].LayerIndex, false);
                     document.Layers.Insert(data[i].LayerIndex, layer);
 
-
                     if (data[i].IsActive)
                     {
                         document.SetMainActiveLayer(data[i].LayerIndex);

+ 5 - 0
PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs

@@ -174,13 +174,18 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
             Guid lastActiveLayerGuid = doc.ActiveLayerGuid;
 
+
             doc.AddNewLayer($"New Layer {Owner.BitmapManager.ActiveDocument.Layers.Count}");
 
+            var oldGroups = doc.LayerStructure.CloneGroups();
+
             if (doc.Layers.Count > 1)
             {
                 doc.MoveLayerInStructure(doc.Layers[^1].LayerGuid, lastActiveLayerGuid, true);
                 Guid? parent = parameter is Layer or LayerStructureItemContainer ? activeLayerParent?.GroupGuid : activeLayerParent.Parent?.GroupGuid;
                 doc.LayerStructure.AssignParent(doc.ActiveLayerGuid, parent);
+                doc.AddLayerStructureToUndo(oldGroups);
+                doc.UndoManager.SquashUndoChanges(3, "Add New Layer");
             }
             if (control != null)
             {