Browse Source

UndoChanges are almost working

Frytek 5 years ago
parent
commit
c194d5f80e

+ 22 - 4
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -75,7 +75,9 @@ namespace PixiEditor.Models.Controllers
         {
             if(SelectedTool.RequiresPreviewLayer)
             {
+                BitmapPixelChanges oldValues = GetOldPixelsValues(_lastChangedPixels.ChangedPixels.Keys.ToArray());
                 Layers[ActiveLayerIndex].ApplyPixels(_lastChangedPixels);
+                BitmapChanged?.Invoke(this, new BitmapChangedEventArgs(_lastChangedPixels, oldValues, ActiveLayerIndex));
                 _previewLayer.Clear();
             }
         }
@@ -86,21 +88,35 @@ namespace PixiEditor.Models.Controllers
             {
                 var mouseMove = MouseController.LastMouseMoveCoordinates.ToList();
                 mouseMove.Reverse();
-                BitmapPixelChanges changedPixels = new BitmapPixelChanges();
+
                 if (!SelectedTool.RequiresPreviewLayer)
                 {
-                    changedPixels = SelectedTool.Use(Layers[ActiveLayerIndex], mouseMove.ToArray(), PrimaryColor, ToolSize);
+                    BitmapPixelChanges changedPixels = SelectedTool.Use(Layers[ActiveLayerIndex], mouseMove.ToArray(), PrimaryColor, ToolSize);
+                    BitmapPixelChanges oldPixelsValues = GetOldPixelsValues(changedPixels.ChangedPixels.Keys.ToArray());
                     ActiveLayer.ApplyPixels(changedPixels);
+                    BitmapChanged?.Invoke(this, new BitmapChangedEventArgs(changedPixels, oldPixelsValues, ActiveLayerIndex));
                 }
                 else
                 {
                     UseToolOnPreviewLayer(mouseMove);
                 }
-                BitmapChanged?.Invoke(this, new BitmapChangedEventArgs(changedPixels, ActiveLayerIndex));
+
                 _lastMousePos = e.NewPosition;
             }
         }
 
+        private BitmapPixelChanges GetOldPixelsValues(Coordinates[] coordinates)
+        {
+            Dictionary<Coordinates, Color> values = new Dictionary<Coordinates, Color>();
+            Layers[ActiveLayerIndex].LayerBitmap.Lock();
+            for (int i = 0; i < coordinates.Length; i++)
+            {
+                values.Add(coordinates[i], Layers[ActiveLayerIndex].LayerBitmap.GetPixel(coordinates[i].X, coordinates[i].Y));
+            }
+            Layers[ActiveLayerIndex].LayerBitmap.Unlock();
+            return new BitmapPixelChanges(values);
+        }
+
         private void UseToolOnPreviewLayer(List<Coordinates> mouseMove)
         {
             BitmapPixelChanges changedPixels;
@@ -144,11 +160,13 @@ namespace PixiEditor.Models.Controllers
 public class BitmapChangedEventArgs : EventArgs
 {
     public BitmapPixelChanges PixelsChanged { get; set; }
+    public BitmapPixelChanges OldPixelsValues { get; set; }
     public int ChangedLayerIndex { get; set; }
 
-    public BitmapChangedEventArgs(BitmapPixelChanges pixelsChanged, int changedLayerIndex)
+    public BitmapChangedEventArgs(BitmapPixelChanges pixelsChanged, BitmapPixelChanges oldPixelsValues, int changedLayerIndex)
     {
         PixelsChanged = pixelsChanged;
+        OldPixelsValues = oldPixelsValues;
         ChangedLayerIndex = changedLayerIndex;
     }
 }

+ 64 - 0
PixiEditor/Models/Controllers/PixelChangesController.cs

@@ -0,0 +1,64 @@
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Position;
+using PixiEditor.Models.Tools;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Windows.Media;
+
+namespace PixiEditor.Models.Controllers
+{
+    public class PixelChangesController
+    {
+        LayerChanges LastChanges { get; set; }
+        LayerChanges LastOldValues { get; set; }
+
+        public void AddChanges(LayerChanges changes, LayerChanges oldValues)
+        {
+            if(LastChanges == null)
+            {
+                LastChanges = changes;
+                LastOldValues = oldValues;
+                return;
+            }
+
+            foreach (var change in changes.PixelChanges.ChangedPixels)
+            {
+                if (LastChanges.PixelChanges.ChangedPixels.ContainsKey(change.Key))
+                {
+                    continue;
+                }
+                else
+                {
+                    LastChanges.PixelChanges.ChangedPixels.Add(change.Key, change.Value);
+                }
+            }
+
+            foreach (var change in oldValues.PixelChanges.ChangedPixels)
+            {
+                if (LastOldValues.PixelChanges.ChangedPixels.ContainsKey(change.Key))
+                {
+                    continue;
+                }
+                else
+                {
+                    LastOldValues.PixelChanges.ChangedPixels.Add(change.Key, change.Value);
+                }
+            }
+        }
+
+        public Tuple<LayerChanges, LayerChanges> PopChanges()
+        {
+            Dictionary<Coordinates, Color> pixelChanges = LastChanges.PixelChanges.ChangedPixels.ToDictionary(entry => entry.Key, entry => entry.Value);
+            Dictionary<Coordinates, Color> oldValues = LastOldValues.PixelChanges.ChangedPixels.ToDictionary(entry => entry.Key, entry => entry.Value);
+            
+            var tmp = new LayerChanges(new BitmapPixelChanges(pixelChanges), LastChanges.LayerIndex);
+            var oldValuesTmp = new LayerChanges(new BitmapPixelChanges(oldValues), LastOldValues.LayerIndex);
+            
+            Tuple<LayerChanges, LayerChanges> outputChanges = new Tuple<LayerChanges, LayerChanges>(tmp, oldValuesTmp);
+            LastChanges.PixelChanges.ChangedPixels.Clear();
+            LastOldValues.PixelChanges.ChangedPixels.Clear();
+            return outputChanges;
+        }
+    }
+}

+ 0 - 2
PixiEditor/Models/Controllers/UndoManager.cs

@@ -83,7 +83,6 @@ namespace PixiEditor.Models.Controllers
             }
             _lastChangeWasUndo = false;
             UndoStack.Push(change);
