mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 14:06:09 +00:00
object.align takes to: "auto", which aligns to the key object, key anchor or key guide when the selection has one, else to the artboard when exactly one object is selected and no anchor points are, else to the selection. New Action and Action Options have Choose Align To at playback (automaticAlignTo on ui.action.new and ui.action.options), off by default. With it on, playback sends to: "auto" for each object.align step, without any artboard index the step stores, so the step uses the active artboard; the stored steps keep their recorded params. Save Actions writes the option, and a set that uses it is saved as file version 7. The dialog's strings have rows in every catalog, and docs/mcp.md describes to: "auto", automaticAlignTo and file version 7. Fixes #6972. Supersedes #7635. Co-authored-by: yoriswind <yoriswind@yahoo.com>
70 lines
3.1 KiB
Rust
70 lines
3.1 KiB
Rust
//! Engine-only controls for explicit automatic Align references.
|
|
|
|
use serde_json::{Value, json};
|
|
use vectorcraft_doc::NodeId;
|
|
use vectorcraft_engine::Session;
|
|
use vectorcraft_geom::Point;
|
|
|
|
fn session() -> Session {
|
|
let mut s = Session::new();
|
|
s.execute("file.new", &json!({"width": 400, "height": 300, "profile": null})).unwrap();
|
|
s
|
|
}
|
|
|
|
fn align_with(s: &mut Session, mut params: Value, automatic: bool) {
|
|
if automatic {
|
|
params["to"] = json!("auto");
|
|
}
|
|
s.execute("object.align", ¶ms).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn automatic_align_keeps_one_objects_key_guide_reference() {
|
|
for automatic in [false, true] {
|
|
let mut s = session();
|
|
let id = NodeId(s.execute("shape.rectangle", &json!({"x": 20, "y": 40, "width": 40, "height": 20})).unwrap()["id"].as_u64().unwrap());
|
|
s.execute("guide.add", &json!({"vertical": false, "pos": 210})).unwrap();
|
|
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
|
s.execute("guide.select", &json!({"indexes": [0], "toggle": true})).unwrap();
|
|
s.execute("select.key", &json!({"guide": 0})).unwrap();
|
|
let selection = s.doc().unwrap().selection.clone();
|
|
assert_eq!(selection.len(), 1);
|
|
assert!(selection.has_key());
|
|
let guide = selection.key_guide.unwrap();
|
|
|
|
align_with(&mut s, json!({"horizontal": "right", "vertical": "bottom", "bounds": "geometric"}), automatic);
|
|
let bounds = s.doc().unwrap().doc.bounds_of(&[id], false).unwrap();
|
|
assert_eq!(
|
|
(bounds.x0, bounds.y0, bounds.x1, bounds.y1),
|
|
(20.0, 190.0, 60.0, 210.0),
|
|
"the horizontal key guide overrides the one-object artboard rule and leaves x alone"
|
|
);
|
|
assert_eq!(s.doc().unwrap().doc.ruler_guide(guide).unwrap().pos, 210.0);
|
|
assert_eq!(s.doc().unwrap().selection, selection, "reference resolution does not create or clear a key");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn automatic_align_keeps_a_single_paths_selected_anchor_bounds() {
|
|
for automatic in [false, true] {
|
|
let mut s = session();
|
|
let id = NodeId(s.execute("path.create", &json!({"d": "M20 40 L60 100 L100 40", "closed": false})).unwrap()["id"].as_u64().unwrap());
|
|
s.execute("select.anchors", &json!({"id": id.0, "anchors": [[0, 0], [0, 1]], "mode": "set"})).unwrap();
|
|
let selection = s.doc().unwrap().selection.clone();
|
|
assert_eq!(selection.len(), 1);
|
|
assert!(!selection.anchors.is_empty());
|
|
assert!(!selection.has_key());
|
|
|
|
align_with(&mut s, json!({"horizontal": "center", "bounds": "geometric"}), automatic);
|
|
let document = &s.doc().unwrap().doc;
|
|
let path = document.node(id).unwrap().path_data().unwrap();
|
|
let points: Vec<Point> = path.subpaths.first().unwrap().anchors.iter().map(|anchor| anchor.p).collect();
|
|
assert_eq!(
|
|
points,
|
|
[Point::new(40.0, 40.0), Point::new(40.0, 100.0), Point::new(100.0, 40.0)],
|
|
"only picked anchors use their own center; the whole path is not centered on the artboard"
|
|
);
|
|
assert_eq!(s.doc().unwrap().selection, selection);
|
|
}
|
|
}
|