mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 19:36:16 +00:00
Guides: a transform's reply lists the guide paths it makes of ruler guides
A selected ruler guide that a transform turns to an angle becomes a
selected guide path, but the reply's ids left it out: object.rotate
with copy on a lone guide returned {"ids": []}. transform_ruler_guides
now returns the ids of the paths it adds, and the transform commands
add them after the transformed objects, on the direct-selected anchor
route too.
This commit is contained in:
@@ -406,8 +406,10 @@ impl Document {
|
||||
/// gives it. A line that stays vertical or horizontal stays a ruler guide, at its new place. A
|
||||
/// line at any other angle becomes a guide path with that line, on the guide's layer, named
|
||||
/// as the guide was; `sel` then holds the path as a selected object instead of the guide.
|
||||
/// Guides that cannot be edited ([`Self::guide_editable`]) stay as they are.
|
||||
pub fn transform_ruler_guides(&mut self, sel: &mut Selection, xf: Affine) {
|
||||
/// Guides that cannot be edited ([`Self::guide_editable`]) stay as they are. Returns the ids
|
||||
/// of the guide paths it adds, in the order it adds them.
|
||||
pub fn transform_ruler_guides(&mut self, sel: &mut Selection, xf: Affine) -> Vec<NodeId> {
|
||||
let mut paths = vec![];
|
||||
for id in sel.guides.clone() {
|
||||
let Some(g) = self.ruler_guide(id).filter(|g| self.guide_editable(g)) else { continue };
|
||||
let (a, b) = match self.transformed_guide(&g, xf) {
|
||||
@@ -431,7 +433,9 @@ impl Document {
|
||||
}
|
||||
self.retain_guides(sel, |g| g.id != Some(id));
|
||||
sel.objects.push(path);
|
||||
paths.push(path);
|
||||
}
|
||||
paths
|
||||
}
|
||||
|
||||
/// The ruler guide node `g` names, if this document has it.
|
||||
|
||||
@@ -172,8 +172,9 @@ fn apply_transform_moving_guides(
|
||||
if c[..4] == [1.0, 0.0, 0.0, 1.0] {
|
||||
interpolate = Some(crate::cmd::path::interpolates_handles(s, p, label)?);
|
||||
}
|
||||
let mut ids = ids;
|
||||
if xf != Affine::IDENTITY {
|
||||
crate::cmd::path::transform_anchors(s, label, xf, sc, interpolate.unwrap_or(false))?;
|
||||
ids.extend(crate::cmd::path::transform_anchors(s, label, xf, sc, interpolate.unwrap_or(false))?);
|
||||
}
|
||||
ids
|
||||
} else if none || (!copy && xf == Affine::IDENTITY) {
|
||||
@@ -181,7 +182,7 @@ fn apply_transform_moving_guides(
|
||||
} else {
|
||||
let kept = if copy { preview_copies(s, &ids, with_guides, below)? } else { None };
|
||||
s.edit(label, |d, sel| {
|
||||
let targets = match &kept {
|
||||
let mut targets = match &kept {
|
||||
// The document becomes the snapshot with the kept copies, untransformed, and the
|
||||
// loop below transforms only the copies.
|
||||
Some(k) => {
|
||||
@@ -231,7 +232,9 @@ fn apply_transform_moving_guides(
|
||||
d.edit_ruler_guide(i, |g| *g = g.moved(delta));
|
||||
}
|
||||
} else if with_guides && !is_translation {
|
||||
d.transform_ruler_guides(sel, xf);
|
||||
// The guide paths are made already transformed, so they join the reply after the
|
||||
// loop above.
|
||||
targets.extend(d.transform_ruler_guides(sel, xf));
|
||||
}
|
||||
Ok(targets)
|
||||
})?
|
||||
|
||||
@@ -330,18 +330,15 @@ pub(crate) fn interpolates_handles(s: &mut Session, p: &Value, cmd: &str) -> Res
|
||||
|
||||
/// [`transform_selected`] on the active document's selection, as one undo step `label`. A
|
||||
/// transform other than a move also transforms the selected ruler guides
|
||||
/// ([`Document::transform_ruler_guides`]).
|
||||
pub(crate) fn transform_anchors(s: &mut Session, label: &str, xf: vectorcraft_geom::Affine, sc: Scaling, interpolate: bool) -> Result<()> {
|
||||
/// ([`Document::transform_ruler_guides`]). Returns the ids of the guide paths that adds.
|
||||
pub(crate) fn transform_anchors(s: &mut Session, label: &str, xf: vectorcraft_geom::Affine, sc: Scaling, interpolate: bool) -> Result<Vec<NodeId>> {
|
||||
let sel = s.doc()?.selection.clone();
|
||||
// As [`transform_selected`] tells a move apart, which moves the guides itself.
|
||||
let c = xf.as_coeffs();
|
||||
let is_translation = (c[0] - 1.0).abs() < 1e-9 && c[1].abs() < 1e-9 && c[2].abs() < 1e-9 && (c[3] - 1.0).abs() < 1e-9;
|
||||
s.edit(label, |doc, live| {
|
||||
transform_selected(doc, &sel, xf, sc, interpolate)?;
|
||||
if !is_translation {
|
||||
doc.transform_ruler_guides(live, xf);
|
||||
}
|
||||
Ok(())
|
||||
Ok(if is_translation { vec![] } else { doc.transform_ruler_guides(live, xf) })
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -4644,6 +4644,47 @@ fn a_ruler_guide_rotates_with_the_selected_art() {
|
||||
assert!(!guides(&s)[0].0, "the vertical guide is horizontal now: {:?}", guides(&s));
|
||||
}
|
||||
|
||||
/// The reply to a transform lists the guide paths it makes of ruler guides: for a guide alone,
|
||||
/// with a copy, beside selected art and beside direct-selected anchors.
|
||||
#[test]
|
||||
fn a_transform_replies_with_the_guide_paths_it_makes() {
|
||||
fn reply_ids(r: &Value) -> Vec<NodeId> {
|
||||
r["ids"].as_array().unwrap().iter().map(|v| NodeId(v.as_u64().unwrap())).collect()
|
||||
}
|
||||
let mut s = session();
|
||||
s.execute("guide.add", &json!({"vertical": true, "pos": 100})).unwrap();
|
||||
s.execute("guide.select", &json!({"indexes": [0]})).unwrap();
|
||||
// A copy keeps the ruler guide and adds a guide path, the only new object.
|
||||
let r = s.execute("object.rotate", &json!({"angle": 45, "copy": true})).unwrap();
|
||||
let [copy] = selected(&s)[..] else { panic!("the copy is selected") };
|
||||
assert_eq!(reply_ids(&r), vec![copy]);
|
||||
// Without a copy the guide becomes a guide path.
|
||||
s.execute("guide.select", &json!({"indexes": [0]})).unwrap();
|
||||
let r = s.execute("object.rotate", &json!({"angle": 30})).unwrap();
|
||||
let [path] = selected(&s)[..] else { panic!("one guide path selected") };
|
||||
assert_ne!(path, copy);
|
||||
assert_eq!(reply_ids(&r), vec![path]);
|
||||
// With art selected, the reply lists the art and then the guide path.
|
||||
let a = rect(&mut s, 300.0, 100.0, 100.0, 100.0);
|
||||
s.execute("guide.add", &json!({"vertical": false, "pos": 50})).unwrap();
|
||||
sel(&mut s, &[a]);
|
||||
s.execute("guide.select", &json!({"indexes": [0], "toggle": true})).unwrap();
|
||||
let r = s.execute("object.rotate", &json!({"angle": 20})).unwrap();
|
||||
let paths: Vec<NodeId> = selected(&s).into_iter().filter(|id| *id != a).collect();
|
||||
let [mixed] = paths[..] else { panic!("the guide path is selected with the art") };
|
||||
assert_eq!(reply_ids(&r), vec![a, mixed]);
|
||||
// With direct-selected anchors, the guide path joins the reply too.
|
||||
s.execute("guide.add", &json!({"vertical": true, "pos": 600})).unwrap();
|
||||
s.execute("select.anchors", &json!({"id": a.0, "anchors": [[0, 1]], "mode": "set"})).unwrap();
|
||||
s.execute("guide.select", &json!({"indexes": [0], "toggle": true})).unwrap();
|
||||
assert!(!s.doc().unwrap().selection.anchors.is_empty());
|
||||
let r = s.execute("object.rotate", &json!({"angle": 30})).unwrap();
|
||||
let anchored: Vec<NodeId> = selected(&s).into_iter().filter(|id| *id != a).collect();
|
||||
let [made] = anchored[..] else { panic!("the guide path is selected with the path") };
|
||||
assert!(line_ends(&s, made).1, "it is a guide path");
|
||||
assert!(reply_ids(&r).contains(&made), "{r}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ruler_guides_and_lock() {
|
||||
let mut s = session();
|
||||
|
||||
+2
-1
@@ -4134,7 +4134,8 @@ Copy) and `object.transformAgain`, also when no object is selected, in one undo
|
||||
guide transforms as the line `view.guides.release` makes of it, and the default origin is the center of the selected
|
||||
objects' box and those lines together. A guide that ends up vertical or horizontal stays a ruler guide at its new
|
||||
place: a turn of 180° keeps it, and a turn of 90° makes a horizontal guide vertical. A guide at any other angle
|
||||
becomes a guide path along its line, on the guide's layer, and is selected as one. Locked or hidden guides do not
|
||||
becomes a guide path along its line, on the guide's layer, and is selected as one; the reply's `ids` list it after
|
||||
the transformed objects. Locked or hidden guides do not
|
||||
transform, nor do guides when `ids` is given or `objects` is false. `object.rotate` with `absolute` and only ruler
|
||||
guides selected is an error, as guides have no bounding box angle.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user