Example #1
0
        /// <summary>
        /// Create the a Node of the type specified by the nodeID at position
        /// Auto-connects the passed connectingOutput if not null to the first compatible input
        /// </summary>
        public static Node Create(string nodeID, Vector2 position, NodeOutput connectingOutput)
        {
            Node node = NodeTypes.getDefaultNode(nodeID);

            if (node == null)
            {
                throw new UnityException("Cannot create Node with id " + nodeID + " as no such Node type is registered!");
            }

            node = node.Create(position);
            node.InitBase();

            if (connectingOutput != null)
            {
                // Handle auto-connection and link the output to the first compatible input
                foreach (NodeInput input in node.Inputs)
                {
                    if (input.TryApplyConnection(connectingOutput))
                    {
                        break;
                    }
                }
            }

            NodeEditorCallbacks.IssueOnAddNode(node);

            return(node);
        }
Example #2
0
        /// <summary>
        /// Creates a node of the specified ID at pos on the specified canvas, optionally auto-connecting the specified output to a matching input
        /// silent disables any events, init specifies whether OnCreate should be called
        /// </summary>
        public static Node Create(string nodeID, Vector2 pos, NodeCanvas hostCanvas, ConnectionPort connectingPort = null, bool silent = false, bool init = true)
        {
            if (string.IsNullOrEmpty(nodeID) || hostCanvas == null)
            {
                throw new ArgumentException();
            }
            if (!NodeCanvasManager.CheckCanvasCompability(nodeID, hostCanvas.GetType()))
            {
                throw new UnityException("Cannot create Node with ID '" + nodeID + "' as it is not compatible with the current canavs type (" + hostCanvas.GetType().ToString() + ")!");
            }
            if (!hostCanvas.CanAddNode(nodeID))
            {
                throw new UnityException("Cannot create Node with ID '" + nodeID + "' on the current canvas of type (" + hostCanvas.GetType().ToString() + ")!");
            }

            // Create node from data
            NodeTypeData data = NodeTypes.getNodeData(nodeID);
            Node         node = (Node)CreateInstance(data.type);

            if (node == null)
            {
                return(null);
            }

            // Init node state
            node.name     = node.Title;
            node.autoSize = node.DefaultSize;
            node.position = pos;
            node.Canvas   = hostCanvas;
            ConnectionPortManager.UpdateConnectionPorts(node);
            if (init)
            {
                node.OnCreate();
            }

            if (connectingPort != null)
            {             // Handle auto-connection and link the output to the first compatible input
                for (int i = 0; i < node.connectionPorts.Count; i++)
                {
                    if (node.connectionPorts[i].TryApplyConnection(connectingPort, silent))
                    {
                        break;
                    }
                }
            }

            // Add node to host canvas
            hostCanvas.nodes.Add(node);
            if (!silent)
            {             // Callbacks
                hostCanvas.OnNodeChange(connectingPort != null ? connectingPort.body : node);
                NodeEditorCallbacks.IssueOnAddNode(node);
                hostCanvas.Validate();
                NodeEditor.RepaintClients();
            }

            return(node);
        }
Example #3
0
        public static Node Create(string nodeID, Vector2 position)
        {
            Node node = NodeTypes.getDefaultNode(nodeID);

            if (node == null)
            {
                throw new UnityException("Cannot create Node with id " + nodeID + " as no such Node type is registered!");
            }

            node = node.Create(position);
            node.InitBase();

            NodeEditorCallbacks.IssueOnAddNode(node);
            return(node);
        }
Example #4
0
        /// <summary>
        /// Create the a Node of the type specified by the nodeID at position
        /// Auto-connects the passed connectingOutput if not null to the first compatible input
        /// </summary>
        public static Node Create(string nodeID, Vector2 position, NodeOutput connectingOutput)
        {
            if (!NodeCanvasManager.CheckCanvasCompability(nodeID, NodeEditor.curNodeCanvas))
            {
                throw new UnityException("Cannot create Node with ID '" + nodeID + "' as it is not compatible with the current canavs type (" + NodeEditor.curNodeCanvas.GetType().ToString() + ")!");
            }
            if (!NodeEditor.curNodeCanvas.CanAddNode(nodeID))
            {
                throw new UnityException("Cannot create another Node with ID '" + nodeID + "' on the current canvas of type (" + NodeEditor.curNodeCanvas.GetType().ToString() + ")!");
            }
            Node node = NodeTypes.getDefaultNode(nodeID);

            if (node == null)
            {
                throw new UnityException("Cannot create Node as ID '" + nodeID + "' is not registered!");
            }

            node = node.Create(position);

            if (node == null)
            {
                return(null);
            }

            node.InitBase();

            if (connectingOutput != null)
            {             // Handle auto-connection and link the output to the first compatible input
                foreach (NodeInput input in node.Inputs)
                {
                    if (input.TryApplyConnection(connectingOutput))
                    {
                        break;
                    }
                }
            }

            NodeEditorCallbacks.IssueOnAddNode(node);
            NodeEditor.curNodeCanvas.Validate();

            return(node);
        }
