flabbet 3 years ago
parent
commit
3513df8697
2 changed files with 52 additions and 12 deletions
  1. 26 5
      PixiEditor/Models/Tools/Tool.cs
  2. 26 7
      PixiEditor/Models/Undo/StorageBasedChange.cs

+ 26 - 5
PixiEditor/Models/Tools/Tool.cs

@@ -16,9 +16,6 @@ namespace PixiEditor.Models.Tools
 {
     public abstract class Tool : NotifyableObject
     {
-        private bool isActive;
-        private string actionDisplay = string.Empty;
-
         public virtual string ToolName => GetType().Name.Replace("Tool", string.Empty);
 
         public virtual string DisplayName => ToolName.AddSpacesBeforeUppercaseLetters();
@@ -55,15 +52,39 @@ namespace PixiEditor.Models.Tools
 
         public bool CanStartOutsideCanvas { get; set; } = false;
 
+        private bool isActive;
+        private string actionDisplay = string.Empty;
+        private StorageBasedChange _change;
+
         public virtual void OnMouseDown(MouseEventArgs e)
         {
+            Document doc = ViewModels.ViewModelMain.Current.BitmapManager.ActiveDocument;
+            _change = new StorageBasedChange(doc, new[] { doc.ActiveLayer }, true);
         }
 
         public virtual void AddUndoProcess(Document document)
         {
-            //StorageBasedChange change = new StorageBasedChange(document, affectedLayers, false);
+            var args = new object[] { _change.Document };
+            document.UndoManager.AddUndoChange(_change.ToChange(UndoProcess, args));
+        }
 
-            //manager.AddUndoChange(change.ToChange(), )
+        private void UndoProcess(Layer[] layers, UndoLayer[] data, object[] args)
+        {
+            if(args.Length > 0 && args[0] is Document document)
+            {
+                for (int i = 0; i < layers.Length; i++)
+                {
+                    Layer layer = layers[i];
+                    document.Layers.RemoveAt(data[i].LayerIndex);
+
+                    document.Layers.Insert(data[i].LayerIndex, layer);
+                    if (data[i].IsActive)
+                    {
+                        document.SetMainActiveLayer(data[i].LayerIndex);
+                    }
+                    layer.InvokeLayerBitmapChange();
+                }
+            }
         }
 
         public virtual void OnMouseUp(MouseEventArgs e)

+ 26 - 7
PixiEditor/Models/Undo/StorageBasedChange.cs

@@ -22,11 +22,11 @@ namespace PixiEditor.Models.Undo
 
         private List<Guid> layersToStore;
 
-        private Document document;
+        public Document Document { get; }
 
         public StorageBasedChange(Document doc, IEnumerable<Layer> layers, bool saveOnStartup = true)
         {
-            document = doc;
+            Document = doc;
             layersToStore = layers.Select(x => x.LayerGuid).ToList();
             UndoChangeLocation = DefaultUndoChangeLocation;
             GenerateUndoLayers();
@@ -38,7 +38,7 @@ namespace PixiEditor.Models.Undo
 
         public StorageBasedChange(Document doc, IEnumerable<Layer> layers, string undoChangeLocation, bool saveOnStartup = true)
         {
-            document = doc;
+            Document = doc;
             layersToStore = layers.Select(x => x.LayerGuid).ToList();
             UndoChangeLocation = undoChangeLocation;
             GenerateUndoLayers();
@@ -54,7 +54,7 @@ namespace PixiEditor.Models.Undo
             int i = 0;
             foreach (var layerGuid in layersToStore)
             {
-                Layer layer = document.Layers.First(x => x.LayerGuid == layerGuid);
+                Layer layer = Document.Layers.First(x => x.LayerGuid == layerGuid);
                 UndoLayer storedLayer = StoredLayers[i];
                 if (Directory.Exists(Path.GetDirectoryName(storedLayer.StoredPngLayerName)))
                 {
@@ -125,6 +125,25 @@ namespace PixiEditor.Models.Undo
             return new Change(finalUndoProcess, processArgs, finalRedoProcess, redoProcessParameters, description);
         }
 
+        /// <summary>
+        ///     Creates UndoManager ready Change instance, where undo and redo is the same, before process images are loaded from disk and current ones are saved.
+        /// </summary>
+        /// <param name="undoRedoProcess">Process that is invoked on redo and undo.</param>
+        /// <param name="processArgs">Custom parameters for undo and redo process.</param>
+        /// <param name="description">Undo change description.</param>
+        /// <returns>UndoManager ready Change instance.</returns>
+        public Change ToChange(Action<Layer[], UndoLayer[], object[]> undoRedoProcess, object[] processArgs, string description = "")
+        {
+            Action<object[]> finalProcess = processParameters =>
+            {
+                Layer[] layers = LoadLayersFromDevice();
+                SaveLayersOnDevice();
+                undoRedoProcess(layers, StoredLayers, processParameters);
+            };
+
+            return new Change(finalProcess, processArgs, finalProcess, processArgs, description);
+        }
+
         /// <summary>
         ///     Creates UndoManager ready Change instance, where undo process loads layers from device, and redo saves them.
         /// </summary>
@@ -210,15 +229,15 @@ namespace PixiEditor.Models.Undo
             int i = 0;
             foreach (var layerGuid in layersToStore)
             {
-                Layer layer = document.Layers.First(x => x.LayerGuid == layerGuid);
-                if (!document.Layers.Contains(layer))
+                Layer layer = Document.Layers.First(x => x.LayerGuid == layerGuid);
+                if (!Document.Layers.Contains(layer))
                 {
                     throw new ArgumentException("Provided document doesn't contain selected layer");
                 }
 
                 layer.ClipCanvas();
 
-                int index = document.Layers.IndexOf(layer);
+                int index = Document.Layers.IndexOf(layer);
                 string pngName = layer.Name + Guid.NewGuid().ToString();
                 StoredLayers[i] = new UndoLayer(
                     Path.Join(