[godot] Fix SpineAnimationTrack and SpineSlotNode for 4.x

This commit is contained in:
badlogic 2022-04-27 14:54:23 +02:00
parent 4c417dd2c2
commit 323a8b9299
3 changed files with 69 additions and 4 deletions

View File

@ -1,5 +1,10 @@
#include "SpineAnimationTrack.h"
#if VERSION_MAJOR > 3
#include "core/config/engine.h"
#else
#include "core/engine.h"
#endif
#include "godot/editor/editor_node.h"
#include "scene/animation/animation_player.h"
#include "scene/resources/animation.h"
@ -75,7 +80,11 @@ void SpineAnimationTrack::_notification(int what) {
case NOTIFICATION_PARENTED: {
sprite = Object::cast_to<SpineSprite>(get_parent());
if (sprite)
#if VERSION_MAJOR > 3
sprite->connect("before_animation_state_update", callable_mp(this, &SpineAnimationTrack::update_animation_state));
#else
sprite->connect("before_animation_state_update", this, "update_animation_state");
#endif
NOTIFY_PROPERTY_LIST_CHANGED();
break;
}
@ -85,7 +94,11 @@ void SpineAnimationTrack::_notification(int what) {
}
case NOTIFICATION_UNPARENTED: {
if (sprite) {
#if VERSION_MAJOR > 3
sprite->disconnect("before_animation_state_update", callable_mp(this, &SpineAnimationTrack::update_animation_state));
#else
sprite->disconnect("before_animation_state_update", this, "update_animation_state");
#endif
sprite = nullptr;
}
break;
@ -134,27 +147,49 @@ void SpineAnimationTrack::setup_animation_player() {
add_child(animation_player);
animation_player->set_owner(sprite->get_owner());
} else {
#if VERSION_MAJOR > 3
List<StringName> animation_libraries;
animation_player->get_animation_library_list(&animation_libraries);
for (int i = 0; i < animation_libraries.size(); i++) {
animation_player->remove_animation_library(animation_libraries[i]);
}
#else
List<StringName> animation_names;
animation_player->get_animation_list(&animation_names);
for (int i = 0; i < animation_names.size(); i++) {
animation_player->remove_animation(animation_names[i]);
}
#endif
}
auto skeleton_data = sprite->get_skeleton_data_res()->get_skeleton_data();
auto &animations = skeleton_data->getAnimations();
#if VERSION_MAJOR > 3
Ref<AnimationLibrary> animation_library;
animation_library.instantiate();
animation_player->add_animation_library("", animation_library);
#endif
for (int i = 0; i < (int)animations.size(); i++) {
auto &animation = animations[i];
Ref<Animation> animation_ref = create_animation(animation, false);
animation_player->add_animation(animation_ref->get_name(), animation_ref);
Ref<Animation> animation_looped_ref = create_animation(animation, true);
#if VERSION_MAJOR > 3
animation_library->add_animation(animation_ref->get_name(), animation_ref);
animation_library->add_animation(animation_looped_ref->get_name(), animation_looped_ref);
#else
animation_player->add_animation(animation_ref->get_name(), animation_ref);
animation_player->add_animation(animation_looped_ref->get_name(), animation_looped_ref);
#endif
}
Ref<Animation> reset_animation_ref;
INSTANTIATE(reset_animation_ref);
reset_animation_ref->set_name("RESET");
#if VERSION_MAJOR > 3
// reset_animation_ref->set_loop(true);
#else
reset_animation_ref->set_loop(true);
#endif
reset_animation_ref->set_length(0.5f);
reset_animation_ref->add_track(Animation::TYPE_VALUE);
reset_animation_ref->track_set_path(0, NodePath(".:animation_name"));
@ -162,8 +197,13 @@ void SpineAnimationTrack::setup_animation_player() {
reset_animation_ref->add_track(Animation::TYPE_VALUE);
reset_animation_ref->track_set_path(1, NodePath(".:loop"));
reset_animation_ref->track_insert_key(1, 0, false);
#if VERSION_MAJOR > 3
animation_library->add_animation(reset_animation_ref->get_name(), reset_animation_ref);
animation_library->add_animation("-- Empty --", reset_animation_ref);
#else
animation_player->add_animation(reset_animation_ref->get_name(), reset_animation_ref);
animation_player->add_animation("-- Empty --", reset_animation_ref);
#endif
}
Ref<Animation> SpineAnimationTrack::create_animation(spine::Animation *animation, bool loop) {
@ -173,7 +213,7 @@ Ref<Animation> SpineAnimationTrack::create_animation(spine::Animation *animation
Ref<Animation> animation_ref;
INSTANTIATE(animation_ref);
animation_ref->set_name(String(animation->getName().buffer()) + (loop ? "" : "_looped"));
animation_ref->set_loop(!loop);
// animation_ref->set_loop(!loop);
animation_ref->set_length(duration);
animation_ref->add_track(Animation::TYPE_VALUE);
@ -205,7 +245,11 @@ void SpineAnimationTrack::update_animation_state(const Variant &variant_sprite)
#ifdef TOOLS_ENABLED
// When the animation dock is no longer visible or we aren't being
// keyed in the current animation, bail.
#if VERSION_MAJOR > 3
auto player_editor = AnimationPlayerEditor::get_singleton();
#else
auto player_editor = AnimationPlayerEditor::singleton;
#endif
if (!player_editor->is_visible_in_tree()) {
skeleton->setToSetupPose();
animation_state->clearTracks();

View File

@ -40,6 +40,7 @@
#define INSTANTIATE(x) (x).instantiate()
#define NOTIFY_PROPERTY_LIST_CHANGED() notify_property_list_changed()
#define VARIANT_FLOAT Variant::FLOAT
#define PROPERTY_USAGE_NOEDITOR PROPERTY_USAGE_NO_EDITOR
#else
#include "core/object.h"
#include "core/reference.h"
@ -74,7 +75,11 @@ protected:
void spine_objects_invalidated() {
spine_object = nullptr;
#if VERSION_MAJOR > 3
spine_owner->disconnect("_internal_spine_objects_invalidated", callable_mp(this, &SpineObjectWrapper::spine_objects_invalidated));
#else
spine_owner->disconnect("_internal_spine_objects_invalidated", this, "_internal_spine_objects_invalidated");
#endif
}
SpineObjectWrapper(): spine_owner(nullptr), spine_object(nullptr) {
@ -96,7 +101,11 @@ protected:
spine_owner = (Object*)_owner;
spine_object = _object;
#if VERSION_MAJOR > 3
spine_owner->connect("_internal_spine_objects_invalidated", callable_mp(this, &SpineObjectWrapper::spine_objects_invalidated));
#else
spine_owner->connect("_internal_spine_objects_invalidated", this, "_internal_spine_objects_invalidated");
#endif
}
void *_get_spine_object_internal() { return spine_object; }

View File

@ -17,8 +17,13 @@ void SpineSlotNode::_notification(int what) {
case NOTIFICATION_PARENTED: {
sprite = Object::cast_to<SpineSprite>(get_parent());
if (sprite) {
#if VERSION_MAJOR > 3
sprite->connect("world_transforms_changed", callable_mp(this, &SpineSlotNode::on_world_transforms_changed));
#else
sprite->connect("world_transforms_changed", this, "_on_world_transforms_changed");
#endif
update_transform(sprite);
#if VERSION_MAJOR == 3
_change_notify("transform/translation");
_change_notify("transform/rotation");
_change_notify("transform/scale");
@ -26,6 +31,7 @@ void SpineSlotNode::_notification(int what) {
_change_notify("rotation");
_change_notify("rotation_deg");
_change_notify("scale");
#endif
} else {
WARN_PRINT("SpineSlotNode parent is not a SpineSprite.");
}
@ -34,7 +40,11 @@ void SpineSlotNode::_notification(int what) {
}
case NOTIFICATION_UNPARENTED: {
if (sprite) {
sprite->disconnect("world_transforms_changed", this, "_on_world_transforms_changed");
#if VERSION_MAJOR > 3
sprite->disconnect("world_transforms_changed", callable_mp(this, &SpineSlotNode::on_world_transforms_changed));
#else
sprite->disconnect("world_transforms_changed", this, "_on_world_transforms_changed");
#endif
}
}
default:
@ -67,6 +77,7 @@ bool SpineSlotNode::_set(const StringName& property, const Variant& value) {
if (property == "slot_name") {
slot_name = value;
update_transform(sprite);
#if VERSION_MAJOR == 3
_change_notify("transform/translation");
_change_notify("transform/rotation");
_change_notify("transform/scale");
@ -74,6 +85,7 @@ bool SpineSlotNode::_set(const StringName& property, const Variant& value) {
_change_notify("rotation");
_change_notify("rotation_deg");
_change_notify("scale");
#endif
return true;
}
return false;
@ -105,4 +117,4 @@ void SpineSlotNode::set_slot_name(const String& _slot_name) {
String SpineSlotNode::get_slot_name() {
return slot_name;
}
}