-            Debug.WriteLine("UndoStackCount: " + UndoStack.Count + " RedoStackCount: " + RedoStack.Count);
         }
         /// <summary>
         /// Adds property change to UndoStack
@@ -100,7 +99,6 @@ namespace PixiEditor.Models.Controllers
             }
             _lastChangeWasUndo = false;
             UndoStack.Push(new Change(property, oldValue, newValue, undoDescription));
-            Debug.WriteLine("UndoStackCount: " + UndoStack.Count + " RedoStackCount: " + RedoStack.Count);
         }
 
         /// <summary>

+ 19 - 0
PixiEditor/Models/DataHolders/LayerChanges.cs

@@ -0,0 +1,19 @@
+using PixiEditor.Models.Tools;
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace PixiEditor.Models.DataHolders
+{
+    public class LayerChanges
+    {
+        public BitmapPixelChanges PixelChanges { get; set; }
+        public int LayerIndex { get; set; }
+
+        public LayerChanges(BitmapPixelChanges pixelChanges, int layerIndex)
+        {
+            PixelChanges = pixelChanges;
+            LayerIndex = layerIndex;
+        }
+    }
+}

+ 2 - 1
PixiEditor/Models/Tools/BitmapPixelChanges.cs

@@ -8,7 +8,8 @@ namespace PixiEditor.Models.Tools
 {
     public struct BitmapPixelChanges
     {
-        public Dictionary<Coordinates, Color> ChangedPixels;
+        public Dictionary<Coordinates, Color> ChangedPixels { get; set; } 
+
 
         public BitmapPixelChanges(Dictionary<Coordinates, Color> changedPixels)
         {

+ 30 - 2
PixiEditor/ViewModels/ViewModelMain.cs

@@ -18,6 +18,7 @@ using PixiEditor.Models.Images;
 using PixiEditor.Models.IO;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
+using PixiEditor.Models.DataHolders;
 
 namespace PixiEditor.ViewModels
 {
@@ -112,13 +113,29 @@ namespace PixiEditor.ViewModels
 
         public List<Tool> ToolSet { get; set; }
 
-
+        private LayerChanges _undoChanges;
+
+        public LayerChanges UndoChanges
+        {
+            get { return _undoChanges; }
+            set 
+            { 
+                _undoChanges = value;
+                BitmapUtility.Layers[value.LayerIndex].ApplyPixels(value.PixelChanges);
+            }
+        }
+
+
         public BitmapOperationsUtility BitmapUtility { get; set; }
+        public PixelChangesController ChangesController { get; set; }
 
         public ViewModelMain()
         {
             PixiFilesManager.InitializeTempDirectories();
             BitmapUtility = new BitmapOperationsUtility();
+            BitmapUtility.BitmapChanged += BitmapUtility_BitmapChanged;
+            BitmapUtility.MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
+            ChangesController = new PixelChangesController();
             SelectToolCommand = new RelayCommand(RecognizeTool);
             GenerateDrawAreaCommand = new RelayCommand(GenerateDrawArea);
             MouseMoveCommand = new RelayCommand(MouseMove);
@@ -139,6 +156,18 @@ namespace PixiEditor.ViewModels
             ToolSize = 1;
         }
 
+        private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
+        {
+            Tuple<LayerChanges, LayerChanges> changes = ChangesController.PopChanges();
+            UndoManager.AddUndoChange(new Change("UndoChanges", changes.Item2, changes.Item1)); //Item2 is old value
+        }
+
+        private void BitmapUtility_BitmapChanged(object sender, BitmapChangedEventArgs e)
+        {
+            ChangesController.AddChanges(new LayerChanges(e.PixelsChanged, e.ChangedLayerIndex), 
+                new LayerChanges(e.OldPixelsValues, e.ChangedLayerIndex));
+        }
+
         public void SetActiveLayer(object parameter)
         {
             BitmapUtility.SetActiveLayer((int)parameter);
@@ -201,7 +230,6 @@ namespace PixiEditor.ViewModels
         /// <param name="parameter"></param>
         private void MouseUp(object parameter)
         {
-            UndoManager.StopRecording();
             BitmapUtility.MouseController.StopRecordingMouseMovementChanges();
         }