Browse Source

Fixed undo and redo of StorageBasedChange, layersToStore didn't update

flabbet 4 years ago
parent
commit
4e75c63cff

+ 3 - 3
PixiEditor/Models/Controllers/BitmapChangedEventArgs.cs

@@ -5,17 +5,17 @@ namespace PixiEditor.Models.Controllers
 {
     public class BitmapChangedEventArgs : EventArgs
     {
-        public BitmapChangedEventArgs(BitmapPixelChanges pixelsChanged, BitmapPixelChanges oldPixelsValues, int changedLayerIndex)
+        public BitmapChangedEventArgs(BitmapPixelChanges pixelsChanged, BitmapPixelChanges oldPixelsValues, Guid changedLayerGuid)
         {
             PixelsChanged = pixelsChanged;
             OldPixelsValues = oldPixelsValues;
-            ChangedLayerIndex = changedLayerIndex;
+            ChangedLayerGuid = changedLayerGuid;
         }
 
         public BitmapPixelChanges PixelsChanged { get; set; }
 
         public BitmapPixelChanges OldPixelsValues { get; set; }
 
-        public int ChangedLayerIndex { get; set; }
+        public Guid ChangedLayerGuid{ get; set; }
     }
 }

+ 17 - 14
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -36,15 +36,15 @@ namespace PixiEditor.Models.Controllers
             }
 
             BitmapPixelChanges changes = BitmapPixelChanges.FromSingleColoredArray(pixels, Color.FromArgb(0, 0, 0, 0));
-            Dictionary<Layer, Color[]> oldValues = BitmapUtils.GetPixelsForSelection(layers, pixels);
+            Dictionary<Guid, Color[]> oldValues = BitmapUtils.GetPixelsForSelection(layers, pixels);
             LayerChange[] old = new LayerChange[layers.Length];
             LayerChange[] newChange = new LayerChange[layers.Length];
             for (int i = 0; i < layers.Length; i++)
             {
-                int indexOfLayer = Manager.ActiveDocument.Layers.IndexOf(layers[i]);
+                Guid guid = layers[i].LayerGuid;
                 old[i] = new LayerChange(
-                    BitmapPixelChanges.FromArrays(pixels, oldValues[layers[i]]), indexOfLayer);
-                newChange[i] = new LayerChange(changes, indexOfLayer);
+                    BitmapPixelChanges.FromArrays(pixels, oldValues[layers[i].LayerGuid]), guid);
+                newChange[i] = new LayerChange(changes, guid);
                 layers[i].SetPixels(changes);
             }
 
@@ -85,15 +85,18 @@ namespace PixiEditor.Models.Controllers
 
             for (int i = 0; i < lastModifiedLayers.Length; i++)
             {
-                Layer layer = Manager.ActiveDocument.Layers[lastModifiedLayers[i].LayerIndex];
+                Layer layer = Manager.ActiveDocument.Layers.FirstOrDefault(x => x.LayerGuid == lastModifiedLayers[i].LayerGuid);
 
-                BitmapPixelChanges oldValues = ApplyToLayer(layer, lastModifiedLayers[i]).PixelChanges;
+                if (layer != null)
+                {
+                    BitmapPixelChanges oldValues = ApplyToLayer(layer, lastModifiedLayers[i]).PixelChanges;
 
-                BitmapChanged?.Invoke(this, new BitmapChangedEventArgs(
-                    lastModifiedLayers[i].PixelChanges,
-                    oldValues,
-                    lastModifiedLayers[i].LayerIndex));
-                Manager.ActiveDocument.GeneratePreviewLayer();
+                    BitmapChanged?.Invoke(this, new BitmapChangedEventArgs(
+                        lastModifiedLayers[i].PixelChanges,
+                        oldValues,
+                        lastModifiedLayers[i].LayerGuid));
+                    Manager.ActiveDocument.GeneratePreviewLayer();
+                }
             }
         }
 
