mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 04:04:25 +00:00
Curvature Pen: a press away from every end point carries on the subpath the host names (PenTool::carry_on), adding its anchor to it
This commit is contained in:
@@ -26,6 +26,7 @@ impl PenTool {
|
||||
/// - the second click of a double click on an anchor toggles it between smooth and corner and reflows; on empty
|
||||
/// canvas the first click's anchor is under it, so a double click places a corner point.
|
||||
pub(super) fn press_curve(&mut self, pointer: &ToolPointer, view: &CanvasView, reach: f32) -> ToolAction {
|
||||
self.carry_on_selected(pointer.pos, view, reach);
|
||||
let mut draft = self.shown.clone();
|
||||
let n = draft.knots.len();
|
||||
let under = draft
|
||||
@@ -80,6 +81,18 @@ impl PenTool {
|
||||
ToolAction::Claimed
|
||||
}
|
||||
|
||||
/// With no draft and the press not on an end point of an open path, the subpath [`carry_on`](PenTool::carry_on)
|
||||
/// names becomes the draft, reported as [`PenEvent::Resumed`]: the press then adds its anchor to it.
|
||||
fn carry_on_selected(&mut self, pos: Pos2, view: &CanvasView, reach: f32) {
|
||||
if !self.shown.knots.is_empty() || self.ends.iter().any(|(_, _, p)| view.to_screen(*p).distance(pos) <= reach) {
|
||||
return;
|
||||
}
|
||||
let Some((path, knot, draft)) = self.carry.take() else { return };
|
||||
self.kept = draft.knots.len();
|
||||
self.shown = draft;
|
||||
self.events.push(PenEvent::Resumed(path, knot));
|
||||
}
|
||||
|
||||
/// The draft line, the hover preview, the square anchors (the last filled) and the close cue.
|
||||
pub(super) fn paint_curve(&self, draw: &Draw<'_>, cx: &ToolContext<'_>, look: &PenStyle, view: &CanvasView) {
|
||||
let draft = &self.shown;
|
||||
|
||||
@@ -180,6 +180,8 @@ pub struct PenTool {
|
||||
pub(super) auto_add_delete: bool,
|
||||
options: PathOptionsSettings,
|
||||
pub(super) ends: Vec<(u64, KnotRef, Point)>,
|
||||
/// The open subpath a Curvature press anywhere carries on, as the draft it would make ([`carry_on`](Self::carry_on)).
|
||||
pub(super) carry: Option<(u64, KnotRef, EditSubpath)>,
|
||||
/// The draft being drawn; empty between drafts.
|
||||
pub(super) shown: EditSubpath,
|
||||
run: Option<Run>,
|
||||
@@ -210,6 +212,7 @@ impl PenTool {
|
||||
auto_add_delete: false,
|
||||
options: PathOptionsSettings::default(),
|
||||
ends: Vec::new(),
|
||||
carry: None,
|
||||
shown: EditSubpath::default(),
|
||||
run: None,
|
||||
resume: false,
|
||||
@@ -309,6 +312,15 @@ impl PenTool {
|
||||
self
|
||||
}
|
||||
|
||||
/// The open subpath the next press carries on, with no draft: its path's key, its end's knot and the subpath as the
|
||||
/// draft it becomes (its anchors in drawing order, the end last). In Curvature mode a press that is not on an end
|
||||
/// point of [`ends`](Self::ends) reports [`PenEvent::Resumed`] for it and adds its anchor to that draft, whose
|
||||
/// anchors are [`kept`](Self::kept). The host gives the end it has selected (Photoshop carries on from it).
|
||||
pub fn carry_on(&mut self, carry: Option<(u64, KnotRef, &EditSubpath)>) -> &mut Self {
|
||||
self.carry = carry.map(|(path, knot, draft)| (path, knot, draft.clone()));
|
||||
self
|
||||
}
|
||||
|
||||
/// Removes the draft's last anchor: the host's Undo while a draft is open. The only anchor discards the draft.
|
||||
/// Returns whether it did anything: false, leaving Undo to the app, with no draft, a [`PenDraft::Foreign`] one,
|
||||
/// or a press that is still down.
|
||||
|
||||
@@ -356,6 +356,22 @@ fn carried_anchors_are_neither_toggled_nor_grabbed() {
|
||||
assert!(draft.knots.len() > 3, "a press on a carried anchor adds an anchor");
|
||||
}
|
||||
|
||||
// With a subpath to carry on, a Curvature press away from every end point reports the resume and adds its anchor to
|
||||
// the carried subpath, whose anchors keep their handles.
|
||||
#[trial]
|
||||
fn a_curvature_press_carries_on_the_given_subpath() {
|
||||
let mut pen = pen();
|
||||
pen.fixture.tool_mut().mode(PenMode::Curvature);
|
||||
let carried = EditSubpath::new(vec![Knot::corner(doc(60.0, 60.0)), Knot::corner(doc(160.0, 60.0))], false);
|
||||
pen.fixture.tool_mut().carry_on(Some((7, craft_draw::ui::pen::KnotRef::new(0, 1), &carried)));
|
||||
pen.click_doc(300.0, 200.0);
|
||||
assert_eq!(pen.kinds().first(), Some(&"resumed"), "{:?}", pen.kinds());
|
||||
let draft = pen.draft();
|
||||
assert_eq!(draft.knots.len(), 3, "the carried anchors and the new one");
|
||||
assert_eq!((draft.knots[0], draft.knots[1].anchor), (carried.knots[0], carried.knots[1].anchor), "the carried anchors keep their places");
|
||||
assert_eq!(draft.knots[2].anchor, doc(300.0, 200.0));
|
||||
}
|
||||
|
||||
/// Three clicks of the Curvature Pen far apart give three anchors, each click an `Update`: on its own, and with the
|
||||
/// host handing the draft it holds back in before every frame, as an app's per-pass configuration does.
|
||||
#[trial]
|
||||
|
||||
Reference in New Issue
Block a user