mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-01 18:23:52 -06:00
shape.connect takes the direction between the shapes' box centres, not the means of their sites
The mean of a shape's connection sites is off its centre when the sites are not symmetric (a triangle's lie mostly along its base), which pulled the dominant direction sideways and glued an arrow to a box's side when the box was mostly below. The centres come from xfrm_of, so placeholders and tables are measured as drawn. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
parent
ffcb3fe68a
commit
6cf5280038
@@ -27,11 +27,10 @@ pub(super) fn connect(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// A site left unsaid is the one facing the other shape along the dominant direction between
|
||||
// the two (a box mostly below is left from the bottom), which is where a person glues an
|
||||
// arrow; "the closest pair" chose a side site for a diagonal neighbour and crossed boxes.
|
||||
let centre = |v: &[deckcraft_geom::Point]| {
|
||||
let n = v.len().max(1) as f64;
|
||||
deckcraft_geom::Point::new(v.iter().map(|q| q.x).sum::<f64>() / n, v.iter().map(|q| q.y).sum::<f64>() / n)
|
||||
};
|
||||
let (ca, cb) = (centre(&a), centre(&b));
|
||||
// The direction runs between the shapes' box centres: the mean of the sites is off-centre
|
||||
// for a shape whose sites are not symmetric (a triangle's lie mostly along its base).
|
||||
let centre = |id| st.shape(id).map(|sh| xfrm_of(&st.doc, &st.selection, sh).center()).unwrap_or_default();
|
||||
let (ca, cb) = (centre(from), centre(to));
|
||||
let facing = |v: &[deckcraft_geom::Point], c: deckcraft_geom::Point, dx: f64, dy: f64| {
|
||||
let len = dx.hypot(dy).max(1e-9);
|
||||
v.iter()
|
||||
|
||||
@@ -6762,6 +6762,13 @@ fn connector_default_sites_follow_the_dominant_direction() {
|
||||
assert_eq!((r["fromSite"].as_u64(), r["toSite"].as_u64()), (Some(2), Some(0)), "mostly below: bottom → top, not right → left: {r}");
|
||||
let r = s.execute("shape.connect", &json!({"from": diag, "to": a})).unwrap();
|
||||
assert_eq!((r["fromSite"].as_u64(), r["toSite"].as_u64()), (Some(0), Some(2)), "and back: top → bottom: {r}");
|
||||
// The direction runs between the shapes' centres, not between the means of their sites: a
|
||||
// triangle's sites lie mostly along its base, which pulled the direction sideways and chose
|
||||
// the box's left site for a box that is mostly below.
|
||||
let tri = s.execute("shape.insert", &json!({"preset": "triangle", "rect": [400, 0, 100, 100]})).unwrap()["id"].as_u64().unwrap();
|
||||
let below = s.execute("shape.insert", &json!({"preset": "rect", "rect": [520, 155, 100, 50]})).unwrap()["id"].as_u64().unwrap();
|
||||
let r = s.execute("shape.connect", &json!({"from": tri, "to": below})).unwrap();
|
||||
assert_eq!(r["toSite"].as_u64(), Some(0), "centre to centre is mostly down: the box's top, not its left: {r}");
|
||||
}
|
||||
|
||||
/// A size (or colour, bold…) set on a shape before it has any text survives the text that is
|
||||
|
||||
Reference in New Issue
Block a user