[godot] SpineSkeletonDataResource animation mixes UI functional.

This commit is contained in:
badlogic 2022-04-13 21:28:46 +02:00
parent 91a7a1f3cc
commit 22c0f2a9eb
7 changed files with 120 additions and 21 deletions

View File

@ -67,6 +67,9 @@ namespace spine {
/// or the DefaultMix if no mix duration has been set.
float getMix(Animation *from, Animation *to);
/// Removes all mixes and sets the default mix to 0.
void clear();
private:
class AnimationPair : public SpineObject {
public:

View File

@ -73,6 +73,11 @@ void AnimationStateData::setDefaultMix(float inValue) {
_defaultMix = inValue;
}
void AnimationStateData::clear() {
_defaultMix = 0;
_animationToMixTime.clear();
}
AnimationStateData::AnimationPair::AnimationPair(Animation *a1, Animation *a2) : _a1(a1), _a2(a2) {
}

View File

@ -1,9 +1,19 @@
[gd_resource type="SpineSkeletonDataResource" load_steps=16 format=2]
[gd_resource type="SpineSkeletonDataResource" load_steps=5 format=2]
[ext_resource path="res://assets/spineboy/spineboy.atlas" type="SpineAtlasResource" id=1]
[ext_resource path="res://assets/spineboy/spineboy-pro.json" type="SpineSkeletonFileResource" id=2]
[sub_resource type="SpineAnimationMix" id=1]
from = "death"
to = "aim"
mix = 1.2
[sub_resource type="SpineAnimationMix" id=2]
from = "idle-turn"
to = "aim"
[resource]
atlas_res = ExtResource( 1 )
skeleton_file_res = ExtResource( 2 )
default_mix = 0.2
animation_mixes = [ SubResource( 1 ), SubResource( 2 ) ]

View File

@ -101,53 +101,110 @@ bool SpineAnimationMixesInspectorPlugin::parse_property(Object *object, Variant:
return false;
}
SpineEditorPropertyAnimationMixes::SpineEditorPropertyAnimationMixes() {
container = memnew(VBoxContainer);
add_child(container);
set_bottom_editor(container);
SpineEditorPropertyAnimationMixes::SpineEditorPropertyAnimationMixes(): skeleton_data(nullptr), container(nullptr), updating(false) {
}
void SpineEditorPropertyAnimationMixes::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_mix"), &SpineEditorPropertyAnimationMixes::add_mix);
ClassDB::bind_method(D_METHOD("delete_mix"), &SpineEditorPropertyAnimationMixes::delete_mix);
ClassDB::bind_method(D_METHOD("property_changed"), &SpineEditorPropertyAnimationMixes::property_changed);
}
void SpineEditorPropertyAnimationMixes::add_mix() {
if (!skeleton_data.is_valid() || !skeleton_data->is_skeleton_data_loaded() || updating) return;
Vector<String> animation_names;
skeleton_data->get_animation_names(animation_names);
Ref<SpineAnimationMix> mix = Ref<SpineAnimationMix>(memnew(SpineAnimationMix));
mix->set_from(animation_names[0]);
mix->set_to(animation_names[0]);
mix->set_mix(0);
Array mixes = skeleton_data->get_animation_mixes().duplicate();
mixes.push_back(mix);
skeleton_data->set_animation_mixes(mixes);
emit_changed(get_edited_property(), mixes);
}
void SpineEditorPropertyAnimationMixes::delete_mix(int64_t idx) {
if (!skeleton_data.is_valid() || !skeleton_data->is_skeleton_data_loaded() || updating) return;
auto mixes = skeleton_data->get_animation_mixes().duplicate();
mixes.remove(idx);
skeleton_data->set_animation_mixes(mixes);
emit_changed(get_edited_property(), mixes);
}
void SpineEditorPropertyAnimationMixes::property_changed(const String &property, Variant value, const String &name, bool changing, Ref<SpineAnimationMix> mix) {
if (property == "from") mix->set_from(value);
if (property == "to") mix->set_to(value);
if (property == "mix") mix->set_mix(value);
emit_changed(get_edited_property(), skeleton_data->get_animation_mixes().duplicate());
}
void SpineEditorPropertyAnimationMixes::update_property() {
if (updating) return;
updating = true;
for (int i = 0; container->get_child_count() != 0; i++) {
auto child = container->get_child(i);
child->queue_delete();
container->remove_child(child);
if (container) {
set_bottom_editor(nullptr);
memdelete(container);
container->queue_delete();
container = nullptr;
}
if (!skeleton_data.is_valid() || !skeleton_data->is_skeleton_data_loaded()) {
updating = false;
return;
}
Vector<String> animation_names;
skeleton_data->get_animation_names(animation_names);
container = memnew(VBoxContainer);
add_child(container);
set_bottom_editor(container);
Array mixes = skeleton_data->get_animation_mixes();
for (int i = 0; i < mixes.size(); i++) {
Ref<SpineAnimationMix> mix = mixes[i];
auto hbox = memnew(HBoxContainer);
hbox->set_h_size_flags(SIZE_FILL);
auto from_label = memnew(Label);
from_label->set_text("From:" );
hbox->add_child(from_label);
auto from_enum = memnew(EditorPropertyTextEnum);
from_enum->set_h_size_flags(SIZE_EXPAND_FILL);
from_enum->setup(animation_names);
from_enum->set_object_and_property(*mix, "from");
from_enum->update_property();
from_enum->connect("property_changed", this, "property_changed", varray(mix));
hbox->add_child(from_enum);
auto to_label = memnew(Label);
to_label->set_text("To: ");
hbox->add_child(to_label);
auto to_enum = memnew(EditorPropertyTextEnum);
to_enum->set_h_size_flags(SIZE_EXPAND_FILL);
to_enum->setup(animation_names);
to_enum->set_object_and_property(*mix, "to");
to_enum->update_property();
to_enum->connect("property_changed", this, "property_changed", varray(mix));
hbox->add_child(to_enum);
auto mix_float = memnew(EditorPropertyFloat);
mix_float->set_h_size_flags(SIZE_EXPAND_FILL);
mix_float->setup(0, 9999999, 0.001, true, false, false, false);
mix_float->set_object_and_property(*mix, "mix");
mix_float->update_property();
mix_float->connect("property_changed", this, "property_changed", varray(mix));
hbox->add_child(mix_float);
auto delete_button = memnew(Button);
delete_button->set_text("Delete");
delete_button->connect("pressed", this, "delete_mix", varray(i));
hbox->add_child(delete_button);
container->add_child(hbox);
}
add_mix_button = memnew(Button);
auto add_mix_button = memnew(Button);
add_mix_button->set_text("Add mix");
add_mix_button->set_h_size_flags(SIZE_EXPAND_FILL);
add_mix_button->connect("pressed", this, "add_mix");

View File

@ -140,13 +140,12 @@ class SpineEditorPropertyAnimationMixes: public EditorProperty {
Ref<SpineSkeletonDataResource> skeleton_data;
VBoxContainer *container;
Button *add_mix_button;
Vector<HBoxContainer *> cells;
Vector<Button *> delete_mix;
bool updating;
static void _bind_methods();
void add_mix();
void delete_mix(int64_t idx);
void property_changed(const String &property, Variant value, const String &name, bool changing, Ref<SpineAnimationMix> mix);
public:
SpineEditorPropertyAnimationMixes();
void setup(Ref<SpineSkeletonDataResource> skeleton_data) { this->skeleton_data = skeleton_data; };

View File

@ -171,6 +171,7 @@ void SpineSkeletonDataResource::load_res(spine::Atlas *atlas, const String &json
}
skeleton_data = data;
animation_state_data = new spine::AnimationStateData(data);
update_mixes();
}
bool SpineSkeletonDataResource::is_skeleton_data_loaded() const {
@ -217,7 +218,7 @@ void SpineSkeletonDataResource::get_skin_names(Vector<String> &skin_names) const
void SpineSkeletonDataResource::set_default_mix(float default_mix) {
this->default_mix = default_mix;
if (animation_state_data) animation_state_data->setDefaultMix(default_mix);
update_mixes();
}
float SpineSkeletonDataResource::get_default_mix() {
@ -226,12 +227,34 @@ float SpineSkeletonDataResource::get_default_mix() {
void SpineSkeletonDataResource::set_animation_mixes(Array animation_mixes) {
this->animation_mixes = animation_mixes;
update_mixes();
}
Array SpineSkeletonDataResource::get_animation_mixes() {
return animation_mixes;
}
void SpineSkeletonDataResource::update_mixes() {
if (!is_skeleton_data_loaded()) return;
animation_state_data->clear();
animation_state_data->setDefaultMix(default_mix);
for (int i = 0; i < animation_mixes.size(); i++) {
Ref<SpineAnimationMix> mix = animation_mixes[i];
spine::Animation *from = skeleton_data->findAnimation(mix->get_from().utf8().ptr());
spine::Animation *to = skeleton_data->findAnimation(mix->get_to().utf8().ptr());
if (!from) {
ERR_PRINT(vformat("Failed to set animation mix %s->%s. Animation %s does not exist in skeleton.", from, to, from));
continue;
}
if (!to) {
ERR_PRINT(vformat("Failed to set animation mix %s->%s. Animation %s does not exist in skeleton.", from, to, to));
continue;
}
animation_state_data->setMix(from, to, mix->get_mix());
}
}
#define CHECK(x) \
if (!is_skeleton_data_loaded()) { \
ERR_PRINT("skeleton data has not loaded yet!"); \

View File

@ -57,6 +57,8 @@ private:
void load_res(spine::Atlas *atlas, const String &json, const Vector<uint8_t> &binary);
void update_mixes();
public:
SpineSkeletonDataResource();
virtual ~SpineSkeletonDataResource();