Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 602e999

Browse files
AtlaStarWhalesState
authored andcommittedMar 16, 2025
Allow fractional FPS values in Animation Editor
Closes godotengine#97548. Care also taken to not reopen issue godotengine#92273 by ensuring that the value rounds to the nearest sixteenth. Optionally any factor of 2 should work while ensuring that there isn't error accumulation. Further testing to ensure issue godotengine#91729 isn't reopened, but initial testing suggests that the issue will not reopen with this PR.
1 parent c710b1c commit 602e999

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed
 

‎editor/animation_track_editor.cpp

+15-15
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@
6969
#include "scene/main/window.h"
7070
#include "servers/audio/audio_stream.h"
7171

72-
constexpr double FPS_DECIMAL = 1;
72+
constexpr double FPS_DECIMAL = 1.0;
7373
constexpr double SECOND_DECIMAL = 0.0001;
74+
constexpr double FACTOR = 0.0625;
7475

7576
void AnimationTrackKeyEdit::_bind_methods() {
7677
ClassDB::bind_method(D_METHOD("_update_obj"), &AnimationTrackKeyEdit::_update_obj);
@@ -1821,7 +1822,7 @@ void AnimationTimelineEdit::update_values() {
18211822
}
18221823

18231824
editing = true;
1824-
if (use_fps && animation->get_step() > 0) {
1825+
if (use_fps && animation->get_step() > 0.0) {
18251826
length->set_value(animation->get_length() / animation->get_step());
18261827
length->set_step(FPS_DECIMAL);
18271828
length->set_tooltip_text(TTR("Animation length (frames)"));
@@ -5019,7 +5020,6 @@ void AnimationTrackEditor::_snap_mode_changed(int p_mode) {
50195020
key_edit->set_use_fps(use_fps);
50205021
}
50215022
marker_edit->set_use_fps(use_fps);
5022-
step->set_step(use_fps ? FPS_DECIMAL : SECOND_DECIMAL);
50235023
_update_step_spinbox();
50245024
}
50255025

@@ -5030,12 +5030,10 @@ void AnimationTrackEditor::_update_step_spinbox() {
50305030
step->set_block_signals(true);
50315031

50325032
if (timeline->is_using_fps()) {
5033-
if (animation->get_step() == 0) {
5034-
step->set_value(0);
5033+
if (animation->get_step() == 0.0) {
5034+
step->set_value(0.0);
50355035
} else {
5036-
// The value stored within tscn cannot restored the original FPS due to lack of precision,
5037-
// so the value should be limited to integer.
5038-
step->set_value(Math::round(1.0 / animation->get_step()));
5036+
step->set_value(1.0 / animation->get_step());
50395037
}
50405038

50415039
} else {
@@ -5149,7 +5147,9 @@ void AnimationTrackEditor::_update_step(double p_new_step) {
51495147

51505148
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
51515149
undo_redo->create_action(TTR("Change Animation Step"));
5152-
float step_value = p_new_step;
5150+
// To ensure that the conversion results are consistent between serialization and load, the value is snapped with 0.0625 to be a rational number.
5151+
// step_value must also be less than or equal to 1000 to ensure that no error accumulates due to interactions with retrieving values from inner range.
5152+
double step_value = MIN(1000.0, Math::snapped(p_new_step, FACTOR));
51535153
if (timeline->is_using_fps()) {
51545154
if (step_value != 0.0) {
51555155
step_value = 1.0 / step_value;
@@ -6366,8 +6366,8 @@ void AnimationTrackEditor::goto_prev_step(bool p_from_mouse_event) {
63666366
return;
63676367
}
63686368
float anim_step = animation->get_step();
6369-
if (anim_step == 0) {
6370-
anim_step = 1;
6369+
if (anim_step == 0.0) {
6370+
anim_step = 1.0;
63716371
}
63726372
if (p_from_mouse_event && Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
63736373
// Use more precise snapping when holding Shift.
@@ -6377,8 +6377,8 @@ void AnimationTrackEditor::goto_prev_step(bool p_from_mouse_event) {
63776377

63786378
float pos = timeline->get_play_position();
63796379
pos = Math::snapped(pos - anim_step, anim_step);
6380-
if (pos < 0) {
6381-
pos = 0;
6380+
if (pos < 0.0) {
6381+
pos = 0.0;
63826382
}
63836383
set_anim_pos(pos);
63846384
_timeline_changed(pos, false);
@@ -6389,8 +6389,8 @@ void AnimationTrackEditor::goto_next_step(bool p_from_mouse_event, bool p_timeli
63896389
return;
63906390
}
63916391
float anim_step = animation->get_step();
6392-
if (anim_step == 0) {
6393-
anim_step = 1;
6392+
if (anim_step == 0.0) {
6393+
anim_step = 1.0;
63946394
}
63956395
if (p_from_mouse_event && Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
63966396
// Use more precise snapping when holding Shift.

0 commit comments

Comments
 (0)
Please sign in to comment.