@@ -110,13 +113,13 @@ namespace PixiEditor.Models.Controllers
                 LayerChange[] oldPixelsValues = new LayerChange[modifiedLayers.Length];
                 for (int i = 0; i < modifiedLayers.Length; i++)
                 {
-                    Layer layer = Manager.ActiveDocument.Layers[modifiedLayers[i].LayerIndex];
+                    Layer layer = Manager.ActiveDocument.Layers.First(x => x.LayerGuid == modifiedLayers[i].LayerGuid);
                     oldPixelsValues[i] = ApplyToLayer(layer, modifiedLayers[i]);
 
                     BitmapChanged?.Invoke(this, new BitmapChangedEventArgs(
                         modifiedLayers[i].PixelChanges,
                         oldPixelsValues[i].PixelChanges,
-                        modifiedLayers[i].LayerIndex));
+                        modifiedLayers[i].LayerGuid));
                 }
             }
             else
@@ -131,7 +134,7 @@ namespace PixiEditor.Models.Controllers
 
             LayerChange oldPixelsValues = new LayerChange(
                 GetOldPixelsValues(change.PixelChanges.ChangedPixels.Keys.ToArray()),
-                change.LayerIndex);
+                change.LayerGuid);
 
             layer.SetPixels(change.PixelChanges, false);
             return oldPixelsValues;

+ 12 - 12
PixiEditor/Models/Controllers/PixelChangesController.cs

