public void StackNodeContainsAddedElements()
        {
            // Create a stack
            StackNode stack = CreateFilteredStackNode(k_DefaultX, k_DefaultY);
            Node      footer;

            // Verify that nodes are properly added to the stack
            stack.AddElement(m_Node1);
            stack.AddElement(m_Node2);
            stack.InsertElement(0, m_Node3);
            stack.InsertElement(0, footer = new FooterNode());

            Assert.AreEqual(4, stack.childCount);
            Assert.AreEqual(stack, m_Node1.parent);
            Assert.AreEqual(stack, m_Node2.parent);
            Assert.AreEqual(stack, m_Node3.parent);
            Assert.AreEqual(stack, footer.parent);
            Assert.AreEqual(0, stack.IndexOf(m_Node3));
            // Verify that the footer is added at the end of the stack even though we tried to insert it at the begining
            Assert.AreEqual(stack.childCount - 1, stack.IndexOf(footer));
        }
        public IEnumerator DragAddSelectionToStackNode()
        {
            // Create a stack
            StackNode stack = CreateStackNode(k_DefaultX, k_DefaultY);

            // Allow two frames to compute the layout of the stack and nodes
            yield return(null);

            yield return(null);

            // Select node1
            m_Node1.Select(graphView, false);

            Vector2 start         = m_Node1.worldBound.center;
            Vector2 centerOfStack = stack.LocalToWorld(stack.GetRect().center); // Center of the stack

            // Drag node1 onto the stack
            helpers.DragTo(start, centerOfStack);

            // Verify that node1 has been added to stack
            Assert.AreEqual(1, stack.childCount);
            Assert.AreEqual(stack, m_Node1.parent);

            // Allow one frame to compute the layout of the stack
            yield return(null);

            // Select node2
            m_Node2.Select(graphView, false);

            start = m_Node2.worldBound.center;
            Vector2 centerOfNode1 = m_Node1.worldBound.center; // Center of Node1

            // Drag node2 onto the stack at the position of node1
            helpers.DragTo(start, centerOfNode1);

            // Verify that node2 has been added to the stack at the previous index of node1
            Assert.AreEqual(2, stack.childCount);
            Assert.AreEqual(stack, m_Node2.parent);
            Assert.AreEqual(0, stack.IndexOf(m_Node2));
        }
        public IEnumerator DragReorderSelectionInStackNode()
        {
            // Creates a stack and add node1, node2, node3
            StackNode stack = CreateStackNode(k_DefaultX, k_DefaultY);

            stack.AddElement(m_Node1);
            stack.AddElement(m_Node2);
            stack.AddElement(m_Node3);

            // Allow two frames to compute the layout of the stack and nodes
            yield return(null);

            yield return(null);

            // Select node1
            m_Node1.Select(graphView, false);

            Vector2 start            = m_Node1.worldBound.center;
            Vector2 outsideStackNode = stack.LocalToWorld(k_OutsidePosition);

            // Drag node1 away from the stack
            helpers.MouseDownEvent(start, MouseButton.LeftMouse);
            helpers.MouseDragEvent(start, outsideStackNode, MouseButton.LeftMouse);

            // Allow one frame to compute the layout of the stack
            yield return(null);

            Vector2 centerOfNode3 = m_Node3.worldBound.center;

            helpers.MouseDragEvent(outsideStackNode, centerOfNode3, MouseButton.LeftMouse);
            helpers.MouseUpEvent(centerOfNode3);

            // Verify that node1 has been moved to index 1 (before node3)
            Assert.AreEqual(3, stack.childCount);
            Assert.AreEqual(1, stack.IndexOf(m_Node1));
        }
Beispiel #4
0
        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>();

                HashSet <GraphElement> elementsToMove = new HashSet <GraphElement>(m_GraphView.selection.OfType <GraphElement>());

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

                foreach (GraphElement ce in elementsToMove)
                {
                    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();
                    //geometry.width = 0;//For the right node aligment
                    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_Snapper.BeginSnap(selectedElement);

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