Browse Source

Fixed some more bugs

flabbet 4 years ago
parent
commit
591fb6aaf9

+ 1 - 1
PixiEditor/Models/DataHolders/Document/Document.Constructors.cs

@@ -19,7 +19,7 @@ namespace PixiEditor.Models.DataHolders
         {
             SetRelayCommands();
             UndoManager = new UndoManager();
-            LayerStructure = new Layers.LayerStructure(this);
+            LayerStructure = new LayerStructure(this);
             XamlAccesibleViewModel = ViewModelMain.Current;
             GeneratePreviewLayer();
             Layers.CollectionChanged += Layers_CollectionChanged;

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

@@ -141,6 +141,11 @@ namespace PixiEditor.Models.DataHolders
 
         public UndoManager UndoManager { get; set; }
 
+        public void RaisePropertyChange(string name)
+        {
+            RaisePropertyChanged(name);
+        }
+
         public void CenterViewport()
         {
             RecenterZoombox = false; // It's a trick to trigger change in UserControl

+ 1 - 5
PixiEditor/Models/Layers/GuidStructureItem.cs

@@ -59,11 +59,7 @@ namespace PixiEditor.Models.Layers
         public bool IsExpanded
         {
             get => isExpanded;
-            set
-            {
-                isExpanded = value;
-                RaisePropertyChanged(nameof(IsExpanded));
-            }
+            set => SetProperty(ref isExpanded, value);
         }
 
         private bool isRenaming = false;

+ 31 - 1
PixiEditor/Models/Layers/LayerStructure.cs

@@ -2,7 +2,7 @@
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Linq;
-using PixiEditor.Helpers.Extensions;
+using PixiEditor.Helpers;
 using PixiEditor.Models.DataHolders;
 
 namespace PixiEditor.Models.Layers
@@ -318,6 +318,36 @@ namespace PixiEditor.Models.Layers
             return layers;
         }
 
+        /// <summary>
+        /// Sets parent groups IsExpanded to true.
+        /// </summary>
+        /// <param name="layerGuid">Guid of layer which parents will be affected.</param>
+        public void ExpandParentGroups(Guid layerGuid)
+        {
+            var group = GetGroupByLayer(layerGuid);
+
+            while (group != null)
+            {
+                group.IsExpanded = true;
+                group = group.Parent;
+            }
+        }
+
+        /// <summary>
+        /// Sets parent groups IsExpanded to true.
+        /// </summary>
+        /// <param name="group">Group which parents will be affected.</param>
+        public void ExpandParentGroups(GuidStructureItem group)
+        {
+            GuidStructureItem currentGroup = group;
+
+            while (currentGroup != null)
+            {
+                currentGroup.IsExpanded = true;
+                currentGroup = currentGroup.Parent;
+            }
+        }
+
         /// <summary>
         /// Gets all layers inside group, including nested groups.
         /// </summary>

+ 10 - 4
PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs

@@ -5,6 +5,7 @@ using System.Windows.Input;
 using PixiEditor.Helpers;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.Layers;
+using PixiEditor.Models.Layers.Utils;
 
 namespace PixiEditor.ViewModels.SubViewModels.Main
 {
@@ -148,9 +149,11 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             Guid lastActiveLayerGuid = doc.ActiveLayerGuid;
 
             doc.AddNewLayer($"New Layer {Owner.BitmapManager.ActiveDocument.Layers.Count}");
+
             if (doc.Layers.Count > 1)
             {
                 doc.MoveLayerInStructure(doc.Layers[^1].LayerGuid, lastActiveLayerGuid, true);
+                doc.UndoManager.UndoStack.Pop();
             }
         }
 
@@ -163,22 +166,24 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         {
             int index = (int)parameter;
 
-            if (Owner.BitmapManager.ActiveDocument.Layers[index].IsActive && Mouse.RightButton == MouseButtonState.Pressed)
+            var doc = Owner.BitmapManager.ActiveDocument;
+
+            if (doc.Layers[index].IsActive && Mouse.RightButton == MouseButtonState.Pressed)
             {
                 return;
             }
 
             if (Keyboard.IsKeyDown(Key.LeftCtrl))
             {
-                Owner.BitmapManager.ActiveDocument.ToggleLayer(index);
+                doc.ToggleLayer(index);
             }
             else if (Keyboard.IsKeyDown(Key.LeftShift) && Owner.BitmapManager.ActiveDocument.Layers.Any(x => x.IsActive))
             {
-                Owner.BitmapManager.ActiveDocument.SelectLayersRange(index);
+                doc.SelectLayersRange(index);
             }
             else
             {
-                Owner.BitmapManager.ActiveDocument.SetMainActiveLayer(index);
+                doc.SetMainActiveLayer(index);
             }
         }
 
@@ -317,6 +322,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             if (e.LayerChangeType == Models.Enums.LayerAction.SetActive)
             {
                 Owner.BitmapManager.ActiveDocument.UpdateLayersColor();
+                Owner.BitmapManager.ActiveDocument.LayerStructure.ExpandParentGroups(e.LayerAffectedGuid);
             }
             else
             {

+ 3 - 2
PixiEditor/Views/UserControls/LayerGroupControl.xaml.cs

@@ -58,8 +58,9 @@ namespace PixiEditor.Views.UserControls
 
         // Using a DependencyProperty as the backing store for GroupOpacity.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty GroupOpacityProperty =
-            DependencyProperty.Register("GroupOpacity", typeof(float), typeof(LayerGroupControl), new PropertyMetadata(1f));
-
+            DependencyProperty.Register("GroupOpacity", typeof(float), typeof(LayerGroupControl), new PropertyMetadata(1f));
+
+
         private static void LayersViewModelCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
         {
             LayerGroupControl control = (LayerGroupControl)d;

+ 17 - 4
PixiEditor/Views/UserControls/LayersManager.xaml.cs

@@ -6,6 +6,7 @@ using System.Windows.Controls;
 using System.Windows.Media;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.Layers;
+using PixiEditor.Models.Layers.Utils;
 using PixiEditor.Models.Undo;
 using PixiEditor.ViewModels.SubViewModels.Main;
 
@@ -69,14 +70,14 @@ namespace PixiEditor.Views.UserControls
                         doc.AddPropertyChangedCallback(nameof(doc.ActiveLayer), () =>
                         {
                             manager.cachedItem = doc.ActiveLayer;
-                            SetInputOpacity(manager.cachedItem, manager.numberInput);
+                            manager.SetInputOpacity(manager.cachedItem);
                         });
                     }
                 });
             }
         }
 
