Browse Source

Changed ActiveLayerIndex to ActiveLayerGuid, more reliable

flabbet 4 years ago
parent
commit
1df296f3dd

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

@@ -5,13 +5,13 @@ namespace PixiEditor.Models.Controllers
 {
 {
     public class LayersChangedEventArgs : EventArgs
     public class LayersChangedEventArgs : EventArgs
     {
     {
-        public LayersChangedEventArgs(int layerAffected, LayerAction layerChangeType)
+        public LayersChangedEventArgs(Guid layerAffectedGuid, LayerAction layerChangeType)
         {
         {
-            LayerAffected = layerAffected;
+            LayerAffectedGuid = layerAffectedGuid;
             LayerChangeType = layerChangeType;
             LayerChangeType = layerChangeType;
         }
         }
 
 
-        public int LayerAffected { get; set; }
+        public Guid LayerAffectedGuid { get; set; }
 
 
         public LayerAction LayerChangeType { get; set; }
         public LayerAction LayerChangeType { get; set; }
     }
     }

+ 20 - 19
PixiEditor/Models/DataHolders/Document/Document.Layers.cs

@@ -17,19 +17,19 @@ namespace PixiEditor.Models.DataHolders
     {
     {
         public const string MainSelectedLayerColor = "#505056";
         public const string MainSelectedLayerColor = "#505056";
         public const string SecondarySelectedLayerColor = "#7D505056";
         public const string SecondarySelectedLayerColor = "#7D505056";
-        private int activeLayerIndex;
+        private Guid activeLayerGuid;
 
 
         public ObservableCollection<Layer> Layers { get; set; } = new ObservableCollection<Layer>();
         public ObservableCollection<Layer> Layers { get; set; } = new ObservableCollection<Layer>();
 
 
-        public Layer ActiveLayer => Layers.Count > 0 ? Layers[ActiveLayerIndex] : null;
+        public Layer ActiveLayer => Layers.Count > 0 ? Layers.FirstOrDefault(x => x.LayerGuid == ActiveLayerGuid) : null;
 
 
-        public int ActiveLayerIndex
+        public Guid ActiveLayerGuid
         {
         {
-            get => activeLayerIndex;
+            get => activeLayerGuid;
             set
             set
             {
             {
-                activeLayerIndex = value;
-                RaisePropertyChanged(nameof(ActiveLayerIndex));
+                activeLayerGuid = value;
+                RaisePropertyChanged(nameof(ActiveLayerGuid));
                 RaisePropertyChanged(nameof(ActiveLayer));
                 RaisePropertyChanged(nameof(ActiveLayer));
             }
             }
         }
         }
@@ -38,7 +38,7 @@ namespace PixiEditor.Models.DataHolders
 
 
         public void SetMainActiveLayer(int index)
         public void SetMainActiveLayer(int index)
         {
         {
-            if (ActiveLayerIndex <= Layers.Count - 1)
+            if (ActiveLayer != null && Layers.IndexOf(ActiveLayer) <= Layers.Count - 1)
             {
             {
                 ActiveLayer.IsActive = false;
                 ActiveLayer.IsActive = false;
             }
             }
@@ -51,17 +51,16 @@ namespace PixiEditor.Models.DataHolders
                 }
                 }
             }
             }
 
 
-            ActiveLayerIndex = index;
+            ActiveLayerGuid = Layers[index].LayerGuid;
             ActiveLayer.IsActive = true;
             ActiveLayer.IsActive = true;
-            LayersChanged?.Invoke(this, new LayersChangedEventArgs(index, LayerAction.SetActive));
+            LayersChanged?.Invoke(this, new LayersChangedEventArgs(ActiveLayerGuid, LayerAction.SetActive));
         }
         }
 
 
         public void UpdateLayersColor()
         public void UpdateLayersColor()
         {
         {
-            int index = 0;
             foreach (var layer in Layers)
             foreach (var layer in Layers)
             {
             {
-                if (index == ActiveLayerIndex)
+                if (layer.LayerGuid == ActiveLayerGuid)
                 {
                 {
                     layer.LayerHighlightColor = MainSelectedLayerColor;
                     layer.LayerHighlightColor = MainSelectedLayerColor;
                 }
                 }
@@ -69,8 +68,6 @@ namespace PixiEditor.Models.DataHolders
                 {
                 {
                     layer.LayerHighlightColor = SecondarySelectedLayerColor;
                     layer.LayerHighlightColor = SecondarySelectedLayerColor;
                 }
                 }
-
-                index++;
             }
             }
         }
         }
 
 
@@ -120,7 +117,7 @@ namespace PixiEditor.Models.DataHolders
                         "Add layer"));
                         "Add layer"));
             }
             }
 
 
-            LayersChanged?.Invoke(this, new LayersChangedEventArgs(0, LayerAction.Add));
+            LayersChanged?.Invoke(this, new LayersChangedEventArgs(Layers[0].LayerGuid, LayerAction.Add));
         }
         }
 
 
         public void SetNextLayerAsActive(int lastLayerIndex)
         public void SetNextLayerAsActive(int lastLayerIndex)
