A drawing app built for agent/human collaboration
Rust
In-betweens on bones can be made again and redone (B3)
Indicode BVa686875e63be
diff --git a/crates/cli/src/inbetween.rs b/crates/cli/src/inbetween.rsindex 2f8b7dc..82fdb62 100644--- a/crates/cli/src/inbetween.rs+++ b/crates/cli/src/inbetween.rs@@ -433,18 +433,10 @@ fn make(bundle: &Bundle, spec: &InbetweenSpec) -> Result<(Inbetweens, Vec<Stringif !jobs.is_empty() {compute(bundle, &staged, &keys, &jobs, &mut made.cels, &mut notes)?;}- rigged.iter().for_each(|(i, rig, t)| {- let (marks, pose) = rig.at(*t, (doc.canvas.width, doc.canvas.height));- made.cels[*i].marks = marks;- made.cels[*i].pose = Some(pose);- });- let mut bent: Vec<&str> = rigged.iter().map(|(i, _, _)| made.cels[*i].layer.as_str()).collect();- bent.dedup();- bent.iter().for_each(|l| {- notes.push(format!(- "{l}: in-betweened on bones (both keys are poses of one rest drawing)"- ))- });+ rigged+ .iter()+ .for_each(|(i, rig, t)| on_bones(rig, *t, (doc.canvas.width, doc.canvas.height), &mut made.cels[*i]));+ bones_note(rigged.iter().map(|(i, _, _)| made.cels[*i].layer.as_str()), &mut notes);keep_parts(doc, &mut made.cels);made.cels.iter().for_each(|c| {notes.push(format!(@@ -458,6 +450,29 @@ fn make(bundle: &Bundle, spec: &InbetweenSpec) -> Result<(Inbetweens, Vec<StringOk((made, notes))}+fn finish(cel: &mut TweenCel, marks: Vec<Mark>) {+ cel.tween.made = made_hash(&marks);+ cel.marks = marks;+}++fn on_bones(rig: &crate::rig::Posed, t: f32, canvas: (u32, u32), cel: &mut TweenCel) {+ let (marks, pose) = rig.at(t, canvas);+ finish(cel, marks);+ cel.pose = Some(pose);+}++fn bones_note<'a>(layers: impl Iterator<Item = &'a str>, notes: &mut Vec<String>) {+ let mut bent: Vec<&str> = layers.collect();+ bent.dedup();+ bent.iter().for_each(|l| {+ notes.push(format!(+ "{l}: in-betweened on bones (both keys are poses of one rest drawing)"+ ))+ });+}++type Rigs = BTreeMap<(String, String, String), Option<crate::rig::Posed>>;+fn redo(bundle: &Bundle, spec: &InbetweenSpec) -> Result<(Inbetweens, Vec<String>)> {let doc = &bundle.doc;let wanted: Option<Vec<String>> = (!spec.layers.is_empty())@@ -469,6 +484,8 @@ fn redo(bundle: &Bundle, spec: &InbetweenSpec) -> Result<(Inbetweens, Vec<Stringlet mut refused = Vec::new();let mut notes = Vec::new();let mut kept = Vec::new();+ let mut rigs: Rigs = BTreeMap::new();+ let mut rigged: Vec<(usize, (String, String, String), f32)> = Vec::new();doc.all_layers().into_iter().filter(|l| l.kind() == Kind::Paint)@@ -516,6 +533,26 @@ fn redo(bundle: &Bundle, spec: &InbetweenSpec) -> Result<(Inbetweens, Vec<String});return Ok(());}+ let pair = (layer.id.clone(), tween.from.clone(), tween.to.clone());+ if !rigs.contains_key(&pair) {+ let rig = crate::rig::posed_pair(bundle, &layer.id, &tween.from, &tween.to)?;+ rigs.insert(pair.clone(), rig);+ }+ if rigs[&pair].is_some() {+ rigged.push((made.cels.len(), pair, tween.t));+ made.cels.push(TweenCel {+ layer: layer.id.clone(),+ cel: cel.id.clone(),+ frame: cel.frame,+ hold: cel.hold,+ tween: tween.clone(),+ marks: Vec::new(),+ fresh: false,+ pose: None,+ parts: BTreeMap::new(),+ });+ return Ok(());+ }let pairs = tween.pairs.clone();let matching = Match::parse(&tween.matching)?;let unpaired = Unpaired::parse(&tween.unpaired)?;@@ -564,7 +601,16 @@ fn redo(bundle: &Bundle, spec: &InbetweenSpec) -> Result<(Inbetweens, Vec<Stringbail!("no in-betweens to redo here (klad inbetween marks the cels it makes; klad frames shows them)");}keys.iter().for_each(|k| pairing_notes(k, &mut notes));- compute(bundle, doc, &keys, &jobs, &mut made.cels, &mut notes)?;+ if !jobs.is_empty() {+ compute(bundle, doc, &keys, &jobs, &mut made.cels, &mut notes)?;+ }+ let canvas = (doc.canvas.width, doc.canvas.height);+ rigged.iter().for_each(|(i, pair, t)| {+ if let Some(rig) = &rigs[pair] {+ on_bones(rig, *t, canvas, &mut made.cels[*i]);+ }+ });+ bones_note(rigged.iter().map(|(i, _, _)| made.cels[*i].layer.as_str()), &mut notes);keep_parts(doc, &mut made.cels);kept_on_top(&mut made.cels, kept, &mut notes);Ok((made, notes))@@ -640,8 +686,7 @@ fn compute(.collect::<Result<Vec<_>>>()?;let mut failed = Vec::new();done.into_iter().flatten().for_each(|d| {- cels[d.index].tween.made = made_hash(&d.marks);- cels[d.index].marks = d.marks;+ finish(&mut cels[d.index], d.marks);failed.extend(d.failed);});if !failed.is_empty() {diff --git a/crates/cli/src/rig.rs b/crates/cli/src/rig.rsindex d3d8297..754e551 100644--- a/crates/cli/src/rig.rs+++ b/crates/cli/src/rig.rs@@ -306,10 +306,11 @@ pub struct Posed {impl Posed {pub fn at(&self, t: f32, canvas: (u32, u32)) -> (Vec<Mark>, Pose) {let pose = between(&self.bones, &self.ik, &self.a, &self.b, t);- (- Rig::new(&self.bones, &self.ik, &self.weights, &pose).pose(&self.rest, canvas),- pose,- )+ let mut marks = Rig::new(&self.bones, &self.ik, &self.weights, &pose).pose(&self.rest, canvas);+ marks+ .iter_mut()+ .for_each(|m| m.set_author(klad_core::stroke::Author::Agent));+ (marks, pose)}}@@ -506,6 +507,92 @@ mod tests {std::fs::remove_dir_all(&dir).unwrap();}+ #[test]+ fn in_betweens_on_bones_are_made_again_and_redone_on_the_bones() {+ use klad_core::stroke::{Author, Stroke};+ use klad_core::tween::made_hash;+ let dir = arm("again");+ edit(+ &dir,+ r#"{"op":"bone","layer":"l1","chain":["upper","fore"],"through":[[100,200],[300,200],[480,200]]}+{"op":"weights","layer":"l1","auto":true}+{"op":"pose","layer":"l1","from":0,"to":4,"bones":{"fore":-60}}"#,+ )+ .unwrap();+ let make = r#"{"op":"inbetween","layers":["l1"],"from":0,"to":4}"#;+ edit(&dir, make).unwrap();+ let tweens = |b: &Bundle| -> Vec<(String, u32)> {+ b.doc+ .layer("l1")+ .unwrap()+ .cels()+ .iter()+ .filter(|c| c.tween.is_some())+ .map(|c| (c.id.clone(), c.frame))+ .collect()+ };+ let bundle = Bundle::open(&dir).unwrap();+ assert_eq!(tweens(&bundle).iter().map(|t| t.1).collect::<Vec<_>>(), [1, 2, 3]);+ tweens(&bundle).iter().for_each(|(id, _)| {+ let cel = bundle+ .doc+ .layer("l1")+ .unwrap()+ .cels()+ .iter()+ .find(|c| &c.id == id)+ .unwrap()+ .clone();+ let marks = bundle.read_marks("l1", id).unwrap();+ assert_eq!(cel.tween.unwrap().made, made_hash(&marks), "{id} is hashed as made");+ assert!(+ marks.iter().all(|m| m.author() == Author::Agent),+ "{id}'s marks are the agent's"+ );+ });+ edit(&dir, make).expect("made again over the same keys, nobody drew on them");+ edit(+ &dir,+ r#"{"op":"pose","layer":"l1","from":4,"to":4,"bones":{"fore":-90}}"#,+ )+ .unwrap();+ edit(&dir, r#"{"op":"inbetween","redo":true}"#).expect("redone after the key changed");+ let bundle = Bundle::open(&dir).unwrap();+ let mid = bundle.doc.layer("l1").unwrap().content_at(2).unwrap().clone();+ let fore = mid.pose.as_ref().expect("a redo keeps the pose").angles["fore"];+ assert!(+ (fore + 45.0).abs() < 1e-3,+ "redone on the bones from the new key: {fore}"+ );+ let end = end_of(&bundle, 2);+ let want = (+ 300.0 + 180.0 * (-45f32).to_radians().cos(),+ 200.0 + 180.0 * (-45f32).to_radians().sin(),+ );+ assert!((end.0 - want.0).hypot(end.1 - want.1) < 3.0, "{end:?} {want:?}");+ let mut marks = bundle.read_marks("l1", &mid.id).unwrap();+ marks.push(Mark::Stroke(Stroke {+ author: Author::Human,+ ..Stroke::new(99, "ink", [0.0, 0.0, 0.0, 1.0], 3.0, 1)+ .with_points(&[(420.0, 60.0, 1.0), (440.0, 80.0, 1.0)], 8.0)+ }));+ let mut bundle = bundle;+ bundle.replace_marks("l1", &mid.id, &marks, "editor", "pen").unwrap();+ assert!(edit(&dir, make).is_err(), "an in-between drawn on is not made over");+ let refused = edit(&dir, r#"{"op":"inbetween","redo":true}"#).unwrap_err();+ assert!(format!("{refused:#}").contains("1 mark drawn by hand"), "{refused:#}");+ edit(&dir, r#"{"op":"inbetween","redo":true,"keepDrawn":true}"#).unwrap();+ let bundle = Bundle::open(&dir).unwrap();+ let kept = bundle.read_marks("l1", &mid.id).unwrap();+ assert_eq!(+ kept.iter().filter(|m| m.author() == Author::Human).count(),+ 1,+ "the hand stroke is kept, once"+ );+ assert_eq!(kept.len(), marks.len(), "nothing doubled");+ std::fs::remove_dir_all(&dir).unwrap();+ }+#[test]fn bones_weights_and_poses_through_the_cli_and_undo() {let dir = arm("pose");diff --git a/crates/core/src/stroke.rs b/crates/core/src/stroke.rsindex 2923f77..e27c15c 100644--- a/crates/core/src/stroke.rs+++ b/crates/core/src/stroke.rs@@ -343,6 +343,13 @@ impl Mark {}}+ pub fn set_author(&mut self, author: Author) {+ match self {+ Mark::Stroke(s) => s.author = author,+ Mark::Fill(f) => f.author = author,+ }+ }+pub fn as_stroke(&self) -> Option<&Stroke> {match self {Mark::Stroke(s) => Some(s),