Ejemplo n.º 1
0
 /// <summary>
 /// Uncompresses the nodeCanvas, meaning it will restore duplicate references for convenience
 /// </summary>
 public static void Uncompress(ref NodeCanvas nodeCanvas)
 {
     //For Backward Compatibility of Old Canvas
     for (int nodeCnt = 0; nodeCnt < nodeCanvas.nodes.Count; nodeCnt++)
     {
         Node node = nodeCanvas.nodes[nodeCnt];
         if ((node.Inputs == null || node.Inputs.Count == 0) || (node.Outputs == null || node.Outputs.Count == 0))
         {
             node.Inputs  = new List <NodeInput>();
             node.Outputs = new List <NodeOutput>();
             for (int knobCnt = 0; knobCnt < node.nodeKnobs.Count; knobCnt++)
             {
                 NodeKnob knob = node.nodeKnobs[knobCnt];
                 if (knob is NodeInput)
                 {
                     node.Inputs.Add(knob as NodeInput);
                 }
                 else if (knob is NodeOutput)
                 {
                     node.Outputs.Add(knob as NodeOutput);
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Will validate this canvas for any broken nodes or references and cleans them.
        /// </summary>
        public void Validate()
        {
            if (nodes == null)
            {
                Debug.LogWarning("NodeCanvas '" + name + "' nodes were erased and set to null! Automatically fixed!");
                nodes = new List <Node> ();
            }
            for (int nodeCnt = 0; nodeCnt < nodes.Count; nodeCnt++)
            {
                Node node = nodes[nodeCnt];
                if (node == null)
                {
                    Debug.LogWarning("NodeCanvas '" + name + "' contained broken (null) nodes! Automatically fixed!");
                    nodes.RemoveAt(nodeCnt);
                    nodeCnt--;
                    continue;
                }
                for (int knobCnt = 0; knobCnt < node.nodeKnobs.Count; knobCnt++)
                {
                    NodeKnob nodeKnob = node.nodeKnobs[knobCnt];
                    if (nodeKnob == null)
                    {
                        Debug.LogWarning("NodeCanvas '" + name + "' Node '" + node.name + "' contained broken (null) NodeKnobs! Automatically fixed!");
                        node.nodeKnobs.RemoveAt(knobCnt);
                        knobCnt--;
                        continue;
                    }

                    if (nodeKnob is NodeInput)
                    {
                        NodeInput input = nodeKnob as NodeInput;
                        if (input.connection != null && input.connection.body == null)
                        {                         // References broken node; Clear connection
                            input.connection = null;
                        }
//						for (int conCnt = 0; conCnt < (nodeKnob as NodeInput).connection.Count; conCnt++)
                    }
                    else if (nodeKnob is NodeOutput)
                    {
                        NodeOutput output = nodeKnob as NodeOutput;
                        for (int conCnt = 0; conCnt < output.connections.Count; conCnt++)
                        {
                            NodeInput con = output.connections[conCnt];
                            if (con == null || con.body == null)
                            {                             // Broken connection; Clear connection
                                output.connections.RemoveAt(conCnt);
                                conCnt--;
                            }
                        }
                    }
                }
            }

            if (editorStates == null)
            {
                Debug.LogWarning("NodeCanvas '" + name + "' editorStates were erased! Automatically fixed!");
                editorStates = new NodeEditorState[0];
            }
            editorStates = editorStates.Where((NodeEditorState state) => state != null).ToArray();
        }
Ejemplo n.º 3
0
        private void SaveNewNodeKnob(NodeKnob knob)
        {
            if (!useCache)
            {
                return;
            }
            CheckCurrentCache();

            if (nodeCanvas.livesInScene)
            {
                return;
            }
            if (!nodeCanvas.nodes.Contains(knob.body))
            {
                return;
            }

            NodeEditorSaveManager.AddSubAsset(knob, knob.body);
            foreach (ScriptableObject so in knob.GetScriptableObjects())
            {
                NodeEditorSaveManager.AddSubAsset(so, knob);
            }

            UpdateCacheFile();
        }
Ejemplo n.º 4
0
 public static void IssueOnAddNodeKnob(NodeKnob nodeKnob)
 {
     if (OnAddNodeKnob != null)
     {
         OnAddNodeKnob.Invoke(nodeKnob);
     }
     for (int cnt = 0; cnt < receiverCount; cnt++)
     {
         if (callbackReceiver [cnt] == null)
         {
             callbackReceiver.RemoveAt(cnt--);
         }
         else
         {
             callbackReceiver [cnt].OnAddNodeKnob(nodeKnob);
         }
     }
 }
Ejemplo n.º 5
0
 public static void IssueOnAddNodeKnob(NodeKnob nodeKnob)
 {
     if (OnAddNodeKnob != null)
     {
         OnAddNodeKnob(nodeKnob);
     }
     for (int i = 0; i < receiverCount; i++)
     {
         if ((UnityEngine.Object)callbackReceiver[i] == (UnityEngine.Object)null)
         {
             callbackReceiver.RemoveAt(i--);
         }
         else
         {
             callbackReceiver[i].OnAddNodeKnob(nodeKnob);
         }
     }
 }
        /// <summary>
        /// Compresses the nodeCanvas, meaning it will remove duplicate references that are only for convenience
        /// </summary>

        /*public static void Compress (ref NodeCanvas nodeCanvas)
         * {
         *      for (int nodeCnt = 0; nodeCnt < nodeCanvas.nodes.Count; nodeCnt++)
         *      {
         *              Node node = nodeCanvas.nodes[nodeCnt];
         *              node.Inputs = new List<NodeInput> ();
         *              node.Outputs = new List<NodeOutput> ();
         *      }
         * }*/


        /// <summary>
        /// Uncompresses the nodeCanvas, meaning it will restore duplicate references for convenience
        /// </summary>
        public static void Uncompress(ref NodeCanvas nodeCanvas)
        {
            for (int nodeCnt = 0; nodeCnt < nodeCanvas.nodes.Count; nodeCnt++)
            {
                Node node = nodeCanvas.nodes[nodeCnt];
                node.Inputs  = new List <NodeInput> ();
                node.Outputs = new List <NodeOutput> ();
                for (int knobCnt = 0; knobCnt < node.nodeKnobs.Count; knobCnt++)
                {
                    NodeKnob knob = node.nodeKnobs[knobCnt];
                    if (knob is NodeInput)
                    {
                        node.Inputs.Add(knob as NodeInput);
                    }
                    else if (knob is NodeOutput)
                    {
                        node.Outputs.Add(knob as NodeOutput);
                    }
                }
            }
        }
Ejemplo n.º 7
0
 public static void Uncompress(ref NodeCanvas nodeCanvas)
 {
     for (int i = 0; i < nodeCanvas.nodes.Count; i++)
     {
         Node node = nodeCanvas.nodes[i];
         if (node.Inputs == null || node.Inputs.Count == 0 || node.Outputs == null || node.Outputs.Count == 0)
         {
             node.Inputs  = new List <NodeInput>();
             node.Outputs = new List <NodeOutput>();
             for (int j = 0; j < node.nodeKnobs.Count; j++)
             {
                 NodeKnob nodeKnob = node.nodeKnobs[j];
                 if (nodeKnob is NodeInput)
                 {
                     node.Inputs.Add(nodeKnob as NodeInput);
                 }
                 else if (nodeKnob is NodeOutput)
                 {
                     node.Outputs.Add(nodeKnob as NodeOutput);
                 }
             }
         }
     }
 }
        /// <summary>
        /// Returns the node at the specified canvas-space position in the specified editor and returns a possible focused knob aswell
        /// </summary>
        public static Node NodeAtPosition(NodeEditorState editorState, Vector2 canvasPos, out NodeKnob focusedKnob)
        {
            focusedKnob = null;
            if (NodeEditorInputSystem.shouldIgnoreInput(editorState))
            {
                return(null);
            }
            NodeCanvas canvas = editorState.canvas;

            for (int nodeCnt = canvas.nodes.Count - 1; nodeCnt >= 0; nodeCnt--)
            {             // Check from top to bottom because of the render order
                Node node = canvas.nodes [nodeCnt];
                if (node.rect.Contains(canvasPos))
                {
                    return(node);
                }
                for (int knobCnt = 0; knobCnt < node.nodeKnobs.Count; knobCnt++)
                {                 // Check if any nodeKnob is focused instead
                    if (node.nodeKnobs[knobCnt].GetCanvasSpaceKnob().Contains(canvasPos))
                    {
                        focusedKnob = node.nodeKnobs[knobCnt];
                        return(node);
                    }
                }
            }
            return(null);
        }
 /// <summary>
 /// Returns the node at the specified canvas-space position in the current editor and returns a possible focused knob aswell
 /// </summary>
 public static Node NodeAtPosition(Vector2 canvasPos, out NodeKnob focusedKnob)
 {
     return(NodeAtPosition(curEditorState, canvasPos, out focusedKnob));
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Validates this canvas, checking for any broken nodes or references and cleans them.
        /// </summary>
        public void Validate(bool deepValidate = false)
        {
            if (nodes == null)
            {
                Debug.LogWarning("NodeCanvas '" + name + "' nodes were erased and set to null! Automatically fixed!");
                nodes = new List <Node> ();
            }
            if (deepValidate)
            {
                for (int groupCnt = 0; groupCnt < groups.Count; groupCnt++)
                {
                    NodeGroup group = groups[groupCnt];
                    if (group == null)
                    {
                        Debug.LogWarning("NodeCanvas '" + name + "' contained broken (null) group! Automatically fixed!");
                        groups.RemoveAt(groupCnt);
                        groupCnt--;
                        continue;
                    }
                }
                for (int nodeCnt = 0; nodeCnt < nodes.Count; nodeCnt++)
                {
                    Node node = nodes[nodeCnt];
                    if (node == null)
                    {
                        Debug.LogWarning("NodeCanvas '" + saveName + "' (" + name + ") contained broken (null) node! Automatically fixed!");
                        nodes.RemoveAt(nodeCnt);
                        nodeCnt--;
                        continue;
                    }
                    for (int knobCnt = 0; knobCnt < node.nodeKnobs.Count; knobCnt++)
                    {
                        NodeKnob nodeKnob = node.nodeKnobs[knobCnt];
                        if (nodeKnob == null)
                        {
                            Debug.LogWarning("NodeCanvas '" + name + "' Node '" + node.name + "' contained broken (null) NodeKnobs! Automatically fixed!");
                            nodes.RemoveAt(nodeCnt);
                            nodeCnt--;
                            break;
//							node.nodeKnobs.RemoveAt (knobCnt);
//							knobCnt--;
//							continue;
                        }

                        if (nodeKnob is NodeInput)
                        {
                            NodeInput input = nodeKnob as NodeInput;
                            if (input.connection != null && input.connection.body == null)
                            {                             // References broken node; Clear connection
                                input.connection = null;
                            }
                            //						for (int conCnt = 0; conCnt < (nodeKnob as NodeInput).connection.Count; conCnt++)
                        }
                        else if (nodeKnob is NodeOutput)
                        {
                            NodeOutput output = nodeKnob as NodeOutput;
                            for (int conCnt = 0; conCnt < output.connections.Count; conCnt++)
                            {
                                NodeInput con = output.connections[conCnt];
                                if (con == null || con.body == null)
                                {                                 // Broken connection; Clear connection
                                    output.connections.RemoveAt(conCnt);
                                    conCnt--;
                                }
                            }
                        }
                    }
                }
                if (editorStates == null)
                {
                    Debug.LogWarning("NodeCanvas '" + name + "' editorStates were erased! Automatically fixed!");
                    editorStates = new NodeEditorState[0];
                }
                editorStates = editorStates.Where((NodeEditorState state) => state != null).ToArray();
                foreach (NodeEditorState state in editorStates)
                {
                    if (!nodes.Contains(state.selectedNode))
                    {
                        state.selectedNode = null;
                    }
                }
            }
            OnValidate();
        }
 public void Validate()
 {
     if (nodes == null)
     {
         Debug.LogWarning("NodeCanvas '" + base.name + "' nodes were erased and set to null! Automatically fixed!");
         nodes = new List <Node>();
     }
     for (int i = 0; i < nodes.Count; i++)
     {
         Node node = nodes[i];
         if ((Object)node == (Object)null)
         {
             Debug.LogWarning("NodeCanvas '" + base.name + "' contained broken (null) nodes! Automatically fixed!");
             nodes.RemoveAt(i);
             i--;
         }
         else
         {
             for (int j = 0; j < node.Inputs.Count; j++)
             {
                 NodeInput nodeInput = node.Inputs[j];
                 if ((Object)nodeInput == (Object)null)
                 {
                     Debug.LogWarning("NodeCanvas '" + base.name + "' Node '" + node.name + "' contained broken (null) NodeKnobs! Automatically fixed!");
                     node.Inputs.RemoveAt(j);
                     j--;
                 }
                 else if ((Object)nodeInput.connection != (Object)null && (Object)nodeInput.connection.body == (Object)null)
                 {
                     nodeInput.connection = null;
                 }
             }
             for (int k = 0; k < node.Outputs.Count; k++)
             {
                 NodeOutput nodeOutput = node.Outputs[k];
                 if ((Object)nodeOutput == (Object)null)
                 {
                     Debug.LogWarning("NodeCanvas '" + base.name + "' Node '" + node.name + "' contained broken (null) NodeKnobs! Automatically fixed!");
                     node.Outputs.RemoveAt(k);
                     k--;
                 }
                 else
                 {
                     for (int l = 0; l < nodeOutput.connections.Count; l++)
                     {
                         NodeInput nodeInput2 = nodeOutput.connections[l];
                         if ((Object)nodeInput2 == (Object)null || (Object)nodeInput2.body == (Object)null)
                         {
                             nodeOutput.connections.RemoveAt(l);
                             l--;
                         }
                     }
                 }
             }
             for (int m = 0; m < node.nodeKnobs.Count; m++)
             {
                 NodeKnob nodeKnob = node.nodeKnobs[m];
                 if ((Object)nodeKnob == (Object)null)
                 {
                     Debug.LogWarning("NodeCanvas '" + base.name + "' Node '" + node.name + "' contained broken (null) NodeKnobs! Automatically fixed!");
                     node.nodeKnobs.RemoveAt(m);
                     m--;
                 }
                 else if (nodeKnob is NodeInput)
                 {
                     NodeInput nodeInput3 = nodeKnob as NodeInput;
                     if ((Object)nodeInput3.connection != (Object)null && (Object)nodeInput3.connection.body == (Object)null)
                     {
                         nodeInput3.connection = null;
                     }
                 }
                 else if (nodeKnob is NodeOutput)
                 {
                     NodeOutput nodeOutput2 = nodeKnob as NodeOutput;
                     for (int n = 0; n < nodeOutput2.connections.Count; n++)
                     {
                         NodeInput nodeInput4 = nodeOutput2.connections[n];
                         if ((Object)nodeInput4 == (Object)null || (Object)nodeInput4.body == (Object)null)
                         {
                             nodeOutput2.connections.RemoveAt(n);
                             n--;
                         }
                     }
                 }
             }
         }
     }
     if (editorStates == null)
     {
         Debug.LogWarning("NodeCanvas '" + base.name + "' editorStates were erased! Automatically fixed!");
         editorStates = new NodeEditorState[0];
     }
     editorStates = (from state in editorStates
                     where (Object)state != (Object)null
                     select state).ToArray();
     NodeEditorState[] array = editorStates;
     foreach (NodeEditorState nodeEditorState in array)
     {
         if (!nodes.Contains(nodeEditorState.selectedNode))
         {
             nodeEditorState.selectedNode = null;
         }
     }
 }
Ejemplo n.º 12
0
        public static NodeCanvas CreateWorkingCopy(NodeCanvas nodeCanvas, bool editorStates)
        {
            nodeCanvas.Validate();
            nodeCanvas = Clone(nodeCanvas);
            List <ScriptableObject> allSOs    = new List <ScriptableObject>();
            List <ScriptableObject> clonedSOs = new List <ScriptableObject>();

            for (int i = 0; i < nodeCanvas.nodes.Count; i++)
            {
                Node node = nodeCanvas.nodes[i];
                node.CheckNodeKnobMigration();
                Node node2 = AddClonedSO(allSOs, clonedSOs, node);
                AddClonedSOs(allSOs, clonedSOs, node2.GetScriptableObjects());
                foreach (NodeKnob nodeKnob3 in node2.nodeKnobs)
                {
                    AddClonedSO(allSOs, clonedSOs, nodeKnob3);
                    AddClonedSOs(allSOs, clonedSOs, nodeKnob3.GetScriptableObjects());
                }
            }
            for (int j = 0; j < nodeCanvas.nodes.Count; j++)
            {
                Node initialSO = nodeCanvas.nodes[j];
                Node node3     = ReplaceSO(allSOs, clonedSOs, initialSO);
                nodeCanvas.nodes[j] = node3;
                Node node4 = node3;
                node4.CopyScriptableObjects((ScriptableObject so) => ReplaceSO(allSOs, clonedSOs, so));
                for (int k = 0; k < node4.nodeKnobs.Count; k++)
                {
                    NodeKnob nodeKnob = ReplaceSO(allSOs, clonedSOs, node4.nodeKnobs[k]);
                    node4.nodeKnobs[k] = nodeKnob;
                    NodeKnob nodeKnob2 = nodeKnob;
                    nodeKnob2.body = node4;
                    nodeKnob2.CopyScriptableObjects((ScriptableObject so) => ReplaceSO(allSOs, clonedSOs, so));
                }
                for (int l = 0; l < node4.Inputs.Count; l++)
                {
                    NodeInput nodeInput = ReplaceSO(allSOs, clonedSOs, node4.Inputs[l]);
                    node4.Inputs[l] = nodeInput;
                    NodeInput nodeInput2 = nodeInput;
                    nodeInput2.body = node4;
                }
                for (int m = 0; m < node4.Outputs.Count; m++)
                {
                    NodeOutput nodeOutput = ReplaceSO(allSOs, clonedSOs, node4.Outputs[m]);
                    node4.Outputs[m] = nodeOutput;
                    NodeOutput nodeOutput2 = nodeOutput;
                    nodeOutput2.body = node4;
                }
            }
            if (editorStates)
            {
                nodeCanvas.editorStates = CreateWorkingCopy(nodeCanvas.editorStates, nodeCanvas);
                NodeEditorState[] editorStates2 = nodeCanvas.editorStates;
                foreach (NodeEditorState nodeEditorState in editorStates2)
                {
                    nodeEditorState.selectedNode = ReplaceSO(allSOs, clonedSOs, nodeEditorState.selectedNode);
                }
            }
            else
            {
                NodeEditorState[] editorStates3 = nodeCanvas.editorStates;
                foreach (NodeEditorState nodeEditorState2 in editorStates3)
                {
                    nodeEditorState2.selectedNode = null;
                }
            }
            return(nodeCanvas);
        }
        /// <summary>
        /// Creates a working copy of the specified nodeCanvas, and optionally also of it's associated editorStates.
        /// This breaks the link of this object to any stored assets and references. That means, that all changes to this object will have to be explicitly saved.
        /// </summary>
        public static NodeCanvas CreateWorkingCopy(NodeCanvas nodeCanvas, bool editorStates)
        {
            nodeCanvas.Validate(true);

            // Lists holding initial and cloned version of each ScriptableObject for later replacement of references
            List <ScriptableObject> allSOs    = new List <ScriptableObject> ();
            List <ScriptableObject> clonedSOs = new List <ScriptableObject> ();

            System.Func <ScriptableObject, ScriptableObject> copySOs = (ScriptableObject so) => ReplaceSO(allSOs, clonedSOs, so);

            // Clone and enter the canvas object and it's referenced SOs
            nodeCanvas = AddClonedSO(allSOs, clonedSOs, nodeCanvas);
            AddClonedSOs(allSOs, clonedSOs, nodeCanvas.GetScriptableObjects());

            // Iterate over the core ScriptableObjects in the canvas and clone them
            for (int nodeCnt = 0; nodeCnt < nodeCanvas.nodes.Count; nodeCnt++)
            {
                Node node = nodeCanvas.nodes[nodeCnt];
                node.CheckNodeKnobMigration();

                // Clone Node and additional scriptableObjects
                Node clonedNode = AddClonedSO(allSOs, clonedSOs, node);
                AddClonedSOs(allSOs, clonedSOs, clonedNode.GetScriptableObjects());

                // Clone NodeKnobs
                foreach (NodeKnob knob in clonedNode.nodeKnobs)
                {
                    AddClonedSO(allSOs, clonedSOs, knob);
                }
            }

            // Replace every reference to any of the initial SOs of the first list with the respective clones of the second list
            nodeCanvas.CopyScriptableObjects(copySOs);

            for (int nodeCnt = 0; nodeCnt < nodeCanvas.nodes.Count; nodeCnt++)
            {             // Replace SOs in all Nodes
                Node node = nodeCanvas.nodes[nodeCnt];

                // Replace node and additional ScriptableObjects with their copies
                Node clonedNode = nodeCanvas.nodes[nodeCnt] = ReplaceSO(allSOs, clonedSOs, node);
                clonedNode.CopyScriptableObjects(copySOs);

                // Replace NodeKnobs and restore Inputs/Outputs by NodeKnob type
                clonedNode.Inputs  = new List <NodeInput> ();
                clonedNode.Outputs = new List <NodeOutput> ();
                for (int knobCnt = 0; knobCnt < clonedNode.nodeKnobs.Count; knobCnt++)
                {                 // Clone generic NodeKnobs
                    NodeKnob knob = clonedNode.nodeKnobs[knobCnt] = ReplaceSO(allSOs, clonedSOs, clonedNode.nodeKnobs[knobCnt]);
                    knob.body = clonedNode;
                    knob.CopyScriptableObjects(copySOs);
                    // Add inputs/outputs to their lists again
                    if (knob is NodeInput)
                    {
                        clonedNode.Inputs.Add(knob as NodeInput);
                    }
                    else if (knob is NodeOutput)
                    {
                        clonedNode.Outputs.Add(knob as NodeOutput);
                    }
                }
            }

            if (editorStates)             // Clone the editorStates
            {
                nodeCanvas.editorStates = CreateWorkingCopy(nodeCanvas.editorStates, nodeCanvas);
            }
            // Fix references in editorStates to Nodes in the canvas
            foreach (NodeEditorState state in nodeCanvas.editorStates)
            {
                state.selectedNode = ReplaceSO(allSOs, clonedSOs, state.selectedNode);
            }

            return(nodeCanvas);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// CreateWorkingCopy a working copy of the canvas and each editorState. This will break the link to the canvas asset and thus all changes made to the working copy have to be explicitly saved.
        /// Check compressed if the copy is not intended for useage but for storage, this will leave the Inputs and Outputs list of Node empty
        /// </summary>
        public static void CreateWorkingCopy(ref NodeCanvas nodeCanvas, NodeEditorState[] editorStates, bool compressed)
        {
            nodeCanvas = Clone(nodeCanvas);

            // Take each SO, make a clone of it and store both versions in the respective list
            // This will only iterate over the 'source instances'
            List <ScriptableObject> allSOs    = new List <ScriptableObject> ();
            List <ScriptableObject> clonedSOs = new List <ScriptableObject> ();

            for (int nodeCnt = 0; nodeCnt < nodeCanvas.nodes.Count; nodeCnt++)
            {
                Node node = nodeCanvas.nodes[nodeCnt];
                node.CheckNodeKnobMigration();
                Node clonedNode = (Node)AddClonedSO(allSOs, clonedSOs, node);
                for (int knobCnt = 0; knobCnt < clonedNode.nodeKnobs.Count; knobCnt++)
                {                 // Clone NodeKnobs
//					Debug.Log ("Cloned " + knobCnt + " " + (clonedNode.nodeKnobs[knobCnt] == null? "null" : clonedNode.nodeKnobs[knobCnt].name) + " on node " + node.name);
                    AddClonedSO(allSOs, clonedSOs, clonedNode.nodeKnobs[knobCnt]);
                    // Clone additional scriptableObjects
                    ScriptableObject[] additionalKnobSOs = clonedNode.nodeKnobs[knobCnt].GetScriptableObjects();
                    foreach (ScriptableObject so in additionalKnobSOs)
                    {
                        AddClonedSO(allSOs, clonedSOs, so);
                    }
                }
                for (int transCnt = 0; transCnt < clonedNode.transitions.Count; transCnt++)
                {                 // Clone Transitions
                    Transition trans = clonedNode.transitions[transCnt];
                    if (trans.startNode == node)
                    {
                        AddClonedSO(allSOs, clonedSOs, trans);
//						Debug.Log ("Did copy Transition " + trans.name + " because its Node " + clonedNode.name + " is the start node!");
                    }
                    else
                    {
//						Debug.Log ("Did NOT copy Transition " + trans.name + " because its Node " + clonedNode.name + " is NOT the start node (" + trans.startNode.name + ")!");
                        clonedNode.transitions.RemoveAt(transCnt);
                        transCnt--;
                    }
                }
                // Clone additional scriptableObjects
                ScriptableObject[] additionalNodeSOs = clonedNode.GetScriptableObjects();
                foreach (ScriptableObject so in additionalNodeSOs)
                {
                    AddClonedSO(allSOs, clonedSOs, so);
                }
            }

            // Replace every reference to any of the initial SOs of the first list with the respective clones of the second list

            nodeCanvas.currentNode       = ReplaceSO(allSOs, clonedSOs, nodeCanvas.currentNode);
            nodeCanvas.currentTransition = ReplaceSO(allSOs, clonedSOs, nodeCanvas.currentTransition);
            for (int nodeCnt = 0; nodeCnt < nodeCanvas.nodes.Count; nodeCnt++)
            {             // Clone Nodes, structural content and additional scriptableObjects
                Node node       = nodeCanvas.nodes[nodeCnt];
                Node clonedNode = nodeCanvas.nodes[nodeCnt] = ReplaceSO(allSOs, clonedSOs, node);
                // We're going to restore these from NodeKnobs if desired (!compressed)
                clonedNode.Inputs  = new List <NodeInput> ();
                clonedNode.Outputs = new List <NodeOutput> ();
                for (int knobCnt = 0; knobCnt < clonedNode.nodeKnobs.Count; knobCnt++)
                {                 // Clone generic NodeKnobs
                    NodeKnob knob = clonedNode.nodeKnobs[knobCnt] = ReplaceSO(allSOs, clonedSOs, clonedNode.nodeKnobs[knobCnt]);
                    knob.body = clonedNode;
                    // Replace additional scriptableObjects in the NodeKnob
                    knob.CopyScriptableObjects((ScriptableObject so) => ReplaceSO(allSOs, clonedSOs, so));
                    if (!compressed)
                    {                     // Add NodeInputs and NodeOutputs to the apropriate lists in Node if desired (!compressed)
                        if (knob is NodeInput)
                        {
                            clonedNode.Inputs.Add(knob as NodeInput);
                        }
                        else if (knob is NodeOutput)
                        {
                            clonedNode.Outputs.Add(knob as NodeOutput);
                        }
                    }
                }
                for (int transCnt = 0; transCnt < clonedNode.transitions.Count; transCnt++)
                {                 // Clone transitions
                    Transition trans = clonedNode.transitions[transCnt];
                    if (trans.startNode != node)
                    {
                        continue;
                    }
                    trans = clonedNode.transitions[transCnt] = ReplaceSO(allSOs, clonedSOs, trans);
                    if (trans == null)
                    {
                        Debug.LogError("Could not copy transition number " + transCnt + " of Node " + clonedNode.name + "!");
                        continue;
                    }

//					Debug.Log ("Did replace contents of Transition " + trans.name + " because its Node " + clonedNode.name + " is the start node!");
                    trans.startNode = ReplaceSO(allSOs, clonedSOs, trans.startNode);
                    trans.endNode   = ReplaceSO(allSOs, clonedSOs, trans.endNode);

                    if (!compressed)
                    {
                        trans.endNode.transitions.Add(trans);
                    }
                }
                // Replace additional scriptableObjects in the Node
                node.CopyScriptableObjects((ScriptableObject so) => ReplaceSO(allSOs, clonedSOs, so));
            }

            // Also create working copies for specified editorStates, if any
            // Needs to be in the same function as the EditorState references nodes from the NodeCanvas
            if (editorStates != null)
            {
                for (int stateCnt = 0; stateCnt < editorStates.Length; stateCnt++)
                {
                    if (editorStates[stateCnt] == null)
                    {
                        continue;
                    }

                    NodeEditorState state = editorStates[stateCnt] = Clone(editorStates[stateCnt]);
                    state.canvas       = nodeCanvas;
                    state.focusedNode  = null;
                    state.selectedNode = state.selectedNode != null?ReplaceSO(allSOs, clonedSOs, state.selectedNode) : null;

                    state.makeTransition = null;
                    state.connectOutput  = null;
                }
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// CreateWorkingCopy a working copy of the canvas and each editorState. This will break the link to the canvas asset and thus all changes made to the working copy have to be explicitly saved.
        /// Check compressed if the copy is not intended for useage but for storage, this will leave the Inputs and Outputs list of Node empty
        /// </summary>
        public static void CreateWorkingCopy(ref NodeCanvas nodeCanvas, NodeEditorState[] editorStates, bool compressed)
        {
            nodeCanvas = Clone(nodeCanvas);

            // Take each SO, make a clone of it and store both versions in the respective list
            // This will only iterate over the 'source instances'
            List <ScriptableObject> allSOs    = new List <ScriptableObject> ();
            List <ScriptableObject> clonedSOs = new List <ScriptableObject> ();

            foreach (Node node in nodeCanvas.nodes)
            {
                node.CheckNodeKnobMigration();
                Node clonedNode = AddClonedSO(allSOs, clonedSOs, node);
                foreach (NodeKnob knob in clonedNode.nodeKnobs)
                {                 // Clone NodeKnobs
                    NodeKnob clonedKnob = AddClonedSO(allSOs, clonedSOs, knob);
                    // Clone additional scriptableObjects
                    ScriptableObject[] additionalKnobSOs = clonedKnob.GetScriptableObjects();
                    foreach (ScriptableObject so in additionalKnobSOs)
                    {
                        AddClonedSO(allSOs, clonedSOs, so);
                    }
                }
                // Clone additional scriptableObjects
                ScriptableObject[] additionalNodeSOs = clonedNode.GetScriptableObjects();
                foreach (ScriptableObject so in additionalNodeSOs)
                {
                    AddClonedSO(allSOs, clonedSOs, so);
                }
            }

            // Replace every reference to any of the initial SOs of the first list with the respective clones of the second list

            for (int nodeCnt = 0; nodeCnt < nodeCanvas.nodes.Count; nodeCnt++)
            {             // Clone Nodes, structural content and additional scriptableObjects
                Node clonedNode = nodeCanvas.nodes[nodeCnt] = ReplaceSO(allSOs, clonedSOs, nodeCanvas.nodes[nodeCnt]);
                // We're going to restore these from NodeKnobs if desired (!compressed)
                clonedNode.Inputs  = new List <NodeInput> ();
                clonedNode.Outputs = new List <NodeOutput> ();
                for (int knobCnt = 0; knobCnt < clonedNode.nodeKnobs.Count; knobCnt++)
                {                 // Clone generic NodeKnobs
                    NodeKnob clonedKnob = clonedNode.nodeKnobs[knobCnt] = ReplaceSO(allSOs, clonedSOs, clonedNode.nodeKnobs[knobCnt]);
                    clonedKnob.body = clonedNode;
                    if (!compressed)
                    {                     // Add NodeInputs and NodeOutputs to the apropriate lists in Node if desired (!compressed)
                        if (clonedKnob is NodeInput)
                        {
                            clonedNode.Inputs.Add(clonedKnob as NodeInput);
                        }
                        else if (clonedKnob is NodeOutput)
                        {
                            clonedNode.Outputs.Add(clonedKnob as NodeOutput);
                        }
                    }
                    // Replace additional scriptableObjects in the NodeKnob
                    clonedKnob.CopyScriptableObjects((ScriptableObject so) => ReplaceSO(allSOs, clonedSOs, so));
                }
                // Replace additional scriptableObjects in the Node
                clonedNode.CopyScriptableObjects((ScriptableObject so) => ReplaceSO(allSOs, clonedSOs, so));
            }

            // Also create working copies for specified editorStates, if any
            // Needs to be in the same function as the EditorState references nodes from the NodeCanvas
            if (editorStates != null)
            {
                for (int stateCnt = 0; stateCnt < editorStates.Length; stateCnt++)
                {
                    if (editorStates[stateCnt] == null)
                    {
                        continue;
                    }

                    NodeEditorState clonedState = editorStates[stateCnt] = Clone(editorStates[stateCnt]);
                    clonedState.canvas       = nodeCanvas;
                    clonedState.focusedNode  = null;
                    clonedState.selectedNode = clonedState.selectedNode != null?ReplaceSO(allSOs, clonedSOs, clonedState.selectedNode) : null;

                    clonedState.makeTransition = null;
                    clonedState.connectOutput  = null;
                }
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Creates a working copy of the specified nodeCanvas, and optionally also of it's associated editorStates.
        /// This breaks the link of this object to any stored assets and references. That means, that all changes to this object will have to be explicitly saved.
        /// </summary>
        public static NodeCanvas CreateWorkingCopy(NodeCanvas nodeCanvas, bool editorStates)
        {
            nodeCanvas.Validate();
            nodeCanvas = Clone(nodeCanvas);

            // Take each SO, make a clone of it and store both versions in the respective list
            // This will only iterate over the 'source instances'
            List <ScriptableObject> allSOs    = new List <ScriptableObject> ();
            List <ScriptableObject> clonedSOs = new List <ScriptableObject> ();

            for (int nodeCnt = 0; nodeCnt < nodeCanvas.nodes.Count; nodeCnt++)
            {
                Node node = nodeCanvas.nodes[nodeCnt];
                node.CheckNodeKnobMigration();

                // Clone Node and additional scriptableObjects
                Node clonedNode = AddClonedSO(allSOs, clonedSOs, node);
                AddClonedSOs(allSOs, clonedSOs, clonedNode.GetScriptableObjects());

                foreach (NodeKnob knob in clonedNode.nodeKnobs)
                {                 // Clone NodeKnobs and additional scriptableObjects
                    AddClonedSO(allSOs, clonedSOs, knob);
                    AddClonedSOs(allSOs, clonedSOs, knob.GetScriptableObjects());
                }
            }

            // Replace every reference to any of the initial SOs of the first list with the respective clones of the second list
            for (int nodeCnt = 0; nodeCnt < nodeCanvas.nodes.Count; nodeCnt++)
            {             // Clone Nodes, structural content and additional scriptableObjects
                Node node = nodeCanvas.nodes[nodeCnt];
                // Replace node and additional ScriptableObjects
                Node clonedNode = nodeCanvas.nodes[nodeCnt] = ReplaceSO(allSOs, clonedSOs, node);
                clonedNode.CopyScriptableObjects((ScriptableObject so) => ReplaceSO(allSOs, clonedSOs, so));

                // We're going to restore these from NodeKnobs if desired (!compressed)
                //clonedNode.Inputs = new List<NodeInput> ();
                //clonedNode.Outputs = new List<NodeOutput> ();
                for (int knobCnt = 0; knobCnt < clonedNode.nodeKnobs.Count; knobCnt++)
                {                 // Clone generic NodeKnobs
                    NodeKnob knob = clonedNode.nodeKnobs[knobCnt] = ReplaceSO(allSOs, clonedSOs, clonedNode.nodeKnobs[knobCnt]);
                    knob.body = clonedNode;
                    // Replace additional scriptableObjects in the NodeKnob
                    knob.CopyScriptableObjects((ScriptableObject so) => ReplaceSO(allSOs, clonedSOs, so));
                }
                for (int knobCnt = 0; knobCnt < clonedNode.Inputs.Count; knobCnt++)
                {                 // Clone generic NodeKnobs
                    NodeInput knob = clonedNode.Inputs[knobCnt] = ReplaceSO(allSOs, clonedSOs, clonedNode.Inputs[knobCnt]);
                    knob.body = clonedNode;
                }
                for (int knobCnt = 0; knobCnt < clonedNode.Outputs.Count; knobCnt++)
                {                 // Clone generic NodeKnobs
                    NodeOutput knob = clonedNode.Outputs[knobCnt] = ReplaceSO(allSOs, clonedSOs, clonedNode.Outputs[knobCnt]);
                    knob.body = clonedNode;
                }
            }

            if (editorStates)
            {
                nodeCanvas.editorStates = CreateWorkingCopy(nodeCanvas.editorStates, nodeCanvas);
                foreach (NodeEditorState state in nodeCanvas.editorStates)
                {
                    state.selectedNode = ReplaceSO(allSOs, clonedSOs, state.selectedNode);
                }
            }
            else
            {
                foreach (NodeEditorState state in nodeCanvas.editorStates)
                {
                    state.selectedNode = null;
                }
            }

            return(nodeCanvas);
        }
 public virtual void OnAddNodeKnob(NodeKnob knob)
 {
 }
        public static Node NodeAtPosition(NodeEditorState editorState, Vector2 canvasPos, out NodeKnob focusedKnob)
        {
            focusedKnob = null;
            if (NodeEditorInputSystem.shouldIgnoreInput(editorState))
            {
                return(null);
            }
            NodeCanvas canvas = editorState.canvas;

            for (int num = canvas.nodes.Count - 1; num >= 0; num--)
            {
                Node node = canvas.nodes[num];
                if (node.rect.Contains(canvasPos))
                {
                    return(node);
                }
                for (int i = 0; i < node.nodeKnobs.Count; i++)
                {
                    if (node.nodeKnobs[i].GetCanvasSpaceKnob().Contains(canvasPos))
                    {
                        focusedKnob = node.nodeKnobs[i];
                        return(node);
                    }
                }
            }
            return(null);
        }