예제 #1
0
        public void RemoveElements(IList <T> elements)
        {
            foreach (var element in elements)
            {
                if (element == m_Root)
                {
                    throw new ArgumentException("It is not allowed to remove the root element");
                }
            }

            var commonAncestors = TreeElementUtility.FindCommonAncestorsWithinList(elements);

            foreach (var element in commonAncestors)
            {
                element.parent.children.Remove(element);
                element.parent = null;
            }

            TreeElementUtility.TreeToList(m_Root, m_Data);

            Changed();
        }
예제 #2
0
    public void MoveElements(TreeElement parentElement, int insertionIndex, List <TreeElement> elements)
    {
        if (insertionIndex < 0)
        {
            throw new ArgumentException("Invalid input: insertionIndex is -1, client needs to decide what index elements should be reparented at");
        }

        // Invalid reparenting input
        if (parentElement == null)
        {
            return;
        }

        // We are moving items so we adjust the insertion index to accomodate that any items above the insertion index is removed before inserting
        if (insertionIndex > 0)
        {
            insertionIndex -= parentElement.children.GetRange(0, insertionIndex).Count(elements.Contains);
        }

        // Remove draggedItems from their parents
        foreach (var draggedItem in elements)
        {
            draggedItem.parent.children.Remove(draggedItem);    // remove from old parent
            draggedItem.parent = parentElement;                 // set new parent
        }

        if (parentElement.children == null)
        {
            parentElement.children = new List <TreeElement>();
        }

        // Insert dragged items under new parent
        parentElement.children.InsertRange(insertionIndex, elements);

        TreeElementUtility.UpdateDepthValues(root);
        TreeElementUtility.TreeToList(m_Root, m_Data);

        Changed();
    }
예제 #3
0
    public static void TestListToTreeThrowsExceptionIfRootIsInvalidDepth()
    {
        // Arrange
        var list = new List <TestElement>();

        list.Add(new TestElement("root", 0));
        list.Add(new TestElement("A", 1));
        list.Add(new TestElement("B", 1));
        list.Add(new TestElement("Bchild", 2));

        // Test
        bool catchedException = false;

        try {
            TreeElementUtility.ListToTree(list);
        }
        catch (Exception) {
            catchedException = true;
        }

        // Assert
        Assert.IsTrue(catchedException, "We require the root.depth to be -1, here it is: " + list[0].depth);
    }
예제 #4
0
    public static void TestTreeModelCanRemoveElements()
    {
        var root = new TreeElement {
            name = "Root", depth = -1
        };
        var listOfElements = new List <TreeElement>();

        listOfElements.Add(root);

        var model = new TreeModel <TreeElement>(listOfElements);

        model.AddElement(new TreeElement {
            name = "Element"
        }, root, 0);
        model.AddElement(new TreeElement {
            name = "Element " + root.children.Count
        }, root, 0);
        model.AddElement(new TreeElement {
            name = "Element " + root.children.Count
        }, root, 0);
        model.AddElement(new TreeElement {
            name = "Sub Element"
        }, root.children[1], 0);

        model.RemoveElements(new[] { root.children[1].children[0], root.children[1] });

        // Assert order is correct
        string[] namesInCorrectOrder = { "Root", "Element 2", "Element" };
        Assert.AreEqual(namesInCorrectOrder.Length, listOfElements.Count, "Result count does not match");
        for (int i = 0; i < namesInCorrectOrder.Length; ++i)
        {
            Assert.AreEqual(namesInCorrectOrder[i], listOfElements[i].name);
        }

        // Assert depths are valid
        TreeElementUtility.ValidateDepthValues(listOfElements);
    }
예제 #5
0
        public static ParallelRunner LoadFromJSON(this BehaviorTreeManagerAsset asset, BehaviorManager manager = null)
        {
            //TODO: Reload button
            //TODO: Confirm reload from json
            if (asset == null)
            {
                Debug.Log("Asset is null when loading");
                return(new ParallelRunner("Empty Root", -1, -1));
            }
            else
            {
                //Elements should be a list of dynamic objects
                var elements = JsonConvert.DeserializeObject <List <dynamic> >(asset.RunnerElementsJSON);

                var newElements = new List <BehaviorTreeElement>();
                foreach (dynamic el in elements)
                {
                    string  typeName    = el.ElementType;
                    Type    type        = Assembly.GetAssembly(typeof(BehaviorTreeElement)).GetType(typeName);
                    dynamic newBehavior = Activator.CreateInstance(type, (string)el.Name, (int)el.Depth, (int)el.ID);

                    JsonConvert.PopulateObject(JsonConvert.SerializeObject(el), newBehavior);
                    newElements.Add(newBehavior);
                }
                var str = "";
                foreach (var e in newElements)
                {
                    e.BehaviorTreeManager = manager;
                    str += e.Name + "\n";
                }

                var tree = TreeElementUtility.ListToTree(newElements);

                return((ParallelRunner)tree);
            }
        }
예제 #6
0
    public void AddElement(T element, TreeElement parent, int insertPosition)
    {
        if (element == null)
        {
            throw new ArgumentNullException("element", "element is null");
        }
        if (parent == null)
        {
            throw new ArgumentNullException("parent", "parent is null");
        }

        if (parent.children == null)
        {
            parent.children = new List <TreeElement> ();
        }

        parent.children.Insert(insertPosition, element);
        element.parent = parent;

        TreeElementUtility.UpdateDepthValues(parent);
        TreeElementUtility.TreeToList(m_Root, m_Data);

        Changed();
    }