public IEnumerator ElementTopBorderShouldNotSnapToTopBorder()
        {
            // Config
            //   +-------+
            //   | Node1 |    +-------+
            //   |       |    | Node2 |
            //   |       |    +-------+
            //   +-------+

            var actions = SetUpUIElements(new Vector2(k_ReferenceNodePos.x + 100, k_ReferenceNodePos.y + 250), k_ReferenceNodePos + new Vector2(0, 200));

            while (actions.MoveNext())
            {
                yield return(null);
            }

            actions = UpdateUINodeSizes(k_SnappedNodeSize, k_ReferenceNodeSizeVertical);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            float   topToTopDistance = m_SnappedNode.layout.yMin - m_ReferenceNode1.layout.yMin;
            float   offSetY          = (k_SnapDistance + 1) - topToTopDistance;
            Vector2 moveOffset       = new Vector2(10, offSetY);

            actions = MoveElementWithOffset(moveOffset);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            // The snapping node's top border should not snap to the reference node's top border, but the X and Y should be dragged normally
            Assert.AreNotEqual(m_ReferenceNode1.layout.yMin, m_SnappedNode.layout.yMin);
            Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.y + moveOffset.y), m_SnappedNode.layout.y);
            Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.x + moveOffset.x), m_SnappedNode.layout.x);
        }
