Exemplo n.º 1
0
        public void SetConnections()
        {
            //finally after every node is populated
            //try and connect them all!
            foreach (Node n in Nodes)
            {
                Node.NodeData nd = null;
                if (tempData.TryGetValue(n.Id, out nd))
                {
                    n.SetConnections(NodeLookup, nd.outputs);
                }
            }

            //then add event handlers!
            foreach (Node n in Nodes)
            {
                n.OnUpdate += N_OnUpdate;
            }

            //release temp data
            tempData.Clear();

            if (this is FunctionGraph)
            {
                (this as FunctionGraph).UpdateOutputTypes();
            }
            else
            {
                TryAndProcess();
            }
        }
Exemplo n.º 2
0
        public virtual void FromJson(string data)
        {
            GraphData d = JsonConvert.DeserializeObject <GraphData>(data);

            if (d != null)
            {
                Dictionary <string, Node>   lookup   = new Dictionary <string, Node>();
                Dictionary <string, string> nodeData = new Dictionary <string, string>();

                Name               = d.name;
                OutputNodes        = d.outputs;
                InputNodes         = d.inputs;
                defaultTextureType = d.defaultTextureType;
                ShiftX             = d.shiftX;
                ShiftY             = d.shiftY;
                Zoom               = d.zoom;

                Parameters = new Dictionary <string, GraphParameterValue>();

                foreach (var k in d.parameters.Keys)
                {
                    Parameters[k] = GraphParameterValue.FromJson(d.parameters[k]);
                }

                //parse node data
                //setup initial object instances
                foreach (string s in d.nodes)
                {
                    Node.NodeData nd = JsonConvert.DeserializeObject <Node.NodeData>(s);

                    if (nd != null)
                    {
                        string type = nd.type;
                        if (!string.IsNullOrEmpty(type))
                        {
                            try
                            {
                                Type t = Type.GetType(type);
                                if (t != null)
                                {
                                    //special case to handle output nodes
                                    if (t.Equals(typeof(OutputNode)))
                                    {
                                        OutputNode n = new OutputNode(defaultTextureType);
                                        n.ParentGraph = this;
                                        n.Id          = nd.id;
                                        lookup[nd.id] = n;
                                        Nodes.Add(n);
                                        nodeData[nd.id] = s;
                                    }
                                    else if (t.Equals(typeof(InputNode)))
                                    {
                                        InputNode n = new InputNode(defaultTextureType);
                                        n.ParentGraph = this;
                                        n.Id          = nd.id;
                                        lookup[nd.id] = n;
                                        Nodes.Add(n);
                                        nodeData[nd.id] = s;
                                    }
                                    else
                                    {
                                        Node n = (Node)Activator.CreateInstance(t, nd.width, nd.height, defaultTextureType);
                                        if (n != null)
                                        {
                                            n.ParentGraph = this;
                                            n.Id          = nd.id;
                                            lookup[nd.id] = n;
                                            Nodes.Add(n);
                                            nodeData[nd.id] = s;
                                        }
                                    }
                                }
                                else
                                {
                                    //log we could not load graph node
                                }
                            }
                            catch
                            {
                                //log we could not load graph node
                            }
                        }
                    }
                }

                NodeLookup = lookup;

                //apply data to nodes
                foreach (Node n in Nodes)
                {
                    string ndata = null;
                    nodeData.TryGetValue(n.Id, out ndata);

                    if (!string.IsNullOrEmpty(ndata))
                    {
                        n.FromJson(lookup, ndata);

                        n.OnUpdate += N_OnUpdate;
                        //origin sizes are only for graph instances
                        //not actually used in the current one being edited
                        //it is used in the ResizeWith
                        OriginSizes[n.Id] = new Point(n.Width, n.Height);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public virtual void FromJson(string data)
        {
            GraphData d = JsonConvert.DeserializeObject <GraphData>(data);

            if (d != null)
            {
                tempData = new Dictionary <string, Node.NodeData>();
                Dictionary <string, Node>   lookup   = new Dictionary <string, Node>();
                Dictionary <string, string> nodeData = new Dictionary <string, string>();

                hdriIndex          = d.hdriIndex;
                Name               = d.name;
                OutputNodes        = d.outputs;
                InputNodes         = d.inputs;
                defaultTextureType = d.defaultTextureType;
                ShiftX             = d.shiftX;
                ShiftY             = d.shiftY;
                Zoom               = d.zoom;
                width              = d.width;
                height             = d.height;

                if (width == 0 || width == int.MaxValue)
                {
                    width = 256;
                }
                if (height == 0 || height == int.MaxValue)
                {
                    height = 256;
                }

                SetJsonReadyCustomParameters(d.customParameters);
                SetJsonReadyCustomFunctions(d.customFunctions);

                //parse node data
                //setup initial object instances
                foreach (string s in d.nodes)
                {
                    Node.NodeData nd = JsonConvert.DeserializeObject <Node.NodeData>(s);

                    if (nd != null)
                    {
                        string type = nd.type;
                        if (!string.IsNullOrEmpty(type))
                        {
                            try
                            {
                                Type t = Type.GetType(type);
                                if (t != null)
                                {
                                    //special case to handle output nodes
                                    if (t.Equals(typeof(OutputNode)))
                                    {
                                        OutputNode n = new OutputNode(defaultTextureType);
                                        n.ParentGraph = this;
                                        n.Id          = nd.id;
                                        lookup[nd.id] = n;
                                        Nodes.Add(n);
                                        nodeData[nd.id] = s;
                                        tempData[nd.id] = nd;
                                    }
                                    else if (t.Equals(typeof(InputNode)))
                                    {
                                        InputNode n = new InputNode(defaultTextureType);
                                        n.ParentGraph = this;
                                        n.Id          = nd.id;
                                        lookup[nd.id] = n;
                                        Nodes.Add(n);
                                        nodeData[nd.id] = s;
                                        tempData[nd.id] = nd;
                                    }
                                    else
                                    {
                                        Node n = (Node)Activator.CreateInstance(t, nd.width, nd.height, defaultTextureType);
                                        if (n != null)
                                        {
                                            n.ParentGraph = this;
                                            n.Id          = nd.id;
                                            lookup[nd.id] = n;
                                            Nodes.Add(n);
                                            nodeData[nd.id] = s;
                                            tempData[nd.id] = nd;
                                        }
                                    }
                                }
                                else
                                {
                                    //log we could not load graph node
                                }
                            }
                            catch
                            {
                                //log we could not load graph node
                            }
                        }
                    }
                }

                //after setting initial lookup
                NodeLookup = lookup;

                //and before applying real data to each node...
                //we need to populate the parameters for them
                SetJsonReadyParameters(d.parameters);

                //apply data to nodes
                foreach (Node n in Nodes)
                {
                    string ndata = null;
                    nodeData.TryGetValue(n.Id, out ndata);

                    if (!string.IsNullOrEmpty(ndata))
                    {
                        n.FromJson(lookup, ndata);

                        //origin sizes are only for graph instances
                        //not actually used in the current one being edited
                        //it is used in the ResizeWith
                        OriginSizes[n.Id] = new Point(n.Width, n.Height);
                    }
                }

                if (!(this is FunctionGraph))
                {
                    SetConnections();
                }
            }
        }