Shapes: Tools bar Tool Creates Shape / Mask and Fill / Stroke Options (#227)
The Tools bar had Fill and Stroke swatches and a Stroke Width, kept in the interface only, so agents couldn't set them and new shapes could only be a solid fill with a solid stroke. As in After Effects: - With a shape layer selected and a shape tool or the Pen active, Tool Creates Shape / Tool Creates Mask choose whether drawing adds shapes to the layer or masks on it (Fill and Stroke hide while drawing masks). - Clicking the word "Fill" or "Stroke" opens Fill Options / Stroke Options: None, Solid Color, Linear Gradient or Radial Gradient, the blend mode and the opacity. The swatches show the kind; Alt-click steps through the kinds. - New shapes and Pen paths are painted with them: a Gradient Fill or Gradient Stroke across the shape for the gradient kinds. The options are engine state (EditorState.shape_tool) set with the new shape.toolOptions command; shape.newShape and shape.newPath use them unless given paint parameters (which now include fillType, fillBlend, fillOpacity and their stroke twins; layer.newShape takes them too). A Pen path without paint parameters now takes the Tools bar's stroke (none by default) instead of a fixed 2 px white one. layer.addMask also draws rounded rectangle, polygon and star masks, so every shape tool makes a mask on a selected footage or solid layer (rounded rectangles, polygons and stars made a new shape layer). Test: shape_tool_options_paint_new_shapes, masks_of_every_shape_tool_kind, tool_creates_mask_draws_a_mask_on_the_selected_shape_layer, fill_options_paint_the_next_shape. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
0048569682
commit
89e075d1b1
@@ -1,7 +1,7 @@
|
||||
//! Layer menu.
|
||||
|
||||
use effectcraft_color::{BlendMode, Label};
|
||||
use effectcraft_keyframe::{Justify, ShapePath, TextDoc, Value as KV};
|
||||
use effectcraft_keyframe::{Justify, TextDoc, Value as KV};
|
||||
use effectcraft_project::build::{self, Ids};
|
||||
use effectcraft_project::{
|
||||
Comp, FrameBlend, GroupKind, ItemId, ItemKind, Layer, LayerId, LayerSource, MaskMode, MatteKind, Project, PropGroup, Quality, Solid, TrackMatte,
|
||||
@@ -205,11 +205,6 @@ fn new_text(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(json!({"layer": id.0}))
|
||||
}
|
||||
|
||||
/// Contents for a new shape (one group with path + fill + stroke).
|
||||
pub(crate) fn c4(c: Option<[f32; 3]>, d: [f64; 4]) -> [f64; 4] {
|
||||
c.map(|c| [c[0] as f64, c[1] as f64, c[2] as f64, 1.0]).unwrap_or(d)
|
||||
}
|
||||
|
||||
fn new_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cid = comp_id(s, p)?;
|
||||
let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?.clone();
|
||||
@@ -219,8 +214,8 @@ fn new_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
.and_then(Value::as_array)
|
||||
.map(|a| [a.first().and_then(Value::as_f64).unwrap_or(200.0), a.get(1).and_then(Value::as_f64).unwrap_or(200.0)])
|
||||
.unwrap_or([300.0, 300.0]);
|
||||
let fill = Some(c4(color_p(p, "fill"), [0.25, 0.55, 1.0, 1.0]));
|
||||
let stroke = (f_p(p, "strokeWidth").unwrap_or(0.0) > 0.0).then(|| (c4(color_p(p, "stroke"), [1.0, 1.0, 1.0, 1.0]), f_p(p, "strokeWidth").unwrap_or(2.0)));
|
||||
// A fill, and a stroke only with a width (not the Tools bar's paint).
|
||||
let paint = super::shape_tool::paint_p(p, &Default::default(), "layer.newShape")?;
|
||||
let pos = p
|
||||
.get("position")
|
||||
.and_then(|v| v.as_array())
|
||||
@@ -229,7 +224,7 @@ fn new_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let id = s.edit("New Shape Layer", None, |proj, st| {
|
||||
let lid = super::shape_tool::new_shape_layer(proj, st, &comp, cid, Some(&name), pos)?;
|
||||
let mut next = proj.next_id;
|
||||
let g = super::shape_tool::shape_group(&mut Ids(&mut next), &kind, size, fill, stroke);
|
||||
let g = super::shape_tool::shape_group(&mut Ids(&mut next), &kind, size, &paint);
|
||||
proj.next_id = next;
|
||||
if let Some(g) = g {
|
||||
super::shape_tool::add_to_contents(proj, cid, lid, g, [0.0, 0.0], "layer.newShape")?;
|
||||
@@ -974,10 +969,9 @@ fn add_mask(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Some([x, y, rw, rh]) => (x + rw / 2.0, y + rh / 2.0, rw, rh),
|
||||
None => (if w > 0.0 { w / 2.0 } else { 0.0 }, h / 2.0, w * 0.6, h * 0.6),
|
||||
};
|
||||
let path = match shape {
|
||||
"ellipse" => ShapePath::ellipse([cx, cy], rw, rh),
|
||||
_ => ShapePath::rect([cx, cy], rw, rh),
|
||||
};
|
||||
// The shape tools' masks: rectangle, ellipse, rounded rectangle, polygon or star.
|
||||
let path = super::shape_tool::mask_path(shape, [cx, cy], [rw, rh])
|
||||
.ok_or_else(|| bad("layer.addMask", format!("unknown shape `{shape}`; one of rect, ellipse, rounded, polygon, star")))?;
|
||||
let mode = str_p(p, "mode").and_then(MaskMode::from_name).unwrap_or(MaskMode::Add);
|
||||
let cycle = s.prefs.appearance.cycle_mask_colors;
|
||||
let uid = s.edit("New Mask", None, |proj, _| {
|
||||
@@ -1292,7 +1286,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Shape Layer",
|
||||
["Layer", "New"],
|
||||
None,
|
||||
"{kind?: rect|rounded|ellipse|star|polygon|none, name?, size?, fill?, stroke?, strokeWidth?, position?}",
|
||||
"{kind?: rect|rounded|ellipse|star|polygon|none, name?, size?, fill?, fillType?, fillBlend?, fillOpacity?, stroke?, strokeType?, strokeBlend?, strokeOpacity?, strokeWidth?, position?}",
|
||||
has_comp,
|
||||
new_shape
|
||||
),
|
||||
@@ -1375,7 +1369,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"New Mask",
|
||||
["Layer", "Mask"],
|
||||
Some("Cmd+Shift+N"),
|
||||
"{layer?, shape?: rect|ellipse, rect? [x,y,w,h], mode?}",
|
||||
"{layer?, shape?: rect|ellipse|rounded|polygon|star, rect? [x,y,w,h] (layer space; a polygon or star fits its width), mode?}",
|
||||
has_layers,
|
||||
add_mask
|
||||
),
|
||||
|
||||
@@ -1,41 +1,224 @@
|
||||
//! The shape tools (Rectangle, Rounded Rectangle, Ellipse, Polygon, Star) as a command: a drawn
|
||||
//! shape goes into the selected shape layer's Contents as a new group, as in After Effects, or
|
||||
//! into a new shape layer.
|
||||
//! The shape tools (Rectangle, Rounded Rectangle, Ellipse, Polygon, Star) and the Pen's options
|
||||
//! in the Tools bar (Tool Creates Shape / Tool Creates Mask, Fill and Stroke with their Fill
|
||||
//! Options / Stroke Options), and drawing with them: a drawn shape goes into the selected shape
|
||||
//! layer's Contents as a new group, as in After Effects, or into a new shape layer.
|
||||
|
||||
use effectcraft_color::BlendMode;
|
||||
use effectcraft_geom::vec2;
|
||||
use effectcraft_keyframe::Value as KV;
|
||||
use effectcraft_keyframe::{Gradient, ShapePath, Value as KV};
|
||||
use effectcraft_project::build::{self, Ids};
|
||||
use effectcraft_project::{Comp, ItemId, LayerId, LayerSource, Project, PropGroup};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::layer::{c4, color_p, insert_layer, place, position_p};
|
||||
use super::{CommandSpec, bad, comp_id, f_p, has_comp, layer_mut, str_p};
|
||||
use super::layer::{color_p, insert_layer, place, position_p};
|
||||
use super::{CommandSpec, always, b_p, bad, comp_id, has_comp, layer_mut, str_p};
|
||||
use crate::{EditorState, EngineError, Result, Session, cmd};
|
||||
|
||||
/// The shape tools' kinds, as `shape.newShape` / `layer.newShape` name them.
|
||||
const KINDS: [&str; 5] = ["rect", "rounded", "ellipse", "polygon", "star"];
|
||||
|
||||
/// A new shape group for a shape tool `kind` of `size`, centred on the group's origin, with a
|
||||
/// Stroke and a Fill ("Rectangle 1", "Ellipse 1" or "Polystar 1"). None for another kind.
|
||||
pub(crate) fn shape_group(ids: &mut Ids, kind: &str, size: [f64; 2], fill: Option<[f64; 4]>, stroke: Option<([f64; 4], f64)>) -> Option<PropGroup> {
|
||||
/// Fill Options / Stroke Options: what new shapes are painted with.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum PaintKind {
|
||||
None,
|
||||
#[default]
|
||||
Solid,
|
||||
Linear,
|
||||
Radial,
|
||||
}
|
||||
|
||||
impl PaintKind {
|
||||
pub const ALL: [PaintKind; 4] = [PaintKind::None, PaintKind::Solid, PaintKind::Linear, PaintKind::Radial];
|
||||
|
||||
/// The `fillType` / `strokeType` value.
|
||||
pub fn id(self) -> &'static str {
|
||||
match self {
|
||||
PaintKind::None => "none",
|
||||
PaintKind::Solid => "solid",
|
||||
PaintKind::Linear => "linear",
|
||||
PaintKind::Radial => "radial",
|
||||
}
|
||||
}
|
||||
|
||||
/// After Effects' name in Fill Options / Stroke Options.
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
PaintKind::None => "None",
|
||||
PaintKind::Solid => "Solid Color",
|
||||
PaintKind::Linear => "Linear Gradient",
|
||||
PaintKind::Radial => "Radial Gradient",
|
||||
}
|
||||
}
|
||||
|
||||
/// An id or label, in any case and spacing (`radial`, `Radial Gradient`).
|
||||
pub fn from_name(s: &str) -> Option<PaintKind> {
|
||||
let k: String = s.chars().filter(|c| c.is_alphanumeric()).collect::<String>().to_ascii_lowercase();
|
||||
PaintKind::ALL.into_iter().find(|m| m.id() == k || m.label().replace(' ', "").to_ascii_lowercase() == k)
|
||||
}
|
||||
}
|
||||
|
||||
/// The Fill or the Stroke of the Tools bar: its kind, colour (for Solid Color), blend mode and
|
||||
/// opacity (%).
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct ToolPaint {
|
||||
pub kind: PaintKind,
|
||||
pub color: [f32; 3],
|
||||
pub blend: BlendMode,
|
||||
pub opacity: f64,
|
||||
}
|
||||
|
||||
impl Default for ToolPaint {
|
||||
fn default() -> Self {
|
||||
ToolPaint { kind: PaintKind::Solid, color: [1.0, 1.0, 1.0], blend: BlendMode::Normal, opacity: 100.0 }
|
||||
}
|
||||
}
|
||||
|
||||
/// The shape tools' and the Pen's options: Tool Creates Mask (with a shape layer selected they
|
||||
/// draw masks on it instead of shapes) and the Fill, Stroke and Stroke Width new shapes get.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct ShapeTool {
|
||||
pub creates_mask: bool,
|
||||
pub fill: ToolPaint,
|
||||
pub stroke: ToolPaint,
|
||||
/// 0 draws no stroke.
|
||||
pub stroke_width: f64,
|
||||
}
|
||||
|
||||
impl Default for ShapeTool {
|
||||
fn default() -> Self {
|
||||
ShapeTool { creates_mask: false, fill: ToolPaint { color: [0.25, 0.55, 1.0], ..ToolPaint::default() }, stroke: ToolPaint::default(), stroke_width: 0.0 }
|
||||
}
|
||||
}
|
||||
|
||||
/// `p`'s paint keys over `base`: `fill` / `stroke` (a colour, which makes a None paint Solid
|
||||
/// Color; `null` or `false` for None), `fillType` / `strokeType` (none|solid|linear|radial),
|
||||
/// `fillBlend` / `strokeBlend` (a blend mode), `fillOpacity` / `strokeOpacity` (%) and
|
||||
/// `strokeWidth` (0 draws no stroke).
|
||||
pub(crate) fn paint_p(p: &Value, base: &ShapeTool, cmd: &str) -> Result<ShapeTool> {
|
||||
let mut o = base.clone();
|
||||
for (key, paint) in [("fill", &mut o.fill), ("stroke", &mut o.stroke)] {
|
||||
match p.get(key) {
|
||||
None => {}
|
||||
Some(Value::Null | Value::Bool(false)) => paint.kind = PaintKind::None,
|
||||
Some(_) => {
|
||||
paint.color = color_p(p, key).ok_or_else(|| bad(cmd, format!("`{key}`: a colour [r, g, b] or #hex, or false for none")))?;
|
||||
if paint.kind == PaintKind::None {
|
||||
paint.kind = PaintKind::Solid;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(v) = p.get(format!("{key}Type").as_str()) {
|
||||
paint.kind = v.as_str().and_then(PaintKind::from_name).ok_or_else(|| bad(cmd, format!("`{key}Type`: none|solid|linear|radial")))?;
|
||||
}
|
||||
if let Some(v) = p.get(format!("{key}Blend").as_str()) {
|
||||
paint.blend = v.as_str().and_then(BlendMode::from_name).ok_or_else(|| bad(cmd, format!("`{key}Blend`: a blend mode name (normal, multiply…)")))?;
|
||||
}
|
||||
if let Some(v) = p.get(format!("{key}Opacity").as_str()) {
|
||||
paint.opacity = v.as_f64().filter(|v| v.is_finite()).ok_or_else(|| bad(cmd, format!("`{key}Opacity`: a percentage")))?.clamp(0.0, 100.0);
|
||||
}
|
||||
}
|
||||
if let Some(v) = p.get("strokeWidth") {
|
||||
o.stroke_width = v.as_f64().filter(|v| v.is_finite()).ok_or_else(|| bad(cmd, "`strokeWidth`: pixels"))?.max(0.0);
|
||||
}
|
||||
Ok(o)
|
||||
}
|
||||
|
||||
/// A Fill or Stroke item with the paint's blend mode and opacity.
|
||||
fn blended(mut g: PropGroup, paint: &ToolPaint) -> PropGroup {
|
||||
let blend = BlendMode::ALL.iter().position(|m| *m == paint.blend).unwrap_or(0) as u32;
|
||||
if let Some(pr) = g.get_mut("blend") {
|
||||
pr.value = KV::Enum(blend);
|
||||
}
|
||||
if let Some(pr) = g.get_mut("opacity") {
|
||||
pr.value = KV::Scalar(paint.opacity);
|
||||
}
|
||||
g
|
||||
}
|
||||
|
||||
/// The Stroke and Fill a new shape gets from `paint`; gradients run across `bounds` (`[x, y, w,
|
||||
/// h]` in the group's space): left to right, or out from the centre.
|
||||
pub(crate) fn paint_items(ids: &mut Ids, paint: &ShapeTool, bounds: [f64; 4]) -> Vec<PropGroup> {
|
||||
let [x, y, w, h] = bounds;
|
||||
let mid = [x + w / 2.0, y + h / 2.0];
|
||||
let ends = |radial: bool| if radial { (mid, [mid[0] + w.max(h).max(2.0) / 2.0, mid[1]]) } else { ([x, mid[1]], [x + w.max(1.0), mid[1]]) };
|
||||
let mut items = vec![];
|
||||
let (s, width) = (&paint.stroke, paint.stroke_width);
|
||||
let stroke = match s.kind {
|
||||
_ if width <= 0.0 => None,
|
||||
PaintKind::None => None,
|
||||
PaintKind::Solid => Some(build::shape_stroke(ids, rgba(s.color), width)),
|
||||
k => {
|
||||
let (a, b) = ends(k == PaintKind::Radial);
|
||||
Some(build::shape_gradient_stroke(ids, k == PaintKind::Radial, a, b, Gradient::default(), width))
|
||||
}
|
||||
};
|
||||
items.extend(stroke.map(|g| blended(g, s)));
|
||||
let f = &paint.fill;
|
||||
let fill = match f.kind {
|
||||
PaintKind::None => None,
|
||||
PaintKind::Solid => Some(build::shape_fill(ids, rgba(f.color))),
|
||||
k => {
|
||||
let (a, b) = ends(k == PaintKind::Radial);
|
||||
Some(build::shape_gradient_fill(ids, k == PaintKind::Radial, a, b, Gradient::default()))
|
||||
}
|
||||
};
|
||||
items.extend(fill.map(|g| blended(g, f)));
|
||||
items
|
||||
}
|
||||
|
||||
fn rgba(c: [f32; 3]) -> [f64; 4] {
|
||||
[c[0] as f64, c[1] as f64, c[2] as f64, 1.0]
|
||||
}
|
||||
|
||||
/// The Rounded Rectangle tool's corner roundness for `size`.
|
||||
fn roundness(size: [f64; 2]) -> f64 {
|
||||
size[0].min(size[1]) * 0.15
|
||||
}
|
||||
|
||||
/// The Polygon and Star tools' Polystar for `size`: (star, points, outer radius, inner radius).
|
||||
fn polystar_of(kind: &str, size: [f64; 2]) -> Option<(bool, f64, f64, f64)> {
|
||||
match kind {
|
||||
"star" => Some((true, 5.0, size[0] / 2.0, size[0] / 4.0)),
|
||||
"polygon" => Some((false, 6.0, size[0] / 2.0, 0.0)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A new shape group for a shape tool `kind` of `size`, centred on the group's origin, painted
|
||||
/// with `paint` ("Rectangle 1", "Ellipse 1" or "Polystar 1"). None for another kind.
|
||||
pub(crate) fn shape_group(ids: &mut Ids, kind: &str, size: [f64; 2], paint: &ShapeTool) -> Option<PropGroup> {
|
||||
let (path, gname) = match kind {
|
||||
"rect" | "rectangle" => (build::shape_rect(ids, size, [0.0, 0.0], 0.0), "Rectangle 1"),
|
||||
"rounded" | "roundedRect" => (build::shape_rect(ids, size, [0.0, 0.0], size[0].min(size[1]) * 0.15), "Rectangle 1"),
|
||||
"rounded" | "roundedRect" => (build::shape_rect(ids, size, [0.0, 0.0], roundness(size)), "Rectangle 1"),
|
||||
"ellipse" => (build::shape_ellipse(ids, size, [0.0, 0.0]), "Ellipse 1"),
|
||||
"star" => (build::shape_star(ids, true, 5.0, [0.0, 0.0], size[0] / 2.0, size[0] / 4.0), "Polystar 1"),
|
||||
"polygon" => (build::shape_star(ids, false, 6.0, [0.0, 0.0], size[0] / 2.0, 0.0), "Polystar 1"),
|
||||
_ => return None,
|
||||
k => {
|
||||
let (star, points, outer, inner) = polystar_of(k, size)?;
|
||||
(build::shape_star(ids, star, points, [0.0, 0.0], outer, inner), "Polystar 1")
|
||||
}
|
||||
};
|
||||
let mut items = vec![path];
|
||||
if let Some((c, w)) = stroke {
|
||||
items.push(build::shape_stroke(ids, c, w));
|
||||
}
|
||||
if let Some(c) = fill {
|
||||
items.push(build::shape_fill(ids, c));
|
||||
}
|
||||
items.extend(paint_items(ids, paint, [-size[0] / 2.0, -size[1] / 2.0, size[0], size[1]]));
|
||||
Some(build::shape_group(ids, gname, items))
|
||||
}
|
||||
|
||||
/// The mask a shape tool draws: `kind` of `size` centred at `centre` (layer space).
|
||||
pub(crate) fn mask_path(kind: &str, centre: [f64; 2], size: [f64; 2]) -> Option<ShapePath> {
|
||||
let bez = match kind {
|
||||
"rect" | "rectangle" => return Some(ShapePath::rect(centre, size[0], size[1])),
|
||||
"ellipse" => return Some(ShapePath::ellipse(centre, size[0], size[1])),
|
||||
"rounded" | "roundedRect" => effectcraft_path::rect(size, centre, roundness(size)),
|
||||
k => {
|
||||
let (star, points, outer, inner) = polystar_of(k, size)?;
|
||||
effectcraft_path::polystar(star, points, centre, 0.0, inner, outer, 0.0, 0.0)
|
||||
}
|
||||
};
|
||||
effectcraft_path::from_kurbo(&bez).into_iter().next()
|
||||
}
|
||||
|
||||
/// The shape layer a shape tool or the Pen draws into: `layer` (which must be a shape layer),
|
||||
/// else the first selected unlocked shape layer, else none (a new shape layer).
|
||||
pub(crate) fn draw_target(s: &Session, comp: &Comp, p: &Value, cmd: &str) -> Result<Option<LayerId>> {
|
||||
@@ -93,10 +276,11 @@ fn pair_p(p: &Value, k: &str) -> Option<[f64; 2]> {
|
||||
}
|
||||
|
||||
/// The shape tools: a rectangle, rounded rectangle, ellipse, polygon or star of `size`, centred
|
||||
/// at `position`. With a shape layer given or selected it goes on top of that layer's Contents
|
||||
/// as a new group ("Rectangle 2"…) placed by the group's Transform, as After Effects draws into
|
||||
/// the selected shape layer; otherwise a new shape layer is centred on it. `position` and `size`
|
||||
/// are in comp pixels, or the layer's space with `space: "layer"`.
|
||||
/// at `position`, painted with the Tools bar's Fill and Stroke unless given. With a shape layer
|
||||
/// given or selected it goes on top of that layer's Contents as a new group ("Rectangle 2"…)
|
||||
/// placed by the group's Transform, as After Effects draws into the selected shape layer;
|
||||
/// otherwise a new shape layer is centred on it. `position` and `size` are in comp pixels, or
|
||||
/// the layer's space with `space: "layer"`.
|
||||
fn new_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let c = "shape.newShape";
|
||||
let cid = comp_id(s, p)?;
|
||||
@@ -110,8 +294,7 @@ fn new_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Some(_) => pair_p(p, "size").filter(|s| s.iter().all(|v| *v >= 0.0)).ok_or_else(|| bad(c, "size: [w, h], not negative"))?,
|
||||
};
|
||||
let pos = position_p(p, c)?.unwrap_or([comp.width as f64 / 2.0, comp.height as f64 / 2.0]);
|
||||
let fill = Some(c4(color_p(p, "fill"), [0.25, 0.55, 1.0, 1.0]));
|
||||
let stroke = (f_p(p, "strokeWidth").unwrap_or(0.0) > 0.0).then(|| (c4(color_p(p, "stroke"), [1.0, 1.0, 1.0, 1.0]), f_p(p, "strokeWidth").unwrap_or(2.0)));
|
||||
let paint = paint_p(p, &s.state.shape_tool, c)?;
|
||||
let target = draw_target(s, &comp, p, c)?;
|
||||
// In the target layer's space: the centre maps through the layer's transform, the size by
|
||||
// its scale.
|
||||
@@ -139,7 +322,7 @@ fn new_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
None => new_shape_layer(proj, st, &comp, cid, name, Some(pos))?,
|
||||
};
|
||||
let mut next = proj.next_id;
|
||||
let g = shape_group(&mut Ids(&mut next), kind, size, fill, stroke).ok_or_else(|| bad(c, "unknown kind"))?;
|
||||
let g = shape_group(&mut Ids(&mut next), kind, size, &paint).ok_or_else(|| bad(c, "unknown kind"))?;
|
||||
proj.next_id = next;
|
||||
let group = add_to_contents(proj, cid, lid, g, centre, c)?;
|
||||
st.selected_layers = vec![lid];
|
||||
@@ -148,14 +331,38 @@ fn new_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(json!({"layer": lid.0, "group": group}))
|
||||
}
|
||||
|
||||
/// The Tools bar's options for the shape tools and the Pen: Tool Creates Shape / Tool Creates
|
||||
/// Mask (`createsMask`) and the Fill and Stroke new shapes get (the keys of [`paint_p`]);
|
||||
/// `reset` restores the defaults first. Reports the options.
|
||||
fn tool_options(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let base = if b_p(p, "reset") == Some(true) { ShapeTool::default() } else { s.state.shape_tool.clone() };
|
||||
let mut o = paint_p(p, &base, "shape.toolOptions")?;
|
||||
if let Some(m) = b_p(p, "createsMask") {
|
||||
o.creates_mask = m;
|
||||
}
|
||||
s.state.shape_tool = o;
|
||||
Ok(serde_json::to_value(&s.state.shape_tool).unwrap_or(Value::Null))
|
||||
}
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
vec![cmd!(
|
||||
"shape.newShape",
|
||||
"Shape Tool",
|
||||
[],
|
||||
None,
|
||||
"{layer?, kind?: rect|rounded|ellipse|polygon|star, size? [w,h], position? [x,y] (the centre), space?: comp|layer, name? (a new layer's), fill?, stroke?, strokeWidth?} → {layer, group}",
|
||||
has_comp,
|
||||
new_shape
|
||||
)]
|
||||
vec![
|
||||
cmd!(
|
||||
"shape.newShape",
|
||||
"Shape Tool",
|
||||
[],
|
||||
None,
|
||||
"{layer?, kind?: rect|rounded|ellipse|polygon|star, size? [w,h], position? [x,y] (the centre), space?: comp|layer, name? (a new layer's), fill?, fillType?, fillBlend?, fillOpacity?, stroke?, strokeType?, strokeBlend?, strokeOpacity?, strokeWidth? (default the Tools bar's, shape.toolOptions)} → {layer, group}",
|
||||
has_comp,
|
||||
new_shape
|
||||
),
|
||||
cmd!(
|
||||
"shape.toolOptions",
|
||||
"Shape Tool Options",
|
||||
[],
|
||||
None,
|
||||
"{createsMask?: bool (Tool Creates Mask with a shape layer selected), fill?: [r,g,b]|#hex|false, fillType?: none|solid|linear|radial, fillBlend?: blend mode, fillOpacity? %, stroke?, strokeType?, strokeBlend?, strokeOpacity?, strokeWidth? px, reset?} → the options",
|
||||
always,
|
||||
tool_options
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
@@ -199,20 +199,10 @@ fn pts(v: Option<&Value>) -> Vec<[f64; 2]> {
|
||||
v.and_then(Value::as_array).map(|a| a.iter().filter_map(|p| Some([p.get(0)?.as_f64()?, p.get(1)?.as_f64()?])).collect()).unwrap_or_default()
|
||||
}
|
||||
|
||||
fn rgba(v: Option<&Value>, d: [f64; 4]) -> [f64; 4] {
|
||||
match v {
|
||||
Some(Value::Array(a)) => {
|
||||
let g = |i: usize, d: f64| a.get(i).and_then(Value::as_f64).unwrap_or(d);
|
||||
[g(0, d[0]), g(1, d[1]), g(2, d[2]), g(3, 1.0)]
|
||||
}
|
||||
Some(Value::String(h)) => effectcraft_color::Rgba::from_hex(h).map(|c| [c.r as f64, c.g as f64, c.b as f64, 1.0]).unwrap_or(d),
|
||||
_ => d,
|
||||
}
|
||||
}
|
||||
|
||||
/// Pen tool on a shape layer: a new Shape group ("Shape n" with Path 1, Stroke 1 and Fill 1) on
|
||||
/// the given / selected shape layer, or on a new shape layer when none is given. Vertices are
|
||||
/// in the layer's space, or comp space (`space: "comp"`, the default for a new layer).
|
||||
/// Pen tool on a shape layer: a new Shape group ("Shape n" with Path 1 and the Tools bar's
|
||||
/// Stroke and Fill unless given) on the given / selected shape layer, or on a new shape layer
|
||||
/// when none is given. Vertices are in the layer's space, or comp space (`space: "comp"`, the
|
||||
/// default for a new layer).
|
||||
fn new_path(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cid = comp_id(s, p)?;
|
||||
let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?.clone();
|
||||
@@ -246,9 +236,11 @@ fn new_path(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
}
|
||||
let path = ShapePath { vertices: v, in_tangents: ins, out_tangents: outs, closed: b_p(p, "closed").unwrap_or(false), feather: Vec::new() };
|
||||
let fill = (!matches!(p.get("fill"), Some(Value::Null) | Some(Value::Bool(false)))).then(|| rgba(p.get("fill"), [0.25, 0.55, 1.0, 1.0]));
|
||||
let width = f_p(p, "strokeWidth").unwrap_or(2.0);
|
||||
let stroke = (width > 0.0).then(|| rgba(p.get("stroke"), [1.0, 1.0, 1.0, 1.0]));
|
||||
let paint = super::shape_tool::paint_p(p, &s.state.shape_tool, "shape.newPath")?;
|
||||
// Gradients run across the path's points.
|
||||
let (lo, hi) =
|
||||
path.vertices.iter().fold(([f64::MAX; 2], [f64::MIN; 2]), |(lo, hi), v| ([lo[0].min(v[0]), lo[1].min(v[1])], [hi[0].max(v[0]), hi[1].max(v[1])]));
|
||||
let bounds = [lo[0], lo[1], hi[0] - lo[0], hi[1] - lo[1]];
|
||||
let name = str_p(p, "name").map(str::to_string);
|
||||
let (lid, group, path_uid) = s.edit("Pen Tool", None, |proj, st| {
|
||||
let lid = match target {
|
||||
@@ -260,12 +252,7 @@ fn new_path(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let pg = build::shape_path(&mut ids, path);
|
||||
let path_uid = pg.uid;
|
||||
let mut items = vec![pg];
|
||||
if let Some(c) = stroke {
|
||||
items.push(build::shape_stroke(&mut ids, c, width));
|
||||
}
|
||||
if let Some(c) = fill {
|
||||
items.push(build::shape_fill(&mut ids, c));
|
||||
}
|
||||
items.extend(super::shape_tool::paint_items(&mut ids, &paint, bounds));
|
||||
let g = build::shape_group(&mut ids, "Shape 1", items);
|
||||
proj.next_id = next;
|
||||
let group = super::shape_tool::add_to_contents(proj, cid, lid, g, [0.0, 0.0], "shape.newPath")?;
|
||||
@@ -500,7 +487,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Pen Tool (Shape Path)",
|
||||
[],
|
||||
None,
|
||||
"{layer?, vertices: [[x,y]…], inTangents?, outTangents?, closed?, space?: comp|layer, fill?: [r,g,b]|#hex|false, stroke?, strokeWidth?, name?}",
|
||||
"{layer?, vertices: [[x,y]…], inTangents?, outTangents?, closed?, space?: comp|layer, fill?: [r,g,b]|#hex|false, fillType?, fillBlend?, fillOpacity?, stroke?, strokeType?, strokeBlend?, strokeOpacity?, strokeWidth? (default the Tools bar's, shape.toolOptions), name?}",
|
||||
shape_layer_or_none,
|
||||
new_path
|
||||
),
|
||||
|
||||
@@ -238,6 +238,9 @@ pub struct EditorState {
|
||||
/// Puppet tool options for new meshes.
|
||||
#[serde(default)]
|
||||
pub puppet: commands::puppet::PuppetOptions,
|
||||
/// The shape tools' and the Pen's options (Tool Creates Shape / Mask, Fill and Stroke).
|
||||
#[serde(default)]
|
||||
pub shape_tool: commands::shape_tool::ShapeTool,
|
||||
/// View ▸ Switch View Layout: 1, 2 or 4 views side by side in the Composition viewer.
|
||||
#[serde(default = "one_view")]
|
||||
pub view_layout: u8,
|
||||
|
||||
@@ -223,7 +223,7 @@ fn shape_pen_builds_shape_group_and_renders() {
|
||||
assert_eq!(img.get(200, 170), [0.0, 1.0, 0.0, 1.0]);
|
||||
assert_eq!(img.get(50, 50)[3], 0.0);
|
||||
// With the shape layer selected the Pen adds a second group (with a stroke) to it.
|
||||
let r2 = s.execute("shape.newPath", json!({"vertices": [[0, 0], [10, 10]], "space": "layer"})).unwrap();
|
||||
let r2 = s.execute("shape.newPath", json!({"vertices": [[0, 0], [10, 10]], "space": "layer", "strokeWidth": 2})).unwrap();
|
||||
assert_eq!(r2["layer"].as_u64(), Some(lid));
|
||||
let l = layer(&s, lid);
|
||||
let names: Vec<String> = l.props.sub("contents").unwrap().groups().map(|g| g.name.clone()).collect();
|
||||
@@ -282,6 +282,69 @@ fn shape_tools_draw_into_the_selected_shape_layer() {
|
||||
assert!(s.execute("shape.newShape", json!({"kind": "blob"})).is_err());
|
||||
}
|
||||
|
||||
/// The Tools bar's Fill / Stroke Options (None, Solid Color, Linear and Radial Gradient, blend
|
||||
/// mode, opacity) and Stroke Width paint new shapes and Pen paths; a command's own paint
|
||||
/// parameters win (#227).
|
||||
#[test]
|
||||
fn shape_tool_options_paint_new_shapes() {
|
||||
use crate::commands::shape_tool::ShapeTool;
|
||||
let mut s = comp();
|
||||
let o = s.execute("shape.toolOptions", json!({})).unwrap();
|
||||
assert_eq!((o["createsMask"].clone(), o["fill"]["kind"].clone(), o["strokeWidth"].clone()), (json!(false), json!("solid"), json!(0.0)));
|
||||
// A radial gradient fill at 50% in Multiply and a red 4 px stroke.
|
||||
let o = s
|
||||
.execute("shape.toolOptions", json!({"fillType": "Radial Gradient", "fillBlend": "multiply", "fillOpacity": 50, "stroke": [1, 0, 0], "strokeWidth": 4}))
|
||||
.unwrap();
|
||||
assert_eq!(o["fill"]["kind"], json!("radial"));
|
||||
let r = s.execute("shape.newShape", json!({"kind": "ellipse", "size": [100, 60]})).unwrap();
|
||||
let lid = r["layer"].as_u64().unwrap();
|
||||
let items = |s: &Session, g: u64| layer(s, lid).props.find_group(g).unwrap().sub("contents").unwrap().clone();
|
||||
let g = items(&s, r["group"].as_u64().unwrap());
|
||||
assert_eq!(g.groups().map(|x| x.match_id.as_str()).collect::<Vec<_>>(), ["ellipse", "stroke", "gfill"]);
|
||||
let at = |g: &effectcraft_project::PropGroup, item: &str, prop: &str| g.groups().find(|x| x.match_id == item).unwrap().get(prop).unwrap().value.clone();
|
||||
let multiply = effectcraft_color::BlendMode::ALL.iter().position(|m| *m == effectcraft_color::BlendMode::Multiply).unwrap() as u32;
|
||||
assert_eq!(at(&g, "gfill", "type"), KV::Enum(1));
|
||||
assert_eq!(at(&g, "gfill", "blend"), KV::Enum(multiply));
|
||||
assert_eq!(at(&g, "gfill", "opacity").as_f64(), 50.0);
|
||||
assert_eq!(at(&g, "gfill", "end").components(), [50.0, 0.0], "out from the centre");
|
||||
assert_eq!(at(&g, "stroke", "color").components(), [1.0, 0.0, 0.0, 1.0]);
|
||||
assert_eq!(at(&g, "stroke", "width").as_f64(), 4.0);
|
||||
// No fill and a linear gradient stroke, across a Pen path's points.
|
||||
s.execute("shape.toolOptions", json!({"fill": false, "strokeType": "linear"})).unwrap();
|
||||
let r = s.execute("shape.newPath", json!({"layer": lid, "vertices": [[10, 20], [110, 40]], "space": "layer"})).unwrap();
|
||||
let g = items(&s, r["group"].as_u64().unwrap());
|
||||
assert_eq!(g.groups().map(|x| x.match_id.as_str()).collect::<Vec<_>>(), ["path", "gstroke"]);
|
||||
assert_eq!((at(&g, "gstroke", "start").components(), at(&g, "gstroke", "end").components()), (vec![10.0, 30.0], vec![110.0, 30.0]));
|
||||
// A command's own paint wins over the Tools bar's.
|
||||
let r = s.execute("shape.newShape", json!({"kind": "rect", "fill": "#00ff00", "strokeWidth": 0})).unwrap();
|
||||
let g = items(&s, r["group"].as_u64().unwrap());
|
||||
assert_eq!(g.groups().map(|x| x.match_id.as_str()).collect::<Vec<_>>(), ["rect", "fill"]);
|
||||
assert_eq!(at(&g, "fill", "color").components(), [0.0, 1.0, 0.0, 1.0]);
|
||||
// Bad values are errors that leave the options as they were; reset restores the defaults.
|
||||
let before = s.state.shape_tool.clone();
|
||||
for bad in [json!({"fillType": "plaid"}), json!({"strokeBlend": "nope"}), json!({"fill": true}), json!({"strokeWidth": "wide"})] {
|
||||
assert!(s.execute("shape.toolOptions", bad.clone()).is_err(), "{bad}");
|
||||
}
|
||||
assert_eq!(s.state.shape_tool, before);
|
||||
s.execute("shape.toolOptions", json!({"reset": true, "createsMask": true})).unwrap();
|
||||
assert_eq!(s.state.shape_tool, ShapeTool { creates_mask: true, ..ShapeTool::default() });
|
||||
}
|
||||
|
||||
/// Tool Creates Mask: every shape tool draws its mask (#227).
|
||||
#[test]
|
||||
fn masks_of_every_shape_tool_kind() {
|
||||
let mut s = comp();
|
||||
let sol = s.execute("layer.newSolid", json!({"color": "#ffffff"})).unwrap()["layer"].as_u64().unwrap();
|
||||
for (kind, n) in [("rect", 4), ("ellipse", 4), ("rounded", 8), ("polygon", 6), ("star", 10)] {
|
||||
let m = s.execute("layer.addMask", json!({"layer": sol, "shape": kind, "rect": [100, 100, 80, 80]})).unwrap()["mask"].as_u64().unwrap();
|
||||
let path = layer(&s, sol).props.find_group(m).unwrap().get("path").unwrap().value.as_path().unwrap().clone();
|
||||
assert!(path.closed, "{kind}");
|
||||
assert!(if kind == "rounded" { path.vertices.len() >= n } else { path.vertices.len() == n }, "{kind}: {}", path.vertices.len());
|
||||
assert!(path.vertices.iter().all(|v| (100.0..=180.0).contains(&v[0]) && (100.0..=180.0).contains(&v[1])), "{kind}: inside the box");
|
||||
}
|
||||
assert!(s.execute("layer.addMask", json!({"layer": sol, "shape": "blob"})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vertex_add_delete_convert_on_shape_paths_and_masks_undo() {
|
||||
let mut s = comp();
|
||||
|
||||
Reference in New Issue
Block a user