Пример #1
0
        protected virtual void OnMouseDown(MouseDownEvent e)
        {
            if (m_Active)
            {
                e.StopImmediatePropagation();
                return;
            }

            if (!CanStartManipulation(e))
            {
                return;
            }

            var port = target.GetFirstAncestorOfType <Port>();

            if (port == null)
            {
                return;
            }

            m_MouseDownPosition = e.localMousePosition;

            m_EdgeDragHelper.CreateEdgeCandidate(port.PortModel.GraphModel);
            m_EdgeDragHelper.draggedPort = port.PortModel;

            if (m_EdgeDragHelper.HandleMouseDown(e))
            {
                m_Active = true;
                target.CaptureMouse();
                e.StopPropagation();
            }
            else
            {
                m_EdgeDragHelper.Reset();
            }
        }
Пример #2
0
        protected void OnMouseMove(MouseMoveEvent evt)
        {
            // If the left mouse button is not down then return
            if (m_Edge == null)
            {
                return;
            }

            evt.StopPropagation();

            bool alreadyDetached = (m_DetachedPort != null);

            // If one end of the edge is not already detached then
            if (!alreadyDetached)
            {
                float delta = (evt.mousePosition - m_PressPos).sqrMagnitude;

                if (delta < (s_StartDragDistance * s_StartDragDistance))
                {
                    return;
                }

                var view         = m_Edge.View;
                var outputPortUI = m_Edge.Output.GetUI <Port>(view);
                var inputPortUI  = m_Edge.Input.GetUI <Port>(view);

                if (outputPortUI == null || inputPortUI == null)
                {
                    return;
                }

                // Determine which end is the nearest to the mouse position then detach it.
                Vector2 outputPos = new Vector2(outputPortUI.GetGlobalCenter().x, outputPortUI.GetGlobalCenter().y);
                Vector2 inputPos  = new Vector2(inputPortUI.GetGlobalCenter().x, inputPortUI.GetGlobalCenter().y);

                float distanceFromOutput = (m_PressPos - outputPos).sqrMagnitude;
                float distanceFromInput  = (m_PressPos - inputPos).sqrMagnitude;

                if (distanceFromInput > 50 * 50 && distanceFromOutput > 50 * 50)
                {
                    return;
                }

                m_DetachedFromInputPort = distanceFromInput < distanceFromOutput;

                IPortModel connectedPort;
                Port       connectedPortUI;


                if (m_DetachedFromInputPort)
                {
                    connectedPort   = m_Edge.Output;
                    connectedPortUI = outputPortUI;

                    m_DetachedPort = m_Edge.Input;
                }
                else
                {
                    connectedPort   = m_Edge.Input;
                    connectedPortUI = inputPortUI;

                    m_DetachedPort = m_Edge.Output;
                }

                // Use the edge drag helper of the still connected port
                m_ConnectedEdgeDragHelper = connectedPortUI.EdgeConnector.edgeDragHelper;
                m_ConnectedEdgeDragHelper.originalEdge = m_Edge;
                m_ConnectedEdgeDragHelper.draggedPort  = connectedPort;
                m_ConnectedEdgeDragHelper.CreateEdgeCandidate(connectedPort.GraphModel);
                m_ConnectedEdgeDragHelper.edgeCandidateModel.EndPoint = evt.mousePosition;

                // Redirect the last mouse down event to active the drag helper

                if (m_ConnectedEdgeDragHelper.HandleMouseDown(m_LastMouseDownEvent))
                {
                    m_Active = true;

                    if (m_DetachedPort.GetConnectedEdges().Count() > 1)
                    {
                        m_AdditionalEdgeDragHelpers = new List <EdgeDragHelper>();

                        foreach (var edge in m_DetachedPort.GetConnectedEdges())
                        {
                            var edgeUI = edge.GetUI <Edge>(view);
                            if (edgeUI != null && edgeUI != m_Edge && edgeUI.IsSelected())
                            {
                                var otherPort = m_DetachedPort == edge.ToPort ? edge.FromPort : edge.ToPort;

                                var edgeDragHelper = otherPort.GetUI <Port>(view)?.EdgeConnector.edgeDragHelper;

                                if (edgeDragHelper != null)
                                {
                                    edgeDragHelper.originalEdge = edgeUI;
                                    edgeDragHelper.draggedPort  = otherPort;
                                    edgeDragHelper.CreateEdgeCandidate(connectedPort.GraphModel);
                                    edgeDragHelper.edgeCandidateModel.EndPoint = evt.mousePosition;

                                    m_AdditionalEdgeDragHelpers.Add(edgeDragHelper);
                                }
                            }
                        }
                        foreach (var edgeDrag in m_AdditionalEdgeDragHelpers)
                        {
                            edgeDrag.HandleMouseDown(m_LastMouseDownEvent);
                        }
                    }
                }
                else
                {
                    Reset();
                }

                m_LastMouseDownEvent = null;
            }

            if (m_Active)
            {
                m_ConnectedEdgeDragHelper.HandleMouseMove(evt);
                if (m_AdditionalEdgeDragHelpers != null)
                {
                    foreach (var dragHelper in m_AdditionalEdgeDragHelpers)
                    {
                        dragHelper.HandleMouseMove(evt);
                    }
                }
            }
        }