//
        // Methods
        //
        public override void OnGUI(SerializedNodeProperty property, ActionNode node, GUIContent guiContent)
        {
            UnityVariablePropertyAttribute att = (UnityVariablePropertyAttribute)attribute;

                        BlackboardCustom blackboard = node.blackboard as BlackboardCustom;

                        List<UnityVariable> blackboardLocalList = blackboard.GetVariableBy (att.variableType);

                        List<GUIContent> displayOptionsList = blackboardLocalList.Select ((item) => new GUIContent ("Local/" + item.name)).ToList ();

                        EditorGUILayout.BeginHorizontal ();

                        //	property.serializedNode.ApplyModifiedProperties ();

                        EditorGUI.BeginChangeCheck ();

                        //EditorGUILayout.LabelField (att.name,new GUILayoutOption[]{GUILayout.MaxWidth(80)});
                        property.value = EditorGUILayoutEx.UnityVariablePopup (new GUIContent(att.name), property.value as UnityVariable, att.variableType, displayOptionsList, blackboardLocalList);

                        if (EditorGUI.EndChangeCheck ()) {
                                property.serializedNode.ApplyModifiedProperties ();
                        }

                        EditorGUILayout.EndHorizontal ();
        }
        /// <summary> 
        /// Paste the supplied nodes in the supplied ActionState.
        /// <param name="actionState">The ActionState to paste the node.</param>
        /// <param name="nodes">The nodes to be pasted.</param>
        /// <returns>The pasted nodes.</returns>
        /// </summary>
        public static ActionNode[] PasteNodes (InternalActionState actionState, ActionNode[] nodes) {
            var newNodes = new List<ActionNode>();

            // Validate parameters
            if (nodes != null && actionState != null) {
                // Register Undo
                #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                Undo.RegisterUndo(actionState,"Paste Node");
                #else
                Undo.RecordObject(actionState,"Paste Node");
                #endif

                // Copy nodes
                for (int i = 0; i < nodes.Length; i++) {
                    if (nodes[i] != null && !(nodes[i] is BranchNode)) {
                        ActionNode newNode = nodes[i].Copy(actionState);
                        if (newNode != null)
                            newNodes.Add(newNode);
                    }
                }

                if (newNodes.Count > 0) {
                    // Saves node and sets dirty flag
                    StateUtility.SetDirty(actionState);

                    // Reload actionState to update variables
                    actionState.LoadNodes();
                }
            }
            return newNodes.ToArray();
        }
Beispiel #3
0
		/// <summary> 
        /// Adds a child node to the branch.
        /// <param name = "child">The node to be added to the branch.</param>
        /// <returns>True if the node was added to the list; otherwise false.</returns>
        /// </summary>
        public override bool Add (ActionNode child) {
            if (CanAddNode(child)) {
                m_Child = child;
                m_Child.branch = this;
                return true;
            }
            return false;
        }
Beispiel #4
0
 /// <summary> 
 /// Set the children in the branch.
 /// Used during the node's serialization.
 /// <param name="newChildren">The new child nodes.</param>
 /// <returns>True if the new children were successfully added; false otherwise.</returns>
 /// <seealso cref="BehaviourMachine.NodeSerialization" />
 /// </summary>
 public override bool SetChildren (ActionNode[] newChildren) {
     if (newChildren.Length == 1 && CanAddNode(newChildren[0])) {
         m_Child = newChildren[0];
         m_Child.branch = this;
         return true;
     }
     else if (newChildren.Length <= 0) {
         if (m_Child != null)
             m_Child.branch = null;
         m_Child = null;
         return true;
     }
     return false;
 }
Beispiel #5
0
        /// <summary> 
        /// Set the children in the branch.
        /// Used during the node's serialization.
        /// <param name="newChildren">The new child nodes.</param>
        /// <returns>True if the new children was successfully added; false otherwise.</returns>
        /// <seealso cref="BehaviourMachine.NodeSerialization" />
        /// </summary>
        public override bool SetChildren (ActionNode[] newChildren) {
            // Validate children
            for (int i = 0; i < newChildren.Length; i++) {
                if (!CanAddNode(newChildren[i]))
                    return false;
            }

            // Set branch
            for (int i = 0; i < newChildren.Length; i++)
                newChildren[i].branch = this;

            m_Children = newChildren;
            return true;
        }
 /// <summary>
 /// A callback invoked whenever a node change its status.
 /// <param name="node">The target node.</param>
 /// </summary>
 private void OnNodeTick (ActionNode node) {
     // The node belongs to the active tree?
     if (node.owner as InternalActionState == m_ActionState && node.status != Status.Ready) {
         // The node status is already been tracked?
         if (m_NodeStatusTime.ContainsKey(node.instanceID)) {
             // Update time
             m_NodeStatusTime[node.instanceID].Update(node.status);
         }
         else {
             // Create a new entry
             m_NodeStatusTime.Add(node.instanceID, new NodeStatusTime(node.status));
         }
     }
 }
