working cursor smear

macos-click-through
Keith Simmons 5 years ago
parent 59601b7d71
commit f6edcc4761

@ -1,17 +1,16 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use skulpin::skia_safe::{Canvas, Paint, Point}; use skulpin::skia_safe::{Canvas, Paint, Path, Point};
use skulpin::skia_safe::canvas::PointMode;
use crate::renderer::{CachingShaper, FontLookup}; use crate::renderer::{CachingShaper, FontLookup};
use crate::editor::{Colors, Cursor, CursorShape, Editor}; use crate::editor::{Colors, Cursor, CursorShape, Editor};
const average_motion_percentage: f32 = 0.5; const AVERAGE_MOTION_PERCENTAGE: f32 = 0.8;
const motion_percentage_spread: f32 = 0.3; 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)] #[derive(Debug, Clone)]
pub struct Corner { pub struct Corner {
@ -34,11 +33,13 @@ impl Corner {
let delta = corner_destination - self.current_position; let delta = corner_destination - self.current_position;
if delta.length() > 0.0 {
let motion_scale = delta.dot(relative_scaled_position) / delta.length() / font_dimensions.length(); 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 motion_percentage = motion_scale * MOTION_PERCENTAGE_SPREAD + AVERAGE_MOTION_PERCENTAGE;
let delta = corner_destination - self.current_position; let delta = corner_destination - self.current_position;
self.current_position += delta * motion_percentage; self.current_position += delta * motion_percentage;
}
delta.length() > 0.001 delta.length() > 0.001
} }
@ -53,21 +54,21 @@ impl CursorRenderer {
let mut renderer = CursorRenderer { let mut renderer = CursorRenderer {
corners: vec![Corner::new((0.0, 0.0).into()); 4] corners: vec![Corner::new((0.0, 0.0).into()); 4]
}; };
renderer.set_cursor_shape(CursorShape::Block); renderer.set_cursor_shape(&CursorShape::Block);
renderer renderer
} }
fn set_cursor_shape(&mut self, cursor_shape: CursorShape) { fn set_cursor_shape(&mut self, cursor_shape: &CursorShape) {
self.corners = self.corners self.corners = self.corners
.clone() .clone()
.into_iter().enumerate() .into_iter().enumerate()
.map(|(i, corner)| { .map(|(i, corner)| {
let (x, y) = standard_corners[i]; let (x, y) = STANDARD_CORNERS[i];
Corner { Corner {
relative_position: match cursor_shape { relative_position: match cursor_shape {
CursorShape::Block => (x, y).into(), CursorShape::Block => (x, y).into(),
CursorShape::Vertical => ((x + 0.5) * bar_width - 0.5, 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::Horizontal => (x, (y + 0.5) * BAR_WIDTH - 0.5).into()
}, },
.. corner .. corner
} }
@ -84,33 +85,46 @@ impl CursorRenderer {
let (grid_x, grid_y) = cursor.position; let (grid_x, grid_y) = cursor.position;
let font_dimensions: Point = (font_width, font_height).into(); let font_dimensions: Point = (font_width, font_height).into();
let center_destination: Point = ( let center_destination: Point = (
grid_x as f32 * (font_width * 1.5), grid_x as f32 * font_width + font_width / 2.0,
grid_y as f32 * (font_height * 1.5) grid_y as f32 * font_height + font_height / 2.0
).into(); ).into();
self.set_cursor_shape(&cursor.shape);
let mut animating = false; let mut animating = false;
if !center_destination.is_zero() {
for corner in self.corners.iter_mut() { for corner in self.corners.iter_mut() {
let corner_animating = corner.update(font_dimensions, center_destination); let corner_animating = corner.update(font_dimensions, center_destination);
animating = animating || corner_animating; animating = animating || corner_animating;
} }
}
if cursor.enabled { if cursor.enabled {
// Draw Background // Draw Background
paint.set_color(cursor.background(&default_colors).to_color()); paint.set_color(cursor.background(&default_colors).to_color());
canvas.draw_points(PointMode::Polygon, &self.corners.iter().map(|corner| corner.current_position).collect::<Vec<_>>(), &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(); let mut position_sum: Point = (0.0, 0.0).into();
for i in 0..4 { for i in 0..4 {
position_sum += self.corners[i].current_position; 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 // Draw foreground
if let CursorShape::Block = cursor.shape { if let CursorShape::Block = cursor.shape {
let (cursor_grid_y, cursor_grid_x) = cursor.position; let (cursor_grid_y, cursor_grid_x) = cursor.position;
paint.set_color(cursor.foreground(&default_colors).to_color()); paint.set_color(cursor.foreground(&default_colors).to_color());
let editor = editor.lock().unwrap(); 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) .map(|(character, _)| character)
.unwrap_or(' '); .unwrap_or(' ');
canvas.draw_text_blob( canvas.draw_text_blob(

@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use skulpin::CoordinateSystemHelper; use skulpin::CoordinateSystemHelper;
use skulpin::skia_safe::{Canvas, Paint, Surface, Budgeted, Rect, Typeface, Font, FontStyle, colors}; 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; use skulpin::skia_safe::gpu::SurfaceOrigin;
mod caching_shaper; mod caching_shaper;
@ -102,6 +103,7 @@ impl Renderer {
let surface = None; let surface = None;
let mut paint = Paint::new(colors::WHITE, None); let mut paint = Paint::new(colors::WHITE, None);
paint.set_anti_alias(false); paint.set_anti_alias(false);
let mut fonts_lookup = FontLookup::new(FONT_NAME, FONT_SIZE); let mut fonts_lookup = FontLookup::new(FONT_NAME, FONT_SIZE);
let shaper = CachingShaper::new(); let shaper = CachingShaper::new();

Loading…
Cancel
Save