From 6d8bb7f73c2bd27ca5a4b4f7bc0c51c434bda620 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Mon, 8 Jan 2024 14:41:56 +0100 Subject: [PATCH] Anim: armature.collections.move() now retains the active collection `armature.collections.move(x, y)` now retains the active collection. Due to the move, the active collection index can change. The index is now updated to accomodate for this. --- .../blender/animrig/intern/bone_collections.cc | 9 +++++++++ .../animrig/intern/bone_collections_test.cc | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/source/blender/animrig/intern/bone_collections.cc b/source/blender/animrig/intern/bone_collections.cc index 3007875f658..114993342bb 100644 --- a/source/blender/animrig/intern/bone_collections.cc +++ b/source/blender/animrig/intern/bone_collections.cc @@ -1383,6 +1383,15 @@ void bonecolls_rotate_block(bArmature *armature, bcoll->child_index += direction; } } + + /* Make sure the active bone collection index is moved as well. */ + const int active_index = armature->runtime.active_collection_index; + if (active_index == move_from_index) { + armature->runtime.active_collection_index = move_to_index; + } + else if (start_index <= active_index && active_index < start_index + count) { + armature->runtime.active_collection_index += direction; + } } void bonecolls_move_to_index(bArmature *armature, const int from_index, const int to_index) diff --git a/source/blender/animrig/intern/bone_collections_test.cc b/source/blender/animrig/intern/bone_collections_test.cc index 1161c47893b..7aa84ff7db4 100644 --- a/source/blender/animrig/intern/bone_collections_test.cc +++ b/source/blender/animrig/intern/bone_collections_test.cc @@ -1354,9 +1354,18 @@ class ANIM_armature_bone_collections_testlist : public testing::Test { TEST_F(ANIM_armature_bone_collections_testlist, move_before_after_index__before_first_sibling) { + /* Set the active index to be one of the affected bone collections. */ + ANIM_armature_bonecoll_active_name_set(&arm, "child2"); + ASSERT_EQ(3, arm.runtime.active_collection_index); + EXPECT_EQ(1, ANIM_armature_bonecoll_move_before_after_index(&arm, 3, 1, MoveLocation::Before)); EXPECT_TRUE(expect_bcolls({"root", "child2", "child0", "child1", "child1_0"})); EXPECT_EQ(0, armature_bonecoll_find_parent_index(&arm, 1)); + + /* The three indicators of the active collection should still be in sync. */ + EXPECT_EQ(1, arm.runtime.active_collection_index); + EXPECT_EQ(child2, arm.runtime.active_collection); + EXPECT_STREQ("child2", arm.active_collection_name); } TEST_F(ANIM_armature_bone_collections_testlist, move_before_after_index__after_first_sibling) @@ -1368,9 +1377,18 @@ TEST_F(ANIM_armature_bone_collections_testlist, move_before_after_index__after_f TEST_F(ANIM_armature_bone_collections_testlist, move_before_after_index__before_last_sibling) { + /* Set the active index to be one of the affected bone collections. */ + ANIM_armature_bonecoll_active_name_set(&arm, "child1"); + ASSERT_EQ(2, arm.runtime.active_collection_index); + EXPECT_EQ(2, ANIM_armature_bonecoll_move_before_after_index(&arm, 1, 3, MoveLocation::Before)); EXPECT_TRUE(expect_bcolls({"root", "child1", "child0", "child2", "child1_0"})); EXPECT_EQ(0, armature_bonecoll_find_parent_index(&arm, 2)); + + /* The three indicators of the active collection should still be in sync. */ + EXPECT_EQ(1, arm.runtime.active_collection_index); + EXPECT_EQ(child1, arm.runtime.active_collection); + EXPECT_STREQ("child1", arm.active_collection_name); } TEST_F(ANIM_armature_bone_collections_testlist, move_before_after_index__after_last_sibling)