Beispiel #7
0
 /// <summary>
 /// Draw the slider.
 /// </summary>
 public override void OnGUI (SerializedNodeProperty property, ActionNode node, GUIContent guiContent) {
     // Integer
     if (property.propertyType == NodePropertyType.Integer) {
         var range = base.attribute as RangeAttribute;
         property.value = EditorGUILayout.IntSlider (guiContent, (int)property.value, (int)range.min, (int)range.max);
     }
     // Float
     else if (property.propertyType == NodePropertyType.Float) {
         var range = base.attribute as RangeAttribute;
         property.value = EditorGUILayout.Slider (guiContent, (float)property.value, range.min, range.max);
     }
     else
         EditorGUILayout.LabelField(guiContent, new GUIContent("Use range with float or int."));
 }
Beispiel #8
0
        /// <summary> 
        /// Adds the node to the branch nodes list.
        /// <param name = "child">The node to be added to the list.</param>
        /// <returns>True if the node was added to the list; otherwise false.</returns>
        /// </summary>
        public override bool Add (ActionNode child) {
            if (CanAddNode(child) && !Contains(child)) {
                // Remove from old branch
                if (child.branch != null)
                    child.branch.Remove(child);

                // Add the child to this branch
                var newChildren = new List<ActionNode>(m_Children);
                newChildren.Add(child);
                m_Children = newChildren.ToArray();
                child.branch = this;

                return true;
            }
            return false;
        }
Beispiel #9
0
        /// <summary> 
        /// Inserts a node in the nodes list at the supplied index.
        /// <param name = "index">The index to insert the behaviour.</param>
        /// <param name = "child">The node to be added to the list.</param>
        /// <returns>True if the node was added to the list; otherwise false.</returns>
        /// </summary>
        public override bool Insert (int index, ActionNode child) {
            if (child != null && index >= 0 && index <= m_Children.Length) {
                if (!Contains(child) && !Add(child))
                    return false;

                // Insert child
                var newChildren = new List<ActionNode>(m_Children);
                newChildren.Remove(child);
                newChildren.Insert(index, child);
                m_Children = newChildren.ToArray();

                return true;
            }

            return false;
        }
        //
        // Methods
        //
        public override void OnGUI(SerializedNodeProperty property, ActionNode node, GUIContent guiContent)
        {
            Enum @enum;

                    if (Enum.IsDefined (this.attribute.GetEnumType (), property.value))
                    {
                        @enum = (Enum)Enum.ToObject (this.attribute.GetEnumType (), property.value);
                    }
                    else
                    {
                        @enum = this.attribute.GetEnumValue ();
                    }

                    @enum=EditorGUILayout.EnumPopup (@enum);

                    property.value= ((int)Convert.ChangeType (@enum, @enum.GetTypeCode ()));
                    property.ApplyModifiedValue ();
        }
Beispiel #11
0
        /// <summary>
        /// Draw the UnityEngine.Object using the objectType field.
        /// </summary>
        public override void OnGUI (SerializedNodeProperty property, ActionNode node, GUIContent guiContent) {
            // Object
            if (property.propertyType == NodePropertyType.UnityObject) {
                
                // Get the type path
                var attr = this.attribute as ObjectValueAttribute;
                string path = property.path;
                int lastDotIndex = path.LastIndexOf('.');

                if (lastDotIndex > -1)
                    path = path.Remove(lastDotIndex + 1, path.Length - lastDotIndex - 1);
                else
                    path = string.Empty;

                // Get the property iterator
                NodePropertyIterator iterator = property.serializedNode.GetIterator();

                // The type of the object
                string objectTypeAsString = string.Empty;

                // Get the object type as string
                if (iterator.Find(path + attr.typePropertyPath)) {
                    SerializedNodeProperty typeProperty = iterator.current;
                    objectTypeAsString = typeProperty.value as string ?? string.Empty;
                }

                // Get the object type
                System.Type objectType = !string.IsNullOrEmpty(objectTypeAsString) ? BehaviourMachine.TypeUtility.GetType(objectTypeAsString) ?? typeof(UnityEngine.Object) : typeof(UnityEngine.Object);

                // Used to detect if the gui control was changed
                EditorGUI.BeginChangeCheck();

                // Draw the object gui control
                UnityEngine.Object newValue = EditorGUILayout.ObjectField(guiContent, property.value as UnityEngine.Object, objectType, !AssetDatabase.Contains(node.self));

                // Value changed?
                if (EditorGUI.EndChangeCheck())
                    property.value = newValue;
            }
            // Not Object
            else
                EditorGUILayout.LabelField(guiContent, new GUIContent("Use ObjectValue with UnityEngine.Object."));
        }
