Exemple #1
0
        /// <summary>
        /// Construct a static targetless nodeport. Used as a template.
        /// </summary>
        public NodePort(FieldInfo fieldInfo)
        {
            _fieldName = fieldInfo.Name;
            ValueType  = fieldInfo.FieldType;
            _dynamic   = false;
            var attribs = fieldInfo.GetCustomAttributes(false);

            for (int i = 0; i < attribs.Length; i++)
            {
                if (attribs[i] is Node.InputAttribute inputAttribute)
                {
                    _direction      = IO.Input;
                    _connectionType = inputAttribute.connectionType;
                    _typeConstraint = inputAttribute.typeConstraint;
                    _allowedTypes   = new ClassTypeReference[inputAttribute.allowedTypes.Length];
                    for (int j = 0; j < inputAttribute.allowedTypes.Length; j++)
                    {
                        _allowedTypes[j] = inputAttribute.allowedTypes[j];
                    }
                }
                else if (attribs[i] is Node.OutputAttribute outputAttribute)
                {
                    _direction      = IO.Output;
                    _connectionType = outputAttribute.connectionType;
                    _typeConstraint = outputAttribute.typeConstraint;
                    _allowedTypes   = new ClassTypeReference[outputAttribute.allowedTypes.Length];
                    for (int j = 0; j < outputAttribute.allowedTypes.Length; j++)
                    {
                        _allowedTypes[j] = outputAttribute.allowedTypes[j];
                    }
                }
            }
        }
Exemple #2
0
 public NTInputAttribute(Node.ShowBackingValue backingValue = Node.ShowBackingValue.Unconnected,
                         Node.ConnectionType connectionType = Node.ConnectionType.Override,
                         Node.TypeConstraint typeConstraint = Node.TypeConstraint.Strict,
                         bool instancePortList = false) :
     base(backingValue, connectionType, typeConstraint, instancePortList)
 {
 }
Exemple #3
0
 public NTInputSelectAttribute(Node.ShowBackingValue backingValue = Node.ShowBackingValue.Always,
                               Node.ConnectionType connectionType = Node.ConnectionType.Multiple,
                               Node.TypeConstraint typeConstraint = Node.TypeConstraint.Strict,
                               bool instancePortList = true) :
     base(backingValue, connectionType, typeConstraint, instancePortList)
 {
 }
Exemple #4
0
        /// <summary> Add a dynamic, serialized port to this node.
        /// </summary>
        /// <seealso cref="AddInstanceInput"/>
        /// <seealso cref="AddInstanceOutput"/>
        private NodePort AddInstancePort(Type type, NodePort.IO direction, Node.ConnectionType connectionType = Node.ConnectionType.Multiple, string fieldName = null, string namePrefix = null)
        {
            if (fieldName == null)
            {
                if (namePrefix == null)
                {
                    namePrefix = "instanceInput_";
                }
                fieldName = namePrefix + "0";
                int i = 0;
                while (HasPort(fieldName))
                {
                    fieldName = namePrefix + (++i);
                }
            }
            else if (HasPort(fieldName))
            {
                Debug.LogWarning("Port '" + fieldName + "' already exists in " + name, this);
                return(ports[fieldName]);
            }
            NodePort port = new NodePort(fieldName, type, direction, connectionType, this);

            ports.Add(fieldName, port);
            return(port);
        }
 /// <summary> Copy a nodePort but assign it to another node. </summary>
 public NodePort(NodePort nodePort, Node node)
 {
     _fieldName      = nodePort._fieldName;
     ValueType       = nodePort.valueType;
     _direction      = nodePort.direction;
     _dynamic        = nodePort._dynamic;
     _connectionType = nodePort._connectionType;
     _node           = node;
 }
 /// <summary> Construct a dynamic port. Dynamic ports are not forgotten on reimport, and is ideal for runtime-created ports. </summary>
 public NodePort(string fieldName, Type type, IO direction, Node.ConnectionType connectionType, Node node)
 {
     _fieldName      = fieldName;
     this.ValueType  = type;
     _direction      = direction;
     _node           = node;
     _dynamic        = true;
     _connectionType = connectionType;
 }
