Example #1
0
 public ConnectionData(NodePortData Port1, NodePortData Port2)
 {
     port1   = Port1;
     port2   = Port2;
     port1ID = Port1.portID;
     port2ID = Port2.portID;
 }
Example #2
0
        public bool RecordConnection(NodePortData portData1, NodePortData portData2)
        {
            if (!portData1.connections.Contains(portData2))
            {
                portData1.connections.Add(portData2);
            }

            if (!portData2.connections.Contains(portData1))
            {
                portData2.connections.Add(portData1);
            }


            if (!connections.Exists((ConnectionData conData) => conData.hasPort(portData1) && conData.hasPort(portData2)))
            {
                ConnectionData conData = new ConnectionData(portData1, portData2);
                connections.Add(conData);
                return(true);
            }
            return(false);
        }
        public NodeGraphData ConvertToNodeGraphData(NodeGraph graph)
        {
            NodeGraphData nodeGraphData = new NodeGraphData(graph);

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

            foreach (Node node in graph.nodes)
            {
                if (node == null)
                {
                    continue;
                }
                // Create node data
                NodeData nodeData = new NodeData(node);
                nodeGraphData.nodes.Add(nodeData);

                foreach (NodePort nodePort in node.Ports)
                {
                    NodePortData portData = new NodePortData(nodeData, nodePort);

                    nodeData.ports.Add(portData);
                    portDatas.Add(nodePort, portData);
                }
            }

            foreach (NodePortData portData in portDatas.Values)
            {
                foreach (NodePort conPort in portData.port.GetConnections())
                {
                    NodePortData conPortData;                     // Get portData associated with the connection port
                    if (portDatas.TryGetValue(conPort, out conPortData))
                    {
                        nodeGraphData.RecordConnection(portData, conPortData);
                    }
                }
            }

            return(nodeGraphData);
        }
        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);
        }
Example #5
0
 public bool hasPort(NodePortData port)
 {
     return(port1ID == port.portID || port2ID == port.portID);
 }