public override void BuildContextualMenu(ContextualMenuPopulateEvent evt) { //TODO: This will need to be a toggle once creating behavior trees is supported // It's not needed for viewnig and can cause negative behavior //base.BuildContextualMenu(evt); if (evt.target is BTGNodeData) { BTGNodeData nodeData = (evt.target as BTGNodeData); int countOfItems = 1; if (nodeData.DecoratorData != null) { for (int i = 0; i < nodeData.DecoratorData.Count; i++) { var name = nodeData.DecoratorData[0].RunTimeNode.GetType().Name; evt.menu.InsertAction(0, $"Open {name}", (e) => { OpenFile($"{name}.cs"); }); countOfItems++; } } string nodeName = nodeData.MainNodeDetails.RunTimeNode.GetType().Name; evt.menu.InsertAction(0, $"Open {nodeName}", (e) => { OpenFile($"{nodeName}.cs"); }); evt.menu.InsertSeparator("", countOfItems); } }
/// <summary> /// Add a new port to an existing graph node /// </summary> /// <param name="targetNode">Node to add the port to</param> /// <param name="portDirection">Whether the port is an input or output</param> /// <param name="capacity">How many connections this port supports</param> /// <returns></returns> private Port GeneratePort(BTGNodeData targetNode, Direction portDirection, Port.Capacity capacity = Port.Capacity.Single) { return(targetNode.InstantiatePort(Orientation.Horizontal, portDirection, capacity, typeof(bool))); }
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()); } } } }