public BTGNodeData(FullNodeInfo mainNodeDetails, bool entryPoint, GraphView.Port parentPort, List <FullNodeInfo> decoratorData)
        {
            MainNodeDetails = mainNodeDetails;
            DecoratorData   = decoratorData;
            MainNodeDetails.RunTimeNode.NodeStatusChanged += OnNodeStatusChanged;

            title = MainNodeDetails.RunTimeNode.Name == null || MainNodeDetails.RunTimeNode.Name.Equals("") ? MainNodeDetails.RunTimeNode.GetType().Name : MainNodeDetails.RunTimeNode.Name;

            Id         = Guid.NewGuid().ToString();
            EntryPoint = entryPoint;
            ParentPort = parentPort;

            m_StatusIcon = new Image()
            {
                style =
                {
                    width       = 25,
                    height      = 25,
                    marginRight =  5,
                    marginTop   = 5
                }
            };

            m_StatusIcon.tintColor = m_White;
            titleContainer.Add(m_StatusIcon);

            m_NodeBorder         = this.Q <VisualElement>("node-border");
            m_NodeTitleContainer = this.Q <VisualElement>("title");

            m_NodeTitleContainer.style.backgroundColor = new StyleColor(MainNodeDetails.PropertyData.TitleBarColor.WithAlpha(BehaviorTreeGraphWindow.SettingsData.GetDimLevel()));

            m_NodeTopMessageGeneral      = GenerateStatusMessageLabel("generalStatusMessage", DisplayStyle.None);
            m_NodeTopMessageDecorator    = GenerateStatusMessageLabel("decoratorReason", DisplayStyle.None);
            m_NodeLastEvaluatedTimeStamp = GenerateStatusMessageLabel("lastEvalTimeStamp", DisplayStyle.None);

            //Add the decorator icon
            if (DecoratorData != null)
            {
                foreach (var decorator in DecoratorData)
                {
                    decorator.RunTimeNode.NodeStatusChanged += OnNodeStatusChanged;

                    Image decoratorImage = CreateDecoratorImage(decorator.PropertyData.Icon.texture);

                    m_NodeTitleContainer.Add(decoratorImage);
                    decoratorImage.SendToBack();
                }
            }

            this.Q <VisualElement>("contents").Add(m_NodeTopMessageGeneral);
            this.Q <VisualElement>("contents").Add(m_NodeTopMessageDecorator);
            this.Q <VisualElement>("contents").Add(m_NodeLastEvaluatedTimeStamp);
            m_NodeLastEvaluatedTimeStamp.SendToBack();
            m_NodeTopMessageGeneral.SendToBack();
            m_NodeTopMessageDecorator.SendToBack();

            //Do an initial call to setup the style of the node in the event that it's already been running (pretty likely)
            OnNodeStatusChanged(MainNodeDetails.RunTimeNode);

            if (DecoratorData != null)
            {
                DecoratorData.ForEach(x => OnNodeStatusChanged(x.RunTimeNode));
            }
        }
Esempio n. 2
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());
                    }
                }
            }
        }