Beispiel #12
0
        /// <summary>
        /// Draw the state pop-up.
        /// </summary>
        public override void OnGUI (SerializedNodeProperty property, ActionNode node, GUIContent guiContent) {
            // InternalStateBehaviour
            if (property.propertyType == NodePropertyType.UnityObject && property.type == typeof(InternalStateBehaviour)) {
                var rect = GUILayoutUtility.GetRect(GUIContent.none, EditorStyles.popup);
                var id = GUIUtility.GetControlID(FocusType.Passive);
                var popupRect = EditorGUI.PrefixLabel(rect, id, guiContent);
                var state = property.value as InternalStateBehaviour;

                // Set the pop-up color
                var oldGUIColor = GUI.color;
                if (state == null)
                    GUI.color = Color.red;

                if (GUI.Button(popupRect, state != null ? state.stateName : "Null", EditorStyles.popup)) {
                    // Get states
                    var states = node.tree.states;

                    // Create the pop-up menu
                    var menu = new GenericMenu();

                    // Add null
                    menu.AddItem(new GUIContent("Null"), state == null, delegate () {property.value = (InternalStateBehaviour)null;});

                    // Add states
                    for (int i = 0; i < states.Count; i++) {
                        InternalStateBehaviour currentState = states[i];
                        //setField = field != null ? new SetField(this.target, target, states[i], field, m_Tree) : new SetField(this.target, arrayField, index, states[i], m_Tree);
                        menu.AddItem(new GUIContent(states[i].stateName), state == states[i], delegate () {property.value = currentState;});
                    }

                    // Show menu
                    menu.ShowAsContext();
                }

                // Restore GUI.color
                GUI.color = oldGUIColor;
            }
            else
                EditorGUILayout.LabelField(guiContent, new GUIContent("Use TreeState with InternalStateBehaviour."));
        }