-        private static void SetInputOpacity(object item, NumberInput numberInput)
+        private void SetInputOpacity(object item)
         {
             if (item is Layer layer)
             {
@@ -111,6 +112,8 @@ namespace PixiEditor.Views.UserControls
 
                 ChangeGroupOpacityProcess(processArgs);
 
+                LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.LayerStructure.ExpandParentGroups(group);
+
                 doc.UndoManager.AddUndoChange(
                 new Change(
                     ChangeGroupOpacityProcess,
@@ -151,8 +154,18 @@ namespace PixiEditor.Views.UserControls
             if (item is Layer layer)
             {
                 float oldOpacity = layer.Opacity;
+
+                var doc = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument;
+
                 layer.OpacityUndoTriggerable = val;
-                UndoManager undoManager = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.UndoManager;
+
+                doc.LayerStructure.ExpandParentGroups(layer.LayerGuid);
+
+                doc.RaisePropertyChange(nameof(doc.LayerStructure));
+
+                UndoManager undoManager = doc.UndoManager;
+
+
                 undoManager.AddUndoChange(
                     new Change(
                         UpdateNumberInputLayerOpacityProcess,
@@ -181,7 +194,7 @@ namespace PixiEditor.Views.UserControls
 
         private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
         {
-            SetInputOpacity(cachedItem, numberInput);
+            SetInputOpacity(cachedItem);
         }
 
         private void TreeView_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)