From 2a364f52f194c853f2cb62122bf84039ab3e1117 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 29 May 2021 13:46:14 +0200 Subject: Readd body touch scroll --- TODO | 1 - src/Body.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/TODO b/TODO index c103c0a..1968ffd 100644 --- a/TODO +++ b/TODO @@ -131,6 +131,5 @@ Notification race condition when fetching the first notifications page and recei Submit on notifications item in matrix should jump to the message in the room. Notifications should load their replied-to-message. Readd copying of Text in Body copy constructor. Find out why we need to make the text dirty on copy. -Readd touch scrolling to Body. Body items that are no longer visible should stop their thumbnail download/creation (moving to bottom of file-manager is very slow). Fix body flickering when moving up and there is a new local image to load. It happens because we have no idea how large the thumbnail is before loading it. \ No newline at end of file diff --git a/src/Body.cpp b/src/Body.cpp index bc56ef8..24cf483 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -450,6 +450,7 @@ namespace QuickMedia { elapsed_time_sec = draw_timer.getElapsedTime().asSeconds(); const int prev_num_visible_items = num_visible_items; + const bool prev_items_cut_off = top_cut_off || bottom_cut_off; num_visible_items = 0; first_visible_item = -1; last_visible_item = -1; @@ -478,6 +479,58 @@ namespace QuickMedia { return; } + if(is_touch_enabled()) { + const sf::Vector2f mouse_pos_diff(mouse_pos_raw.x - mouse_pos.x, mouse_pos_raw.y - mouse_pos.y); + const float move_speed = 35.0f; + sf::Vector2f prev_mouse_pos = mouse_pos; + mouse_pos.x += (mouse_pos_diff.x * std::min(1.0f, frame_time * move_speed)); + mouse_pos.y += (mouse_pos_diff.y * std::min(1.0f, frame_time * move_speed)); + const sf::Vector2f mouse_pos_diff_smooth = mouse_pos - prev_mouse_pos; + + sf::Vector2f mouse_pos_raw_diff(mouse_pos_raw.x - prev_mouse_pos_raw.x, mouse_pos_raw.y - prev_mouse_pos_raw.y); + prev_mouse_pos_raw = mouse_pos_raw; + + if(prev_items_cut_off) { + if(mouse_left_pressed) { + page_scroll += mouse_pos_diff_smooth.y; + mouse_scroll_accel = sf::Vector2f(mouse_pos_raw_diff.x, mouse_pos_raw_diff.y); + mouse_scroll_accel *= (float)((double)frame_time * 120.0); + } else { + page_scroll += mouse_scroll_accel.y; + } + } + + if(mouse_scroll_accel.y > 0.1f && first_visible_item != -1) { + selected_item = first_visible_item; + // TODO: Cache this + if(on_top_reached && get_previous_visible_item(selected_item) == -1) + on_top_reached(); + } else if(mouse_scroll_accel.y < -0.1f && last_visible_item != -1) { + selected_item = last_visible_item; + // TODO: Cache this + if(on_bottom_reached && get_next_visible_item(selected_item) == -1) + on_bottom_reached(); + } + + if(!mouse_left_pressed) { + const float scroll_deaccel = 1.02f; + double deaccel = scroll_deaccel * (1.0 + frame_time); + if(deaccel > 0.0001) { + mouse_scroll_accel.x /= deaccel; + if(fabs(mouse_scroll_accel.x) < 0.0001) + mouse_scroll_accel.x = 0.0; + + mouse_scroll_accel.y /= deaccel; + if(fabs(mouse_scroll_accel.y) < 0.0001) + mouse_scroll_accel.y = 0.0; + } else { + deaccel = 0.0; + mouse_scroll_accel.x = 0.0; + mouse_scroll_accel.y = 0.0; + } + } + } + const int selected_prev_item = get_previous_visible_item(selected_item); const bool selected_merge_with_previous = selected_prev_item != -1 && body_item_merge_handler && body_item_merge_handler(items[selected_prev_item].get(), items[selected_item].get()); get_item_height(items[selected_item].get(), size.x, true, true, selected_merge_with_previous, selected_item); @@ -544,6 +597,7 @@ namespace QuickMedia { } pos.y += selected_scrolled; + const float pos_y_start = pos.y; BodyItem *prev_body_item = nullptr; const double height_move_speed = 1000.0f; @@ -653,6 +707,17 @@ namespace QuickMedia { window.setView(prev_view); + // TODO: Move up where scroll is limited? then we wont delay this by 1 frame creating a small scroll overflow only in the opposite direction of attach side + if(attach_side == AttachSide::TOP) { + const float body_total_height = pos.y - pos_y_start; + if(top_cut_off && !bottom_cut_off && body_total_height > body_size.y) + page_scroll = -(body_total_height - body_size.y); + } else { + const float body_total_height = pos_y_start - pos.y; + if(bottom_cut_off && !top_cut_off && body_total_height > body_size.y) + page_scroll = (body_total_height - body_size.y); + } + const float item_target_top_diff = item_background_target_pos_y; const float item_target_bottom_diff = (item_background_target_pos_y + item_background_target_height + spacing_y) - body_size.y; if(((body_size_changed && attach_side == AttachSide::BOTTOM) || selected_item_diff < 0) && item_target_top_diff < 0.0f) { -- cgit v1.2.3