Skip to content

Commit 46c2c05

Browse files
committed
Separate fps mode snapping from timeline
1 parent 80f0b33 commit 46c2c05

File tree

2 files changed

+79
-39
lines changed

2 files changed

+79
-39
lines changed

editor/animation_track_editor.cpp

+69-35
Original file line numberDiff line numberDiff line change
@@ -1357,13 +1357,23 @@ float AnimationTimelineEdit::_get_zoom_scale(double p_zoom_value) const {
13571357
}
13581358
}
13591359

1360+
void AnimationTimelineEdit::_timeline_mode_changed(int p_mode) {
1361+
bool use_fps = p_mode == 1;
1362+
set_use_fps_timeline(use_fps);
1363+
if (track_edit->editor->key_edit) {
1364+
track_edit->editor->key_edit->set_use_fps(use_fps);
1365+
}
1366+
track_edit->editor->marker_edit->set_use_fps(use_fps);
1367+
}
1368+
13601369
void AnimationTimelineEdit::_anim_length_changed(double p_new_len) {
13611370
if (editing) {
13621371
return;
13631372
}
13641373

13651374
p_new_len = MAX(SECOND_DECIMAL, p_new_len);
1366-
if (use_fps && animation->get_step() > 0) {
1375+
if (use_fps_timeline && animation->get_step() > 0) {
1376+
p_new_len = Math::snapped(p_new_len, FPS_STEP_FRACTION);
13671377
p_new_len *= animation->get_step();
13681378
}
13691379

@@ -1442,7 +1452,10 @@ void AnimationTimelineEdit::_notification(int p_what) {
14421452
case NOTIFICATION_THEME_CHANGED: {
14431453
add_track->set_icon(get_editor_theme_icon(SNAME("Add")));
14441454
loop->set_icon(get_editor_theme_icon(SNAME("Loop")));
1445-
time_icon->set_texture(get_editor_theme_icon(SNAME("Time")));
1455+
1456+
time_gauge->get_popup()->clear();
1457+
time_gauge->get_popup()->add_icon_item(get_editor_theme_icon(SNAME("Time")), TTR("Second"));
1458+
time_gauge->get_popup()->add_icon_item(get_editor_theme_icon(SNAME("Time")), TTR("FPS"));
14461459

14471460
add_track->get_popup()->clear();
14481461
add_track->get_popup()->add_icon_item(get_editor_theme_icon(SNAME("KeyValue")), TTR("Property Track..."));
@@ -1618,7 +1631,7 @@ void AnimationTimelineEdit::_notification(int p_what) {
16181631
}
16191632
}
16201633

1621-
if (use_fps) {
1634+
if (use_fps_timeline) {
16221635
float step_size = animation->get_step();
16231636
if (step_size > 0) {
16241637
int prev_frame_ofs = -10000000;
@@ -1800,11 +1813,10 @@ void AnimationTimelineEdit::update_values() {
18001813
}
18011814

18021815
editing = true;
1803-
if (use_fps && animation->get_step() > 0.0) {
1816+
if (use_fps_timeline && animation->get_step() > 0.0) {
18041817
length->set_value(animation->get_length() / animation->get_step());
18051818
length->set_step(FPS_DECIMAL);
18061819
length->set_tooltip_text(TTR("Animation length (frames)"));
1807-
time_icon->set_tooltip_text(TTR("Animation length (frames)"));
18081820
if (track_edit) {
18091821
track_edit->editor->_update_key_edit();
18101822
track_edit->editor->marker_edit->_update_key_edit();
@@ -1813,7 +1825,6 @@ void AnimationTimelineEdit::update_values() {
18131825
length->set_value(animation->get_length());
18141826
length->set_step(SECOND_DECIMAL);
18151827
length->set_tooltip_text(TTR("Animation length (seconds)"));
1816-
time_icon->set_tooltip_text(TTR("Animation length (seconds)"));
18171828
}
18181829

18191830
switch (animation->get_loop_mode()) {
@@ -1943,13 +1954,21 @@ void AnimationTimelineEdit::_zoom_callback(float p_zoom_factor, Vector2 p_origin
19431954
get_zoom()->set_value(MAX(0.01, current_zoom_value - (1.0 - p_zoom_factor)));
19441955
}
19451956

1946-
void AnimationTimelineEdit::set_use_fps(bool p_use_fps) {
1947-
use_fps = p_use_fps;
1957+
void AnimationTimelineEdit::set_use_fps_timeline(bool p_use_fps) {
1958+
use_fps_timeline = p_use_fps;
19481959
queue_redraw();
19491960
}
19501961

1951-
bool AnimationTimelineEdit::is_using_fps() const {
1952-
return use_fps;
1962+
bool AnimationTimelineEdit::is_using_fps_timeline() const {
1963+
return use_fps_timeline;
1964+
}
1965+
1966+
void AnimationTimelineEdit::set_use_fps_snap(bool p_use_fps) {
1967+
use_fps_snap = p_use_fps;
1968+
}
1969+
1970+
bool AnimationTimelineEdit::is_using_fps_snap() const {
1971+
return use_fps_snap;
19531972
}
19541973

19551974
void AnimationTimelineEdit::set_hscroll(HScrollBar *p_hscroll) {
@@ -1990,10 +2009,15 @@ AnimationTimelineEdit::AnimationTimelineEdit() {
19902009
expander->set_h_size_flags(SIZE_EXPAND_FILL);
19912010
expander->set_mouse_filter(MOUSE_FILTER_IGNORE);
19922011
len_hb->add_child(expander);
1993-
time_icon = memnew(TextureRect);
1994-
time_icon->set_v_size_flags(SIZE_SHRINK_CENTER);
1995-
time_icon->set_tooltip_text(TTR("Animation length (seconds)"));
1996-
len_hb->add_child(time_icon);
2012+
time_gauge = memnew(OptionButton);
2013+
time_gauge->set_v_size_flags(SIZE_SHRINK_CENTER);
2014+
time_gauge->set_tooltip_text(TTR("Animation length"));
2015+
time_gauge->set_clip_text(true);
2016+
time_gauge->add_item(TTR("Seconds"));
2017+
time_gauge->add_item(TTR("FPS"));
2018+
time_gauge->connect(SceneStringName(item_selected), callable_mp(this, &AnimationTimelineEdit::_timeline_mode_changed));
2019+
time_gauge->set_custom_minimum_size(Vector2(70 * EDSCALE, 0));
2020+
len_hb->add_child(time_gauge);
19972021
length = memnew(EditorSpinSlider);
19982022
length->set_min(SECOND_DECIMAL);
19992023
length->set_max(36000);
@@ -2021,6 +2045,8 @@ AnimationTimelineEdit::AnimationTimelineEdit() {
20212045
panner->set_callbacks(callable_mp(this, &AnimationTimelineEdit::_pan_callback), callable_mp(this, &AnimationTimelineEdit::_zoom_callback));
20222046
panner->set_pan_axis(ViewPanner::PAN_AXIS_HORIZONTAL);
20232047

2048+
time_gauge->select(0);
2049+
20242050
set_layout_direction(Control::LAYOUT_DIRECTION_LTR);
20252051
}
20262052

@@ -3873,26 +3899,39 @@ bool AnimationTrackEditor::has_keying() const {
38733899

38743900
Dictionary AnimationTrackEditor::get_state() const {
38753901
Dictionary state;
3876-
state["fps_mode"] = timeline->is_using_fps();
3902+
state["fps_mode_snap"] = timeline->is_using_fps_snap();
3903+
state["fps_mode_timeline"] = timeline->is_using_fps_timeline();
38773904
state["zoom"] = zoom->get_value();
38783905
state["offset"] = timeline->get_value();
38793906
state["v_scroll"] = scroll->get_v_scroll_bar()->get_value();
38803907
return state;
38813908
}
38823909

38833910
void AnimationTrackEditor::set_state(const Dictionary &p_state) {
3884-
if (p_state.has("fps_mode")) {
3885-
bool fps_mode = p_state["fps_mode"];
3911+
if (p_state.has("fps_mode_snap")) {
3912+
bool fps_mode = p_state["fps_mode_snap"];
38863913
if (fps_mode) {
38873914
snap_mode->select(1);
38883915
} else {
38893916
snap_mode->select(0);
38903917
}
38913918
_snap_mode_changed(snap_mode->get_selected());
38923919
} else {
3893-
snap_mode->select(0);
3920+
snap_mode->select(1);
38943921
_snap_mode_changed(snap_mode->get_selected());
38953922
}
3923+
if (p_state.has("fps_mode_timeline")) {
3924+
bool fps_mode = p_state["fps_mode_timeline"];
3925+
if (fps_mode) {
3926+
timeline->time_gauge->select(1);
3927+
} else {
3928+
timeline->time_gauge->select(0);
3929+
}
3930+
timeline->_timeline_mode_changed(timeline->time_gauge->get_selected());
3931+
} else {
3932+
timeline->time_gauge->select(0);
3933+
timeline->_timeline_mode_changed(timeline->time_gauge->get_selected());
3934+
}
38963935
if (p_state.has("zoom")) {
38973936
zoom->set_value(p_state["zoom"]);
38983937
} else {
@@ -5023,12 +5062,8 @@ void AnimationTrackEditor::_animation_changed() {
50235062

50245063
void AnimationTrackEditor::_snap_mode_changed(int p_mode) {
50255064
bool use_fps = p_mode == 1;
5026-
timeline->set_use_fps(use_fps);
5027-
if (key_edit) {
5028-
key_edit->set_use_fps(use_fps);
5029-
}
5030-
marker_edit->set_use_fps(use_fps);
50315065
// To ensure that the conversion results are consistent between serialization and load, the value is snapped with 0.0625 to be a rational number when FPS mode is used.
5066+
timeline->set_use_fps_snap(use_fps);
50325067
step->set_step(use_fps ? FPS_STEP_FRACTION : SECOND_DECIMAL);
50335068
_update_step_spinbox();
50345069
}
@@ -5039,7 +5074,7 @@ void AnimationTrackEditor::_update_step_spinbox() {
50395074
}
50405075
step->set_block_signals(true);
50415076

5042-
if (timeline->is_using_fps()) {
5077+
if (timeline->is_using_fps_snap()) {
50435078
if (animation->get_step() == 0.0) {
50445079
step->set_value(0.0);
50455080
} else {
@@ -5158,11 +5193,10 @@ void AnimationTrackEditor::_update_step(double p_new_step) {
51585193
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
51595194
undo_redo->create_action(TTR("Change Animation Step"));
51605195
double step_value = p_new_step;
5161-
if (timeline->is_using_fps()) {
5196+
if (timeline->is_using_fps_snap()) {
51625197
if (step_value != 0.0) {
5163-
// 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.
5198+
// 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.
51645199
step_value = 1.0 / MIN(1000.0, p_new_step);
5165-
;
51665200
}
51675201
timeline->queue_redraw();
51685202
}
@@ -5696,7 +5730,7 @@ void AnimationTrackEditor::_update_key_edit() {
56965730
key_edit->animation = animation;
56975731
key_edit->animation_read_only = read_only;
56985732
key_edit->track = selection.front()->key().track;
5699-
key_edit->use_fps = timeline->is_using_fps();
5733+
key_edit->use_fps = timeline->is_using_fps_timeline();
57005734
key_edit->editor = this;
57015735

57025736
int key_id = selection.front()->key().key;
@@ -5743,7 +5777,7 @@ void AnimationTrackEditor::_update_key_edit() {
57435777
multi_key_edit->key_ofs_map = key_ofs_map;
57445778
multi_key_edit->base_map = base_map;
57455779
multi_key_edit->hint = _find_hint_for_track(first_track, base_map[first_track]);
5746-
multi_key_edit->use_fps = timeline->is_using_fps();
5780+
multi_key_edit->use_fps = timeline->is_using_fps_timeline();
57475781
multi_key_edit->root_path = root;
57485782

57495783
EditorNode::get_singleton()->push_item(multi_key_edit);
@@ -7318,13 +7352,10 @@ void AnimationTrackEditor::_update_snap_unit() {
73187352
return; // Avoid zero div.
73197353
}
73207354

7321-
if (timeline->is_using_fps()) {
7355+
if (timeline->is_using_fps_snap()) {
73227356
snap_unit = 1.0 / step->get_value();
73237357
} else {
7324-
double integer;
7325-
double fraction = Math::modf(step->get_value(), &integer);
7326-
fraction = 1.0 / Math::round(1.0 / fraction);
7327-
snap_unit = integer + fraction;
7358+
snap_unit = step->get_value();
73287359
}
73297360
}
73307361

@@ -7900,6 +7931,8 @@ AnimationTrackEditor::AnimationTrackEditor() {
79007931
track_copy_select->set_hide_root(true);
79017932
track_copy_vbox->add_child(track_copy_select);
79027933
track_copy_dialog->connect(SceneStringName(confirmed), callable_mp(this, &AnimationTrackEditor::_edit_menu_pressed).bind(EDIT_COPY_TRACKS_CONFIRM));
7934+
7935+
snap_mode->select(1);
79037936
}
79047937

79057938
AnimationTrackEditor::~AnimationTrackEditor() {
@@ -8170,7 +8203,7 @@ void AnimationMarkerEdit::_update_key_edit() {
81708203
key_edit->animation = animation;
81718204
key_edit->animation_read_only = read_only;
81728205
key_edit->marker_name = *selection.begin();
8173-
key_edit->use_fps = timeline->is_using_fps();
8206+
key_edit->use_fps = timeline->is_using_fps_timeline();
81748207
key_edit->marker_edit = this;
81758208

81768209
EditorNode::get_singleton()->push_item(key_edit);
@@ -8702,6 +8735,7 @@ void AnimationMarkerEdit::update_play_position() {
87028735
}
87038736

87048737
void AnimationMarkerEdit::set_use_fps(bool p_use_fps) {
8738+
timeline->set_use_fps_snap(p_use_fps);
87058739
if (key_edit) {
87068740
key_edit->use_fps = p_use_fps;
87078741
key_edit->notify_property_list_changed();

editor/animation_track_editor.h

+10-4
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class AnimationTimelineEdit : public Range {
204204
HBoxContainer *len_hb = nullptr;
205205
EditorSpinSlider *length = nullptr;
206206
Button *loop = nullptr;
207-
TextureRect *time_icon = nullptr;
207+
OptionButton *time_gauge = nullptr;
208208

209209
MenuButton *add_track = nullptr;
210210
Control *play_position = nullptr; //separate control used to draw so updates for only position changed are much faster
@@ -218,7 +218,8 @@ class AnimationTimelineEdit : public Range {
218218
Rect2 hsize_rect;
219219

220220
bool editing = false;
221-
bool use_fps = false;
221+
bool use_fps_timeline = false;
222+
bool use_fps_snap = true;
222223

223224
Ref<ViewPanner> panner;
224225
void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
@@ -240,6 +241,8 @@ class AnimationTimelineEdit : public Range {
240241
float _get_zoom_scale(double p_zoom_value) const;
241242
void _scroll_to_start();
242243

244+
void _timeline_mode_changed(int p_mode);
245+
243246
protected:
244247
static void _bind_methods();
245248
void _notification(int p_what);
@@ -263,8 +266,11 @@ class AnimationTimelineEdit : public Range {
263266

264267
void update_values();
265268

266-
void set_use_fps(bool p_use_fps);
267-
bool is_using_fps() const;
269+
void set_use_fps_timeline(bool p_use_fps);
270+
bool is_using_fps_timeline() const;
271+
272+
void set_use_fps_snap(bool p_use_fps);
273+
bool is_using_fps_snap() const;
268274

269275
void set_hscroll(HScrollBar *p_hscroll);
270276

0 commit comments

Comments
 (0)