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

Add vertical scrolling to bézier track editor. #83776

Merged
merged 1 commit into from
Oct 23, 2023
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
62 changes: 39 additions & 23 deletions editor/animation_bezier_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

float AnimationBezierTrackEdit::_bezier_h_to_pixel(float p_h) {
float h = p_h;
h = (h - v_scroll) / v_zoom;
h = (h - timeline_v_scroll) / timeline_v_zoom;
h = (get_size().height / 2.0) - h;
return h;
}
Expand Down Expand Up @@ -251,7 +251,9 @@ void AnimationBezierTrackEdit::_notification(int p_what) {

int right_limit = get_size().width;

int vofs = vsep;
track_v_scroll_max = vsep;

int vofs = vsep + track_v_scroll;
int margin = 0;

RBMap<int, Color> subtrack_colors;
Expand Down Expand Up @@ -328,6 +330,7 @@ void AnimationBezierTrackEdit::_notification(int p_what) {
text_buf.draw(get_canvas_item(), string_pos, color);

vofs += h + vsep;
track_v_scroll_max += h + vsep;
}
}

Expand Down Expand Up @@ -439,6 +442,7 @@ void AnimationBezierTrackEdit::_notification(int p_what) {
subtrack_icons[current_track] = track_icons;

vofs += text_buf.get_size().y + vsep;
track_v_scroll_max += text_buf.get_size().y + vsep;
}
}

