예제 #1
0
        public IEnumerator ScopeWithAddedElementHasValidBounds()
        {
            // Create a scope with an empty title to determine the total (left, top, right, bottom) margins around the content of the scope
            Scope scope = CreateScope(10, 10);

            Node TestNode1 = CreateNode("Test node 1", new Vector2(100, 100), 1, 1);
            Node TestNode2 = CreateNode("Test node 2", new Vector2(200, 200), 1, 1);

            // Allow one frame for the scope to be placed onto a layer and for the nodes to compute their layouts
            yield return(null);

            scope.AddElement(TestNode1);
            scope.AddElement(TestNode2);

            // Allow couple frames for the scope to compute its geometry (position)
            int iteration = 0;

            while (scope.hasPendingGeometryUpdate && iteration++ < k_MaxIterations)
            {
                yield return(null);
            }

            // Allow one frame for the scope to update its layout
            yield return(null);

            Rect contentBoundingRectInViewportSpace = GetBoundingRectInViewportSpace(scope.containedElements);
            Rect scopeGeometryInViewportSpace       = scope.ChangeCoordinatesTo(graphView.contentViewContainer, scope.GetRect());

            Assert.IsTrue(scopeGeometryInViewportSpace.Contains(contentBoundingRectInViewportSpace.min));
            Assert.IsTrue(scopeGeometryInViewportSpace.Contains(contentBoundingRectInViewportSpace.max));
        }
예제 #2
0
        public void ScopeContainsAddedElements()
        {
            // Create a scope
            Scope scope = CreateScope(10, 10);

            // Verify that nodes are properly added to the scope
            scope.AddElement(m_Node1);
            scope.AddElement(m_Node2);

            Assert.AreEqual(2, scope.containedElements.Count());

            Assert.IsTrue(scope.ContainsElement(m_Node1));
            Assert.AreEqual(scope, m_Node1.GetContainingScope());
            Assert.AreNotEqual(scope, m_Node1.parent);

            Assert.IsTrue(scope.ContainsElement(m_Node2));
            Assert.AreEqual(scope, m_Node2.GetContainingScope());
            Assert.AreNotEqual(scope, m_Node2.parent);
        }
예제 #3
0
        public void PreventNestedScope()
        {
            // Creates two scopes
            Scope scope1 = CreateScope(10, 10);
            Scope scope2 = CreateScope(10, 10);

            ArgumentException ex = Assert.Throws <ArgumentException>(() => { scope1.AddElement(scope2); });

            Assert.AreEqual("Nested scope is not supported yet.", ex.Message);
        }
예제 #4
0
        public IEnumerator MoveScope()
        {
            // Create a scope and add node1, node2
            Scope scope = CreateScope(10, 10);

            scope.AddElement(m_Node1);
            scope.AddElement(m_Node2);

            // Allow one frame for the scope to be placed onto a layer and for the nodes to compute their layouts
            yield return(null);

            // Allow couple frames for the scope to compute its geometry (position)
            int iteration = 0;

            while (scope.hasPendingGeometryUpdate && iteration++ < k_MaxIterations)
            {
                yield return(null);
            }

            // Allow one frame for the scope to update its layout
            yield return(null);

            Vector2 node1Geom = m_Node1.GetPosition().position;
            Vector2 node2Geom = m_Node2.GetPosition().position;

            // Move the scope
            Rect geometry = scope.GetPosition();

            geometry.x += 10;
            geometry.y += 10;

            scope.SetPosition(geometry);
            yield return(null);

            // Verify that node1 and node2 have been moved by (10, 10)
            Assert.AreEqual(node1Geom + new Vector2(10, 10), m_Node1.GetPosition().position);
            Assert.AreEqual(node2Geom + new Vector2(10, 10), m_Node2.GetPosition().position);
        }
예제 #5
0
        public void ScopeDoesNoContainRemovedElements()
        {
            // Create a scope
            Scope scope = CreateScope(10, 10);

            scope.AddElement(m_Node1);
            scope.AddElement(m_Node2);

            // Verify that a node is properly removed from its containing scope
            scope.RemoveElement(m_Node1);

            Assert.AreEqual(1, scope.containedElements.Count());
            Assert.IsFalse(scope.ContainsElement(m_Node1));
            Assert.IsNull(m_Node1.GetContainingScope());
            Assert.IsNotNull(m_Node1.parent);

            scope.RemoveElement(m_Node2);

            Assert.AreEqual(0, scope.containedElements.Count());
            Assert.IsFalse(scope.ContainsElement(m_Node2));
            Assert.IsNull(m_Node2.GetContainingScope());
            Assert.IsNotNull(m_Node2.parent);
        }