Example #5
0
        public static Node Create(string nodeID, Vector2 position, NodeOutput connectingOutput)
        {
            Node defaultNode = NodeTypes.getDefaultNode(nodeID);

            if ((UnityEngine.Object)defaultNode == (UnityEngine.Object)null)
            {
                throw new UnityException("Cannot create Node with id " + nodeID + " as no such Node type is registered!");
            }
            defaultNode = defaultNode.Create(position);
            defaultNode.InitBase();
            if ((UnityEngine.Object)connectingOutput != (UnityEngine.Object)null)
            {
                foreach (NodeInput input in defaultNode.Inputs)
                {
                    if (input.TryApplyConnection(connectingOutput))
                    {
                        break;
                    }
                }
            }
            NodeEditorCallbacks.IssueOnAddNode(defaultNode);
            return(defaultNode);
        }
        public static void DuplicateNodeOrNodes(NodeEditorInputInfo inputInfo)
        {
            if (inputInfo.editorState.selectedNode == null && NodeEditor.curEditorState.selectedNodes.Count == 0)
            {
                return;
            }
            inputInfo.SetAsCurrentEnvironment();
            NodeEditorState state = inputInfo.editorState;

            if (inputInfo.editorState.selectedNode != null && !NodeEditor.curEditorState.selectedNodes.Contains(inputInfo.editorState.selectedNode))
            {
                NodeEditor.curEditorState.selectedNodes.Add(inputInfo.editorState.selectedNode);
            }

            {
//                if(m_CacheOff!=null)
//                    m_CacheOff();

                Dictionary <Node, Node> nodeMap     = new Dictionary <Node, Node>();
                List <Node>             newSelected = new List <Node>();
                foreach (var n in NodeEditor.curEditorState.selectedNodes)
                {
                    Node duplicated = Node.Create(n.GetID, n.rect.center, null, false);   //state.connectOutput);
//                    Debug.Log("dupe n "+n);
                    duplicated.CloneFieldsFrom(n);
                    state.selectedNode = state.focusedNode = duplicated;
                    newSelected.Add(duplicated);
                    nodeMap.Add(n, duplicated);
                }
                //handle connections
                foreach (var n in NodeEditor.curEditorState.selectedNodes)
                {
                    for (int index = 0; index < n.Inputs.Count; index++)
                    {
                        NodeInput nodeInput = n.Inputs[index];
                        if (nodeInput.connection != null)
                        {
                            Node connectTo = nodeInput.connection.body;
                            if (nodeMap.ContainsKey(nodeInput.connection.body))
                            {
                                connectTo = nodeMap[nodeInput.connection.body];
                            }

                            foreach (NodeOutput on in connectTo.Outputs)
                            {
                                if (on.name == nodeInput.connection.name)
                                {
                                    if (index < nodeMap[n].Inputs.Count)
                                    {
                                        nodeMap[n].Inputs[index].ApplyConnection(on);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    for (int index = 0; index < n.Outputs.Count; index++)
                    {
                        NodeOutput nodeOutput = n.Outputs[index];

                        for (int index1 = 0; index1 < nodeOutput.connections.Count; index1++)
                        {
                            NodeInput nodeInput = nodeOutput.connections[index1];
                            if (nodeInput == null)
                            {
                                continue;
                            }

                            Node connectTo = nodeInput.body;
                            if (nodeMap.ContainsKey(connectTo))
                            {
                                continue; //we did it allready
                            }
                            for (int i = 0; i < connectTo.Inputs.Count; i++)
                            {
                                NodeInput inputnode = connectTo.Inputs[i];
                                if (inputnode.name == nodeInput.name)
                                {
                                    inputnode.ApplyConnection(nodeOutput);
                                    break;
                                }
                            }
                        }
                    }
                }

                NodeEditor.curEditorState.selectedNodes.Clear();
                NodeEditor.curEditorState.selectedNodes = newSelected;
                NodeEditor.curEditorState.selectedNode  = newSelected[0];
//                foreach (var x in newSelected)
//                    Debug.Log(" selected node "+x);
//                if (m_CacheOn != null)
//                    m_CacheOn();
                NodeEditorCallbacks.IssueOnAddNode(NodeEditor.curEditorState.selectedNode); //just do it on one node, so it saves the canvas/repaints
            }

            state.connectOutput = null;

            if (m_FinishedDupe != null)
            {
                m_FinishedDupe();
            }
        }
Example #7
0
        /// <summary>
        /// Context Click selection. Here you'll need to register your own using a string identifier
        /// </summary>
        public static void ContextCallback(object obj)
        {
            callbackObject cbObj = obj as callbackObject;

            curNodeCanvas  = cbObj.canvas;
            curEditorState = cbObj.editor;

            switch (cbObj.message)
            {
            case "deleteNode":
                if (curEditorState.focusedNode != null)
                {
                    curEditorState.focusedNode.Delete();
                }
                break;

            case "duplicateNode":
                if (curEditorState.focusedNode != null)
                {
                    ContextCallback(new callbackObject(curEditorState.focusedNode.GetID, curNodeCanvas, curEditorState));
                    Node duplicatedNode = curNodeCanvas.nodes [curNodeCanvas.nodes.Count - 1];

                    curEditorState.focusedNode    = duplicatedNode;
                    curEditorState.dragNode       = true;
                    curEditorState.makeTransition = null;
                    curEditorState.connectOutput  = null;
                    curEditorState.panWindow      = false;
                }
                break;

            case "startTransition":
                if (curEditorState.focusedNode != null)
                {
                    curEditorState.makeTransition = curEditorState.focusedNode;
                    curEditorState.connectOutput  = null;
                }
                curEditorState.dragNode  = false;
                curEditorState.panWindow = false;

                break;

            default:
                Vector2 createPos = ScreenToGUIPos(mousePos);

                Node node = NodeTypes.getDefaultNode(cbObj.message);
                if (node == null)
                {
                    break;
                }

                bool acceptTransitions = NodeTypes.nodes [node].transitions;

                node = node.Create(createPos);
                node.InitBase();
                NodeEditorCallbacks.IssueOnAddNode(node);

                if (curEditorState.connectOutput != null)
                {                 // If nodeOutput is defined, link it to the first input of the same type
                    foreach (NodeInput input in node.Inputs)
                    {
                        if (Node.CanApplyConnection(curEditorState.connectOutput, input))
                        {                         // If it can connect (type is equals, it does not cause recursion, ...)
                            Node.ApplyConnection(curEditorState.connectOutput, input);
                            break;
                        }
                    }
                }
                else if (acceptTransitions && curEditorState.makeTransition != null)
                {
                    Node.CreateTransition(curEditorState.makeTransition, node);
                }

                curEditorState.makeTransition = null;
                curEditorState.connectOutput  = null;
                curEditorState.dragNode       = false;
                curEditorState.panWindow      = false;

                break;
            }

            if (NodeEditor.Repaint != null)
            {
                NodeEditor.Repaint();
            }
        }
Example #8
0
        /// <summary>
        /// Creates a copy of the specified node at pos on the specified canvas, optionally auto-connecting the specified output to a matching input
        /// silent disables any events
        /// </summary>
        public static Node CreateCopy(Node toCopy, Vector2 pos, NodeCanvas hostCanvas, ConnectionPort connectingPort = null, bool silent = false)
        {
            if (toCopy == null || hostCanvas == null)
            {
                throw new ArgumentException();
            }
            if (!NodeCanvasManager.CheckCanvasCompability(toCopy.GetID, hostCanvas.GetType()))
            {
                throw new UnityException("Cannot create Node with ID '" + toCopy.GetID + "' as it is not compatible with the current canvas type (" + hostCanvas.GetType().ToString() + ")!");
            }
            if (!hostCanvas.CanAddNode(toCopy.GetID))
            {
                throw new UnityException("Cannot create Node with ID '" + toCopy.GetID + "' on the current canvas of type (" + hostCanvas.GetType().ToString() + ")!");
            }
            Node node = ScriptableObject.Instantiate(toCopy);

            //Clone static connection ports
            foreach (ConnectionPortDeclaration portDecl in ConnectionPortManager.GetPortDeclarationEnumerator(node, true))
            {
                ConnectionPort port = (ConnectionPort)portDecl.portField.GetValue(node);
                port = portDecl.portInfo.CreateNew(node);
                portDecl.portField.SetValue(node, port);
            }
            //Clone dynamic connection ports
            for (int i = 0; i < node.dynamicConnectionPorts.Count; ++i)
            {
                node.dynamicConnectionPorts[i]      = ScriptableObject.Instantiate(node.dynamicConnectionPorts[i]);
                node.dynamicConnectionPorts[i].body = node;
                node.dynamicConnectionPorts[i].ClearConnections();
            }
            ConnectionPortManager.UpdateRepresentativePortLists(node);
            //Clone child SOs
            System.Func <ScriptableObject, ScriptableObject> copySOs = (ScriptableObject so) => ScriptableObject.Instantiate(so);;
            node.CopyScriptableObjects(copySOs);
            if (node == null)
            {
                return(null);
            }

            // Init node state
            node.name     = node.Title;
            node.autoSize = node.DefaultSize;
            node.position = pos;
            ConnectionPortManager.UpdateConnectionPorts(node);

            if (connectingPort != null)
            { // Handle auto-connection and link the output to the first compatible input
                for (int i = 0; i < node.connectionPorts.Count; i++)
                {
                    if (node.connectionPorts[i].TryApplyConnection(connectingPort, silent))
                    {
                        break;
                    }
                }
            }

            // Add node to host canvas
            hostCanvas.nodes.Add(node);
            if (!silent)
            { // Callbacks
                hostCanvas.OnNodeChange(connectingPort != null ? connectingPort.body : node);
                NodeEditorCallbacks.IssueOnAddNode(node);
                hostCanvas.Validate();
                NodeEditor.RepaintClients();
            }

            return(node);
        }
Example #9
0
        /// <summary>
        /// Creates a node of the specified ID at pos on the specified canvas, optionally auto-connecting the specified output to a matching input
        /// silent disables any events, init specifies whether OnCreate should be called
        /// </summary>
        public static Node Create(string nodeID, Vector2 pos, NodeCanvas hostCanvas, ConnectionPort connectingPort = null, bool silent = false, bool init = true)
        {
            if (string.IsNullOrEmpty(nodeID) || hostCanvas == null)
            {
                throw new ArgumentException();
            }
            if (!NodeCanvasManager.CheckCanvasCompability(nodeID, hostCanvas.GetType()))
            {
                throw new UnityException("Cannot create Node with ID '" + nodeID + "' as it is not compatible with the current canavs type (" + hostCanvas.GetType().ToString() + ")!");
            }
            if (!hostCanvas.CanAddNode(nodeID))
            {
                throw new UnityException("Cannot create Node with ID '" + nodeID + "' on the current canvas of type (" + hostCanvas.GetType().ToString() + ")!");
            }

            // Create node from data
            NodeTypeData data = NodeTypes.getNodeData(nodeID);
            Node         node = (Node)CreateInstance(data.type);

            if (node == null)
            {
                return(null);
            }

            // Init node state
            node.canvas   = hostCanvas;
            node.name     = node.Title;
            node.autoSize = node.DefaultSize;
            node.position = pos;
            ConnectionPortManager.UpdateConnectionPorts(node);
            if (init)
            {
                node.OnCreate();
            }

            if (connectingPort != null)
            {             // Handle auto-connection and link the output to the first compatible input
                for (int i = 0; i < node.connectionPorts.Count; i++)
                {
                    if (node.connectionPorts[i].TryApplyConnection(connectingPort, true))
                    {
                        break;
                    }
                }
            }

            // Add node to host canvas
            hostCanvas.nodes.Add(node);
            if (!silent)
            {             // Callbacks
                hostCanvas.OnNodeChange(connectingPort != null ? connectingPort.body : node);
                NodeEditorCallbacks.IssueOnAddNode(node);
                hostCanvas.Validate();
                NodeEditor.RepaintClients();
            }

#if UNITY_EDITOR
            if (!silent)
            {
                List <ConnectionPort> connectedPorts = new List <ConnectionPort>();
                foreach (ConnectionPort port in node.connectionPorts)
                {                 // 'Encode' connected ports in one list (double level cannot be serialized)
                    foreach (ConnectionPort conn in port.connections)
                    {
                        connectedPorts.Add(conn);
                    }
                    connectedPorts.Add(null);
                }
                Node createdNode = node;
                UndoPro.UndoProManager.RecordOperation(
                    () => NodeEditorUndoActions.ReinstateNode(createdNode, connectedPorts),
                    () => NodeEditorUndoActions.RemoveNode(createdNode),
                    "Create Node");
                // Make sure the new node is in the memory dump
                NodeEditorUndoActions.CompleteSOMemoryDump(hostCanvas);
            }
#endif

            return(node);
        }