@@ -7,9 +7,9 @@ namespace PixiEditor.Models.Controllers
 {
     public class PixelChangesController
     {
-        private Dictionary<int, LayerChange> LastChanges { get; set; }
+        private Dictionary<Guid, LayerChange> LastChanges { get; set; }
 
-        private Dictionary<int, LayerChange> LastOldValues { get; set; }
+        private Dictionary<Guid, LayerChange> LastOldValues { get; set; }
 
         /// <summary>
         ///     Adds layer changes to controller.
@@ -22,10 +22,10 @@ namespace PixiEditor.Models.Controllers
             {
                 if (LastChanges == null)
                 {
-                    LastChanges = new Dictionary<int, LayerChange> { { changes.LayerIndex, changes } };
-                    LastOldValues = new Dictionary<int, LayerChange> { { oldValues.LayerIndex, oldValues } };
+                    LastChanges = new Dictionary<Guid, LayerChange> { { changes.LayerGuid, changes } };
+                    LastOldValues = new Dictionary<Guid, LayerChange> { { oldValues.LayerGuid, oldValues } };
                 }
-                else if (LastChanges.ContainsKey(changes.LayerIndex))
+                else if (LastChanges.ContainsKey(changes.LayerGuid))
                 {
                     AddToExistingLayerChange(changes, oldValues);
                 }
@@ -50,7 +50,7 @@ namespace PixiEditor.Models.Controllers
 
             Tuple<LayerChange, LayerChange>[] result = new Tuple<LayerChange, LayerChange>[LastChanges.Count];
             int i = 0;
-            foreach (KeyValuePair<int, LayerChange> change in LastChanges)
+            foreach (KeyValuePair<Guid, LayerChange> change in LastChanges)
             {
                 Dictionary<Position.Coordinates, System.Windows.Media.Color> pixelChanges =
                     change.Value.PixelChanges.ChangedPixels.ToDictionary(entry => entry.Key, entry => entry.Value);
@@ -71,33 +71,33 @@ namespace PixiEditor.Models.Controllers
 
         private void AddNewLayerChange(LayerChange changes, LayerChange oldValues)
         {
-            LastChanges[changes.LayerIndex] = changes;
-            LastOldValues[changes.LayerIndex] = oldValues;
+            LastChanges[changes.LayerGuid] = changes;
+            LastOldValues[changes.LayerGuid] = oldValues;
         }
 
         private void AddToExistingLayerChange(LayerChange layerChange, LayerChange oldValues)
         {
             foreach (KeyValuePair<Position.Coordinates, System.Windows.Media.Color> change in layerChange.PixelChanges.ChangedPixels)
             {
-                if (LastChanges[layerChange.LayerIndex].PixelChanges.ChangedPixels.ContainsKey(change.Key))
+                if (LastChanges[layerChange.LayerGuid].PixelChanges.ChangedPixels.ContainsKey(change.Key))
                 {
                     continue;
                 }
                 else
                 {
-                    LastChanges[layerChange.LayerIndex].PixelChanges.ChangedPixels.Add(change.Key, change.Value);
+                    LastChanges[layerChange.LayerGuid].PixelChanges.ChangedPixels.Add(change.Key, change.Value);
                 }
             }
 
             foreach (KeyValuePair<Position.Coordinates, System.Windows.Media.Color> change in oldValues.PixelChanges.ChangedPixels)
             {
-                if (LastOldValues[layerChange.LayerIndex].PixelChanges.ChangedPixels.ContainsKey(change.Key))
+                if (LastOldValues[layerChange.LayerGuid].PixelChanges.ChangedPixels.ContainsKey(change.Key))
                 {
                     continue;
                 }
                 else
                 {
-                    LastOldValues[layerChange.LayerIndex].PixelChanges.ChangedPixels.Add(change.Key, change.Value);
+                    LastOldValues[layerChange.LayerGuid].PixelChanges.ChangedPixels.Add(change.Key, change.Value);
                 }
             }
         }

+ 2 - 2
PixiEditor/Models/DataHolders/Document.cs

@@ -308,7 +308,7 @@ namespace PixiEditor.Models.DataHolders
                 MoveLayerProcess,
                 new object[] { layerIndex + amount, -amount },
                 MoveLayerProcess,
-                new object[] { layerIndex, amount }, 
+                new object[] { layerIndex, amount },
                 "Move layer"));
         }
 
@@ -337,7 +337,7 @@ namespace PixiEditor.Models.DataHolders
 
             if (Layers.Count > 1)
             {
-                StorageBasedChange storageChange = new StorageBasedChange(this, new[] { Layers[^1] });
+                StorageBasedChange storageChange = new StorageBasedChange(this, new[] { Layers[^1] }, false);
                 UndoManager.AddUndoChange(
                     storageChange.ToChange(
                         RemoveLayerProcess,

+ 7 - 7
PixiEditor/Models/DataHolders/LayerChange.cs

@@ -1,24 +1,24 @@
-using PixiEditor.Models.Layers;
-using PixiEditor.ViewModels;
-
+using System;
+using PixiEditor.Models.Layers;
+
 namespace PixiEditor.Models.DataHolders
 {
     public class LayerChange
     {
-        public LayerChange(BitmapPixelChanges pixelChanges, int layerIndex)
+        public LayerChange(BitmapPixelChanges pixelChanges, Guid layerGuid)
         {
             PixelChanges = pixelChanges;
-            LayerIndex = layerIndex;
+            LayerGuid = layerGuid;
         }
 
         public LayerChange(BitmapPixelChanges pixelChanges, Layer layer)
         {
             PixelChanges = pixelChanges;
-            LayerIndex = ViewModelMain.Current.BitmapManager.ActiveDocument.Layers.IndexOf(layer);
+            LayerGuid = layer.LayerGuid;
         }
 
         public BitmapPixelChanges PixelChanges { get; set; }
 
-        public int LayerIndex { get; set; }
+        public Guid LayerGuid { get; set; }
     }
 }

+ 11 - 10
PixiEditor/Models/ImageManipulation/BitmapUtils.cs

@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.Windows.Media.Imaging;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
@@ -70,30 +71,30 @@ namespace PixiEditor.Models.ImageManipulation
             return finalBitmap;
         }
 
-        public static Dictionary<Layer, Color[]> GetPixelsForSelection(Layer[] layers, Coordinates[] selection)
+        public static Dictionary<Guid, Color[]> GetPixelsForSelection(IEnumerable<Layer> layers, Coordinates[] selection)
         {
-            Dictionary<Layer, Color[]> result = new Dictionary<Layer, Color[]>();
+            Dictionary<Guid, Color[]> result = new Dictionary<Guid, Color[]>();
 
-            for (int i = 0; i < layers.Length; i++)
+            foreach (Layer layer in layers)
             {
                 Color[] pixels = new Color[selection.Length];
 
-                using (layers[i].LayerBitmap.GetBitmapContext())
+                using (layer.LayerBitmap.GetBitmapContext())
                 {
                     for (int j = 0; j < pixels.Length; j++)
                     {
-                        Coordinates position = layers[i].GetRelativePosition(selection[j]);
-                        if (position.X < 0 || position.X > layers[i].Width - 1 || position.Y < 0 ||
-                            position.Y > layers[i].Height - 1)
+                        Coordinates position = layer.GetRelativePosition(selection[j]);
+                        if (position.X < 0 || position.X > layer.Width - 1 || position.Y < 0 ||
+                            position.Y > layer.Height - 1)
                         {
                             continue;
                         }
 
-                        pixels[j] = layers[i].GetPixel(position.X, position.Y);
+                        pixels[j] = layer.GetPixel(position.X, position.Y);
                     }
                 }
 
-                result[layers[i]] = pixels;
+                result[layer.LayerGuid] = pixels;
             }
 
             return result;

+ 2 - 0
PixiEditor/Models/Layers/BasicLayer.cs

@@ -29,5 +29,7 @@ namespace PixiEditor.Models.Layers
                 RaisePropertyChanged("Height");
             }
         }
+
+        public Guid LayerGuid { get; init; }
     }
 }

