Skip to content

Commit

Permalink
Use typesafe grid and pixel units
Browse files Browse the repository at this point in the history
  • Loading branch information
fredizzimo committed Apr 14, 2024
1 parent d2f54c8 commit b83bcc4
Show file tree
Hide file tree
Showing 16 changed files with 534 additions and 538 deletions.
10 changes: 5 additions & 5 deletions src/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use tokio::runtime::{Builder, Runtime};
use winit::event_loop::EventLoopProxy;

use crate::{
cmd_line::CmdLineSettings, dimensions::Dimensions, editor::start_editor, running_tracker::*,
settings::*, window::UserEvent,
cmd_line::CmdLineSettings, editor::start_editor, running_tracker::*, settings::*,
units::GridSize, window::UserEvent,
};
pub use handler::NeovimHandler;
use session::{NeovimInstance, NeovimSession};
Expand Down Expand Up @@ -169,7 +169,7 @@ pub async fn show_error_message(
nvim.echo(prepared_lines, true, vec![]).await
}

async fn launch(handler: NeovimHandler, grid_size: Option<Dimensions>) -> Result<NeovimSession> {
async fn launch(handler: NeovimHandler, grid_size: Option<GridSize<u32>>) -> Result<NeovimSession> {
let neovim_instance = neovim_instance()?;

let session = NeovimSession::new(neovim_instance, handler)
Expand Down Expand Up @@ -215,7 +215,7 @@ async fn launch(handler: NeovimHandler, grid_size: Option<Dimensions>) -> Result

// Triggers loading the user config

let grid_size = grid_size.map_or(DEFAULT_GRID_SIZE, |v| v.clamped_grid_size());
let grid_size = grid_size.map_or(DEFAULT_GRID_SIZE, |v| clamped_grid_size(&v));
let res = session
.neovim
.ui_attach(grid_size.width as i64, grid_size.height as i64, &options)
Expand Down Expand Up @@ -251,7 +251,7 @@ impl NeovimRuntime {
pub fn launch(
&mut self,
event_loop_proxy: EventLoopProxy<UserEvent>,
grid_size: Option<Dimensions>,
grid_size: Option<GridSize<u32>>,
) -> Result<()> {
let handler = start_editor(event_loop_proxy);
let runtime = self.runtime.as_ref().unwrap();
Expand Down
19 changes: 0 additions & 19 deletions src/dimensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,13 @@ use std::{
use serde::{Deserialize, Serialize};
use winit::dpi::PhysicalSize;

use crate::settings;

// Maybe this should be independent from serialization?
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
pub struct Dimensions {
pub width: u64,
pub height: u64,
}

impl Dimensions {
pub fn clamped_grid_size(&self) -> Self {
let min = settings::MIN_GRID_SIZE;
let max = settings::MAX_GRID_SIZE;
Dimensions {
width: self.width.clamp(min.width, max.width),
height: self.height.clamp(min.height, max.height),
}
}
}

impl Default for Dimensions {
fn default() -> Self {
settings::DEFAULT_GRID_SIZE
}
}

impl FromStr for Dimensions {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod profiling;
mod renderer;
mod running_tracker;
mod settings;
mod units;
mod utils;
mod window;

Expand Down
17 changes: 11 additions & 6 deletions src/renderer/animation_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use skia_safe::Point;
use crate::units::PixelPos;

#[allow(dead_code)]
pub fn ease_linear(t: f32) -> f32 {
Expand Down Expand Up @@ -73,11 +73,16 @@ pub fn ease(ease_func: fn(f32) -> f32, start: f32, end: f32, t: f32) -> f32 {
lerp(start, end, ease_func(t))
}

pub fn ease_point(ease_func: fn(f32) -> f32, start: Point, end: Point, t: f32) -> Point {
Point {
x: ease(ease_func, start.x, end.x, t),
y: ease(ease_func, start.y, end.y, t),
}
pub fn ease_point(
ease_func: fn(f32) -> f32,
start: PixelPos<f32>,
end: PixelPos<f32>,
t: f32,
) -> PixelPos<f32> {
PixelPos::new(
ease(ease_func, start.x, end.x, t),
ease(ease_func, start.y, end.y, t),
)
}

pub struct CriticallyDampedSpringAnimation {
Expand Down
96 changes: 49 additions & 47 deletions src/renderer/cursor_renderer/cursor_vfx.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
use log::error;
use nvim_rs::Value;
use skia_safe::{paint::Style, BlendMode, Canvas, Color, Paint, Point, Rect};
use skia_safe::{paint::Style, BlendMode, Canvas, Color, Paint, Rect};

use crate::{
editor::Cursor,
renderer::cursor_renderer::CursorSettings,
renderer::{animation_utils::*, grid_renderer::GridRenderer},
settings::*,
units::{PixelPos, PixelSize, PixelVec},
};

pub trait CursorVfx {
fn update(
&mut self,
settings: &CursorSettings,
current_cursor_destination: Point,
cursor_dimensions: Point,
current_cursor_destination: PixelPos<f32>,
cursor_dimensions: PixelSize<f32>,
immediate_movement: bool,
dt: f32,
) -> bool;
fn restart(&mut self, position: Point);
fn restart(&mut self, position: PixelPos<f32>);
fn render(
&self,
settings: &CursorSettings,
Expand Down Expand Up @@ -95,15 +96,15 @@ pub fn new_cursor_vfx(mode: &VfxMode) -> Option<Box<dyn CursorVfx>> {

pub struct PointHighlight {
t: f32,
center_position: Point,
center_position: PixelPos<f32>,
mode: HighlightMode,
}

impl PointHighlight {
pub fn new(mode: &HighlightMode) -> PointHighlight {
PointHighlight {
t: 0.0,
center_position: Point::new(0.0, 0.0),
center_position: PixelPos::new(0.0, 0.0),
mode: mode.clone(),
}
}
Expand All @@ -113,16 +114,16 @@ impl CursorVfx for PointHighlight {
fn update(
&mut self,
_settings: &CursorSettings,
_current_cursor_destination: Point,
_cursor_dimensions: Point,
_current_cursor_destination: PixelPos<f32>,
_cursor_dimensions: PixelSize<f32>,
_immediate_movement: bool,
dt: f32,
) -> bool {
self.t = (self.t + dt * 5.0).min(1.0); // TODO - speed config
self.t < 1.0
}

fn restart(&mut self, position: Point) {
fn restart(&mut self, position: PixelPos<f32>) {
self.t = 0.0;
self.center_position = position;
}
Expand All @@ -148,9 +149,9 @@ impl CursorVfx for PointHighlight {

paint.set_color(color);

let cursor_height = grid_renderer.font_dimensions.height;
let size = 3 * cursor_height;
let radius = self.t * size as f32;
let cursor_height = grid_renderer.grid_scale.0.height;
let size = 3.0 * cursor_height;
let radius = self.t * size;
let hr = radius * 0.5;
let rect = Rect::from_xywh(
self.center_position.x - hr,
Expand All @@ -165,12 +166,12 @@ impl CursorVfx for PointHighlight {
}
HighlightMode::Ripple => {
paint.set_style(Style::Stroke);
paint.set_stroke_width(cursor_height as f32 * 0.2);
paint.set_stroke_width(cursor_height * 0.2);
canvas.draw_oval(rect, &paint);
}
HighlightMode::Wireframe => {
paint.set_style(Style::Stroke);
paint.set_stroke_width(cursor_height as f32 * 0.2);
paint.set_stroke_width(cursor_height * 0.2);
canvas.draw_rect(rect, &paint);
}
}
Expand All @@ -179,15 +180,15 @@ impl CursorVfx for PointHighlight {

#[derive(Clone)]
struct ParticleData {
pos: Point,
speed: Point,
pos: PixelPos<f32>,
speed: PixelVec<f32>,
rotation_speed: f32,
lifetime: f32,
}

pub struct ParticleTrail {
particles: Vec<ParticleData>,
previous_cursor_dest: Point,
previous_cursor_dest: PixelPos<f32>,
trail_mode: TrailMode,
rng: RngState,
}
Expand All @@ -196,13 +197,19 @@ impl ParticleTrail {
pub fn new(trail_mode: &TrailMode) -> ParticleTrail {
ParticleTrail {
particles: vec![],
previous_cursor_dest: Point::new(0.0, 0.0),
previous_cursor_dest: PixelPos::new(0.0, 0.0),
trail_mode: trail_mode.clone(),
rng: RngState::new(),
}
}

fn add_particle(&mut self, pos: Point, speed: Point, rotation_speed: f32, lifetime: f32) {
fn add_particle(
&mut self,
pos: PixelPos<f32>,
speed: PixelVec<f32>,
rotation_speed: f32,
lifetime: f32,
) {
self.particles.push(ParticleData {
pos,
speed,
Expand All @@ -222,8 +229,8 @@ impl CursorVfx for ParticleTrail {
fn update(
&mut self,
settings: &CursorSettings,
current_cursor_dest: Point,
cursor_dimensions: Point,
current_cursor_dest: PixelPos<f32>,
cursor_dimensions: PixelSize<f32>,
immediate_movement: bool,
dt: f32,
) -> bool {
Expand Down Expand Up @@ -253,7 +260,7 @@ impl CursorVfx for ParticleTrail {
let travel_distance = travel.length();

// Increase amount of particles when cursor travels further
let particle_count = ((travel_distance / cursor_dimensions.y).powf(1.5)
let particle_count = ((travel_distance / cursor_dimensions.height).powf(1.5)
* settings.vfx_particle_density
* 0.01) as usize;

Expand All @@ -266,20 +273,19 @@ impl CursorVfx for ParticleTrail {
TrailMode::Railgun => {
let phase = t / std::f32::consts::PI
* settings.vfx_particle_phase
* (travel_distance / cursor_dimensions.y);
Point::new(phase.sin(), phase.cos()) * 2.0 * settings.vfx_particle_speed
* (travel_distance / cursor_dimensions.height);
PixelVec::new(phase.sin(), phase.cos())
* 2.0
* settings.vfx_particle_speed
}
TrailMode::Torpedo => {
let mut travel_dir = travel;
travel_dir.normalize();
let mut particle_dir =
self.rng.rand_dir_normalized() - travel_dir * 1.5;
particle_dir.normalize();
particle_dir * settings.vfx_particle_speed
let travel_dir = travel.normalize();
let particle_dir = self.rng.rand_dir_normalized() - travel_dir * 1.5;
particle_dir.normalize() * settings.vfx_particle_speed
}
TrailMode::PixieDust => {
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 = PixelVec::new(base_dir.x * 0.5, 0.4 + base_dir.y.abs());
dir * 3.0 * settings.vfx_particle_speed
}
};
Expand All @@ -290,7 +296,7 @@ impl CursorVfx for ParticleTrail {
TrailMode::PixieDust | TrailMode::Torpedo => {
prev_p
+ travel * self.rng.next_f32()
+ Point::new(0.0, cursor_dimensions.y * 0.5)
+ PixelVec::new(0.0, cursor_dimensions.height * 0.5)
}
};

Expand Down Expand Up @@ -319,7 +325,7 @@ impl CursorVfx for ParticleTrail {
!self.particles.is_empty()
}

fn restart(&mut self, _position: Point) {}
fn restart(&mut self, _position: PixelPos<f32>) {}

fn render(
&self,
Expand All @@ -329,11 +335,11 @@ impl CursorVfx for ParticleTrail {
cursor: &Cursor,
) {
let mut paint = Paint::new(skia_safe::colors::WHITE, None);
let font_dimensions = grid_renderer.font_dimensions;
let font_dimensions = grid_renderer.grid_scale.0;
match self.trail_mode {
TrailMode::Torpedo | TrailMode::Railgun => {
paint.set_style(Style::Stroke);
paint.set_stroke_width(font_dimensions.height as f32 * 0.2);
paint.set_stroke_width(font_dimensions.height * 0.2);
}
_ => {}
}
Expand All @@ -350,10 +356,8 @@ impl CursorVfx for ParticleTrail {
paint.set_color(color);

let radius = match self.trail_mode {
TrailMode::Torpedo | TrailMode::Railgun => {
font_dimensions.width as f32 * 0.5 * lifetime
}
TrailMode::PixieDust => font_dimensions.width as f32 * 0.2,
TrailMode::Torpedo | TrailMode::Railgun => font_dimensions.width * 0.5 * lifetime,
TrailMode::PixieDust => font_dimensions.width * 0.2,
};

let hr = radius * 0.5;
Expand Down Expand Up @@ -426,23 +430,21 @@ impl RngState {

// Produces a random vector with x and y in the [-1,1) range
// Note: Vector is not normalized.
fn rand_dir(&mut self) -> Point {
fn rand_dir(&mut self) -> PixelVec<f32> {
let x = self.next_f32();
let y = self.next_f32();

Point::new(x * 2.0 - 1.0, y * 2.0 - 1.0)
PixelVec::new(x * 2.0 - 1.0, y * 2.0 - 1.0)
}

fn rand_dir_normalized(&mut self) -> Point {
let mut v = self.rand_dir();
v.normalize();
v
fn rand_dir_normalized(&mut self) -> PixelVec<f32> {
self.rand_dir().normalize()
}
}

fn rotate_vec(v: Point, rot: f32) -> Point {
fn rotate_vec(v: PixelVec<f32>, rot: f32) -> PixelVec<f32> {
let sin = rot.sin();
let cos = rot.cos();

Point::new(v.x * cos - v.y * sin, v.x * sin + v.y * cos)
PixelVec::new(v.x * cos - v.y * sin, v.x * sin + v.y * cos)
}
Loading

0 comments on commit b83bcc4

Please sign in to comment.