/// <summary>
        /// Callback for the MouseUp event.
        /// </summary>
        /// <param name="e">The event.</param>
        protected void OnMouseUp(MouseUpEvent e)
        {
            if (!m_Active || m_SelectionContainer == null)
            {
                if (m_SelectedElement != null && m_SelectionContainer != null && !m_Dragging)
                {
                    if (m_SelectedElement.IsSelected() && !e.actionKey && e.button == (int)m_ActivateButton)
                    {
                        m_SelectedElement.CommandDispatcher.Dispatch(new SelectElementsCommand(SelectElementsCommand.SelectionMode.Replace, m_SelectedElement.Model));
                    }
                }

                Reset();
                return;
            }

            if (e.button == (int)m_ActivateButton)
            {
                // Since we didn't drag after all, update selection with current element only
                if (!e.actionKey)
                {
                    m_SelectedElement.CommandDispatcher.Dispatch(new SelectElementsCommand(SelectElementsCommand.SelectionMode.Replace, m_SelectedElement.Model));
                }
                else if (m_AddedByMouseDown && !m_Dragging && m_SelectedElement.IsSelected())
                {
                    m_SelectedElement.CommandDispatcher.Dispatch(new SelectElementsCommand(SelectElementsCommand.SelectionMode.Remove, m_SelectedElement.Model));
                }

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

            m_Active           = false;
            m_Dragging         = false;
            m_AddedByMouseDown = false;

            if (target == null)
            {
                return;
            }

            m_SelectionContainer = target.GetFirstAncestorOfType <IDragSource>();

            if (m_SelectionContainer == null)
            {
                // Keep for potential later use in OnMouseUp (where e.target might be different then)
                m_SelectionContainer = target.GetFirstOfType <IDragSource>();
                m_SelectedElement    = e.target as GraphElement;
                return;
            }

            m_SelectedElement = target.GetFirstOfType <GraphElement>();

            if (m_SelectedElement == null)
            {
                return;
            }

            // Since we didn't drag after all, update selection with current element only
            if (!m_SelectedElement.IsSelected())
            {
                var selectionMode = e.actionKey ? SelectElementsCommand.SelectionMode.Add : SelectElementsCommand.SelectionMode.Replace;
                m_SelectedElement.CommandDispatcher.Dispatch(new SelectElementsCommand(selectionMode, m_SelectedElement.Model));
                m_AddedByMouseDown = true;
            }

            if (e.button == (int)m_ActivateButton)
            {
                // avoid starting a manipulation on a non movable object

                if (!m_SelectedElement.Model.IsDroppable())
                {
                    return;
                }

                m_MouseDownPosition = e.localMousePosition;
                m_Active            = true;
                target.CaptureMouse();
                e.StopPropagation();
            }
        }
Exemplo n.º 3
0
        bool IsIgnoredElement(GraphElement selectedElement, GraphElement element, Rect rectToFit)
        {
            if (selectedElement is Placemat placemat && element.layout.Overlaps(placemat.layout) || element is Edge || !element.visible ||
                element.IsSelected() || element.layout.Overlaps(selectedElement.layout))
            {
                return(true);
            }

            Rect localSelRect = m_GraphView.ChangeCoordinatesTo(element, rectToFit);

            return(!element.Overlaps(localSelRect));
        }
Exemplo n.º 4
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();
            }
        }