mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 02:07:10 +01:00
Merge pull request #8853 from dotCooCoo/feat/swatches-pattern-drag-to-canvas
Swatches: a pattern swatch dropped on empty canvas places a copy of its tile art
This commit is contained in:
38 files changed
+390
-28
No files matched your search
@@ -38,7 +38,7 @@ mod dynamic;
|
||||
mod symbolism;
|
||||
mod symbols;
|
||||
|
||||
pub(crate) use brushes::{apply_brush, brush_targets, stroke_paint};
|
||||
pub(crate) use brushes::{apply_brush, brush_targets, frame_marker, stroke_paint};
|
||||
use brushes::{
|
||||
brush_apply, brush_delete, brush_duplicate, brush_freehand, brush_get, brush_list, brush_move, brush_new, brush_object_options, brush_options,
|
||||
brush_place_art, brush_remove, brush_replace_art, brush_selection_art, brush_set_current, brush_unused, expand_brush,
|
||||
|
||||
@@ -766,10 +766,12 @@ pub(super) fn brush_place_art(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(json!({ "id": id.0 }))
|
||||
}
|
||||
|
||||
/// The repeat frame an unpainted rectangle at the back of a tile group marks ([`placed_art`]): the
|
||||
/// The repeat frame an unpainted rectangle at the back of a tile group marks ([`placed_art`]), and
|
||||
/// the tile such a rectangle at the back of a pattern's art marks (`object.pattern.replaceArt`): the
|
||||
/// rectangle's bounds, when `n` is a path of one closed subpath through the four corners of its
|
||||
/// bounds, without curves, whose fills and strokes paint nothing and that has no other appearance.
|
||||
fn frame_marker(n: &Node) -> Option<vectorcraft_geom::Rect> {
|
||||
/// bounds in order around them (either way), without curves, whose fills and strokes paint nothing
|
||||
/// and that has no other appearance.
|
||||
pub(crate) fn frame_marker(n: &Node) -> Option<vectorcraft_geom::Rect> {
|
||||
let NodeKind::Path { path, guide: false, clipping: false, .. } = &n.kind else { return None };
|
||||
let unpainted = n.appearance.effects.is_empty()
|
||||
&& n.appearance.items.iter().all(|i| match i {
|
||||
@@ -781,7 +783,9 @@ fn frame_marker(n: &Node) -> Option<vectorcraft_geom::Rect> {
|
||||
let corners = [(r.x0, r.y0), (r.x1, r.y0), (r.x1, r.y1), (r.x0, r.y1)];
|
||||
let at_corners = sp.anchors.len() == 4
|
||||
&& sp.anchors.iter().all(|a| a.h_in == a.p && a.h_out == a.p)
|
||||
&& corners.iter().all(|c| sp.anchors.iter().any(|a| (a.p.x, a.p.y) == *c));
|
||||
&& corners.iter().all(|c| sp.anchors.iter().any(|a| (a.p.x, a.p.y) == *c))
|
||||
// Each side, the closing one included, joins two corners that share an edge.
|
||||
&& sp.anchors.iter().zip(sp.anchors.iter().cycle().skip(1)).all(|(a, b)| a.p.x == b.p.x || a.p.y == b.p.y);
|
||||
(unpainted && sp.closed && at_corners && r.width() > 1e-9 && r.height() > 1e-9).then_some(r)
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ use vectorcraft_doc::swatches::{PaintFn, graph_paints, node_paints, override_pai
|
||||
use vectorcraft_doc::{Document, Node, NodeId, NodeKind, Selection};
|
||||
use vectorcraft_geom::{Point, Rect};
|
||||
|
||||
use super::brushsym::frame_marker;
|
||||
use super::edit::{outermost, selected_roots};
|
||||
use super::pathops::Beside;
|
||||
use super::{
|
||||
@@ -43,10 +44,19 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Replace Pattern Art",
|
||||
[],
|
||||
None,
|
||||
"{name, ids?} make copies of the selected art the tile art of pattern `name`, as Alt-dragging (Option-dragging on macOS) art onto its swatch in the Swatches panel does; the pattern keeps its name, swatch, tile and options (with Size Tile to Art on, the tile fits the new art), every fill and stroke painted with it draws the new tile, and the art stays on the canvas, selected; one undo step, none when the art matches the tile art already; refused while a pattern is edited, with nothing selected, for an id that names no object, and for art painted with the pattern itself → {name}",
|
||||
"{name, ids?} make copies of the selected art the tile art of pattern `name`, as Alt-dragging (Option-dragging on macOS) art onto its swatch in the Swatches panel does; the pattern keeps its name, swatch and options; an unpainted rectangle at the back of the art (or at the back of one selected group, as pattern.placeArt places it) becomes the tile and is left out of the tile art, otherwise the tile keeps its size and moves as far as the art's bounds moved from the old tile art's (with Size Tile to Art on, the tile fits the new art); every fill and stroke painted with it draws the new tile, and the art stays on the canvas, selected; one undo step, none when the art and tile match the pattern's already; refused while a pattern is edited, with nothing selected, for an id that names no object, and for art painted with the pattern itself → {name}",
|
||||
can_replace_pattern_art,
|
||||
pattern_replace_art
|
||||
),
|
||||
cmd!(
|
||||
"pattern.placeArt",
|
||||
"Place Pattern Art",
|
||||
[],
|
||||
None,
|
||||
"{name, x?, y?} place a copy of pattern `name`'s tile art, as dragging its swatch from the Swatches panel onto an empty part of the canvas does: one group with an unpainted rectangle of the tile at its back, centered on (x, y) (default: the center of the first artboard) in the layer new art goes into, and selected; object.pattern.replaceArt reads the rectangle back as the tile; the pattern does not change; one undo step; refused while a pattern is edited and for a pattern with no art → {id}",
|
||||
can_replace_pattern_art,
|
||||
pattern_place_art
|
||||
),
|
||||
cmd!(
|
||||
"object.pattern.edit",
|
||||
"Edit Pattern",
|
||||
@@ -434,41 +444,102 @@ pub(crate) fn finish_edit(d: &mut Document, sel: &mut Selection) -> Result<Strin
|
||||
}
|
||||
|
||||
/// Give pattern `name` fresh-id copies of the selected art (or of `ids`) as its tile art, as one
|
||||
/// undo step (`object.pattern.replaceArt`). The pattern keeps its name, swatch, tile and options,
|
||||
/// and its tile fits the new art when its Size Tile to Art is on; paints refer to a pattern by
|
||||
/// name, so every fill and stroke of it draws the new tile. The art stays where it is, selected as
|
||||
/// before. Art that matches the tile art already records nothing.
|
||||
/// undo step (`object.pattern.replaceArt`). The pattern keeps its name, swatch and options. An
|
||||
/// unpainted rectangle at the back of the art becomes the tile ([`tile_and_art`]); without one the
|
||||
/// tile keeps its size and moves as far as the art's bounds moved from the old tile art's, so art
|
||||
/// drawn anywhere stays in its tile. With Size Tile to Art on, the tile fits the new art. Paints
|
||||
/// refer to a pattern by name, so every fill and stroke of it draws the new tile. The art stays
|
||||
/// where it is, selected as before. Art and a tile that match the pattern's already record nothing.
|
||||
fn pattern_replace_art(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "object.pattern.replaceArt";
|
||||
let name = text_param(p, "name", C)?.ok_or_else(|| bad(C, "missing name"))?.to_string();
|
||||
let roots = roots(s, p, C)?;
|
||||
let d = &s.doc()?.doc;
|
||||
let old = d.pattern(&name).map(|def| def.art.clone()).ok_or_else(|| bad(C, format!("no pattern `{name}`")))?;
|
||||
if roots.is_empty() {
|
||||
let def = d.pattern(&name).ok_or_else(|| bad(C, format!("no pattern `{name}`")))?;
|
||||
let (src, marked) = tile_and_art(roots.iter().filter_map(|id| d.node(*id).cloned()).map(Arc::new).collect());
|
||||
if src.is_empty() {
|
||||
return Err(bad(C, "select the art for the pattern tile"));
|
||||
}
|
||||
let src: Vec<Arc<Node>> = roots.iter().filter_map(|id| d.node(*id).cloned()).map(Arc::new).collect();
|
||||
if paints_pattern(d, &src, &name) {
|
||||
return Err(bad(C, format!("art painted with `{name}` can't be its tile")));
|
||||
}
|
||||
let mut next = def.clone();
|
||||
next.tile = match (marked, vectorcraft_doc::live::nodes_bounds(&def.art), vectorcraft_doc::live::nodes_bounds(&src)) {
|
||||
(Some(tile), ..) => tile,
|
||||
(None, Some(was), Some(now)) => def.tile + (now.origin() - was.origin()),
|
||||
(None, ..) => def.tile,
|
||||
};
|
||||
next.art = src.clone();
|
||||
if next.size_tile_to_art {
|
||||
next.fit_tile_to_art();
|
||||
}
|
||||
let tile = next.tile;
|
||||
// Copies made from the same next id are equal when the art matches the tile art.
|
||||
let fresh = |art: &[Arc<Node>]| copies(&mut Document::clone(d), art);
|
||||
if fresh(&old) == fresh(&src) {
|
||||
if tile == def.tile && fresh(&def.art) == fresh(&src) {
|
||||
return Ok(json!({ "name": name }));
|
||||
}
|
||||
s.edit("Replace Pattern Art", |d, _| {
|
||||
let art: Vec<Arc<Node>> = copies(d, &src).into_iter().map(Arc::new).collect();
|
||||
if let Some(def) = d.pattern_mut(&name) {
|
||||
def.art = art;
|
||||
if def.size_tile_to_art {
|
||||
def.fit_tile_to_art();
|
||||
}
|
||||
def.tile = tile;
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(json!({ "name": name }))
|
||||
}
|
||||
|
||||
/// The tile art and the tile that art `src` (in paint order) gives a pattern: an unpainted
|
||||
/// rectangle at the back of the art ([`frame_marker`]), or at the back of a lone plain group as
|
||||
/// `pattern.placeArt` places it, is the tile and is left out of the art, and that group's members
|
||||
/// are the art. Without such a rectangle the art is `src` and there is no tile.
|
||||
fn tile_and_art(src: Vec<Arc<Node>>) -> (Vec<Arc<Node>>, Option<Rect>) {
|
||||
let mut art = src;
|
||||
if let [g] = art.as_slice()
|
||||
&& let NodeKind::Group { children, clip: false } = &g.kind
|
||||
&& children.first().is_some_and(|c| frame_marker(c).is_some())
|
||||
&& **g == Node::group(g.id, children.clone())
|
||||
{
|
||||
art = children.clone();
|
||||
}
|
||||
match art.first().and_then(|c| frame_marker(c)) {
|
||||
Some(tile) => (art.split_off(1), Some(tile)),
|
||||
None => (art, None),
|
||||
}
|
||||
}
|
||||
|
||||
/// `pattern.placeArt`: a copy of pattern `name`'s tile art in one group, with an unpainted
|
||||
/// rectangle of its tile at the back, centered on (`x`, `y`) (default: the center of the first
|
||||
/// artboard) in the layer new art goes into, and selected, as one undo step. The pattern does not
|
||||
/// change; `object.pattern.replaceArt` reads the rectangle back as the tile ([`tile_and_art`]).
|
||||
fn pattern_place_art(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "pattern.placeArt";
|
||||
let name = text_param(p, "name", C)?.ok_or_else(|| bad(C, "missing name"))?.to_string();
|
||||
let d = &s.doc()?.doc;
|
||||
let center = d.artboards.first().map(|a| a.rect.center()).unwrap_or(Point::ZERO);
|
||||
let at = Point::new(super::number_or(p, "x", center.x, C)?, super::number_or(p, "y", center.y, C)?);
|
||||
let def = d.pattern(&name).ok_or_else(|| bad(C, format!("no pattern `{name}`")))?;
|
||||
if def.art.is_empty() {
|
||||
return Err(bad(C, "no artwork"));
|
||||
}
|
||||
let unpainted = vectorcraft_doc::Appearance::basic(Paint::None, Paint::None, 1.0);
|
||||
let marker = Node::path(NodeId(0), vectorcraft_geom::shapes::rectangle(def.tile), unpainted);
|
||||
let mut art = Node::group(NodeId(0), std::iter::once(Arc::new(marker)).chain(def.art.iter().cloned()).collect());
|
||||
if let Some(r) = art.geometric_bounds_with(&d.symbol_boxes()) {
|
||||
art.transform(vectorcraft_geom::Affine::translate(at - r.center()), false);
|
||||
}
|
||||
let parent = s.doc()?.target_parent()?;
|
||||
let id = s.edit("Place Pattern Art", |d, sel| {
|
||||
let art = d.reid(&art);
|
||||
let id = art.id;
|
||||
d.insert(parent, usize::MAX, art)?;
|
||||
sel.set([id]);
|
||||
Ok(id)
|
||||
})?;
|
||||
Ok(json!({ "id": id.0 }))
|
||||
}
|
||||
|
||||
/// What a piece of art uses by name, for [`paints_pattern`].
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
enum Uses<'a> {
|
||||
|
||||
@@ -489,6 +489,7 @@ pub(super) const ALLOWED: &[&str] = &[
|
||||
"pattern.delete",
|
||||
"pattern.list",
|
||||
"pattern.options",
|
||||
"pattern.placeArt",
|
||||
"pattern.transform",
|
||||
"perspective.attach",
|
||||
"perspective.draw",
|
||||
|
||||
@@ -222,8 +222,9 @@ fn blue_rect(s: &mut Session) -> NodeId {
|
||||
}
|
||||
|
||||
/// Replace Pattern Art gives the pattern copies of the selected art as its tile art in one undo
|
||||
/// step: the pattern keeps its name, swatch, place, tile and options, art painted with it keeps the
|
||||
/// paint, and the selected art stays on the canvas, selected. Undo and Redo swap the two tiles.
|
||||
/// step: the pattern keeps its name, swatch, place and options, the tile keeps its size and moves as
|
||||
/// far as the art's bounds moved, art painted with it keeps the paint, and the selected art stays on
|
||||
/// the canvas, selected. Undo and Redo swap the two tiles.
|
||||
#[test]
|
||||
fn replace_art_gives_a_pattern_new_tile_art() {
|
||||
let mut s = session();
|
||||
@@ -243,7 +244,9 @@ fn replace_art_gives_a_pattern_new_tile_art() {
|
||||
assert_eq!(def.art[0].kind, node(&s, blue).kind, "the tile art is the blue rectangle");
|
||||
assert_eq!(def.art[0].appearance, node(&s, blue).appearance);
|
||||
assert_ne!(def.art[0].id, blue, "a copy with an id of its own");
|
||||
assert_eq!((def.tile, def.tile_type, &def.name), (old.tile, old.tile_type, &old.name), "the tile and options stay");
|
||||
// The red square's bounds start at (0, 0) and the blue rectangle's at (100, 100).
|
||||
assert_eq!(def.tile, old.tile + vectorcraft_geom::Vec2::new(100.0, 100.0), "the tile moves with the art");
|
||||
assert_eq!((def.tile_type, &def.name), (old.tile_type, &old.name), "the options stay");
|
||||
assert_eq!((d.patterns.len(), place(&s), d.swatches_iter().count()), (1, at, swatches), "no new pattern or swatch");
|
||||
assert!(is_pattern(&node(&s, filled).appearance.fill_paint(), "Dots"), "the filled art keeps the pattern");
|
||||
assert!(d.node(blue).is_some() && d.node(red).is_some(), "the art stays");
|
||||
@@ -282,6 +285,164 @@ fn replace_art_that_matches_the_tile_art_records_nothing() {
|
||||
assert_eq!(doc(&s), &before);
|
||||
}
|
||||
|
||||
/// The geometric bounds of object `id`.
|
||||
fn bounds_of(s: &Session, id: NodeId) -> Rect {
|
||||
node(s, id).geometric_bounds().unwrap()
|
||||
}
|
||||
|
||||
/// Place Pattern Art places a copy of the tile art in a group with an unpainted rectangle of the
|
||||
/// tile at its back, centered where it is asked for (by default the first artboard's center), in
|
||||
/// the current layer, selected, as one undo step; the pattern does not change.
|
||||
#[test]
|
||||
fn place_art_places_the_tile_art_with_its_tile() {
|
||||
let mut s = session();
|
||||
let red = make_dots(&mut s);
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
let def = doc(&s).pattern("Dots").unwrap().clone();
|
||||
assert_eq!(def.tile, Rect::new(-5.0, -5.0, 15.0, 15.0));
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
let id = id_of(&s.execute("pattern.placeArt", &json!({"name": "Dots", "x": 200, "y": 150})).unwrap());
|
||||
let st = s.doc().unwrap();
|
||||
assert_eq!((st.history.undo.len(), st.history.undo.last().map(|e| e.label.as_str())), (steps + 1, Some("Place Pattern Art")));
|
||||
assert_eq!(st.selection.objects.to_vec(), vec![id], "the copy is selected");
|
||||
assert_eq!(st.doc.parent_of(id), Some(st.doc.layers[0].id), "in the current layer");
|
||||
assert_eq!(doc(&s).pattern("Dots").unwrap(), &def, "the pattern stays as it was");
|
||||
let group = node(&s, id);
|
||||
let NodeKind::Group { children, clip: false } = &group.kind else { panic!("a group: {:?}", group.kind) };
|
||||
let [marker, art] = children.as_slice() else { panic!("the tile and the art: {children:?}") };
|
||||
// The tile, centered on (200, 150), is unpainted; the art keeps its place in the tile.
|
||||
assert_eq!(marker.geometric_bounds(), Some(Rect::new(190.0, 140.0, 210.0, 160.0)));
|
||||
assert!(marker.appearance.fill_paint().is_none() && marker.appearance.stroke().is_none_or(|k| k.paint.is_none()));
|
||||
assert_eq!(art.geometric_bounds(), Some(Rect::new(195.0, 145.0, 205.0, 155.0)));
|
||||
assert_eq!(art.appearance, def.art[0].appearance);
|
||||
assert!(art.id != red && art.id != def.art[0].id, "a copy with an id of its own");
|
||||
// Without a position: the first artboard's center (the document is 400 × 400).
|
||||
let centered = id_of(&s.execute("pattern.placeArt", &json!({"name": "Dots"})).unwrap());
|
||||
assert_eq!(bounds_of(&s, centered).center(), Point::new(200.0, 200.0));
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(doc(&s).node(id).is_none() && doc(&s).node(centered).is_none(), "Undo removes the copies");
|
||||
}
|
||||
|
||||
/// The placed copy, edited and given back with Replace Pattern Art, becomes the tile art, and its
|
||||
/// rectangle becomes the tile: the art keeps its place in the tile, so the pattern draws the same
|
||||
/// arrangement with the new art. Given back unchanged, nothing is recorded.
|
||||
#[test]
|
||||
fn placed_art_given_back_keeps_its_place_in_the_tile() {
|
||||
let mut s = session();
|
||||
make_dots(&mut s);
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
let id = id_of(&s.execute("pattern.placeArt", &json!({"name": "Dots", "x": 200, "y": 150})).unwrap());
|
||||
let (before, steps) = (doc(&s).clone(), s.doc().unwrap().history.undo.len());
|
||||
s.execute("object.pattern.replaceArt", &json!({"name": "Dots", "ids": [id.0]})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1);
|
||||
let def = doc(&s).pattern("Dots").unwrap().clone();
|
||||
assert_eq!(def.tile, Rect::new(190.0, 140.0, 210.0, 160.0), "the rectangle is the tile");
|
||||
assert_eq!(def.art.len(), 1, "the rectangle is not part of the art: {:?}", def.art);
|
||||
assert_eq!(def.art[0].geometric_bounds(), Some(Rect::new(195.0, 145.0, 205.0, 155.0)));
|
||||
let old = before.pattern("Dots").unwrap();
|
||||
let place = |tile: Rect, art: &[Arc<Node>]| vectorcraft_doc::live::nodes_bounds(art).unwrap().origin() - tile.origin();
|
||||
assert_eq!(place(def.tile, &def.art), place(old.tile, &old.art), "the art keeps its place in the tile");
|
||||
// Given back again without a change: nothing to record.
|
||||
let (again, steps) = (doc(&s).clone(), s.doc().unwrap().history.undo.len());
|
||||
s.execute("object.pattern.replaceArt", &json!({"name": "Dots", "ids": [id.0]})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps);
|
||||
assert_eq!(doc(&s), &again);
|
||||
// Edited (the art recolored and the rectangle widened, ungrouped): the new art and tile.
|
||||
let NodeKind::Group { children, .. } = &node(&s, id).kind else { panic!("a group") };
|
||||
let (marker, art) = (children[0].id, children[1].id);
|
||||
s.execute("paint.setFill", &json!({"ids": [art.0], "color": "#0000ff"})).unwrap();
|
||||
sel(&mut s, &[id]);
|
||||
s.execute("object.ungroup", &json!({})).unwrap();
|
||||
assert!(doc(&s).node(id).is_none() && doc(&s).node(marker).is_some() && doc(&s).node(art).is_some(), "ungrouped");
|
||||
s.execute("object.transform", &json!({"ids": [marker.0], "matrix": [2, 0, 0, 1, -190, 0]})).unwrap();
|
||||
s.execute("object.pattern.replaceArt", &json!({"name": "Dots", "ids": [marker.0, art.0]})).unwrap();
|
||||
let def = doc(&s).pattern("Dots").unwrap();
|
||||
assert_eq!(def.tile, Rect::new(190.0, 140.0, 230.0, 160.0), "the widened rectangle is the tile");
|
||||
assert_eq!(def.art.len(), 1);
|
||||
assert_eq!(def.art[0].appearance, node(&s, art).appearance, "the recolored art");
|
||||
}
|
||||
|
||||
/// Only an unpainted rectangle at the back of the art is the tile: a painted one, or an unpainted
|
||||
/// one in front of other art, is tile art like any other, and a rectangle alone leaves no art.
|
||||
#[test]
|
||||
fn only_an_unpainted_rectangle_at_the_back_is_the_tile() {
|
||||
let mut s = session();
|
||||
make_dots(&mut s);
|
||||
let back = rect(&mut s, 100.0, 100.0, 40.0, 40.0);
|
||||
let front = rect(&mut s, 110.0, 110.0, 10.0, 10.0);
|
||||
s.execute("paint.setFill", &json!({"ids": [front.0], "color": "#00ff00"})).unwrap();
|
||||
// Painted: art.
|
||||
s.execute("paint.setFill", &json!({"ids": [back.0], "color": "#ffffff"})).unwrap();
|
||||
s.execute("object.pattern.replaceArt", &json!({"name": "Dots", "ids": [back.0, front.0]})).unwrap();
|
||||
assert_eq!(doc(&s).pattern("Dots").unwrap().art.len(), 2);
|
||||
// Unpainted, but in front of the other art: art.
|
||||
s.execute("paint.setFill", &json!({"ids": [back.0], "none": true})).unwrap();
|
||||
s.execute("paint.setStroke", &json!({"ids": [back.0], "none": true})).unwrap();
|
||||
sel(&mut s, &[back]);
|
||||
s.execute("object.arrange.bringToFront", &json!({})).unwrap();
|
||||
s.execute("object.pattern.replaceArt", &json!({"name": "Dots", "ids": [back.0, front.0]})).unwrap();
|
||||
assert_eq!(doc(&s).pattern("Dots").unwrap().art.len(), 2);
|
||||
// Unpainted and at the back: the tile.
|
||||
s.execute("object.arrange.sendToBack", &json!({})).unwrap();
|
||||
s.execute("object.pattern.replaceArt", &json!({"name": "Dots", "ids": [back.0, front.0]})).unwrap();
|
||||
let def = doc(&s).pattern("Dots").unwrap();
|
||||
assert_eq!((def.art.len(), def.tile), (1, Rect::new(100.0, 100.0, 140.0, 140.0)));
|
||||
// A rectangle alone is a tile with no art.
|
||||
let err = s.execute("object.pattern.replaceArt", &json!({"name": "Dots", "ids": [back.0]})).unwrap_err();
|
||||
assert!(err.to_string().contains("select the art for the pattern tile"), "{err}");
|
||||
// An unpainted path through the four corners of its bounds in a crossing order is not a
|
||||
// rectangle: art.
|
||||
let corners = [(200, 200), (240, 240), (240, 200), (200, 240)];
|
||||
let anchors: Vec<_> = corners.iter().map(|(x, y)| json!({"x": x, "y": y})).collect();
|
||||
let bowtie = NodeId(s.execute("path.create", &json!({"anchors": anchors, "closed": true})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("paint.setFill", &json!({"ids": [bowtie.0], "none": true})).unwrap();
|
||||
s.execute("paint.setStroke", &json!({"ids": [bowtie.0], "none": true})).unwrap();
|
||||
sel(&mut s, &[bowtie]);
|
||||
s.execute("object.arrange.sendToBack", &json!({})).unwrap();
|
||||
s.execute("object.pattern.replaceArt", &json!({"name": "Dots", "ids": [bowtie.0, front.0]})).unwrap();
|
||||
let def = doc(&s).pattern("Dots").unwrap();
|
||||
assert_eq!(def.art.len(), 2, "the crossing path is art: {:?}", def.art);
|
||||
assert_ne!(def.tile, Rect::new(200.0, 200.0, 240.0, 240.0), "the crossing path is not the tile");
|
||||
}
|
||||
|
||||
/// Place Pattern Art is refused, changing nothing, for a missing or unknown name, a position that
|
||||
/// is not a number, a pattern with no art, while a pattern is edited and on a locked layer.
|
||||
#[test]
|
||||
fn place_art_is_refused_without_changing_anything() {
|
||||
let mut s = session();
|
||||
make_dots(&mut s);
|
||||
doc_mut(&mut s).patterns.push(vectorcraft_doc::pattern::PatternDef::new("Empty", vec![]));
|
||||
let (before, steps) = (doc(&s).clone(), s.doc().unwrap().history.undo.len());
|
||||
let unchanged = |s: &Session, case: &str| {
|
||||
assert_eq!(doc(s), &before, "{case}: the document stays");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps, "{case}: no undo step");
|
||||
};
|
||||
for (case, p, why) in [
|
||||
("no name", json!({}), "missing name"),
|
||||
("no pattern", json!({"name": "Stripes"}), "no pattern `Stripes`"),
|
||||
("x not a number", json!({"name": "Dots", "x": "100"}), "`x` must be a number"),
|
||||
("y not a number", json!({"name": "Dots", "y": true}), "`y` must be a number"),
|
||||
("no art", json!({"name": "Empty"}), "no artwork"),
|
||||
] {
|
||||
let err = s.execute("pattern.placeArt", &p).unwrap_err();
|
||||
assert!(err.to_string().contains(why), "{case}: {err}");
|
||||
unchanged(&s, case);
|
||||
}
|
||||
let layer = doc(&s).layers[0].id;
|
||||
s.execute("layer.setProps", &json!({"ids": [layer.0], "locked": true})).unwrap();
|
||||
let (before, steps) = (doc(&s).clone(), s.doc().unwrap().history.undo.len());
|
||||
let err = s.execute("pattern.placeArt", &json!({"name": "Dots"})).unwrap_err();
|
||||
assert_eq!(err.to_string(), "the target layer is locked or hidden");
|
||||
assert_eq!((doc(&s), s.doc().unwrap().history.undo.len()), (&before, steps), "locked: unchanged");
|
||||
s.execute("layer.setProps", &json!({"ids": [layer.0], "locked": false})).unwrap();
|
||||
s.execute("object.pattern.edit", &json!({"name": "Dots"})).unwrap();
|
||||
let (before, steps) = (doc(&s).clone(), s.doc().unwrap().history.undo.len());
|
||||
let err = s.execute("pattern.placeArt", &json!({"name": "Dots"})).unwrap_err();
|
||||
assert!(matches!(&err, EngineError::Disabled(_, w) if w == "finish editing the pattern first"), "while editing: {err}");
|
||||
assert_eq!((doc(&s), s.doc().unwrap().history.undo.len()), (&before, steps), "while editing: unchanged");
|
||||
}
|
||||
|
||||
/// Replace Pattern Art is refused, changing nothing, for a missing name or pattern, with no art,
|
||||
/// while a pattern is edited, and for art painted with the pattern itself, directly, through
|
||||
/// another pattern, through a symbol, through the paints a graph stores, through a graph design a
|
||||
|
||||
Reference in new issue
Block a user