+ 3 - 4
PixiEditor/Models/Layers/Layer.cs

@@ -55,8 +55,6 @@ namespace PixiEditor.Models.Layers
 
         public Dictionary<Coordinates, Color> LastRelativeCoordinates { get; set; }
 
-        public Guid LayerGuid { get; init; }
-
         public string Name
         {
             get => name;
@@ -138,7 +136,7 @@ namespace PixiEditor.Models.Layers
         /// <summary>
         ///     Returns clone of layer.
         /// </summary>
-        public Layer Clone()
+        public Layer Clone(bool generateNewGuid = false)
         {
             return new Layer(Name, LayerBitmap.Clone())
             {
@@ -148,7 +146,8 @@ namespace PixiEditor.Models.Layers
                 MaxWidth = MaxWidth,
                 Opacity = Opacity,
                 IsActive = IsActive,
-                IsRenaming = IsRenaming
+                IsRenaming = IsRenaming,
+                LayerGuid = generateNewGuid ? Guid.NewGuid() : LayerGuid
             };
         }
 

+ 5 - 4
PixiEditor/Models/Tools/BitmapOperationTool.cs

@@ -1,4 +1,5 @@
-using System.Windows.Media;
+using System;
+using System.Windows.Media;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
@@ -11,7 +12,7 @@ namespace PixiEditor.Models.Tools
 
         public bool UseDefaultUndoMethod { get; set; } = true;
 
-        private readonly LayerChange[] onlyLayerArr = new LayerChange[] { new LayerChange(BitmapPixelChanges.Empty, 0) };
+        private readonly LayerChange[] onlyLayerArr = new LayerChange[] { new LayerChange(BitmapPixelChanges.Empty, Guid.Empty) };
 
         public abstract LayerChange[] Use(Layer layer, Coordinates[] mouseMove, Color color);
 
@@ -21,9 +22,9 @@ namespace PixiEditor.Models.Tools
             return onlyLayerArr;
         }
 
-        protected LayerChange[] Only(BitmapPixelChanges changes, int layerIndex)
+        protected LayerChange[] Only(BitmapPixelChanges changes, Guid layerGuid)
         {
-            onlyLayerArr[0] = new LayerChange(changes, layerIndex);
+            onlyLayerArr[0] = new LayerChange(changes, layerGuid);
             return onlyLayerArr;
         }
     }

+ 18 - 15
PixiEditor/Models/Tools/Tools/MoveTool.cs