Beispiel #13
0
        /// <summary>
        /// Removes node from tree.
        /// <param name="node">The node to be removed.</param>
        /// <param name="includeHierarchy">If true, the hierarchy will also be removed.</param>
        /// </summary>
        public void RemoveNode(ActionNode node, bool includeHierarchy)
        {
            GetNodes();
            var        nodes  = new List <ActionNode>(m_Nodes);
            BranchNode branch = node as BranchNode;

            // Remove children
            if (includeHierarchy && branch != null)
            {
                {
                    var __array3       = branch.GetHierarchy();
                    var __arrayLength3 = __array3.Length;
                    for (int __i3 = 0; __i3 < __arrayLength3; ++__i3)
                    {
                        var n = (ActionNode)__array3[__i3];
                        {
                            nodes.Remove(n);
                        }
                    }
                }
            }

            // Remove node
            nodes.Remove(node);
            m_Nodes = nodes.ToArray();

            if (Application.isPlaying && this.enabled)
            {
                // Update function nodes
                m_FunctionNodes = this.GetFunctionNodes();
                // Reset status
                node.ResetStatus();
                // Disable node
                node.OnDisable();
            }

            HierarchyChanged();
        }
        /// <summary> 
        /// Destroys the suplied node and its hierarchy from the tree.
        /// <param name="node">The node to be destroyed.</param>
        /// <returns>True if the node was successfully destroyed; false otherwise.</returns>
        /// </summary>
        public static bool DestroyNode (ActionNode node) {
            // Get the tree action state
            var actionState = node != null ? node.owner as InternalActionState : null;

            // Validate parameters
            if (actionState != null) {                
                // Register Undo
                #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                Undo.RegisterUndo(actionState, "Delete " + node.name);
                #else
                Undo.RecordObject(actionState, "Delete " + node.name);
                #endif

                // Removes node from actionState
                actionState.RemoveNode(node);

                // Saves actionState and marks dirty flag
                StateUtility.SetDirty(actionState);

                return true;
            }
            return false;
        }
        //
        // Methods
        //
        public override void OnGUI(SerializedNodeProperty property, ActionNode node, GUIContent guiContent)
        {
            attribute.serializedObject = node;

                        GUILayout.BeginHorizontal ();

                        if (attribute.Min < attribute.Max) {
                                if (attribute.HasEnableToggle)
                                        attribute.Enabled = GUILayout.Toggle (attribute.Enabled, property.label);

                                if (attribute.Enabled) {

                                        GUILayout.Label (attribute.Min.ToString ());

                                        _valueCurrent = GUILayout.HorizontalSlider (_valueCurrent, attribute.Min, attribute.Max);
                                        property.value = _valueCurrent;
                                        GUILayout.Label (attribute.Max.ToString ());

                                        property.ApplyModifiedValue ();
                                }
                        }

                        GUILayout.EndHorizontal ();
        }
        /// <summary>
        /// Draw the TextArea.
        /// </summary>
        public override void OnGUI (SerializedNodeProperty property, ActionNode node, GUIContent guiContent) {
            // The text to display
            string text;

            // string
            if (property.propertyType == NodePropertyType.String) {
                // Get the text
                text = property.value as string ?? string.Empty;
            }
            // StringVar
            else if (property.propertyType == NodePropertyType.Variable && property.type == typeof(StringVar)) {
                // Get the text
                var stringVar = property.value as StringVar;
                if (stringVar != null)
                    text = stringVar.Value as string ?? string.Empty;
                else
                    text = string.Empty;
            }
            // Not supported
            else {
                EditorGUILayout.LabelField(guiContent, new GUIContent("Use TextAreaAttribute with string or StringVar."));
                return;
            }

            // Create style?
            if (s_Styles == null)
                s_Styles = new NodeTextAreaDrawer.Styles();

            // Get the text area
            var textAreaAttr = (NodeTextAreaAttribute) attribute;

            // Store the current content color
            Color contentColor = GUI.contentColor;

            // The text is empty?
            if (string.IsNullOrEmpty(text)) {
                text = textAreaAttr.hint;
                Color textColor = EditorStyles.label.normal.textColor;
                textColor.a = .5f;
                GUI.contentColor = textColor;
            }

            // Draw the text area.
            EditorGUI.BeginChangeCheck ();


            #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
            string value = EditorGUILayout.TextArea (text, s_Styles.textArea, GUILayout.Height(16f * textAreaAttr.lines));
            #else
            string value = EditorGUILayout.TextArea (text, s_Styles.textArea, GUILayout.Height(EditorGUIUtility.singleLineHeight * textAreaAttr.lines));
            #endif
            

            // Check for changes
            if (EditorGUI.EndChangeCheck ()) {
                if (property.propertyType == NodePropertyType.String)
                    property.value = value;
                else {
                    // Get the text
                    var stringVar = property.value as StringVar;
                    if (stringVar != null) {
                        stringVar.Value = value;
                        property.ValueChanged();
                    }
                }
            }

            // Restore content color
            GUI.contentColor = contentColor;
        }
        /// <summary> 
        /// Returns action and condition nodes in the supplied nodes; branch nodes are ignored but their hierarchy will be included.
        /// <returns>The actions and conditions in the supplied nodes.</returns>
        /// </summary>
        public static ActionNode[] GetActionsAndConditions (ActionNode[] nodes) {
            var actionCondition = new List<ActionNode>();

            if (nodes != null) {
                for (int i = 0; i < nodes.Length; i++) {
                    if (nodes[i] is BranchNode) {
                        var branch = nodes[i] as BranchNode;
                        actionCondition.AddRange(ActionStateUtility.GetActionsAndConditions(branch.children));
                    }
                    else
                        actionCondition.Add(nodes[i]);
                }
            }

            return actionCondition.ToArray();
        }
        //
        // Methods
        //
        /// <summary>
        /// Handles the onGUI event.
        /// </summary>
        /// <param name="property">Property.</param>
        /// <param name="node">Node.</param>
        /// <param name="guiContent">GUI content.</param>
        public override void OnGUI(SerializedNodeProperty property, ActionNode node, GUIContent guiContent)
        {
            //if (animatorSerialized == null || aniController == null) {
                        if(aniController == null) {

                                //!!! Serialization never serialized Animator cos its initialized in Reset after
            //								NodePropertyIterator iter= property.serializedNode.GetIterator();
            //								iter.Find(attribute.animatorFieldName);
            //								 animatorSerialized=iter.current;
                                //				//								if(animatorSerialized==null || animatorSerialized.value==null){
                                //										Debug.LogError("AnimatorStateNodePropertyDrawer> No Animator component set on node parent GameObject");
                                //									return;
                                //								}

                                //runtimeContoller =( (Animator)animatorSerialized.value).runtimeAnimatorController;
                                Animator animator = node.GetType ().GetField (attribute.animatorFieldName).GetValue (node) as Animator;

                                RuntimeAnimatorController runtimeContoller;

                                runtimeContoller = animator.runtimeAnimatorController;

                                if (runtimeContoller is AnimatorOverrideController)
                                        aniController = ((AnimatorOverrideController)runtimeContoller).runtimeAnimatorController as UnityEditor.Animations.AnimatorController;
                                else
                                        aniController = runtimeContoller as UnityEditor.Animations.AnimatorController;

                        }

                        animatorStateDisplayOptions = MecanimUtility.GetDisplayOptions (aniController);
                        animatorStateValues = MecanimUtility.GetAnimatorStates (aniController);

                        if(property.value!=null){

                            if(animatorStateValues.Length>0){
                                    animatorStateSelectedPrev=animatorStateSelected=animatorStateValues.FirstOrDefault((itm)=>itm.nameHash==((ws.winx.unity.AnimatorState)property.value).nameHash);

                            }
                        }

                        animatorStateSelected = EditorGUILayoutEx.CustomObjectPopup (guiContent, animatorStateSelected, animatorStateDisplayOptions, animatorStateValues);//,compare);

                        //TODO try Begin/End Check
                        if (animatorStateSelectedPrev != animatorStateSelected) {

                                NodePropertyIterator iter = property.serializedNode.GetIterator ();
                                iter.Find (attribute.layerIndexFieldName);
                                SerializedNodeProperty layerIndexSerialized = iter.current;

                                layerIndexSerialized.value = MecanimUtility.GetLayerIndex (aniController, animatorStateSelected);
                                layerIndexSerialized.ApplyModifiedValue ();

                                    ws.winx.unity.AnimatorState state=property.value as ws.winx.unity.AnimatorState;
                                    if(state==null) state=ScriptableObject.CreateInstance<ws.winx.unity.AnimatorState>();

                                    state.motion=animatorStateSelected.motion;
                                    state.nameHash=animatorStateSelected.nameHash;
                                    state.layer=(int)layerIndexSerialized.value;

                                    if(state.motion is UnityEditor.Animations.BlendTree){
                                        BlendTree tree =(BlendTree)state.motion;
                                        int blendParamsNum= tree.GetRecursiveBlendParamCount();

                                        state.blendParamsHashes=new int[blendParamsNum];

                                        for(int i=0;i<blendParamsNum;i++)
                                            state.blendParamsHashes[i]=Animator.StringToHash(tree.GetRecursiveBlendParam(i));

                                    }else{
                                            state.blendParamsHashes=null;
                                    }

                                    //property.value=state;
                                    property.ValueChanged();

                                property.ApplyModifiedValue ();

                                animatorStateSelectedPrev = animatorStateSelected;
                        }

                        if (animatorStateSelected.motion == null)
                                Debug.LogError ("Selected state doesn't have Motion set");
        }
 public override bool CanAddNode(ActionNode child)
 {
     return(base.CanAddNode(child) && m_Child == null);
 }
        /// <summary>
        /// Create the runtime lists of nodes.
        /// </summary>
        void CreateRuntimeListsOfNodes()
        {
            // iterator
            int i;

            // OnEnable Nodes
            var onEnableList = new List <ActionNode>();

            for (i = 0; i < m_Nodes.Length; i++)
            {
                if (!(m_Nodes[i] is Update))
                {
                    onEnableList.Add(m_Nodes[i]);
                }
                else
                {
                    break;
                }
            }

            if (onEnableList.Count > 0)
            {
                m_OnEnable = ActionNode.CreateInstance(typeof(OnEnable), gameObject, this) as OnEnable;
                m_OnEnable.SetChildren(onEnableList.ToArray());
            }
            else
            {
                m_OnEnable = null;
            }

            // Update Nodes
            var updateList = new List <ActionNode>();
            // FixedUpdate Nodes
            var fixedUpdateList = new List <ActionNode>();
            // GUI Nodes
            var onGuiList = new List <ActionNode>();

            for (int j = i + 1; j < m_Nodes.Length; j++)
            {
                // OnGUI
                if (m_Nodes[j] is IGUINode)
                {
                    onGuiList.Add(m_Nodes[j]);
                }
                // FixedUpdate
                else if (m_Nodes[j] is IFixedUpdateNode)
                {
                    fixedUpdateList.Add(m_Nodes[j]);
                }
                // Update
                else
                {
                    updateList.Add(m_Nodes[j]);
                }
            }

            // FixedUpdate
            if (fixedUpdateList.Count > 0)
            {
                m_FixedUpdate = ActionNode.CreateInstance(typeof(FixedUpdate), gameObject, this) as FixedUpdate;
                // Call Reset if it's not in editor
                if (!Application.isEditor)
                {
                    m_FixedUpdate.Reset();
                }

                m_FixedUpdate.SetChildren(fixedUpdateList.ToArray());
            }
            else
            {
                m_FixedUpdate = null;
            }

            // Update
            if (updateList.Count > 0)
            {
                m_Update = ActionNode.CreateInstance(typeof(Update), gameObject, this) as Update;
                // Call Reset if it's not in editor
                if (!Application.isEditor)
                {
                    m_Update.Reset();
                }

                m_Update.SetChildren(updateList.ToArray());
            }
            else
            {
                m_Update = null;
            }

            // OnGUI
            if (onGuiList.Count > 0)
            {
                m_OnGUI = ActionNode.CreateInstance(typeof(OnGUI), gameObject, this) as OnGUI;
                // Call Reset if it's not in editor
                if (!Application.isEditor)
                {
                    m_OnGUI.Reset();
                }

                m_OnGUI.SetChildren(onGuiList.ToArray());
            }
            else
            {
                m_OnGUI = null;
            }
        }
        /// <summary>
        /// Returns the node index.
        /// <param name="node">The taget node.</param>
        /// <returns>The index of the supplied node.</returns>
        /// </summary>
        public int GetIndex(ActionNode node)
        {
            var nodes = GetNodes();

            return(nodes != null?System.Array.IndexOf(nodes, node) : -1);
        }
