Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clamp bezier handle length to half the length of animation #93930

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions editor/animation_bezier_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ void AnimationBezierTrackEdit::gui_input(const Ref<InputEvent> &p_event) {
//insert new point
if (mb->get_position().x >= limit && mb->get_position().x < get_size().width && mb->is_command_or_control_pressed()) {
float h = (get_size().height / 2.0 - mb->get_position().y) * timeline_v_zoom + timeline_v_scroll;
Array new_point = make_default_bezier_key(h);
Array new_point = animation->make_default_bezier_key(h);

real_t time = ((mb->get_position().x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
while (animation->track_find_key(selected_track, time, Animation::FIND_MODE_APPROX) != -1) {
Expand Down Expand Up @@ -1643,19 +1643,6 @@ void AnimationBezierTrackEdit::_zoom_callback(float p_zoom_factor, Vector2 p_ori
queue_redraw();
}

Array AnimationBezierTrackEdit::make_default_bezier_key(float p_value) {
Array new_point;
new_point.resize(5);

new_point[0] = p_value;
new_point[1] = -0.25;
new_point[2] = 0;
new_point[3] = 0.25;
new_point[4] = 0;

return new_point;
}

float AnimationBezierTrackEdit::get_bezier_key_value(Array p_bezier_key_array) {
return p_bezier_key_array[0];
}
Expand All @@ -1675,7 +1662,7 @@ void AnimationBezierTrackEdit::_menu_selected(int p_index) {
time += 0.001;
}
float h = (get_size().height / 2.0 - menu_insert_key.y) * timeline_v_zoom + timeline_v_scroll;
Array new_point = make_default_bezier_key(h);
Array new_point = animation->make_default_bezier_key(h);
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(TTR("Add Bezier Point"));
undo_redo->add_do_method(animation.ptr(), "track_insert_key", selected_track, time, new_point);
Expand Down Expand Up @@ -1877,7 +1864,7 @@ void AnimationBezierTrackEdit::paste_keys(real_t p_ofs, bool p_ofs_valid) {

Variant value = key.value;
if (key.track_type != Animation::TYPE_BEZIER) {
value = make_default_bezier_key(key.value);
value = animation->make_default_bezier_key(key.value);
}

undo_redo->add_do_method(animation.ptr(), "track_insert_key", selected_track, dst_time, value, key.transition);
Expand Down
1 change: 0 additions & 1 deletion editor/animation_bezier_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ class AnimationBezierTrackEdit : public Control {
void _notification(int p_what);

public:
static Array make_default_bezier_key(float p_value);
static float get_bezier_key_value(Array p_bezier_key_array);

virtual String get_tooltip(const Point2 &p_pos) const override;
Expand Down
24 changes: 4 additions & 20 deletions editor/animation_track_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4456,16 +4456,8 @@ AnimationTrackEditor::TrackIndices AnimationTrackEditor::_confirm_insert(InsertD

} break;
case Animation::TYPE_BEZIER: {
Array array;
array.resize(5);
array[0] = p_id.value;
array[1] = -0.25;
array[2] = 0;
array[3] = 0.25;
array[4] = 0;
value = array;
value = animation->make_default_bezier_key(p_id.value);
bezier_edit_icon->set_disabled(false);

} break;
default: {
// Other track types shouldn't use this code path.
Expand Down Expand Up @@ -5267,15 +5259,7 @@ void AnimationTrackEditor::_insert_key_from_track(float p_ofs, int p_track) {
NodePath bp;
Variant value;
_find_hint_for_track(p_track, bp, &value);
Array arr;
arr.resize(5);
arr[0] = value;
arr[1] = -0.25;
arr[2] = 0;
arr[3] = 0.25;
arr[4] = 0;

id.value = arr;
id.value = animation->make_default_bezier_key(value);
} break;
case Animation::TYPE_AUDIO: {
Dictionary ak;
Expand Down Expand Up @@ -5819,7 +5803,7 @@ void AnimationTrackEditor::_anim_duplicate_keys(float p_ofs, bool p_ofs_valid, i
if (key_is_bezier && !track_is_bezier) {
value = AnimationBezierTrackEdit::get_bezier_key_value(value);
} else if (!key_is_bezier && track_is_bezier) {
value = AnimationBezierTrackEdit::make_default_bezier_key(value);
value = animation->make_default_bezier_key(value);
}

undo_redo->add_do_method(animation.ptr(), "track_insert_key", dst_track, dst_time, value, animation->track_get_key_transition(E->key().track, E->key().key));
Expand Down Expand Up @@ -5963,7 +5947,7 @@ void AnimationTrackEditor::_anim_paste_keys(float p_ofs, bool p_ofs_valid, int p
if (key_is_bezier && !track_is_bezier) {
value = AnimationBezierTrackEdit::get_bezier_key_value(value);
} else if (!key_is_bezier && track_is_bezier) {
value = AnimationBezierTrackEdit::make_default_bezier_key(value);
value = animation->make_default_bezier_key(value);
}

undo_redo->add_do_method(animation.ptr(), "track_insert_key", dst_track, dst_time, value, key.transition);
Expand Down
14 changes: 14 additions & 0 deletions scene/resources/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3189,6 +3189,20 @@ StringName Animation::method_track_get_name(int p_track, int p_key_idx) const {
return pm->methods[p_key_idx].method;
}

Array Animation::make_default_bezier_key(float p_value) {
const double max_width = length / 2.0;
Array new_point;
new_point.resize(5);

new_point[0] = p_value;
new_point[1] = MAX(-0.25, -max_width);
new_point[2] = 0;
new_point[3] = MIN(0.25, max_width);
new_point[4] = 0;

return new_point;
}

int Animation::bezier_track_insert_key(int p_track, double p_time, real_t p_value, const Vector2 &p_in_handle, const Vector2 &p_out_handle) {
ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
Track *t = tracks[p_track];
Expand Down
1 change: 1 addition & 0 deletions scene/resources/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ class Animation : public Resource {
void track_set_interpolation_type(int p_track, InterpolationType p_interp);
InterpolationType track_get_interpolation_type(int p_track) const;

Array make_default_bezier_key(float p_value);
int bezier_track_insert_key(int p_track, double p_time, real_t p_value, const Vector2 &p_in_handle, const Vector2 &p_out_handle);
void bezier_track_set_key_value(int p_track, int p_index, real_t p_value);
void bezier_track_set_key_in_handle(int p_track, int p_index, const Vector2 &p_handle, real_t p_balanced_value_time_ratio = 1.0);
Expand Down
Loading