Browse Source

Added toggle for loop interpolation based on adolson's code

Christian Dannie Storgaard 9 năm trước cách đây
mục cha
commit
8884b8f519

+ 13 - 1
scene/animation/animation_player.cpp

@@ -538,6 +538,7 @@ void AnimationPlayer::_animation_process_data(PlaybackData &cd,float p_delta,flo
 
 	float len=cd.from->animation->get_length();
 	bool loop=cd.from->animation->has_loop();
+	bool loop_interpolation=cd.from->animation->has_loop_interpolation();
 
 	if (!loop) {
 
@@ -563,10 +564,21 @@ void AnimationPlayer::_animation_process_data(PlaybackData &cd,float p_delta,flo
 
 		}
 
-	} else {
+	} else if (loop_interpolation) {
 
 		next_pos=Math::fposmod(next_pos,len);
 
+	} else {
+		
+		if (next_pos<0 or next_pos>len) {
+			if (!backwards)
+				next_pos=0;
+			else if (backwards)
+				next_pos=len;
+		}
+		// fix delta - not sure if needed here
+		delta=next_pos-cd.pos;
+
 	}
 
 	cd.pos=next_pos;

+ 16 - 1
scene/resources/animation.cpp

@@ -38,6 +38,8 @@ bool Animation::_set(const StringName& p_name, const Variant& p_value) {
 		set_length(p_value);
 	else if (name=="loop")
 		set_loop(p_value);
+	else if (name=="loop_interpolation")
+		set_loop_interpolation(p_value);
 	else if (name=="step")
 		set_step(p_value);
 	else if (name.begins_with("tracks/")) {
@@ -1221,7 +1223,7 @@ T Animation::_interpolate( const Vector< TKey<T> >& p_keys, float p_time,  Inter
 	float c=0;
 	// prepare for all cases of interpolation
 
-	if (loop) {
+	if (loop and loop_interpolation) {
 	// loop
 		if (idx>=0) {
 
@@ -1607,10 +1609,19 @@ void Animation::set_loop(bool p_enabled) {
 	loop=p_enabled;
 	emit_changed();
 }
+void Animation::set_loop_interpolation(bool p_enabled) {
+
+	loop_interpolation=p_enabled;
+	emit_changed();
+}
 bool Animation::has_loop() const {
 
 	return loop;
 }
+bool Animation::has_loop_interpolation() const {
+	
+	return loop_interpolation;
+}
 
 void Animation::track_move_up(int p_track) {
 
@@ -1689,7 +1700,9 @@ void Animation::_bind_methods() {
 	ObjectTypeDB::bind_method(_MD("get_length"),&Animation::get_length);
 
 	ObjectTypeDB::bind_method(_MD("set_loop","enabled"),&Animation::set_loop);
+	ObjectTypeDB::bind_method(_MD("set_loop_interpolation","enabled"),&Animation::set_loop_interpolation);
 	ObjectTypeDB::bind_method(_MD("has_loop"),&Animation::has_loop);
+	ObjectTypeDB::bind_method(_MD("has_loop_interpolation"),&Animation::has_loop_interpolation);
 
 	ObjectTypeDB::bind_method(_MD("set_step","size_sec"),&Animation::set_step);
 	ObjectTypeDB::bind_method(_MD("get_step"),&Animation::get_step);
@@ -1712,6 +1725,7 @@ void Animation::clear() {
 		memdelete( tracks[i] );
 	tracks.clear();
 	loop=false;
+	loop_interpolation=true;
 	length=1;
 
 }
@@ -1971,6 +1985,7 @@ Animation::Animation() {
 
 	step=0.1;
 	loop=false;
+	loop_interpolation=true;
 	length=1;
 }
 

+ 3 - 0
scene/resources/animation.h

@@ -163,6 +163,7 @@ private:
 	float length;
 	float step;
 	bool loop;
+	bool loop_interpolation;
 
 // bind helpers
 private:
@@ -265,7 +266,9 @@ public:
 	float get_length() const;
 
 	void set_loop(bool p_enabled);
+	void set_loop_interpolation(bool p_enabled);
 	bool has_loop() const;
+	bool has_loop_interpolation() const;
 
 	void set_step(float p_step);
 	float get_step() const;

+ 24 - 0
tools/editor/animation_editor.cpp

@@ -2920,6 +2920,7 @@ void AnimationKeyEditor::_notification(int p_what) {
 				edit_button->connect("pressed",this,"_toggle_edit_curves");
 
 				loop->set_icon(get_icon("Loop","EditorIcons"));
+				loop_interpolation->set_icon(get_icon("LoopInterpolation","EditorIcons"));
 				curve_edit->connect("transition_changed",this,"_curve_transition_changed");
 
 				//edit_button->add_color_override("font_color",get_color("font_color","Tree"));
@@ -3009,6 +3010,7 @@ void AnimationKeyEditor::_update_menu() {
 
 		length->set_val(animation->get_length());
 		loop->set_pressed(animation->has_loop());
+		loop_interpolation->set_pressed(animation->has_loop_interpolation());
 		step->set_val(animation->get_step());
 	}
 
@@ -3459,6 +3461,21 @@ void AnimationKeyEditor::_animation_loop_changed() {
 
 }
 
+void AnimationKeyEditor::_animation_loop_interpolation_changed() {
+
+	if (updating)
+		return;
+
+	if (!animation.is_null()) {
+
+		undo_redo->create_action(TTR("Change Anim Loop Interpolation"));
+		undo_redo->add_do_method(animation.ptr(),"set_loop_interpolation",loop_interpolation->is_pressed());
+		undo_redo->add_undo_method(animation.ptr(),"set_loop_interpolation",!loop_interpolation->is_pressed());
+		undo_redo->commit_action();
+	}
+
+}
+
 
 void AnimationKeyEditor::_create_value_item(int p_type) {
 
@@ -3744,6 +3761,7 @@ void AnimationKeyEditor::_bind_methods() {
 
 
 	ObjectTypeDB::bind_method(_MD("_animation_loop_changed"),&AnimationKeyEditor::_animation_loop_changed);
+	ObjectTypeDB::bind_method(_MD("_animation_loop_interpolation_changed"),&AnimationKeyEditor::_animation_loop_interpolation_changed);
 	ObjectTypeDB::bind_method(_MD("_animation_len_changed"),&AnimationKeyEditor::_animation_len_changed);
 	ObjectTypeDB::bind_method(_MD("_create_value_item"),&AnimationKeyEditor::_create_value_item);
 	ObjectTypeDB::bind_method(_MD("_pane_drag"),&AnimationKeyEditor::_pane_drag);
@@ -3854,6 +3872,12 @@ AnimationKeyEditor::AnimationKeyEditor() {
 	hb->add_child(loop);
 	loop->set_tooltip(TTR("Enable/Disable looping in animation."));
 
+	loop_interpolation = memnew( ToolButton );
+	loop_interpolation->set_toggle_mode(true);
+	loop_interpolation->connect("pressed",this,"_animation_loop_interpolation_changed");
+	hb->add_child(loop_interpolation);
+	loop_interpolation->set_tooltip(TTR("Enable/Disable interpolation when looping animation."));
+
 	hb->add_child( memnew( VSeparator ) );
 
 	menu_add_track = memnew( MenuButton );

+ 2 - 0
tools/editor/animation_editor.h

@@ -173,6 +173,7 @@ class AnimationKeyEditor : public VBoxContainer  {
 	//MenuButton *menu;
 	SpinBox *length;
 	Button *loop;
+	Button *loop_interpolation;
 	bool keying;
 	ToolButton *edit_button;
 	ToolButton *move_up_button;
@@ -238,6 +239,7 @@ class AnimationKeyEditor : public VBoxContainer  {
 
 	void _animation_len_changed(float p_len);
 	void _animation_loop_changed();
+	void _animation_loop_interpolation_changed();
 	void _step_changed(float p_len);
 
 	struct InsertData {

BIN
tools/editor/icons/icon_loop_interpolation.png