diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-07-16 15:11:28 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-16 15:11:28 +0200 |
commit | cfc9e41222111ba0d87f0777b10091befda3e84a (patch) | |
tree | 925a6063d38815308b67d709df568c11aec6e3a1 | |
parent | 170d2e7b14fcdbd88fd2839bbd3dffd00df5196b (diff) |
Add middle-mouse-scroll (aka autoscroll) for infinite image view mode
-rw-r--r-- | include/ImageViewer.hpp | 8 | ||||
-rw-r--r-- | src/ImageViewer.cpp | 39 |
2 files changed, 42 insertions, 5 deletions
diff --git a/include/ImageViewer.hpp b/include/ImageViewer.hpp index ed3363d..68eaa05 100644 --- a/include/ImageViewer.hpp +++ b/include/ImageViewer.hpp @@ -65,5 +65,13 @@ namespace QuickMedia { sf::Vector2<double> window_size; bool window_size_set = false; + bool middle_mouse_scrolling = false; + double autoscroll_start_y = 0.0; + + sf::Cursor default_cursor; + sf::Cursor size_vertical_cursor; + + bool has_default_cursor; + bool has_size_vertical_cursor; }; }
\ No newline at end of file diff --git a/src/ImageViewer.cpp b/src/ImageViewer.cpp index 574bf94..9777611 100644 --- a/src/ImageViewer.cpp +++ b/src/ImageViewer.cpp @@ -28,6 +28,9 @@ namespace QuickMedia { page_size[i].loaded = false; } page_text.setFillColor(sf::Color::White); + + has_default_cursor = default_cursor.loadFromSystem(sf::Cursor::Arrow); + has_size_vertical_cursor = size_vertical_cursor.loadFromSystem(sf::Cursor::SizeVertical); } bool ImageViewer::render_page(sf::RenderWindow &window, int page, double offset_y) { @@ -118,6 +121,7 @@ namespace QuickMedia { const double frame_delta = frame_timer.restart().asSeconds(); const double scroll_speed_key_input = 450.0; const double scroll_speed_mouse_wheel = 600.0; + const double scroll_speed_autoscroll = 10.0; const double scroll_deaccel = 0.96; if(!window_size_set) { @@ -131,7 +135,8 @@ namespace QuickMedia { sf::Event event; while(window.pollEvent(event)) { if (event.type == sf::Event::Closed) { - //current_page = Page::EXIT; + if(has_default_cursor) + window.setMouseCursor(default_cursor); window.close(); return ImageViewerAction::RETURN; } else if(event.type == sf::Event::Resized) { @@ -148,19 +153,43 @@ namespace QuickMedia { } else if(event.key.code == sf::Keyboard::Down) { scroll_speed -= scroll_speed_key_input * frame_delta; } else if(event.key.code == sf::Keyboard::Escape) { + if(has_default_cursor) + window.setMouseCursor(default_cursor); return ImageViewerAction::RETURN; } else if(event.key.code == sf::Keyboard::I) { return ImageViewerAction::SWITCH_TO_SINGLE_IMAGE_MODE; } } else if(event.type == sf::Event::MouseWheelScrolled && event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel) { scroll_speed += scroll_speed_mouse_wheel * event.mouseWheelScroll.delta * frame_delta; + } else if(event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Button::Middle) { + middle_mouse_scrolling = true; + autoscroll_start_y = sf::Mouse::getPosition(window).y; + if(has_size_vertical_cursor) + window.setMouseCursor(size_vertical_cursor); + + } else if(event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Button::Middle) { + middle_mouse_scrolling = false; + scroll_speed = 0.0; + if(has_default_cursor) + window.setMouseCursor(default_cursor); } } - scroll += scroll_speed; - scroll *= scroll_deaccel; - if(std::abs(scroll) < 0.1) - scroll = 0.0; + if(middle_mouse_scrolling) { + double distance_to_start_y = (double)sf::Mouse::getPosition(window).y - autoscroll_start_y; + scroll_speed = -distance_to_start_y * scroll_speed_autoscroll * frame_delta; + const double max_speed = 100.0; + if(scroll_speed > max_speed) + scroll_speed = max_speed; + if(scroll_speed < -max_speed) + scroll_speed = -max_speed; + scroll += scroll_speed; + } else { + scroll_speed *= scroll_deaccel; + if(std::abs(scroll_speed) < 0.01) + scroll_speed = 0.0; + scroll += scroll_speed; + } min_page_center_dist = 9999999.0; page_closest_to_center = -1; |