Beispiel #22
0
 /// <summary> 
 /// Determines whether a node is a child of this branch.
 /// <param name = "child">The object to locate in the children list.</param>
 /// <returns>True if node is found in the children list; otherwise, false.</returns>
 /// </summary>
 public override bool Contains (ActionNode child) {
     for (int i = 0; i < m_Children.Length; i++) {
         if (m_Children[i] == child)
             return true;
     }
     return false;
 }
 /// <summary>
 /// Determines whether a node is a child of this branch.
 /// <param name = "child">The node to locate in the children list.</param>
 /// <returns>True if node is found in the children list; otherwise, false.</returns>
 /// </summary>
 public override bool Contains(ActionNode child)
 {
     return(m_Child == child);
 }
Beispiel #24
0
 public override bool CanAddNode (ActionNode child) {
     return base.CanAddNode(child) && m_Child == null;
 }
        /// <summary> 
        /// Destroys the suplied node and its hierarchy from the tree.
        /// <param name="node">The node to be destroyed.</param>
        /// <returns>True if the node was successfully destroyed; false otherwise.</returns>
        /// </summary>
        public static bool DestroyNode (ActionNode node) {
            // Validate parameters
            if (node != null && node.tree != null) {
                // Get parent and tree
                var tree = node.tree; 
                var parent = node.branch;               

                // Register Undo
                #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                Undo.RegisterUndo(tree, "Delete " + node.name);
                #else
                Undo.RecordObject(tree, "Delete " + node.name);
                #endif

                // Removes node from parent
                if (parent != null) {
                    parent.Remove(node);

                    // Call OnValidate on the parent
                    parent.OnValidate();
                }

                // Removes node from tree
                tree.RemoveNode(node, true);

                // Saves tree and marks dirty flag
                StateUtility.SetDirty(tree);

                return true;
            }
            return false;
        }
        /// <summary> 
        /// Inserts a new node to the supplied branch, automatically handles undo, dirty flag and save node.
        /// <param name="node">The branch to add a new node.</param>
        /// <param name="newNodePosition">Move the node to the position of this node.</param>
        /// <param name="branch">The branch to drop the node or null.</param>
        /// </summary>
        public static bool MoveNode (ActionNode node, ActionNode newNodePosition, BranchNode branch) {
            // Validate parameters
            if (node != null  && node.tree != null) {
                // Get the tree
                var tree = node.tree;

                // The node does not belongs to the tree?
                if (!tree.GetNodes().Contains(node))
                    return false;

                // Register Undo
                #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                Undo.RegisterUndo(tree,"Move Node");
                #else
                Undo.RecordObject(tree,"Move Node");
                #endif

                // The node will be a root node?
                if (branch == null) {
                    // Remove from old branch
                    if (node.branch != null) {
                        BranchNode oldBranch = node.branch;
                        node.branch.Remove(node);

                        // Call OnValidate on old branch
                        oldBranch.OnValidate();
                    }

                    if (newNodePosition == null) {
                        var newIndex = node.tree.GetNodes().Count - 1;
                        node.tree.MoveNode(node.GetIndex(), newIndex);
                    }
                    else {
                        var newIndex = newNodePosition.root.GetIndex();
                        node.tree.MoveNode(node.GetIndex(), newIndex);
                    }
                }
                // The new node position is null?
                else if (newNodePosition == null) {
                    // node.branch = branch;
                    // Store old branch
                    var oldBranch = node.branch;

                    // Remove from old branch
                    if (oldBranch != null) {
                        oldBranch.Remove(node);
                    }

                    // Add to drop
                    if (!branch.Add(node)) {
                        // Restore old branch
                        if (oldBranch != null)
                            oldBranch.Add(node);
                        return false;
                    }

                    // Call OnValidate on branches
                    branch.OnValidate();
                    if (oldBranch != null && oldBranch != branch)
                        oldBranch.OnValidate();

                    node.tree.HierarchyChanged();
                }
                else {
                    // Cache the oldBranch
                    BranchNode oldBranch = node.branch;

                    // Get index
                    var index = -1;
                    var children = branch.children;
                    for (int i = 0; i < children.Length; i++) {
                        if (children[i] == newNodePosition) {
                            index = i;
                            break;
                        }
                    }

                    // The index is invalid?
                    if (index < 0 || !branch.Insert(index, node)) {
                        return false;
                    }
                    else {
                        // Call OnValidate on the branches
                        if (oldBranch != null)
                            oldBranch.OnValidate();
                        branch.OnValidate();
                        node.tree.HierarchyChanged();
                    } 
                }

                // Save move opration
                StateUtility.SetDirty(tree);

                return true;
            }
            return false;
        }
