diff --git a/src/renderer/cursor_renderer.rs b/src/renderer/cursor_renderer.rs index 4f2340b..1fa7c3c 100644 --- a/src/renderer/cursor_renderer.rs +++ b/src/renderer/cursor_renderer.rs @@ -1,17 +1,16 @@ use std::sync::{Arc, Mutex}; -use skulpin::skia_safe::{Canvas, Paint, Point}; -use skulpin::skia_safe::canvas::PointMode; +use skulpin::skia_safe::{Canvas, Paint, Path, Point}; use crate::renderer::{CachingShaper, FontLookup}; use crate::editor::{Colors, Cursor, CursorShape, Editor}; -const average_motion_percentage: f32 = 0.5; -const motion_percentage_spread: f32 = 0.3; +const AVERAGE_MOTION_PERCENTAGE: f32 = 0.8; +const MOTION_PERCENTAGE_SPREAD: f32 = 0.5; -const bar_width: f32 = 1.0 / 8.0; +const BAR_WIDTH: 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(Debug, Clone)] pub struct Corner { @@ -34,11 +33,13 @@ impl Corner { let delta = corner_destination - self.current_position; - let motion_scale = delta.dot(relative_scaled_position) / delta.length() / font_dimensions.length(); - let motion_percentage = motion_scale * motion_percentage_spread + average_motion_percentage; + if delta.length() > 0.0 { + let motion_scale = delta.dot(relative_scaled_position) / delta.length() / font_dimensions.length(); + let motion_percentage = motion_scale * MOTION_PERCENTAGE_SPREAD + AVERAGE_MOTION_PERCENTAGE; - let delta = corner_destination - self.current_position; - self.current_position += delta * motion_percentage; + let delta = corner_destination - self.current_position; + self.current_position += delta * motion_percentage; + } delta.length() > 0.001 } @@ -53,21 +54,21 @@ impl CursorRenderer { let mut renderer = CursorRenderer { corners: vec![Corner::new((0.0, 0.0).into()); 4] }; - renderer.set_cursor_shape(CursorShape::Block); + renderer.set_cursor_shape(&CursorShape::Block); renderer } - fn set_cursor_shape(&mut self, cursor_shape: CursorShape) { + fn set_cursor_shape(&mut self, cursor_shape: &CursorShape) { self.corners = self.corners .clone() .into_iter().enumerate() .map(|(i, corner)| { - let (x, y) = standard_corners[i]; + let (x, y) = STANDARD_CORNERS[i]; Corner { relative_position: match cursor_shape { CursorShape::Block => (x, y).into(), - CursorShape::Vertical => ((x + 0.5) * bar_width - 0.5, y).into(), - CursorShape::Horizontal => (x, (y + 0.5) * bar_width - 0.5).into() + CursorShape::Vertical => ((x + 0.5) * BAR_WIDTH - 0.5, y).into(), + CursorShape::Horizontal => (x, (y + 0.5) * BAR_WIDTH - 0.5).into() }, .. corner } @@ -84,33 +85,46 @@ impl CursorRenderer { let (grid_x, grid_y) = cursor.position; let font_dimensions: Point = (font_width, font_height).into(); let center_destination: Point = ( - grid_x as f32 * (font_width * 1.5), - grid_y as f32 * (font_height * 1.5) + grid_x as f32 * font_width + font_width / 2.0, + grid_y as f32 * font_height + font_height / 2.0 ).into(); + self.set_cursor_shape(&cursor.shape); + let mut animating = false; - for corner in self.corners.iter_mut() { - let corner_animating = corner.update(font_dimensions, center_destination); - animating = animating || corner_animating; + if !center_destination.is_zero() { + for corner in self.corners.iter_mut() { + let corner_animating = corner.update(font_dimensions, center_destination); + animating = animating || corner_animating; + } } + if cursor.enabled { // Draw Background paint.set_color(cursor.background(&default_colors).to_color()); - canvas.draw_points(PointMode::Polygon, &self.corners.iter().map(|corner| corner.current_position).collect::>(), &paint); + + let mut path = Path::new(); + path.move_to(self.corners[0].current_position); + path.line_to(self.corners[1].current_position); + path.line_to(self.corners[2].current_position); + path.line_to(self.corners[3].current_position); + path.close(); + + canvas.draw_path(&path, &paint); let mut position_sum: Point = (0.0, 0.0).into(); for i in 0..4 { position_sum += self.corners[i].current_position; } - let Point { x: cursor_x, y: cursor_y } = position_sum * (1.0 / 4.0); + let Point { x: cursor_x, y: cursor_y } = position_sum * (1.0 / 4.0) - font_dimensions * 0.5; // Draw foreground if let CursorShape::Block = cursor.shape { let (cursor_grid_y, cursor_grid_x) = cursor.position; paint.set_color(cursor.foreground(&default_colors).to_color()); let editor = editor.lock().unwrap(); - let character = editor.grid[cursor_grid_y as usize][cursor_grid_x as usize].clone() + let character = editor.grid[cursor_grid_x as usize][cursor_grid_y as usize].clone() .map(|(character, _)| character) .unwrap_or(' '); canvas.draw_text_blob( diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index c118c9d..25db09a 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use std::sync::{Arc, Mutex}; use skulpin::CoordinateSystemHelper; use skulpin::skia_safe::{Canvas, Paint, Surface, Budgeted, Rect, Typeface, Font, FontStyle, colors}; +use skulpin::skia_safe::paint::Style as PaintStyle; use skulpin::skia_safe::gpu::SurfaceOrigin; mod caching_shaper; @@ -102,6 +103,7 @@ impl Renderer { let surface = None; let mut paint = Paint::new(colors::WHITE, None); paint.set_anti_alias(false); + let mut fonts_lookup = FontLookup::new(FONT_NAME, FONT_SIZE); let shaper = CachingShaper::new();