Skip to content

armgit / klad

Public Imported from GitHub
0

A drawing app built for agent/human collaboration

Rust

Bending a stroke bends what is attached to it

Indicode BV0131a3314691
diff --git a/crates/app/src/editor/ui_tests.rs b/crates/app/src/editor/ui_tests.rs
index 5fec198..7ce1a56 100644
--- a/crates/app/src/editor/ui_tests.rs
+++ b/crates/app/src/editor/ui_tests.rs
@@ -2504,6 +2504,12 @@ fn the_brush_window_edits_a_brush_shows_it_and_saves_it_in_the_drawing() {
#[test]
fn a_bends_a_brush_stroke_and_the_stroke_keeps_its_pressure() {
let path = sample("reshape");
+ Bundle::open(&path)
+ .unwrap()
+ .apply(
+ &parse_lines(r#"{"op":"stroke","layer":"l6","brush":"eraser","path":"M60 40 L60 65","size":10}"#).unwrap(),
+ )
+ .unwrap();
let mut h = harness(editor(&path), vec2(1440.0, 900.0));
activate(&mut h, "notes");
h.key_press(Key::A);
@@ -2555,6 +2561,17 @@ fn a_bends_a_brush_stroke_and_the_stroke_keeps_its_pressure() {
.all(|(a, b)| a.pressure == b.pressure)
);
assert_eq!(h.state().direct.as_ref().map(|d| d.node.clone()), Some(node));
+ let eraser_y = |h: &Harness<'static, Editor>| {
+ let marks = h.state().scene.bundle.read_marks("l6", "c6").unwrap();
+ let eraser = marks.last().unwrap().as_stroke().unwrap().clone();
+ assert_eq!(eraser.on, vec![1], "the eraser sits on the bent stroke");
+ eraser.samples.iter().map(|s| s.pos().1).sum::<f32>() / eraser.samples.len() as f32
+ };
+ let bent = eraser_y(&h);
+ assert!(
+ bent - 52.5 > moved * 0.5,
+ "the cut near the start moved down with the stroke: {bent} for a start moved {moved}"
+ );
h.state_mut().invoke(Cmd::Undo).unwrap();
h.run_steps(2);
let undone = h.state().scene.bundle.read_marks("l6", "c6").unwrap()[0]
diff --git a/crates/core/src/attach.rs b/crates/core/src/attach.rs
index 46d1a00..a892ac9 100644
--- a/crates/core/src/attach.rs
+++ b/crates/core/src/attach.rs
@@ -293,6 +293,57 @@ pub fn follow(marks: &mut Vec<Mark>, chosen: &[u32], ids: MarkIds) -> Followed {
followed
}
+pub fn bend(marks: &mut Vec<Mark>, target: u32, old: &[(f32, f32)], ids: MarkIds) {
+ let Some(new) = marks
+ .iter()
+ .find(|m| m.id() == target)
+ .and_then(Mark::as_stroke)
+ .map(|s| s.samples.iter().map(Sample::pos).collect::<Vec<_>>())
+ else {
+ return;
+ };
+ let live: BTreeSet<u32> = marks.iter().map(Mark::id).collect();
+ let (from, to) = ([old.to_vec()], [new]);
+ let bent = |stroke: &mut Stroke| {
+ stroke.samples.iter_mut().for_each(|sample| {
+ let moved = crate::tween::anchor(&from, sample.pos()).and_then(|a| crate::tween::place(&a, &to));
+ if let Some((x, y)) = moved {
+ sample.x = (x * crate::stroke::POS_SCALE).round() as i32;
+ sample.y = (y * crate::stroke::POS_SCALE).round() as i32;
+ }
+ })
+ };
+ let mut next = ids.next(marks);
+ let out = std::mem::take(marks).into_iter().flat_map(|mark| {
+ let Mark::Stroke(mut stroke) = mark else {
+ return vec![mark];
+ };
+ if stroke.id == target || !stroke.on.contains(&target) {
+ return vec![Mark::Stroke(stroke)];
+ }
+ let others: Vec<u32> = stroke
+ .on
+ .iter()
+ .copied()
+ .filter(|id| *id != target && live.contains(id))
+ .collect();
+ if others.is_empty() {
+ bent(&mut stroke);
+ return vec![Mark::Stroke(stroke)];
+ }
+ let mut copy = Stroke {
+ id: next,
+ on: vec![target],
+ ..stroke.clone()
+ };
+ next += 1;
+ bent(&mut copy);
+ stroke.on = others;
+ vec![Mark::Stroke(stroke), Mark::Stroke(copy)]
+ });
+ *marks = out.collect();
+}
+
pub fn prune(marks: &mut Vec<Mark>, before: &BTreeSet<u32>) {
let live: BTreeSet<u32> = marks.iter().map(Mark::id).collect();
let gone = |id: &u32| before.contains(id) && !live.contains(id);
@@ -631,6 +682,47 @@ mod tests {
assert_eq!(start_x(&marks, 2), 100.0, "a detached eraser stays put");
}
+ #[test]
+ fn bending_a_stroke_bends_its_erasers_and_splits_one_it_shares() {
+ let bend = |marks: &mut Vec<Mark>| {
+ edit(
+ marks,
+ crate::markedit::MarkChange::Set {
+ id: 1,
+ props: serde_json::json!({"path": "M10 50 L200 240"})
+ .as_object()
+ .unwrap()
+ .clone(),
+ },
+ );
+ };
+ let mut alone = crossed();
+ if let Mark::Stroke(s) = &mut alone[2] {
+ s.on = vec![1];
+ }
+ bend(&mut alone);
+ let eraser = alone[2].as_stroke().unwrap();
+ let ends = (eraser.samples[0].pos(), eraser.samples[1].pos());
+ let near = |p: (f32, f32), q: (f32, f32)| (p.0 - q.0).abs() < 0.5 && (p.1 - q.1).abs() < 0.5;
+ assert!(
+ near(ends.0, (114.14, 125.86)) && near(ends.1, (57.57, 182.43)),
+ "the cut stays halfway along the line and turns with it: {ends:?}"
+ );
+ let mut shared = crossed();
+ bend(&mut shared);
+ assert_eq!(
+ on(&shared, 2),
+ (3, vec![2]),
+ "the eraser stays with the line that did not bend"
+ );
+ assert_eq!(on(&shared, 3).1, vec![1], "a copy right after it bent with the line");
+ assert_eq!(start_x(&shared, 2), 100.0);
+ assert_ne!(
+ shared[3].as_stroke().unwrap().samples[0].pos(),
+ shared[2].as_stroke().unwrap().samples[0].pos()
+ );
+ }
+
#[test]
fn attach_fills_in_a_stroke_already_in_the_list() {
let defs = defs();
diff --git a/crates/core/src/markedit.rs b/crates/core/src/markedit.rs
index 161ccda..8eec20a 100644
--- a/crates/core/src/markedit.rs
+++ b/crates/core/src/markedit.rs
@@ -653,11 +653,18 @@ pub fn apply_following(marks: &mut Vec<Mark>, change: &mut MarkChange, cel: &str
MarkChange::Set { id, props } => {
let at = position(marks, cel, *id)?;
let what = address(cel, *id);
+ let line: Option<Vec<(f32, f32)>> = marks[at]
+ .as_stroke()
+ .filter(|_| props.contains_key("path"))
+ .map(|s| s.samples.iter().map(Sample::pos).collect());
match &mut marks[at] {
Mark::Stroke(s) => set_stroke(s, props),
Mark::Fill(f) => set_fill(f, props),
}
.map_err(|e| anyhow!("set {what}: {e}"))?;
+ if let Some(line) = line {
+ attach::bend(marks, *id, &line, ids);
+ }
}
MarkChange::Remove { id } => {
let at = position(marks, cel, *id)?;