Beispiel #27
0
        /// <summary> 
        /// Removes a node from the branch.
        /// <param name = "child">The node to be removed from the list.</param>
        /// </summary>
        public override void Remove (ActionNode child) {
        	if (m_Child == child) {
                m_Child = null;

        		if (child != null && child.branch == this)
        			child.branch = null;
        	}
        }
        /// <summary>
        /// Removes node from tree.
        /// <param name="node">The node to be removed.</param>
        /// </summary>
        public void RemoveNode (ActionNode node) {
            // It's not the Update node?
            if (!(node is Update)) {
                GetNodes();

                // Remove node
                var nodes = new List<ActionNode>(m_Nodes);
                nodes.Remove(node);
                m_Nodes = nodes.ToArray();

                this.HierarchyChanged();
            }
        }
 /// <summary>
 /// Returns the node index.
 /// <param name="node">The taget node.</param>
 /// <returns>The index of the supplied node.</returns>
 /// </summary>
 public int GetIndex (ActionNode node) {
     var nodes = GetNodes();
     return nodes != null ? System.Array.IndexOf(nodes, node) : -1;
 }
Beispiel #30
0
 /// <summary> 
 /// Inserts a node in the branch list at the supplied index.
 /// <param name = "index">The index to insert the behaviour.</param>
 /// <param name = "child">The node to be added to the list.</param>
 /// <returns>True if the node was added to the list; otherwise false.</returns>
 /// </summary>
 public override bool Insert (int index, ActionNode child) {
 	if (index != 0)
 		return false;
 	return Add (child);
 }
        /// <summary>
        /// Removes node from tree.
        /// <param name="node">The node to be removed.</param>
        /// <param name="includeHierarchy">If true, the hierarchy will also be removed.</param>
        /// </summary>
        public void RemoveNode (ActionNode node, bool includeHierarchy) {
            GetNodes();
            var nodes = new List<ActionNode>(m_Nodes);
            BranchNode branch = node as BranchNode;

            // Remove children
            if (includeHierarchy && branch != null) {
                foreach (ActionNode n in branch.GetHierarchy()) {
                    nodes.Remove(n);
                }
            }

            // Remove node
            nodes.Remove(node);
            m_Nodes = nodes.ToArray();

            if (Application.isPlaying && this.enabled)  {
                // Update function nodes
                m_FunctionNodes = this.GetFunctionNodes();
                // Reset status
                node.ResetStatus();
                // Disable node
                node.OnDisable();
            }

            HierarchyChanged();
        }
