diff --git a/blendalot_animation_node.cpp b/blendalot_animation_node.cpp index da5c6f7..fb20c0c 100644 --- a/blendalot_animation_node.cpp +++ b/blendalot_animation_node.cpp @@ -545,6 +545,10 @@ bool BLTAnimationNodeBlendTree::BLTBlendTreeGraph::remove_node(const Ref removed_node_index) { + connection_info.parent_node_index--; + } + // Map connected subtrees HashSet old_indices = connection_info.input_subtree_node_indices; connection_info.input_subtree_node_indices.clear(); @@ -563,13 +567,14 @@ bool BLTAnimationNodeBlendTree::BLTBlendTreeGraph::remove_node(const Ref sorted_node_indices = get_sorted_node_indices(); - Vector> sorted_nodes; + LocalVector> sorted_nodes; LocalVector old_node_connection_info = node_connection_info; for (unsigned int i = 0; i < sorted_node_indices.size(); i++) { int node_index = sorted_node_indices[i]; sorted_nodes.push_back(nodes[node_index]); node_connection_info[i] = old_node_connection_info[node_index]; } + nodes = sorted_nodes; for (NodeConnectionInfo &connection_info : node_connection_info) { @@ -586,6 +591,20 @@ LocalVector BLTAnimationNodeBlendTree::BLTBlendTreeGraph::get_sorted_node_i sort_nodes_recursive(0, result); result.reverse(); + HashSet connected_node_indices; + for (int node_index : result) { + connected_node_indices.insert(node_index); + } + + // Ensure that nodes that are not reachable from the root node are still added to + // the sorted nodes indices. + for (Ref &node : nodes) { + int node_index = find_node_index(node); + if (!connected_node_indices.has(node_index)) { + result.push_back(node_index); + } + } + return result; } diff --git a/tests/test_blendalot_animgraph.h b/tests/test_blendalot_animgraph.h index af028e6..4bed159 100644 --- a/tests/test_blendalot_animgraph.h +++ b/tests/test_blendalot_animgraph.h @@ -537,7 +537,14 @@ TEST_CASE_FIXTURE(BlendTreeFixture, "[SceneTree][Blendalot][BlendTreeGraph][Chan CHECK(blend_tree_graph.node_connection_info[0].input_subtree_node_indices.size() == 6); CHECK(blend_tree_graph.node_connection_info[blend2_node_a_index_pre_remove].input_subtree_node_indices.size() == 5); - SUBCASE("Remove animation_sampler_node_a") { + SUBCASE("Removing the output node does nothing") { + int num_nodes = blend_tree_graph.nodes.size(); + int num_connections = blend_tree_graph.connections.size(); + CHECK(blend_tree_graph.remove_node(blend_tree_graph.get_output_node()) == false); + CHECK(blend_tree_graph.connections.size() == num_connections); + } + + SUBCASE("Remove a node with no children") { blend_tree_graph.remove_node(animation_sampler_node_a); for (const BLTBlendTreeConnection &connection : blend_tree_graph.connections) { @@ -561,8 +568,12 @@ TEST_CASE_FIXTURE(BlendTreeFixture, "[SceneTree][Blendalot][BlendTreeGraph][Chan CHECK(blend_tree_graph.node_connection_info[blend2_node_b_index_post_remove].input_subtree_node_indices.has(animation_sampler_node_b_index_post_remove)); } - SUBCASE("Remove blend2_node_a") { + SUBCASE("Remove a node with parent and children") { + int num_nodes = blend_tree_graph.nodes.size(); blend_tree_graph.remove_node(blend2_node_a); + blend_tree_graph.sort_nodes_and_references(); + + CHECK(blend_tree_graph.nodes.size() == num_nodes - 1); for (const BLTBlendTreeConnection &connection : blend_tree_graph.connections) { bool is_connection_with_removed_node = connection.source_node == blend2_node_a || connection.target_node == blend2_node_a;