Skip to content

Commit 88a866f

Browse files
committed
Add separate timeline snapping control to Animation Editor
1 parent 88197d4 commit 88a866f

6 files changed

+47
-33
lines changed

editor/animation_bezier_editor.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@ void AnimationBezierTrackEdit::_menu_selected(int p_index) {
16601660
switch (p_index) {
16611661
case MENU_KEY_INSERT: {
16621662
if (animation->get_track_count() > 0) {
1663-
if (editor->snap->is_pressed() && editor->step->get_value() != 0) {
1663+
if (editor->snap_keys->is_pressed() && editor->step->get_value() != 0) {
16641664
time = editor->snap_time(time);
16651665
}
16661666
while (animation->track_find_key(selected_track, time, Animation::FIND_MODE_APPROX) != -1) {
@@ -1736,7 +1736,7 @@ void AnimationBezierTrackEdit::duplicate_selected_keys(real_t p_ofs, bool p_ofs_
17361736
real_t insert_pos = p_ofs_valid ? p_ofs : timeline->get_play_position();
17371737

17381738
if (p_ofs_valid) {
1739-
if (editor->snap->is_pressed() && editor->step->get_value() != 0) {
1739+
if (editor->snap_keys->is_pressed() && editor->step->get_value() != 0) {
17401740
insert_pos = editor->snap_time(insert_pos);
17411741
}
17421742
}
@@ -1859,7 +1859,7 @@ void AnimationBezierTrackEdit::paste_keys(real_t p_ofs, bool p_ofs_valid) {
18591859

18601860
float insert_pos = p_ofs_valid ? p_ofs : timeline->get_play_position();
18611861
if (p_ofs_valid) {
1862-
if (editor->snap->is_pressed() && editor->step->get_value() != 0) {
1862+
if (editor->snap_keys->is_pressed() && editor->step->get_value() != 0) {
18631863
insert_pos = editor->snap_time(insert_pos);
18641864
}
18651865
}

editor/animation_track_editor.cpp

+31-16
Original file line numberDiff line numberDiff line change
@@ -3615,7 +3615,8 @@ void AnimationTrackEditor::set_animation(const Ref<Animation> &p_anim, bool p_re
36153615
_update_step_spinbox();
36163616
step->set_block_signals(false);
36173617
step->set_read_only(false);
3618-
snap->set_disabled(false);
3618+
snap_keys->set_disabled(false);
3619+
snap_timeline->set_disabled(false);
36193620
snap_mode->set_disabled(false);
36203621
auto_fit->set_disabled(false);
36213622
auto_fit_bezier->set_disabled(false);
@@ -3636,7 +3637,8 @@ void AnimationTrackEditor::set_animation(const Ref<Animation> &p_anim, bool p_re
36363637
step->set_value(0);
36373638
step->set_block_signals(false);
36383639
step->set_read_only(true);
3639-
snap->set_disabled(true);
3640+
snap_keys->set_disabled(true);
3641+
snap_timeline->set_disabled(true);
36403642
snap_mode->set_disabled(true);
36413643
bezier_edit_icon->set_disabled(true);
36423644
auto_fit->set_disabled(true);
@@ -4555,8 +4557,12 @@ bool AnimationTrackEditor::is_key_clipboard_active() const {
45554557
return key_clipboard.keys.size();
45564558
}
45574559

4558-
bool AnimationTrackEditor::is_snap_enabled() const {
4559-
return snap->is_pressed() ^ Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL);
4560+
bool AnimationTrackEditor::is_snap_timeline_enabled() const {
4561+
return snap_timeline->is_pressed() ^ Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL);
4562+
}
4563+
4564+
bool AnimationTrackEditor::is_snap_keys_enabled() const {
4565+
return snap_keys->is_pressed() ^ Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL);
45604566
}
45614567

45624568
bool AnimationTrackEditor::is_bezier_editor_active() const {
@@ -4896,7 +4902,8 @@ void AnimationTrackEditor::_notification(int p_what) {
48964902
case NOTIFICATION_THEME_CHANGED: {
48974903
zoom_icon->set_texture(get_editor_theme_icon(SNAME("Zoom")));
48984904
bezier_edit_icon->set_icon(get_editor_theme_icon(SNAME("EditBezier")));
4899-
snap->set_icon(get_editor_theme_icon(SNAME("Snap")));
4905+
snap_timeline->set_icon(get_editor_theme_icon(SNAME("SnapTimeline")));
4906+
snap_keys->set_icon(get_editor_theme_icon(SNAME("SnapKeys")));
49004907
view_group->set_icon(get_editor_theme_icon(view_group->is_pressed() ? SNAME("AnimationTrackList") : SNAME("AnimationTrackGroup")));
49014908
selected_filter->set_icon(get_editor_theme_icon(SNAME("AnimationFilter")));
49024909
imported_anim_warning->set_icon(get_editor_theme_icon(SNAME("NodeWarning")));
@@ -5217,7 +5224,7 @@ int AnimationTrackEditor::_get_track_selected() {
52175224
void AnimationTrackEditor::_insert_key_from_track(float p_ofs, int p_track) {
52185225
ERR_FAIL_INDEX(p_track, animation->get_track_count());
52195226

5220-
if (snap->is_pressed() && step->get_value() != 0) {
5227+
if (snap_keys->is_pressed() && step->get_value() != 0) {
52215228
p_ofs = snap_time(p_ofs);
52225229
}
52235230
while (animation->track_find_key(p_track, p_ofs, Animation::FIND_MODE_APPROX) != -1) { // Make sure insertion point is valid.
@@ -5840,7 +5847,7 @@ void AnimationTrackEditor::_anim_duplicate_keys(float p_ofs, bool p_ofs_valid, i
58405847
float insert_pos = p_ofs_valid ? p_ofs : timeline->get_play_position();
58415848

58425849
if (p_ofs_valid) {
5843-
if (snap->is_pressed() && step->get_value() != 0) {
5850+
if (snap_keys->is_pressed() && step->get_value() != 0) {
58445851
insert_pos = snap_time(insert_pos);
58455852
}
58465853
}
@@ -5988,7 +5995,7 @@ void AnimationTrackEditor::_anim_paste_keys(float p_ofs, bool p_ofs_valid, int p
59885995
float insert_pos = p_ofs_valid ? p_ofs : timeline->get_play_position();
59895996

59905997
if (p_ofs_valid) {
5991-
if (snap->is_pressed() && step->get_value() != 0) {
5998+
if (snap_keys->is_pressed() && step->get_value() != 0) {
59925999
insert_pos = snap_time(insert_pos);
59936000
}
59946001
}
@@ -7069,7 +7076,7 @@ void AnimationTrackEditor::_update_snap_unit() {
70697076
}
70707077

70717078
float AnimationTrackEditor::snap_time(float p_value, bool p_relative) {
7072-
if (is_snap_enabled()) {
7079+
if (is_snap_keys_enabled()) {
70737080
if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
70747081
// Use more precise snapping when holding Shift.
70757082
snap_unit *= 0.25;
@@ -7323,13 +7330,21 @@ AnimationTrackEditor::AnimationTrackEditor() {
73237330
bottom_hb->add_child(view_group);
73247331
bottom_hb->add_child(memnew(VSeparator));
73257332

7326-
snap = memnew(Button);
7327-
snap->set_flat(true);
7328-
snap->set_text(TTR("Snap:") + " ");
7329-
bottom_hb->add_child(snap);
7330-
snap->set_disabled(true);
7331-
snap->set_toggle_mode(true);
7332-
snap->set_pressed(true);
7333+
snap_timeline = memnew(Button);
7334+
snap_timeline->set_flat(true);
7335+
bottom_hb->add_child(snap_timeline);
7336+
snap_timeline->set_disabled(true);
7337+
snap_timeline->set_toggle_mode(true);
7338+
snap_timeline->set_pressed(false);
7339+
snap_timeline->set_tooltip_text(TTR("Apply snapping to timeline cursor."));
7340+
7341+
snap_keys = memnew(Button);
7342+
snap_keys->set_flat(true);
7343+
bottom_hb->add_child(snap_keys);
7344+
snap_keys->set_disabled(true);
7345+
snap_keys->set_toggle_mode(true);
7346+
snap_keys->set_pressed(true);
7347+
snap_keys->set_tooltip_text(TTR("Apply snapping to selected key(s)."));
73337348

73347349
step = memnew(EditorSpinSlider);
73357350
step->set_min(0);

editor/animation_track_editor.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ class AnimationTrackEditor : public VBoxContainer {
408408
HSlider *zoom = nullptr;
409409
EditorSpinSlider *step = nullptr;
410410
TextureRect *zoom_icon = nullptr;
411-
Button *snap = nullptr;
411+
Button *snap_keys = nullptr;
412+
Button *snap_timeline = nullptr;
412413
Button *bezier_edit_icon = nullptr;
413414
OptionButton *snap_mode = nullptr;
414415
Button *auto_fit = nullptr;
@@ -728,7 +729,8 @@ class AnimationTrackEditor : public VBoxContainer {
728729
bool is_selection_active() const;
729730
bool is_key_clipboard_active() const;
730731
bool is_moving_selection() const;
731-
bool is_snap_enabled() const;
732+
bool is_snap_timeline_enabled() const;
733+
bool is_snap_keys_enabled() const;
732734
bool is_bezier_editor_active() const;
733735
bool can_add_reset_key() const;
734736
float get_moving_selection_offset() const;

editor/icons/SnapKeys.svg

+1
Loading

editor/icons/SnapTimeline.svg

+1
Loading

editor/plugins/animation_player_editor_plugin.cpp

+7-12
Original file line numberDiff line numberDiff line change
@@ -537,17 +537,12 @@ void AnimationPlayerEditor::_select_anim_by_name(const String &p_anim) {
537537
}
538538

539539
float AnimationPlayerEditor::_get_editor_step() const {
540-
// Returns the effective snapping value depending on snapping modifiers, or 0 if snapping is disabled.
541-
if (track_editor->is_snap_enabled()) {
542-
const String current = player->get_assigned_animation();
543-
const Ref<Animation> anim = player->get_animation(current);
544-
ERR_FAIL_COND_V(!anim.is_valid(), 0.0);
540+
const String current = player->get_assigned_animation();
541+
const Ref<Animation> anim = player->get_animation(current);
542+
ERR_FAIL_COND_V(anim.is_null(), 0.0);
545543

546-
// Use more precise snapping when holding Shift
547-
return Input::get_singleton()->is_key_pressed(Key::SHIFT) ? anim->get_step() * 0.25 : anim->get_step();
548-
}
549-
550-
return 0.0f;
544+
// Use more precise snapping when holding Shift
545+
return Input::get_singleton()->is_key_pressed(Key::SHIFT) ? anim->get_step() * 0.25 : anim->get_step();
551546
}
552547

553548
void AnimationPlayerEditor::_animation_name_edited() {
@@ -1353,7 +1348,7 @@ void AnimationPlayerEditor::_seek_value_changed(float p_value, bool p_timeline_o
13531348
anim = player->get_animation(current);
13541349

13551350
double pos = CLAMP((double)anim->get_length() * (p_value / frame->get_max()), 0, (double)anim->get_length());
1356-
if (track_editor->is_snap_enabled()) {
1351+
if (track_editor->is_snap_timeline_enabled()) {
13571352
pos = Math::snapped(pos, _get_editor_step());
13581353
}
13591354
pos = CLAMP(pos, 0, (double)anim->get_length() - CMP_EPSILON2); // Hack: Avoid fposmod with LOOP_LINEAR.
@@ -1471,7 +1466,7 @@ void AnimationPlayerEditor::_animation_key_editor_seek(float p_pos, bool p_timel
14711466
}
14721467

14731468
updating = true;
1474-
frame->set_value(Math::snapped(p_pos, _get_editor_step()));
1469+
frame->set_value(track_editor->is_snap_timeline_enabled() ? Math::snapped(p_pos, _get_editor_step()) : p_pos);
14751470
updating = false;
14761471
_seek_value_changed(p_pos, p_timeline_only);
14771472
}

0 commit comments

Comments
 (0)