void InstantiateNodes(string[] serializedNodes, Vector2 posOffset, string undoMsg)
        {
            // Make sure it knows about the editor
            SF_Parser.editor = editor;

            List <SF_Node> newNodes = new List <SF_Node>();             // List of all new nodes
            List <SF_Link> links    = new List <SF_Link>();             // Used for multi-clone

            int[] idOld = new int[serializedNodes.Length];
            int[] idNew = new int[serializedNodes.Length];

            for (int i = 0; i < serializedNodes.Length; i++)
            {
                SF_Node node = SF_Node.Deserialize(serializedNodes[i], ref links);
                if (node.IsProperty())
                {
                    if (editor.PropertyNameTaken(node.property))
                    {
                        node.property.SetName(node.property.GetClonedName());                           // Rename if needed
                        node.variableName = node.property.nameInternal;
                    }
                }

                idOld[i] = node.id;
                node.AssignID();                 // Increment IDs
                if (!node.IsProperty())
                {
                    node.ResetVariableName();
                }
                idNew[i]     = node.id;
                node.rect.x += posOffset.x;
                node.rect.y += posOffset.y;
                newNodes.Add(node);
            }

            // Establish all links
            foreach (SF_Link link in links)
            {
                link.Remap(idOld, idNew);
                link.Establish(editor, LinkingMethod.Default);
            }
            Undo.IncrementCurrentGroup();
            DeselectAll(registerUndo: true, undoMsg: undoMsg);
            Undo.CollapseUndoOperations(Undo.GetCurrentGroup() - 1);
            Event.current.Use();
            foreach (SF_Node n in newNodes)
            {
                n.Select(registerUndo: false);
            }
        }
예제 #2
0
        private static bool LoadFromNodeData(string data, float version, out string missingNode)
        {
            // First, split by rows (;)
            missingNode = "";
            string[] rows = data.Split(';');               // TODO: Escape ; and | characters in user created comments!

            // TODO: Subshaders etc
            SF_Parser.settingUp = true;
            SF_Parser.quickLoad = true;
            foreach (string row in rows)
            {
                if (row.StartsWith("n:"))
                {
                    //Debug.Log("Deserializing node:" + row);
                    SF_Node node = SF_Node.Deserialize(row.Substring(2), ref links);
                    if (node == null)
                    {
                        missingNode         = row.Substring(2).Split(',')[0].Split(':')[1];
                        SF_Parser.settingUp = false;
                        SF_Parser.quickLoad = false;
                        return(false);                        // Interrupt node loading, node wasn't found
                    }
                    continue;
                }
                if (row.StartsWith("ps:"))
                {
                    editor.ps.Deserialize(row.Substring(3));
                    continue;
                }
                if (row.StartsWith("proporder:"))
                {
                    editor.nodeView.treeStatus.DeserializeProps(row.Substring(10));
                    continue;
                }
            }

            // Create all node links
            for (int i = 0; i < links.Count; i++)
            {
                links[i].Establish(editor);
            }


            // If this was created in a version older than 0.37, reverse the node tree around its center point
            if (version <= 0.36f)
            {
                Debug.Log("Reversing node tree due to shader being created before the reversal in 0.37");

                // Average node position
                float avgX = editor.nodes.Average(x => x.rect.center.x);

                // Reverse all nodes
                foreach (SF_Node node in editor.nodes)
                {
                    Vector2 old = node.rect.center;
                    node.rect.center = new Vector2(2 * avgX - old.x, old.y);
                }
            }



            //Debug.Log("All links established, hierarchally refreshing...");
            // Refresh hierarchally

            //Profiler.BeginSample ("MyPieceOfCode");

            //editor.nodeView.HierarchalRefresh();

            //Profiler.EndSample();


            //Debug.Log( "Reconnect pending..." );

            editor.nodeView.ReconnectConnectedPending();
            SF_Parser.quickLoad = false;

            //Debug.Log( "Reconnect done, updating auto settings..." );

            // Update auto settings based on everything connected
            editor.ps.UpdateAutoSettings();

            //Debug.Log( "Auto settings done, centering camera..." );

            // Center camera
            editor.nodeView.CenterCamera();
            SF_Parser.settingUp = false;
            SF_Parser.quickLoad = false;


            // Update preview images by refreshing all outermost nodes
            editor.nodeView.HierarchalRefresh();

            //Debug.Log( "Centered camera, recompiling shader..." );
            editor.mainNode.OnUpdateNode(NodeUpdateType.Hard, true);

            return(true);
        }