Browse Source

Moving members positions

flabbet 6 months ago
parent
commit
17f50357a6

+ 42 - 35
src/PixiEditor.ChangeableDocument/Changes/NodeGraph/NodeOperations.cs

@@ -141,51 +141,58 @@ public static class NodeOperations
         List<IChangeInfo> changes = new();
         List<IChangeInfo> changes = new();
         Dictionary<Guid, VecD> originalPositionDict = new();
         Dictionary<Guid, VecD> originalPositionDict = new();
 
 
-        if (previouslyConnected != null)
-        {
-            member.Position = new VecD(appendedTo.Position.X - 250, appendedTo.Position.Y);
+        member.Position = new VecD(appendedTo.Position.X - 250, appendedTo.Position.Y);
 
 
-            changes.Add(new NodePosition_ChangeInfo(member.Id, member.Position));
+        changes.Add(new NodePosition_ChangeInfo(member.Id, member.Position));
 
 
-            previouslyConnected.TraverseBackwards((aNode, previousNode, _) =>
+        previouslyConnected?.TraverseBackwards((aNode, previousNode, _) =>
+        {
+            if (aNode is Node toMove)
             {
             {
-                if (aNode is Node toMove)
-                {
-                    originalPositionDict.Add(toMove.Id, toMove.Position);
-                    var y = toMove.Position.Y;
-                    toMove.Position = (previousNode?.Position ?? member.Position) - new VecD(250, 0);
-                    toMove.Position = new VecD(toMove.Position.X, y);
-                    changes.Add(new NodePosition_ChangeInfo(toMove.Id, toMove.Position));
-                }
+                originalPositionDict.Add(toMove.Id, toMove.Position);
+                var y = toMove.Position.Y;
+                toMove.Position = (previousNode?.Position ?? member.Position) - new VecD(250, 0);
+                toMove.Position = new VecD(toMove.Position.X, y);
+                changes.Add(new NodePosition_ChangeInfo(toMove.Id, toMove.Position));
+            }
 
 
-                return true;
-            });
-        }
-        else
+            return true;
+        });
+
+        originalPositions = originalPositionDict;
+        return changes;
+    }
+
+    public static List<IChangeInfo> AdjustPositionsBeforeAppend(Node member, Node appendedTo,
+        out Dictionary<Guid, VecD> originalPositions)
+    {
+        List<IChangeInfo> changes = new();
+        Dictionary<Guid, VecD> originalPositionDict = new();
+
+        member.TraverseBackwards((aNode, previousNode, _) =>
         {
         {
-            member.TraverseBackwards((aNode, previousNode, _) =>
+            if (aNode is Node toMove)
             {
             {
-                if (aNode is Node toMove)
+                originalPositionDict.Add(toMove.Id, toMove.Position);
+                var y = toMove.Position.Y;
+                VecD pos = member.Position + new VecD(250, 0);
+                if (previousNode != null)
                 {
                 {
-                    originalPositionDict.Add(toMove.Id, toMove.Position);
-                    var y = toMove.Position.Y;
-                    VecD pos = member.Position + new VecD(250, 0);
-                    if(previousNode != null)
-                    {
-                        pos = previousNode.Position - new VecD(250, 0);
-                    }
-                    toMove.Position = pos;
-                    toMove.Position = new VecD(toMove.Position.X, y);
-                    changes.Add(new NodePosition_ChangeInfo(toMove.Id, toMove.Position));
+                    pos = previousNode.Position - new VecD(250, 0);
                 }
                 }
 
 
-                return true;
-            });
-            
-            member.Position = new VecD(appendedTo.Position.X - 250, appendedTo.Position.Y);
-            changes.Add(new NodePosition_ChangeInfo(member.Id, member.Position));
-        }
+                toMove.Position = pos;
+                toMove.Position = new VecD(toMove.Position.X, y);
+                changes.Add(new NodePosition_ChangeInfo(toMove.Id, toMove.Position));
+                
+                if(aNode == appendedTo) return false;
+            }
+
+            return true;
+        });
 
 
+        member.Position = new VecD(appendedTo.Position.X - 250, appendedTo.Position.Y);
+        changes.Add(new NodePosition_ChangeInfo(member.Id, member.Position));
 
 
         originalPositions = originalPositionDict;
         originalPositions = originalPositionDict;
         return changes;
         return changes;

+ 71 - 4
src/PixiEditor.ChangeableDocument/Changes/Structure/MoveStructureMember_Change.cs

@@ -2,6 +2,7 @@
 using PixiEditor.ChangeableDocument.Changeables.Graph;
 using PixiEditor.ChangeableDocument.Changeables.Graph;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
+using PixiEditor.ChangeableDocument.ChangeInfos.NodeGraph;
 using PixiEditor.ChangeableDocument.ChangeInfos.Structure;
 using PixiEditor.ChangeableDocument.ChangeInfos.Structure;
 using PixiEditor.ChangeableDocument.Changes.NodeGraph;
 using PixiEditor.ChangeableDocument.Changes.NodeGraph;
 
 
@@ -65,10 +66,22 @@ internal class MoveStructureMember_Change : Change
 
 
         var previouslyConnected = inputProperty.Connection;
         var previouslyConnected = inputProperty.Connection;
 
 
-        if (previouslyConnected == null)
+        bool isMovingBelow = false;
+        
+        inputProperty.Node.TraverseForwards(x =>
         {
         {
-            changes.AddRange(NodeOperations.AdjustPositionsAfterAppend(sourceNode, inputProperty.Node,
-                null, out originalPositions));
+            if (x.Id == sourceNodeGuid)
+            {
+                isMovingBelow = true;
+                return false;
+            }
+            
+            return true;
+        });
+
+        if (isMovingBelow)
+        {
+            changes.AddRange(NodeOperations.AdjustPositionsBeforeAppend(sourceNode, inputProperty.Node, out originalPositions));
         }
         }
 
 
         changes.AddRange(NodeOperations.DetachStructureNode(sourceNode));
         changes.AddRange(NodeOperations.DetachStructureNode(sourceNode));
@@ -76,12 +89,17 @@ internal class MoveStructureMember_Change : Change
             sourceNode.Background,
             sourceNode.Background,
             sourceNode.Id));
             sourceNode.Id));
 
 
