Browse Source

Merge pull request #31898 from Calinou/animation-editor-add-precise-snapping

Implement precise snapping in the animation editor
Rémi Verschelde 6 years ago
parent
commit
037237f518

+ 15 - 2
editor/plugins/animation_player_editor_plugin.cpp

@@ -477,6 +477,19 @@ void AnimationPlayerEditor::_select_anim_by_name(const String &p_anim) {
 	_animation_selected(idx);
 	_animation_selected(idx);
 }
 }
 
 
+double AnimationPlayerEditor::_get_editor_step() const {
+
+	// Returns the effective snapping value depending on snapping modifiers, or 0 if snapping is disabled.
+	if (track_editor->is_snap_enabled()) {
+		const String current = player->get_assigned_animation();
+		const Ref<Animation> anim = player->get_animation(current);
+		// Use more precise snapping when holding Shift
+		return Input::get_singleton()->is_key_pressed(KEY_SHIFT) ? anim->get_step() * 0.25 : anim->get_step();
+	}
+
+	return 0.0;
+}
+
 void AnimationPlayerEditor::_animation_name_edited() {
 void AnimationPlayerEditor::_animation_name_edited() {
 
 
 	player->stop();
 	player->stop();
@@ -1017,7 +1030,7 @@ void AnimationPlayerEditor::_seek_value_changed(float p_value, bool p_set) {
 
 
 	float pos = CLAMP(anim->get_length() * (p_value / frame->get_max()), 0, anim->get_length());
 	float pos = CLAMP(anim->get_length() * (p_value / frame->get_max()), 0, anim->get_length());
 	if (track_editor->is_snap_enabled()) {
 	if (track_editor->is_snap_enabled()) {
-		pos = Math::stepify(pos, anim->get_step());
+		pos = Math::stepify(pos, _get_editor_step());
 	}
 	}
 
 
 	if (player->is_valid() && !p_set) {
 	if (player->is_valid() && !p_set) {
@@ -1068,7 +1081,7 @@ void AnimationPlayerEditor::_animation_key_editor_seek(float p_pos, bool p_drag)
 	Ref<Animation> anim = player->get_animation(player->get_assigned_animation());
 	Ref<Animation> anim = player->get_animation(player->get_assigned_animation());
 
 
 	updating = true;
 	updating = true;
-	frame->set_value(Math::stepify(p_pos, track_editor->is_snap_enabled() ? anim->get_step() : 0));
+	frame->set_value(Math::stepify(p_pos, _get_editor_step()));
 	updating = false;
 	updating = false;
 	_seek_value_changed(p_pos, !p_drag);
 	_seek_value_changed(p_pos, !p_drag);
 
 

+ 1 - 0
editor/plugins/animation_player_editor_plugin.h

@@ -161,6 +161,7 @@ class AnimationPlayerEditor : public VBoxContainer {
 	} onion;
 	} onion;
 
 
 	void _select_anim_by_name(const String &p_anim);
 	void _select_anim_by_name(const String &p_anim);
+	double _get_editor_step() const;
 	void _play_pressed();
 	void _play_pressed();
 	void _play_from_pressed();
 	void _play_from_pressed();
 	void _play_bw_pressed();
 	void _play_bw_pressed();