Пример #1
0
        void DoGraphControls()
        {
            var e = Event.current;

            //variable is set as well, so that  nodes know if they can be clicked
            allowClick = !inspectorRect.Contains(e.mousePosition) && !blackboardRect.Contains(e.mousePosition);
            if (allowClick)
            {
                //canvas click to deselect all
                if (e.button == 0 && e.isMouse && e.type == EventType.MouseDown)
                {
                    currentSelection = null;
                    return;
                }

                //right click canvas to add node
                if (e.button == 1 && e.type == EventType.MouseDown)
                {
                    var pos = e.mousePosition + scrollOffset;
                    System.Action <System.Type> Selected = delegate(System.Type type){
                        var newNode = AddNode(type);
                        newNode.nodeRect.center = pos;
                        if (NCPrefs.autoConnect && focusedNode != null && (focusedNode.outConnections.Count < focusedNode.maxOutConnections || focusedNode.maxOutConnections == -1))
                        {
                            ConnectNode(focusedNode, newNode);
                        }
                        else
                        {
                            currentSelection = newNode;
                        }
                    };

                    EditorUtils.GetTypeSelectionMenu(baseNodeType, Selected).ShowAsContext();
                    e.Use();
                }
            }

            //Duplicate
            if (e.isKey && e.control && e.keyCode == KeyCode.D && focusedNode != null)
            {
                currentSelection = focusedNode.Duplicate();
                e.Use();
            }
        }
Пример #2
0
        //Draw the connections line from this node, to all of its children. This is the default hierarchical style. Override in each system's base node class.
        virtual public void DrawNodeConnections()
        {
            if (isHidden)
            {
                return;
            }

            var e = Event.current;

            //Receive connections first
            if (clickedPort != null && e.type == EventType.MouseUp)
            {
                if (nodeRect.Contains(e.mousePosition))
                {
                    if (graph.ConnectNode(clickedPort.parent, this, clickedPort.portIndex) != null)
                    {
                        clickedPort = null;
                        e.Use();
                    }
                }
                else
                {
                    if (ID == graph.allNodes.Count)
                    {
                        var source = clickedPort.parent;
                        var index  = clickedPort.portIndex;
                        var pos    = e.mousePosition;
                        clickedPort = null;

                        System.Action <System.Type> Selected = delegate(System.Type type){
                            var newNode = graph.AddNode(type);
                            newNode.nodeRect.center = pos;
                            graph.ConnectNode(source, newNode, index);
                            newNode.SortConnectionsByPositionX();
                        };

                        EditorUtils.GetTypeSelectionMenu(graph.baseNodeType, Selected).ShowAsContext();
                        e.Use();
                    }
                }
            }

            if (maxOutConnections == 0)
            {
                return;
            }

            var nodeOutputBox = new Rect(nodeRect.x, nodeRect.yMax - 4, nodeRect.width, 12);

            GUI.Box(nodeOutputBox, "", new GUIStyle("nodePortContainer"));

            if (outConnections.Count < maxOutConnections || maxOutConnections == -1)
            {
                for (int i = 0; i < outConnections.Count + 1; i++)
                {
                    var portRect = new Rect(0, 0, 10, 10);
                    portRect.center = new Vector2(((nodeRect.width / (outConnections.Count + 1)) * (i + 0.5f)) + nodeRect.xMin, nodeRect.yMax + 6);
                    GUI.Box(portRect, "", "nodePortEmpty");

                    if (childrenCollapsed)
                    {
                        continue;
                    }

                    EditorGUIUtility.AddCursorRect(portRect, MouseCursor.ArrowPlus);
                    if (e.button == 0 && e.type == EventType.MouseDown && portRect.Contains(e.mousePosition))
                    {
                        clickedPort = new Port(i, this, portRect.center);
                        e.Use();
                    }
                }
            }

            //draw the new connection line if in link mode
            if (clickedPort != null && clickedPort.parent == this)
            {
                Handles.DrawBezier(clickedPort.pos, e.mousePosition, clickedPort.pos, e.mousePosition, restingColor, null, 2);
            }

            //draw all connected lines
            for (int connectionIndex = 0; connectionIndex < outConnections.Count; connectionIndex++)
            {
                var connection = outConnections[connectionIndex];
                if (connection != null)
                {
                    var sourcePos = new Vector2(((nodeRect.width / (outConnections.Count + 1)) * (connectionIndex + 1)) + nodeRect.xMin, nodeRect.yMax + 6);
                    var targetPos = new Vector2(connection.targetNode.nodeRect.center.x, connection.targetNode.nodeRect.y);

                    var connectedPortRect = new Rect(0, 0, 12, 12);
                    connectedPortRect.center = sourcePos;
                    GUI.Box(connectedPortRect, "", "nodePortConnected");

                    if (childrenCollapsed || connection.targetNode.isHidden)
                    {
                        continue;
                    }

                    connection.DrawConnectionGUI(sourcePos, targetPos);

                    //On right click disconnect connection from the source.
                    if (e.button == 1 && e.type == EventType.MouseDown && connectedPortRect.Contains(e.mousePosition))
                    {
                        graph.RemoveConnection(connection);
                        e.Use();
                        return;
                    }
                }
            }
        }