void SnapOrMoveElement(KeyValuePair <GraphElement, OriginalPos> v, Rect selectedElementGeom) { GraphElement ce = v.Key; if (EditorPrefs.GetBool("GraphSnapping")) { Vector2 geomDiff = selectedElementGeom.position - m_OriginalPos[selectedElement].pos.position; Rect ceLayout = ce.GetPosition(); ce.SetPosition(new Rect(v.Value.pos.x + geomDiff.x, v.Value.pos.y + geomDiff.y, ceLayout.width, ceLayout.height)); } else { MoveElement(ce, v.Value.pos); } }
protected new void OnMouseDown(MouseDownEvent e) { if (m_Active) { e.StopImmediatePropagation(); return; } if (CanStartManipulation(e)) { m_GraphView = target as GraphView; if (m_GraphView == null) { return; } selectedElement = null; // avoid starting a manipulation on a non movable object clickedElement = e.target as GraphElement; if (clickedElement == null) { var ve = e.target as VisualElement; clickedElement = ve.GetFirstAncestorOfType <GraphElement>(); if (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 (!clickedElement.IsMovable() || !clickedElement.HitTest(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_GraphView.selection.Contains(clickedElement)) { e.StopImmediatePropagation(); return; } selectedElement = clickedElement; m_OriginalPos = new Dictionary <GraphElement, OriginalPos>(); foreach (ISelectable s in m_GraphView.selection) { GraphElement ce = s as GraphElement; if (ce == null || !ce.IsMovable()) { continue; } StackNode stackNode = null; if (ce.parent is StackNode) { stackNode = ce.parent as StackNode; if (stackNode.IsSelected(m_GraphView)) { continue; } } Rect geometry = ce.GetPosition(); Rect geometryInContentViewSpace = ce.hierarchy.parent.ChangeCoordinatesTo(m_GraphView.contentViewContainer, geometry); m_OriginalPos[ce] = new OriginalPos { pos = geometryInContentViewSpace, scope = ce.GetContainingScope(), stack = stackNode, stackIndex = stackNode != null?stackNode.IndexOf(ce) : -1 }; } m_originalMouse = e.mousePosition; m_ItemPanDiff = Vector3.zero; if (m_PanSchedule == null) { m_PanSchedule = m_GraphView.schedule.Execute(Pan).Every(k_PanInterval).StartingIn(k_PanInterval); m_PanSchedule.Pause(); } m_Active = true; target.CaptureMouse(); // We want to receive events even when mouse is not over ourself. e.StopImmediatePropagation(); } }