-        if (previouslyConnected != null)
+        if (!isMovingBelow)
         {
         {
             changes.AddRange(NodeOperations.AdjustPositionsAfterAppend(sourceNode, inputProperty.Node,
             changes.AddRange(NodeOperations.AdjustPositionsAfterAppend(sourceNode, inputProperty.Node,
                 previouslyConnected?.Node as Node, out originalPositions));
                 previouslyConnected?.Node as Node, out originalPositions));
         }
         }
 
 
+        if (targetNode is FolderNode)
+        {
+            changes.AddRange(AdjustPutIntoFolderPositions(targetNode, originalPositions));
+        }
+
         changes.Add(changeInfo);
         changes.Add(changeInfo);
 
 
         return changes;
         return changes;
@@ -111,4 +129,53 @@ internal class MoveStructureMember_Change : Change
 
 
         return changes;
         return changes;
     }
     }
+    
+    private static List<IChangeInfo> AdjustPutIntoFolderPositions(Node targetNode, Dictionary<Guid, VecD> originalPositions)
+    {
+        List<IChangeInfo> changes = new();
+
+        if (targetNode is FolderNode folder)
+        {
+            folder.Content.Connection.Node.TraverseBackwards(contentNode =>
+            {
+                if (contentNode is Node node)
+                {
+                    if (!originalPositions.ContainsKey(node.Id))
+                    {
+                        originalPositions[node.Id] = node.Position;
+                    }
+                    
+                    node.Position = new VecD(node.Position.X, folder.Position.Y + 250);
+                    changes.Add(new NodePosition_ChangeInfo(node.Id, node.Position));
+                }
+                
+                return true;
+            });
+            
+            folder.Background.Connection?.Node.TraverseBackwards(bgNode =>
+            {
+                if (bgNode is Node node)
+                {
+                    if (!originalPositions.ContainsKey(node.Id))
+                    {
+                        originalPositions[node.Id] = node.Position;
+                    }
+
+                    double pos = folder.Position.Y;
+
+                    if (folder.Content.Connection != null)
+                    {
+                        pos -= 250;
+                    }
+                    
+                    node.Position = new VecD(node.Position.X, pos);
+                    changes.Add(new NodePosition_ChangeInfo(node.Id, node.Position));
+                }
+                
+                return true;
+            });
+        }
+
+        return changes;
+    }
 }
 }

+ 54 - 27
src/PixiEditor/Views/Nodes/NodeGraphView.cs

@@ -1,4 +1,5 @@
-using System.Collections.ObjectModel;
+using System.Collections;
+using System.Collections.ObjectModel;
 using System.Collections.Specialized;
 using System.Collections.Specialized;
 using System.ComponentModel;
 using System.ComponentModel;
 using System.Windows.Input;
 using System.Windows.Input;
