diff --git a/crates/engine/src/cmd/buildcmds/live_paint.rs b/crates/engine/src/cmd/buildcmds/live_paint.rs index 02ba081477..f943766452 100644 --- a/crates/engine/src/cmd/buildcmds/live_paint.rs +++ b/crates/engine/src/cmd/buildcmds/live_paint.rs @@ -37,7 +37,7 @@ fn check_live_paint_takes(d: &Document, roots: &[NodeId]) -> Result<()> { fn make_live(d: &mut Document, sel: &mut Selection, roots: &[NodeId]) -> Result<(NodeId, usize, usize)> { check_live_paint_takes(d, roots)?; if roots.iter().any(|r| d.node(*r).is_some_and(is_live_paint)) { - return merge_live(d, sel, roots); + return merge_live(d, sel, roots, false); } let mut sources = vec![]; for r in roots { @@ -178,8 +178,10 @@ impl Carried { /// Add the other `roots` (back → front) to the first Live Paint group among them, which keeps its /// id, the paint of its faces and edges, and its own properties apart from the Gap Options, which -/// are merged; returns (group, faces, edges). -fn merge_live(d: &mut Document, sel: &mut Selection, roots: &[NodeId]) -> Result<(NodeId, usize, usize)> { +/// are merged; returns (group, faces, edges). When the groups and paths hold no path outline +/// between them, `keep_empty` leaves the group with its paths in its sources group and no faces or +/// edges; without it, that is an error. +fn merge_live(d: &mut Document, sel: &mut Selection, roots: &[NodeId], keep_empty: bool) -> Result<(NodeId, usize, usize)> { let target = roots .iter() .copied() @@ -212,7 +214,14 @@ fn merge_live(d: &mut Document, sel: &mut Selection, roots: &[NodeId]) -> Result // Merge applies the Gap Options' merge rules to the groups' options, bottom-most first. let groups: Vec = roots.iter().filter_map(|r| d.node(*r)).filter(|n| is_live_paint(n)).map(Node::gap_options).collect(); let options = merged_gap_options(&groups); - let (children, nf, ne) = build_children(d, sources, options.max_gap())?; + let (children, nf, ne) = if keep_empty && source_shapes(&sources).0.is_empty() { + let mut src = Node::group(d.alloc_id(), sources); + src.name = Some(SOURCES_NAME.into()); + src.visible = false; + (vec![Arc::new(src)], 0, 0) + } else { + build_children(d, sources, options.max_gap())? + }; // The group keeps its name, lock, visibility, transparency, appearance, opacity mask, graphic // style link and the rest; its children are made again and its Gap Options set to the merged ones. let mut g = old; @@ -266,7 +275,7 @@ pub(super) fn lp_merge(s: &mut Session, p: &Value) -> Result { if let Some(reply) = lone_live_group(s, &roots)? { return Ok(reply); } - let (id, faces, edges) = s.edit("Merge Live Paint", |d, sel| merge_live(d, sel, &roots))?; + let (id, faces, edges) = s.edit("Merge Live Paint", |d, sel| merge_live(d, sel, &roots, false))?; Ok(json!({ "id": id.0, "faces": faces, "edges": edges })) } @@ -301,7 +310,7 @@ pub(crate) fn merge_drawn_in_isolation(s: &mut Session, g: NodeId) -> Result = std::iter::once(g).chain(drawn).collect(); - s.edit("Merge Live Paint", |d, sel| merge_live(d, sel, &roots))?; + s.edit("Merge Live Paint", |d, sel| merge_live(d, sel, &roots, false))?; Ok(true) } @@ -346,12 +355,31 @@ fn enclosing_live_paints(d: &Document, g: NodeId) -> Vec { out } +/// Whether a shown row whose parent is `parent`, among Live Paint group `g`'s paths, gives `g` faces +/// and edges: every group between it and `g`'s sources group is shown. +fn shown_in_sources(d: &Document, parent: Option, g: NodeId) -> bool { + let src = d.node(g).and_then(b::sources).map(|s| s.id); + let mut at = parent; + while let Some(x) = at { + if Some(x) == src { + return true; + } + if !d.node(x).is_some_and(|n| n.visible) { + return false; + } + at = d.parent_of(x); + } + true +} + /// What moving rows in the Layers panel (`layer.move`, `node.move`) does to Live Paint groups. /// Rows that go into a Live Paint group from elsewhere are added to it as `livePaint.merge` adds /// paths. Rows that go into or leave a group's paths (its sources group) change those paths, and /// the group's faces and edges are made again from them, keeping the paint of the faces and edges /// that stay, and so are those of every Live Paint group that holds a changed group among its -/// paths. Rows moved within one group's paths, or among a group's own children, change no faces. +/// paths. Rows moved within one group's paths change no faces unless a shown row goes into or out +/// of a hidden group there; rows moved among a group's own children change no faces. A group's +/// sources group itself stays in the group. pub(crate) struct LivePaintMove { /// The Live Paint group the rows go into, and for each row whether it joins the group's paths. join: Option<(NodeId, Vec)>, @@ -365,16 +393,29 @@ pub(crate) struct LivePaintMove { impl LivePaintMove { /// Plan moving `rows` (with `copy`, copies of them) into `parent`, before anything changes. /// Refuses rows Live Paint can't take ([`b::live_paint_takes`]) that would go among a group's - /// paths, with the message Make and Merge give. + /// paths, with the message Make and Merge give, and a group's sources group moved out of the + /// group. pub(crate) fn plan(d: &Document, rows: &[NodeId], parent: Option, copy: bool) -> Result { + if !copy { + for r in rows { + if let Some(g) = d.parent_of(*r) + && parent != Some(g) + && d.node(g).filter(|n| is_live_paint(n)).and_then(b::sources).is_some_and(|s| s.id == *r) + { + return Err(EngineError::Other("a Live Paint group's sources group stays in the group: move the paths in it instead".into())); + } + } + } let to = spot_in(d, parent); let from: Vec = rows.iter().map(|r| if copy { Spot::Outside } else { spot_in(d, d.parent_of(*r)) }).collect(); let mut rebuild = vec![]; - for f in &from { - if let Spot::Source(g) = *f - && *f != to - && !rebuild.contains(&g) - { + for (r, f) in rows.iter().zip(&from) { + let Spot::Source(g) = *f else { continue }; + // A shown row that goes into or out of a hidden group among the same paths starts or + // stops giving the group faces and edges. + let shown_changes = + *f == to && d.node(*r).is_some_and(|n| n.visible) && shown_in_sources(d, d.parent_of(*r), g) != shown_in_sources(d, parent, g); + if (*f != to || shown_changes) && !rebuild.contains(&g) { rebuild.push(g); } } @@ -444,7 +485,9 @@ impl LivePaintMove { for g in groups { if Some(g) == join_group { let roots: Vec = std::iter::once(g).chain(joined.iter().copied()).collect(); - merge_live(d, sel, &roots)?; + // Rows that join a group with no path outline among them keep the move: the group + // is left with no faces or edges, as when its last path leaves it. + merge_live(d, sel, &roots, true)?; } else if d.node(g).is_some() { rebuild_paths(d, g)?; } diff --git a/crates/engine/src/tests/build.rs b/crates/engine/src/tests/build.rs index 8296619f91..f8ebd9a93b 100644 --- a/crates/engine/src/tests/build.rs +++ b/crates/engine/src/tests/build.rs @@ -5474,6 +5474,90 @@ fn layers_drag_into_a_nested_live_paint_group_makes_the_outer_group_again() { assert_eq!(faces(&node(&s, outer)).len(), cut, "the outer group is made after the inner one"); } +/// A Live Paint group's sources group can't be dragged out of the group, which would leave faces +/// and edges with no paths: the move is refused and changes nothing. A reorder within the group, +/// and a copy dragged out, are allowed. +#[test] +fn layers_drag_keeps_the_sources_group_in_its_live_paint_group() { + use vectorcraft_tools::builder::{faces, is_live_paint, sources}; + let mut s = session(); + let g = live(&mut s); + let layer = s.doc().unwrap().doc.parent_of(g).unwrap(); + let src = sources(&node(&s, g)).unwrap().id; + let before = (s.doc().unwrap().doc.clone(), s.doc().unwrap().history.undo.len()); + let err = s.execute("layer.move", &json!({"ids": [src.0], "target": layer.0})).unwrap_err().to_string(); + assert!(err.contains("sources group stays in the group"), "{err}"); + let err = s.execute("node.move", &json!({"id": src.0, "parent": layer.0, "index": 0})).unwrap_err().to_string(); + assert!(err.contains("sources group stays in the group"), "{err}"); + assert!(s.doc().unwrap().doc == before.0, "nothing moved"); + assert_eq!(s.doc().unwrap().history.undo.len(), before.1); + s.execute("node.move", &json!({"id": src.0, "parent": g.0, "index": 99})).unwrap(); + let n = node(&s, g); + assert!(is_live_paint(&n)); + assert_eq!(sources(&n).unwrap().id, src); + assert_eq!(faces(&n).len(), 3); + s.execute("layer.move", &json!({"ids": [src.0], "target": layer.0, "copy": true})).unwrap(); + assert_eq!(s.doc().unwrap().doc.parent_of(src), Some(g), "a copy leaves the sources group where it is"); +} + +/// A shown path dragged into a hidden group among a Live Paint group's paths stops cutting its +/// faces, and dragged back out of it cuts them again. +#[test] +fn layers_drag_into_a_hidden_group_among_the_sources_makes_the_faces_again() { + use vectorcraft_tools::builder::{faces, sources}; + let mut s = session(); + let g = live(&mut s); + let first_source = |s: &Session| sources(&node(s, g)).unwrap().children().unwrap()[0].id; + let a = line_to_right_face(&mut s, 30.0); + let target = first_source(&s); + s.execute("layer.move", &json!({"ids": [a.0], "target": target.0, "place": "above"})).unwrap(); + assert_eq!(faces(&node(&s, g)).len(), 5); + let b = line_to_right_face(&mut s, 70.0); + s.execute("select.set", &json!({"ids": [b.0]})).unwrap(); + let hidden = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); + s.execute("object.hide", &json!({"ids": [hidden.0]})).unwrap(); + let target = first_source(&s); + s.execute("layer.move", &json!({"ids": [hidden.0], "target": target.0, "place": "above"})).unwrap(); + let src = sources(&node(&s, g)).unwrap().id; + assert_eq!(s.doc().unwrap().doc.parent_of(hidden), Some(src)); + assert_eq!(faces(&node(&s, g)).len(), 5, "a hidden group cuts nothing"); + s.execute("layer.move", &json!({"ids": [a.0], "target": hidden.0, "place": "inside"})).unwrap(); + assert_eq!(s.doc().unwrap().doc.parent_of(a), Some(hidden)); + assert_eq!(faces(&node(&s, g)).len(), 3, "the path in the hidden group no longer cuts the faces"); + let target = first_source(&s); + s.execute("layer.move", &json!({"ids": [a.0], "target": target.0, "place": "above"})).unwrap(); + assert_eq!(s.doc().unwrap().doc.parent_of(a), Some(sources(&node(&s, g)).unwrap().id)); + assert_eq!(faces(&node(&s, g)).len(), 5, "back among the shown paths, it cuts them again"); +} + +/// A Live Paint group with no paths dragged into another one with none joins it: the drop is one +/// "Merge Live Paint" step, and the group stays a Live Paint group with no faces or edges. +#[test] +fn layers_drag_of_an_empty_live_paint_group_into_another_joins_it() { + use vectorcraft_tools::builder::{edges, faces, is_live_paint, sources}; + let mut s = session(); + let empty = |s: &mut Session| { + let g = live(s); + let layer = s.doc().unwrap().doc.parent_of(g).unwrap(); + for p in sources(&node(s, g)).unwrap().children().unwrap().iter().map(|c| c.id).collect::>() { + s.execute("layer.move", &json!({"ids": [p.0], "target": layer.0})).unwrap(); + } + assert!(faces(&node(s, g)).is_empty()); + g + }; + let (g, h) = (empty(&mut s), empty(&mut s)); + let r = s.execute("layer.move", &json!({"ids": [h.0], "target": g.0, "place": "inside"})).unwrap(); + assert_eq!(r["ids"], json!([g.0])); + assert_eq!(undo_label(&s).as_deref(), Some("Merge Live Paint")); + let n = node(&s, g); + assert!(is_live_paint(&n)); + assert!(sources(&n).unwrap().children().unwrap().is_empty()); + assert!(faces(&n).is_empty() && edges(&n).is_empty()); + assert!(s.doc().unwrap().doc.node(h).is_none(), "the dropped group joined"); + s.execute("edit.undo", &json!({})).unwrap(); + assert!(s.doc().unwrap().doc.node(h).is_some()); +} + /// Give Live Paint group `g` a lock, hidden state, appearance, knockout, opacity mask and /// graphic style link, and return it as it then is. fn dress_live_group(s: &mut Session, g: NodeId) -> Node {