Переглянути джерело

Groups now wrap other groups when creating them

flabbet 4 роки тому
батько
коміт
57dd90ddff

+ 28 - 3
PixiEditor/Models/Layers/LayerStructure.cs

@@ -140,6 +140,32 @@ namespace PixiEditor.Models.Layers
             return group;
         }
 
+        public GuidStructureItem AddNewGroup(string groupName, GuidStructureItem childGroup)
+        {
+            if (childGroup == null)
+            {
+                throw new ArgumentException("Child group can't be null.");
+            }
+            GuidStructureItem group = new($"{childGroup.Name} (1)", childGroup.StartLayerGuid, childGroup.EndLayerGuid, new[] { childGroup }, childGroup.Parent) { IsExpanded = true };
+            if (childGroup.Parent == null)
+            {
+                Groups.Add(group);
+                Groups.Remove(childGroup);
+            }
+            else
+            {
+                childGroup.Parent.Subgroups.Add(group);
+                childGroup.Parent.Subgroups.Remove(childGroup);
+            }
+
+            childGroup.Parent = group;
+
+            group.GroupsChanged += Group_GroupsChanged;
+
+            LayerStructureChanged?.Invoke(this, new LayerStructureChangedEventArgs(GetGroupLayerGuids(group)));
+            return group;
+        }
+
 #nullable enable
         /// <summary>
         /// Moves group and it's children from one index to another. This method makes changes in <see cref="Document"/> Layers.
@@ -639,9 +665,8 @@ namespace PixiEditor.Models.Layers
         {
             foreach (var currentGroup in groups)
             {
-                var endLayer = Owner.Layers.FirstOrDefault(x => x.LayerGuid == currentGroup.EndLayerGuid);
-                var startLayer = Owner.Layers.FirstOrDefault(x => x.LayerGuid == currentGroup.StartLayerGuid);
-                if (endLayer == null || startLayer == null) continue;
+                var endLayer = Owner.Layers.First(x => x.LayerGuid == currentGroup.EndLayerGuid);
+                var startLayer = Owner.Layers.First(x => x.LayerGuid == currentGroup.StartLayerGuid);
 
                 int topIndex = Owner.Layers.IndexOf(endLayer);
                 int bottomIndex = Owner.Layers.IndexOf(startLayer);

+ 12 - 3
PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs

@@ -72,7 +72,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         public bool CanDeleteSelected(object parameter)
         {
-            return (parameter is not null and(Layer or LayerGroup)) || (Owner.BitmapManager?.ActiveDocument?.ActiveLayer != null);
+            return (parameter is not null and (Layer or LayerGroup)) || (Owner.BitmapManager?.ActiveDocument?.ActiveLayer != null);
         }
 
         public void DeleteSelected(object parameter)
@@ -121,13 +121,22 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         public void NewGroup(object parameter)
         {
+            GuidStructureItem control = GetGroupFromParameter(parameter);
             var doc = Owner.BitmapManager.ActiveDocument;
             if (doc != null)
             {
                 var lastGroups = doc.LayerStructure.CloneGroups();
-                GuidStructureItem group = doc.LayerStructure.AddNewGroup($"{doc.ActiveLayer.Name} Group", doc.ActiveLayer.LayerGuid);
+                if (parameter is Layer or LayerStructureItemContainer)
+                {
+                    GuidStructureItem group = doc.LayerStructure.AddNewGroup($"{doc.ActiveLayer.Name} Group", doc.ActiveLayer.LayerGuid);
+
+                    Owner.BitmapManager.ActiveDocument.LayerStructure.ExpandParentGroups(group);
+                }
+                else if(control != null)
+                {
+                    doc.LayerStructure.AddNewGroup($"{doc.ActiveLayer.Name} Group", control);
+                }
 
-                Owner.BitmapManager.ActiveDocument.LayerStructure.ExpandParentGroups(group);
                 doc.AddLayerStructureToUndo(lastGroups);
                 doc.RaisePropertyChange(nameof(doc.LayerStructure));
             }