Exposing cursor vfx parameters as settings

macos-click-through
Jon Valdés 5 years ago
parent 1eadd7fad6
commit 885aca19da

@ -4,11 +4,12 @@ use skulpin::skia_safe::{paint::Style, BlendMode, Canvas, Color, Paint, Point, R
use super::animation_utils::*; use super::animation_utils::*;
use crate::editor::{Colors, Cursor}; use crate::editor::{Colors, Cursor};
use crate::settings::*; use crate::settings::*;
use super::CursorSettings;
pub trait CursorVfx { pub trait CursorVfx {
fn update(&mut self, current_cursor_destination: Point, dt: f32) -> bool; fn update(&mut self, settings: &CursorSettings, current_cursor_destination: Point, dt: f32) -> bool;
fn restart(&mut self, position: Point); fn restart(&mut self, position: Point);
fn render(&self, canvas: &mut Canvas, cursor: &Cursor, colors: &Colors, font_size: (f32, f32)); fn render(&self, settings: &CursorSettings, canvas: &mut Canvas, cursor: &Cursor, colors: &Colors, font_size: (f32, f32));
} }
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
@ -93,7 +94,7 @@ impl PointHighlight {
} }
impl CursorVfx for PointHighlight { impl CursorVfx for PointHighlight {
fn update(&mut self, _current_cursor_destination: Point, dt: f32) -> bool { fn update(&mut self, settings: &CursorSettings, _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
self.t < 1.0 self.t < 1.0
} }
@ -103,7 +104,7 @@ impl CursorVfx for PointHighlight {
self.center_position = position; self.center_position = position;
} }
fn render(&self, canvas: &mut Canvas, cursor: &Cursor, colors: &Colors, font_size: (f32, f32)) { fn render(&self, settings: &CursorSettings, canvas: &mut Canvas, cursor: &Cursor, colors: &Colors, font_size: (f32, f32)) {
if self.t == 1.0 { if self.t == 1.0 {
return; return;
} }
@ -111,7 +112,7 @@ impl CursorVfx for PointHighlight {
paint.set_blend_mode(BlendMode::SrcOver); 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 = ease(ease_in_quad, 255.0, 0.0, self.t) as u8; let alpha = ease(ease_in_quad, settings.vfx_opacity, 0.0, self.t) as u8;
let color = Color::from_argb(alpha, base_color.r(), base_color.g(), base_color.b()); let color = Color::from_argb(alpha, base_color.r(), base_color.g(), base_color.b());
paint.set_color(color); paint.set_color(color);
@ -181,10 +182,9 @@ impl ParticleTrail {
} }
const PARTICLE_DENSITY: f32 = 0.008; const PARTICLE_DENSITY: f32 = 0.008;
const PARTICLE_LIFETIME: f32 = 1.2;
impl CursorVfx for ParticleTrail { impl CursorVfx for ParticleTrail {
fn update(&mut self, current_cursor_dest: Point, dt: f32) -> bool { fn update(&mut self, settings: &CursorSettings, current_cursor_dest: Point, dt: f32) -> bool {
// Update lifetimes and remove dead particles // Update lifetimes and remove dead particles
let mut i = 0; let mut i = 0;
while i < self.particles.len() { while i < self.particles.len() {
@ -232,7 +232,7 @@ impl CursorVfx for ParticleTrail {
let pos = prev_p + travel * (t + 0.3 * rng.next_f32() / particle_count as f32); let pos = prev_p + travel * (t + 0.3 * rng.next_f32() / particle_count as f32);
self.add_particle(pos, speed, t * PARTICLE_LIFETIME); self.add_particle(pos, speed, t * settings.vfx_particle_lifetime);
} }
self.previous_cursor_dest = current_cursor_dest; self.previous_cursor_dest = current_cursor_dest;
@ -244,7 +244,7 @@ impl CursorVfx for ParticleTrail {
fn restart(&mut self, _position: Point) {} fn restart(&mut self, _position: Point) {}
fn render(&self, canvas: &mut Canvas, cursor: &Cursor, colors: &Colors, font_size: (f32, f32)) { fn render(&self, settings: &CursorSettings, canvas: &mut Canvas, cursor: &Cursor, colors: &Colors, font_size: (f32, f32)) {
let mut paint = Paint::new(skulpin::skia_safe::colors::WHITE, None); let mut paint = Paint::new(skulpin::skia_safe::colors::WHITE, None);
match self.trail_mode { match self.trail_mode {
TrailMode::Torpedo | TrailMode::Railgun => { TrailMode::Torpedo | TrailMode::Railgun => {
@ -259,8 +259,8 @@ impl CursorVfx for ParticleTrail {
paint.set_blend_mode(BlendMode::SrcOver); paint.set_blend_mode(BlendMode::SrcOver);
self.particles.iter().for_each(|particle| { self.particles.iter().for_each(|particle| {
let l = particle.lifetime / PARTICLE_LIFETIME; let l = particle.lifetime / settings.vfx_particle_lifetime;
let alpha = (l * 128.0) as u8; let alpha = (l * settings.vfx_opacity) as u8;
let color = Color::from_argb(alpha, base_color.r(), base_color.g(), base_color.b()); let color = Color::from_argb(alpha, base_color.r(), base_color.g(), base_color.b());
paint.set_color(color); paint.set_color(color);

@ -12,30 +12,40 @@ use animation_utils::*;
mod cursor_vfx; mod cursor_vfx;
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;
const STANDARD_CORNERS: &[(f32, f32); 4] = &[(-0.5, -0.5), (0.5, -0.5), (0.5, 0.5), (-0.5, 0.5)]; const STANDARD_CORNERS: &[(f32, f32); 4] = &[(-0.5, -0.5), (0.5, -0.5), (0.5, 0.5), (-0.5, 0.5)];
// ----------------------------------------------------------------------------
#[derive(Clone)] #[derive(Clone)]
pub struct CursorSettings { pub struct CursorSettings {
animation_length: f32, animation_length: f32,
trail_size: f32,
vfx_mode: cursor_vfx::VfxMode, vfx_mode: cursor_vfx::VfxMode,
vfx_opacity: f32,
vfx_particle_lifetime: f32,
} }
pub fn initialize_settings() { pub fn initialize_settings() {
SETTINGS.set(&CursorSettings { SETTINGS.set(&CursorSettings {
animation_length: 0.13, animation_length: 0.13,
trail_size: 0.7,
vfx_mode: cursor_vfx::VfxMode::Disabled, vfx_mode: cursor_vfx::VfxMode::Disabled,
vfx_opacity: 200.0,
vfx_particle_lifetime: 1.2,
}); });
register_nvim_setting!("cursor_animation_length", CursorSettings::animation_length); register_nvim_setting!("cursor_animation_length", CursorSettings::animation_length);
register_nvim_setting!("cursor_trail_size", CursorSettings::trail_size);
register_nvim_setting!("cursor_vfx_mode", CursorSettings::vfx_mode); register_nvim_setting!("cursor_vfx_mode", CursorSettings::vfx_mode);
register_nvim_setting!("cursor_vfx_opacity", CursorSettings::vfx_opacity);
register_nvim_setting!("cursor_vfx_particle_lifetime", CursorSettings::vfx_particle_lifetime);
} }
// ----------------------------------------------------------------------------
enum BlinkState { enum BlinkState {
Waiting, Waiting,
@ -184,7 +194,7 @@ impl Corner {
// 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
self.t = 2.0; self.t = 2.0;
} else { } else {
let corner_dt = dt * lerp(1.0, 1.0 - CURSOR_TRAIL_SIZE, -direction_alignment); let corner_dt = dt * lerp(1.0, 1.0 - settings.trail_size, -direction_alignment);
self.t = (self.t + corner_dt / settings.animation_length).min(1.0) self.t = (self.t + corner_dt / settings.animation_length).min(1.0)
} }
@ -325,7 +335,7 @@ impl CursorRenderer {
} }
let vfx_animating = if let Some(vfx) = self.cursor_vfx.as_mut() { let vfx_animating = if let Some(vfx) = self.cursor_vfx.as_mut() {
vfx.update(center_destination, dt) vfx.update(&settings, center_destination, dt)
}else{ }else{
false false
}; };
@ -362,7 +372,7 @@ impl CursorRenderer {
} }
canvas.restore(); canvas.restore();
if let Some(vfx) = self.cursor_vfx.as_ref() { if let Some(vfx) = self.cursor_vfx.as_ref() {
vfx.render(canvas, &cursor, &default_colors, (font_width, font_height)); vfx.render(&settings, canvas, &cursor, &default_colors, (font_width, font_height));
} }
} }

Loading…
Cancel
Save