Răsfoiți Sursa

Fixed move above group layer bug and create group from multiple wip

flabbet 4 ani în urmă
părinte
comite
e86ed98324

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

@@ -1,4 +1,5 @@
 using System;
+using System.Buffers;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Linq;
@@ -536,7 +537,7 @@ namespace PixiEditor.Models.DataHolders
                 newIndex++;
             }
 
-            return newIndex;
+            return Math.Clamp(newIndex, 0, Layers.Count - 1);
         }
 
         private void MoveLayerInStructureProcess(object[] parameter)

+ 79 - 9
PixiEditor/Models/Layers/LayerStructure.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Linq;
+using System.Security.Cryptography;
 using System.Windows;
 using PixiEditor.Helpers;
 using PixiEditor.Models.DataHolders;
@@ -31,7 +32,45 @@ namespace PixiEditor.Models.Layers
             return GetGroupByLayer(layerGuid, Groups);
         }
 
-        public void AddNewGroup(string groupName, Guid childLayer)
+        public GuidStructureItem AddNewGroup(string groupName, IEnumerable<Layer> layers, Guid activeLayer)
+        {
+            var activeLayerParent = GetGroupByLayer(activeLayer);
+
+            List<GuidStructureItem> sameLevelGroups = new List<GuidStructureItem>();
+
+            var group = AddNewGroup(groupName, activeLayer);
+
+            if (activeLayerParent == null)
+            {
+                sameLevelGroups.AddRange(Groups);
+            }
+            else
+            {
+                sameLevelGroups.AddRange(activeLayerParent.Subgroups);
+            }
+
+            sameLevelGroups.Remove(group);
+            group.Subgroups = new ObservableCollection<GuidStructureItem>(sameLevelGroups);
+
+            sameLevelGroups = new(sameLevelGroups.Where(x => IsChildOf(activeLayer, x)));
+
+            Guid lastLayer = activeLayer;
+
+            foreach (var layer in layers)
+            {
+                if (layer.LayerGuid == activeLayer)
+                {
+                    continue;
+                }
+
+                Owner.MoveLayerInStructure(layer.LayerGuid, lastLayer, false);
+                lastLayer = layer.LayerGuid;
+            }
+
+            return group;
+        }
+
+        public GuidStructureItem AddNewGroup(string groupName, Guid childLayer)
         {
             var parent = GetGroupByLayer(childLayer);
             GuidStructureItem group = new (groupName, childLayer);
@@ -48,9 +87,9 @@ namespace PixiEditor.Models.Layers
             group.GroupsChanged += Group_GroupsChanged;
 
             LayerStructureChanged?.Invoke(this, EventArgs.Empty);
+            return group;
         }
 
-
 #nullable enable
         public void MoveGroup(Guid groupGuid, GuidStructureItem? parentGroup, int newIndex)
         {
@@ -175,6 +214,37 @@ namespace PixiEditor.Models.Layers
             return false;
         }
 
+        /// <summary>
+        /// Checks if layer is nested inside parent group.
+        /// </summary>
+        /// <param name="layerGuid">Layer GUID to check.</param>
+        /// <param name="parent">Parent of that group.</param>
+        /// <returns>True if layer is nested inside parent, false if not.</returns>
+        public bool IsChildOf(Guid layerGuid, GuidStructureItem parent)
+        {
+            var layerParent = GetGroupByLayer(layerGuid);
+
+            if(layerParent == parent)
+            {
+                return true;
+            }
+            else
+            {
+                GuidStructureItem nextParent = parent;
+                while (nextParent.Parent != null)
+                {
+                    if(nextParent == parent)
+                    {
+                        return true;
+                    }
+
+                    nextParent = nextParent.Parent;
+                }
+            }
+
+            return false;
+        }
+
         public void PostMoveReassignBounds(GuidStructureItem? parentGroup, Guid layerGuid)
         {
             if (parentGroup != null)
@@ -400,17 +470,17 @@ namespace PixiEditor.Models.Layers
         }
 
 #nullable disable
