Make CursorVFX a trait, tweak cursor trail with cubic easing function

macos-click-through
Jon Valdés 5 years ago
parent e56bddf6b6
commit 7c3585e9a6

@ -25,6 +25,28 @@ pub fn ease_in_out_quad(t: f32) -> f32 {
} }
} }
#[allow(dead_code)]
pub fn ease_in_cubic(t: f32) -> f32 {
t * t * t
}
#[allow(dead_code)]
pub fn ease_out_cubic(t: f32) -> f32 {
let n = t - 1.0;
n * n * n + 1.0
}
#[allow(dead_code)]
pub fn ease_in_out_cubic(t: f32) -> f32 {
let n = 2.0 * t;
if n < 1.0 {
0.5 * n * n * n
} else {
let n = n - 2.0;
0.5 * (n * n * n + 2.0)
}
}
pub fn lerp(start: f32, end: f32, t: f32) -> f32 { pub fn lerp(start: f32, end: f32, t: f32) -> f32 {
start + (end - start) * t start + (end - start) * t
} }

@ -2,26 +2,29 @@ use skulpin::skia_safe::{BlendMode, Canvas, Color, Paint, Point, Rect};
use crate::editor::{Colors, Cursor}; use crate::editor::{Colors, Cursor};
pub trait CursorVFX {
fn update(&mut self, current_cursor_destination: Point, dt: f32) -> bool;
fn restart(&mut self, position: Point);
fn render(&self, _paint: &mut Paint, canvas: &mut Canvas, cursor: &Cursor, colors: &Colors);
}
// TODO -- Make this a trait to have several impls pub struct SonicBoom {
// TODO -- Rename this impl as SonicBoom
pub struct CursorVFX {
pub t: f32, pub t: f32,
pub center_position: Point, pub center_position: Point,
} }
impl CursorVFX { impl CursorVFX for SonicBoom {
pub fn update(&mut self, dt: f32) -> bool { fn update(&mut self, _current_cursor_destination: Point, dt: f32) -> bool {
self.t = (self.t + dt * 5.0).min(1.0); // TODO - speed config self.t = (self.t + dt * 5.0).min(1.0); // TODO - speed config
return self.t < 1.0 return self.t < 1.0;
} }
pub fn restart(&mut self, position: Point) { fn restart(&mut self, position: Point) {
self.t = 0.0; self.t = 0.0;
self.center_position = position; self.center_position = position;
} }
pub fn render(&self, _paint: &mut Paint, canvas: &mut Canvas, cursor: &Cursor, colors: &Colors) { fn render(&self, _paint: &mut Paint, canvas: &mut Canvas, cursor: &Cursor, colors: &Colors) {
if self.t == 1.0 { if self.t == 1.0 {
return; return;
} }
@ -36,9 +39,13 @@ impl CursorVFX {
let size = 40.0; // TODO -- Compute from font size let size = 40.0; // TODO -- Compute from font size
let radius = self.t * size; let radius = self.t * size;
let hr = radius * 0.5; let hr = radius * 0.5;
let rect = Rect::from_xywh(self.center_position.x - hr, self.center_position.y - hr, radius, radius); let rect = Rect::from_xywh(
self.center_position.x - hr,
self.center_position.y - hr,
radius,
radius,
);
canvas.draw_oval(&rect, &paint); canvas.draw_oval(&rect, &paint);
} }
} }

@ -13,7 +13,7 @@ use animation_utils::*;
mod cursor_vfx; mod cursor_vfx;
const BASE_ANIMATION_LENGTH_SECONDS: f32 = 0.06; const BASE_ANIMATION_LENGTH_SECONDS: f32 = 0.06;
const CURSOR_TRAIL_SIZE: f32 = 0.6; const CURSOR_TRAIL_SIZE: f32 = 0.7;
const COMMAND_LINE_DELAY_FRAMES: u64 = 5; const COMMAND_LINE_DELAY_FRAMES: u64 = 5;
const DEFAULT_CELL_PERCENTAGE: f32 = 1.0 / 8.0; const DEFAULT_CELL_PERCENTAGE: f32 = 1.0 / 8.0;
@ -162,7 +162,7 @@ impl Corner {
let direction_alignment = travel_direction.dot(corner_direction); let direction_alignment = travel_direction.dot(corner_direction);
self.current_position = self.current_position =
ease_point(ease_linear, self.start_position, corner_destination, self.t); ease_point(ease_out_cubic, self.start_position, corner_destination, self.t);
if self.t == 1.0 { if self.t == 1.0 {
// We are at destination, move t out of 0-1 range to stop the animation // We are at destination, move t out of 0-1 range to stop the animation
@ -182,7 +182,7 @@ pub struct CursorRenderer {
pub command_line_delay: u64, pub command_line_delay: u64,
blink_status: BlinkStatus, blink_status: BlinkStatus,
previous_cursor_shape: Option<CursorShape>, previous_cursor_shape: Option<CursorShape>,
cursor_vfx: cursor_vfx::CursorVFX, cursor_vfx: Box<dyn cursor_vfx::CursorVFX>,
} }
impl CursorRenderer { impl CursorRenderer {
@ -193,7 +193,7 @@ impl CursorRenderer {
command_line_delay: 0, command_line_delay: 0,
blink_status: BlinkStatus::new(), blink_status: BlinkStatus::new(),
previous_cursor_shape: None, previous_cursor_shape: None,
cursor_vfx: cursor_vfx::CursorVFX{t: 0.0, center_position: Point{x:0.0, y:0.0}}, cursor_vfx: Box::new(cursor_vfx::SonicBoom{t: 0.0, center_position: Point{x:0.0, y:0.0}}),
}; };
renderer.set_cursor_shape(&CursorShape::Block, DEFAULT_CELL_PERCENTAGE); renderer.set_cursor_shape(&CursorShape::Block, DEFAULT_CELL_PERCENTAGE);
renderer renderer
@ -293,7 +293,7 @@ impl CursorRenderer {
let corner_animating = corner.update(font_dimensions, center_destination, dt); let corner_animating = corner.update(font_dimensions, center_destination, dt);
animating |= corner_animating; animating |= corner_animating;
} }
let vfx_animating = self.cursor_vfx.update(dt); let vfx_animating = self.cursor_vfx.update(center_destination, dt);
animating |= vfx_animating; animating |= vfx_animating;
} }

Loading…
Cancel
Save