@@ -20,12 +20,12 @@ namespace PixiEditor.Models.Tools.Tools
     public class MoveTool : BitmapOperationTool
     {
         private Layer[] affectedLayers;
-        private Dictionary<Layer, bool> clearedPixels = new Dictionary<Layer, bool>();
+        private Dictionary<Guid, bool> clearedPixels = new Dictionary<Guid, bool>();
         private Coordinates[] currentSelection;
         private Coordinates lastMouseMove;
         private Coordinates lastStartMousePos;
-        private Dictionary<Layer, Color[]> startPixelColors;
-        private Dictionary<Layer, Thickness> startingOffsets;
+        private Dictionary<Guid, Color[]> startPixelColors;
+        private Dictionary<Guid, Thickness> startingOffsets;
         private Coordinates[] startSelection;
         private bool updateViewModelSelection = true;
 
@@ -66,12 +66,12 @@ namespace PixiEditor.Models.Tools.Tools
                 {
                     BitmapPixelChanges beforeMovePixels = BitmapPixelChanges.FromArrays(startSelection, item.Value);
                     Change changes = undoManager.UndoStack.Peek();
-                    int layerIndex = ViewModelMain.Current.BitmapManager.ActiveDocument.Layers.IndexOf(item.Key);
+                    Guid layerGuid = item.Key;
 
-                    ((LayerChange[])changes.OldValue).First(x => x.LayerIndex == layerIndex).PixelChanges.ChangedPixels
+                    ((LayerChange[])changes.OldValue).First(x => x.LayerGuid == layerGuid).PixelChanges.ChangedPixels
                         .AddRangeOverride(beforeMovePixels.ChangedPixels);
 
-                    ((LayerChange[])changes.NewValue).First(x => x.LayerIndex == layerIndex).PixelChanges.ChangedPixels
+                    ((LayerChange[])changes.NewValue).First(x => x.LayerGuid == layerGuid).PixelChanges.ChangedPixels
                         .AddRangeNewOnly(BitmapPixelChanges
                             .FromSingleColoredArray(startSelection, System.Windows.Media.Colors.Transparent)
                             .ChangedPixels);
@@ -166,24 +166,26 @@ namespace PixiEditor.Models.Tools.Tools
             ClearSelectedPixels(layer, previousSelection);
 
             lastMouseMove = end;
-            return BitmapPixelChanges.FromArrays(currentSelection, startPixelColors[layer]);
+            return BitmapPixelChanges.FromArrays(currentSelection, startPixelColors[layer.LayerGuid]);
         }
 
         private void ApplyOffsets(object[] parameters)
         {
-            Dictionary<Layer, Thickness> offsets = (Dictionary<Layer, Thickness>)parameters[0];
+            Dictionary<Guid, Thickness> offsets = (Dictionary<Guid, Thickness>)parameters[0];
             foreach (var offset in offsets)
             {
-                offset.Key.Offset = offset.Value;
+                Layer layer = ViewModelMain.Current?.BitmapManager?.
+                    ActiveDocument?.Layers?.First(x => x.LayerGuid == offset.Key);
+                layer.Offset = offset.Value;
             }
         }
 
-        private Dictionary<Layer, Thickness> GetOffsets(Layer[] layers)
+        private Dictionary<Guid, Thickness> GetOffsets(Layer[] layers)
         {
-            Dictionary<Layer, Thickness> dict = new Dictionary<Layer, Thickness>();
+            Dictionary<Guid, Thickness> dict = new Dictionary<Guid, Thickness>();
             for (int i = 0; i < layers.Length; i++)
             {
-                dict.Add(layers[i], layers[i].Offset);
+                dict.Add(layers[i].LayerGuid, layers[i].Offset);
             }
 
             return dict;
@@ -203,7 +205,7 @@ namespace PixiEditor.Models.Tools.Tools
         {
             lastStartMousePos = start;
             lastMouseMove = start;
-            clearedPixels = new Dictionary<Layer, bool>();
+            clearedPixels = new Dictionary<Guid, bool>();
             updateViewModelSelection = true;
             startPixelColors = null;
             startSelection = null;
@@ -218,12 +220,13 @@ namespace PixiEditor.Models.Tools.Tools
 
         private void ClearSelectedPixels(Layer layer, Coordinates[] selection)
         {
-            if (!clearedPixels.ContainsKey(layer) || clearedPixels[layer] == false)
+            Guid layerGuid = layer.LayerGuid;
+            if (!clearedPixels.ContainsKey(layerGuid) || clearedPixels[layerGuid] == false)
             {
                 ViewModelMain.Current.BitmapManager.ActiveDocument.Layers.First(x => x == layer)
                     .SetPixels(BitmapPixelChanges.FromSingleColoredArray(selection, System.Windows.Media.Colors.Transparent));
 
-                clearedPixels[layer] = true;
+                clearedPixels[layerGuid] = true;
             }
         }
     }

+ 19 - 13
PixiEditor/Models/Undo/StorageBasedChange.cs

@@ -22,14 +22,14 @@ namespace PixiEditor.Models.Undo
 
         public UndoLayer[] StoredLayers { get; set; }
 
-        private IEnumerable<Layer> layersToStore;
+        private IEnumerable<Guid> layersToStore;
 
         private Document document;
 
         public StorageBasedChange(Document doc, IEnumerable<Layer> layers, bool saveOnStartup = true)
         {
             document = doc;
-            layersToStore = layers;
+            layersToStore = layers.Select(x => x.LayerGuid);
             UndoChangeLocation = DefaultUndoChangeLocation;
             GenerateUndoLayers();
             if (saveOnStartup)
@@ -41,7 +41,7 @@ namespace PixiEditor.Models.Undo
         public StorageBasedChange(Document doc, IEnumerable<Layer> layers, string undoChangeLocation, bool saveOnStartup = true)
         {
             document = doc;
-            layersToStore = layers;
+            layersToStore = layers.Select(x => x.LayerGuid);
             UndoChangeLocation = undoChangeLocation;
             GenerateUndoLayers();
 
@@ -54,8 +54,9 @@ namespace PixiEditor.Models.Undo
         public void SaveLayersOnDevice()
         {
             int i = 0;
-            foreach (var layer in layersToStore)
+            foreach (var layerGuid in layersToStore)
             {
+                Layer layer = document.Layers.First(x => x.LayerGuid == layerGuid);
                 UndoLayer storedLayer = StoredLayers[i];
                 if (Directory.Exists(Path.GetDirectoryName(storedLayer.StoredPngLayerName)))
                 {
@@ -65,7 +66,7 @@ namespace PixiEditor.Models.Undo
                 i++;
             }
 
-            layersToStore = Array.Empty<Layer>();
+            layersToStore = Array.Empty<Guid>();
         }
 
         /// <summary>
@@ -87,13 +88,15 @@ namespace PixiEditor.Models.Undo
                     MaxHeight = storedLayer.MaxHeight,
                     IsVisible = storedLayer.IsVisible,
                     IsActive = storedLayer.IsActive,
-                    LayerGuid = storedLayer.LayerGuid
+                    LayerGuid = storedLayer.LayerGuid,
+                    Width = storedLayer.Width,
+                    Height = storedLayer.Height,
                 };
 
                 File.Delete(StoredLayers[i].StoredPngLayerName);
             }
 
-            layersToStore = layers;
+            layersToStore = layers.Select(x => x.LayerGuid);
             return layers;
         }
 
@@ -113,13 +116,13 @@ namespace PixiEditor.Models.Undo
                 undoProcess(layers, StoredLayers);
             };
 
-            Action<object[]> fianlRedoProcess = parameters =>
+            Action<object[]> finalRedoProcess = parameters =>
             {
                 SaveLayersOnDevice();
                 redoProcess(parameters);
             };
 
-            return new Change(finalUndoProcess, null, fianlRedoProcess, redoProcessParameters, description);
+            return new Change(finalUndoProcess, null, finalRedoProcess, redoProcessParameters, description);
         }
 
         /// <summary>
