示例#1
0
        /// <summary>
        /// Inserts a new node to the supplied branch, automatically handles undo, dirty flag and save node.
        /// <param name="branch">The branch to add a new node.</param>
        /// <param name="index">The index of the new node.</param>
        /// <param name="nodeType">The type of the new node.</param>
        /// <returns>The new node.</returns>
        /// </summary>
        public static ActionNode InsertNode(BranchNode branch, int index, System.Type nodeType)
        {
            // Validate parameters
            if (branch != null && branch.tree != null && nodeType != null && index >= 0 && index <= branch.children.Length)
            {
                // Get the tree
                var tree = branch.tree;

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

                // Create new node
                var newNode = tree.AddNode(nodeType);

                if (newNode != null)
                {
                    // Insert new node
                    branch.Insert(index, newNode);

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

                    // Saves node and sets dirty flag
                    StateUtility.SetDirty(tree);
                    return(newNode);
                }
            }
            return(null);
        }
示例#2
0
        /// <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);
        }