diff --git a/src/lib.rs b/src/lib.rs index 09f798b..e0ef94c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,11 +25,13 @@ impl Plugin for ScrollViewPlugin { create_scroll_view, update_size, scroll_events, + fling_update, scroll_update, ) .chain(), ) - .add_observer(on_drag); + .add_observer(on_drag) + .add_observer(on_drag_end); } } @@ -40,12 +42,21 @@ pub struct ScrollView { /// Field which control speed of the scrolling. /// Could be negative number to implement invert scroll pub scroll_speed: f32, + /// Amount of friction to apply to slow down the fling + pub friction: f32, + /// Current vertical velocity + velocity: f32, + /// Drag delta for fling + drag_delta: Option, } impl Default for ScrollView { fn default() -> Self { Self { scroll_speed: 1200.0, + friction: 4.2, + velocity: 0.0, + drag_delta: None, } } } @@ -148,12 +159,39 @@ fn update_size( } } +#[derive(Debug, Reflect)] +struct DragDelta { + /// Sum of drag deltas since last reset + pub diff: Vec2, + /// Last time when drag_delta was added to velocity and reset + pub time: f32, +} + fn on_drag( mut drag: Trigger>, - mut q: Query<&Children, With>, + mut q: Query<(&mut ScrollView, &mut Children)>, mut content_q: Query<&mut ScrollableContent>, + time: Res