diff --git a/src/renderer/cursor_renderer/cursor_vfx.rs b/src/renderer/cursor_renderer/cursor_vfx.rs index 2a50d6e..c429ef7 100644 --- a/src/renderer/cursor_renderer/cursor_vfx.rs +++ b/src/renderer/cursor_renderer/cursor_vfx.rs @@ -2,8 +2,8 @@ use log::error; use skia_safe::{paint::Style, BlendMode, Canvas, Color, Paint, Point, Rect}; use super::CursorSettings; -use crate::editor::{Colors, Cursor}; -use crate::renderer::animation_utils::*; +use crate::editor::Cursor; +use crate::renderer::{animation_utils::*, grid_renderer::GridRenderer}; use crate::settings::*; pub trait CursorVfx { @@ -11,7 +11,7 @@ pub trait CursorVfx { &mut self, settings: &CursorSettings, current_cursor_destination: Point, - font_size: (u64, u64), + cursor_dimensions: Point, dt: f32, ) -> bool; fn restart(&mut self, position: Point); @@ -19,9 +19,8 @@ pub trait CursorVfx { &self, settings: &CursorSettings, canvas: &mut Canvas, + grid_renderer: &mut GridRenderer, cursor: &Cursor, - colors: &Colors, - font_size: (u64, u64), ); } @@ -111,7 +110,7 @@ impl CursorVfx for PointHighlight { &mut self, _settings: &CursorSettings, _current_cursor_destination: Point, - _font_size: (u64, u64), + _cursor_dimensions: Point, dt: f32, ) -> bool { self.t = (self.t + dt * 5.0).min(1.0); // TODO - speed config @@ -127,9 +126,8 @@ impl CursorVfx for PointHighlight { &self, settings: &CursorSettings, canvas: &mut Canvas, + grid_renderer: &mut GridRenderer, cursor: &Cursor, - colors: &Colors, - font_size: (u64, u64), ) { if (self.t - 1.0).abs() < std::f32::EPSILON { return; @@ -138,13 +136,15 @@ impl CursorVfx for PointHighlight { let mut paint = Paint::new(skia_safe::colors::WHITE, None); paint.set_blend_mode(BlendMode::SrcOver); + let colors = &grid_renderer.default_style.colors; 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 color = Color::from_argb(alpha, base_color.r(), base_color.g(), base_color.b()); paint.set_color(color); - let size = 3 * font_size.1; + let cursor_height = grid_renderer.font_dimensions.height; + let size = 3 * cursor_height; let radius = self.t * size as f32; let hr = radius * 0.5; let rect = Rect::from_xywh( @@ -160,12 +160,12 @@ impl CursorVfx for PointHighlight { } HighlightMode::Ripple => { paint.set_style(Style::Stroke); - paint.set_stroke_width(font_size.1 as f32 * 0.2); + paint.set_stroke_width(cursor_height as f32 * 0.2); canvas.draw_oval(&rect, &paint); } HighlightMode::Wireframe => { paint.set_style(Style::Stroke); - paint.set_stroke_width(font_size.1 as f32 * 0.2); + paint.set_stroke_width(cursor_height as f32 * 0.2); canvas.draw_rect(&rect, &paint); } } @@ -218,7 +218,7 @@ impl CursorVfx for ParticleTrail { &mut self, settings: &CursorSettings, current_cursor_dest: Point, - font_size: (u64, u64), + cursor_dimensions: Point, dt: f32, ) -> bool { // Update lifetimes and remove dead particles @@ -246,7 +246,7 @@ impl CursorVfx for ParticleTrail { let travel_distance = travel.length(); // Increase amount of particles when cursor travels further - let particle_count = ((travel_distance / font_size.0 as f32).powf(1.5) + let particle_count = ((travel_distance / cursor_dimensions.y as f32).powf(1.5) * settings.vfx_particle_density * 0.01) as usize; @@ -259,7 +259,7 @@ impl CursorVfx for ParticleTrail { TrailMode::Railgun => { let phase = t / std::f32::consts::PI * settings.vfx_particle_phase - * (travel_distance / font_size.0 as f32); + * (travel_distance / cursor_dimensions.y as f32); Point::new(phase.sin(), phase.cos()) * 2.0 * settings.vfx_particle_speed } TrailMode::Torpedo => { @@ -282,7 +282,7 @@ impl CursorVfx for ParticleTrail { TrailMode::PixieDust | TrailMode::Torpedo => { prev_p + travel * self.rng.next_f32() - + Point::new(0.0, font_size.1 as f32 * 0.5) + + Point::new(0.0, cursor_dimensions.y as f32 * 0.5) } }; @@ -316,19 +316,20 @@ impl CursorVfx for ParticleTrail { &self, settings: &CursorSettings, canvas: &mut Canvas, + grid_renderer: &mut GridRenderer, cursor: &Cursor, - colors: &Colors, - font_size: (u64, u64), ) { let mut paint = Paint::new(skia_safe::colors::WHITE, None); + let font_dimensions = grid_renderer.font_dimensions; match self.trail_mode { TrailMode::Torpedo | TrailMode::Railgun => { paint.set_style(Style::Stroke); - paint.set_stroke_width(font_size.1 as f32 * 0.2); + paint.set_stroke_width(font_dimensions.height as f32 * 0.2); } _ => {} } + let colors = &grid_renderer.default_style.colors; let base_color: Color = cursor.background(colors).to_color(); paint.set_blend_mode(BlendMode::SrcOver); @@ -340,8 +341,10 @@ impl CursorVfx for ParticleTrail { paint.set_color(color); let radius = match self.trail_mode { - TrailMode::Torpedo | TrailMode::Railgun => font_size.0 as f32 * 0.5 * lifetime, - TrailMode::PixieDust => font_size.0 as f32 * 0.2, + TrailMode::Torpedo | TrailMode::Railgun => { + font_dimensions.width as f32 * 0.5 * lifetime + } + TrailMode::PixieDust => font_dimensions.width as f32 * 0.2, }; let hr = radius * 0.5; diff --git a/src/renderer/cursor_renderer/mod.rs b/src/renderer/cursor_renderer/mod.rs index 6514244..2e7a559 100644 --- a/src/renderer/cursor_renderer/mod.rs +++ b/src/renderer/cursor_renderer/mod.rs @@ -6,12 +6,11 @@ use std::collections::HashMap; // use neovide_derive::SettingGroup; use skia_safe::{Canvas, Paint, Path, Point}; -use super::RenderedWindow; +use super::{GridRenderer, RenderedWindow}; use crate::bridge::EditorMode; -use crate::editor::{Colors, Cursor, CursorShape}; +use crate::editor::{Cursor, CursorShape}; use crate::redraw_scheduler::REDRAW_SCHEDULER; use crate::renderer::animation_utils::*; -use crate::renderer::CachingShaper; use crate::settings::{FromValue, SETTINGS}; use blink::*; @@ -257,10 +256,8 @@ impl CursorRenderer { pub fn draw( &mut self, - default_colors: &Colors, - (font_width, font_height): (u64, u64), + grid_renderer: &mut GridRenderer, current_mode: &EditorMode, - shaper: &mut CachingShaper, canvas: &mut Canvas, dt: f32, ) { @@ -277,19 +274,23 @@ impl CursorRenderer { let character = self.cursor.character.clone(); - let font_width = match (self.cursor.double_width, &self.cursor.shape) { - (true, CursorShape::Block) => font_width * 2, - _ => font_width, - }; + let mut cursor_width = grid_renderer.font_dimensions.width; + if self.cursor.double_width && self.cursor.shape == CursorShape::Block { + cursor_width *= 2; + } - let font_dimensions: Point = (font_width as f32, font_height as f32).into(); + let cursor_dimensions: Point = ( + cursor_width as f32, + grid_renderer.font_dimensions.height as f32, + ) + .into(); let in_insert_mode = matches!(current_mode, EditorMode::Insert); let changed_to_from_cmdline = !matches!(self.previous_editor_mode, EditorMode::CmdLine) ^ matches!(current_mode, EditorMode::CmdLine); - let center_destination = self.destination + font_dimensions * 0.5; + let center_destination = self.destination + cursor_dimensions * 0.5; let new_cursor = Some(self.cursor.shape.clone()); if self.previous_cursor_shape != new_cursor { @@ -315,7 +316,7 @@ impl CursorRenderer { let corner_animating = corner.update( &settings, - font_dimensions, + cursor_dimensions, center_destination, dt, immediate_movement, @@ -325,7 +326,7 @@ impl CursorRenderer { } let vfx_animating = if let Some(vfx) = self.cursor_vfx.as_mut() { - vfx.update(&settings, center_destination, (font_width, font_height), dt) + vfx.update(&settings, center_destination, cursor_dimensions, dt) } else { false }; @@ -341,7 +342,11 @@ impl CursorRenderer { if self.cursor.enabled && render { // Draw Background - paint.set_color(self.cursor.background(default_colors).to_color()); + let background_color = self + .cursor + .background(&grid_renderer.default_style.colors) + .to_color(); + paint.set_color(background_color); // The cursor is made up of four points, so I create a path with each of the four // corners. @@ -356,13 +361,19 @@ impl CursorRenderer { canvas.draw_path(&path, &paint); // Draw foreground - paint.set_color(self.cursor.foreground(default_colors).to_color()); + let foreground_color = self + .cursor + .foreground(&grid_renderer.default_style.colors) + .to_color(); + paint.set_color(foreground_color); canvas.save(); canvas.clip_path(&path, None, Some(false)); - let y_adjustment = shaper.y_adjustment(); - let blobs = &shaper.shape_cached(&[character], false, false); + let y_adjustment = grid_renderer.shaper.y_adjustment(); + let blobs = &grid_renderer + .shaper + .shape_cached(&[character], false, false); for blob in blobs.iter() { canvas.draw_text_blob( @@ -375,13 +386,7 @@ impl CursorRenderer { canvas.restore(); if let Some(vfx) = self.cursor_vfx.as_ref() { - vfx.render( - &settings, - canvas, - &self.cursor, - default_colors, - (font_width, font_height), - ); + vfx.render(&settings, canvas, grid_renderer, &self.cursor); } } } diff --git a/src/renderer/grid_renderer.rs b/src/renderer/grid_renderer.rs new file mode 100644 index 0000000..54fe42f --- /dev/null +++ b/src/renderer/grid_renderer.rs @@ -0,0 +1,185 @@ +use std::sync::Arc; + +use glutin::dpi::PhysicalSize; +use log::trace; +use skia_safe::{colors, dash_path_effect, BlendMode, Canvas, Color, Paint, Rect, HSV}; + +use super::{CachingShaper, RendererSettings}; +use crate::editor::{Colors, Style}; +use crate::settings::*; +use crate::utils::Dimensions; + +pub struct GridRenderer { + pub shaper: CachingShaper, + pub paint: Paint, + pub default_style: Arc