public SerializableEdge Connect(BaseNode inputNode, string inputFieldName, BaseNode outputNode, string outputFieldName) { var edge = SerializableEdge.CreateNewEdge(this, inputNode, inputFieldName, outputNode, outputFieldName); edges.Add(edge); return(edge); }
public bool Connect(PortView inputPortView, PortView outputPortView, bool autoDisconnectInputs = true) { var inputPort = inputPortView.owner.nodeTarget.GetPort(inputPortView.fieldName, inputPortView.portData.identifier); var outputPort = outputPortView.owner.nodeTarget.GetPort(outputPortView.fieldName, outputPortView.portData.identifier); // Checks that the node we are connecting still exists if (inputPortView.owner.parent == null || outputPortView.owner.parent == null) { return(false); } var newEdge = SerializableEdge.CreateNewEdge(graph, inputPort, outputPort); var edgeView = new EdgeView() { userData = newEdge, input = inputPortView, output = outputPortView, }; return(Connect(edgeView)); }
void UnserializeAndPasteCallback(string operationName, string serializedData) { var data = JsonUtility.FromJson <CopyPasteHelper>(serializedData); RegisterCompleteObjectUndo(operationName); Dictionary <string, BaseNode> copiedNodesMap = new Dictionary <string, BaseNode>(); foreach (var serializedNode in data.copiedNodes) { var node = JsonSerializer.DeserializeNode(serializedNode); if (node == null) { continue; } string sourceGUID = node.GUID; graph.nodesPerGUID.TryGetValue(sourceGUID, out var sourceNode); //Call OnNodeCreated on the new fresh copied node node.OnNodeCreated(); //And move a bit the new node node.position.position += new Vector2(20, 20); var newNodeView = AddNode(node); // If the nodes were copied from another graph, then the source is null if (sourceNode != null) { nodeDuplicated?.Invoke(sourceNode, node); } copiedNodesMap[sourceGUID] = node; //Select the new node AddToSelection(nodeViewsPerNode[node]); } foreach (var serializedGroup in data.copiedGroups) { var group = JsonSerializer.Deserialize <Group>(serializedGroup); //Same than for node group.OnCreated(); // try to centre the created node in the screen group.position.position += new Vector2(20, 20); var oldGUIDList = group.innerNodeGUIDs.ToList(); group.innerNodeGUIDs.Clear(); foreach (var guid in oldGUIDList) { graph.nodesPerGUID.TryGetValue(guid, out var node); // In case group was copied from another graph if (node == null) { copiedNodesMap.TryGetValue(guid, out node); group.innerNodeGUIDs.Add(node.GUID); } else { group.innerNodeGUIDs.Add(copiedNodesMap[guid].GUID); } } AddGroup(group); } foreach (var serializedEdge in data.copiedEdges) { var edge = JsonSerializer.Deserialize <SerializableEdge>(serializedEdge); edge.Deserialize(); // Find port of new nodes: copiedNodesMap.TryGetValue(edge.inputNode.GUID, out var oldInputNode); copiedNodesMap.TryGetValue(edge.outputNode.GUID, out var oldOutputNode); // We avoid to break the graph by replacing unique connections: if (oldInputNode == null && !edge.inputPort.portData.acceptMultipleEdges || !edge.outputPort.portData.acceptMultipleEdges) { continue; } oldInputNode = oldInputNode ?? edge.inputNode; oldOutputNode = oldOutputNode ?? edge.outputNode; var inputPort = oldInputNode.GetPort(edge.inputPort.fieldName, edge.inputPortIdentifier); var outputPort = oldOutputNode.GetPort(edge.outputPort.fieldName, edge.outputPortIdentifier); var newEdge = SerializableEdge.CreateNewEdge(graph, inputPort, outputPort); if (nodeViewsPerNode.ContainsKey(oldInputNode) && nodeViewsPerNode.ContainsKey(oldOutputNode)) { var edgeView = new EdgeView() { userData = newEdge, input = nodeViewsPerNode[oldInputNode].GetPortViewFromFieldName(newEdge.inputFieldName, newEdge.inputPortIdentifier), output = nodeViewsPerNode[oldOutputNode].GetPortViewFromFieldName(newEdge.outputFieldName, newEdge.outputPortIdentifier) }; Connect(edgeView); } } }