Example #1
0
        /// <summary>
        /// Create a new node from reflection data and insert into the Graph.
        /// </summary>
        internal void AddNodeFromReflectionData(
            NodeReflectionData data,
            Vector2 screenPosition,
            PortView connectedPort = null
            )
        {
            // Calculate where to place this node on the graph
            var windowRoot          = m_EditorWindow.rootVisualElement;
            var windowMousePosition = m_EditorWindow.rootVisualElement.ChangeCoordinatesTo(
                windowRoot.parent,
                screenPosition - m_EditorWindow.position.position
                );

            var graphMousePosition = contentViewContainer.WorldToLocal(windowMousePosition);

            // Create a new node instance and set initial data (ports, etc)
            Undo.RegisterCompleteObjectUndo(m_Graph, $"Add Node {data.name}");

            Debug.Log($"+node {data.name}");

            var node = data.CreateInstance();

            node.graphPosition = graphMousePosition;

            m_Graph.AddNode(node);
            m_SerializedGraph.Update();
            EditorUtility.SetDirty(m_Graph);

            var serializedNodesArr = m_SerializedGraph.FindProperty("nodes");

            var nodeIdx        = m_Graph.nodes.IndexOf(node);
            var serializedNode = serializedNodesArr.GetArrayElementAtIndex(nodeIdx);

            // Add a node to the visual graph
            var editorType = NodeReflection.GetNodeEditorType(data.type);
            var element    = Activator.CreateInstance(editorType) as NodeView;

            element.Initialize(node, serializedNode, m_EdgeListener);

            AddElement(element);

            // If there was a provided existing port to connect to, find the best
            // candidate port on the new node and connect.
            if (connectedPort != null)
            {
                var edge = new Edge();

                if (connectedPort.direction == Direction.Input)
                {
                    edge.input  = connectedPort;
                    edge.output = element.GetCompatibleOutputPort(connectedPort);
                }
                else
                {
                    edge.output = connectedPort;
                    edge.input  = element.GetCompatibleInputPort(connectedPort);
                }

                AddEdge(edge, false);
            }

            Dirty(element);
        }
        public Node Instantiate(SearchResult result)
        {
            NodeReflectionData data = result.UserData as NodeReflectionData;

            return(data.CreateInstance());
        }