예제 #6
0
        public void MoveToAnotherScope()
        {
            // Create two scopes
            Scope scope1 = CreateScope(10, 10);
            Scope scope2 = CreateScope(10, 10);

            // Add to scope1
            scope1.AddElement(m_Node1);

            Assert.AreEqual(1, scope1.containedElements.Count());
            Assert.IsTrue(scope1.ContainsElement(m_Node1));
            Assert.AreEqual(scope1, m_Node1.GetContainingScope());

            // ...and then move to scope2
            scope2.AddElement(m_Node1);

            Assert.AreEqual(0, scope1.containedElements.Count());
            Assert.IsFalse(scope1.ContainsElement(m_Node1));
            Assert.AreEqual(1, scope2.containedElements.Count());
            Assert.IsTrue(scope2.ContainsElement(m_Node1));
            Assert.AreEqual(scope2, m_Node1.GetContainingScope());
        }
        public void Reload(IEnumerable <MathNode> nodesToReload, IEnumerable <MathPlacemat> placemats, IEnumerable <MathStickyNote> stickies, Dictionary <string, string> oldToNewIdMapping = null)
        {
            string oldId;
            var    nodes          = new Dictionary <MathNode, GraphElement>();
            var    oldIdToNewNode = new Dictionary <string, ISelectable>();

            var newToOldIdMapping = new Dictionary <string, string>();

            if (oldToNewIdMapping != null)
            {
                foreach (var oldIdKV in oldToNewIdMapping)
                {
                    newToOldIdMapping[oldIdKV.Value] = oldIdKV.Key;
                }
            }

            // Create the nodes.
            foreach (MathNode mathNode in nodesToReload)
            {
                GraphElement node = m_SimpleGraphViewWindow.CreateNode(mathNode);

                if (node is Group)
                {
                    node.name = "SimpleGroup";
                }
                else if (node is Scope)
                {
                    node.name = "SimpleScope";
                }
                else
                {
                    node.name = "SimpleNode";
                }

                if (node == null)
                {
                    Debug.LogError("Could not create node " + mathNode);
                    continue;
                }

                nodes[mathNode] = node;

                if (mathNode.groupNode == null)
                {
                    if (newToOldIdMapping.TryGetValue(mathNode.nodeID.ToString(), out oldId))
                    {
                        oldIdToNewNode.Add(oldId, node);
                    }
                }

                AddElement(node);
            }

            // Assign scopes
            foreach (MathNode mathNode in nodesToReload)
            {
                if (mathNode.groupNode == null)
                {
                    continue;
                }

                Scope graphScope = nodes[mathNode.groupNode] as Scope;

                graphScope.AddElement(nodes[mathNode]);
            }

            // Add to stacks
            foreach (MathNode mathNode in nodesToReload)
            {
                MathStackNode stack = mathNode as MathStackNode;

                if (stack == null)
                {
                    continue;
                }

                StackNode graphStackNode = nodes[stack] as StackNode;

                for (int i = 0; i < stack.nodeCount; ++i)
                {
                    MathNode stackMember = stack.GetNode(i);
                    if (stackMember == null)
                    {
                        Debug.LogWarning("null stack member! Item " + i + " of stack " + stack.name + " is null. Possibly a leftover from bad previous manips.");
                    }

                    graphStackNode.AddElement(nodes[stackMember]);
                }
            }

            // Connect the presenters.
            foreach (var mathNode in nodesToReload)
            {
                if (mathNode is MathOperator)
                {
                    MathOperator mathOperator = mathNode as MathOperator;

                    if (!nodes.ContainsKey(mathNode))
                    {
                        Debug.LogError("No element found for " + mathNode);
                        continue;
                    }

                    var graphNode = nodes[mathNode] as Node;

                    if (mathOperator.left != null && nodes.ContainsKey(mathOperator.left))
                    {
                        var outputPort = (nodes[mathOperator.left] as Node).outputContainer[0] as Port;
                        var inputPort  = graphNode.inputContainer[0] as Port;

                        Edge edge = inputPort.ConnectTo(outputPort);
                        edge.viewDataKey = mathOperator.left.nodeID + "_edge";
                        AddElement(edge);
                    }
                    else if (mathOperator.left != null)
                    {
                        //add.m_Left = null;
                        Debug.LogWarning("Invalid left operand for operator " + mathOperator + " , " + mathOperator.left);
                    }

                    if (mathOperator.right != null && nodes.ContainsKey(mathOperator.right))
                    {
                        var outputPort = (nodes[mathOperator.right] as Node).outputContainer[0] as Port;
                        var inputPort  = graphNode.inputContainer[1] as Port;

                        Edge edge = inputPort.ConnectTo(outputPort);
                        edge.viewDataKey = mathOperator.right.nodeID + "_edge";
                        AddElement(edge);
                    }
                    else if (mathOperator.right != null)
                    {
                        Debug.LogWarning("Invalid right operand for operator " + mathOperator + " , " + mathOperator.right);
                    }
                }
                else if (mathNode is MathFunction)
                {
                    MathFunction mathFunction = mathNode as MathFunction;

                    if (!nodes.ContainsKey(mathNode))
                    {
                        Debug.LogError("No element found for " + mathNode);
                        continue;
                    }

                    var graphNode = nodes[mathNode] as Node;

                    for (int i = 0; i < mathFunction.parameterCount; ++i)
                    {
                        MathNode param = mathFunction.GetParameter(i);

                        if (param != null && nodes.ContainsKey(param))
                        {
                            var outputPort = (nodes[param] as Node).outputContainer[0] as Port;
                            var inputPort  = graphNode.inputContainer[i] as Port;

                            Edge edge = inputPort.ConnectTo(outputPort);
                            edge.viewDataKey = param.nodeID + "_edge";
                            AddElement(edge);
                        }
                        else if (param != null)
                        {
                            Debug.LogWarning("Invalid parameter for function" + mathFunction + " , " +
                                             param);
                        }
                    }
                }
                else if (mathNode is MathResult)
                {
                    MathResult mathResult = mathNode as MathResult;
                    var        graphNode  = nodes[mathNode] as Node;

                    if (mathResult.root != null)
                    {
                        var outputPort = (nodes[mathResult.root] as Node).outputContainer[0] as Port;
                        var inputPort  = graphNode.inputContainer[0] as Port;

                        Edge edge = inputPort.ConnectTo(outputPort);
                        edge.viewDataKey = mathResult.root.nodeID + "_edge";
                        AddElement(edge);
                    }
                }
            }

            foreach (var matModel in placemats.OrderBy(p => p.zOrder))
            {
                var newPlacemat = placematContainer.CreatePlacemat <SimplePlacemat>(matModel.position, matModel.zOrder, matModel.title);
                newPlacemat.userData    = matModel;
                newPlacemat.viewDataKey = matModel.identification;
                newPlacemat.Model       = matModel;

                if (newToOldIdMapping.TryGetValue(matModel.identification, out oldId))
                {
                    oldIdToNewNode.Add(oldId, newPlacemat);
                }
            }

            if (stickies != null)
            {
                var existingStickies = this.Query <SimpleStickyNote>().ToList();

                foreach (var sticky in existingStickies.Where(t => !stickies.Contains(t.model)))
                {
                    RemoveElement(sticky);
                }

                foreach (var stickyModel in stickies.Except(existingStickies.Select(t => t.model)))
                {
                    var newSticky = new SimpleStickyNote();
                    newSticky.model    = stickyModel;
                    newSticky.userData = stickyModel;
                    AddElement(newSticky);

                    if (newToOldIdMapping.TryGetValue(stickyModel.id, out oldId))
                    {
                        oldIdToNewNode.Add(oldId, newSticky);
                    }
                }
            }

            // Now that all graph elements have been created, init the collapsed elements of each placemat.
            foreach (var p in this.Query <SimplePlacemat>().ToList())
            {
                p.InitCollapsedElementsFromModel();
            }

            // Make sure collapsed edges are hidden.
            placematContainer.HideCollapsedEdges();

            UpdateSelection(oldIdToNewNode);

            RebuildBlackboard();
        }