Example #2
0
        public IEnumerator ElementShouldSnapToBorderAndGridAndSpacingAndPort()
        {
            // Config
            //   |              |             |
            // --+--------------+-------------+
            //   |              |             |
            //   +-------+      +-------+     +-------+
            //   | Node3 |      | Node2 o-----o Node1 |
            //   +-------+      +-------+     +-------+
            //   |              |             |
            // --+--------------+-------------+
            //   |              |             |
            //   |              |             |

            var actions = SetUpUIElements(new Vector2(3f * k_Spacing, k_ReferenceNodePos.y),
                                          k_ReferenceNodePos,
                                          new Vector2(k_ReferenceNodePos.x - k_Spacing, k_ReferenceNodePos.y), false, true);


            while (actions.MoveNext())
            {
                yield return(null);
            }

            actions = UpdateUINodeSizes(new Vector2(k_HalfSpacing, k_HalfSpacing), new Vector2(k_HalfSpacing, k_HalfSpacing), new Vector2(k_HalfSpacing, k_HalfSpacing));
            while (actions.MoveNext())
            {
                yield return(null);
            }

            Vector2 moveOffset = new Vector2(SnapToGridHelper.GetSnapDistance(m_SnappedNode, SnapToGridHelper.Edge.Left), k_SnapDistance);

            actions = MoveElementWithOffset(moveOffset);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            // Snapped to first grid line (positioned at k_Spacing)
            var borderWidth = SnapToGridStrategy.GetBorderWidth(m_SnappedNode);

            Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(3f * k_Spacing),
                            GraphViewStaticBridge.RoundToPixelGrid(m_SnappedNode.layout.x - borderWidth.Left));

            // Snapped to borders
            Assert.AreEqual(m_SnappedNode.layout.y, m_ReferenceNode1.layout.y);

            // Get the UI ports
            var outputPortUI = m_OutputPort.GetUI <Port>(graphView);
            var inputPortUI  = m_InputPort.GetUI <Port>(graphView);

            Assert.IsNotNull(outputPortUI);
            Assert.IsNotNull(inputPortUI);

            // Snapped to Port
            Assert.AreEqual(inputPortUI.GetGlobalCenter().y, outputPortUI.GetGlobalCenter().y);

            // The three nodes should be evenly spaced horizontally up to one pixel difference
            float distanceBetweenNode1AndNode2 = m_SnappedNode.layout.xMin - GraphViewStaticBridge.RoundToPixelGrid(borderWidth.Right) - m_ReferenceNode1.layout.xMax;
            float distanceBetweenNode2AndNode3 = m_ReferenceNode1.layout.xMin - m_ReferenceNode2.layout.xMax;

            var pixelSize = 1f / GraphViewStaticBridge.PixelPerPoint;

            Assert.AreEqual(distanceBetweenNode1AndNode2, distanceBetweenNode2AndNode3, pixelSize + 0.0001f);

            // Should not be dragged normally
            Assert.AreNotEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.y + moveOffset.y),
                               m_SnappedNode.layout.y);
            Assert.AreNotEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.x + moveOffset.x),
                               m_SnappedNode.layout.x);

            yield return(null);
        }
        public IEnumerator NodeIsItsOwnParent()
        {
            // Config   +---------+
            //          |+-------+|                    +-------+
            //          +o Node2 o+-+                +-o Node5 |
            //           +-------+  |   +-------+    | +-------+
            // +-------+ +-------+  +-+-o Node4 o-+--+
            // | Node1 o-o Node3 o--+   +-------+
            // +-------+ +-------+

            Vector2 firstNodePos  = new Vector2(0, 400);
            Vector2 secondNodePos = new Vector2(200, 0);
            Vector2 thirdNodePos  = new Vector2(200, 400);
            Vector2 fourthNodePos = new Vector2(400, 200);
            Vector2 fifthNodePos  = new Vector2(600, 10);

            FirstNodeModel  = CreateNode("Node1", firstNodePos, 0, 0, 0, 1);
            SecondNodeModel = CreateNode("Node2", secondNodePos, 0, 0, 1, 1);
            ThirdNodeModel  = CreateNode("Node3", thirdNodePos, 0, 0, 1, 1);
            FourthNodeModel = CreateNode("Node4", fourthNodePos, 0, 0, 1, 1);
            var fifthNodeModel = CreateNode("Node5", fifthNodePos, 0, 0, 1);

            MarkGraphViewStateDirty();
            yield return(null);

            IPortModel outputPortFirstNode = FirstNodeModel.OutputsByDisplayOrder[0];

            Assert.IsNotNull(outputPortFirstNode);

            IPortModel inputPortSecondNode  = SecondNodeModel.InputsByDisplayOrder[0];
            IPortModel outputPortSecondNode = SecondNodeModel.OutputsByDisplayOrder[0];

            Assert.IsNotNull(inputPortSecondNode);
            Assert.IsNotNull(outputPortSecondNode);

            IPortModel inputPortThirdNode  = ThirdNodeModel.InputsByDisplayOrder[0];
            IPortModel outputPortThirdNode = ThirdNodeModel.OutputsByDisplayOrder[0];

            Assert.IsNotNull(inputPortThirdNode);
            Assert.IsNotNull(outputPortThirdNode);

            IPortModel inputPortFourthNode  = FourthNodeModel.InputsByDisplayOrder[0];
            IPortModel outputPortFourthNode = FourthNodeModel.OutputsByDisplayOrder[0];

            Assert.IsNotNull(inputPortFourthNode);
            Assert.IsNotNull(outputPortFourthNode);

            IPortModel inputPortFifthNode = fifthNodeModel.InputsByDisplayOrder[0];

            Assert.IsNotNull(inputPortFifthNode);

            // Connect the ports together
            var actions = ConnectPorts(outputPortFirstNode, inputPortThirdNode);

            while (actions.MoveNext())
            {
                yield return(null);
            }

            // Connect to itself
            actions = ConnectPorts(outputPortSecondNode, inputPortSecondNode);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            actions = ConnectPorts(outputPortSecondNode, inputPortFourthNode);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            actions = ConnectPorts(outputPortThirdNode, inputPortFourthNode);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            actions = ConnectPorts(outputPortFourthNode, inputPortFifthNode);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            Assert.AreEqual(5, GraphModel.EdgeModels.Count, "Not all edges were created.");

            // Get the UI nodes
            m_FirstNode  = FirstNodeModel.GetUI <Node>(graphView);
            m_SecondNode = SecondNodeModel.GetUI <Node>(graphView);
            m_ThirdNode  = ThirdNodeModel.GetUI <Node>(graphView);
            m_FourthNode = FourthNodeModel.GetUI <Node>(graphView);
            var fifthNode = fifthNodeModel.GetUI <Node>(graphView);

            Assert.IsNotNull(m_FirstNode);
            Assert.IsNotNull(m_SecondNode);
            Assert.IsNotNull(m_ThirdNode);
            Assert.IsNotNull(m_FourthNode);
            Assert.IsNotNull(fifthNode);

            SelectConnectedNodes();
            yield return(null);

            // Select node 5
            Vector2 selectionPosNode5 = graphView.ContentViewContainer.LocalToWorld(fifthNode.layout.position) + k_SelectionOffset;

            actions = SelectElement(selectionPosNode5);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            float expectedDistanceBetweenSecondThird = m_ThirdNode.layout.yMin - m_SecondNode.layout.yMax;
            float expectedTop = GraphViewStaticBridge.RoundToPixelGrid(0f);

            actions = AlignElements(AutoAlignmentHelper.AlignmentReference.Top);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            fifthNode = fifthNodeModel.GetUI <Node>(graphView);
            Assert.IsNotNull(fifthNode);

            // Nodes 2, 3 are part of the same group: they move together
            Assert.AreEqual(expectedTop, m_SecondNode.layout.yMin);
            Assert.AreNotEqual(expectedTop, m_ThirdNode.layout.yMin);
            Assert.AreEqual(expectedDistanceBetweenSecondThird, m_ThirdNode.layout.yMin - m_SecondNode.layout.yMax);

            // Nodes 1, 4 and 5 move individually
            Assert.AreEqual(expectedTop, m_FirstNode.layout.yMin);
            Assert.AreEqual(expectedTop, m_FourthNode.layout.yMin);
            Assert.AreEqual(expectedTop, fifthNode.layout.yMin);

            var nodeDependencies = m_AlignmentHelper.NodeDependencies;

            // There should be 3 parents
            Assert.AreEqual(3, nodeDependencies.Keys.Count);

            // Node 2 should not be considered a parent even if it is its own parent
            Assert.False(nodeDependencies.ContainsKey(SecondNodeModel));

            // Node 3 should be parent to Node 1
            Assert.True(nodeDependencies.ContainsKey(ThirdNodeModel));
            Assert.AreEqual(1, nodeDependencies[ThirdNodeModel].Count);
            Assert.True(nodeDependencies[ThirdNodeModel].Contains(FirstNodeModel));

            // Node 4 should be parent to Nodes 2,3
            Assert.True(nodeDependencies.ContainsKey(FourthNodeModel));
            Assert.AreEqual(2, nodeDependencies[FourthNodeModel].Count);
            Assert.True(nodeDependencies[FourthNodeModel].Contains(SecondNodeModel));
            Assert.True(nodeDependencies[FourthNodeModel].Contains(ThirdNodeModel));

            // Node 5 should be parent to Node 4
            Assert.True(nodeDependencies.ContainsKey(fifthNodeModel));
            Assert.AreEqual(1, nodeDependencies[fifthNodeModel].Count);
            Assert.True(nodeDependencies[fifthNodeModel].Contains(FourthNodeModel));
        }
        public IEnumerator AlignComplexConnectedNodes()
        {
            // Config
            //           +-------+                      +-------+
            //           | Node2 o--+---------------+---o Node5 |
            //           +-------+  |   +-------+   |   +-------+
            // +-------+ +-------+  +---o Node4 o---+
            // | Node1 o-o Node3 o--+   +-------+
            // +-------+ +-------+

            Vector2 firstNodePos  = new Vector2(0, 400);
            Vector2 secondNodePos = new Vector2(200, 10);
            Vector2 thirdNodePos  = new Vector2(200, 400);
            Vector2 fourthNodePos = new Vector2(400, 200);
            Vector2 fifthNodePos  = new Vector2(600, 0);

            FirstNodeModel  = CreateNode("Node1", firstNodePos, 0, 0, 0, 1);
            SecondNodeModel = CreateNode("Node2", secondNodePos, 0, 0, 0, 1);
            ThirdNodeModel  = CreateNode("Node3", thirdNodePos, 0, 0, 1, 1);
            FourthNodeModel = CreateNode("Node4", fourthNodePos, 0, 0, 1, 1);
            var fifthNodeModel = CreateNode("Node5", fifthNodePos, 0, 0, 1);

            MarkGraphViewStateDirty();
            yield return(null);

            IPortModel outputPortFirstNode = FirstNodeModel.OutputsByDisplayOrder[0];

            Assert.IsNotNull(outputPortFirstNode);

            IPortModel outputPortSecondNode = SecondNodeModel.OutputsByDisplayOrder[0];

            Assert.IsNotNull(outputPortSecondNode);

            IPortModel inputPortThirdNode  = ThirdNodeModel.InputsByDisplayOrder[0];
            IPortModel outputPortThirdNode = ThirdNodeModel.OutputsByDisplayOrder[0];

            Assert.IsNotNull(inputPortThirdNode);
            Assert.IsNotNull(outputPortThirdNode);

            IPortModel inputPortFourthNode  = FourthNodeModel.InputsByDisplayOrder[0];
            IPortModel outputPortFourthNode = FourthNodeModel.OutputsByDisplayOrder[0];

            Assert.IsNotNull(inputPortFourthNode);
            Assert.IsNotNull(outputPortFourthNode);

            IPortModel inputPortFifthNode = fifthNodeModel.InputsByDisplayOrder[0];

            Assert.IsNotNull(inputPortFifthNode);

            // Connect the ports together
            var actions = ConnectPorts(outputPortFirstNode, inputPortThirdNode);

            while (actions.MoveNext())
            {
                yield return(null);
            }

            actions = ConnectPorts(outputPortSecondNode, inputPortFourthNode);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            actions = ConnectPorts(outputPortThirdNode, inputPortFourthNode);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            actions = ConnectPorts(outputPortSecondNode, inputPortFifthNode);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            actions = ConnectPorts(outputPortFourthNode, inputPortFifthNode);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            Assert.AreEqual(5, GraphModel.EdgeModels.Count, "Not all edges were created.");

            // Get the UI nodes
            m_FirstNode  = FirstNodeModel.GetUI <Node>(graphView);
            m_SecondNode = SecondNodeModel.GetUI <Node>(graphView);
            m_ThirdNode  = ThirdNodeModel.GetUI <Node>(graphView);
            m_FourthNode = FourthNodeModel.GetUI <Node>(graphView);
            var fifthNode = fifthNodeModel.GetUI <Node>(graphView);

            Assert.IsNotNull(m_FirstNode);
            Assert.IsNotNull(m_SecondNode);
            Assert.IsNotNull(m_ThirdNode);
            Assert.IsNotNull(m_FourthNode);
            Assert.IsNotNull(fifthNode);

            SelectConnectedNodes();
            yield return(null);

            // Select node 5
            Vector2 selectionPosNode5 = graphView.ContentViewContainer.LocalToWorld(fifthNode.layout.position) + k_SelectionOffset;

            actions = SelectElement(selectionPosNode5);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            float expectedDistanceBetweenSecondThird  = m_ThirdNode.layout.yMin - m_SecondNode.layout.yMax;
            float expectedDistanceBetweenSecondFourth = m_FourthNode.layout.yMin - m_SecondNode.layout.yMax;
            float expectedTop = GraphViewStaticBridge.RoundToPixelGrid(0f);

            actions = AlignElements(AutoAlignmentHelper.AlignmentReference.Top);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            fifthNode = fifthNodeModel.GetUI <Node>(graphView);
            Assert.IsNotNull(fifthNode);

            // Nodes 2, 3 and 4 are part of the same group: they move together
            Assert.AreEqual(expectedTop, m_SecondNode.layout.yMin);
            Assert.AreNotEqual(expectedTop, m_ThirdNode.layout.yMin);
            Assert.AreNotEqual(expectedTop, m_FourthNode.layout.yMin);
            Assert.AreEqual(expectedDistanceBetweenSecondThird, m_ThirdNode.layout.yMin - m_SecondNode.layout.yMax);
            Assert.AreEqual(expectedDistanceBetweenSecondFourth, m_FourthNode.layout.yMin - m_SecondNode.layout.yMax);

            // Node 1 and node 5 move individually
            Assert.AreEqual(expectedTop, m_FirstNode.layout.yMin);
            Assert.AreEqual(expectedTop, fifthNode.layout.yMin);
        }