Beispiel #32
0
        /// <summary> 
        /// Removes the node from this branch.
        /// <param name = "child">The object to be removed from the list.</param>
        /// </summary>
        public override void Remove (ActionNode child) {
            if (Contains(child)) {
                // Remove child
                var newChildren = new List<ActionNode>(m_Children);
                newChildren.Remove(child);
                m_Children = newChildren.ToArray();
                if (child != null && child.branch == this)
                    child.branch = null;

                // Call ResetStatus if it is playing
                if (Application.isPlaying)
                    child.ResetStatus();
            }
        }
Beispiel #33
0
        /// <summary>
        /// Reset the supplied node properties.
        /// <param name="node">The node to be reseted.</param>
        /// </summary>
        public static void ResetNode (ActionNode node) {
            // Get the owner as an Uniyt object
            var ownerUnityObj = node != null ? node.owner as UnityEngine.Object : null;
            // Validate parameters
            if (ownerUnityObj != null) {

                // Register Undo
                #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                Undo.RegisterUndo(ownerUnityObj, "Reset Node");
                #else
                Undo.RecordObject(ownerUnityObj, "Reset Node");
                #endif

                node.name = node.GetType().Name;
                node.Reset();
                node.OnValidate();
                StateUtility.SetDirty(node.owner);
            }
        }
 /// <summary>
 /// Returns the node index.
 /// <param name="node">The taget node.</param>
 /// <returns>The index of the supplied node.</returns>
 /// </summary>
 public int GetIndex (ActionNode node) {
     var nodes = GetNodes ();
     return nodes != null ? nodes.IndexOf(node) : -1;
 }
 /// <summary>
 /// Returns true if the supplied node can be add as child of the branch.
 /// <param name="newChild">The node to be tested if it can be a child of the branch.</param>
 /// <returns>True if the newChild can be a child of the branch; otherwise false.</returns>
 /// </summary>
 public virtual bool CanAddNode(ActionNode newChild)
 {
     return(newChild != null && newChild.tree == this.tree && !(newChild is FunctionNode) && (!(newChild is BranchNode) || !IsAncestor(newChild as BranchNode)));
 }