@@ -234,36 +235,30 @@ internal class NodeGraphView : Zoombox.Zoombox
         {
         {
             nodeItemsControl.ItemsPanelRoot.Children.CollectionChanged += NodeItems_CollectionChanged;
             nodeItemsControl.ItemsPanelRoot.Children.CollectionChanged += NodeItems_CollectionChanged;
             nodeViewsCache = nodeItemsControl.ItemsPanelRoot.Children.ToList();
             nodeViewsCache = nodeItemsControl.ItemsPanelRoot.Children.ToList();
+            HandleNodesAdded(nodeViewsCache);
         });
         });
     }
     }
 
 
+    protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
+    {
+        nodeItemsControl.ItemsPanelRoot.Children.CollectionChanged -= NodeItems_CollectionChanged;
+    }
+
+    protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
+    {
+        if (nodeItemsControl is { ItemsPanelRoot: not null })
+        {
+            nodeItemsControl.ItemsPanelRoot.Children.CollectionChanged += NodeItems_CollectionChanged;
+            nodeViewsCache = nodeItemsControl.ItemsPanelRoot.Children.ToList();
+            HandleNodesAdded(nodeViewsCache);
+        }
+    }
+
     private void NodeItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
     private void NodeItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
     {
     {
         if (e.Action == NotifyCollectionChangedAction.Add)
         if (e.Action == NotifyCollectionChangedAction.Add)
         {
         {
-            foreach (Control control in e.NewItems)
-            {
-                if (control is not ContentPresenter presenter)
-                {
-                    continue;
-                }
-
-                nodeViewsCache.Add(presenter);
-                presenter.PropertyChanged += OnPresenterPropertyChanged;
-
-                if (presenter.Content is NodeViewModel nvm)
-                {
-                    nvm.PropertyChanged += Node_PropertyChanged;
-                }
-                
-                if (presenter.Child == null)
-                {
-                    continue;
-                }
-
-                NodeView nodeView = (NodeView)presenter.Child;
-                nodeView.PropertyChanged += NodeView_PropertyChanged;
-            }
+            HandleNodesAdded(e.NewItems);
         }
         }
         else if (e.Action == NotifyCollectionChangedAction.Remove)
         else if (e.Action == NotifyCollectionChangedAction.Remove)
         {
         {
@@ -281,7 +276,7 @@ internal class NodeGraphView : Zoombox.Zoombox
                 {
                 {
                     nvm.PropertyChanged -= Node_PropertyChanged;
                     nvm.PropertyChanged -= Node_PropertyChanged;
                 }
                 }
-                
+
                 if (presenter.Child == null)
                 if (presenter.Child == null)
                 {
                 {
                     continue;
                     continue;
@@ -297,6 +292,37 @@ internal class NodeGraphView : Zoombox.Zoombox
         }
         }
     }
     }
 
 
+    private void HandleNodesAdded(IList? items)
+    {
+        foreach (Control control in items)
+        {
+            if (control is not ContentPresenter presenter)
+            {
+                continue;
+            }
+
+            if (!nodeViewsCache.Contains(presenter))
+            {
+                nodeViewsCache.Add(presenter);
+            }
+
+            presenter.PropertyChanged += OnPresenterPropertyChanged;
+
+            if (presenter.Content is NodeViewModel nvm)
+            {
+                nvm.PropertyChanged += Node_PropertyChanged;
+            }
+
+            if (presenter.Child == null)
+            {
+                continue;
+            }
+
+            NodeView nodeView = (NodeView)presenter.Child;
+            nodeView.PropertyChanged += NodeView_PropertyChanged;
+        }
+    }
+
     private void OnPresenterPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
     private void OnPresenterPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
     {
     {
         if (e.Property == ContentPresenter.ChildProperty)
         if (e.Property == ContentPresenter.ChildProperty)
@@ -614,7 +640,7 @@ internal class NodeGraphView : Zoombox.Zoombox
                 {
                 {
                     continue;
                     continue;
                 }
                 }
-                
+
                 if (connectionView.InputProperty == propertyView || connectionView.OutputProperty == propertyView)
                 if (connectionView.InputProperty == propertyView || connectionView.OutputProperty == propertyView)
                 {
                 {
                     connectionView.UpdateSocketPoints();
                     connectionView.UpdateSocketPoints();
@@ -652,7 +678,8 @@ internal class NodeGraphView : Zoombox.Zoombox
             return;
             return;
         }
         }
 
 
-        (INodePropertyHandler, INodePropertyHandler, INodePropertyHandler?) connection = (startConnectionProperty, null, null);
+        (INodePropertyHandler, INodePropertyHandler, INodePropertyHandler?) connection = (startConnectionProperty, null,
+            null);
         if (socket != null)
         if (socket != null)
         {
         {
             endConnectionNode = socket.Node;
             endConnectionNode = socket.Node;