Example #5
0
        public IEnumerator ElementUnderMouseShouldSnapWhenMultipleSelectedElements()
        {
            // Config
            //
            //    |               |
            //----+-------+-----------------
            //    | Node1 | +-----|----+
            //    +-------+ | Placemat |
            //    |         +----------+
            //    |               |

            m_SnappingNodePos = new Vector2(k_ReferenceNodePos.x, k_ReferenceNodePos.y);
            snappingNodeModel = CreateNode("Node1", m_SnappingNodePos);

            Vector2 secondElementPos   = new Vector2(m_SnappingNodePos.x + k_HalfSpacing, m_SnappingNodePos.y + 10);
            var     secondElementModel = CreatePlacemat(new Rect(secondElementPos, new Vector2(k_HalfSpacing, k_HalfSpacing)), "Placemat");

            MarkGraphViewStateDirty();
            yield return(null);

            // Get the UI nodes
            var      snappingNode  = snappingNodeModel.GetUI <Node>(graphView);
            Placemat secondElement = secondElementModel.GetUI <Placemat>(graphView);

            Assert.IsNotNull(snappingNode);
            Assert.IsNotNull(secondElement);

            // Changing the node' size to make it easier to test the snapping
            SetUINodeSize(ref snappingNode, k_HalfSpacing, k_HalfSpacing);
            yield return(null);

            Vector2 worldPosSnappingNode  = graphView.ContentViewContainer.LocalToWorld(m_SnappingNodePos);
            Vector2 worldPosSecondElement = graphView.ContentViewContainer.LocalToWorld(secondElementPos);

            Vector2 selectionPosSnappingNode  = worldPosSnappingNode + m_SelectionOffset;
            Vector2 selectionPosSecondElement = worldPosSecondElement + m_SelectionOffset;

            // Select placemat by clicking on it and pressing Ctrl
            helpers.MouseDownEvent(selectionPosSecondElement, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            helpers.MouseUpEvent(selectionPosSecondElement, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            // Move mouse to Node2
            helpers.MouseMoveEvent(selectionPosSecondElement, selectionPosSnappingNode, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            // Select Node1 by clicking on it and pressing Ctrl
            helpers.MouseDownEvent(selectionPosSnappingNode, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            // Move Node1 within snapping distance
            Vector2 moveOffset = new Vector2(10, SnapToGridHelper.GetSnapDistance(snappingNode, SnapToGridHelper.Edge.Top));
            Vector2 end        = selectionPosSnappingNode + moveOffset;

            helpers.MouseDragEvent(selectionPosSnappingNode, end, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            helpers.MouseUpEvent(end, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            // The snapping Node1 top border should snap but X should be dragged normally
            var borderWidth = SnapToGridStrategy.GetBorderWidth(snappingNode);

            Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.y),
                            snappingNode.layout.y - GraphViewStaticBridge.RoundToPixelGrid(borderWidth.Top));
            Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.x + moveOffset.x),
                            snappingNode.layout.x);
            Assert.AreNotEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.y + moveOffset.y),
                               snappingNode.layout.y);

            // placemat should follow the same offset as Node1
            Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(secondElementPos.y),
                            secondElement.layout.y - GraphViewStaticBridge.RoundToPixelGrid(borderWidth.Top));
            Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(secondElementPos.x + moveOffset.x),
                            secondElement.layout.x);
            Assert.AreNotEqual(GraphViewStaticBridge.RoundToPixelGrid(secondElementPos.y + moveOffset.y),
                               secondElement.layout.y);

            yield return(null);
        }
