Esempio n. 1
0
        private void HandleMouseLeftClicks()
        {
            Event current = Event.current;

            if (!current.isMouse)
            {
                return;
            }
            if (current.button != 0)
            {
                return;
            }

            if (current.type == EventType.MouseDown)
            {
                //left mouse button is down and the space key is down as well -> enter panning mode
                if (m_spaceKeyDown)
                {
                    m_mode = GraphMode.Pan;
                    current.Use();
                    return;
                }

                if (m_altKeyPressed) //delete mode
                {
                    BaseNodeGUI nodeGUI = GetNodeGUIOfDeleteButtonAtWorldPosition(CurrentMousePosition);
                    if (nodeGUI != null)
                    {
                        SoftDeleteNode(nodeGUI.Node, true, false);
                        current.Use();
                        return;
                    }
                }

                //pressed left mouse button over a socket point -> but we have at least two nodes selected -> do not allow starting any connections
                if (WindowSettings.SelectedNodes.Count < 2)
                {
                    if (m_currentHoveredVirtualPoint != null)
                    {
//                        if (current.alt)
                        if (m_altKeyPressed)
                        {
                            //pressed left mouse button over a socket virtual point while holding down Alt -> Disconnect Virtual Point
                            DisconnectVirtualPoint(m_currentHoveredVirtualPoint, true);
                        }
                        else
                        {
                            //pressed left mouse button over a socket connection point -> it's a possible start of a connection
                            m_activeSocket = m_currentHoveredVirtualPoint.Socket; //set the socket as the active socket
                            m_mode         = GraphMode.Connect;                   //set the graph in connection mode
                        }

                        current.Use();
                        return;
                    }

                    Socket socket = GetSocketAtWorldPositionFromHoverRect(CurrentMousePosition);
                    if (socket != null)
                    {
//                        if (current.alt)
                        if (m_altKeyPressed)
                        {
                            //pressed left mouse button over a socket while holding down Alt -> Remove Socket
                            RemoveSocket(socket, true);
                        }
                        else
                        {
                            //pressed left mouse button over a socket -> it's a possible start of a connection
                            m_activeSocket = socket;            //set the socket as the active socket
                            m_mode         = GraphMode.Connect; //set the graph in connection mode
                        }

                        current.Use();
                        return;
                    }
                }

                //pressed left mouse button over a node -> check to see if it's inside the header (if no node is currently selected) or it just over a node (if at least 2 nodes are selected)
                if (m_currentHoveredNode != null)
                {
                    if (GetNodeGridRect(m_currentHoveredNode).Contains(CurrentMousePosition) || //if mouse is inside node -> allow dragging
                        WindowSettings.SelectedNodes.Count > 1)                                 //OR if there are at least 2 nodes selected -> allow dragging from any point on the node
                    {
                        //pressed left mouse button over a node -> select/deselect it
                        if (current.shift || current.control || current.command)               //if using modifiers -> create custom selection
                        {
                            SelectNodes(new List <Node> {
                                m_currentHoveredNode
                            }, true, true);                                                    //add/remove the node to/from selection
                        }
                        else if (!WindowSettings.SelectedNodes.Contains(m_currentHoveredNode)) //we may have a selection and we do not want to override it in order to be able to start dragging
                        {
                            SelectNodes(new List <Node> {
                                m_currentHoveredNode
                            }, false, true);                                                   //select this node only
                        }
                        //allow dragging ONLY IF the mouse is over a selected node
                        //in the previous lines we only checked if it's over a node, but not if the node we are hovering over is currently selected
                        if (WindowSettings.SelectedNodes.Contains(m_currentHoveredNode))
                        {
                            if (Selection.activeObject != m_currentHoveredNode)
                            {
                                Selection.activeObject = m_currentHoveredNode; //select the node
                            }
                            //pressed left mouse button over a node -> it's a possible start drag
                            PrepareToDragSelectedNodes(CurrentMousePosition);
                            m_mode = GraphMode.Drag;
                        }
                    }

                    current.Use();
                    return;
                }

                //pressed left mouse button over nothing -> it's a possible start selection
                PrepareToCreateSelectionBox(CurrentMousePosition);
                current.Use();
                return;
            }

            if (current.type == EventType.MouseDrag)
            {
                //left mouse click is dragging and the graph is in panning mode
                if (m_mode == GraphMode.Pan)
                {
                    //check that the space key is held down -> otherwise exit pan mode
                    if (!m_spaceKeyDown)
                    {
                        m_mode = GraphMode.None;
                    }
                    else
                    {
                        DoPanning(current);
                    }

                    current.Use();
                    return;
                }

                //mouse left click is dragging a connection
                if (m_mode == GraphMode.Connect)
                {
                    if (m_currentHoveredSocket != null) //mouse is over a socket -> color the line to green if connection is possible or red otherwise
                    {
                        m_createConnectionLineColor = m_activeSocket.CanConnect(m_currentHoveredSocket) ? Color.green : Color.red;
                    }
                    else if (m_currentHoveredVirtualPoint != null) //mouse is over a socket connection point -> color the line to green if connection is possible or red otherwise
                    {
                        m_createConnectionLineColor = m_activeSocket.CanConnect(m_currentHoveredVirtualPoint.Socket) ? Color.green : Color.red;
                    }
                    else //mouse is not over anything connectable -> show the connection point color to look for
                    {
                        m_createConnectionLineColor =
                            m_activeSocket.IsInput
                                ? DGUI.Colors.GetDColor(DGUI.Colors.NodyOutputColorName).Normal //source socket is input -> looking for an output socket -> color the line to the output color
                                : DGUI.Colors.GetDColor(DGUI.Colors.NodyInputColorName).Normal; //source socket is output -> looking for an input socket -> color the line to the input color
                    }
                    current.Use();
                    return;
                }

                //mouse left click is dragging one or more nodes
                if (m_mode == GraphMode.Drag) // && GUIUtility.hotControl == dragNodesControlId)
                {
                    m_isDraggingAnimBool.target = false;
                    RecordUndo("Move Nodes");
                    UpdateSelectedNodesWhileDragging();
                    current.Use();
                    return;
                }

                //mouse left click is dragging and creating a selection box <- we know this because the the mouse is not over a point nor a node
                if (m_startSelectPoint != null)
                {
                    m_mode = GraphMode.Select;
                }
                if (m_mode == GraphMode.Select)
                {
                    UpdateSelectionBox(CurrentMousePosition);
                    UpdateSelectBoxSelectedNodesWhileSelecting(current);
                    UpdateNodesSelectedState(m_selectedNodesWhileSelecting);
                    current.Use();
                    return;
                }
            }

            if (current.type == EventType.MouseUp)
            {
                if (RegisteredDoubleClick)
                {
                    if (m_previousHoveredNode != null)
                    {
                        NodesGUIsDatabase[m_previousHoveredNode.Id].OnDoubleClick(this);
                    }
                }

                //lifted left mouse button and was panning (space key was/is down) -> reset graph to idle
                if (m_mode == GraphMode.Pan)
                {
                    m_mode = GraphMode.None;
                    current.Use();
                    return;
                }

                //lifted left mouse button and was dragging -> reset graph to idle
                if (m_mode == GraphMode.Drag)
                {
                    m_initialDragNodePositions.Clear();
                    m_isDraggingAnimBool.target = true;
                    m_mode = GraphMode.None;
                    current.Use();
                    return;
                }

                //lifted left mouse button and was selecting via selection box -> end selections and reset graph to idle mode
                if (m_mode == GraphMode.Select)
                {
                    EndDragSelectedNodes();
                    m_mode = GraphMode.None;
                    current.Use();
                    return;
                }


                //check if this happened over another socket or connection point
                if (m_currentHoveredSocket != null)
                {
                    //lifted left mouse button over a socket
                    if (m_activeSocket != null &&                                   //if there is an active socket
                        m_activeSocket != m_currentHoveredSocket &&                 //and it's not this one
                        m_activeSocket.CanConnect(m_currentHoveredSocket))          //and the two sockets can get connected
                    {
                        ConnectSockets(m_activeSocket, m_currentHoveredSocket);     //connect the two sockets
                    }
                    else                                                            //this was a failed connection attempt
                    {
                        GraphEvent.Send(GraphEvent.EventType.EVENT_CONNECTING_END); //send a graph event
                    }
                    m_activeSocket = null;                                          //clear the active socket
                    m_mode         = GraphMode.None;                                //set the graph in idle mode
                    current.Use();
                    return;
                }

                if (m_currentHoveredVirtualPoint != null)
                {
                    //lifted left mouse button over a socket connection point
                    if (m_activeSocket != null &&                                            //if there is an active socket
                        m_activeSocket != m_currentHoveredVirtualPoint.Socket &&             //and it's not this one
                        m_activeSocket.CanConnect(m_currentHoveredVirtualPoint.Socket))      //and the two sockets can get connected
                    {
                        ConnectSockets(m_activeSocket, m_currentHoveredVirtualPoint.Socket); //connect the two sockets
                    }
                    else                                                                     //this was a failed connection attempt
                    {
                        GraphEvent.Send(GraphEvent.EventType.EVENT_CONNECTING_END);          //send a graph event
                    }
                    m_activeSocket = null;                                                   //clear the active socket
                    m_mode         = GraphMode.None;                                         //set the graph in idle mode
                    current.Use();
                    return;
                }

                //it a connecting process was under way -> clear it
                //lifted left mouse button, but no virtual point was under the mouse position
                if (m_mode == GraphMode.Connect)
                {
                    m_activeSocket = null;           //clear the active socket
                    m_mode         = GraphMode.None; //set the graph in idle mode
                }

                Node node = GetNodeAtWorldPosition(CurrentMousePosition);
                if (node != null)
                {
                    m_mode = GraphMode.None; //set the graph in idle mode
                    current.Use();
                    return;
                }

                //lifted mouse left button over nothing -> deselect all and select the graph itself
                ExecuteGraphAction(GraphAction.DeselectAll); //deselect all nodes and select the graph itself
                m_mode = GraphMode.None;                     //set the graph in idle mode
                current.Use();
                return;
            }

            //check if the developer released the left mouse button outside of the graph window
            if (current.rawType == EventType.MouseUp || current.rawType == EventType.MouseLeaveWindow)
            {
                switch (m_mode)
                {
                case GraphMode.Select:
                    EndDragSelectedNodes();
                    m_mode = GraphMode.None;
                    current.Use();
                    break;
                }
            }
        }
Esempio n. 2
0
 public static bool CanConnect(string host, int port, TimeSpan timeout, ProtocolType protocolType = ProtocolType.Tcp)
 {
     using (var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, protocolType)) {
         return(sock.CanConnect(host, port, timeout));
     }
 }