mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 06:38: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
|
||||
|
||||
@@ -16,6 +16,8 @@ pub(super) struct HoverContext {
|
||||
pub(super) center: Point,
|
||||
pub(super) zoom: f64,
|
||||
pub(super) rotation: f64,
|
||||
/// A pen turning in place changes a Bristle tip even when its pointer stays still.
|
||||
pub(super) pen: Option<PenPose>,
|
||||
/// The active document's id and revision (which moves with every edit, undo and selection
|
||||
/// change).
|
||||
pub(super) doc: Option<(u64, u64)>,
|
||||
|
||||
@@ -12,6 +12,9 @@ use super::{Color32, Drag, Mods, Point, PointerEvent, PointerKind, Pos2, Shape,
|
||||
|
||||
/// Dispatch canvas pointer, keyboard and drop events through the active document view.
|
||||
pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Response, rect: egui::Rect) {
|
||||
if let Some(feed) = app.services.stylus.as_ref() {
|
||||
feed.bind_repaint(ui.ctx());
|
||||
}
|
||||
// A modal dialog takes the canvas's presses, finger taps, long touches and view navigation, from the frame it opens.
|
||||
let modal = crate::panel_docking::modal_open(app, ui.ctx());
|
||||
let (pointer, m, space, hover) = wheel_view(app, ui, resp, rect, modal);
|
||||
@@ -53,7 +56,7 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo
|
||||
});
|
||||
// What a hover means besides where the pointer is on screen: the tool, the canvas, the view
|
||||
// (scrolled, zoomed or turned by the wheel, a gesture, keys or a command) and the document and
|
||||
// selection as they are (another tab, an edit, an undo). Under a still pointer, a tool switched
|
||||
// selection as they are (another tab, an edit, an undo), and the pen's orientation. Under a still pointer, a tool switched
|
||||
// to is hovered at once, and when the rest changes, a tool that asks for it
|
||||
// ([`Session::tool_wants_rehover`]) is hovered again where the pointer is.
|
||||
let context = HoverContext {
|
||||
@@ -62,13 +65,14 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo
|
||||
center: v.center,
|
||||
zoom: v.zoom,
|
||||
rotation: v.rotation,
|
||||
pen: pen_pose(app, &xf),
|
||||
doc: app.session.active().map(|st| (st.uid, st.revision)),
|
||||
};
|
||||
let (tool_switched, context_changed) = ui.data_mut(|d| {
|
||||
let (tool_switched, context_changed, pen_changed) = ui.data_mut(|d| {
|
||||
let id = egui::Id::unique("canvas-hover-context");
|
||||
let prev = d.get_temp::<HoverContext>(id);
|
||||
d.insert_temp(id, context);
|
||||
prev.map_or((false, false), |prev| (prev.tool != context.tool, prev != context))
|
||||
prev.map_or((false, false, false), |prev| (prev.tool != context.tool, prev != context, prev.pen != context.pen))
|
||||
});
|
||||
|
||||
if app.ui.screen_mode == 3 && matches!(drag, None | Some(Drag::Present | Drag::Gesture)) {
|
||||
@@ -137,7 +141,12 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo
|
||||
} else if let Some(p) = hover_tool
|
||||
// A press on an artboard's name starts no drag: the moves while it is held are not hover.
|
||||
&& !(pointer.primary_down() && ui.data(|d| d.get_temp::<(usize, bool)>(name_press_id()).is_some()))
|
||||
&& (pointer.is_moving() || mods_changed || !was_over || tool_switched || (context_changed && app.session.tool_wants_rehover()))
|
||||
&& (pointer.is_moving()
|
||||
|| mods_changed
|
||||
|| !was_over
|
||||
|| tool_switched
|
||||
|| (context_changed && app.session.tool_wants_rehover())
|
||||
|| (pen_changed && context.tool == "paintbrush"))
|
||||
{
|
||||
let ev = PointerEvent {
|
||||
kind: PointerKind::Move,
|
||||
@@ -145,7 +154,7 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo
|
||||
mods: mods(m, space),
|
||||
backquote: ui.input(|i| i.key_down(egui::Key::Backtick)),
|
||||
pressure: 1.0,
|
||||
pen: None,
|
||||
pen: pen_pose(app, &xf),
|
||||
};
|
||||
dispatch(app, &ev, view);
|
||||
}
|
||||
|
||||
@@ -11482,6 +11482,46 @@ fn the_tip_brings_back_the_perspective_grid_tool_without_showing_the_grid() {
|
||||
assert!(!visible(&app), "the grid stays hidden");
|
||||
}
|
||||
|
||||
/// The native pen feed's barrel rotation reaches the Paintbrush during canvas hover, before any
|
||||
/// stroke begins. Turning the pen or replacing it with a mouse updates a still pointer too.
|
||||
#[test]
|
||||
fn bristle_pen_tip_reaches_the_canvas_before_a_press() {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
let feed = StylusFeed::default();
|
||||
app.services.stylus = Some(feed.clone());
|
||||
app.session.execute("file.new", &json!({"width": 800, "height": 600})).unwrap();
|
||||
app.session.execute("brush.setCurrent", &json!({"name": "Bristle Flat Fan"})).unwrap();
|
||||
app.select_tool("paintbrush");
|
||||
let ctx = egui::Context::default();
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
let at = app.canvas_rect.unwrap().center();
|
||||
let tip = |app: &mut VectorcraftApp| {
|
||||
app.session.overlays(app.view_info()).into_iter().find_map(|o| match o {
|
||||
vectorcraft_tools::Overlay::Path { path, color: [0, 0, 0], width: 1.0, .. } => Some(path.bounding_box()),
|
||||
_ => None,
|
||||
})
|
||||
};
|
||||
feed.set_sample(Some(crate::stylus::PenSample {
|
||||
pressure: 0.0,
|
||||
pose: PenPose { rotation: Some(0.0), ..Default::default() },
|
||||
..Default::default()
|
||||
}));
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at)]);
|
||||
let horizontal = tip(&mut app).expect("hovering pen shows its brush tip");
|
||||
assert!(horizontal.width() > horizontal.height() * 2.0, "{horizontal:?}");
|
||||
feed.set_sample(Some(crate::stylus::PenSample {
|
||||
pressure: 0.0,
|
||||
pose: PenPose { rotation: Some(90.0), ..Default::default() },
|
||||
..Default::default()
|
||||
}));
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
let vertical = tip(&mut app).expect("turning a still pen turns its tip");
|
||||
assert!(vertical.height() > vertical.width() * 2.0, "{vertical:?}");
|
||||
feed.set(None);
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
assert!(tip(&mut app).is_none(), "a mouse clears a still pen's tip");
|
||||
}
|
||||
|
||||
/// #764: the pen's tilt and barrel rotation in the host's feed reach the Paintbrush's points,
|
||||
/// the tilt turned onto the page when the view is rotated; a mouse's points carry neither.
|
||||
#[test]
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
//! the tip selects the tool used before.
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
|
||||
use std::sync::{Arc, Condvar, Mutex, MutexGuard, OnceLock};
|
||||
use std::time::Duration;
|
||||
|
||||
pub use vectorcraft_tools::PenPose;
|
||||
@@ -82,6 +82,8 @@ impl Pen {
|
||||
#[derive(Default)]
|
||||
struct Shared {
|
||||
pen: Mutex<Pen>,
|
||||
/// Wake the canvas for a pen that turns without a new window pointer event.
|
||||
repaint: OnceLock<egui::Context>,
|
||||
/// Signaled on every [`StylusFeed::set_sample`] ([`StylusFeed::set`] included).
|
||||
set: Condvar,
|
||||
/// Written on the UI thread itself ([`StylusFeed::on_ui_thread`]): a press never waits.
|
||||
@@ -104,6 +106,10 @@ impl StylusFeed {
|
||||
self.0.pen.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
}
|
||||
|
||||
pub(crate) fn bind_repaint(&self, ctx: &egui::Context) {
|
||||
self.0.repaint.get_or_init(|| ctx.clone());
|
||||
}
|
||||
|
||||
/// A pen turned to its tip moved with `pressure`, or `None`: another pointer moved
|
||||
/// ([`Self::set_sample`]).
|
||||
pub fn set(&self, pressure: Option<f32>) {
|
||||
@@ -119,6 +125,7 @@ impl StylusFeed {
|
||||
let pressure = sample.map(|s| if s.pressure.is_finite() { s.pressure.clamp(0.0, 1.0) } else { 1.0 });
|
||||
let eraser = sample.is_some_and(|s| s.eraser);
|
||||
let mut pen = self.lock();
|
||||
let before_pose = pen.pose;
|
||||
if eraser != pen.eraser {
|
||||
*pen = Pen::default();
|
||||
}
|
||||
@@ -138,8 +145,12 @@ impl StylusFeed {
|
||||
}
|
||||
}
|
||||
pen.latest = pressure;
|
||||
let pose_changed = pen.pose != before_pose;
|
||||
drop(pen);
|
||||
self.0.set.notify_all();
|
||||
if pose_changed && let Some(ctx) = self.0.repaint.get() {
|
||||
ctx.request_repaint();
|
||||
}
|
||||
}
|
||||
|
||||
/// Like [`Self::set_sample`], for a pen sample that comes with one pointer move of the window,
|
||||
@@ -282,6 +293,30 @@ pub(crate) fn sync_eraser_tool(app: &mut VectorcraftApp) -> bool {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
#[test]
|
||||
fn changing_a_still_pens_pose_wakes_the_canvas() {
|
||||
let feed = StylusFeed::default();
|
||||
let ctx = egui::Context::default();
|
||||
let repaints = Arc::new(AtomicUsize::new(0));
|
||||
let count = repaints.clone();
|
||||
ctx.set_repaint_observer(move |_| {
|
||||
count.fetch_add(1, Ordering::Relaxed);
|
||||
});
|
||||
feed.bind_repaint(&ctx);
|
||||
let pose = PenPose { rotation: Some(0.0), ..Default::default() };
|
||||
let sample = PenSample { pressure: 0.0, pose, ..Default::default() };
|
||||
let writer = feed.clone();
|
||||
std::thread::spawn(move || writer.set_sample(Some(sample))).join().unwrap();
|
||||
assert_eq!(repaints.load(Ordering::Relaxed), 1);
|
||||
feed.set_sample(Some(sample));
|
||||
assert_eq!(repaints.load(Ordering::Relaxed), 1, "an unchanged pose needs no new frame");
|
||||
feed.set_sample(Some(PenSample { pose: PenPose { rotation: Some(90.0), ..pose }, ..sample }));
|
||||
assert_eq!(repaints.load(Ordering::Relaxed), 2);
|
||||
feed.set_sample(None);
|
||||
assert_eq!(repaints.load(Ordering::Relaxed), 3, "mouse input clears the tip");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_feed_keeps_the_last_pressure_within_0_to_1() {
|
||||
|
||||
Reference in New Issue
Block a user