Exemple #7
0
 /// <summary> Construct a dynamic port. Dynamic ports are not forgotten on reimport, and is ideal for runtime-created ports. </summary>
 public NodePort(string fieldName, Type type, IO direction, Node.ConnectionType connectionType, Node.TypeConstraint typeConstraint, Node node)
 {
     _fieldName      = fieldName;
     ValueType       = type;
     _direction      = direction;
     _node           = node;
     _dynamic        = true;
     _connectionType = connectionType;
     _typeConstraint = typeConstraint;
 }
Exemple #8
0
 /// <summary> Copy a nodePort but assign it to another node. </summary>
 public NodePort(NodePort nodePort, Node node)
 {
     _fieldName      = nodePort._fieldName;
     ValueType       = nodePort.valueType;
     _direction      = nodePort.direction;
     _dynamic        = nodePort._dynamic;
     _connectionType = nodePort._connectionType;
     _typeConstraint = nodePort._typeConstraint;
     _node           = node;
     NodeIdentifier  = _node.Identifier;
 }
Exemple #9
0
 /// <summary>
 /// Copy a nodePort but assign it to another node.
 /// </summary>
 public NodePort(NodePort nodePort, Node node)
 {
     _fieldName      = nodePort._fieldName;
     ValueType       = nodePort.valueType;
     _direction      = nodePort.direction;
     _dynamic        = nodePort._dynamic;
     _connectionType = nodePort._connectionType;
     _typeConstraint = nodePort._typeConstraint;
     _allowedTypes   = nodePort._allowedTypes;
     _node           = node;
 }
Exemple #10
0
        /// <summary> Add a dynamic, serialized port to this node. </summary>
        /// <seealso cref="AddDynamicInput"/>
        /// <seealso cref="AddDynamicOutput"/>
        private NodePort AddDynamicPort(Type type, NodePort.IO direction, Node.ConnectionType connectionType = Node.ConnectionType.Multiple, Node.TypeConstraint typeConstraint = TypeConstraint.None, string fieldName = null)
        {
            if (fieldName == null)
            {
                fieldName = "dynamicInput_0";
                int i = 0;
                while (HasPort(fieldName))
                {
                    fieldName = "dynamicInput_" + (++i);
                }
            }
            else if (HasPort(fieldName))
            {
                Debug.LogWarning("Port '" + fieldName + "' already exists in " + name, this);
                return(ports[fieldName]);
            }
            NodePort port = new NodePort(fieldName, type, direction, connectionType, typeConstraint, this);

            ports.Add(fieldName, port);
            return(port);
        }
Exemple #11
0
        /// <summary> Construct a static targetless nodeport. Used as a template. </summary>
        public NodePort(FieldInfo fieldInfo)
        {
            _fieldName = fieldInfo.Name;
            ValueType  = fieldInfo.FieldType;
            _dynamic   = false;
            var attribs = fieldInfo.GetCustomAttributes(false);

            for (int i = 0; i < attribs.Length; i++)
            {
                if (attribs[i] is Node.InputAttribute)
                {
                    _direction      = IO.Input;
                    _connectionType = (attribs[i] as Node.InputAttribute).connectionType;
                }
                else if (attribs[i] is Node.OutputAttribute)
                {
                    _direction      = IO.Output;
                    _connectionType = (attribs[i] as Node.OutputAttribute).connectionType;
                }
            }
        }