@@ -138,13 +141,13 @@ namespace PixiEditor.Models.Undo
                 undoProcess(parameters);
             };
 
-            Action<object[]> fianlRedoProcess = parameters =>
+            Action<object[]> finalRedoProcess = parameters =>
             {
                 Layer[] layers = LoadLayersFromDevice();
                 redoProcess(layers, StoredLayers);
             };
 
-            return new Change(finalUndoProcess, undoProcessParameters, fianlRedoProcess, null, description);
+            return new Change(finalUndoProcess, undoProcessParameters, finalRedoProcess, null, description);
         }
 
         /// <summary>
@@ -154,15 +157,18 @@ namespace PixiEditor.Models.Undo
         {
             StoredLayers = new UndoLayer[layersToStore.Count()];
             int i = 0;
-            foreach (var layer in layersToStore)
+            foreach (var layerGuid in layersToStore)
             {
+                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);
-                string pngName = layer.Name + index + Guid.NewGuid().ToString();
+                string pngName = layer.Name + Guid.NewGuid().ToString();
                 StoredLayers[i] = new UndoLayer(
                     Path.Join(
                         UndoChangeLocation,

+ 1 - 0
PixiEditor/Models/Undo/UndoLayer.cs

@@ -23,6 +23,7 @@ namespace PixiEditor.Models.Undo
         public int MaxHeight { get; set; }
 
         public bool IsVisible { get; set; }
+
         public bool IsActive { get; set; }
 
         public int OffsetX { get; set; }

+ 1 - 1
PixiEditor/ViewModels/SubViewModels/Main/UndoViewModel.cs

@@ -25,7 +25,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
                 undoChanges = value;
                 for (int i = 0; i < value.Length; i++)
                 {
-                    Owner.BitmapManager.ActiveDocument.Layers[value[i].LayerIndex].SetPixels(value[i].PixelChanges);
+                    Owner.BitmapManager.ActiveDocument.Layers.First(x => x.LayerGuid == value[i].LayerGuid).SetPixels(value[i].PixelChanges);
                 }
             }
         }

+ 2 - 2
PixiEditor/ViewModels/ViewModelMain.cs

@@ -250,8 +250,8 @@ namespace PixiEditor.ViewModels
         private void BitmapUtility_BitmapChanged(object sender, BitmapChangedEventArgs e)
         {
             ChangesController.AddChanges(
-                new LayerChange(e.PixelsChanged, e.ChangedLayerIndex),
-                new LayerChange(e.OldPixelsValues, e.ChangedLayerIndex));
+                new LayerChange(e.PixelsChanged, e.ChangedLayerGuid),
+                new LayerChange(e.OldPixelsValues, e.ChangedLayerGuid));
             BitmapManager.ActiveDocument.ChangesSaved = false;
             if (BitmapManager.IsOperationTool())
             {

+ 3 - 3
PixiEditorTests/ModelsTests/ControllersTests/MockedSinglePixelPenTool.cs

@@ -1,8 +1,8 @@
-using PixiEditor.Models.DataHolders;
+using System.Windows.Media;
+using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools;
-using System.Windows.Media;
 
 namespace PixiEditorTests.ModelsTests.ControllersTests
 {
@@ -10,7 +10,7 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
     {
         public override LayerChange[] Use(Layer layer, Coordinates[] mouseMove, Color color)
         {
-            return Only(BitmapPixelChanges.FromSingleColoredArray(new[] { mouseMove[0] }, color), 0);
+            return Only(BitmapPixelChanges.FromSingleColoredArray(new[] { mouseMove[0] }, color), layer.LayerGuid);
         }
     }
 }

+ 19 - 13
PixiEditorTests/ModelsTests/ControllersTests/PixelChangesControllerTests.cs

@@ -1,4 +1,5 @@
-using System.Windows.Media;
+using System;
+using System.Windows.Media;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Position;
@@ -11,7 +12,7 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
         [Fact]
         public void TestThatPopChangesPopsChanges()
         {
-            PixelChangesController controller = CreateBasicController();
+            PixelChangesController controller = CreateBasicController().Item2;
 
             System.Tuple<LayerChange, LayerChange>[] changes = controller.PopChanges();
             Assert.NotEmpty(changes);
@@ -21,13 +22,15 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
         [Fact]
         public void TestThatAddChangesAddsAsNewChange()
         {
-            PixelChangesController controller = CreateBasicController();
+            var data = CreateBasicController();
+            PixelChangesController controller = data.Item2;
             Coordinates[] cords = { new Coordinates(5, 3), new Coordinates(7, 2) };
+            Guid guid = Guid.NewGuid();
 
             controller.AddChanges(
                 new LayerChange(
-                    BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Black), 1),
-                new LayerChange(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Transparent), 1));
+                    BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Black), guid),
+                new LayerChange(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Transparent), guid));
 
             System.Tuple<LayerChange, LayerChange>[] changes = controller.PopChanges();
             Assert.Equal(2, changes.Length);
