From c3d3bc0c07595d716c818d8a98630f509e1cad56 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 23 May 2020 11:38:54 +0200 Subject: [PATCH] More accurate recent mouse velocity --- emigui/src/input.rs | 3 +-- emigui/src/movement_tracker.rs | 8 +++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/emigui/src/input.rs b/emigui/src/input.rs index 403eb793..52ba998b 100644 --- a/emigui/src/input.rs +++ b/emigui/src/input.rs @@ -180,8 +180,7 @@ impl MouseInput { // the user tried to throw } - // TODO: pass current time as argument so we don't have a velocity after mouse up - let velocity = self.pos_tracker.velocity().unwrap_or_default(); + let velocity = self.pos_tracker.velocity_noew(new.time).unwrap_or_default(); MouseInput { down: new.mouse_down && new.mouse_pos.is_some(), diff --git a/emigui/src/movement_tracker.rs b/emigui/src/movement_tracker.rs index 7cf480c9..0b9ddf49 100644 --- a/emigui/src/movement_tracker.rs +++ b/emigui/src/movement_tracker.rs @@ -104,8 +104,14 @@ 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(&self) -> Option { + pub fn velocity_all(&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 {