public void AddElements(IList <T> elements, BehaviorTreeNode parent) { if (elements == null) { throw new ArgumentNullException("elements", "elements is null"); } if (elements.Count == 0) { throw new ArgumentNullException("elements", "elements Count is 0: nothing to add"); } if (parent == null) { throw new ArgumentNullException("parent", "parent is null"); } RecordUndo("Add nodes"); if (parent.Children == null) { parent.Children = new List <BehaviorTreeNode>(); } parent.Children.AddRange(elements.Cast <BehaviorTreeNode>()); foreach (var element in elements) { element.Parent = parent; element.Depth = parent.Depth + 1; BehaviorTreeNodeUtility.UpdateDepthValues(element); } BehaviorTreeNodeUtility.TreeToList(Root, _data); Changed(); }
public void AddElement(T element, BehaviorTreeNode parent) { if (element == null) { throw new ArgumentNullException("element", "element is null"); } if (parent == null) { throw new ArgumentNullException("parent", "parent is null"); } RecordUndo("Add node"); if (parent.Children == null) { parent.Children = new List <BehaviorTreeNode>(); } parent.Children.Add(element); element.Parent = parent; BehaviorTreeNodeUtility.UpdateDepthValues(parent); BehaviorTreeNodeUtility.TreeToList(Root, _data); Changed(); }
// For updating depth values below any given element e.g after reparenting elements public static void UpdateDepthValues <T>(T root) where T : BehaviorTreeNode { if (root == null) { throw new ArgumentNullException("root", "The root is null"); } if (!root.HasChildren) { return; } Stack <BehaviorTreeNode> stack = new Stack <BehaviorTreeNode>(); stack.Push(root); while (stack.Count > 0) { BehaviorTreeNode current = stack.Pop(); if (current.Children != null) { foreach (var child in current.Children) { child.Depth = current.Depth + 1; stack.Push(child); } } } }
public void OnAfterDeserialize() { //Debug.Log("Deserializing Behavior Tree."); if (SerializedNodeList.Count > 0) { DeserializeNodeData(0, out Root); } else { Root = CreateRoot(); } }
public void OnBeforeSerialize() { //Debug.Log("Serializing Behavior Tree."); if (SerializedNodeList == null) { SerializedNodeList = new List <SerializableNodeData>(); } if (Root == null) { Root = CreateRoot(); } SerializedNodeList.Clear(); SerializeNodeData(Root); }
public IList <int> GetAncestors(int id) { var parents = new List <int>(); BehaviorTreeNode T = Find(id); if (T != null) { while (T.Parent != null) { parents.Add(T.Parent.ID); T = T.Parent; } } return(parents); }
public void AddNode(BehaviorTreeNodeItem selectedItem) { BehaviorTreeViewWindow btWindow = BehaviorTreeViewWindow.GetWindow(); if (btWindow.BehaviorTreeAsset == null) { Debug.LogError("Open a behavior tree asset before trying to add a node."); return; } BehaviorTreeModel <BehaviorTreeNode> btModel = btWindow.TreeView.TreeModel; var selectedIDsInBT = btWindow.TreeView.GetSelection(); // Case: No node is selected if (selectedIDsInBT.Count == 0) { BehaviorTreeNode nodeToAdd = (BehaviorTreeNode)Activator.CreateInstance( selectedItem.Data, selectedItem.displayName, 0, btModel.GenerateUniqueID()); //Debug.Log("Adding node: " + nodeToAdd.ToString() + " to Root"); btModel.AddElement(nodeToAdd); return; } for (int i = 0; i < selectedIDsInBT.Count; ++i) { //Debug.Log(selectedIDsInBT[i]); BehaviorTreeNode possibleParent = btModel.Find(selectedIDsInBT[i]); if (possibleParent != null) { BehaviorTreeNode nodeToAdd = (BehaviorTreeNode)Activator.CreateInstance( selectedItem.Data, selectedItem.displayName, 0, btModel.GenerateUniqueID()); //Debug.Log("Adding node: " + nodeToAdd.ToString() + " to: " + possibleParent.ToString()); btModel.AddElement(nodeToAdd, possibleParent); } // Just in case the ID wasn't found else { BehaviorTreeNode nodeToAdd = (BehaviorTreeNode)Activator.CreateInstance( selectedItem.Data, selectedItem.displayName, 0, btModel.GenerateUniqueID()); //Debug.Log("Adding node: " + nodeToAdd.ToString() + " to Root"); btModel.AddElement(nodeToAdd); return; } } }
int DeserializeNodeData(int index, out BehaviorTreeNode node) { var serializedNode = SerializedNodeList[index]; //Debug.Log("Deserializing: " + serializedNode.ToString()); Type nodeType = Type.GetType(serializedNode.TypeAsString); BehaviorTreeNode newNode = (BehaviorTreeNode)Activator.CreateInstance(nodeType, serializedNode.Name, serializedNode.Depth, serializedNode.ID); for (int i = 0; i < serializedNode.ChildCount; ++i) { BehaviorTreeNode childNode; index = DeserializeNodeData(++index, out childNode); newNode.Children.Add(childNode); } node = newNode; return(index); }
public void MoveElements(BehaviorTreeNode parentElement, int insertionIndex, List <BehaviorTreeNode> 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; } RecordUndo("Move nodes"); // 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 <BehaviorTreeNode>(); } // Insert dragged items under new parent parentElement.Children.InsertRange(insertionIndex, elements); BehaviorTreeNodeUtility.UpdateDepthValues(Root); BehaviorTreeNodeUtility.TreeToList(Root, _data); Changed(); }
void SerializeNodeData(BehaviorTreeNode node) { //Debug.Log("Serializing: " + node.ToString()); if (node.Children == null) { node.Children = new List <BehaviorTreeNode>(); } var serializedNodeData = new SerializableNodeData() { TypeAsString = node.GetType().ToString(), Name = node.Name, ID = node.ID, Depth = node.Depth, ChildCount = node.Children.Count }; SerializedNodeList.Add(serializedNodeData); for (int i = 0; i < node.Children.Count; ++i) { SerializeNodeData(node.Children[i]); } }
void CellGUI(Rect cellRect, TreeViewItem <BehaviorTreeNode> item, ViewColumns column, ref RowGUIArgs args) { CenterRectUsingSingleLineHeight(ref cellRect); BehaviorTreeNode node = item.data; switch (column) { case ViewColumns.kIcon: { GUI.DrawTexture(cellRect, (Texture2D)EditorGUIUtility.Load(node.GetIconPath()), ScaleMode.ScaleToFit); } break; case ViewColumns.kName: { Rect toggleRect = cellRect; toggleRect.x += GetContentIndent(item); toggleRect.width = TOGGLE_WIDTH; if (toggleRect.xMax < cellRect.xMax) { item.data.DEBUG_on = EditorGUI.Toggle(toggleRect, item.data.DEBUG_on); } args.rowRect = cellRect; args.label = item.data.Name; base.RowGUI(args); } break; case ViewColumns.kType: { cellRect.x += GetContentIndent(item); DefaultGUI.Label(cellRect, node.GetType().ToString().Split('.')[1], args.selected, args.focused); } break; } }
IList <int> GetParentsBelowStackBased(BehaviorTreeNode searchFromThis) { Stack <BehaviorTreeNode> stack = new Stack <BehaviorTreeNode>(); stack.Push(searchFromThis); var parentsBelow = new List <int>(); while (stack.Count > 0) { BehaviorTreeNode current = stack.Pop(); if (current.HasChildren) { parentsBelow.Add(current.ID); foreach (var T in current.Children) { stack.Push(T); } } } return(parentsBelow); }
public void AddNode(BehaviorTreeNode node) { Children.Add(node); }