mirror of https://github.com/sgoudham/neovide.git
Husky (#277)
* add husky dev dep * clippy suggestions * bump version * save cargo lock * fix epsilon issue for linux * forgot to save * my mistakemacos-click-through
parent
92757ea70c
commit
1892073c8a
@ -1,434 +1,434 @@
|
|||||||
use log::error;
|
use log::error;
|
||||||
use skulpin::skia_safe::{paint::Style, BlendMode, Canvas, Color, Paint, Point, Rect};
|
use skulpin::skia_safe::{paint::Style, BlendMode, Canvas, Color, Paint, Point, Rect};
|
||||||
|
|
||||||
use super::animation_utils::*;
|
use super::animation_utils::*;
|
||||||
use super::CursorSettings;
|
use super::CursorSettings;
|
||||||
use crate::editor::{Colors, Cursor};
|
use crate::editor::{Colors, Cursor};
|
||||||
use crate::settings::*;
|
use crate::settings::*;
|
||||||
|
|
||||||
pub trait CursorVfx {
|
pub trait CursorVfx {
|
||||||
fn update(
|
fn update(
|
||||||
&mut self,
|
&mut self,
|
||||||
settings: &CursorSettings,
|
settings: &CursorSettings,
|
||||||
current_cursor_destination: Point,
|
current_cursor_destination: Point,
|
||||||
font_size: (f32, f32),
|
font_size: (f32, f32),
|
||||||
dt: f32,
|
dt: f32,
|
||||||
) -> bool;
|
) -> bool;
|
||||||
fn restart(&mut self, position: Point);
|
fn restart(&mut self, position: Point);
|
||||||
fn render(
|
fn render(
|
||||||
&self,
|
&self,
|
||||||
settings: &CursorSettings,
|
settings: &CursorSettings,
|
||||||
canvas: &mut Canvas,
|
canvas: &mut Canvas,
|
||||||
cursor: &Cursor,
|
cursor: &Cursor,
|
||||||
colors: &Colors,
|
colors: &Colors,
|
||||||
font_size: (f32, f32),
|
font_size: (f32, f32),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Clone, PartialEq)]
|
||||||
pub enum HighlightMode {
|
pub enum HighlightMode {
|
||||||
SonicBoom,
|
SonicBoom,
|
||||||
Ripple,
|
Ripple,
|
||||||
Wireframe,
|
Wireframe,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Clone, PartialEq)]
|
||||||
pub enum TrailMode {
|
pub enum TrailMode {
|
||||||
Railgun,
|
Railgun,
|
||||||
Torpedo,
|
Torpedo,
|
||||||
PixieDust,
|
PixieDust,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Clone, PartialEq)]
|
||||||
pub enum VfxMode {
|
pub enum VfxMode {
|
||||||
Highlight(HighlightMode),
|
Highlight(HighlightMode),
|
||||||
Trail(TrailMode),
|
Trail(TrailMode),
|
||||||
Disabled,
|
Disabled,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromValue for VfxMode {
|
impl FromValue for VfxMode {
|
||||||
fn from_value(&mut self, value: Value) {
|
fn from_value(&mut self, value: Value) {
|
||||||
if value.is_str() {
|
if value.is_str() {
|
||||||
*self = match value.as_str().unwrap() {
|
*self = match value.as_str().unwrap() {
|
||||||
"sonicboom" => VfxMode::Highlight(HighlightMode::SonicBoom),
|
"sonicboom" => VfxMode::Highlight(HighlightMode::SonicBoom),
|
||||||
"ripple" => VfxMode::Highlight(HighlightMode::Ripple),
|
"ripple" => VfxMode::Highlight(HighlightMode::Ripple),
|
||||||
"wireframe" => VfxMode::Highlight(HighlightMode::Wireframe),
|
"wireframe" => VfxMode::Highlight(HighlightMode::Wireframe),
|
||||||
"railgun" => VfxMode::Trail(TrailMode::Railgun),
|
"railgun" => VfxMode::Trail(TrailMode::Railgun),
|
||||||
"torpedo" => VfxMode::Trail(TrailMode::Torpedo),
|
"torpedo" => VfxMode::Trail(TrailMode::Torpedo),
|
||||||
"pixiedust" => VfxMode::Trail(TrailMode::PixieDust),
|
"pixiedust" => VfxMode::Trail(TrailMode::PixieDust),
|
||||||
"" => VfxMode::Disabled,
|
"" => VfxMode::Disabled,
|
||||||
value => {
|
value => {
|
||||||
error!("Expected a VfxMode name, but received {:?}", value);
|
error!("Expected a VfxMode name, but received {:?}", value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
error!("Expected a VfxMode string, but received {:?}", value);
|
error!("Expected a VfxMode string, but received {:?}", value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<VfxMode> for Value {
|
impl From<VfxMode> for Value {
|
||||||
fn from(mode: VfxMode) -> Self {
|
fn from(mode: VfxMode) -> Self {
|
||||||
match mode {
|
match mode {
|
||||||
VfxMode::Highlight(HighlightMode::SonicBoom) => Value::from("sonicboom"),
|
VfxMode::Highlight(HighlightMode::SonicBoom) => Value::from("sonicboom"),
|
||||||
VfxMode::Highlight(HighlightMode::Ripple) => Value::from("ripple"),
|
VfxMode::Highlight(HighlightMode::Ripple) => Value::from("ripple"),
|
||||||
VfxMode::Highlight(HighlightMode::Wireframe) => Value::from("wireframe"),
|
VfxMode::Highlight(HighlightMode::Wireframe) => Value::from("wireframe"),
|
||||||
VfxMode::Trail(TrailMode::Railgun) => Value::from("railgun"),
|
VfxMode::Trail(TrailMode::Railgun) => Value::from("railgun"),
|
||||||
VfxMode::Trail(TrailMode::Torpedo) => Value::from("torpedo"),
|
VfxMode::Trail(TrailMode::Torpedo) => Value::from("torpedo"),
|
||||||
VfxMode::Trail(TrailMode::PixieDust) => Value::from("pixiedust"),
|
VfxMode::Trail(TrailMode::PixieDust) => Value::from("pixiedust"),
|
||||||
VfxMode::Disabled => Value::from(""),
|
VfxMode::Disabled => Value::from(""),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_cursor_vfx(mode: &VfxMode) -> Option<Box<dyn CursorVfx>> {
|
pub fn new_cursor_vfx(mode: &VfxMode) -> Option<Box<dyn CursorVfx>> {
|
||||||
match mode {
|
match mode {
|
||||||
VfxMode::Highlight(mode) => Some(Box::new(PointHighlight::new(mode))),
|
VfxMode::Highlight(mode) => Some(Box::new(PointHighlight::new(mode))),
|
||||||
VfxMode::Trail(mode) => Some(Box::new(ParticleTrail::new(mode))),
|
VfxMode::Trail(mode) => Some(Box::new(ParticleTrail::new(mode))),
|
||||||
VfxMode::Disabled => None,
|
VfxMode::Disabled => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PointHighlight {
|
pub struct PointHighlight {
|
||||||
t: f32,
|
t: f32,
|
||||||
center_position: Point,
|
center_position: Point,
|
||||||
mode: HighlightMode,
|
mode: HighlightMode,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PointHighlight {
|
impl PointHighlight {
|
||||||
pub fn new(mode: &HighlightMode) -> PointHighlight {
|
pub fn new(mode: &HighlightMode) -> PointHighlight {
|
||||||
PointHighlight {
|
PointHighlight {
|
||||||
t: 0.0,
|
t: 0.0,
|
||||||
center_position: Point::new(0.0, 0.0),
|
center_position: Point::new(0.0, 0.0),
|
||||||
mode: mode.clone(),
|
mode: mode.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CursorVfx for PointHighlight {
|
impl CursorVfx for PointHighlight {
|
||||||
fn update(
|
fn update(
|
||||||
&mut self,
|
&mut self,
|
||||||
_settings: &CursorSettings,
|
_settings: &CursorSettings,
|
||||||
_current_cursor_destination: Point,
|
_current_cursor_destination: Point,
|
||||||
_font_size: (f32, f32),
|
_font_size: (f32, f32),
|
||||||
dt: f32,
|
dt: f32,
|
||||||
) -> bool {
|
) -> 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
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render(
|
fn render(
|
||||||
&self,
|
&self,
|
||||||
settings: &CursorSettings,
|
settings: &CursorSettings,
|
||||||
canvas: &mut Canvas,
|
canvas: &mut Canvas,
|
||||||
cursor: &Cursor,
|
cursor: &Cursor,
|
||||||
colors: &Colors,
|
colors: &Colors,
|
||||||
font_size: (f32, f32),
|
font_size: (f32, f32),
|
||||||
) {
|
) {
|
||||||
if self.t == 1.0 {
|
if (self.t - 1.0).abs() < std::f32::EPSILON {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut paint = Paint::new(skulpin::skia_safe::colors::WHITE, None);
|
let mut paint = Paint::new(skulpin::skia_safe::colors::WHITE, None);
|
||||||
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, settings.vfx_opacity, 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);
|
||||||
|
|
||||||
let size = 3.0 * font_size.1;
|
let size = 3.0 * font_size.1;
|
||||||
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(
|
let rect = Rect::from_xywh(
|
||||||
self.center_position.x - hr,
|
self.center_position.x - hr,
|
||||||
self.center_position.y - hr,
|
self.center_position.y - hr,
|
||||||
radius,
|
radius,
|
||||||
radius,
|
radius,
|
||||||
);
|
);
|
||||||
|
|
||||||
match self.mode {
|
match self.mode {
|
||||||
HighlightMode::SonicBoom => {
|
HighlightMode::SonicBoom => {
|
||||||
canvas.draw_oval(&rect, &paint);
|
canvas.draw_oval(&rect, &paint);
|
||||||
}
|
}
|
||||||
HighlightMode::Ripple => {
|
HighlightMode::Ripple => {
|
||||||
paint.set_style(Style::Stroke);
|
paint.set_style(Style::Stroke);
|
||||||
paint.set_stroke_width(font_size.1 * 0.2);
|
paint.set_stroke_width(font_size.1 * 0.2);
|
||||||
canvas.draw_oval(&rect, &paint);
|
canvas.draw_oval(&rect, &paint);
|
||||||
}
|
}
|
||||||
HighlightMode::Wireframe => {
|
HighlightMode::Wireframe => {
|
||||||
paint.set_style(Style::Stroke);
|
paint.set_style(Style::Stroke);
|
||||||
paint.set_stroke_width(font_size.1 * 0.2);
|
paint.set_stroke_width(font_size.1 * 0.2);
|
||||||
canvas.draw_rect(&rect, &paint);
|
canvas.draw_rect(&rect, &paint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct ParticleData {
|
struct ParticleData {
|
||||||
pos: Point,
|
pos: Point,
|
||||||
speed: Point,
|
speed: Point,
|
||||||
rotation_speed: f32,
|
rotation_speed: f32,
|
||||||
lifetime: f32,
|
lifetime: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ParticleTrail {
|
pub struct ParticleTrail {
|
||||||
particles: Vec<ParticleData>,
|
particles: Vec<ParticleData>,
|
||||||
previous_cursor_dest: Point,
|
previous_cursor_dest: Point,
|
||||||
trail_mode: TrailMode,
|
trail_mode: TrailMode,
|
||||||
rng: RngState,
|
rng: RngState,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParticleTrail {
|
impl ParticleTrail {
|
||||||
pub fn new(trail_mode: &TrailMode) -> ParticleTrail {
|
pub fn new(trail_mode: &TrailMode) -> ParticleTrail {
|
||||||
ParticleTrail {
|
ParticleTrail {
|
||||||
particles: vec![],
|
particles: vec![],
|
||||||
previous_cursor_dest: Point::new(0.0, 0.0),
|
previous_cursor_dest: Point::new(0.0, 0.0),
|
||||||
trail_mode: trail_mode.clone(),
|
trail_mode: trail_mode.clone(),
|
||||||
rng: RngState::new(),
|
rng: RngState::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_particle(&mut self, pos: Point, speed: Point, rotation_speed: f32, lifetime: f32) {
|
fn add_particle(&mut self, pos: Point, speed: Point, rotation_speed: f32, lifetime: f32) {
|
||||||
self.particles.push(ParticleData {
|
self.particles.push(ParticleData {
|
||||||
pos,
|
pos,
|
||||||
speed,
|
speed,
|
||||||
rotation_speed,
|
rotation_speed,
|
||||||
lifetime,
|
lifetime,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note this method doesn't keep particles in order
|
// Note this method doesn't keep particles in order
|
||||||
fn remove_particle(&mut self, idx: usize) {
|
fn remove_particle(&mut self, idx: usize) {
|
||||||
self.particles[idx] = self.particles[self.particles.len() - 1].clone();
|
self.particles[idx] = self.particles[self.particles.len() - 1].clone();
|
||||||
self.particles.pop();
|
self.particles.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CursorVfx for ParticleTrail {
|
impl CursorVfx for ParticleTrail {
|
||||||
fn update(
|
fn update(
|
||||||
&mut self,
|
&mut self,
|
||||||
settings: &CursorSettings,
|
settings: &CursorSettings,
|
||||||
current_cursor_dest: Point,
|
current_cursor_dest: Point,
|
||||||
font_size: (f32, f32),
|
font_size: (f32, f32),
|
||||||
dt: f32,
|
dt: f32,
|
||||||
) -> bool {
|
) -> 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() {
|
||||||
let particle: &mut ParticleData = &mut self.particles[i];
|
let particle: &mut ParticleData = &mut self.particles[i];
|
||||||
particle.lifetime -= dt;
|
particle.lifetime -= dt;
|
||||||
if particle.lifetime <= 0.0 {
|
if particle.lifetime <= 0.0 {
|
||||||
self.remove_particle(i);
|
self.remove_particle(i);
|
||||||
} else {
|
} else {
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update particle positions
|
// Update particle positions
|
||||||
for i in 0..self.particles.len() {
|
for i in 0..self.particles.len() {
|
||||||
let particle = &mut self.particles[i];
|
let particle = &mut self.particles[i];
|
||||||
particle.pos += particle.speed * dt;
|
particle.pos += particle.speed * dt;
|
||||||
particle.speed = rotate_vec(particle.speed, dt * particle.rotation_speed);
|
particle.speed = rotate_vec(particle.speed, dt * particle.rotation_speed);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spawn new particles
|
// Spawn new particles
|
||||||
if current_cursor_dest != self.previous_cursor_dest {
|
if current_cursor_dest != self.previous_cursor_dest {
|
||||||
let travel = current_cursor_dest - self.previous_cursor_dest;
|
let travel = current_cursor_dest - self.previous_cursor_dest;
|
||||||
let travel_distance = travel.length();
|
let travel_distance = travel.length();
|
||||||
|
|
||||||
// Increase amount of particles when cursor travels further
|
// Increase amount of particles when cursor travels further
|
||||||
let particle_count = ((travel_distance / font_size.0).powf(1.5)
|
let particle_count = ((travel_distance / font_size.0).powf(1.5)
|
||||||
* settings.vfx_particle_density
|
* settings.vfx_particle_density
|
||||||
* 0.01) as usize;
|
* 0.01) as usize;
|
||||||
|
|
||||||
let prev_p = self.previous_cursor_dest;
|
let prev_p = self.previous_cursor_dest;
|
||||||
|
|
||||||
for i in 0..particle_count {
|
for i in 0..particle_count {
|
||||||
let t = i as f32 / (particle_count as f32);
|
let t = i as f32 / (particle_count as f32);
|
||||||
|
|
||||||
let speed = match self.trail_mode {
|
let speed = match self.trail_mode {
|
||||||
TrailMode::Railgun => {
|
TrailMode::Railgun => {
|
||||||
let phase = t / 3.141592
|
let phase = t / std::f32::consts::PI
|
||||||
* settings.vfx_particle_phase
|
* settings.vfx_particle_phase
|
||||||
* (travel_distance / font_size.0);
|
* (travel_distance / font_size.0);
|
||||||
Point::new(phase.sin(), phase.cos()) * 2.0 * settings.vfx_particle_speed
|
Point::new(phase.sin(), phase.cos()) * 2.0 * settings.vfx_particle_speed
|
||||||
}
|
}
|
||||||
TrailMode::Torpedo => {
|
TrailMode::Torpedo => {
|
||||||
let mut travel_dir = travel;
|
let mut travel_dir = travel;
|
||||||
travel_dir.normalize();
|
travel_dir.normalize();
|
||||||
let mut particle_dir = self.rng.rand_dir_normalized() - travel_dir * 1.5;
|
let mut particle_dir = self.rng.rand_dir_normalized() - travel_dir * 1.5;
|
||||||
particle_dir.normalize();
|
particle_dir.normalize();
|
||||||
particle_dir * settings.vfx_particle_speed
|
particle_dir * settings.vfx_particle_speed
|
||||||
}
|
}
|
||||||
TrailMode::PixieDust => {
|
TrailMode::PixieDust => {
|
||||||
let base_dir = self.rng.rand_dir_normalized();
|
let base_dir = self.rng.rand_dir_normalized();
|
||||||
let dir = Point::new(base_dir.x * 0.5, 0.4 + base_dir.y.abs());
|
let dir = Point::new(base_dir.x * 0.5, 0.4 + base_dir.y.abs());
|
||||||
dir * 3.0 * settings.vfx_particle_speed
|
dir * 3.0 * settings.vfx_particle_speed
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Distribute particles along the travel distance
|
// Distribute particles along the travel distance
|
||||||
let pos = match self.trail_mode {
|
let pos = match self.trail_mode {
|
||||||
TrailMode::Railgun => prev_p + travel * t,
|
TrailMode::Railgun => prev_p + travel * t,
|
||||||
TrailMode::PixieDust | TrailMode::Torpedo => {
|
TrailMode::PixieDust | TrailMode::Torpedo => {
|
||||||
prev_p + travel * self.rng.next_f32() + Point::new(0.0, font_size.1 * 0.5)
|
prev_p + travel * self.rng.next_f32() + Point::new(0.0, font_size.1 * 0.5)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let rotation_speed = match self.trail_mode {
|
let rotation_speed = match self.trail_mode {
|
||||||
TrailMode::Railgun => std::f32::consts::PI * settings.vfx_particle_curl,
|
TrailMode::Railgun => std::f32::consts::PI * settings.vfx_particle_curl,
|
||||||
TrailMode::PixieDust | TrailMode::Torpedo => {
|
TrailMode::PixieDust | TrailMode::Torpedo => {
|
||||||
(self.rng.next_f32() - 0.5)
|
(self.rng.next_f32() - 0.5)
|
||||||
* std::f32::consts::FRAC_PI_2
|
* std::f32::consts::FRAC_PI_2
|
||||||
* settings.vfx_particle_curl
|
* settings.vfx_particle_curl
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
self.add_particle(
|
self.add_particle(
|
||||||
pos,
|
pos,
|
||||||
speed,
|
speed,
|
||||||
rotation_speed,
|
rotation_speed,
|
||||||
t * settings.vfx_particle_lifetime,
|
t * settings.vfx_particle_lifetime,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.previous_cursor_dest = current_cursor_dest;
|
self.previous_cursor_dest = current_cursor_dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep animating as long as there are particles alive
|
// Keep animating as long as there are particles alive
|
||||||
!self.particles.is_empty()
|
!self.particles.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn restart(&mut self, _position: Point) {}
|
fn restart(&mut self, _position: Point) {}
|
||||||
|
|
||||||
fn render(
|
fn render(
|
||||||
&self,
|
&self,
|
||||||
settings: &CursorSettings,
|
settings: &CursorSettings,
|
||||||
canvas: &mut Canvas,
|
canvas: &mut Canvas,
|
||||||
cursor: &Cursor,
|
cursor: &Cursor,
|
||||||
colors: &Colors,
|
colors: &Colors,
|
||||||
font_size: (f32, f32),
|
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 => {
|
||||||
paint.set_style(Style::Stroke);
|
paint.set_style(Style::Stroke);
|
||||||
paint.set_stroke_width(font_size.1 * 0.2);
|
paint.set_stroke_width(font_size.1 * 0.2);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
let base_color: Color = cursor.background(&colors).to_color();
|
let base_color: Color = cursor.background(&colors).to_color();
|
||||||
|
|
||||||
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 / settings.vfx_particle_lifetime;
|
let l = particle.lifetime / settings.vfx_particle_lifetime;
|
||||||
let alpha = (l * settings.vfx_opacity) 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);
|
||||||
|
|
||||||
let radius = match self.trail_mode {
|
let radius = match self.trail_mode {
|
||||||
TrailMode::Torpedo | TrailMode::Railgun => font_size.0 * 0.5 * l,
|
TrailMode::Torpedo | TrailMode::Railgun => font_size.0 * 0.5 * l,
|
||||||
TrailMode::PixieDust => font_size.0 * 0.2,
|
TrailMode::PixieDust => font_size.0 * 0.2,
|
||||||
};
|
};
|
||||||
|
|
||||||
let hr = radius * 0.5;
|
let hr = radius * 0.5;
|
||||||
let rect = Rect::from_xywh(particle.pos.x - hr, particle.pos.y - hr, radius, radius);
|
let rect = Rect::from_xywh(particle.pos.x - hr, particle.pos.y - hr, radius, radius);
|
||||||
|
|
||||||
match self.trail_mode {
|
match self.trail_mode {
|
||||||
TrailMode::Torpedo | TrailMode::Railgun => {
|
TrailMode::Torpedo | TrailMode::Railgun => {
|
||||||
canvas.draw_oval(&rect, &paint);
|
canvas.draw_oval(&rect, &paint);
|
||||||
}
|
}
|
||||||
TrailMode::PixieDust => {
|
TrailMode::PixieDust => {
|
||||||
canvas.draw_rect(&rect, &paint);
|
canvas.draw_rect(&rect, &paint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Random number generator based on http://www.pcg-random.org/
|
// Random number generator based on http://www.pcg-random.org/
|
||||||
struct RngState {
|
struct RngState {
|
||||||
state: u64,
|
state: u64,
|
||||||
inc: u64,
|
inc: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RngState {
|
impl RngState {
|
||||||
fn new() -> RngState {
|
fn new() -> RngState {
|
||||||
RngState {
|
RngState {
|
||||||
state: 0x853C49E6748FEA9Bu64,
|
state: 0x853C49E6748FEA9Bu64,
|
||||||
inc: (0xDA3E39CB94B95BDBu64 << 1) | 1,
|
inc: (0xDA3E39CB94B95BDBu64 << 1) | 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn next(&mut self) -> u32 {
|
fn next(&mut self) -> u32 {
|
||||||
let old_state = self.state;
|
let old_state = self.state;
|
||||||
|
|
||||||
// Implementation copied from:
|
// Implementation copied from:
|
||||||
// https://rust-random.github.io/rand/src/rand_pcg/pcg64.rs.html#103
|
// https://rust-random.github.io/rand/src/rand_pcg/pcg64.rs.html#103
|
||||||
let new_state = old_state
|
let new_state = old_state
|
||||||
.wrapping_mul(6_364_136_223_846_793_005u64)
|
.wrapping_mul(6_364_136_223_846_793_005u64)
|
||||||
.wrapping_add(self.inc);
|
.wrapping_add(self.inc);
|
||||||
|
|
||||||
self.state = new_state;
|
self.state = new_state;
|
||||||
|
|
||||||
const ROTATE: u32 = 59; // 64 - 5
|
const ROTATE: u32 = 59; // 64 - 5
|
||||||
const XSHIFT: u32 = 18; // (5 + 32) / 2
|
const XSHIFT: u32 = 18; // (5 + 32) / 2
|
||||||
const SPARE: u32 = 27; // 64 - 32 - 5
|
const SPARE: u32 = 27; // 64 - 32 - 5
|
||||||
|
|
||||||
let rot = (old_state >> ROTATE) as u32;
|
let rot = (old_state >> ROTATE) as u32;
|
||||||
let xsh = (((old_state >> XSHIFT) ^ old_state) >> SPARE) as u32;
|
let xsh = (((old_state >> XSHIFT) ^ old_state) >> SPARE) as u32;
|
||||||
xsh.rotate_right(rot)
|
xsh.rotate_right(rot)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_f32(&mut self) -> f32 {
|
fn next_f32(&mut self) -> f32 {
|
||||||
let v = self.next();
|
let v = self.next();
|
||||||
|
|
||||||
// In C we'd do ldexp(v, -32) to bring a number in the range [0,2^32) down to [0,1) range.
|
// In C we'd do ldexp(v, -32) to bring a number in the range [0,2^32) down to [0,1) range.
|
||||||
// But as we don't have ldexp in Rust, we're implementing the same idea (subtracting 32
|
// But as we don't have ldexp in Rust, we're implementing the same idea (subtracting 32
|
||||||
// from the floating point exponent) manually.
|
// from the floating point exponent) manually.
|
||||||
|
|
||||||
// First, extract exponent bits
|
// First, extract exponent bits
|
||||||
let float_bits = (v as f64).to_bits();
|
let float_bits = (v as f64).to_bits();
|
||||||
let exponent = (float_bits >> 52) & ((1 << 11) - 1);
|
let exponent = (float_bits >> 52) & ((1 << 11) - 1);
|
||||||
|
|
||||||
// Set exponent for [0-1) range
|
// Set exponent for [0-1) range
|
||||||
let new_exponent = exponent.max(32) - 32;
|
let new_exponent = exponent.max(32) - 32;
|
||||||
|
|
||||||
// Build the new f64 value from the old mantissa and sign, and the new exponent
|
// Build the new f64 value from the old mantissa and sign, and the new exponent
|
||||||
let new_bits = (new_exponent << 52) | (float_bits & 0x801F_FFFF_FFFF_FFFFu64);
|
let new_bits = (new_exponent << 52) | (float_bits & 0x801F_FFFF_FFFF_FFFFu64);
|
||||||
|
|
||||||
f64::from_bits(new_bits) as f32
|
f64::from_bits(new_bits) as f32
|
||||||
}
|
}
|
||||||
|
|
||||||
// Produces a random vector with x and y in the [-1,1) range
|
// Produces a random vector with x and y in the [-1,1) range
|
||||||
// Note: Vector is not normalized.
|
// Note: Vector is not normalized.
|
||||||
fn rand_dir(&mut self) -> Point {
|
fn rand_dir(&mut self) -> Point {
|
||||||
let x = self.next_f32();
|
let x = self.next_f32();
|
||||||
let y = self.next_f32();
|
let y = self.next_f32();
|
||||||
|
|
||||||
Point::new(x * 2.0 - 1.0, y * 2.0 - 1.0)
|
Point::new(x * 2.0 - 1.0, y * 2.0 - 1.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rand_dir_normalized(&mut self) -> Point {
|
fn rand_dir_normalized(&mut self) -> Point {
|
||||||
let mut v = self.rand_dir();
|
let mut v = self.rand_dir();
|
||||||
v.normalize();
|
v.normalize();
|
||||||
v
|
v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rotate_vec(v: Point, rot: f32) -> Point {
|
fn rotate_vec(v: Point, rot: f32) -> Point {
|
||||||
let sin = rot.sin();
|
let sin = rot.sin();
|
||||||
let cos = rot.cos();
|
let cos = rot.cos();
|
||||||
|
|
||||||
Point::new(v.x * cos - v.y * sin, v.x * sin + v.y * cos)
|
Point::new(v.x * cos - v.y * sin, v.x * sin + v.y * cos)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue