From 169a04079d45a974c5f4eccceda126e89f46cf20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Vald=C3=A9s?= Date: Thu, 20 Feb 2020 03:58:31 +0100 Subject: [PATCH] Better idle detection for cursor animation --- src/renderer/cursor_renderer.rs | 59 +++++++++++++++++---------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/src/renderer/cursor_renderer.rs b/src/renderer/cursor_renderer.rs index 4791ffe..0a63bb3 100644 --- a/src/renderer/cursor_renderer.rs +++ b/src/renderer/cursor_renderer.rs @@ -109,47 +109,50 @@ impl Corner { } pub fn update(&mut self, font_dimensions: Point, destination: Point, dt: f32) -> bool { + // Update destination if needed if destination != self.previous_destination { self.t = 0.0; self.start_position = self.current_position; self.previous_destination = destination; } - let finished_animating = self.t >= 1.0; // Check first if animation's over, so we still render one frame at t == 1.0 - - if self.t <= 1.0 { - // Calculate window-space destination for corner - let relative_scaled_position: Point = ( - self.relative_position.x * font_dimensions.x, - self.relative_position.y * font_dimensions.y, - ) - .into(); - - let corner_destination = destination + relative_scaled_position; - - // Calculate how much a corner will be lagging behind based on how much it's aligned - // with the direction of motion. Corners in front will move faster than corners in the - // back - let corner_direction = { - let mut d = destination - self.current_position; - d.normalize(); - d - }; + // Check first if animation's over + if self.t > 1.0 { + return false; + } - let direction_alignment = corner_direction.dot(self.relative_position); + // Calculate window-space destination for corner + let relative_scaled_position: Point = ( + self.relative_position.x * font_dimensions.x, + self.relative_position.y * font_dimensions.y, + ) + .into(); + + let corner_destination = destination + relative_scaled_position; + + // Calculate how much a corner will be lagging behind based on how much it's aligned + // with the direction of motion. Corners in front will move faster than corners in the + // back + let corner_direction = { + let mut d = destination - self.current_position; + d.normalize(); + d + }; - self.current_position = ease_point( - ease_linear, - self.start_position, - corner_destination, - self.t); + let direction_alignment = corner_direction.dot(self.relative_position); - let corner_dt = dt * lerp(1.0, 1.0 - CURSOR_TRAIL_SIZE, direction_alignment); + self.current_position = + ease_point(ease_linear, self.start_position, corner_destination, self.t); + if self.t == 1.0 { + // We are at destination, move t out of 0-1 range to stop the animation + self.t = 2.0; + } else { + let corner_dt = dt * lerp(1.0, 1.0 - CURSOR_TRAIL_SIZE, direction_alignment); self.t = (self.t + corner_dt / BASE_ANIMATION_LENGTH_SECONDS).min(1.0) } - !finished_animating + true } }