mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 20:19:40 +00:00
Brushes: drag a brush onto the canvas to place a copy of its art
A scatter, art or pattern brush dragged from the Brushes panel onto an empty part of the canvas places a copy of its art there to edit, centered where it is dropped (brush.placeArt). Dropped on a path, a brush is still applied to it. A pattern brush is placed as its tiles in a row, 10 pt apart, each in a group named for its tile, with an unpainted rectangle at the back of a tile that has a repeat frame. Calligraphic and bristle brushes have no art and are refused. A symbol instance in the art is measured by its symbol's art. Docs: docs/mcp.md, docs/target-app-parity.md.
This commit is contained in:
@@ -33,8 +33,8 @@ mod symbols;
|
||||
|
||||
pub(crate) use brushes::{apply_brush, brush_targets, stroke_paint};
|
||||
use brushes::{
|
||||
brush_apply, brush_delete, brush_duplicate, brush_freehand, brush_get, brush_list, brush_move, brush_new, brush_options, brush_remove,
|
||||
brush_selection_art, brush_set_current, brush_unused, expand_brush,
|
||||
brush_apply, brush_delete, brush_duplicate, brush_freehand, brush_get, brush_list, brush_move, brush_new, brush_options, brush_place_art,
|
||||
brush_remove, brush_selection_art, brush_set_current, brush_unused, expand_brush,
|
||||
};
|
||||
pub(crate) use dynamic::{edit_part, part_only, selected_part};
|
||||
use dynamic::{symbol_instance_reset, symbol_instance_select_part, symbol_instance_set_appearance};
|
||||
@@ -67,6 +67,15 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
has_doc,
|
||||
brush_selection_art
|
||||
),
|
||||
cmd!(
|
||||
"brush.placeArt",
|
||||
"Place Brush Art",
|
||||
[],
|
||||
None,
|
||||
"{name, x?, y?} place a copy of the art a scatter, art or pattern brush is made of, centered on (x, y) (default: the center of the first artboard), and select it, as dragging the brush from the Brushes panel onto an empty part of the canvas does; a pattern brush places its tiles left to right (side, outer corner, inner corner, start, end; the ones it has art for) 10 pt apart, each in a group named Side Tile, Outer Corner Tile, Inner Corner Tile, Start Tile or End Tile, with an unpainted rectangle at the back of a tile that has a repeat frame marking that frame; a calligraphic or bristle brush is refused → {id}",
|
||||
has_doc,
|
||||
brush_place_art
|
||||
),
|
||||
cmd!(
|
||||
"brush.apply",
|
||||
"Apply Brush",
|
||||
|
||||
@@ -483,6 +483,93 @@ pub(super) fn brush_options(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(json!({ "name": out }))
|
||||
}
|
||||
|
||||
/// The name `brush.placeArt` gives each pattern brush tile it places, by tile, in the order it
|
||||
/// places them.
|
||||
const TILE_NAMES: [&str; 5] = ["Side Tile", "Outer Corner Tile", "Inner Corner Tile", "Start Tile", "End Tile"];
|
||||
|
||||
/// The space between the pattern brush tiles `brush.placeArt` places, in points.
|
||||
const TILE_GAP: f64 = 10.0;
|
||||
|
||||
/// The message for a brush that is not made of art.
|
||||
fn no_art(name: &str) -> String {
|
||||
format!("`{name}` has no art (only scatter, art and pattern brushes do)")
|
||||
}
|
||||
|
||||
/// Tile `i` of `p` ([`TILE_NAMES`] order): its art and its repeat frame, when it has art.
|
||||
fn tile(p: &brush::PatternBrush, i: usize) -> Option<(&Node, Option<vectorcraft_geom::Rect>)> {
|
||||
match i {
|
||||
0 => Some((&p.side, p.side_frame)),
|
||||
1 => p.outer_corner.as_ref().map(|n| (n, p.outer_corner_frame)),
|
||||
2 => p.inner_corner.as_ref().map(|n| (n, p.inner_corner_frame)),
|
||||
3 => p.start.as_ref().map(|n| (n, p.start_frame)),
|
||||
_ => p.end.as_ref().map(|n| (n, p.end_frame)),
|
||||
}
|
||||
}
|
||||
|
||||
/// The art `brush.placeArt` places for brush `b`, in the brush's own coordinates: a scatter or art
|
||||
/// brush's art, or a pattern brush's tiles in a row, each in a group named for its tile
|
||||
/// ([`TILE_NAMES`]), with an unpainted rectangle at the back of a tile that has a repeat frame
|
||||
/// marking that frame. `None` for a brush that is not made of art. Symbol instances in the art are
|
||||
/// measured by their symbols' art in `symbols`.
|
||||
fn placed_art(b: &Brush, symbols: &vectorcraft_doc::SymbolBoxes) -> Option<Node> {
|
||||
match &b.kind {
|
||||
BrushKind::Scatter(sc) => Some(sc.art.clone()),
|
||||
BrushKind::Art(a) => Some(a.art.clone()),
|
||||
BrushKind::Pattern(pb) => {
|
||||
let mut tiles = vec![];
|
||||
let mut x = 0.0;
|
||||
for (i, tile_name) in TILE_NAMES.iter().enumerate() {
|
||||
let Some((art, frame)) = tile(pb, i) else { continue };
|
||||
let mut children = vec![];
|
||||
if let Some(f) = frame {
|
||||
let marker = Node::path(
|
||||
NodeId(0),
|
||||
vectorcraft_geom::shapes::rectangle(f),
|
||||
vectorcraft_doc::Appearance::basic(Paint::None, Paint::None, 1.0),
|
||||
);
|
||||
children.push(Arc::new(marker));
|
||||
}
|
||||
children.push(Arc::new(art.clone()));
|
||||
let mut g = Node::group(NodeId(0), children);
|
||||
g.name = Some((*tile_name).to_string());
|
||||
let Some(r) = g.geometric_bounds_with(symbols) else { continue };
|
||||
// Each tile sits right of the one before it, centered on the line y = 0.
|
||||
g.transform(vectorcraft_geom::Affine::translate((x - r.x0, -r.center().y)), false);
|
||||
x += r.width() + TILE_GAP;
|
||||
tiles.push(Arc::new(g));
|
||||
}
|
||||
Some(Node::group(NodeId(0), tiles))
|
||||
}
|
||||
BrushKind::Calligraphic(_) | BrushKind::Bristle(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// `brush.placeArt`: a copy of the art brush `name` is made of, centered on (`x`, `y`) (default: the
|
||||
/// center of the first artboard) in the layer new art goes into, and selected, as one undo step.
|
||||
/// Scatter and art brushes place their art; pattern brushes place their tiles ([`placed_art`]).
|
||||
pub(super) fn brush_place_art(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "brush.placeArt";
|
||||
let name = name_param(p, C)?.to_string();
|
||||
let d = &s.doc()?.doc;
|
||||
let center = d.artboards.first().map(|a| a.rect.center()).unwrap_or(super::Point::ZERO);
|
||||
let at = super::Point::new(super::number_or(p, "x", center.x, C)?, super::number_or(p, "y", center.y, C)?);
|
||||
let b = brush::find(d, &name).ok_or_else(|| bad(C, format!("no brush named `{name}`")))?;
|
||||
let symbols = d.symbol_boxes();
|
||||
let mut art = placed_art(&b, &symbols).ok_or_else(|| bad(C, no_art(&name)))?;
|
||||
if let Some(r) = art.geometric_bounds_with(&symbols) {
|
||||
art.transform(vectorcraft_geom::Affine::translate(at - r.center()), false);
|
||||
}
|
||||
let parent = s.doc()?.target_parent()?;
|
||||
let id = s.edit("Place Brush 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 }))
|
||||
}
|
||||
|
||||
pub(super) fn expand_brush(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let ids = brush_targets(&s.doc()?.doc, &roots(s, p)?);
|
||||
let ids: Vec<NodeId> = ids.into_iter().filter(|id| s.doc().ok().and_then(|st| st.doc.node(*id)).is_some_and(brush::has_brush)).collect();
|
||||
|
||||
@@ -108,6 +108,7 @@ pub(super) const ALLOWED: &[&str] = &[
|
||||
"brush.move",
|
||||
"brush.new",
|
||||
"brush.options",
|
||||
"brush.placeArt",
|
||||
"brush.remove",
|
||||
"brush.selectionArt",
|
||||
"brush.setCurrent",
|
||||
|
||||
@@ -253,6 +253,118 @@ fn selection_art_is_the_art_new_brush_takes() {
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1, "only brush.new is an undo step");
|
||||
}
|
||||
|
||||
fn undo_steps(s: &Session) -> usize {
|
||||
s.doc().unwrap().history.undo.len()
|
||||
}
|
||||
|
||||
fn selected(s: &Session) -> Vec<NodeId> {
|
||||
s.doc().unwrap().selection.objects.clone()
|
||||
}
|
||||
|
||||
/// The JSON of object `id`, as `brush.options` params take art.
|
||||
fn art_json(s: &mut Session, id: NodeId) -> Value {
|
||||
s.execute("brush.selectionArt", &json!({"ids": [id.0]})).unwrap()["art"].clone()
|
||||
}
|
||||
|
||||
/// `brush.placeArt` places a copy of an art or scatter brush's art centered where it is dropped
|
||||
/// (by default on the first artboard's center), selected, in one undo step; brushes without art,
|
||||
/// unknown brushes and a position that is not a number are refused.
|
||||
#[test]
|
||||
fn place_art_puts_a_copy_of_the_brush_art_where_it_is_dropped() {
|
||||
let mut s = session();
|
||||
let r = rect(&mut s, 10.0, 20.0, 40.0, 8.0);
|
||||
s.execute("brush.new", &json!({"type": "art", "name": "Bar", "ids": [r.0]})).unwrap();
|
||||
let steps = undo_steps(&s);
|
||||
let id = NodeId(s.execute("brush.placeArt", &json!({"name": "Bar", "x": 100, "y": 80})).unwrap()["id"].as_u64().unwrap());
|
||||
assert_eq!(undo_steps(&s), steps + 1);
|
||||
assert_eq!(selected(&s), vec![id]);
|
||||
assert_ne!(id, r, "a copy, not the art the brush was made from");
|
||||
assert!(close(bounds(&s, id), Rect::new(80.0, 76.0, 120.0, 84.0), 1e-6), "{:?}", bounds(&s, id));
|
||||
// Without a position: the first artboard's center (the document is 400 × 300).
|
||||
let scatter = NodeId(s.execute("brush.placeArt", &json!({"name": "Dots"})).unwrap()["id"].as_u64().unwrap());
|
||||
let c = bounds(&s, scatter).center();
|
||||
assert!((c.x - 200.0).abs() < 1e-6 && (c.y - 150.0).abs() < 1e-6, "{c:?}");
|
||||
let steps = undo_steps(&s);
|
||||
for (p, msg) in [
|
||||
(json!({"name": "3 pt. Round"}), "`3 pt. Round` has no art (only scatter, art and pattern brushes do)"),
|
||||
(json!({"name": "Bristle Round"}), "`Bristle Round` has no art (only scatter, art and pattern brushes do)"),
|
||||
(json!({"name": "Nope"}), "no brush named `Nope`"),
|
||||
] {
|
||||
let e = s.execute("brush.placeArt", &p).unwrap_err();
|
||||
assert!(e.to_string().contains(msg), "{p}: {e}");
|
||||
}
|
||||
assert!(s.execute("brush.placeArt", &json!({"name": "Bar", "x": "100"})).is_err());
|
||||
assert_eq!(undo_steps(&s), steps, "a refused call adds no undo step");
|
||||
}
|
||||
|
||||
/// Brush art holding an instance of a symbol whose art lies away from the symbol's origin (as
|
||||
/// imported symbols can) is centered by that art, not by the box an instance has without its
|
||||
/// document.
|
||||
#[test]
|
||||
fn placed_art_with_a_symbol_instance_is_centered_by_the_symbols_art() {
|
||||
let mut s = session();
|
||||
let r = rect(&mut s, 300.0, 10.0, 100.0, 40.0);
|
||||
let art = node(&s, r);
|
||||
s.execute("edit.clear", &json!({"ids": [r.0]})).unwrap();
|
||||
let st = s.doc_mut().unwrap();
|
||||
let d = std::sync::Arc::make_mut(&mut st.doc);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Off".into(), art: std::sync::Arc::new(art) });
|
||||
let layer = d.layers[0].id;
|
||||
let inst = d.alloc_id();
|
||||
let xf = vectorcraft_geom::Affine::translate((50.0, 50.0));
|
||||
d.insert(Some(layer), usize::MAX, Node::new(inst, NodeKind::SymbolInstance { symbol: "Off".into(), xf })).unwrap();
|
||||
let inst = inst.0;
|
||||
s.execute("brush.new", &json!({"type": "art", "name": "Stamp", "ids": [inst]})).unwrap();
|
||||
let id = NodeId(s.execute("brush.placeArt", &json!({"name": "Stamp", "x": 200, "y": 150})).unwrap()["id"].as_u64().unwrap());
|
||||
let st = s.doc().unwrap();
|
||||
let b = st.doc.bounds_of(&[id], false).unwrap();
|
||||
assert!((b.center().x - 200.0).abs() < 1e-6 && (b.center().y - 150.0).abs() < 1e-6, "{b:?}");
|
||||
assert!((b.width() - 100.0).abs() < 1e-6, "{b:?}");
|
||||
}
|
||||
|
||||
/// A pattern brush with side, outer corner and start tiles, the side tile with a repeat frame
|
||||
/// wider than its art: → its name.
|
||||
fn framed_pattern_brush(s: &mut Session) -> String {
|
||||
let side = rect(s, 0.0, 0.0, 20.0, 10.0);
|
||||
let corner = rect(s, 0.0, 40.0, 10.0, 10.0);
|
||||
let start = rect(s, 0.0, 80.0, 6.0, 10.0);
|
||||
let name = s.execute("brush.new", &json!({"type": "pattern", "name": "Rail", "ids": [side.0]})).unwrap()["name"].as_str().unwrap().to_string();
|
||||
let params = json!({
|
||||
"side": art_json(s, side),
|
||||
"side_frame": Rect::new(-2.0, 0.0, 22.0, 10.0),
|
||||
"outer_corner": art_json(s, corner),
|
||||
"start": art_json(s, start),
|
||||
});
|
||||
s.execute("brush.options", &json!({"name": name, "params": params})).unwrap();
|
||||
name
|
||||
}
|
||||
|
||||
/// `brush.placeArt` lays a pattern brush's tiles out left to right, each in a group named for its
|
||||
/// tile, with an unpainted rectangle at the back of a tile with a repeat frame marking that frame.
|
||||
#[test]
|
||||
fn pattern_brushes_place_their_tiles_in_a_row() {
|
||||
let mut s = session();
|
||||
let name = framed_pattern_brush(&mut s);
|
||||
let id = NodeId(s.execute("brush.placeArt", &json!({"name": name, "x": 200, "y": 150})).unwrap()["id"].as_u64().unwrap());
|
||||
let placed = node(&s, id);
|
||||
let tiles: Vec<Node> = placed.children().unwrap().iter().map(|c| (**c).clone()).collect();
|
||||
let names: Vec<&str> = tiles.iter().map(|t| t.name.as_deref().unwrap_or("")).collect();
|
||||
assert_eq!(names, ["Side Tile", "Outer Corner Tile", "Start Tile"]);
|
||||
let boxes: Vec<Rect> = tiles.iter().map(|t| t.geometric_bounds().unwrap()).collect();
|
||||
assert!(boxes.windows(2).all(|w| (w[1].x0 - w[0].x1 - 10.0).abs() < 1e-6), "10 pt apart, left to right: {boxes:?}");
|
||||
// The side tile: the frame's rectangle, unpainted, behind the art.
|
||||
let side = tiles[0].children().unwrap();
|
||||
assert_eq!(side.len(), 2);
|
||||
let marker = &side[0];
|
||||
assert!(marker.appearance.items.iter().all(|i| match i {
|
||||
vectorcraft_doc::AppearanceItem::Fill(f) => f.paint.is_none(),
|
||||
vectorcraft_doc::AppearanceItem::Stroke(st) => st.paint.is_none(),
|
||||
}));
|
||||
let frame = marker.geometric_bounds().unwrap();
|
||||
assert!((frame.width() - 24.0).abs() < 1e-6 && (frame.height() - 10.0).abs() < 1e-6, "{frame:?}");
|
||||
assert_eq!(tiles[1].children().unwrap().len(), 1, "no frame, no rectangle");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn brush_options_rename_duplicate_and_delete_update_strokes() {
|
||||
let mut s = session();
|
||||
|
||||
Reference in New Issue
Block a user