diff --git a/egui/src/input.rs b/egui/src/input.rs index 1a3a2821..a265c5a0 100644 --- a/egui/src/input.rs +++ b/egui/src/input.rs @@ -224,6 +224,12 @@ impl MouseInput { could_be_click = false; } + if self.pressed { + // Start of a drag: we want to track the velocity for during the drag + // and ignore any incoming movement + self.pos_tracker.clear(); + } + if let Some(mouse_pos) = new.mouse_pos { self.pos_tracker.add(new.time, mouse_pos); } else { @@ -232,7 +238,12 @@ impl MouseInput { // the user tried to throw } - let velocity = self.pos_tracker.velocity_noew(new.time).unwrap_or_default(); + self.pos_tracker.flush(new.time); + let velocity = if self.pos_tracker.len() >= 3 && self.pos_tracker.dt() > 0.01 { + self.pos_tracker.velocity().unwrap_or_default() + } else { + Vec2::default() + }; MouseInput { down: new.mouse_down && new.mouse_pos.is_some(), diff --git a/egui/src/movement_tracker.rs b/egui/src/movement_tracker.rs index 0b9ddf49..1c3236c9 100644 --- a/egui/src/movement_tracker.rs +++ b/egui/src/movement_tracker.rs @@ -33,6 +33,15 @@ where self.values.len() } + /// Amount of time contained from start to end in this `MovementTracker` + pub fn dt(&self) -> f32 { + if let (Some(front), Some(back)) = (self.values.front(), self.values.back()) { + (back.0 - front.0) as f32 + } else { + 0.0 + } + } + pub fn values<'a>(&'a self) -> impl Iterator + 'a { self.values.iter().map(|(_time, value)| *value) } @@ -64,7 +73,8 @@ where } } - fn flush(&mut self, now: f64) { + /// Remove samples that are too old + pub fn flush(&mut self, now: f64) { while self.values.len() > self.max_len { self.values.pop_front(); } @@ -104,14 +114,8 @@ where T: std::ops::Sub, Vel: std::ops::Div, { - /// Calculate a smooth velocity (per second) from start until now - pub fn velocity_noew(&mut self, now: f64) -> Option { - self.flush(now); - self.velocity_all() - } - /// Calculate a smooth velocity (per second) over the entire time span - pub fn velocity_all(&self) -> Option { + pub fn velocity(&self) -> Option { if let (Some(first), Some(last)) = (self.values.front(), self.values.back()) { let dt = (last.0 - first.0) as f32; if dt > 0.0 {