Esempio n. 1
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);
        }
Esempio n. 2
0
        protected bool DoDisConnectToList <T>(NodePort port, ref List <T> condList, NodePort.IO dir = NodePort.IO.Input) where T : Node
        {
            if (port.direction != dir)
            {
                return(false);
            }

            if (port.ValueType != typeof(List <T>))
            {
                return(false);
            }

            if (condList != null)
            {
                condList.Clear();
            }
            else
            {
                condList = new List <T> ();
            }

            for (int i = 0; i < port.ConnectionCount; ++i)
            {
                T cc = port.GetConnection(i).node as T;
                if (cc != null)
                {
                    condList.Add(cc);
                }
            }

            return(true);
        }
Esempio n. 3
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,
                                        ConnectionType connectionType = ConnectionType.Multiple,
                                        TypeConstraint typeConstraint = TypeConstraint.None,
                                        string fieldName = null)
        {
            if (fieldName == null)
            {
                fieldName = "dynamicInput_0";
                var i = 0;

                while (this.HasPort(fieldName))
                {
                    fieldName = "dynamicInput_" + (++i);
                }
            }
            else if (this.HasPort(fieldName))
            {
                Debug.LogWarning("Port '" + fieldName + "' already exists in " + this.name, this);
                return(this.ports[fieldName]);
            }

            var port = new NodePort(fieldName, type, direction, connectionType, typeConstraint, this);

            this.ports.Add(fieldName, port);

            return(port);
        }
Esempio n. 4
0
        private NodePort AddInstancePort(Type type, NodePort.IO direction, string fieldName = null)
        {
            if (fieldName == null)
            {
                fieldName = "instanceInput_0";
                int i = 0;
                while (HasPort(fieldName))
                {
                    fieldName = "instanceInput_" + (++i);
                }
            }
            else if (HasPort(fieldName))
            {
                Debug.LogWarning("Port '" + fieldName + "' already exists in " + name, this);
                return(ports[fieldName]);
            }
            NodePort port = new NodePort(fieldName, type, direction, this);

            ports.Add(fieldName, port);
            return(port);
        }
Esempio n. 5
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)
        {
            int i = 0;

            if (fieldName == null)
            {
                fieldName = "dynamicInput_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, i);

            Debug.Log("NEw node at " + i);
            ports.Add(fieldName, port);
            return(port);
        }
Esempio n. 6
0
        protected bool DoDisConnect <T>(NodePort port, ref T item, NodePort.IO dir = NodePort.IO.Input) where T : Node
        {
            if (port.direction != dir)
            {
                return(false);
            }

            if (port.ValueType != typeof(T))
            {
                return(false);
            }

            for (int i = 0; i < port.ConnectionCount; ++i)
            {
                T cc = port.GetConnection(i).node as T;
                if (cc != null)
                {
                    item = cc;
                    return(true);
                }
            }
            item = null;
            return(true);
        }
Esempio n. 7
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));
 }
Esempio n. 8
0
        protected bool DoCreateConnectToList <T>(NodePort from, NodePort to, ref List <T> condList, string condListName, NodePort.IO dir = NodePort.IO.Input) where T : Node
        {
            // 不允许自己连接自己
            if (from.node == to.node)
            {
                from.Disconnect(to);
                return(false);
            }

            if (dir == NodePort.IO.Input)
            {
                if (from.node == this)
                {
                    return(false);
                }
            }
            else if (dir == NodePort.IO.Output)
            {
                if (from.node != this)
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }

            T item = default(T);

            if (dir == NodePort.IO.Input)
            {
                if (to.fieldName != condListName)
                {
                    return(false);
                }
                item = from.node as T;
            }
            else if (dir == NodePort.IO.Output)
            {
                if (from.fieldName != condListName)
                {
                    return(false);
                }
                item = from.node as T;
            }
            else
            {
                return(false);
            }

            if (item != null)
            {
                if (condList == null)
                {
                    condList = new List <T> ();
                }
                T cc = from.node as T;
                if (!condList.Contains(cc))
                {
                    condList.Add(cc);
                }
                return(true);
            }
            else
            {
                from.Disconnect(to);
            }

            return(false);
        }
Esempio n. 9
0
        protected bool DoCreateConnect <T>(NodePort from, NodePort to, ref T item, string itemName, NodePort.IO dir = NodePort.IO.Input) where T : Node
        {
            // 不允许自己连接自己
            if (from.node == to.node)
            {
                from.Disconnect(to);
                return(false);
            }

            if (dir == NodePort.IO.Input)
            {
                if (from.node == this)
                {
                    return(false);
                }
            }
            else if (dir == NodePort.IO.Output)
            {
                if (from.node != this)
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }

            if (dir == NodePort.IO.Input)
            {
                if (to.fieldName != itemName)
                {
                    return(false);
                }
                item = from.node as T;
            }
            else if (dir == NodePort.IO.Output)
            {
                if (from.fieldName != itemName)
                {
                    return(false);
                }
                item = from.node as T;
            }
            else
            {
                return(false);
            }

            if (item == null)
            {
                from.Disconnect(to);
                return(false);
            }
            return(true);
        }
        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);
        }