Example #6
0
        void TestElementPosition(Vector2 offset, bool isSnapping, bool isHorizontalSnapping, SnapToGridHelper.Edge edgeToTest)
        {
            var   borderWidth = SnapToGridStrategy.GetBorderWidth(m_SnappedNode);
            float borderOffset;

            switch (edgeToTest)
            {
            case SnapToGridHelper.Edge.Top:
                borderOffset = -GraphViewStaticBridge.RoundToPixelGrid(borderWidth.Top);
                break;

            case SnapToGridHelper.Edge.Right:
                borderOffset = GraphViewStaticBridge.RoundToPixelGrid(borderWidth.Right);
                break;

            case SnapToGridHelper.Edge.Bottom:
                borderOffset = GraphViewStaticBridge.RoundToPixelGrid(borderWidth.Bottom);
                break;

            case SnapToGridHelper.Edge.Left:
                borderOffset = -GraphViewStaticBridge.RoundToPixelGrid(borderWidth.Left);
                break;

            default:
                borderOffset = 0.0f;
                break;
            }

            if (isSnapping)
            {
                if (isHorizontalSnapping)
                {
                    // X should snap
                    Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.x),
                                    m_SnappedNode.layout.x + borderOffset);
                    Assert.AreNotEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.x + offset.x),
                                       m_SnappedNode.layout.x + borderOffset);

                    // Y should be dragged normally
                    Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.y + offset.y),
                                    m_SnappedNode.layout.y);
                }
                else
                {
                    // Y should snap
                    Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.y),
                                    m_SnappedNode.layout.y + borderOffset);
                    Assert.AreNotEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.y + offset.y),
                                       m_SnappedNode.layout.y + borderOffset);

                    // X should be dragged normally
                    Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.x + offset.x),
                                    m_SnappedNode.layout.x);
                }
            }
            else
            {
                // X and Y should be dragged normally
                Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.y + offset.y),
                                m_SnappedNode.layout.y);
                Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.x + offset.x),
                                m_SnappedNode.layout.x);
            }
        }
