Esempio n. 1
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();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Processes input events
        /// </summary>
        public static void InputEvents(List <Rect> ignoreInput)
        {
            Event e = Event.current;

            mousePos = e.mousePosition;

            if (OverlayGUI.HasPopupControl())
            {
                return;
            }

            bool insideCanvas = curEditorState.canvasRect.Contains(e.mousePosition);

            for (int ignoreCnt = 0; ignoreCnt < ignoreInput.Count; ignoreCnt++)
            {
                if (ignoreInput [ignoreCnt].Contains(e.mousePosition))
                {
                    insideCanvas = false;
                    break;
                }
            }

            if (!insideCanvas)
            {
                return;
            }

            curEditorState.focusedNode = null;
            if (insideCanvas && (e.type == EventType.MouseDown || e.type == EventType.MouseUp))
            {
                curEditorState.focusedNode = NodeEditor.NodeAtPosition(e.mousePosition);
                if (e.button == 0)
                {
                    curEditorState.activeNode = curEditorState.focusedNode;
                    if (Repaint != null)
                    {
                        Repaint();
                    }
                }
            }

        #if UNITY_EDITOR
            if (curEditorState.focusedNode != null)
            {
                UnityEditor.Selection.activeObject = curEditorState.focusedNode;
            }
        #endif

            switch (e.type)
            {
            case EventType.MouseDown:

                curEditorState.dragNode  = false;
                curEditorState.panWindow = false;

                if (curEditorState.focusedNode != null)
                {                 // A click on a node
                    if (e.button == 1)
                    {             // Right click -> Node Context Click
                        // TODO: Node Editor: Editor-Independancy - GenericMenu conversion
//	#if UNITY_EDITOR
//						UnityEditor.GenericMenu menu = new UnityEditor.GenericMenu ();
//	#else
                        GenericMenu menu = new GenericMenu();
//	#endif
                        menu.AddItem(new GUIContent("Delete Node"), false, ContextCallback, new callbackObject("deleteNode", curNodeCanvas, curEditorState));
                        menu.AddItem(new GUIContent("Duplicate Node"), false, ContextCallback, new callbackObject("duplicateNode", curNodeCanvas, curEditorState));
                        if (NodeTypes.getNodeData(curEditorState.focusedNode).transitions)
                        {
                            menu.AddSeparator("Seperator");
                            menu.AddItem(new GUIContent("Make Transition"), false, ContextCallback, new callbackObject("startTransition", curNodeCanvas, curEditorState));
                        }

                        menu.ShowAsContext();

                        e.Use();
                    }
                    else if (e.button == 0)
                    {
                        if (!GUIToScreenRect(curEditorState.focusedNode.rect).Contains(e.mousePosition))
                        {                         // Left click at node edges -> Check for clicked connections to edit
                            NodeOutput nodeOutput = curEditorState.focusedNode.GetOutputAtPos(e.mousePosition);
                            if (nodeOutput != null)
                            {                             // Output Node -> New Connection drawn from this
                                curEditorState.connectOutput = nodeOutput;
                                e.Use();
                            }
                            else
                            {                             // no output clicked, check input
                                NodeInput nodeInput = curEditorState.focusedNode.GetInputAtPos(e.mousePosition);
                                if (nodeInput != null && nodeInput.connection != null)
                                {                                 // Input node -> Loose and edit Connection
                                    curEditorState.connectOutput = nodeInput.connection;
                                    Node.RemoveConnection(nodeInput);
                                    e.Use();
                                }
                            }
                        }
                    }
                }
                else
                {                 // A click on the empty canvas
                    if (e.button == 2 || e.button == 0)
                    {             // Left/Middle Click -> Start scrolling
                        curEditorState.panWindow = true;
                        e.delta = Vector2.zero;
                    }
                    else if (e.button == 1)
                    {                     // Right click -> Editor Context Click
                        // TODO: Node Editor: Editor-Independancy - GenericMenu conversion
                        if (curEditorState.connectOutput != null || curEditorState.makeTransition != null)
                        {
//							#if UNITY_EDITOR
//							UnityEditor.GenericMenu menu = new UnityEditor.GenericMenu ();
//							#else
                            GenericMenu menu = new GenericMenu();
//							#endif

                            // Iterate through all compatible nodes
                            foreach (Node node in NodeTypes.nodes.Keys)
                            {
                                if (curEditorState.connectOutput != null)
                                {
                                    foreach (var input in node.Inputs)
                                    {
                                        if (input.type == curEditorState.connectOutput.type)
                                        {
                                            menu.AddItem(new GUIContent("Add " + NodeTypes.nodes[node].adress), false, ContextCallback, new callbackObject(node.GetID, curNodeCanvas, curEditorState));
                                            break;
                                        }
                                    }
                                }
                                else if (curEditorState.makeTransition != null && NodeTypes.nodes [node].transitions)
                                {
                                    menu.AddItem(new GUIContent("Add " + NodeTypes.nodes[node].adress), false, ContextCallback, new callbackObject(node.GetID, curNodeCanvas, curEditorState));
                                }
                            }

                            menu.ShowAsContext();
                        }
                        else
                        {
//							#if UNITY_EDITOR
//							UnityEditor.GenericMenu menu = new UnityEditor.GenericMenu ();
//							#else
                            GenericMenu menu = new GenericMenu();
//							#endif

                            foreach (Node node in NodeTypes.nodes.Keys)
                            {
                                menu.AddItem(new GUIContent("Add " + NodeTypes.nodes [node].adress), false, ContextCallback, new callbackObject(node.GetID, curNodeCanvas, curEditorState));
                            }

                            menu.ShowAsContext();
                        }
                        e.Use();
                    }
                }

                break;

            case EventType.MouseUp:

                if (curEditorState.focusedNode != null)
                {
                    if (curEditorState.makeTransition != null)
                    {
                        Node.CreateTransition(curEditorState.makeTransition, curEditorState.focusedNode);
                    }
                    else if (curEditorState.connectOutput != null)
                    {                     // Apply a connection if theres a clicked input
                        if (!curEditorState.focusedNode.Outputs.Contains(curEditorState.connectOutput))
                        {                 // If an input was clicked, it'll will now be connected
                            NodeInput clickedInput = curEditorState.focusedNode.GetInputAtPos(e.mousePosition);
                            if (Node.CanApplyConnection(curEditorState.connectOutput, clickedInput))
                            {                             // If it can connect (type is equals, it does not cause recursion, ...)
                                Node.ApplyConnection(curEditorState.connectOutput, clickedInput);
                            }
                        }
                        e.Use();
                    }
                }

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

                break;

            case EventType.ScrollWheel:

                curEditorState.zoom = (float)Math.Round(Math.Min(2.0f, Math.Max(0.6f, curEditorState.zoom + e.delta.y / 15)), 2);
                if (Repaint != null)
                {
                    Repaint();
                }

                break;

            case EventType.KeyDown:

                // TODO: Node Editor: Shortcuts
                if (e.keyCode == KeyCode.N)                 // Start Navigating (curve to origin / active Node)
                {
                    curEditorState.navigate = true;
                }

                if (e.keyCode == KeyCode.LeftControl && curEditorState.activeNode != null)                 // Snap
                {
                    curEditorState.activeNode.rect.position = new Vector2(Mathf.RoundToInt((curEditorState.activeNode.rect.position.x - curEditorState.panOffset.x) / 10) * 10 + curEditorState.panOffset.x,
                                                                          Mathf.RoundToInt((curEditorState.activeNode.rect.position.y - curEditorState.panOffset.y) / 10) * 10 + curEditorState.panOffset.y);
                }
                if (Repaint != null)
                {
                    Repaint();
                }

                break;

            case EventType.KeyUp:

                if (e.keyCode == KeyCode.N)                 // Stop Navigating
                {
                    curEditorState.navigate = false;
                }

                if (Repaint != null)
                {
                    Repaint();
                }

                break;

            case EventType.MouseDrag:

                if (curEditorState.panWindow)
                {                 // Scroll everything with the current mouse delta
                    curEditorState.panOffset += e.delta * curEditorState.zoom;
                    for (int nodeCnt = 0; nodeCnt < curNodeCanvas.nodes.Count; nodeCnt++)
                    {
                        curNodeCanvas.nodes [nodeCnt].rect.position += e.delta * curEditorState.zoom;
                    }
                    e.delta = Vector2.zero;
                    if (Repaint != null)
                    {
                        Repaint();
                    }
                }
                else
                {
                    curEditorState.panWindow = false;
                }

                if (curEditorState.dragNode && curEditorState.activeNode != null && GUIUtility.hotControl == 0)
                {                 // Drag the active node with the current mouse delta
                    curEditorState.activeNode.rect.position += e.delta * curEditorState.zoom;
                    NodeEditorCallbacks.IssueOnMoveNode(curEditorState.activeNode);
                    e.delta = Vector2.zero;
                    if (Repaint != null)
                    {
                        Repaint();
                    }
                }
                else
                {
                    curEditorState.dragNode = false;
                }

                break;
            }
        }