public IEnumerator ElementShouldSnapToGridAndPort() { // Config // | | // --------+-------+----+------ + // | Node1 o----o Node2 | // +-------+ | +-------+ // | | GraphViewSettings.UserSettings.EnableSnapToBorders = false; GraphViewSettings.UserSettings.EnableSnapToSpacing = false; var actions = SetUpUIElements(new Vector2(k_Spacing, k_Spacing + k_QuarterSpacing), k_ReferenceNodePos, Vector2.zero, false, true); while (actions.MoveNext()) { yield return(null); } actions = UpdateUINodeSizes(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); } // 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); // Snapped to Grid var borderWidth = SnapToGridStrategy.GetBorderWidth(m_SnappedNode); Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.x), GraphViewStaticBridge.RoundToPixelGrid(m_SnappedNode.layout.x - borderWidth.Left)); Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.y), m_SnappedNode.layout.y, 0.0001); // 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), GraphViewStaticBridge.RoundToPixelGrid(m_SnappedNode.layout.x - borderWidth.Left)); yield return(null); }
public IEnumerator ElementShouldSnapToBorderAndGrid() { // Config // | | // --+------------+-------- // | | // +-------+ +-------+ // | Node1 o----o Node2 | // +-------+ +-------+ // | | // --+------------+-------| // | | // | | GraphViewSettings.UserSettings.EnableSnapToPort = false; GraphViewSettings.UserSettings.EnableSnapToSpacing = false; var actions = SetUpUIElements(new Vector2(k_Spacing + k_QuarterSpacing, 2f * k_Spacing + k_QuarterSpacing), k_ReferenceNodePos, Vector2.zero, false, true); while (actions.MoveNext()) { yield return(null); } actions = UpdateUINodeSizes(new Vector2(k_HalfSpacing, k_HalfSpacing), new Vector2(k_HalfSpacing, k_HalfSpacing)); while (actions.MoveNext()) { yield return(null); } Vector2 moveOffset = new Vector2(k_SnapDistance - k_QuarterSpacing, -(k_Spacing + 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(k_Spacing), GraphViewStaticBridge.RoundToPixelGrid(m_SnappedNode.layout.x - borderWidth.Left)); // Snapped to borders Assert.AreEqual(m_SnappedNode.layout.y, m_ReferenceNode1.layout.y); // Should not be dragged normally Assert.AreNotEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.y + moveOffset.y), GraphViewStaticBridge.RoundToPixelGrid(m_SnappedNode.layout.y - borderWidth.Top)); Assert.AreNotEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.x + moveOffset.x), GraphViewStaticBridge.RoundToPixelGrid(m_SnappedNode.layout.x - borderWidth.Left)); yield return(null); }
public IEnumerator ElementShouldSnapToSpacingAndGrid() { // Config // //---+-------+----------------------------- // | Node1 | +-------+ +-------+ // +-------+ | Node2 | | Node3 | // +-------+ +-------+ // GraphViewSettings.UserSettings.EnableSnapToPort = false; GraphViewSettings.UserSettings.EnableSnapToBorders = false; var actions = SetUpUIElements(new Vector2(k_HalfSpacing + k_QuarterSpacing, k_ReferenceNodePos.y + k_QuarterSpacing), k_ReferenceNodePos, k_ReferenceNodePos + new Vector2(k_Spacing + k_QuarterSpacing, 0)); 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(k_SnapDistance, -k_HalfSpacing + SnapToGridHelper.GetSnapDistance(m_SnappedNode, SnapToGridHelper.Edge.Top)); actions = MoveElementWithOffset(moveOffset); while (actions.MoveNext()) { yield return(null); } // Snapped to top grid line var borderWidth = SnapToGridStrategy.GetBorderWidth(m_SnappedNode); Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(k_Spacing), m_SnappedNode.layout.yMin - GraphViewStaticBridge.RoundToPixelGrid(borderWidth.Top)); // The three nodes should be evenly spaced horizontally float distanceBetweenNode1AndNode2 = m_ReferenceNode1.layout.xMin - m_SnappedNode.layout.xMax; float distanceBetweenNode2AndNode3 = m_ReferenceNode2.layout.xMin - m_ReferenceNode1.layout.xMax; Assert.AreEqual(distanceBetweenNode1AndNode2, distanceBetweenNode2AndNode3); // 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 ElementShouldSnapToMultipleGridLines() { // Config // | | // --------+--+-------+--------- // | | Node1 | // | +-------+ // | | var actions = SetUpUIElements(new Vector2(k_ReferenceNodePos.x + k_HalfSpacing, k_ReferenceNodePos.y)); while (actions.MoveNext()) { yield return(null); } actions = UpdateUINodeSizes(k_NewSize, k_NewSize, k_NewSize); while (actions.MoveNext()) { yield return(null); } Vector2 moveOffset = new Vector2(SnapToGridHelper.GetSnapDistance(m_SnappedNode, SnapToGridHelper.Edge.Right), SnapToGridHelper.GetSnapDistance(m_SnappedNode, SnapToGridHelper.Edge.Top)); actions = MoveElementWithOffset(moveOffset); while (actions.MoveNext()) { yield return(null); } // The snapping node's top and right border should snap to the corresponding grid lines var borderWidth = SnapToGridStrategy.GetBorderWidth(m_SnappedNode); Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.x), GraphViewStaticBridge.RoundToPixelGrid(m_SnappedNode.layout.x + borderWidth.Right)); Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.y), m_SnappedNode.layout.y - GraphViewStaticBridge.RoundToPixelGrid(borderWidth.Top)); Assert.AreNotEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.y + moveOffset.y), m_SnappedNode.layout.y - GraphViewStaticBridge.RoundToPixelGrid(borderWidth.Top)); Assert.AreNotEqual(GraphViewStaticBridge.RoundToPixelGrid(m_SnappingNodePos.x + moveOffset.x), GraphViewStaticBridge.RoundToPixelGrid(m_SnappedNode.layout.x + borderWidth.Right)); yield return(null); }
public static float GetSnapDistance(GraphElement node, Edge edge) { var borderWidth = SnapToGridStrategy.GetBorderWidth(node); switch (edge) { case Edge.Top: return(k_SnapDistance + borderWidth.Top); case Edge.Right: return(k_SnapDistance - borderWidth.Right); case Edge.Bottom: return(k_SnapDistance - borderWidth.Bottom); case Edge.Left: return(k_SnapDistance + borderWidth.Left); default: return(k_SnapDistance); } }
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 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); }
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); } }