Esempio n. 1
0
        protected bool IsCompatibleWithSourcePort(NodeReflectionData node)
        {
            if (sourcePort.direction == Direction.Output)
            {
                return(node.HasInputOfType(sourcePort.portType));
            }

            return(node.HasOutputOfType(sourcePort.portType));
        }
Esempio n. 2
0
        static NodeReflectionData LoadMethodReflection(MethodInfo method, FuncNodeModuleAttribute moduleAttr)
        {
            var attr           = method.GetCustomAttribute <FuncNodeAttribute>();
            var name           = attr?.name ?? ObjectNames.NicifyVariableName(method.Name);
            var returnPortName = attr?.returnName ?? "Result";

            // FuncNode.module can override FuncNodeModule.path.
            var path = attr?.module ?? moduleAttr.path;

            // FuncNode.classType can override default class type
            var classType = attr?.classType ?? typeof(FuncNode);

            var node = new NodeReflectionData()
            {
                type    = classType,
                path    = path?.Split('/'),
                name    = name,
                tooltip = "TODO!",
                method  = method
            };

            var parameters = method.GetParameters();

            foreach (var parameter in parameters)
            {
                node.ports.Add(new PortReflectionData()
                {
                    type = parameter.IsOut ?
                           parameter.ParameterType.GetElementType() :
                           parameter.ParameterType,
                    portName  = ObjectNames.NicifyVariableName(parameter.Name),
                    fieldName = parameter.Name,
                    acceptsMultipleConnections = parameter.IsOut,
                    isInput = !parameter.IsOut
                });
            }

            // Add an output port for the return value if non-void
            if (method.ReturnType != typeof(void))
            {
                node.ports.Add(new PortReflectionData()
                {
                    type      = method.ReturnType,
                    portName  = returnPortName,
                    fieldName = null,
                    acceptsMultipleConnections = true,
                    isInput = false
                });
            }

            // Merge in any reflection data from the wrapper class itself
            // Specifically if it contains additional ports to include
            node.AddPortsFromClass(classType);

            return(node);
        }
        private bool IsCompatible(Port sourcePort, NodeReflectionData node)
        {
            if (sourcePort == null)
            {
                return(true);
            }

            if (sourcePort.Direction == PortDirection.Input)
            {
                return(node.HasOutputOfType(sourcePort.Type));
            }

            return(node.HasInputOfType(sourcePort.Type));
        }
Esempio n. 4
0
        /// <summary>
        /// Extract NodeField information from class reflection + attributes
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        static NodeReflectionData LoadClassReflection(Type type, NodeAttribute nodeAttr)
        {
            string name = nodeAttr.name ?? ObjectNames.NicifyVariableName(type.Name);
            string path = nodeAttr.module;

            var node = new NodeReflectionData()
            {
                type    = type,
                path    = path?.Split('/'),
                name    = name,
                tooltip = nodeAttr.help
            };

            node.AddPortsFromClass(type);
            return(node);
        }
Esempio n. 5
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());
        }