@@ -145,8 +142,8 @@ namespace PixiEditor.Models.DataHolders
             {
             {
                 if (layer.LayerGuid != lastLayerGuid)
                 if (layer.LayerGuid != lastLayerGuid)
                 {
                 {
-                    ActiveLayerIndex = Layers.IndexOf(layer);
-                    LayersChanged?.Invoke(this, new LayersChangedEventArgs(ActiveLayerIndex, LayerAction.SetActive));
+                    ActiveLayerGuid = layer.LayerGuid;
+                    LayersChanged?.Invoke(this, new LayersChangedEventArgs(ActiveLayerGuid, LayerAction.SetActive));
                     return;
                     return;
                 }
                 }
             }
             }
@@ -162,7 +159,7 @@ namespace PixiEditor.Models.DataHolders
                     return;
                     return;
                 }
                 }
 
 
-                if (ActiveLayerIndex == Layers.IndexOf(layer))
+                if (ActiveLayerGuid == layer.LayerGuid)
                 {
                 {
                     SetNextSelectedLayerAsActive(layer.LayerGuid);
                     SetNextSelectedLayerAsActive(layer.LayerGuid);
                 }
                 }
@@ -365,8 +362,12 @@ namespace PixiEditor.Models.DataHolders
                 for (int i = 0; i < layers.Length; i++)
                 for (int i = 0; i < layers.Length; i++)
                 {
                 {
                     Layer layer = layers[i];
                     Layer layer = layers[i];
+                    layer.IsActive = true;
                     Layers.Insert(data[i].LayerIndex, layer);
                     Layers.Insert(data[i].LayerIndex, layer);
                 }
                 }
+
+                ActiveLayerGuid = layers.First(x => x.LayerHighlightColor == MainSelectedLayerColor).LayerGuid;
+                // Identifying main layer by highlightColor is a bit hacky, but shhh
             }
             }
         }
         }
 
 
@@ -394,7 +395,7 @@ namespace PixiEditor.Models.DataHolders
             int amount = (int)parameter[1];
             int amount = (int)parameter[1];
 
 
             Layers.Move(layerIndex, layerIndex + amount);
             Layers.Move(layerIndex, layerIndex + amount);
-            if (ActiveLayerIndex == layerIndex)
+            if (Layers.IndexOf(ActiveLayer) == layerIndex)
             {
             {
                 SetMainActiveLayer(layerIndex + amount);
                 SetMainActiveLayer(layerIndex + amount);
             }
             }
@@ -423,7 +424,7 @@ namespace PixiEditor.Models.DataHolders
                 bool wasActive = layer.IsActive;
                 bool wasActive = layer.IsActive;
                 Layers.Remove(layer);
                 Layers.Remove(layer);
 
 
-                if (wasActive || ActiveLayerIndex >= index)
+                if (wasActive || Layers.IndexOf(ActiveLayer) >= index)
                 {
                 {
                     SetNextLayerAsActive(index);
                     SetNextLayerAsActive(index);
                 }
                 }

+ 2 - 2
PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs

@@ -104,10 +104,10 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
 
             if (index == null)
             if (index == null)
             {
             {
-                index = Owner.BitmapManager.ActiveDocument.ActiveLayerIndex;
+                index = Owner.BitmapManager.ActiveDocument.Layers.IndexOf(Owner.BitmapManager.ActiveDocument.ActiveLayer);
             }
             }
 
 
-            Owner.BitmapManager.ActiveDocument.Layers[index.Value].IsRenaming = true;
+            Owner.BitmapManager.ActiveDocument.Layers[(int)index].IsRenaming = true;
         }
         }
 
 
         public bool CanRenameLayer(object parameter)
         public bool CanRenameLayer(object parameter)

+ 1 - 1
PixiEditor/ViewModels/ViewModelMain.cs

@@ -143,7 +143,7 @@ namespace PixiEditor.ViewModels
                     new Shortcut(Key.N, FileSubViewModel.OpenNewFilePopupCommand, modifier: ModifierKeys.Control),
                     new Shortcut(Key.N, FileSubViewModel.OpenNewFilePopupCommand, modifier: ModifierKeys.Control),
 
 
                     // Layers
                     // Layers
-                    new Shortcut(Key.F2, LayersSubViewModel.RenameLayerCommand, BitmapManager.ActiveDocument?.ActiveLayerIndex),
+                    new Shortcut(Key.F2, LayersSubViewModel.RenameLayerCommand, BitmapManager.ActiveDocument?.ActiveLayerGuid),
 
 
                     // View
                     // View
                     new Shortcut(Key.OemTilde, ViewportSubViewModel.ToggleGridLinesCommand, modifier: ModifierKeys.Control)
                     new Shortcut(Key.OemTilde, ViewportSubViewModel.ToggleGridLinesCommand, modifier: ModifierKeys.Control)

+ 1 - 1
PixiEditorTests/ModelsTests/DataHoldersTests/DocumentLayersTests.cs

@@ -148,7 +148,7 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
 
 
             document.SetNextSelectedLayerAsActive(document.Layers[1].LayerGuid);
             document.SetNextSelectedLayerAsActive(document.Layers[1].LayerGuid);
 
 
-            Assert.Equal(0, document.ActiveLayerIndex);
+            Assert.Equal(document.Layers[0].LayerGuid, document.ActiveLayerGuid);
         }
         }
     }
     }
 }
 }