Browse Source

Undo and Redo fixed

flabbet 5 years ago
parent
commit
d3560f5b52

+ 5 - 2
PixiEditorDotNetCore3/Models/Change.cs

@@ -13,6 +13,8 @@ namespace PixiEditorDotNetCore3.Models
     {
         public object OldValue { get; set; }
 
+        public object NewValue { get; set; }
+
         public string Description { get; set; }
 
         public string Property { get; set; }
@@ -21,12 +23,13 @@ namespace PixiEditorDotNetCore3.Models
         {
             Property = property;
             OldValue = oldValue;
-            Description = description;
+            Description = description;
+            NewValue = OldValue;
         }
 
         public Change()
         {
-
+           
         }
 
     }

+ 0 - 1
PixiEditorDotNetCore3/Models/Tools/ToolsManager.cs

@@ -102,7 +102,6 @@ namespace PixiEditorDotNetCore3.Models.Tools
                 _color = color;
                 _toolSzie = toolSize;
             }
-                     
 
             if (_loopTimer.Enabled == false)
             {

+ 20 - 7
PixiEditorDotNetCore3/Models/UndoManager.cs

@@ -17,8 +17,6 @@ namespace PixiEditorDotNetCore3.Models
 {
     public static class UndoManager
     {
-        private const int MaximumChangesInRam = 1;
-
         public static StackEx<Change> UndoStack { get; set; } = new StackEx<Change>(); 
         public static StackEx<Change> RedoStack { get; set; } = new StackEx<Change>();
         private static bool _stopRecording = false; 
@@ -61,10 +59,12 @@ namespace PixiEditorDotNetCore3.Models
         {
             if (_stopRecording == false)
             {
-                if (_recordedChanges.Count < 2)
+                if (_recordedChanges.Count >= 2)
                 {
-                    _recordedChanges.Add(new Change(property, oldValue, undoDescription));
+                    _recordedChanges.RemoveAt(_recordedChanges.Count - 1);
                 }
+                _recordedChanges.Add(new Change(property, oldValue, undoDescription));
+
             }
         }
 
@@ -77,11 +77,23 @@ namespace PixiEditorDotNetCore3.Models
             if (_recordedChanges.Count > 0)
             {
                 Change changeToSave = _recordedChanges[0];
-                AddUndoChange(changeToSave.Property, changeToSave.OldValue, changeToSave.Description);
+                changeToSave.NewValue = _recordedChanges.Last().OldValue;
+                AddUndoChange(changeToSave);
                 _recordedChanges.Clear();
             }
             _stopRecording = false;
         }
+
+        public static void AddUndoChange(Change change)
+        {
+            if (_lastChangeWasUndo == false && RedoStack.Count > 0) //Cleares RedoStack if las move wasn't redo or undo and if redo stack is greater than 0
+            {
+                RedoStack.Clear();
+            }
+            _lastChangeWasUndo = false;
+            UndoStack.Push(change);
+            Debug.WriteLine("UndoStackCount: " + UndoStack.Count + " RedoStackCount: " + RedoStack.Count);
+        }
         /// <summary>
         /// Adds property change to UndoStack
         /// </summary>
@@ -94,7 +106,7 @@ namespace PixiEditorDotNetCore3.Models
             if(_lastChangeWasUndo == false && RedoStack.Count > 0) //Cleares RedoStack if las move wasn't redo or undo and if redo stack is greater than 0
             {
                 RedoStack.Clear();
-            }
+            }            
             _lastChangeWasUndo = false;
             UndoStack.Push(new Change(property, oldValue, undoDescription));
             Debug.WriteLine("UndoStackCount: " + UndoStack.Count + " RedoStackCount: " + RedoStack.Count);
@@ -118,7 +130,8 @@ namespace PixiEditorDotNetCore3.Models
         {
             _lastChangeWasUndo = true;
             PropertyInfo propinfo = MainRoot.GetType().GetProperty(RedoStack.Peek().Property);
-            propinfo.SetValue(MainRoot, RedoStack.Pop().OldValue);
+            propinfo.SetValue(MainRoot, RedoStack.Peek().NewValue);
+            UndoStack.Push(RedoStack.Pop());
 
         }
     }

+ 2 - 7
PixiEditorDotNetCore3/ViewModels/ViewModelMain.cs

@@ -53,13 +53,6 @@ namespace PixiEditor.ViewModels
         {
             get { return _activeLayer; }
             set {
-                if (_activeLayer != null)
-                {
-                    UndoManager.AddUndoChange("ActiveLightLayer",
-                        new LightLayer(_activeLayer.LayerBitmap.ToByteArray(), ActiveLayer.Height, ActiveLayer.Width),
-                        "Layer Changed");
-                }
-
                 _activeLayer = value;
                 RefreshImage();
                 RaisePropertyChanged("ActiveLayer");
@@ -251,6 +244,8 @@ namespace PixiEditor.ViewModels
 
             if (SelectedTool != ToolType.ColorPicker)
             {
+                UndoManager.RecordChanges("ActiveLightLayer", new LightLayer(ActiveLayer.LayerBitmap.ToByteArray(), (int)ActiveLayer.LayerBitmap.Height, 
+                    (int)ActiveLayer.LayerBitmap.Width), $"Used {SelectedTool.ToString()}");
                 primaryToolSet.ExecuteTool(ActiveLayer, cords, SelectedColor, ToolSize);
                 RefreshImage();
             }