Expand All @@ -447,20 +451,20 @@ void AnimationBezierTrackEdit::_notification(int p_what) {
{ //guides
float min_left_scale = font->get_height(font_size) + vsep;

float scale = (min_left_scale * 2) * v_zoom;
float scale = (min_left_scale * 2) * timeline_v_zoom;
float step = Math::pow(10.0, Math::round(Math::log(scale / 5.0) / Math::log(10.0))) * 5.0;
scale = Math::snapped(scale, step);

while (scale / v_zoom < min_left_scale * 2) {
while (scale / timeline_v_zoom < min_left_scale * 2) {
scale += step;
}

bool first = true;
int prev_iv = 0;
for (int i = font->get_height(font_size); i < get_size().height; i++) {
float ofs = get_size().height / 2.0 - i;
ofs *= v_zoom;
ofs += v_scroll;
ofs *= timeline_v_zoom;
ofs += timeline_v_scroll;

int iv = int(ofs / scale);
if (ofs < 0) {
Expand Down Expand Up @@ -908,9 +912,9 @@ void AnimationBezierTrackEdit::gui_input(const Ref<InputEvent> &p_event) {
}

if (Math::is_finite(minimum_value) && Math::is_finite(maximum_value)) {
v_scroll = (maximum_value + minimum_value) / 2.0;
timeline_v_scroll = (maximum_value + minimum_value) / 2.0;
if (maximum_value - minimum_value > CMP_EPSILON) {
v_zoom = (maximum_value - minimum_value) / ((get_size().height - timeline->get_size().height) * 0.9);
timeline_v_zoom = (maximum_value - minimum_value) / ((get_size().height - timeline->get_size().height) * 0.9);
}
}

Expand Down Expand Up @@ -1160,7 +1164,7 @@ void AnimationBezierTrackEdit::gui_input(const Ref<InputEvent> &p_event) {
Array new_point;
new_point.resize(5);

float h = (get_size().height / 2.0 - mb->get_position().y) * v_zoom + v_scroll;
float h = (get_size().height / 2.0 - mb->get_position().y) * timeline_v_zoom + timeline_v_scroll;

new_point[0] = h;
new_point[1] = -0.25;
Expand Down Expand Up @@ -1393,7 +1397,7 @@ void AnimationBezierTrackEdit::gui_input(const Ref<InputEvent> &p_event) {
select_single_attempt = IntPair(-1, -1);
}

float y = (get_size().height / 2.0 - mm->get_position().y) * v_zoom + v_scroll;
float y = (get_size().height / 2.0 - mm->get_position().y) * timeline_v_zoom + timeline_v_scroll;
float x = editor->snap_time(((mm->get_position().x - limit) / timeline->get_zoom_scale()) + timeline->get_value());

if (!read_only) {
Expand Down Expand Up @@ -1422,7 +1426,7 @@ void AnimationBezierTrackEdit::gui_input(const Ref<InputEvent> &p_event) {
}

if ((moving_handle == 1 || moving_handle == -1) && mm.is_valid()) {
float y = (get_size().height / 2.0 - mm->get_position().y) * v_zoom + v_scroll;
float y = (get_size().height / 2.0 - mm->get_position().y) * timeline_v_zoom + timeline_v_scroll;
float x = editor->snap_time((mm->get_position().x - timeline->get_name_limit()) / timeline->get_zoom_scale()) + timeline->get_value();

Vector2 key_pos = Vector2(animation->track_get_key_time(selected_track, moving_handle_key), animation->bezier_track_get_key_value(selected_track, moving_handle_key));
Expand All @@ -1438,7 +1442,7 @@ void AnimationBezierTrackEdit::gui_input(const Ref<InputEvent> &p_event) {
Animation::HandleMode handle_mode = animation->bezier_track_get_key_handle_mode(moving_handle_track, moving_handle_key);

if (handle_mode == Animation::HANDLE_MODE_BALANCED) {
real_t ratio = timeline->get_zoom_scale() * v_zoom;
real_t ratio = timeline->get_zoom_scale() * timeline_v_zoom;
Transform2D xform;
xform.set_scale(Vector2(1.0, 1.0 / ratio));

Expand All @@ -1455,7 +1459,7 @@ void AnimationBezierTrackEdit::gui_input(const Ref<InputEvent> &p_event) {
Animation::HandleMode handle_mode = animation->bezier_track_get_key_handle_mode(moving_handle_track, moving_handle_key);

if (handle_mode == Animation::HANDLE_MODE_BALANCED) {
real_t ratio = timeline->get_zoom_scale() * v_zoom;
real_t ratio = timeline->get_zoom_scale() * timeline_v_zoom;
Transform2D xform;
xform.set_scale(Vector2(1.0, 1.0 / ratio));

Expand All @@ -1475,11 +1479,11 @@ void AnimationBezierTrackEdit::gui_input(const Ref<InputEvent> &p_event) {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(TTR("Move Bezier Points"));
if (moving_handle == -1) {
real_t ratio = timeline->get_zoom_scale() * v_zoom;
real_t ratio = timeline->get_zoom_scale() * timeline_v_zoom;
undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_in_handle", moving_handle_track, moving_handle_key, moving_handle_left, ratio);
undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_in_handle", moving_handle_track, moving_handle_key, animation->bezier_track_get_key_in_handle(moving_handle_track, moving_handle_key), ratio);
} else if (moving_handle == 1) {
real_t ratio = timeline->get_zoom_scale() * v_zoom;
real_t ratio = timeline->get_zoom_scale() * timeline_v_zoom;
undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_out_handle", moving_handle_track, moving_handle_key, moving_handle_right, ratio);
undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_out_handle", moving_handle_track, moving_handle_key, animation->bezier_track_get_key_out_handle(moving_handle_track, moving_handle_key), ratio);
}
Expand All @@ -1491,22 +1495,34 @@ void AnimationBezierTrackEdit::gui_input(const Ref<InputEvent> &p_event) {
}

void AnimationBezierTrackEdit::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
v_scroll += p_scroll_vec.y * v_zoom;
v_scroll = CLAMP(v_scroll, -100000, 100000);
timeline->set_value(timeline->get_value() - p_scroll_vec.x / timeline->get_zoom_scale());
queue_redraw();
Ref<InputEventMouseMotion> mm = p_event;
if (mm.is_valid()) {
if (mm->get_position().x > timeline->get_name_limit()) {
timeline_v_scroll += p_scroll_vec.y * timeline_v_zoom;
timeline_v_scroll = CLAMP(timeline_v_scroll, -100000, 100000);
timeline->set_value(timeline->get_value() - p_scroll_vec.x / timeline->get_zoom_scale());
} else {
track_v_scroll += p_scroll_vec.y;
if (track_v_scroll < -track_v_scroll_max) {
track_v_scroll = -track_v_scroll_max;
} else if (track_v_scroll > 0) {
track_v_scroll = 0;
}
}
queue_redraw();
}
}

void AnimationBezierTrackEdit::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
const float v_zoom_orig = v_zoom;
const float v_zoom_orig = timeline_v_zoom;
Ref<InputEventWithModifiers> iewm = p_event;
if (iewm.is_valid() && iewm->is_alt_pressed()) {
// Alternate zoom (doesn't affect timeline).
v_zoom = CLAMP(v_zoom * p_zoom_factor, 0.000001, 100000);
timeline_v_zoom = CLAMP(timeline_v_zoom * p_zoom_factor, 0.000001, 100000);
} else {
timeline->get_zoom()->set_value(timeline->get_zoom()->get_value() / p_zoom_factor);
}
v_scroll = v_scroll + (p_origin.y - get_size().y / 2.0) * (v_zoom - v_zoom_orig);
timeline_v_scroll = timeline_v_scroll + (p_origin.y - get_size().y / 2.0) * (timeline_v_zoom - v_zoom_orig);
queue_redraw();
}

Expand All @@ -1517,7 +1533,7 @@ void AnimationBezierTrackEdit::_menu_selected(int p_index) {
Array new_point;
new_point.resize(5);

float h = (get_size().height / 2.0 - menu_insert_key.y) * v_zoom + v_scroll;
float h = (get_size().height / 2.0 - menu_insert_key.y) * timeline_v_zoom + timeline_v_scroll;

new_point[0] = h;
new_point[1] = -0.25;
Expand Down
7 changes: 5 additions & 2 deletions editor/animation_bezier_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ class AnimationBezierTrackEdit : public Control {
int solo_track = -1;
bool is_filtered = false;

float v_scroll = 0;
float v_zoom = 1;
float track_v_scroll = 0;
float track_v_scroll_max = 0;

float timeline_v_scroll = 0;
float timeline_v_zoom = 1;

PopupMenu *menu = nullptr;

Expand Down
Loading