@@ -37,29 +40,32 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
         public void TestThatAddChangesAddsToExistingChange()
         {
             Coordinates[] cords2 = { new Coordinates(2, 2), new Coordinates(5, 5) };
-            PixelChangesController controller = CreateBasicController();
+            var data = CreateBasicController();
+            PixelChangesController controller = data.Item2;
 
             controller.AddChanges(
                 new LayerChange(
-                    BitmapPixelChanges.FromSingleColoredArray(cords2, Colors.Black), 0),
-                new LayerChange(BitmapPixelChanges.FromSingleColoredArray(cords2, Colors.Transparent), 0));
+                    BitmapPixelChanges.FromSingleColoredArray(cords2, Colors.Black), data.Item1),
+                new LayerChange(BitmapPixelChanges.FromSingleColoredArray(cords2, Colors.Transparent), data.Item1));
 
-            System.Tuple<LayerChange, LayerChange>[] changes = controller.PopChanges();
+            Tuple<LayerChange, LayerChange>[] changes = controller.PopChanges();
             Assert.Single(changes);
             Assert.Equal(4, changes[0].Item1.PixelChanges.ChangedPixels.Count);
             Assert.Equal(4, changes[0].Item2.PixelChanges.ChangedPixels.Count);
         }
 
-        private static PixelChangesController CreateBasicController()
+        private static Tuple<Guid, PixelChangesController> CreateBasicController()
         {
             Coordinates[] cords = { new Coordinates(0, 0), new Coordinates(1, 1) };
             PixelChangesController controller = new PixelChangesController();
 
+            Guid guid = Guid.NewGuid();
+
             controller.AddChanges(
                 new LayerChange(
-                    BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Black), 0),
-                new LayerChange(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Transparent), 0));
-            return controller;
+                    BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Black), guid),
+                new LayerChange(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Transparent), guid));
+            return new Tuple<Guid, PixelChangesController>(guid, controller);
         }
     }
 }

+ 4 - 3
PixiEditorTests/ModelsTests/ImageManipulationTests/BitmapUtilsTests.cs

@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.Linq;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
@@ -78,11 +79,11 @@ namespace PixiEditorTests.ModelsTests.ImageManipulationTests
             layers[0].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new[] { cords[0] }, Colors.Green));
             layers[1].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new[] { cords[1] }, Colors.Red));
 
-            Dictionary<Layer, Color[]> output = BitmapUtils.GetPixelsForSelection(layers, cords);
+            Dictionary<Guid, Color[]> output = BitmapUtils.GetPixelsForSelection(layers, cords);
 
             List<Color> colors = new List<Color>();
 
-            foreach (KeyValuePair<Layer, Color[]> layerColor in output.ToArray())
+            foreach (KeyValuePair<Guid, Color[]> layerColor in output.ToArray())
             {
                 foreach (Color color in layerColor.Value)
                 {