diff --git a/crates/engine/src/cmd/buildcmds.rs b/crates/engine/src/cmd/buildcmds.rs index 7bf0dadfc6..df4b03e3f0 100644 --- a/crates/engine/src/cmd/buildcmds.rs +++ b/crates/engine/src/cmd/buildcmds.rs @@ -126,7 +126,7 @@ pub fn specs() -> Vec { "Live Paint Bucket (Stroke)", [], None, - "{group: id, point: [x,y], color?|swatch?|none? (default: current stroke), width?: pt (default: current weight), tolerance?: pt (4), scope?: \"one\" (default: the edge alone) | \"connected\" (a double-click: also each edge joined to it, end to end, through edges that draw with stroke paint match) | \"samePaint\" (a triple-click: also every edge of the group that draws with stroke paint match), match?: paint (as the document stores it; scope connected or samePaint only; default: the stroke paint the edge draws with; an edge draws with the paint of its topmost stroke that is shown, with paint, above 0 pt and not fully transparent, and with none when the edge is fully transparent or has no such stroke), tint?: 0..100, keepModel?: bool (both as paint.setFill)} paint the edge nearest point, skipping hidden edges; when every edge it reaches already has the paint and the width, nothing changes (no undo step, and the recent colors stay as they are) → {group, edge, count (edges reached)}", + "{group: id, point: [x,y], color?|swatch?|none? (default: current stroke), width?: pt (default: current weight), tolerance?: pt (4), scope?: \"one\" (default: the edge alone) | \"connected\" (a double-click: also each edge joined to it, end to end, through edges that draw with stroke paint match) | \"samePaint\" (a triple-click: also every edge of the group that draws with stroke paint match), match?: paint (as the document stores it; scope connected or samePaint only; default: the stroke paint the edge draws with; an edge draws with the paint of its topmost stroke that is shown, with paint, above 0 pt and not fully transparent, and with none when the edge is fully transparent or has no such stroke), tint?: 0..100, keepModel?: bool (both as paint.setFill)} paint the edge nearest point, skipping hidden edges: each edge reached takes the paint and width in its topmost stroke that is shown, or in a new stroke above the others when it has none; when every edge it reaches already has the paint and the width there, nothing changes (no undo step, and the recent colors stay as they are) → {group, edge, count (edges reached)}", has_doc, lp_stroke_edge ), diff --git a/crates/engine/src/cmd/buildcmds/live_paint.rs b/crates/engine/src/cmd/buildcmds/live_paint.rs index 2778e0495a..fff0aae76b 100644 --- a/crates/engine/src/cmd/buildcmds/live_paint.rs +++ b/crates/engine/src/cmd/buildcmds/live_paint.rs @@ -1,7 +1,7 @@ use super::{ - Appearance, Arc, Beside, Document, EDGE_NAME, EngineError, FACE_NAME, GapOptions, GapSize, LIVE_PAINT_NAME, Node, NodeId, Paint, PathData, Point, - Rect, Result, SOURCES_NAME, Selection, Session, SubPath, Value, b, bad, build_children, edge_near, face_at, finite, ids_json, is_live_paint, - json, number_or, object_param, paint_from, po, point_param, repaint_from, replace_roots, root_ids, source_nodes, source_shapes, switch_or, + Appearance, Arc, Beside, Document, EDGE_NAME, EngineError, FACE_NAME, GapOptions, GapSize, LIVE_PAINT_NAME, Node, NodeId, PathData, Point, Rect, + Result, SOURCES_NAME, Selection, Session, SubPath, Value, b, bad, build_children, edge_near, face_at, finite, ids_json, is_live_paint, json, + number_or, object_param, paint_from, po, point_param, repaint_from, replace_roots, root_ids, source_nodes, source_shapes, switch_or, }; /// `options` as stored on a Live Paint group: none for the defaults. @@ -367,11 +367,12 @@ pub(super) fn lp_gap_options(s: &mut Session, p: &Value) -> Result { enum LpScope { /// The part under the pointer alone (a click). One, - /// A face and the faces reached from it across unstroked edges, or an edge and the edges - /// joined to it that have the same stroke (a double-click). + /// A face and the faces reached from it across edges that draw no stroke ([`b::flood_faces`]), + /// or an edge and the edges joined to it end to end that draw with the same stroke paint + /// ([`b::connected_edges`], [`b::lp_stroke_paint`]) (a double-click). Connected, - /// Every face with the same fill, or every edge with the same stroke, in the group (a - /// triple-click). + /// Every face with the same fill, or every edge that draws with the same stroke paint, in the + /// group (a triple-click). SamePaint, } @@ -391,12 +392,12 @@ fn scope_param(p: &Value, c: &str) -> Result { /// The `match` of `livePaint.fill` and `livePaint.strokeEdge`: a paint as the document stores it, /// None when left out or null. With `allowed` false (a scope that compares no paints) a `match` is /// an error naming `scopes`, the scopes it applies to. -fn match_param(p: &Value, c: &str, allowed: bool, scopes: &str) -> Result> { +fn match_param(p: &Value, c: &str, allowed: bool, scopes: &str) -> Result> { let Some(v) = p.get("match").filter(|v| !v.is_null()) else { return Ok(None) }; if !allowed { return Err(bad(c, format!("match applies only with scope {scopes}"))); } - serde_json::from_value::(v.clone()).map(Some).map_err(|_| bad(c, "match must be a paint as the document stores it")) + serde_json::from_value::(v.clone()).map(Some).map_err(|_| bad(c, "match must be a paint as the document stores it")) } pub(super) fn lp_fill(s: &mut Session, p: &Value) -> Result { @@ -570,12 +571,13 @@ pub(super) fn lp_stroke_edge(s: &mut Session, p: &Value) -> Result { .collect(), }; let count = targets.len(); - // An edge that already has the paint and the width (or has no stroke and gets none) stays as - // it is; when every edge does, nothing changes and no undo step is recorded. + // An edge whose shown stroke already has the paint and the width (or that has no shown stroke + // and gets none) stays as it is; when every edge does, nothing changes and no undo step is + // recorded. let changed: Vec = targets .into_iter() .filter(|t| { - d.node(*t).is_none_or(|e| match e.appearance.stroke() { + d.node(*t).is_none_or(|e| match shown_stroke(e) { Some(st) => !(st.paint == paint && st.width == width), None => !paint.is_none(), }) @@ -589,9 +591,12 @@ pub(super) fn lp_stroke_edge(s: &mut Session, p: &Value) -> Result { s.edit("Live Paint Bucket", |d, _| { for target in &changed { let e = d.node_mut(*target).ok_or(EngineError::NoNode(*target))?; - e.appearance.set_stroke(paint.clone()); - if let Some(st) = e.appearance.stroke_mut() { - st.width = width; + match shown_stroke_mut(e) { + Some(st) => { + st.paint = paint.clone(); + st.width = width; + } + None => e.appearance.items.push(super::AppearanceItem::Stroke(super::StrokeLayer::new(paint.clone(), width))), } } Ok(()) @@ -600,6 +605,24 @@ pub(super) fn lp_stroke_edge(s: &mut Session, p: &Value) -> Result { Ok(json!({ "group": g.0, "edge": eid.0, "count": count })) } +/// The stroke `livePaint.strokeEdge` paints on Live Paint edge `e`: its topmost stroke that is +/// shown. None when it has no stroke or every stroke is hidden; the command then adds a stroke +/// above the others. +fn shown_stroke(e: &Node) -> Option<&super::StrokeLayer> { + e.appearance.items.iter().rev().find_map(|i| match i { + super::AppearanceItem::Stroke(s) if s.visible => Some(s), + _ => None, + }) +} + +/// [`shown_stroke`], to change. +fn shown_stroke_mut(e: &mut Node) -> Option<&mut super::StrokeLayer> { + e.appearance.items.iter_mut().rev().find_map(|i| match i { + super::AppearanceItem::Stroke(s) if s.visible => Some(s), + _ => None, + }) +} + pub(super) fn lp_info(s: &mut Session, p: &Value) -> Result { let g = match object_param(p, "group", "livePaint.info")? { Some(g) => g, diff --git a/crates/engine/src/tests/build.rs b/crates/engine/src/tests/build.rs index b616aaa8df..315085a874 100644 --- a/crates/engine/src/tests/build.rs +++ b/crates/engine/src/tests/build.rs @@ -3079,6 +3079,59 @@ fn live_paint_bucket_scopes_follow_the_stroke_an_edge_draws() { }; assert_eq!(stroke(&mut s, [25.0, 0.0]), json!(all.len() - clear.len() - hidden.len()), "the edges that draw black"); assert_eq!(stroke(&mut s, [50.0, 25.0]), json!(clear.len() + hidden.len()), "the edges that draw no stroke"); + // With the top edge between them fully transparent too, the connected scope walks from the edge + // at x = 50 through it to the edge at x = 100, and reaches no edge that draws black. + let top = edges_in(&s, g, Rect::new(50.0, 0.0, 100.0, 0.0)); + assert_eq!(top.len(), 1); + s.edit("t", |d, _| { + d.node_mut(top[0]).unwrap().opacity = 0.0; + Ok(()) + }) + .unwrap(); + let r = + s.execute("livePaint.strokeEdge", &json!({"group": g.0, "point": [50, 25], "color": "#00ff00", "width": 3, "scope": "connected"})).unwrap(); + assert_eq!(r["count"], json!(clear.len() + top.len() + hidden.len())); +} + +/// `livePaint.strokeEdge` paints an edge's topmost stroke that is shown, so the edge then draws +/// with the paint; an edge whose strokes are all hidden takes a new stroke above them. Painting +/// again changes nothing. +#[test] +fn live_paint_stroke_edge_paints_the_shown_stroke() { + let mut s = session(); + let g = live_squares(&mut s, &[0.0, 50.0]); + let black = Paint::solid(Color::rgb(0.0, 0.0, 0.0)); + let red = Paint::solid(Color::rgb(1.0, 0.0, 0.0)); + let green = Paint::solid(Color::from_hex("#00ff00").unwrap()); + let all = edges_in(&s, g, Rect::new(-1.0, -1.0, 101.0, 51.0)); + paint_parts(&mut s, &all, &black, &[]); + let middle = edges_in(&s, g, Rect::new(50.0, 0.0, 50.0, 50.0)); + let left: Vec = edges_in(&s, g, Rect::new(0.0, 0.0, 50.0, 50.0)).into_iter().filter(|e| !middle.contains(e)).collect(); + let (&[m], &[l]) = (&middle[..], &left[..]) else { panic!("one middle edge and one other edge round the left face") }; + // The middle edge draws red under a hidden black stroke; every stroke of the left face's other + // edge is hidden. + s.edit("t", |d, _| { + let n = d.node_mut(m).unwrap(); + n.appearance.items.insert(0, AppearanceItem::Stroke(vectorcraft_doc::appearance::StrokeLayer::new(red.clone(), 1.0))); + n.appearance.stroke_mut().unwrap().visible = false; + d.node_mut(l).unwrap().appearance.stroke_mut().unwrap().visible = false; + Ok(()) + }) + .unwrap(); + let drawn = |s: &Session, id: NodeId| vectorcraft_tools::builder::lp_stroke_paint(&node(s, id)); + assert_eq!(drawn(&s, m), red); + let paint = |s: &mut Session, at: [f64; 2]| { + s.execute("livePaint.strokeEdge", &json!({"group": g.0, "point": at, "color": "#00ff00", "width": 2})).unwrap(); + }; + for (id, at) in [(m, [50.0, 25.0]), (l, [0.0, 25.0])] { + paint(&mut s, at); + assert_eq!(drawn(&s, id), green, "{id}"); + let depth = history_state(&s).0; + paint(&mut s, at); + assert_eq!(history_state(&s).0, depth, "painting {id} again"); + } + assert_eq!(node(&s, m).appearance.items.len(), 2, "the hidden stroke stays as it was"); + assert_eq!(node(&s, l).appearance.items.len(), 2, "a new stroke above the hidden one"); } /// With Cmd held, the Live Paint Bucket's presses go to the selection tool Cmd lends, and the diff --git a/crates/tools/src/builder/live_paint.rs b/crates/tools/src/builder/live_paint.rs index 0694d67ab3..e99aec8756 100644 --- a/crates/tools/src/builder/live_paint.rs +++ b/crates/tools/src/builder/live_paint.rs @@ -223,7 +223,8 @@ struct LpPress { part: Option, /// Where the press was. at: Point, - /// Its fill (a face) or stroke paint (an edge) before the press. + /// Its fill (a face), or the stroke paint it draws with ([`lp_stroke_paint`], an edge), before + /// the press. before: Paint, } @@ -907,7 +908,8 @@ impl Tool for LivePaintSelectionTool { (false, false) => vec![Action::Exec("select.set".into(), json!({ "ids": json_ids(&ids) }))], } } - // The third click's press and release already selected what a click selects. + // The tool takes no action on a triple-click; its third press and release select as a + // click does. PointerKind::TripleClick => vec![], } } diff --git a/crates/tools/src/builder/tests.rs b/crates/tools/src/builder/tests.rs index 28daa4fdc9..27a19c3a07 100644 --- a/crates/tools/src/builder/tests.rs +++ b/crates/tools/src/builder/tests.rs @@ -1206,6 +1206,39 @@ fn bucket_double_and_triple_click_widen_what_a_click_paints() { assert!(t.pointer(&cx, &ev(PointerKind::DoubleClick, 400.0, 400.0)).is_empty()); } +/// On an edge that stores a stroke paint but draws none (its stroke hidden, or the edge fully +/// transparent), a double-click or triple-click matches no paint, with or without the clicks +/// before it. +#[test] +fn bucket_multi_click_on_an_edge_matches_the_paint_it_draws() { + let (mut d, gid, _, e) = live_group_doc(); + d.node_mut(e).unwrap().appearance.set_stroke(vectorcraft_color::Paint::solid(vectorcraft_color::Color::rgb(0.0, 0.0, 0.0))); + let mut hidden = d.clone(); + hidden.node_mut(e).unwrap().appearance.stroke_mut().unwrap().visible = false; + let mut clear = d.clone(); + clear.node_mut(e).unwrap().opacity = 0.0; + let none = serde_json::to_value(vectorcraft_color::Paint::None).unwrap(); + let s = Selection::default(); + let p = paint(); + let shift = Mods { shift: true, ..Default::default() }; + let stroke = |scope: &str| { + vec![Action::Exec( + "livePaint.strokeEdge".into(), + json!({ "group": gid.0, "point": [51.0, 25.0], "tolerance": 4.0, "scope": scope, "match": none }), + )] + }; + for doc in [&hidden, &clear] { + let cx = cx(doc, &s, &p); + let mut t = LivePaintBucketTool::default(); + assert_eq!(t.pointer(&cx, &ev(PointerKind::DoubleClick, 51.0, 25.0).with_mods(shift)), stroke("connected")); + for _ in 0..3 { + t.pointer(&cx, &ev(PointerKind::Down, 51.0, 25.0).with_mods(shift)); + t.pointer(&cx, &ev(PointerKind::Up, 51.0, 25.0).with_mods(shift)); + } + assert_eq!(t.pointer(&cx, &ev(PointerKind::TripleClick, 51.0, 25.0).with_mods(shift)), stroke("samePaint")); + } +} + /// A triple-click whose first click makes the Live Paint group matches the fill of the face the /// group makes there: an open path in front, whose outline bounds no face, does not give it. #[test] diff --git a/docs/mcp.md b/docs/mcp.md index add1e3a117..bd9ae904be 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3690,7 +3690,8 @@ already walks Image Trace. Short agent entry points: transparent or has no such stroke. Each click of a double-click or triple-click runs as it arrives and is its own undo step. `match` is the paint the face or edge had before the first of the double-click's two clicks or the triple-click's three that painted it, so the clicks before the last do not change what it matches; - a first click that made the Live Paint group counts when the face it filled is the clicked face. Hidden faces and edges are not painted. These rules + a first click that made the Live Paint group counts when the face it filled is the clicked face. Hidden faces and edges are not painted. An edge takes the + paint and weight in its topmost stroke that is shown, or in a new stroke above the others when it has none. These rules for what faces and edges join and what paints match are VectorCraft's own. The help page describes the gestures: https://helpx.adobe.com/illustrator/desktop/paint-and-fill/learn-painting-basics/paint-with-the-live-paint-bucket-tool.html. `livePaint.fill {scope}` and `livePaint.strokeEdge {scope}` take `scope` as `"one"` (the default), `"connected"` or @@ -3715,7 +3716,8 @@ already walks Image Trace. Short agent entry points: objects; otherwise it returns an error and does not merge the objects. A Live Paint command that changes nothing records no undo step, keeps the redo steps, leaves a saved document saved and replies as usual: `livePaint.fill` (a click, or a Bucket drag) when every face it reaches already has the paint, - `livePaint.strokeEdge` when the edge already has the paint and the width, `livePaint.gapOptions` (and the Gap Options + `livePaint.strokeEdge` when the topmost shown stroke of each edge it reaches already has the paint and the width, + `livePaint.gapOptions` (and the Gap Options dialog's OK) when every group already has the options and `closeGaps` finds no gap, and `livePaint.make` or `livePaint.merge` given one Live Paint group alone, which they select. Such a fill or stroke also leaves the recent colors and the last color as they are. `livePaint.gapOptions` makes a group's faces again, with new ids, only when it adds a path or when Gap Detection is turned on or off or given a new size while it is on.