-        private GuidStructureItem GetGroupByLayer(Guid layerGuid, IEnumerable<GuidStructureItem> folders)
+        private GuidStructureItem GetGroupByLayer(Guid layerGuid, IEnumerable<GuidStructureItem> groups)
         {
-            foreach (var folder in folders)
+            foreach (var currentGroup in groups)
             {
-                int topIndex = Owner.Layers.IndexOf(Owner.Layers.First(x => x.LayerGuid == folder.EndLayerGuid));
-                int bottomIndex = Owner.Layers.IndexOf(Owner.Layers.First(x => x.LayerGuid == folder.StartLayerGuid));
+                int topIndex = Owner.Layers.IndexOf(Owner.Layers.First(x => x.LayerGuid == currentGroup.EndLayerGuid));
+                int bottomIndex = Owner.Layers.IndexOf(Owner.Layers.First(x => x.LayerGuid == currentGroup.StartLayerGuid));
                 var layers = GetLayersInOrder(new FolderData(topIndex, bottomIndex));
 
-                if (folder.Subgroups.Count > 0)
+                if (currentGroup.Subgroups.Count > 0)
                 {
-                    var group = GetGroupByLayer(layerGuid, folder.Subgroups);
+                    var group = GetGroupByLayer(layerGuid, currentGroup.Subgroups);
                     if(group != null)
                     {
                         return group;
@@ -419,7 +489,7 @@ namespace PixiEditor.Models.Layers
 
                 if (layers.Contains(layerGuid))
                 {
-                    return folder;
+                    return currentGroup;
                 }
             }
 

+ 19 - 0
PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs

@@ -15,7 +15,11 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         public RelayCommand NewLayerCommand { get; set; }
 
         public RelayCommand NewGroupCommand { get; set; }
+
+        public RelayCommand CreateGroupFromActiveLayersCommand { get; set; }
+
         public RelayCommand DeleteSelectedCommand { get; set; }
+
         public RelayCommand DeleteGroupCommand { get; set; }
 
         public RelayCommand DeleteLayersCommand { get; set; }
@@ -40,6 +44,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             SetActiveLayerCommand = new RelayCommand(SetActiveLayer);
             NewLayerCommand = new RelayCommand(NewLayer, CanCreateNewLayer);
             NewGroupCommand = new RelayCommand(NewGroup, CanCreateNewLayer);
+            CreateGroupFromActiveLayersCommand = new RelayCommand(CreateGroupFromActiveLayers, CanCreateGroupFromSelected);
             DeleteLayersCommand = new RelayCommand(DeleteLayer, CanDeleteLayer);
             MoveToBackCommand = new RelayCommand(MoveLayerToBack, CanMoveToBack);
             MoveToFrontCommand = new RelayCommand(MoveLayerToFront, CanMoveToFront);
@@ -53,6 +58,15 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             Owner.BitmapManager.DocumentChanged += BitmapManager_DocumentChanged;
         }
 
+        public void CreateGroupFromActiveLayers(object parameter)
+        {
+            var doc = Owner.BitmapManager.ActiveDocument;
+            if (doc != null)
+            {
+                doc.LayerStructure.AddNewGroup($"{Owner.BitmapManager.ActiveLayer.Name} Group", doc.Layers.Where(x => x.IsActive).Reverse(), Owner.BitmapManager.ActiveDocument.ActiveLayerGuid);
+            }
+        }
+
         public bool CanDeleteSelected(object parameter)
         {
             return parameter is not null and (Layer or LayerGroup);
@@ -109,6 +123,11 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             return Owner.BitmapManager.ActiveDocument?.Layers.Count(x => x.IsActive) > 1;
         }
 
+        public bool CanCreateGroupFromSelected(object obj)
+        {
+            return Owner.BitmapManager.ActiveDocument?.Layers.Count(x => x.IsActive) > 0;
+        }
+
         public void NewLayer(object parameter)
         {
             var doc = Owner.BitmapManager.ActiveDocument;

+ 2 - 0
PixiEditor/ViewModels/SubViewModels/Main/UpdateViewModel.cs

@@ -86,10 +86,12 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         private async void Owner_OnStartupEvent(object sender, EventArgs e)
         {
+#if RELEASE
             if (IPreferences.Current.GetPreference("CheckUpdatesOnStartup", true))
             {
                 await CheckForUpdate();
             }
+#endif
         }
 
         private void RestartApplication(object parameter)

+ 2 - 0
PixiEditor/Views/UserControls/LayerStructureItemContainer.xaml

@@ -38,6 +38,8 @@
                                             RelativeSource={RelativeSource AncestorType=ContextMenu}}" 
                            CommandParameter="{Binding PlacementTarget.Tag.ContainerIndex, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                 </MenuItem>
+                <MenuItem Header="Create group"  Command="{Binding PlacementTarget.Tag.LayerCommandsViewModel.CreateGroupFromActiveLayersCommand, 
+                                            RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
                 <Separator/>
                 <MenuItem Header="Merge selected"
                                      Command="{Binding PlacementTarget.Tag.LayerCommandsViewModel.MergeSelectedCommand,