Kinetic scrolling (#20)
* Fixed typo in function description * Added vertical kinetic scrolling * Checked off kinetic scrolling from TODO Co-authored-by: Markus Webel <m@rkus.online>
This commit is contained in:
parent
6ca11aff8c
commit
6b30e21f4e
3 changed files with 26 additions and 3 deletions
2
TODO.md
2
TODO.md
|
@ -18,7 +18,7 @@ TODO-list for the Egui project. If you looking for something to do, look here.
|
||||||
* [x] Scroll-wheel input
|
* [x] Scroll-wheel input
|
||||||
* [x] Drag background to scroll
|
* [x] Drag background to scroll
|
||||||
* [ ] Horizontal scrolling
|
* [ ] Horizontal scrolling
|
||||||
* [ ] Kinetic scrolling
|
* [X] Kinetic scrolling
|
||||||
* [ ] Text
|
* [ ] Text
|
||||||
* [ ] Unicode
|
* [ ] Unicode
|
||||||
* [ ] Shared mutable expanding texture map?
|
* [ ] Shared mutable expanding texture map?
|
||||||
|
|
|
@ -32,7 +32,7 @@ impl State {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An area on the screen that can be move by dragging.
|
/// An area on the screen that can be moved by dragging.
|
||||||
///
|
///
|
||||||
/// This forms the base of the `Window` container.
|
/// This forms the base of the `Window` container.
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
|
|
@ -8,6 +8,10 @@ pub(crate) struct State {
|
||||||
offset: Vec2,
|
offset: Vec2,
|
||||||
|
|
||||||
show_scroll: bool,
|
show_scroll: bool,
|
||||||
|
|
||||||
|
/// Momentum, used for kinetic scrolling
|
||||||
|
#[cfg_attr(feature = "serde", serde(skip))]
|
||||||
|
pub vel: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for State {
|
impl Default for State {
|
||||||
|
@ -15,6 +19,7 @@ impl Default for State {
|
||||||
Self {
|
Self {
|
||||||
offset: Vec2::zero(),
|
offset: Vec2::zero(),
|
||||||
show_scroll: false,
|
show_scroll: false,
|
||||||
|
vel: Vec2::zero(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,8 +158,26 @@ impl Prepared {
|
||||||
if content_is_too_small {
|
if content_is_too_small {
|
||||||
// Drag contents to scroll (for touch screens mostly):
|
// Drag contents to scroll (for touch screens mostly):
|
||||||
let content_response = ui.interact(inner_rect, id.with("area"), Sense::drag());
|
let content_response = ui.interact(inner_rect, id.with("area"), Sense::drag());
|
||||||
|
|
||||||
|
let input = ui.input();
|
||||||
if content_response.active {
|
if content_response.active {
|
||||||
state.offset.y -= ui.input().mouse.delta.y;
|
state.offset.y -= input.mouse.delta.y;
|
||||||
|
state.vel = input.mouse.velocity;
|
||||||
|
} else {
|
||||||
|
let stop_speed = 20.0; // Pixels per second.
|
||||||
|
let friction_coeff = 1000.0; // Pixels per second squared.
|
||||||
|
let dt = input.unstable_dt;
|
||||||
|
|
||||||
|
let friction = friction_coeff * dt;
|
||||||
|
if friction > state.vel.length() || state.vel.length() < stop_speed {
|
||||||
|
state.vel = Vec2::zero();
|
||||||
|
} else {
|
||||||
|
state.vel -= friction * state.vel.normalized();
|
||||||
|
// Offset has an inverted coordinate system compared to
|
||||||
|
// the velocity, so we subtract it instead of adding it
|
||||||
|
state.offset.y -= state.vel.y * dt;
|
||||||
|
ui.ctx().request_repaint();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue