瀏覽代碼

Fix animation change callbacks

SaracenOne 3 年之前
父節點
當前提交
6a8fbf3d25

+ 5 - 0
doc/classes/AnimationPlayer.xml

@@ -268,6 +268,11 @@
 				[b]Note:[/b] This signal is not emitted if an animation is looping.
 				[b]Note:[/b] This signal is not emitted if an animation is looping.
 			</description>
 			</description>
 		</signal>
 		</signal>
+		<signal name="animation_libraries_updated">
+			<description>
+				Notifies when the animation libraries have changed.
+			</description>
+		</signal>
 		<signal name="animation_list_changed">
 		<signal name="animation_list_changed">
 			<description>
 			<description>
 				Notifies when an animation list is changed.
 				Notifies when an animation list is changed.

+ 25 - 0
editor/plugins/animation_player_editor_plugin.cpp

@@ -720,7 +720,18 @@ void AnimationPlayerEditor::set_state(const Dictionary &p_state) {
 	if (p_state.has("player")) {
 	if (p_state.has("player")) {
 		Node *n = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["player"]);
 		Node *n = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["player"]);
 		if (Object::cast_to<AnimationPlayer>(n) && EditorNode::get_singleton()->get_editor_selection()->is_selected(n)) {
 		if (Object::cast_to<AnimationPlayer>(n) && EditorNode::get_singleton()->get_editor_selection()->is_selected(n)) {
+			if (player) {
+				if (player->is_connected("animation_libraries_updated", callable_mp(this, &AnimationPlayerEditor::_animation_libraries_updated))) {
+					player->disconnect("animation_libraries_updated", callable_mp(this, &AnimationPlayerEditor::_animation_libraries_updated));
+				}
+			}
 			player = Object::cast_to<AnimationPlayer>(n);
 			player = Object::cast_to<AnimationPlayer>(n);
+			if (player) {
+				if (!player->is_connected("animation_libraries_updated", callable_mp(this, &AnimationPlayerEditor::_animation_libraries_updated))) {
+					player->connect("animation_libraries_updated", callable_mp(this, &AnimationPlayerEditor::_animation_libraries_updated));
+				}
+			}
+
 			_update_player();
 			_update_player();
 			EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
 			EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
 			set_process(true);
 			set_process(true);
@@ -977,9 +988,18 @@ void AnimationPlayerEditor::edit(AnimationPlayer *p_player) {
 	if (player && pin->is_pressed()) {
 	if (player && pin->is_pressed()) {
 		return; // Ignore, pinned.
 		return; // Ignore, pinned.
 	}
 	}
+
+	if (player) {
+		if (player->is_connected("animation_libraries_updated", callable_mp(this, &AnimationPlayerEditor::_animation_libraries_updated))) {
+			player->disconnect("animation_libraries_updated", callable_mp(this, &AnimationPlayerEditor::_animation_libraries_updated));
+		}
+	}
 	player = p_player;
 	player = p_player;
 
 
 	if (player) {
 	if (player) {
+		if (!player->is_connected("animation_libraries_updated", callable_mp(this, &AnimationPlayerEditor::_animation_libraries_updated))) {
+			player->connect("animation_libraries_updated", callable_mp(this, &AnimationPlayerEditor::_animation_libraries_updated));
+		}
 		_update_player();
 		_update_player();
 
 
 		if (onion.enabled) {
 		if (onion.enabled) {
@@ -1146,6 +1166,10 @@ void AnimationPlayerEditor::_animation_player_changed(Object *p_pl) {
 	}
 	}
 }
 }
 
 
+void AnimationPlayerEditor::_animation_libraries_updated() {
+	_animation_player_changed(player);
+}
+
 void AnimationPlayerEditor::_list_changed() {
 void AnimationPlayerEditor::_list_changed() {
 	if (is_visible_in_tree()) {
 	if (is_visible_in_tree()) {
 		_update_player();
 		_update_player();
@@ -1540,6 +1564,7 @@ void AnimationPlayerEditor::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("_animation_edit"), &AnimationPlayerEditor::_animation_edit);
 	ClassDB::bind_method(D_METHOD("_animation_edit"), &AnimationPlayerEditor::_animation_edit);
 	ClassDB::bind_method(D_METHOD("_animation_resource_edit"), &AnimationPlayerEditor::_animation_resource_edit);
 	ClassDB::bind_method(D_METHOD("_animation_resource_edit"), &AnimationPlayerEditor::_animation_resource_edit);
 	ClassDB::bind_method(D_METHOD("_animation_player_changed"), &AnimationPlayerEditor::_animation_player_changed);
 	ClassDB::bind_method(D_METHOD("_animation_player_changed"), &AnimationPlayerEditor::_animation_player_changed);
+	ClassDB::bind_method(D_METHOD("_animation_libraries_updated"), &AnimationPlayerEditor::_animation_libraries_updated);
 	ClassDB::bind_method(D_METHOD("_list_changed"), &AnimationPlayerEditor::_list_changed);
 	ClassDB::bind_method(D_METHOD("_list_changed"), &AnimationPlayerEditor::_list_changed);
 	ClassDB::bind_method(D_METHOD("_animation_duplicate"), &AnimationPlayerEditor::_animation_duplicate);
 	ClassDB::bind_method(D_METHOD("_animation_duplicate"), &AnimationPlayerEditor::_animation_duplicate);
 
 

+ 1 - 0
editor/plugins/animation_player_editor_plugin.h

@@ -193,6 +193,7 @@ class AnimationPlayerEditor : public VBoxContainer {
 	void _blend_edited();
 	void _blend_edited();
 
 
 	void _animation_player_changed(Object *p_pl);
 	void _animation_player_changed(Object *p_pl);
+	void _animation_libraries_updated();
 
 
 	void _animation_key_editor_seek(float p_pos, bool p_drag, bool p_timeline_only = false);
 	void _animation_key_editor_seek(float p_pos, bool p_drag, bool p_timeline_only = false);
 	void _animation_key_editor_anim_len_changed(float p_len);
 	void _animation_key_editor_anim_len_changed(float p_len);

+ 30 - 2
scene/animation/animation_player.cpp

@@ -109,7 +109,7 @@ bool AnimationPlayer::_set(const StringName &p_name, const Variant &p_value) {
 			Ref<AnimationLibrary> lib = d[lib_name];
 			Ref<AnimationLibrary> lib = d[lib_name];
 			add_animation_library(lib_name, lib);
 			add_animation_library(lib_name, lib);
 		}
 		}
-
+		emit_signal("animation_libraries_updated");
 	} else if (name.begins_with("next/")) {
 	} else if (name.begins_with("next/")) {
 		String which = name.get_slicec('/', 1);
 		String which = name.get_slicec('/', 1);
 		animation_set_next(which, p_value);
 		animation_set_next(which, p_value);
@@ -1274,6 +1274,23 @@ void AnimationPlayer::_animation_set_cache_update() {
 }
 }
 
 
 void AnimationPlayer::_animation_added(const StringName &p_name, const StringName &p_library) {
 void AnimationPlayer::_animation_added(const StringName &p_name, const StringName &p_library) {
+	{
+		int at_pos = -1;
+
+		for (uint32_t i = 0; i < animation_libraries.size(); i++) {
+			if (animation_libraries[i].name == p_library) {
+				at_pos = i;
+				break;
+			}
+		}
+
+		ERR_FAIL_COND(at_pos == -1);
+
+		ERR_FAIL_COND(!animation_libraries[at_pos].library->animations.has(p_name));
+
+		_ref_anim(animation_libraries[at_pos].library->animations[p_name]);
+	}
+
 	_animation_set_cache_update();
 	_animation_set_cache_update();
 }
 }
 
 
@@ -1283,6 +1300,12 @@ void AnimationPlayer::_animation_removed(const StringName &p_name, const StringN
 	if (!animation_set.has(name)) {
 	if (!animation_set.has(name)) {
 		return; // No need to update because not the one from the library being used.
 		return; // No need to update because not the one from the library being used.
 	}
 	}
+
+	AnimationData animation_data = animation_set[name];
+	if (animation_data.animation_library == p_library) {
+		_unref_anim(animation_data.animation);
+	}
+
 	_animation_set_cache_update();
 	_animation_set_cache_update();
 
 
 	// Erase blends if needed
 	// Erase blends if needed
@@ -1379,6 +1402,10 @@ Error AnimationPlayer::add_animation_library(const StringName &p_name, const Ref
 	ald.library->connect(SNAME("animation_removed"), callable_mp(this, &AnimationPlayer::_animation_added).bind(p_name));
 	ald.library->connect(SNAME("animation_removed"), callable_mp(this, &AnimationPlayer::_animation_added).bind(p_name));
 	ald.library->connect(SNAME("animation_renamed"), callable_mp(this, &AnimationPlayer::_animation_renamed).bind(p_name));
 	ald.library->connect(SNAME("animation_renamed"), callable_mp(this, &AnimationPlayer::_animation_renamed).bind(p_name));
 
 
+	for (const KeyValue<StringName, Ref<Animation>> &K : ald.library->animations) {
+		_ref_anim(K.value);
+	}
+
 	_animation_set_cache_update();
 	_animation_set_cache_update();
 
 
 	notify_property_list_changed();
 	notify_property_list_changed();
@@ -1399,7 +1426,7 @@ void AnimationPlayer::remove_animation_library(const StringName &p_name) {
 	ERR_FAIL_COND(at_pos == -1);
 	ERR_FAIL_COND(at_pos == -1);
 
 
 	animation_libraries[at_pos].library->disconnect(SNAME("animation_added"), callable_mp(this, &AnimationPlayer::_animation_added));
 	animation_libraries[at_pos].library->disconnect(SNAME("animation_added"), callable_mp(this, &AnimationPlayer::_animation_added));
-	animation_libraries[at_pos].library->disconnect(SNAME("animation_removed"), callable_mp(this, &AnimationPlayer::_animation_added));
+	animation_libraries[at_pos].library->disconnect(SNAME("animation_removed"), callable_mp(this, &AnimationPlayer::_animation_removed));
 	animation_libraries[at_pos].library->disconnect(SNAME("animation_renamed"), callable_mp(this, &AnimationPlayer::_animation_renamed));
 	animation_libraries[at_pos].library->disconnect(SNAME("animation_renamed"), callable_mp(this, &AnimationPlayer::_animation_renamed));
 
 
 	stop();
 	stop();
@@ -2152,6 +2179,7 @@ void AnimationPlayer::_bind_methods() {
 	ADD_SIGNAL(MethodInfo("animation_changed", PropertyInfo(Variant::STRING_NAME, "old_name"), PropertyInfo(Variant::STRING_NAME, "new_name")));
 	ADD_SIGNAL(MethodInfo("animation_changed", PropertyInfo(Variant::STRING_NAME, "old_name"), PropertyInfo(Variant::STRING_NAME, "new_name")));
 	ADD_SIGNAL(MethodInfo("animation_started", PropertyInfo(Variant::STRING_NAME, "anim_name")));
 	ADD_SIGNAL(MethodInfo("animation_started", PropertyInfo(Variant::STRING_NAME, "anim_name")));
 	ADD_SIGNAL(MethodInfo("animation_list_changed"));
 	ADD_SIGNAL(MethodInfo("animation_list_changed"));
+	ADD_SIGNAL(MethodInfo("animation_libraries_updated"));
 	ADD_SIGNAL(MethodInfo("caches_cleared"));
 	ADD_SIGNAL(MethodInfo("caches_cleared"));
 
 
 	BIND_ENUM_CONSTANT(ANIMATION_PROCESS_PHYSICS);
 	BIND_ENUM_CONSTANT(ANIMATION_PROCESS_PHYSICS);