public IEnumerator DragRemoveSelectionFromStackNode()
        {
            // 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 and node2
            m_Node1.Select(graphView, false);
            m_Node2.Select(graphView, true);

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

            // Drag node1 and node2 away from the stack
            helpers.DragTo(start, outsideStackNode);

            Assert.AreEqual(1, stack.childCount);
            Assert.IsTrue(stack.Contains(m_Node3));
        }
        public void ValidateSupportedElements()
        {
            StackNode stack = CreateFilteredStackNode(k_DefaultX, k_DefaultY);

            stack.AddElement(CreateStackNode(10, 10));
            stack.AddElement(CreateScope(10, 10));
            stack.AddElement(CreateGroup("", 10, 10));
            stack.AddElement(new UnsupportedNode());
            // Verify that none of the above attempts succeeded
            Assert.AreEqual(0, stack.childCount);

            stack.AddElement(m_Node1);
            //Verify that a regular element is accepted
            Assert.AreEqual(1, stack.childCount);
        }
Esempio n. 3
0
        public override void SetUp()
        {
            base.SetUp();

            m_Node1     = CreateNode("Node 1", new Vector2(100, 100), 2, 2);
            m_Node2     = CreateNode("Node 2", new Vector2(200, 200), 2, 2);
            m_Node3     = CreateNode("Node 3", new Vector2(400, 400));
            m_Node4     = CreateNode("Node 4", new Vector2(500, 500));
            m_Group     = CreateGroup("Group", 500, 0);
            m_StackNode = CreateStackNode(300, 300);
            m_Edge1     = CreateEdge(m_Node2.outputContainer[0] as Port, m_Node1.inputContainer[0] as Port);
            m_Edge2     = CreateEdge(m_Node2.outputContainer[1] as Port, m_Node1.inputContainer[0] as Port);

            m_Group.AddElement(m_Node2);

            m_StackNode.AddElement(m_Node3);
            m_StackNode.AddElement(m_Node4);
        }
        public IEnumerator StackSelectionIsRestoredWhenEnteringPlaymode_AddNodesAfterPersistence()
        {
            var stack = new StackNode();

            graphView.AddElement(stack);

            stack.AddElement(node1);
            stack.AddElement(node2);
            stack.AddElement(node3);

            // Add two nodes to selection.
            graphView.AddToSelection(node1);
            graphView.AddToSelection(node3);

            Assert.True(node1.selected);
            Assert.False(node2.selected);
            Assert.True(node3.selected);

            // Allow 1 frame to let the persistent data get saved
            yield return(null);

            // This will re-create the window, flushing all temporary state
            yield return(new EnterPlayMode());

            // Allow 1 frame to let the persistence be restored
            yield return(null);

            node1 = CreateNodeWithoutAddingToGraphView(key1, new Vector2(200, 200), viewDataKey: key1);
            node2 = CreateNodeWithoutAddingToGraphView(key2, new Vector2(400, 400), viewDataKey: key2);
            node3 = CreateNodeWithoutAddingToGraphView(key3, new Vector2(600, 600), viewDataKey: key3);

            stack = new StackNode();

            stack.AddElement(node1);
            stack.AddElement(node2);
            stack.AddElement(node3);

            graphView.AddElement(stack);

            Assert.True(node1.selected);
            Assert.False(node2.selected);
            Assert.True(node3.selected);
        }
        public void StackNodeDoesNoContainRemovedElements()
        {
            // Create a stack
            StackNode stack = CreateStackNode(k_DefaultX, k_DefaultY);

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

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

            Assert.AreEqual(1, stack.childCount);
            Assert.IsFalse(stack.Contains(m_Node1));
            Assert.IsNull(m_Node1.parent);

            stack.RemoveElement(m_Node2);

            Assert.AreEqual(0, stack.childCount);
            Assert.IsFalse(stack.Contains(m_Node2));
            Assert.IsNull(m_Node2.parent);
        }
        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 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));
        }
        [UnityTest] // Case 1015984
        public IEnumerator DragRemoveAndThenAddSelectionBackToStackNode()
        {
            // Creates a stack and add node1
            StackNode stack = CreateStackNode(k_DefaultX, k_DefaultY);

            stack.AddElement(m_Node1);

            // 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.DragTo(start, outsideStackNode);

            Assert.AreEqual(0, stack.childCount);

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

            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 back to the stack
            Assert.AreEqual(1, stack.childCount);
            Assert.AreEqual(stack, m_Node1.parent);
        }
        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();
        }
