private void RemoveConnections([NotNull] SerializedBehaviorTreeNode node)
        {
            for (int edgeIndex = 0, edgeCount = node.outputEdgeCount; edgeIndex < edgeCount; ++edgeIndex)
            {
                Edge edge = node.RemoveChild(edgeIndex);

                if (edge != null)
                {
                    ((SerializedBehaviorTreeNode)edge.input.node).DisconnectParent();
                    RemoveElement(edge);
                }
            }

            for (int nodeIndex = 0, nodeCount = m_nodes.Count; nodeIndex < nodeCount; ++nodeIndex)
            {
                SerializedBehaviorTreeNode otherNode = m_nodes[nodeIndex];

                for (int childIndex = 0, childrenCount = otherNode.outputEdgeCount;
                     childIndex < childrenCount;
                     ++childIndex)
                {
                    if (otherNode.GetChild(childIndex) == node)
                    {
                        Edge edge = otherNode.RemoveChild(childIndex);

                        if (edge != null)
                        {
                            RemoveElement(edge);
                        }
                    }
                }
            }
        }
        private SerializedProperty GetPropertyData([NotNull] SerializedBehaviorTreeNode node)
        {
            int index = GetPropertyDataIndex(node);

            return(index < 0 ? null : m_serializedBehaviorTreeObject.FindProperty(SerializedBehaviorDataPropertyName)
                   .GetArrayElementAtIndex(index));
        }
        public void CreateNewBehavior([NotNull] Type behaviorType, Vector2 position)
        {
            Undo.SetCurrentGroupName("Behavior Tree changed");
            int undoGroup = Undo.GetCurrentGroup();

            Undo.RegisterCompleteObjectUndo(m_serializedBehaviorTree, "Changed Behavior Tree");

            var behavior = (SerializedBehavior_Base)ScriptableObject.CreateInstance(behaviorType);

            behavior.name = behaviorType.Name;
            AssetDatabase.AddObjectToAsset(behavior, m_serializedBehaviorTree);
            var node = new SerializedBehaviorTreeNode(behavior, this);

            AddElement(node);
            m_nodes.Add(node);

            SerializedProperty serializedDatas = m_serializedBehaviorTreeObject
                                                 .FindProperty(SerializedBehaviorDataPropertyName);
            int index = serializedDatas.arraySize++;
            SerializedProperty serializedData = serializedDatas.GetArrayElementAtIndex(index);

            serializedData.FindPropertyRelative(SerializedBehaviorPropertyName).objectReferenceValue = behavior;
            position -= m_editorWindow.position.position;
            position  = contentViewContainer.WorldToLocal(position);
            serializedData.FindPropertyRelative(NodeGraphInfoPropertyName)
            .FindPropertyRelative(PositionPropertyName).vector2Value = position;
            node.SetPosition(new Rect(position, s_defaultSize));

            node.RefreshExpandedState();
            node.RefreshPorts();

            m_serializedBehaviorTreeObject.ApplyModifiedProperties();

            Undo.CollapseUndoOperations(undoGroup);
        }
        public void OnPortRemoved([NotNull] SerializedBehaviorTreeNode node, [CanBeNull] Edge edge, int index)
        {
            Undo.SetCurrentGroupName("Behavior Tree changed");
            int undoGroup = Undo.GetCurrentGroup();

            Undo.RegisterCompleteObjectUndo(m_serializedBehaviorTree, "Changed Behavior Tree");

            if (edge != null)
            {
                ((SerializedBehaviorTreeNode)edge.input.node).DisconnectParent();
                RemoveElement(edge);
            }

            SerializedProperty serializedBehaviorData = GetPropertyData(node);
            SerializedProperty children = serializedBehaviorData.FindPropertyRelative(ChildrenIndicesPropertyName);
            int childrenCount           = children.arraySize;

            do
            {
                children.DeleteArrayElementAtIndex(index);
            } while (children.arraySize == childrenCount);

            m_serializedBehaviorTreeObject.ApplyModifiedProperties();

            Undo.CollapseUndoOperations(undoGroup);
        }
        private void UpdateChildren([NotNull] SerializedProperty serializedBehaviorDataArray)
        {
            var removedEdges = new List <Edge>();

            for (int i = 0, count = serializedBehaviorDataArray.arraySize; i < count; ++i)
            {
                SerializedProperty serializedBehaviorData = serializedBehaviorDataArray.GetArrayElementAtIndex(i);
                var serializedBehavior = (SerializedBehavior_Base)serializedBehaviorData
                                         .FindPropertyRelative(SerializedBehaviorPropertyName).objectReferenceValue;
                SerializedBehaviorTreeNode node     = FindNode(serializedBehavior);
                SerializedProperty         children = serializedBehaviorData.FindPropertyRelative(ChildrenIndicesPropertyName);
                node.SetOutputCapacity(removedEdges, children.arraySize);

                for (int edgeIndex = 0, edgeCount = removedEdges.Count; edgeIndex < edgeCount; ++edgeIndex)
                {
                    Edge removedEdge = removedEdges[edgeIndex];
                    ((SerializedBehaviorTreeNode)removedEdge.input.node).DisconnectParent();
                    RemoveElement(removedEdge);
                }

                removedEdges.Clear();

                for (int childIndex = 0, childrenCount = children.arraySize; childIndex < childrenCount; ++childIndex)
                {
                    int childBehaviorIndex = children.GetArrayElementAtIndex(childIndex).intValue;

                    if (childBehaviorIndex < 0)
                    {
                        if (node.GetChild(childIndex) != null)
                        {
                            Edge edge = node.RemoveChild(childIndex);
                            ((SerializedBehaviorTreeNode)edge.input.node).DisconnectParent();
                            RemoveElement(edge);
                        }

                        continue;
                    }

                    var child = (SerializedBehavior_Base)serializedBehaviorDataArray
                                .GetArrayElementAtIndex(childBehaviorIndex)
                                .FindPropertyRelative(SerializedBehaviorPropertyName).objectReferenceValue;

                    if (node.GetChild(childIndex)?.dependedSerializedBehavior == child)
                    {
                        continue;
                    }

                    Edge previousEdge = node.RemoveChild(childIndex);

                    if (previousEdge != null)
                    {
                        ((SerializedBehaviorTreeNode)previousEdge.input.node).DisconnectParent();
                        RemoveElement(previousEdge);
                    }

                    AddElement(node.SetChild(FindNode(child), childIndex));
                }
            }
        }
        public Edge SetChild([NotNull] SerializedBehaviorTreeNode child, int index)
        {
            var  outputPort = (Port)outputContainer[index];
            var  inputPort  = (Port)child.inputContainer[0];
            Edge newEdge    = outputPort.ConnectTo(inputPort);

            m_outputEdges[index] = newEdge;

            return(newEdge);
        }
        private void RefreshNodes()
        {
            for (int i = 0, count = m_nodes.Count; i < count; ++i)
            {
                SerializedBehaviorTreeNode node = m_nodes[i];
                node.RefreshExpandedState();
                node.RefreshPorts();
            }

            m_rootNode.RefreshExpandedState();
            m_rootNode.RefreshPorts();
        }
 private void UpdatePositions([NotNull] SerializedProperty serializedBehaviorDataArray)
 {
     for (int i = 0, count = serializedBehaviorDataArray.arraySize; i < count; ++i)
     {
         SerializedProperty serializedBehaviorData = serializedBehaviorDataArray.GetArrayElementAtIndex(i);
         var serializedBehavior = (SerializedBehavior_Base)serializedBehaviorData
                                  .FindPropertyRelative(SerializedBehaviorPropertyName).objectReferenceValue;
         SerializedBehaviorTreeNode node = FindNode(serializedBehavior);
         Vector2 position = serializedBehaviorData.FindPropertyRelative(NodeGraphInfoPropertyName)
                            .FindPropertyRelative(PositionPropertyName).vector2Value;
         node.SetPosition(new Rect(position, s_defaultSize));
     }
 }
        private SerializedBehaviorTreeNode FindNode([NotNull] SerializedBehavior_Base serializedBehavior)
        {
            for (int i = 0, count = m_nodes.Count; i < count; ++i)
            {
                SerializedBehaviorTreeNode node = m_nodes[i];

                if (node.dependedSerializedBehavior == serializedBehavior)
                {
                    return(node);
                }
            }

            return(null);
        }
        public void OnPortAdded([NotNull] SerializedBehaviorTreeNode node)
        {
            Undo.SetCurrentGroupName("Behavior Tree changed");
            int undoGroup = Undo.GetCurrentGroup();

            Undo.RegisterCompleteObjectUndo(m_serializedBehaviorTree, "Changed Behavior Tree");

            SerializedProperty serializedBehaviorData = GetPropertyData(node);
            SerializedProperty childrenIndices        = serializedBehaviorData
                                                        .FindPropertyRelative(ChildrenIndicesPropertyName);
            int last = childrenIndices.arraySize++;

            childrenIndices.GetArrayElementAtIndex(last).intValue = -1;

            m_serializedBehaviorTreeObject.ApplyModifiedProperties();

            Undo.CollapseUndoOperations(undoGroup);
        }
        private void UpdateCreatedBehaviors([NotNull] SerializedProperty serializedBehaviorDataArray)
        {
            for (int i = 0, count = serializedBehaviorDataArray.arraySize; i < count; ++i)
            {
                SerializedProperty serializedBehaviorData = serializedBehaviorDataArray.GetArrayElementAtIndex(i);
                var serializedBehavior = (SerializedBehavior_Base)serializedBehaviorData
                                         .FindPropertyRelative(SerializedBehaviorPropertyName).objectReferenceValue;

                if (FindNode(serializedBehavior) != null)
                {
                    continue;
                }

                var newNode = new SerializedBehaviorTreeNode(serializedBehavior, this);
                AddElement(newNode);
                m_nodes.Add(newNode);
            }
        }
        private int GetPropertyDataIndex([NotNull] SerializedBehaviorTreeNode node)
        {
            SerializedProperty serializedData = m_serializedBehaviorTreeObject
                                                .FindProperty(SerializedBehaviorDataPropertyName);

            for (int i = 0, count = serializedData.arraySize; i < count; ++i)
            {
                SerializedProperty serializedBehaviorData = serializedData.GetArrayElementAtIndex(i);
                Object             serializedBehavior     = serializedBehaviorData.FindPropertyRelative(SerializedBehaviorPropertyName)
                                                            .objectReferenceValue;

                if (serializedBehavior == node.dependedSerializedBehavior)
                {
                    return(i);
                }
            }

            return(-1);
        }
        private void UpdateRoot()
        {
            int     rootNode = m_serializedBehaviorTreeObject.FindProperty(RootNodePropertyName).intValue;
            Vector2 position = m_serializedBehaviorTreeObject.FindProperty(RootGraphInfoPropertyName)
                               .FindPropertyRelative(PositionPropertyName).vector2Value;

            if (rootNode < 0)
            {
                Edge edge = m_rootNode.Disconnect();

                if (edge != null)
                {
                    ((SerializedBehaviorTreeNode)edge.input.node).DisconnectParent();
                    RemoveElement(edge);
                }
            }
            else
            {
                var rootBehavior = (SerializedBehavior_Base)m_serializedBehaviorTreeObject
                                   .FindProperty(SerializedBehaviorDataPropertyName)
                                   .GetArrayElementAtIndex(rootNode).FindPropertyRelative(SerializedBehaviorPropertyName)
                                   .objectReferenceValue;
                SerializedBehaviorTreeNode node = FindNode(rootBehavior);

                if (m_rootNode.GetChild() != node)
                {
                    Edge edge = m_rootNode.Disconnect();

                    if (edge != null)
                    {
                        ((SerializedBehaviorTreeNode)edge.input.node).DisconnectParent();
                        RemoveElement(edge);
                    }

                    AddElement(m_rootNode.Connect(node));
                }
            }

            m_rootNode.SetPosition(new Rect(position, s_defaultSize));
        }
        private void UpdateDeletedBehaviors([NotNull] SerializedProperty serializedBehaviorDataArray)
        {
            for (int i = m_nodes.Count - 1; i >= 0; --i)
            {
                SerializedBehaviorTreeNode node = m_nodes[i];

                bool exists = false;

                for (int j = 0, count = serializedBehaviorDataArray.arraySize; j < count & !exists; ++j)
                {
                    exists = serializedBehaviorDataArray.GetArrayElementAtIndex(j)
                             .FindPropertyRelative(SerializedBehaviorPropertyName).objectReferenceValue ==
                             node.dependedSerializedBehavior;
                }

                if (!exists)
                {
                    RemoveConnections(node);
                    RemoveElement(node);
                    m_nodes.RemoveAt(i);
                }
            }
        }
Exemplo n.º 15
0
 public Edge Connect(SerializedBehaviorTreeNode child)
 {
     m_outputEdge = ((Port)outputContainer[0]).ConnectTo((Port)child.inputContainer[0]);
     return(m_outputEdge);
 }