Example #1
0
        public virtual void OnDropOutsidePort(Edge edge, Vector2 position)
        {
            this.graphView.RegisterCompleteObjectUndo("Disconnect edge");

            //If the edge was already existing, remove it
            if (!edge.isGhostEdge)
            {
                graphView.Disconnect(edge as EdgeView);
            }

            // when on of the port is null, then the edge was created and dropped outside of a port
            if (edge.input == null || edge.output == null)
            {
                Vector2 mousePos = graphView.ChangeCoordinatesTo(graphView.contentViewContainer, position);

                // Empirical offset:
                mousePos += new Vector2(-10f, -18);

                // TODO: function
                this.graphView.RegisterCompleteObjectUndo("Added relay node ");
                var relayNode = BaseNode.CreateFromType <RelayNode>(mousePos);
                var relayView = graphView.AddNode(relayNode);

                // Connect to the new relay node:
                var  port    = edge.output ?? edge.input;
                var  pv      = port as PortView;
                bool isInput = edge.input == null;

                var p = pv.owner.nodeTarget.GetPort(pv.fieldName, pv.portData.identifier);

                // Wait for node to be created ...
                graphView.schedule.Execute(() => {
                    // We can't use edge here because it have been destroyed
                    if (isInput)
                    {
                        var newEdge = graphView.graph.Connect(relayNode.inputPorts[0], p);
                        graphView.ConnectView(new EdgeView()
                        {
                            userData = newEdge,
                            input    = relayView.GetPortViewFromFieldName(newEdge.inputFieldName, newEdge.inputPortIdentifier),
                            output   = pv.owner.GetPortViewFromFieldName(newEdge.outputFieldName, newEdge.outputPortIdentifier)
                        });
                    }
                    else
                    {
                        var newEdge = graphView.graph.Connect(p, relayNode.outputPorts[0]);
                        graphView.ConnectView(new EdgeView()
                        {
                            userData = newEdge,
                            input    = pv.owner.GetPortViewFromFieldName(newEdge.inputFieldName, newEdge.inputPortIdentifier),
                            output   = relayView.GetPortViewFromFieldName(newEdge.outputFieldName, newEdge.outputPortIdentifier)
                        });
                    }
                }).ExecuteLater(1);
            }

            //TODO: open new nodes selector and connect the created node if there is one
        }