Browse Source

move structure member revert

flabbet 1 year ago
parent
commit
b0abdaccd1

+ 43 - 8
src/PixiEditor.ChangeableDocument/Changes/NodeGraph/NodeOperations.cs

@@ -37,6 +37,25 @@ public static class NodeOperations
     {
     {
         List<IChangeInfo> changes = new();
         List<IChangeInfo> changes = new();
 
 
+        if (structureNode.Background.Connection != null)
+        {
+            // connect connection to next input if possible
+
+            var connections = structureNode.Output.Connections.ToArray();
+
+            var output = structureNode.Background.Connection;
+
+            foreach (var input in connections)
+            {
+                output.ConnectTo(input);
+                changes.Add(new ConnectProperty_ChangeInfo(output.Node.Id, input.Node.Id,
+                    output.InternalPropertyName, input.InternalPropertyName));
+            }
+            
+            structureNode.Background.Connection.DisconnectFrom(structureNode.Background);
+            changes.Add(new ConnectProperty_ChangeInfo(null, structureNode.Id, null,
+                structureNode.Background.InternalPropertyName));
+        }
 
 
         var outputs = structureNode.Output.Connections.ToArray();
         var outputs = structureNode.Output.Connections.ToArray();
         foreach (var outputConnection in outputs)
         foreach (var outputConnection in outputs)
@@ -46,19 +65,35 @@ public static class NodeOperations
                 outputConnection.InternalPropertyName));
                 outputConnection.InternalPropertyName));
         }
         }
 
 
-        if (structureNode.Background.Connection != null)
+        return changes;
+    }
+
+    public static List<IChangeInfo> ConnectStructureNodeProperties(List<IInputProperty> originalOutputConnections,
+        List<(IInputProperty, IOutputProperty?)> originalInputConnections, StructureNode node)
+    {
+        List<IChangeInfo> changes = new();
+        foreach (var connection in originalOutputConnections)
         {
         {
-            // connect connection to next input if possible
+            node.Output.ConnectTo(connection);
+            changes.Add(new ConnectProperty_ChangeInfo(node.Id, connection.Node.Id, node.Output.InternalPropertyName,
+                connection.InternalPropertyName));
+        }
 
 
-            var connections = structureNode.Output.Connections.ToArray();
+        foreach (var connection in originalInputConnections)
+        {
+            if (connection.Item2 is null)
+                continue;
 
 
-            var output = structureNode.Background.Connection;
+            IInputProperty? input =
+                node.InputProperties.FirstOrDefault(
+                    x => x.InternalPropertyName == connection.Item1.InternalPropertyName);
 
 
-            foreach (var input in connections)
+            if (input != null)
             {
             {
-                output.ConnectTo(input);
-                changes.Add(new ConnectProperty_ChangeInfo(output.Node.Id, input.Node.Id,
-                    output.InternalPropertyName, input.InternalPropertyName));
+                connection.Item2.ConnectTo(input);
+                changes.Add(new ConnectProperty_ChangeInfo(connection.Item2.Node.Id, node.Id,
+                    connection.Item2.InternalPropertyName,
+                    input.InternalPropertyName));
             }
             }
         }
         }
 
 

+ 2 - 23
src/PixiEditor.ChangeableDocument/Changes/Structure/DeleteStructureMember_Change.cs

@@ -2,6 +2,7 @@
 using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 using PixiEditor.ChangeableDocument.ChangeInfos.NodeGraph;
 using PixiEditor.ChangeableDocument.ChangeInfos.NodeGraph;
 using PixiEditor.ChangeableDocument.ChangeInfos.Structure;
 using PixiEditor.ChangeableDocument.ChangeInfos.Structure;
+using PixiEditor.ChangeableDocument.Changes.NodeGraph;
 
 
 namespace PixiEditor.ChangeableDocument.Changes.Structure;
 namespace PixiEditor.ChangeableDocument.Changes.Structure;
 
 
@@ -79,29 +80,7 @@ internal class DeleteStructureMember_Change : Change
         
         
         changes.Add(createChange);
         changes.Add(createChange);
 
 
-        foreach (var connection in originalOutputConnections)
-        {
-            copy.Output.ConnectTo(connection);
-            changes.Add(new ConnectProperty_ChangeInfo(copy.Id, connection.Node.Id, copy.Output.InternalPropertyName,
-                connection.InternalPropertyName));
-        }
-
-        foreach (var connection in originalInputConnections)
-        {
-            if (connection.Item2 is null)
-                continue;
-
-            IInputProperty? input =
-                copy.InputProperties.FirstOrDefault(x => x.InternalPropertyName == connection.Item1.InternalPropertyName);
-
-            if (input != null)
-            {
-                connection.Item2.ConnectTo(input);
-                changes.Add(new ConnectProperty_ChangeInfo(connection.Item2.Node.Id, copy.Id,
-                    connection.Item2.InternalPropertyName,
-                    input.InternalPropertyName));
-            }
-        }
+        changes.AddRange(NodeOperations.ConnectStructureNodeProperties(originalOutputConnections, originalInputConnections, copy)); 
         
         
         return changes;
         return changes;
     }
     }