Example #7
0
        public IEnumerator ChangingGraphViewTransformRoundsToPixelGrid()
        {
            var capturedPos = Vector3.zero;

            graphView.viewTransformChanged += graphView => capturedPos = graphView.contentViewContainer.transform.position;

            var pos = new Vector3(10.3f, 10.6f, 10.0f);

            graphView.UpdateViewTransform(pos, new Vector3(10, 10));

            yield return(null);

            Assert.AreEqual(new Vector3(GraphViewStaticBridge.RoundToPixelGrid(pos.x), GraphViewStaticBridge.RoundToPixelGrid(pos.y), 10.0f), capturedPos);

            yield return(null);
        }
        public IEnumerator NodeUnderMouseShouldSnapWhenMultipleSelectedNodes()
        {
            // Config (ports are connected horizontally)
            //   +-------+
            //   | Node1 o +-------+
            //   +-------+ o Node2 o +-------+
            //             +-------+ o Node3 |
            //                       +-------+

            referenceNode1Model = CreateNode("Node1", k_ReferenceNodePos, 0, 1);

            m_SnappingNodePos = new Vector2(k_ReferenceNodePos.x + k_NodeSize.x, k_ReferenceNodePos.y + k_NodeSize.y * 0.5f);
            snappingNodeModel = CreateNode("Node2", m_SnappingNodePos, 1, 1);

            // Third node
            Vector2 secondSelectedNodePos = new Vector2(m_SnappingNodePos.x + k_NodeSize.x, m_SnappingNodePos.y + k_NodeSize.y * 0.5f);
            IInputOutputPortsNodeModel secondSelectedNodeModel = CreateNode("Node3", secondSelectedNodePos, 1);

            var node1OutputPort = referenceNode1Model.OutputsByDisplayOrder[0];
            var node2InputPort  = snappingNodeModel.InputsByDisplayOrder[0];
            var node2OutputPort = snappingNodeModel.OutputsByDisplayOrder[0];
            var node3InputPort  = secondSelectedNodeModel.InputsByDisplayOrder[0];

            Assert.IsNotNull(node1OutputPort);
            Assert.IsNotNull(node2InputPort);
            Assert.IsNotNull(node2OutputPort);
            Assert.IsNotNull(node3InputPort);

            MarkGraphViewStateDirty();
            yield return(null);

            // Connect the ports together
            var actions = ConnectPorts(node1OutputPort, node2InputPort);

            while (actions.MoveNext())
            {
                yield return(null);
            }
            actions = ConnectPorts(node2OutputPort, node3InputPort);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            Vector2 worldPosNode2 = graphView.ContentViewContainer.LocalToWorld(m_SnappingNodePos);
            Vector2 worldPosNode3 = graphView.ContentViewContainer.LocalToWorld(secondSelectedNodePos);

            Vector2 selectionPosNode2 = worldPosNode2 + m_SelectionOffset;
            Vector2 selectionPosNode3 = worldPosNode3 + m_SelectionOffset;

            // Select Node3 by clicking on it and pressing Ctrl
            helpers.MouseDownEvent(selectionPosNode3, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            helpers.MouseUpEvent(selectionPosNode3, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            // Move mouse to Node2
            helpers.MouseMoveEvent(selectionPosNode3, selectionPosNode2, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            // Select Node2 by clicking on it and pressing Ctrl
            helpers.MouseDownEvent(selectionPosNode2, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            // Move Node2 toward reference Node1 within the snapping range
            float   topToTopDistance = k_NodeSize.y * 0.5f;
            float   offSetY          = k_SnapDistance - topToTopDistance;
            Vector2 moveOffset       = new Vector2(0, offSetY);
            Vector2 end = selectionPosNode2 + moveOffset;

            helpers.MouseDragEvent(selectionPosNode2, end, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            helpers.MouseUpEvent(end, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            // Get the UI ports
            var node1OutputPortUI = node1OutputPort.GetUI <Port>(graphView);
            var node2InputPortUI  = node2InputPort.GetUI <Port>(graphView);
            var node2OutputPortUI = node2OutputPort.GetUI <Port>(graphView);
            var node3InputPortUI  = node3InputPort.GetUI <Port>(graphView);
            var node3             = secondSelectedNodeModel.GetUI <Node>(graphView);

            Assert.IsNotNull(node1OutputPortUI);
            Assert.IsNotNull(node2InputPortUI);
            Assert.IsNotNull(node2OutputPortUI);
            Assert.IsNotNull(node3InputPortUI);
            Assert.IsNotNull(node3);

            // The snapping Node2 should snap to Node1's port
            Assert.AreEqual(node1OutputPortUI.GetGlobalCenter().y, node2InputPortUI.GetGlobalCenter().y);
            // Node3 should not snap to Node1's port
            Assert.AreNotEqual(node1OutputPortUI.GetGlobalCenter().y, node3InputPortUI.GetGlobalCenter().y);
            // Node 3 should have moved by the move offset in x and the same y offset as Node2
            Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(secondSelectedNodePos.x + moveOffset.x), node3.layout.x);
            Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(secondSelectedNodePos.y - topToTopDistance), node3.layout.y);

            yield return(null);
        }
        public IEnumerator NodeShouldSnapToNearestConnectedPort()
        {
            // Config (ports are connected horizontally)
            //   +-------+
            //   | Node1 o +-------+
            //   +-------+ o Node2 o +-------+
            //             +-------+ o Node3 |
            //                       +-------+

            referenceNode1Model = CreateNode("Node1", GraphViewStaticBridge.RoundToPixelGrid(k_ReferenceNodePos), 0, 1);

            m_SnappingNodePos = GraphViewStaticBridge.RoundToPixelGrid(new Vector2(k_ReferenceNodePos.x + k_NodeSize.x, k_ReferenceNodePos.y + k_NodeSize.y * 0.5f));
            snappingNodeModel = CreateNode("Node2", m_SnappingNodePos, 1, 1);

            // Third node
            Vector2 secondReferenceNodePos = GraphViewStaticBridge.RoundToPixelGrid(new Vector2(m_SnappingNodePos.x + k_NodeSize.x, m_SnappingNodePos.y + k_NodeSize.y * 0.5f));
            IInputOutputPortsNodeModel secondReferenceNodeModel = CreateNode("Node3", secondReferenceNodePos, 1);

            var node1OutputPort = referenceNode1Model.OutputsByDisplayOrder[0];
            var node2InputPort  = snappingNodeModel.InputsByDisplayOrder[0];
            var node2OutputPort = snappingNodeModel.OutputsByDisplayOrder[0];
            var node3InputPort  = secondReferenceNodeModel.InputsByDisplayOrder[0];

            Assert.IsNotNull(node1OutputPort);
            Assert.IsNotNull(node2InputPort);
            Assert.IsNotNull(node2OutputPort);
            Assert.IsNotNull(node3InputPort);

            MarkGraphViewStateDirty();
            yield return(null);

            // Connect the ports together
            var actions = ConnectPorts(node1OutputPort, node2InputPort);

            while (actions.MoveNext())
            {
                yield return(null);
            }
            actions = ConnectPorts(node2OutputPort, node3InputPort);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            Vector2 worldNodePos = graphView.ContentViewContainer.LocalToWorld(m_SnappingNodePos);
            Vector2 start        = worldNodePos + m_SelectionOffset;

            // We move the snapping Node2 toward reference Node1 within the snapping range
            float   offSetY    = k_SnapDistance - k_NodeSize.y * 0.5f;
            Vector2 moveOffset = GraphViewStaticBridge.RoundToPixelGrid(new Vector2(0, offSetY));
            Vector2 end        = start + moveOffset;

            // Move the snapping node.
            helpers.MouseDownEvent(start);
            yield return(null);

            helpers.MouseDragEvent(start, end);
            yield return(null);

            helpers.MouseUpEvent(end);
            yield return(null);

            // Get the UI ports
            var node1OutputPortUI = node1OutputPort.GetUI <Port>(graphView);
            var node2InputPortUI  = node2InputPort.GetUI <Port>(graphView);
            var node2OutputPortUI = node2OutputPort.GetUI <Port>(graphView);
            var node3InputPortUI  = node3InputPort.GetUI <Port>(graphView);

            Assert.IsNotNull(node1OutputPortUI);
            Assert.IsNotNull(node2InputPortUI);
            Assert.IsNotNull(node2OutputPortUI);
            Assert.IsNotNull(node3InputPortUI);

            // The snapping Node2 should snap to Node1's port
            Assert.AreEqual(node1OutputPortUI.GetGlobalCenter().y, node2InputPortUI.GetGlobalCenter().y);
            // The snapping Node2 should not snap to Node3's port
            Assert.AreNotEqual(node3InputPortUI.GetGlobalCenter().y, node2OutputPortUI.GetGlobalCenter().y);

            worldNodePos = graphView.ContentViewContainer.LocalToWorld(snappingNodeModel.Position);
            start        = worldNodePos + m_SelectionOffset;

            // We move the snapping Node2 toward Node3 within the snapping range
            offSetY    = k_NodeSize.y + k_SnapDistance - 1;
            moveOffset = GraphViewStaticBridge.RoundToPixelGrid(new Vector2(0, offSetY));

            // Move the snapping node.
            helpers.MouseDownEvent(start);
            yield return(null);

            end = start + moveOffset;
            helpers.MouseDragEvent(start, end);
            yield return(null);

            helpers.MouseUpEvent(end);
            yield return(null);

            // The snapping Node2's port should snap to Node3's port
            Assert.AreEqual(node3InputPortUI.GetGlobalCenter().y, node2OutputPortUI.GetGlobalCenter().y);
            // The snapping Node2's port should not snap to Node1's port
            Assert.AreNotEqual(node1OutputPortUI.GetGlobalCenter().y, node2InputPortUI.GetGlobalCenter().y);

            yield return(null);
        }
Example #10
0
        public IEnumerator ChangingGraphViewTransformRoundsToPixelGrid()
        {
            var pos = new Vector3(10.3f, 10.6f, 10.0f);

            CommandDispatcher.Dispatch(new ReframeGraphViewCommand(pos, new Vector3(10, 10)));
            yield return(null);

            Vector3 p = graphView.ContentViewContainer.transform.position;

            Assert.AreEqual(new Vector3(GraphViewStaticBridge.RoundToPixelGrid(pos.x), GraphViewStaticBridge.RoundToPixelGrid(pos.y), 10.0f), p);
        }