Browse Source

Added nested property to undo and undo change to crop

flabbet 5 years ago
parent
commit
8fe8b451bc

+ 17 - 4
PixiEditor/Models/Controllers/UndoManager.cs

@@ -1,5 +1,7 @@
 using PixiEditor.Models.DataHolders;
+using System;
 using System.Collections.Generic;
+using System.Linq;
 using System.Reflection;
 
 namespace PixiEditor.Models.Controllers
@@ -70,8 +72,7 @@ namespace PixiEditor.Models.Controllers
         public static void Undo()
         {
             _lastChangeWasUndo = true;
-            PropertyInfo propInfo = MainRoot.GetType().GetProperty(UndoStack.Peek().Property);
-            propInfo.SetValue(MainRoot, UndoStack.Peek().OldValue);
+            SetPropertyValue(MainRoot, UndoStack.Peek().Property, UndoStack.Peek().OldValue);
             RedoStack.Push(UndoStack.Pop());
         }
 
@@ -81,10 +82,22 @@ namespace PixiEditor.Models.Controllers
         public static void Redo()
         {
             _lastChangeWasUndo = true;
-            PropertyInfo propinfo = MainRoot.GetType().GetProperty(RedoStack.Peek().Property);
-            propinfo.SetValue(MainRoot, RedoStack.Peek().NewValue);
+            SetPropertyValue(MainRoot, RedoStack.Peek().Property, RedoStack.Peek().NewValue);
             UndoStack.Push(RedoStack.Pop());
 
         }
+       
+
+        private static void SetPropertyValue(object target, string propName, object value)
+        {
+            string[] bits = propName.Split('.');
+            for (int i = 0; i < bits.Length - 1; i++)
+            {
+                PropertyInfo propertyToGet = target.GetType().GetProperty(bits[i]);
+                target = propertyToGet.GetValue(target, null);
+            }
+            PropertyInfo propertyToSet = target.GetType().GetProperty(bits.Last());
+            propertyToSet.SetValue(target, value, null);
+        }
     }
 }

+ 21 - 0
PixiEditor/Models/DataHolders/Document.cs

@@ -1,12 +1,15 @@
 using PixiEditor.Helpers;
+using PixiEditor.Models.Controllers;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
+using System;
 using System.Collections.ObjectModel;
 using System.Linq;
 using System.Windows.Media.Imaging;
 
 namespace PixiEditor.Models.DataHolders
 {
+    [Serializable]
     public class Document : NotifyableObject
     {
         private int _width;
@@ -57,8 +60,25 @@ namespace PixiEditor.Models.DataHolders
             Height = height;
         }
 
+        public Document DeepClone()
+        {
+            Document doc = new Document(Width, Height)
+            {
+                Layers = new ObservableCollection<Layer>(Layers.Select(x => new Layer(x.LayerBitmap.Clone()) 
+                {
+                    Name = x.Name,
+                    Width = x.Width,
+                    Height = x.Height,
+                    IsActive = x.IsActive,
+                    IsVisible = x.IsVisible
+                })),
+            };
+            return doc;
+        }
+
         public void Crop(int x, int y, int width, int height)
         {
+            Document copy = DeepClone();
             for (int i = 0; i < Layers.Count; i++)
             {
                 Layers[i].LayerBitmap = Layers[i].LayerBitmap.Crop(x, y, width, height);
@@ -67,6 +87,7 @@ namespace PixiEditor.Models.DataHolders
             }
             Height = height;
             Width = width;
+            UndoManager.AddUndoChange(new Change("BitmapManager.ActiveDocument", copy, this, "Crop document"));
         }
 
         public void ClipCanvas()

+ 2 - 0
PixiEditor/ViewModels/ViewModelMain.cs

@@ -491,6 +491,8 @@ namespace PixiEditor.ViewModels
             BitmapManager.ActiveDocument = new Document(width, height);
             BitmapManager.AddNewLayer("Base Layer", width, height, true);
             BitmapManager.PreviewLayer = null;
+            UndoManager.UndoStack.Clear();
+            UndoManager.RedoStack.Clear();
         }
 
         /// <summary>