+ 21 - 16
src/PixiEditor.ChangeableDocument/Changes/Structure/MoveStructureMember_Change.cs

@@ -12,8 +12,10 @@ internal class MoveStructureMember_Change : Change
     private Guid targetNodeGuid;
     private Guid targetNodeGuid;
 
 
     private Guid originalFolderGuid;
     private Guid originalFolderGuid;
-    
-    private Guid originalParentGuid;
+
+    private List<IInputProperty> originalOutputConnections = new();
+    private List<(IInputProperty, IOutputProperty?)> originalInputConnections = new();
+
 
 
     [GenerateMakeChangeAction]
     [GenerateMakeChangeAction]
     public MoveStructureMember_Change(Guid memberGuid, Guid targetNode)
     public MoveStructureMember_Change(Guid memberGuid, Guid targetNode)
@@ -29,12 +31,8 @@ internal class MoveStructureMember_Change : Change
         if (member is null || targetFolder is null)
         if (member is null || targetFolder is null)
             return false;
             return false;
 
 
-        member.TraverseForwards(node =>
-        {
-            originalParentGuid = node.Id;
-            return false;
-        });
-        
+        originalOutputConnections = member.Output.Connections.ToList();
+        originalInputConnections = member.InputProperties.Select(x => ((IInputProperty)x, x.Connection)).ToList();
         return true;
         return true;
     }
     }
 
 
@@ -44,17 +42,19 @@ internal class MoveStructureMember_Change : Change
         var targetNode = document.FindNode(targetNodeGuid);
         var targetNode = document.FindNode(targetNodeGuid);
         if (sourceNode is null || targetNode is not IBackgroundInput backgroundInput)
         if (sourceNode is null || targetNode is not IBackgroundInput backgroundInput)
             return [];
             return [];
-        
+
         List<IChangeInfo> changes = new();
         List<IChangeInfo> changes = new();
-        
+
         changes.AddRange(NodeOperations.DetachStructureNode(sourceNode));
         changes.AddRange(NodeOperations.DetachStructureNode(sourceNode));
-        changes.AddRange(NodeOperations.AppendMember(backgroundInput.Background, sourceNode.Output, sourceNode.Background,
+        changes.AddRange(NodeOperations.AppendMember(backgroundInput.Background, sourceNode.Output,
+            sourceNode.Background,
             sourceNode.Id));
             sourceNode.Id));
-        
+
         return changes;
         return changes;
     }
     }
 
 
-    public override OneOf<None, IChangeInfo, List<IChangeInfo>> Apply(Document target, bool firstApply, out bool ignoreInUndo)
+    public override OneOf<None, IChangeInfo, List<IChangeInfo>> Apply(Document target, bool firstApply,
+        out bool ignoreInUndo)
     {
     {
         var changes = Move(target, memberGuid, targetNodeGuid);
         var changes = Move(target, memberGuid, targetNodeGuid);
         ignoreInUndo = false;
         ignoreInUndo = false;
@@ -63,8 +63,13 @@ internal class MoveStructureMember_Change : Change
 
 
     public override OneOf<None, IChangeInfo, List<IChangeInfo>> Revert(Document target)
     public override OneOf<None, IChangeInfo, List<IChangeInfo>> Revert(Document target)
     {
     {
-        // TODO: this is lossy, original connections might be lost
-        var changes = Move(target, memberGuid, originalFolderGuid);
-        return changes; 
+        StructureNode member = target.FindMember(memberGuid);
+
+        List<IChangeInfo> changes = new List<IChangeInfo>();
+        
+        changes.AddRange(NodeOperations.DetachStructureNode(member));
+        changes.AddRange(NodeOperations.ConnectStructureNodeProperties(originalOutputConnections,
+            originalInputConnections, member));
+        return changes;
     }
     }
 }
 }