mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 20:19:44 +00:00
Merge pull request #8500 from dotCooCoo/feat/bristle-pen-cursor
Paintbrush: preview Bristle tip for rotation-capable pens
This commit is contained in:
@@ -37,7 +37,7 @@ use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_brush::{Calligraphic, Variation};
|
||||
use vectorcraft_brush::{BristleShape, BrushKind, Calligraphic, Variation};
|
||||
use vectorcraft_doc::{NodeId, NodeKind};
|
||||
use vectorcraft_geom::{Affine, BezPath, FillRule, ParamCurve, PathData, PathSeg, Point, Rect, Vec2, shapes};
|
||||
use vectorcraft_pathops as po;
|
||||
@@ -200,6 +200,12 @@ pub struct GestureTool {
|
||||
/// Where the Blob Brush's or the Eraser's tip outline shows: the pointer over the canvas,
|
||||
/// unless Cmd/Ctrl lends a selection tool or Alt the Smooth tool.
|
||||
tip_at: Option<Point>,
|
||||
/// Alt as it was at the last pointer event; the release can select a new path after the tool
|
||||
/// returns, so the Bristle tip checks the current selection when overlays are drawn.
|
||||
tip_alt: bool,
|
||||
/// The last rotation-capable pen over the Paintbrush's Bristle tip; a mouse or a pen without
|
||||
/// barrel rotation has no tip annotator.
|
||||
bristle_pen: Option<(PenPose, f64)>,
|
||||
/// A pen pressed less than fully at a point of the stroke whose pressure is kept.
|
||||
light: bool,
|
||||
/// Which way the pen leaned at the last point of the stroke where it leaned 1° or more from
|
||||
@@ -899,6 +905,8 @@ impl GestureTool {
|
||||
snap: DrawSnap::default(),
|
||||
pressed: Point::ZERO,
|
||||
tip_at: None,
|
||||
tip_alt: false,
|
||||
bristle_pen: None,
|
||||
light: false,
|
||||
bearing: None,
|
||||
bearings: vec![],
|
||||
@@ -1088,12 +1096,50 @@ impl GestureTool {
|
||||
p
|
||||
}
|
||||
|
||||
fn pointer_tip(&mut self, cx: &ToolContext, ev: &PointerEvent) {
|
||||
if self.varies_size() {
|
||||
self.tip_at = (!ev.mods.cmd && !self.blob_alt_smooths(ev.mods)).then_some(ev.pos);
|
||||
} else if self.id == "paintbrush" {
|
||||
self.tip_alt = ev.mods.alt;
|
||||
self.bristle_pen =
|
||||
ev.pen.filter(|pen| pen.rotation.is_some()).map(|pen| (pen, if ev.kind == PointerKind::Move { 0.5 } else { f64::from(ev.pressure) }));
|
||||
let borrows_smooth = !self.active && ev.mods.alt && has_selected_smooth_path(cx);
|
||||
self.tip_at = self.bristle_pen.filter(|_| !ev.mods.cmd && !borrows_smooth).map(|_| ev.pos);
|
||||
}
|
||||
}
|
||||
|
||||
/// The Blob Brush's or the Eraser's tip at the pointer, before and while painting: its outline
|
||||
/// `size` across at its angle and roundness (the Eraser's as the pen varies them while it
|
||||
/// erases, [`Self::tip_now`]), at the zoom, black on a white halo so it reads on
|
||||
/// any art. Precise cursors show only the crosshair, and smoothing shows no tip.
|
||||
fn tip_outline(&self, cx: &ToolContext) -> Vec<Overlay> {
|
||||
let Some(at) = self.tip_at.filter(|_| !self.smoothing && !cx.precise_cursors) else { return vec![] };
|
||||
if self.id == "paintbrush" {
|
||||
if !self.active && self.tip_alt && has_selected_smooth_path(cx) {
|
||||
return vec![];
|
||||
}
|
||||
let Some((pose, pressure)) = self.bristle_pen else { return vec![] };
|
||||
let Some(name) = vectorcraft_brush::current(cx.doc) else { return vec![] };
|
||||
let Some(brush) = vectorcraft_brush::find(cx.doc, &name) else { return vec![] };
|
||||
let BrushKind::Bristle(bristle) = brush.kind else { return vec![] };
|
||||
let [pressure, tilt, bearing, rotation] = super::pen_controls(pressure, Some(pose), super::pen_bearing(pose));
|
||||
let size = bristle.size.max(0.1) * cx.paint.stroke_width.max(0.0) * (0.5 + pressure) * (1.25 - 0.5 * tilt);
|
||||
if size <= 0.0 {
|
||||
return vec![];
|
||||
}
|
||||
let roundness = match bristle.shape {
|
||||
BristleShape::RoundFan | BristleShape::FlatFan => 0.25,
|
||||
BristleShape::FlatPoint | BristleShape::FlatBlunt | BristleShape::FlatCurve | BristleShape::FlatAngle => 0.45,
|
||||
_ => 0.8,
|
||||
};
|
||||
let angle = std::f64::consts::TAU * (bearing + rotation - 1.0);
|
||||
let circle = kurbo::Shape::to_path(&kurbo::Circle::new(Point::ORIGIN, size / 2.0), size.max(0.1) * 0.05);
|
||||
let path = Affine::translate(at.to_vec2()) * Affine::rotate(-angle) * Affine::scale_non_uniform(1.0, roundness) * circle;
|
||||
return vec![
|
||||
Overlay::Path { path: path.clone(), color: [0xff, 0xff, 0xff], width: 3.0, dashed: false },
|
||||
Overlay::Path { path, color: [0, 0, 0], width: 1.0, dashed: false },
|
||||
];
|
||||
}
|
||||
let [angle, roundness, size] = self.tip_now();
|
||||
let tip = if self.id == "blobBrush" { blob_tip(angle, roundness) } else { eraser_tip(angle, roundness) };
|
||||
let r = size / 2.0;
|
||||
@@ -1587,9 +1633,7 @@ impl Tool for GestureTool {
|
||||
}
|
||||
fn pointer(&mut self, cx: &ToolContext, ev: &PointerEvent) -> Vec<Action> {
|
||||
let p = ev.pos;
|
||||
if self.varies_size() {
|
||||
self.tip_at = (!ev.mods.cmd && !self.blob_alt_smooths(ev.mods)).then_some(p);
|
||||
}
|
||||
self.pointer_tip(cx, ev);
|
||||
if let Some(out) = self.marquee_pointer(cx, ev) {
|
||||
return out;
|
||||
}
|
||||
@@ -1762,6 +1806,10 @@ impl Tool for GestureTool {
|
||||
self.close_to = None;
|
||||
self.connect_to.clear();
|
||||
self.redraw = None;
|
||||
if self.id == "paintbrush" {
|
||||
// The release can change a drawing stroke into an Alt-borrowed Smooth hover.
|
||||
self.pointer_tip(cx, ev);
|
||||
}
|
||||
out
|
||||
}
|
||||
_ => vec![],
|
||||
|
||||
@@ -3043,6 +3043,103 @@ fn the_blob_brush_and_the_eraser_show_their_tip_at_the_pointer() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A rotation-capable pen shows the current Bristle brush's tip before and during a Paintbrush
|
||||
/// stroke. A mouse, a pen without rotation, precise cursors and a borrowed selection tool do not.
|
||||
#[test]
|
||||
fn bristle_tip_annotator_follows_a_rotation_capable_pen() {
|
||||
let (mut d, id) = doc_with_rect();
|
||||
d.unknown.insert(vectorcraft_brush::CURRENT_KEY.into(), json!("Bristle Flat Fan"));
|
||||
let s = Selection::default();
|
||||
let p = paint();
|
||||
let view = cx(&d, &s, &p);
|
||||
let bounds = |t: &GestureTool, cx: &ToolContext| {
|
||||
let overlays = t.overlays(cx);
|
||||
let Some(Overlay::Path { path, color: [0, 0, 0], width: 1.0, .. }) = overlays.last() else { return None };
|
||||
assert!(matches!(overlays.get(overlays.len() - 2), Some(Overlay::Path { color: [0xff, 0xff, 0xff], width: 3.0, .. })));
|
||||
Some(vectorcraft_geom::Shape::bounding_box(path))
|
||||
};
|
||||
let mut tool = GestureTool::new("paintbrush");
|
||||
let at =
|
||||
|rotation| PointerEvent { pen: Some(PenPose { tilt: Some([30.0, 0.0]), rotation }), ..PointerEvent::new(PointerKind::Move, 100.0, 50.0) };
|
||||
assert!(bounds(&tool, &view).is_none());
|
||||
tool.pointer(&view, &PointerEvent::new(PointerKind::Move, 100.0, 50.0));
|
||||
assert!(bounds(&tool, &view).is_none(), "mouse");
|
||||
tool.pointer(&view, &at(None));
|
||||
assert!(bounds(&tool, &view).is_none(), "pen without rotation");
|
||||
tool.pointer(&view, &at(Some(0.0)));
|
||||
let horizontal = bounds(&tool, &view).expect("rotating pen shows a tip");
|
||||
let mut selected = Selection::default();
|
||||
selected.set([id]);
|
||||
let selected_view = cx(&d, &selected, &p);
|
||||
tool.pointer(&selected_view, &at(Some(0.0)).with_mods(Mods { alt: true, ..Default::default() }));
|
||||
assert!(bounds(&tool, &selected_view).is_none(), "Alt borrows Smooth for a selected path");
|
||||
tool.pointer(&view, &at(Some(0.0)));
|
||||
let mut wider_paint = p.clone();
|
||||
wider_paint.stroke_width = 10.0;
|
||||
let wider_view = cx(&d, &s, &wider_paint);
|
||||
let wider = bounds(&tool, &wider_view).expect("stroke width scales the tip");
|
||||
assert!((wider.width() - horizontal.width() * 10.0).abs() < 1e-6, "{horizontal:?}, {wider:?}");
|
||||
wider_paint.stroke_width = 0.0;
|
||||
assert!(bounds(&tool, &cx(&d, &s, &wider_paint)).is_none(), "a zero-width stroke has no tip");
|
||||
tool.pointer(&view, &at(Some(90.0)));
|
||||
let vertical = bounds(&tool, &view).expect("tip turns with the barrel");
|
||||
assert!(horizontal.width() > horizontal.height() * 2.0, "{horizontal:?}");
|
||||
assert!(vertical.height() > vertical.width() * 2.0, "{vertical:?}");
|
||||
let mut light = at(Some(0.0));
|
||||
light.kind = PointerKind::Down;
|
||||
light.pressure = 0.1;
|
||||
tool.pointer(&view, &light);
|
||||
let light_tip = bounds(&tool, &view).expect("light pressure shows a smaller tip");
|
||||
let mut heavy = light;
|
||||
heavy.kind = PointerKind::Drag;
|
||||
heavy.pressure = 0.9;
|
||||
tool.pointer(&view, &heavy);
|
||||
let heavy_tip = bounds(&tool, &view).expect("heavy pressure shows a larger tip");
|
||||
assert!(heavy_tip.width() > light_tip.width() * 1.5, "{light_tip:?}, {heavy_tip:?}");
|
||||
let mut release = heavy;
|
||||
release.kind = PointerKind::Up;
|
||||
tool.pointer(&view, &release);
|
||||
let mut alt_stroke = light;
|
||||
alt_stroke.pressure = 0.5;
|
||||
tool.pointer(&selected_view, &alt_stroke);
|
||||
alt_stroke.kind = PointerKind::Drag;
|
||||
alt_stroke.mods.alt = true;
|
||||
tool.pointer(&selected_view, &alt_stroke);
|
||||
assert!(bounds(&tool, &selected_view).is_some(), "Alt during a drawing stroke keeps the tip");
|
||||
alt_stroke.kind = PointerKind::Up;
|
||||
tool.pointer(&selected_view, &alt_stroke);
|
||||
assert_eq!(tool.cursor(&selected_view, alt_stroke.pos, alt_stroke.mods), Cursor::Smooth);
|
||||
assert!(bounds(&tool, &selected_view).is_none(), "Alt release borrows Smooth without a stale tip");
|
||||
// A completed stroke can select its new path after `pointer` returns its actions. The overlay
|
||||
// must use that current selection even without a further pointer move.
|
||||
let mut newly_selected = GestureTool::new("paintbrush");
|
||||
let mut new_stroke = light;
|
||||
newly_selected.pointer(&view, &new_stroke);
|
||||
new_stroke.kind = PointerKind::Drag;
|
||||
new_stroke.mods.alt = true;
|
||||
newly_selected.pointer(&view, &new_stroke);
|
||||
new_stroke.kind = PointerKind::Up;
|
||||
newly_selected.pointer(&view, &new_stroke);
|
||||
assert!(bounds(&newly_selected, &view).is_some(), "no selected path borrows Smooth yet");
|
||||
assert_eq!(newly_selected.cursor(&selected_view, new_stroke.pos, new_stroke.mods), Cursor::Smooth);
|
||||
assert!(bounds(&newly_selected, &selected_view).is_none(), "post-commit selection also hides the tip");
|
||||
tool.pointer(&view, &at(Some(0.0)));
|
||||
assert!(bounds(&tool, &view).is_some(), "a normal pen hover shows the tip before Precise Cursors hides it");
|
||||
let precise = ToolContext { precise_cursors: true, ..cx(&d, &s, &p) };
|
||||
assert!(bounds(&tool, &precise).is_none());
|
||||
tool.pointer(&view, &at(Some(0.0)).with_mods(Mods { cmd: true, ..Default::default() }));
|
||||
assert!(bounds(&tool, &view).is_none(), "selection tool borrowed by Cmd");
|
||||
tool.pointer(&view, &at(Some(0.0)));
|
||||
tool.hover_ended(&view);
|
||||
assert!(bounds(&tool, &view).is_none(), "leaving the canvas");
|
||||
|
||||
let (mut d, _) = doc_with_rect();
|
||||
d.unknown.insert(vectorcraft_brush::CURRENT_KEY.into(), json!("3 pt. Round"));
|
||||
let calligraphic = cx(&d, &s, &p);
|
||||
tool.pointer(&calligraphic, &at(Some(0.0)));
|
||||
assert!(bounds(&tool, &calligraphic).is_none(), "other brush types keep their cursor");
|
||||
}
|
||||
|
||||
/// Eraser Tool Options' Angle, Roundness and Size variation menus: the Eraser keeps each mode and
|
||||
/// Variation, the release sends them with the pen's readings along the drag, and while it erases
|
||||
/// its tip outline at the pointer takes the angle, roundness and size the pen gives there. The Blob
|
||||
|
||||
Reference in New Issue
Block a user