Browse Source

Fixed connection undo

flabbet 1 year ago
parent
commit
1ea4191fae

+ 17 - 9
src/PixiEditor.AvaloniaUI/Models/DocumentModels/DocumentUpdater.cs

@@ -518,17 +518,25 @@ internal class DocumentUpdater
     
     private void ProcessConnectProperty(ConnectProperty_ChangeInfo info)
     {
-        NodeViewModel outputNode = doc.StructureHelper.FindNode<NodeViewModel>(info.SourceNodeId);
+        NodeViewModel outputNode = info.SourceNodeId.HasValue ? doc.StructureHelper.FindNode<NodeViewModel>(info.SourceNodeId.Value) : null;
         NodeViewModel inputNode = doc.StructureHelper.FindNode<NodeViewModel>(info.TargetNodeId);
-        NodeConnectionViewModel connection = new NodeConnectionViewModel()
+
+        if (inputNode != null && outputNode != null)
         {
-            InputNode = inputNode,
-            OutputNode = outputNode,
-            InputProperty = inputNode.FindInputProperty(info.TargetProperty),
-            OutputProperty = outputNode.FindOutputProperty(info.SourceProperty)
-        };
-        
-        doc.NodeGraphHandler.SetConnection(connection);
+            NodeConnectionViewModel connection = new NodeConnectionViewModel()
+            {
+                InputNode = inputNode,
+                OutputNode = outputNode,
+                InputProperty = inputNode.FindInputProperty(info.TargetProperty),
+                OutputProperty = outputNode.FindOutputProperty(info.SourceProperty)
+            };
+            
+            doc.NodeGraphHandler.SetConnection(connection);
+        }
+        else
+        {
+            doc.NodeGraphHandler.RemoveConnection(info.TargetNodeId, info.TargetProperty);
+        }
     }
     
     private void ProcessNodePosition(NodePosition_ChangeInfo info)

+ 1 - 0
src/PixiEditor.AvaloniaUI/Models/Handlers/INodeGraphHandler.cs

@@ -12,4 +12,5 @@ internal interface INodeGraphHandler
    public void AddNode(INodeHandler node);
    public void RemoveNode(Guid nodeId);
    public void SetConnection(NodeConnectionViewModel connection);
+   public void RemoveConnection(Guid nodeId, string property);
 }

+ 10 - 1
src/PixiEditor.AvaloniaUI/ViewModels/Document/NodeGraphViewModel.cs

@@ -51,7 +51,16 @@ internal class NodeGraphViewModel : ViewModelBase, INodeGraphHandler
         
         Connections.Add(connection);
     }
-    
+
+    public void RemoveConnection(Guid nodeId, string property)
+    {
+        var connection = Connections.FirstOrDefault(x => x.InputProperty.Node.Id == nodeId && x.InputProperty.PropertyName == property);
+        if (connection != null)
+        {
+            Connections.Remove(connection);
+        } 
+    }
+
     public bool TryTraverse(Func<INodeHandler, bool> func)
     {
         if (OutputNode == null) return false;

+ 2 - 2
src/PixiEditor.ChangeableDocument/ChangeInfos/NodeGraph/ConnectProperty_ChangeInfo.cs

@@ -1,7 +1,7 @@
 namespace PixiEditor.ChangeableDocument.ChangeInfos.NodeGraph;
 
 public record ConnectProperty_ChangeInfo(
-    Guid SourceNodeId,
+    Guid? SourceNodeId,
     Guid TargetNodeId,
-    string SourceProperty,
+    string? SourceProperty,
     string TargetProperty) : IChangeInfo;

+ 16 - 1
src/PixiEditor.ChangeableDocument/Changes/NodeGraph/ConnectProperties_Change.cs

@@ -70,9 +70,24 @@ internal class ConnectProperties_Change : Change
         OutputProperty outputProp = outputNode.GetOutputProperty(OutputProperty);
 
         outputProp.DisconnectFrom(inputProp);
+        
+        ConnectProperty_ChangeInfo change = new(null, InputNodeId, null, InputProperty);
+        
         inputProp.Connection = originalConnection;
 
+        List<IChangeInfo> changes = new()
+        {
+            change,
+        };
+        
+        if (originalConnection != null)
+        {
+            ConnectProperty_ChangeInfo oldConnection = new(originalConnection.Node.Id, InputNodeId,
+                originalConnection.InternalPropertyName, InputProperty);
+            changes.Add(oldConnection);
+        }
 
-        return new ConnectProperty_ChangeInfo(outputNode.Id, inputNode.Id, OutputProperty, InputProperty);
+
+        return changes;
     }
 }