Beispiel #36
0
        /// <summary>
        /// Returns the node index.
        /// <param name="node">The taget node.</param>
        /// <returns>The index of the supplied node.</returns>
        /// </summary>
        public int GetIndex(ActionNode node)
        {
            var nodes = GetNodes();

            return(nodes != null?nodes.IndexOf(node) : -1);
        }
 /// <summary>
 /// Determines whether a node is a child of this branch.
 /// <param name = "child">The object to locate in the children list.</param>
 /// <returns>True if node is found in the children list; otherwise, false.</returns>
 /// </summary>
 public abstract bool Contains(ActionNode child);
 /// <summary>
 /// Removes the supplied node from this branch.
 /// <param name = "child">The object to be removed from the list.</param>
 /// </summary>
 public abstract void Remove(ActionNode child);
 /// <summary>
 /// Inserts a node in the nodes list at the supplied index.
 /// <param name = "index">The index to insert the behaviour.</param>
 /// <param name = "child">The node to be added to the list.</param>
 /// <returns>True if the node was added to the list; otherwise false.</returns>
 /// </summary>
 public abstract bool Insert(int index, ActionNode child);
 /// <summary>
 /// Adds the node to the branch nodes list.
 /// <param name = "child">The node to be added.</param>
 /// <returns>True if the node was added to the list; otherwise false.</returns>
 /// </summary>
 public abstract bool Add(ActionNode child);
        /// <summary>
        /// Draw the property pop-up.
        /// </summary>
        public override void OnGUI (SerializedNodeProperty property, ActionNode node, GUIContent guiContent) {
            // String
            if (property.propertyType == NodePropertyType.String) {
                var rect = GUILayoutUtility.GetRect(GUIContent.none, EditorStyles.popup);
                var id = GUIUtility.GetControlID(FocusType.Passive);
                var popupRect = EditorGUI.PrefixLabel(rect, id, guiContent);
                var popupName = property.value as string ?? string.Empty;
                var propertyOrField = node as PropertyOrField;

                var oldGUIColor = GUI.color;
                if (propertyOrField.propertyType == null)
                    GUI.color = Color.red;

                if (GUI.Button(popupRect, string.IsNullOrEmpty(popupName) ? "None" : popupName, EditorStyles.popup)) {
                    var isSetProperty = propertyOrField is SetProperty;
                    var members = FieldUtility.GetPublicMembers(propertyOrField.targetObject.ObjectType, null, false, isSetProperty, !isSetProperty);
                    var menu = new GenericMenu();

                    // Add none
                    menu.AddItem(new GUIContent("None"), string.IsNullOrEmpty(popupName), delegate () {property.value = string.Empty;});

                    // Add members
                    for (int i = 0; i < members.Length; i++) {
                        var memberName = members[i].Name;
                        menu.AddItem(new GUIContent(memberName), popupName == memberName, delegate () {property.value = memberName;});
                    }

                    menu.ShowAsContext();
                }
                GUI.color = oldGUIColor;
            }
            // Not String
            else
                EditorGUILayout.LabelField(guiContent, new GUIContent("Use UnityObjectProperty with string."));
        }