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 {
start + (end - start) * t
}

@ -2,33 +2,36 @@ use skulpin::skia_safe::{BlendMode, Canvas, Color, Paint, Point, Rect};
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
// TODO -- Rename this impl as SonicBoom
pub struct CursorVFX {
pub struct SonicBoom {
pub t: f32,
pub center_position: Point,
}
impl CursorVFX {
pub fn update(&mut self, dt: f32) -> bool {
impl CursorVFX for SonicBoom {
fn update(&mut self, _current_cursor_destination: Point, dt: f32) -> bool {
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.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 {
return;
}
let mut paint = Paint::new(skulpin::skia_safe::colors::WHITE, None);
paint.set_blend_mode(BlendMode::SrcOver);
let base_color : Color = cursor.background(&colors).to_color();
let base_color: Color = cursor.background(&colors).to_color();
let alpha = ((1.0 - self.t) * 255.0) as u8;
let color = Color::from_argb(alpha, base_color.r(), base_color.g(), base_color.b());
paint.set_color(color);
@ -36,9 +39,13 @@ impl CursorVFX {
let size = 40.0; // TODO -- Compute from font size
let radius = self.t * size;
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);
}
}

@ -13,7 +13,7 @@ use animation_utils::*;
mod cursor_vfx;
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 DEFAULT_CELL_PERCENTAGE: f32 = 1.0 / 8.0;
@ -162,7 +162,7 @@ impl Corner {
let direction_alignment = travel_direction.dot(corner_direction);
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 {
// 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,
blink_status: BlinkStatus,
previous_cursor_shape: Option<CursorShape>,
cursor_vfx: cursor_vfx::CursorVFX,
cursor_vfx: Box<dyn cursor_vfx::CursorVFX>,
}
impl CursorRenderer {
@ -193,7 +193,7 @@ impl CursorRenderer {
command_line_delay: 0,
blink_status: BlinkStatus::new(),
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
@ -293,7 +293,7 @@ impl CursorRenderer {
let corner_animating = corner.update(font_dimensions, center_destination, dt);
animating |= corner_animating;
}
let vfx_animating = self.cursor_vfx.update(dt);
let vfx_animating = self.cursor_vfx.update(center_destination, dt);
animating |= vfx_animating;
}

Loading…
Cancel
Save