예제 #8
0
        public IEnumerator UpdateScopeGeometryAfterMovedElements()
        {
            Scope scope = CreateScope(10, 10);

            // Allow one frame for the scope to be placed onto a layer and for the nodes to compute their layouts
            yield return(null);

            // Allow one frame for the scope to compute its geometry (position)
            yield return(null);

            // Allow one frame for the scope to update its layout
            yield return(null);

            // Get the total margins around the content of the scope
            float leftMargin, topMargin, rightMargin, bottomMargin;

            GetScopeMargins(scope, out leftMargin, out topMargin, out rightMargin, out bottomMargin);

            Vector2 initialSize = scope.GetPosition().size;
            Rect    node1Geom   = m_Node1.GetPosition();
            Rect    node2Geom   = m_Node2.GetPosition();

            scope.AddElement(m_Node1);
            scope.AddElement(m_Node2);

            // Allow couple frames for the scope to compute its geometry (position)
            int iteration = 0;

            while (scope.hasPendingGeometryUpdate && iteration++ < k_MaxIterations)
            {
                yield return(null);
            }

            // Allow one frame for the scope to update its layout
            yield return(null);

            // Moves node1 and node2
            node1Geom.x -= 20;
            node1Geom.y -= 30;
            node2Geom.x += 40;
            node2Geom.y += 50;

            m_Node1.SetPosition(node1Geom);
            m_Node2.SetPosition(node2Geom);

            // Allow two frames for node1 and node2 to compute their layouts
            yield return(null);

            yield return(null);


            // Allow couple frames for the scope to compute its geometry (position)
            iteration = 0;

            while (scope.hasPendingGeometryUpdate && iteration++ < k_MaxIterations)
            {
                yield return(null);
            }

            // Allow one frame for the scope to update its layout
            yield return(null);

            // Computes the bounding rect of node1 and node2
            Rect contentBoundingRectInViewportSpace = GetBoundingRectInViewportSpace(scope.containedElements);
            Rect scopeGeometryInViewportSpace       = scope.ChangeCoordinatesTo(graphView.contentViewContainer, scope.GetRect());

            // Verify that the geometries of node1 and node2 have not changed while the geometry of the scope has been recomputed
            Assert.AreEqual(node1Geom, m_Node1.GetPosition());
            Assert.AreEqual(node2Geom, m_Node2.GetPosition());
            Assert.AreEqual(scopeGeometryInViewportSpace, RectUtils.Inflate(contentBoundingRectInViewportSpace, leftMargin, topMargin, rightMargin, bottomMargin));
        }