Esempio n. 10
0
        public void FrameNextPrevWithNestedElementsTest()
        {
            StackNode stackNode;

            graphView.AddElement(new FooNode()
            {
                name = "N0"
            });
            graphView.AddElement(stackNode = new StackNode {
                name = "N1"
            });
            stackNode.AddElement(new FooNode()
            {
                name = "N2"
            });
            graphView.AddElement(new FooNode()
            {
                name = "N3"
            });

            graphView.ClearSelection();
            graphView.AddToSelection(graphView.graphElements.First());

            graphView.FrameNext();
            Assert.That(graphView.selection.Count, Is.EqualTo(1));
            Assert.That(graphView.selection[0], Is.AssignableTo(typeof(GraphElement)));
            Assert.That(((GraphElement)graphView.selection[0]).name, Is.EqualTo("N1"));

            graphView.FrameNext();
            Assert.That(graphView.selection.Count, Is.EqualTo(1));
            Assert.That(graphView.selection[0], Is.AssignableTo(typeof(GraphElement)));
            Assert.That(((GraphElement)graphView.selection[0]).name, Is.EqualTo("N2"));

            graphView.FrameNext();
            Assert.That(graphView.selection.Count, Is.EqualTo(1));
            Assert.That(graphView.selection[0], Is.AssignableTo(typeof(GraphElement)));
            Assert.That(((GraphElement)graphView.selection[0]).name, Is.EqualTo("N3"));

            graphView.FrameNext();
            Assert.That(graphView.selection.Count, Is.EqualTo(1));
            Assert.That(graphView.selection[0], Is.AssignableTo(typeof(GraphElement)));
            Assert.That(((GraphElement)graphView.selection[0]).name, Is.EqualTo("N0"));

            graphView.FramePrev();
            Assert.That(graphView.selection.Count, Is.EqualTo(1));
            Assert.That(graphView.selection[0], Is.AssignableTo(typeof(GraphElement)));
            Assert.That(((GraphElement)graphView.selection[0]).name, Is.EqualTo("N3"));

            graphView.FramePrev();
            Assert.That(graphView.selection.Count, Is.EqualTo(1));
            Assert.That(graphView.selection[0], Is.AssignableTo(typeof(GraphElement)));
            Assert.That(((GraphElement)graphView.selection[0]).name, Is.EqualTo("N2"));

            graphView.FramePrev();
            Assert.That(graphView.selection.Count, Is.EqualTo(1));
            Assert.That(graphView.selection[0], Is.AssignableTo(typeof(GraphElement)));
            Assert.That(((GraphElement)graphView.selection[0]).name, Is.EqualTo("N1"));

            graphView.FramePrev();
            Assert.That(graphView.selection.Count, Is.EqualTo(1));
            Assert.That(graphView.selection[0], Is.AssignableTo(typeof(GraphElement)));
            Assert.That(((GraphElement)graphView.selection[0]).name, Is.EqualTo("N0"));
        }
Esempio n. 11
0
        public void DrawNodes(bool entryPoint, NodeBase currentNode, int columnIndex, Port parentPort, StackNode stackNode, string[] styleClasses = null, List <FullNodeInfo> decoratorData = null)
        {
            int colIndex = columnIndex;

            FullNodeInfo fullDetails = new FullNodeInfo();

            fullDetails.RunTimeNode = currentNode;

            //Loses reference for some reason
            if (BehaviorTreeGraphWindow.SettingsData == null)
            {
                BehaviorTreeGraphWindow.SettingsData = new DataManager();
            }

            fullDetails.PropertyData = BehaviorTreeGraphWindow.SettingsData.GetNodeStyleDetails(currentNode);


            if (fullDetails.PropertyData != null && fullDetails.PropertyData.IsDecorator)
            {
                if (decoratorData == null)
                {
                    decoratorData = new List <FullNodeInfo>();
                }

                decoratorData.Add(fullDetails);

                if (currentNode.ChildNodes.Count == 0)
                {
                    $"Decorator ({currentNode.GetType().Name}) does not have any children. Nothing will be drawn.".BTDebugLog();
                }
                else
                {
                    DrawNodes(false, currentNode.ChildNodes[0], colIndex, parentPort, stackNode, null, decoratorData);
                }
            }
            else
            {
                BTGNodeData node = new BTGNodeData(fullDetails, entryPoint, parentPort, decoratorData);

                //Add general action image to title bar
                Image nodeIcon = CreateImage(fullDetails.PropertyData.Icon);
                node.titleContainer.Add(nodeIcon);
                nodeIcon.SendToBack();

                //Style the title label
                VisualElement titleLabel = node.Q <VisualElement>("title-label");
                titleLabel.style.color    = new StyleColor(m_White);
                titleLabel.style.flexGrow = 1;
                titleLabel.style.unityFontStyleAndWeight = new StyleEnum <FontStyle>(FontStyle.Bold);

                if (!entryPoint)
                {
                    node.AddPort(GeneratePort(node, Direction.Input, Port.Capacity.Multi), "Parent", true);
                    node.GenerateEdge();

                    if (stackNode != null)
                    {
                        stackNode.AddElement(node);
                        ((BTGStackNodeData)stackNode).childNodes.Add(node);
                    }
                    else
                    {
                        AddElement(node);
                    }

                    if (styleClasses != null)
                    {
                        foreach (string style in styleClasses)
                        {
                            node.AddToClassList(style);
                        }
                    }
                }
                else
                {
                    AddElement(node);
                }

                if (currentNode.ChildNodes.Count > 0)
                {
                    colIndex++;

                    BTGStackNodeData stack = m_StackNodes.FirstOrDefault(x => x.ColumnId == colIndex);

                    if (stack == null)
                    {
                        stack = new BTGStackNodeData()
                        {
                            ColumnId = colIndex,
                            style    =
                            {
                                width = 350
                            }
                        };

                        Vector2 pos = (Vector2.right * 300) * colIndex;
                        stack.SetPosition(new Rect(pos, c_NodeSize));

                        stack.RemoveFromClassList("stack-node");
                        AddElement(stack);
                    }

                    for (int i = 0; i < currentNode.ChildNodes.Count; i++)
                    {
                        node.AddPort(GeneratePort(node, Direction.Output, Port.Capacity.Multi), (i + 1).ToString(), false);

                        List <string> newStyles = new List <string>();

                        if (i == 0)
                        {
                            newStyles.Add("FirstNodeSpacing");
                        }
                        else if (i == currentNode.ChildNodes.Count - 1)
                        {
                            newStyles.Add("LastNodeSpacing");
                        }

                        DrawNodes(false, currentNode.ChildNodes[i], colIndex, node.OutputPorts[i], stack, newStyles.ToArray());
                    }
                }
            }
        }