Browse Source

[godot] SpineSkeletonDataResource animation mixes UI functional.

badlogic 3 years ago
parent
commit
22c0f2a9eb

+ 3 - 0
spine-cpp/spine-cpp/include/spine/AnimationStateData.h

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

+ 5 - 0
spine-cpp/spine-cpp/src/spine/AnimationStateData.cpp

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

+ 11 - 1
spine-godot/example/assets/spineboy/spinebody-data-res.tres

@@ -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.atlas" type="SpineAtlasResource" id=1]
 [ext_resource path="res://assets/spineboy/spineboy-pro.json" type="SpineSkeletonFileResource" id=2]
 [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]
 [resource]
 atlas_res = ExtResource( 1 )
 atlas_res = ExtResource( 1 )
 skeleton_file_res = ExtResource( 2 )
 skeleton_file_res = ExtResource( 2 )
 default_mix = 0.2
 default_mix = 0.2
+animation_mixes = [ SubResource( 1 ), SubResource( 2 ) ]

+ 75 - 18
spine-godot/spine_godot/SpineEditorPlugin.cpp

@@ -101,53 +101,110 @@ bool SpineAnimationMixesInspectorPlugin::parse_property(Object *object, Variant:
 	return false;
 	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() {
 void SpineEditorPropertyAnimationMixes::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("add_mix"), &SpineEditorPropertyAnimationMixes::add_mix);
 	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() {
 void SpineEditorPropertyAnimationMixes::add_mix() {
 	if (!skeleton_data.is_valid() || !skeleton_data->is_skeleton_data_loaded() || updating) return;
 	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));
 	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();
 	Array mixes = skeleton_data->get_animation_mixes().duplicate();
 	mixes.push_back(mix);
 	mixes.push_back(mix);
 	skeleton_data->set_animation_mixes(mixes);
 	skeleton_data->set_animation_mixes(mixes);
 	emit_changed(get_edited_property(), 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() {
 void SpineEditorPropertyAnimationMixes::update_property() {
 	if (updating) return;
 	if (updating) return;
 	updating = true;
 	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();
 	Array mixes = skeleton_data->get_animation_mixes();
 	for (int i = 0; i < mixes.size(); i++) {
 	for (int i = 0; i < mixes.size(); i++) {
+		Ref<SpineAnimationMix> mix = mixes[i];
 		auto hbox = memnew(HBoxContainer);
 		auto hbox = memnew(HBoxContainer);
-
-		auto from_label = memnew(Label);
-		from_label->set_text("From:" );
-		hbox->add_child(from_label);
-
-		auto to_label = memnew(Label);
-		to_label->set_text("To: ");
-		hbox->add_child(to_label);
+		hbox->set_h_size_flags(SIZE_FILL);
+
+		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_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);
 		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_text("Add mix");
 	add_mix_button->set_h_size_flags(SIZE_EXPAND_FILL);
 	add_mix_button->set_h_size_flags(SIZE_EXPAND_FILL);
 	add_mix_button->connect("pressed", this, "add_mix");
 	add_mix_button->connect("pressed", this, "add_mix");

+ 2 - 3
spine-godot/spine_godot/SpineEditorPlugin.h

@@ -140,13 +140,12 @@ class SpineEditorPropertyAnimationMixes: public EditorProperty {
 
 
 	Ref<SpineSkeletonDataResource> skeleton_data;
 	Ref<SpineSkeletonDataResource> skeleton_data;
 	VBoxContainer *container;
 	VBoxContainer *container;
-	Button *add_mix_button;
-	Vector<HBoxContainer *> cells;
-	Vector<Button *> delete_mix;
 	bool updating;
 	bool updating;
 
 
 	static void _bind_methods();
 	static void _bind_methods();
 	void add_mix();
 	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:
 public:
 	SpineEditorPropertyAnimationMixes();
 	SpineEditorPropertyAnimationMixes();
 	void setup(Ref<SpineSkeletonDataResource> skeleton_data) { this->skeleton_data = skeleton_data; };
 	void setup(Ref<SpineSkeletonDataResource> skeleton_data) { this->skeleton_data = skeleton_data; };

+ 24 - 1
spine-godot/spine_godot/SpineSkeletonDataResource.cpp

@@ -171,6 +171,7 @@ void SpineSkeletonDataResource::load_res(spine::Atlas *atlas, const String &json
 	}
 	}
 	skeleton_data = data;
 	skeleton_data = data;
 	animation_state_data = new spine::AnimationStateData(data);
 	animation_state_data = new spine::AnimationStateData(data);
+	update_mixes();
 }
 }
 
 
 bool SpineSkeletonDataResource::is_skeleton_data_loaded() const {
 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) {
 void SpineSkeletonDataResource::set_default_mix(float default_mix) {
 	this->default_mix = default_mix;
 	this->default_mix = default_mix;
-	if (animation_state_data) animation_state_data->setDefaultMix(default_mix);
+	update_mixes();
 }
 }
 
 
 float SpineSkeletonDataResource::get_default_mix() {
 float SpineSkeletonDataResource::get_default_mix() {
@@ -226,12 +227,34 @@ float SpineSkeletonDataResource::get_default_mix() {
 
 
 void SpineSkeletonDataResource::set_animation_mixes(Array animation_mixes) {
 void SpineSkeletonDataResource::set_animation_mixes(Array animation_mixes) {
 	this->animation_mixes = animation_mixes;
 	this->animation_mixes = animation_mixes;
+	update_mixes();
 }
 }
 
 
 Array SpineSkeletonDataResource::get_animation_mixes() {
 Array SpineSkeletonDataResource::get_animation_mixes() {
 	return 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)                                      \
 #define CHECK(x)                                      \
 	if (!is_skeleton_data_loaded()) {                   \
 	if (!is_skeleton_data_loaded()) {                   \
 		ERR_PRINT("skeleton data has not loaded yet!"); \
 		ERR_PRINT("skeleton data has not loaded yet!"); \

+ 2 - 0
spine-godot/spine_godot/SpineSkeletonDataResource.h

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