Shapes: drawing with a shape tool adds to the selected shape layer (#227)

With a shape layer selected, the Rectangle, Rounded Rectangle, Ellipse,
Polygon and Star tools made a new shape layer for every shape. After
Effects draws into the selected shape layer instead: the shape becomes a
new group on top of its Contents ("Rectangle 2", ...), placed by the
group's Transform. The new shape.newShape command does that (in the
layer's space, so a moved or scaled layer still gets the shape where it
was drawn), and makes a new shape layer only when no unlocked shape
layer is selected. The viewer's shape tools use it; masks on other
selected layers are unchanged. layer.newShape and the Pen's
shape.newPath share its layer and Contents helpers.

Test: shape_tools_draw_into_the_selected_shape_layer,
shape_tool_draws_into_the_selected_shape_layer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bflatastic
2026-10-08 13:03:38 +07:00
co-authored by Claude Opus 5.5
parent 7e4a7cd863
commit 9968950b71
8 changed files with 263 additions and 58 deletions
+7 -3
View File
@@ -1828,12 +1828,16 @@ fn create_shape(app: &mut EffectcraftApp, tool: Tool, a: [f64; 2], b: [f64; 2],
return;
}
}
// Into the selected shape layer's Contents as a new group (as in After Effects), else a new
// shape layer.
let fill = app.ui.fill_color;
let stroke = app.ui.stroke_color;
let _ = app.session.execute(
"layer.newShape",
if let Err(e) = app.session.execute(
"shape.newShape",
json!({"kind": kind, "size": [w, h], "position": [cx, cy], "fill": [fill[0], fill[1], fill[2]], "stroke": [stroke[0], stroke[1], stroke[2]], "strokeWidth": app.ui.stroke_width}),
);
) {
app.ui.status = e.to_string();
}
}
fn empty_state(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
+33
View File
@@ -247,6 +247,39 @@ fn layer_drag_snaps_to_comp_centre_and_ctrl_disables() {
assert!((p[0] - 320.0).abs() > 1.0, "{p:?}");
}
/// With a shape layer selected the Rectangle tool draws a new group into its Contents (After
/// Effects' behaviour); with nothing selected it draws a new shape layer (#227).
#[test]
fn shape_tool_draws_into_the_selected_shape_layer() {
let mut h = harness();
let l = h.state_mut().session.execute("layer.newShape", json!({"kind": "rect", "size": [100, 100], "position": [320, 180]})).unwrap()["layer"]
.as_u64()
.unwrap();
h.state_mut().ui.tool = Tool::Rectangle;
h.run_steps(2);
let n0 = h.state().session.active_comp().unwrap().layers.len();
let (a, b) = (screen(&h, [100.0, 100.0]), screen(&h, [200.0, 160.0]));
drag(&mut h, a, b);
let comp = h.state().session.active_comp().unwrap().clone();
assert_eq!(comp.layers.len(), n0, "no new layer");
let contents = comp.layer(LayerId(l)).unwrap().props.sub("contents").unwrap().clone();
let names: Vec<&str> = contents.groups().map(|g| g.name.as_str()).collect();
assert_eq!(names, ["Rectangle 2", "Rectangle 1"]);
// Placed where it was drawn (centred near comp (150, 130)), in the layer's space: the layer
// sits at the comp centre.
let g = contents.groups().next().unwrap();
let at = g.sub("transform").unwrap().get("position").unwrap().value.components();
assert!((at[0] + 170.0).abs() < 8.0 && (at[1] + 50.0).abs() < 8.0, "{at:?}");
// Nothing selected: a new shape layer.
h.state_mut().session.execute("edit.deselectAll", json!({})).unwrap();
let (a, b) = (screen(&h, [400.0, 250.0]), screen(&h, [500.0, 300.0]));
drag(&mut h, a, b);
let comp = h.state().session.active_comp().unwrap();
assert_eq!(comp.layers.len(), n0 + 1);
assert!(matches!(comp.layers[0].source, effectcraft_engine::project::LayerSource::Shape));
assert_ne!(comp.layers[0].id, LayerId(l));
}
#[test]
fn shape_pen_draws_a_closed_shape_layer() {
let mut h = harness();