Exemple #12
0
        /// <summary>
        ///     Gets the given dynamic port for this node.
        /// </summary>
        /// <param name="port"></param>
        /// <param name="input"></param>
        /// <param name="connectionType"></param>
        /// <returns></returns>
        protected NodePort GetDynamicPort(string port, bool input,
                                          Node.ConnectionType connectionType = Node.ConnectionType.Override)
        {
            NodePort dynamicPort = target.GetPort(port);

            if (dynamicPort == null)
            {
                if (input)
                {
                    dynamicPort = target.AddDynamicInput(typeof(Node), connectionType,
                                                         Node.TypeConstraint.Inherited, port);
                }
                else
                {
                    dynamicPort = target.AddDynamicOutput(typeof(Node), connectionType,
                                                          Node.TypeConstraint.Inherited, port);
                }
            }

            return(dynamicPort);
        }
Exemple #13
0
 /// <summary> Convenience function.
 /// </summary>
 /// <seealso cref="AddInstancePort"/>
 /// <seealso cref="AddInstanceOutput"/>
 public NodePort AddInstanceInput(Type type, Node.ConnectionType connectionType = Node.ConnectionType.Multiple, string fieldName = null)
 {
     return(AddInstancePort(type, NodePort.IO.Input, connectionType, fieldName));
 }
Exemple #14
0
 public NodePort AddInstanceInput(Type type, Node.ConnectionType connectionType = Node.ConnectionType.Multiple, Node.TypeConstraint typeConstraint = TypeConstraint.None, string fieldName = null)
 {
     return(AddDynamicInput(type, connectionType, typeConstraint, fieldName));
 }
Exemple #15
0
 private NodePort AddInstancePort(Type type, NodePort.IO direction, Node.ConnectionType connectionType = Node.ConnectionType.Multiple, Node.TypeConstraint typeConstraint = TypeConstraint.None, string fieldName = null)
 {
     return(AddDynamicPort(type, direction, connectionType, typeConstraint, fieldName));
 }
Exemple #16
0
 public NTOutputAttribute(Node.ShowBackingValue backingValue = Node.ShowBackingValue.Always,
                          Node.ConnectionType connectionType = Node.ConnectionType.Multiple,
                          bool instancePortList = false) :
     base(backingValue, connectionType, instancePortList)
 {
 }
