/// <summary> /// Update the selected/active node. /// </summary> private void UpdateActiveNode() { if (m_NodeList != null && m_ActionState != null) { m_NodeList.list = m_ActionState.GetNodes(); ActionNode activeNode = this.GetActiveNode(); if (activeNode != null) { m_NodeList.index = activeNode.GetIndex(); } else { this.OnSelectNode(m_NodeList); } } }
/// <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); }