public IEnumerator ResizingGraphElementExecutesElementResizedDelegate() { bool elementResized = false; graphView.elementResized += elements => elementResized = true; var graphElement = new TestGraphElement(); graphElement.SetPosition(new Rect(50, 50, 50, 50)); graphElement.style.width = 50; graphElement.style.height = 50; graphElement.capabilities |= Capabilities.Resizable; graphView.AddElement(graphElement); yield return(null); var size = graphElement.worldBound.size; var resizeElementPosition = graphElement.worldBound.center + size / 2 - new Vector2(5, 15); helpers.MouseDownEvent(resizeElementPosition); yield return(null); helpers.MouseDragEvent(resizeElementPosition, resizeElementPosition + new Vector2(10, 10)); yield return(null); helpers.MouseUpEvent(resizeElementPosition + new Vector2(10, 10)); yield return(null); Assert.IsTrue(elementResized); yield return(null); }
public static bool AreAdjacent(TestGraphElement lhs, TestGraphElement rhs) { Vector2 lhsPos = lhs.position; Vector2 rhsPos = rhs.position; bool adjCol = lhsPos.x == rhsPos.x && Mathf.Abs(lhsPos.y - rhsPos.y) == 1.0f; bool adjRow = lhsPos.y == rhsPos.y && Mathf.Abs(lhsPos.x - rhsPos.x) == 1.0f; return(adjCol || adjRow); }
public void CompletePathTest() { var graph = new Algorithms.AStar <TestGraphElement> .LazyGraph(); Func <TestGraphElement, TestGraphElement, float> costFunc = (TestGraphElement lhs, TestGraphElement rhs) => { return(TestGraphElement.DistanceBetween(lhs, rhs)); }; IDictionary <Vector2, TestGraphElement> graphElements = new Dictionary <Vector2, TestGraphElement> (); Func <Vector2, TestGraphElement> getGraphElementFunc = (Vector2 position) => { if (!graphElements.ContainsKey(position)) { graphElements[position] = TestGraphElement.At(position); } Assert.IsTrue(graphElements[position] != null && graphElements[position].position == position); return(graphElements[position]); }; graph.getElementsConnectedToElementFunc = (TestGraphElement testPos) => { IList <TestGraphElement> connectedElems = new List <TestGraphElement> (); connectedElems.Add(getGraphElementFunc(OffsetPos(testPos.position, Vector2.right))); connectedElems.Add(getGraphElementFunc(OffsetPos(testPos.position, -Vector2.right))); connectedElems.Add(getGraphElementFunc(OffsetPos(testPos.position, Vector2.up))); connectedElems.Add(getGraphElementFunc(OffsetPos(testPos.position, -Vector2.up))); return(connectedElems); }; graph.getActualCostForMovementBetweenElementsFunc = costFunc; graph.getLowestCostEstimateForMovementBetweenElementsFunc = costFunc; Assert.IsTrue(graph.GetIsValid()); var aStarSearch = new Algorithms.AStar <TestGraphElement>(graph); // Calculate a route... Vector2 startPos = Vector2.zero; Vector2 endPos = new Vector2(5, 0); int xOffset = Mathf.RoundToInt(Mathf.Abs(startPos.x - endPos.x)); int yOffset = Mathf.RoundToInt(Mathf.Abs(startPos.y - endPos.y)); int shortestRouteNumElements = xOffset + yOffset + 1; IList <TestGraphElement> route = aStarSearch.Calculate(getGraphElementFunc(startPos), getGraphElementFunc(endPos)); Assert.IsNotNull(route); Assert.AreEqual(route.Count, shortestRouteNumElements); Assert.AreEqual(route.First().position, startPos); Assert.AreEqual(route.Last().position, endPos); for (int currTestIndex = 1; currTestIndex < route.Count; ++currTestIndex) { TestGraphElement prevElem = route[currTestIndex - 1]; TestGraphElement currElem = route[currTestIndex]; Assert.IsTrue(TestGraphElement.AreAdjacent(prevElem, currElem)); } }
public override void SetUp() { base.SetUp(); m_Element1 = new TestGraphElement(); m_Element1.SetPosition(k_Element1Position); m_Element1.style.width = k_Element1Position.width; m_Element1.style.height = k_Element1Position.height; m_Element1.capabilities |= Capabilities.Resizable; graphView.AddElement(m_Element1); m_Element2 = new TestGraphElement(); m_Element2.SetPosition(k_Element2Position); m_Element2.style.width = k_Element2Position.width; m_Element2.style.height = k_Element2Position.height; m_Element2.capabilities |= Capabilities.Resizable; graphView.AddElement(m_Element2); }
public static float DistanceBetween(TestGraphElement lhs, TestGraphElement rhs) { Assert.IsTrue(lhs != null && rhs != null); return((lhs.position - rhs.position).magnitude); }
public void PartialPathTest() { var graph = new Algorithms.AStar <TestGraphElement> .LazyGraph(); Func <TestGraphElement, TestGraphElement, float> costFunc = (TestGraphElement lhs, TestGraphElement rhs) => { return(TestGraphElement.DistanceBetween(lhs, rhs)); }; IDictionary <Vector2, TestGraphElement> graphElements = new Dictionary <Vector2, TestGraphElement> (); Func <Vector2, TestGraphElement> getGraphElementFunc = (Vector2 position) => { if (!graphElements.ContainsKey(position)) { graphElements[position] = TestGraphElement.At(position); } Assert.IsTrue(graphElements[position] != null && graphElements[position].position == position); return(graphElements[position]); }; graph.getElementsConnectedToElementFunc = (TestGraphElement testPos) => { // Add a gap in the graph between x=2 and x=3 so we cannot do a complete path. float graphBreakXPos = 2.0f; IList <TestGraphElement> connectedElems = new List <TestGraphElement> (); if (testPos.position.x != graphBreakXPos) { connectedElems.Add(getGraphElementFunc(OffsetPos(testPos.position, Vector2.right))); } if (testPos.position.x != graphBreakXPos + 1.0f) { connectedElems.Add(getGraphElementFunc(OffsetPos(testPos.position, -Vector2.right))); } connectedElems.Add(getGraphElementFunc(OffsetPos(testPos.position, Vector2.up))); connectedElems.Add(getGraphElementFunc(OffsetPos(testPos.position, -Vector2.up))); return(connectedElems); }; graph.getActualCostForMovementBetweenElementsFunc = costFunc; graph.getLowestCostEstimateForMovementBetweenElementsFunc = costFunc; Assert.IsTrue(graph.GetIsValid()); var aStarSearch = new Algorithms.AStar <TestGraphElement>(graph); // Calculate a route... Vector2 startPos = Vector2.zero; Vector2 endPos = new Vector2(5, 0); // Check that we can't find a complete route between the two points (as there is a break in the graph). // Note: As we have an non-finite graph and there is no complete path, we MUST give a worst acceptable route cost as otherwise the // calculation will never terminate (as it can never be sure there isn't a viable solution in the parts of the graph it hasn't reached yet). IList <TestGraphElement> completeRoute = aStarSearch.Calculate(getGraphElementFunc(startPos), getGraphElementFunc(endPos), false, 100.0f); Assert.IsNull(completeRoute); IList <TestGraphElement> partialRoute = aStarSearch.Calculate(getGraphElementFunc(startPos), getGraphElementFunc(endPos), true, 100.0f); Assert.IsNotNull(partialRoute); // Check the best partial route we found is what we would expect... Assert.AreEqual(partialRoute.Count, 3); Assert.AreEqual(partialRoute[0].position, startPos); Assert.AreEqual(partialRoute[1].position, new Vector2(1, 0)); Assert.AreEqual(partialRoute[2].position, new Vector2(2, 0)); }