Exemple #17
0
 /// <summary> Convenience function.
 /// </summary>
 /// <seealso cref="AddInstancePort"/>
 /// <seealso cref="AddInstanceInput"/>
 public NodePort AddInstanceOutput(Type type, Node.ConnectionType connectionType = Node.ConnectionType.Multiple, string fieldName = null, string namePrefix = null)
 {
     return(AddInstancePort(type, NodePort.IO.Output, connectionType, fieldName, namePrefix));
 }
        public override NodeGraphData ImportData(string serializedData)
        {
            NodeGraphData returnData = null;


            JSONNode root = JSON.Parse(serializedData);

            Dictionary <int, object>            references = new Dictionary <int, object>();
            Dictionary <NodePort, NodePortData> portDatas  = new Dictionary <NodePort, NodePortData>();

            List <string> ignoredFields = new List <string> {
                "name", "graph", "ports", "nodes"
            };
            JSONObject graphJObject;

            if (root.HasKey("graph"))
            {
                graphJObject = root["graph"].AsObject;
                int    id         = graphJObject["id"];
                string graphTypeS = graphJObject["type"];
                Type   graphType  = Type.GetType(graphTypeS);

                NodeGraph graph = (NodeGraph)Activator.CreateInstance(graphType);
                graph.name = graphJObject["name"];

                references.Add(id, graph);
                returnData = new NodeGraphData(graph);

                //Debug.Log("Basic graph OK!");
            }
            else
            {
                Debug.LogWarning("Basic graph KO!");
                return(returnData);
            }

            if (root.HasKey("nodes"))
            {
                JSONArray nodesJArray = root["nodes"].AsArray;

                foreach (JSONObject nodeJObject in nodesJArray.Values)
                {
                    int    id        = nodeJObject["id"];
                    string nodeTypeS = nodeJObject["type"];
                    Type   nodeType  = Type.GetType(nodeTypeS);

                    Node node = (Node)Activator.CreateInstance(nodeType);
                    node.name = nodeJObject["name"];
                    object nodeOBJ = (object)node;

                    SimpleJSONExtension.FromJSON(ref nodeOBJ, nodeType, nodeJObject, ignoredFields, references);
                    node.graph = returnData.graph;
                    NodeData nodeData = new NodeData(node);

                    JSONArray nodePortsArray = nodeJObject["ports"].AsArray;
                    foreach (var nodePort in nodePortsArray.Values)
                    {
                        bool     dynamic  = nodePort["dynamic"].AsBool;
                        string   portName = nodePort["name"];
                        NodePort port     = null;
                        int      portId   = 0;

                        if (dynamic)
                        {
                            if (!node.HasPort(portName))
                            {
                                Type dynamicType = Type.GetType(nodePort["valueType"]);
                                Node.TypeConstraint constraint     = (Node.TypeConstraint)nodePort["typeConstraint"].AsInt;
                                Node.ConnectionType connectionType = (Node.ConnectionType)nodePort["connectionType"].AsInt;
                                NodePort.IO         direction      = (NodePort.IO)nodePort["direction"].AsInt;

                                if (direction == NodePort.IO.Input)
                                {
                                    port = node.AddInstanceInput(dynamicType, connectionType, constraint, portName);
                                }
                                else
                                {
                                    port = node.AddInstanceOutput(dynamicType, connectionType, constraint, portName);
                                }
                            }
                            else
                            {
                                Debug.LogWarning("Ignoring port bc is already created");
                            }
                        }

                        port = node.GetPort(nodePort["name"]);

                        if (port == null)
                        {
                            Debug.Log("Port is null? " + node.name);

                            foreach (var p in node.Ports)
                            {
                                Debug.Log(p.fieldName);
                            }
                        }

                        portId = nodePort["id"];

                        NodePortData portData = new NodePortData(nodeData, port);

                        nodeData.ports.Add(portData);
                        portDatas.Add(port, portData);
                        references.Add(portId, port);
                    }

                    references.Add(id, node);
                    returnData.nodes.Add(nodeData);
                }

                //Debug.Log("Basic Nodes OK!");
            }
            else
            {
                Debug.LogWarning("Basic Nodes KO!");
                return(returnData);
            }


            if (root.HasKey("connections"))
            {
                JSONArray connectionsJArray = root["connections"].AsArray;

                foreach (JSONObject connectionJObject in connectionsJArray.Values)
                {
                    int port1ID = connectionJObject["port1ID"].AsInt;
                    int port2ID = connectionJObject["port2ID"].AsInt;

                    if (references.ContainsKey(port1ID) && references.ContainsKey(port2ID))
                    {
                        NodePort p1 = (NodePort)references[port1ID];
                        NodePort p2 = (NodePort)references[port2ID];

                        p1.Connect(p2);
                    }
                    else
                    {
                        Debug.LogWarning("Error recovering one connection");
                    }
                }
                //Debug.Log("Connections OK!");
            }
            else
            {
                Debug.LogWarning("Connections KO!");
                return(returnData);
            }


            object graphObject = returnData.graph;

            SimpleJSONExtension.FromJSON(ref graphObject, returnData.graph.GetType(), graphJObject, ignoredFields, references);


            return(returnData);
        }
Exemple #19
0
 /// <summary>
 /// Convenience function.
 /// </summary>
 /// <seealso cref="AddInstancePort"/>
 /// <seealso cref="AddInstanceInput"/>
 public NodePort AddDynamicOutput(Type type, Node.ConnectionType connectionType = Node.ConnectionType.Multiple, Node.TypeConstraint typeConstraint = TypeConstraint.None, string fieldName = null) => AddDynamicPort(type, NodePort.IO.Output, connectionType, typeConstraint, fieldName);