Exemplo n.º 1
0
        protected void OnMouseUp(MouseUpEvent e)
        {
            GraphElement ce = e.target as GraphElement;

            if (ce != null && !ce.IsMovable())
            {
                return;
            }

            if (m_Active)
            {
                if (CanStopManipulation(e))
                {
                    m_Active = false;
                    target.ReleaseMouse();
                    e.StopPropagation();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Callback for the MouseDown event.
        /// </summary>
        /// <param name="e">The event.</param>
        protected void OnMouseDown(MouseDownEvent e)
        {
            if (m_Active)
            {
                e.StopImmediatePropagation();
                return;
            }

            if (CanStartManipulation(e))
            {
                if (m_GraphView == null)
                {
                    return;
                }

                m_SelectedElement = null;

                // avoid starting a manipulation on a non movable object
                m_ClickedElement = e.target as GraphElement;
                if (m_ClickedElement == null)
                {
                    var ve = e.target as VisualElement;
                    m_ClickedElement = ve?.GetFirstAncestorOfType <GraphElement>();
                    if (m_ClickedElement == null)
                    {
                        return;
                    }
                }

                // Only start manipulating if the clicked element is movable, selected and that the mouse is in its clickable region (it must be deselected otherwise).
                if (!m_ClickedElement.IsMovable() || !m_ClickedElement.ContainsPoint(m_ClickedElement.WorldToLocal(e.mousePosition)))
                {
                    return;
                }

                // If we hit this, this likely because the element has just been unselected
                // It is important for this manipulator to receive the event so the previous one did not stop it
                // but we shouldn't let it propagate to other manipulators to avoid a re-selection
                if (!m_ClickedElement.IsSelected())
                {
                    e.StopImmediatePropagation();
                    return;
                }

                m_SelectedElement = m_ClickedElement;

                m_OriginalPos = new Dictionary <GraphElement, OriginalPos>();

                var selection = m_GraphView.GetSelection();

                var elementsToMove = new HashSet <GraphElement>(selection.Select(model => model.GetUI(m_GraphView)).OfType <GraphElement>());

                var selectedPlacemats = new HashSet <Placemat>(elementsToMove.OfType <Placemat>());
                foreach (var placemat in selectedPlacemats)
                {
                    placemat.GetElementsToMove(e.shiftKey, elementsToMove);
                }

                m_EdgesToUpdate.Clear();
                HashSet <INodeModel> nodeModelsToMove = new HashSet <INodeModel>(elementsToMove.Select(element => element.Model).OfType <INodeModel>());
                foreach (var edge in m_GraphView.Edges.ToList())
                {
                    if (nodeModelsToMove.Contains(edge.Input?.NodeModel) && nodeModelsToMove.Contains(edge.Output?.NodeModel))
                    {
                        m_EdgesToUpdate.Add(edge);
                    }
                }

                foreach (GraphElement ce in elementsToMove)
                {
                    if (ce == null || !ce.IsMovable())
                    {
                        continue;
                    }

                    Rect geometry = ce.GetPosition();
                    Rect geometryInContentViewSpace = ce.hierarchy.parent.ChangeCoordinatesTo(m_GraphView.ContentViewContainer, geometry);
                    m_OriginalPos[ce] = new OriginalPos
                    {
                        pos = geometryInContentViewSpace,
                    };
                }

                m_OriginalMouse = e.mousePosition;
                m_ItemPanDiff   = Vector3.zero;

                if (m_PanSchedule == null)
                {
                    m_PanSchedule = m_GraphView.schedule.Execute(Pan).Every(panInterval).StartingIn(panInterval);
                    m_PanSchedule.Pause();
                }

                m_Snapper.BeginSnap(m_SelectedElement);

                m_Active = true;
                target.CaptureMouse(); // We want to receive events even when mouse is not over ourself.
                e.StopImmediatePropagation();
            }
        }