mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-24 23:50:13 +00:00
3D: a replay draws the custom bevel it recorded, and a mixed selection takes the whole bevel
effect.apply and effect.setParams given bevel {custom: name} record the
bevel's points in their journal entry as savedBevel, and savedBevel given
with the name is used in place of the bevels file's points, so a replay
draws the same bevel after the file changes or drops the name.
The 3D and Materials panel sends a custom bevel's name and points with
bevel whenever any of the three changes, so an Extrude & Bevel in a mixed
selection that had no custom bevel takes the whole bevel.
This commit is contained in:
@@ -277,19 +277,29 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
/// The command parameter that holds the points a custom bevel given by name had when the command
|
||||
/// ran. The journal records it, so a replay draws the same bevel after the bevels file changes or
|
||||
/// loses that name.
|
||||
pub const SAVED_BEVEL: &str = "savedBevel";
|
||||
|
||||
/// `params` of an effect with `bevel` given as `{custom: name}`: the bevel of the bevels file named
|
||||
/// so, stored as `bevel` `"custom"` with its `bevelName` and `bevelProfile`. Other values are left
|
||||
/// for the effect to check.
|
||||
pub(crate) fn resolve_custom(s: &Session, params: &mut Value, cmd: &str) -> Result<()> {
|
||||
/// so, stored as `bevel` `"custom"` with its `bevelName` and `bevelProfile`. The command's
|
||||
/// [`SAVED_BEVEL`] points (`p`), when given, are used in place of the file's, and the points used
|
||||
/// join the journal entry as [`SAVED_BEVEL`]. Other values are left for the effect to check.
|
||||
pub(crate) fn resolve_custom(s: &mut Session, p: &Value, params: &mut Value, cmd: &str) -> Result<()> {
|
||||
let Some(Value::Object(o)) = params.get("bevel") else { return Ok(()) };
|
||||
let name = match (o.len(), o.get("custom")) {
|
||||
(1, Some(Value::String(name))) => name.clone(),
|
||||
_ => return Err(bad(cmd, "bevel: give a shape id, or {custom: name} for a bevel of the bevels file (threeD.bevels.list)")),
|
||||
};
|
||||
let bevel = s.bevels.get(&name).ok_or_else(|| bad(cmd, format!("no custom bevel {name} (see threeD.bevels.list)")))?;
|
||||
let points = match p.get(SAVED_BEVEL).filter(|v| !v.is_null()) {
|
||||
Some(saved) => saved.clone(),
|
||||
None => s.bevels.get(&name).ok_or_else(|| bad(cmd, format!("no custom bevel {name} (see threeD.bevels.list)")))?.profile_json(),
|
||||
};
|
||||
s.note_journal(SAVED_BEVEL, points.clone());
|
||||
params["bevel"] = json!("custom");
|
||||
params[BEVEL_NAME] = json!(bevel.name);
|
||||
params[BEVEL_PROFILE] = bevel.profile_json();
|
||||
params[BEVEL_NAME] = json!(name);
|
||||
params[BEVEL_PROFILE] = points;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ fn effect_specs() -> Vec<CommandSpec> {
|
||||
"Apply Effect",
|
||||
[],
|
||||
None,
|
||||
"{effect: id (see effect.list, e.g. \"stylize.dropShadow\", \"distort.roughen\", \"warp.arc\", or \"plugin.<id>\" for an installed effect plug-in), params?: {…} (missing keys take the dialog defaults), item?: appearance item index|null (apply to that fill/stroke only; omitted: the Appearance panel's active item, else the whole object), ids?|id?: [..] (layers too), target?: \"object\"|\"contents\" (contents: the objects inside groups and layers)} append a live effect to each selected object's appearance (a group's or layer's apply to its members as one piece: one combined shadow; a Pathfinder effect, which combines a group's contents, groups several loose selected objects first, in the same undo step) → {ids, index, item, grouped?}",
|
||||
"{effect: id (see effect.list, e.g. \"stylize.dropShadow\", \"distort.roughen\", \"warp.arc\", or \"plugin.<id>\" for an installed effect plug-in), params?: {…} (missing keys take the dialog defaults), item?: appearance item index|null (apply to that fill/stroke only; omitted: the Appearance panel's active item, else the whole object), ids?|id?: [..] (layers too), target?: \"object\"|\"contents\" (contents: the objects inside groups and layers), savedBevel?: [[x, y]…] (with params bevel {custom: name}: the custom bevel's points, used in place of the bevels file's; the journal records them so a replay draws the same bevel after the file changes)} append a live effect to each selected object's appearance (a group's or layer's apply to its members as one piece: one combined shadow; a Pathfinder effect, which combines a group's contents, groups several loose selected objects first, in the same undo step) → {ids, index, item, grouped?}",
|
||||
has_doc,
|
||||
apply
|
||||
),
|
||||
@@ -76,7 +76,7 @@ fn effect_specs() -> Vec<CommandSpec> {
|
||||
"Effect Options",
|
||||
[],
|
||||
None,
|
||||
"{index: int, params?: {…} (merged into the current parameters), warpStyle?: string (an existing Warp style suffix, e.g. arc or flag; changes only an applied Warp in place), threeDType?: plane|extrude|revolve|inflate (the 3D and Materials panel's object type: changes only an applied 3D effect in place into Rotate, Extrude & Bevel, Revolve or Inflate; the settings the new effect also takes keep their values, rotation angles wrapped into -180..180 outside Revolve, and the others start at its defaults; params then merge into the new effect's; the type the effect already has, with nothing else given, changes nothing and records no undo step), visible?: bool, item?: appearance item index|null (as effect.remove), ids?|id?: [..], target?: \"object\"|\"contents\", aligned?: bool (index and item are rows of the first target's stack: each other target edits its equal one, as the Appearance panel does for a mixed selection)} → {ids}",
|
||||
"{index: int, params?: {…} (merged into the current parameters), warpStyle?: string (an existing Warp style suffix, e.g. arc or flag; changes only an applied Warp in place), threeDType?: plane|extrude|revolve|inflate (the 3D and Materials panel's object type: changes only an applied 3D effect in place into Rotate, Extrude & Bevel, Revolve or Inflate; the settings the new effect also takes keep their values, rotation angles wrapped into -180..180 outside Revolve, and the others start at its defaults; params then merge into the new effect's; the type the effect already has, with nothing else given, changes nothing and records no undo step), visible?: bool, item?: appearance item index|null (as effect.remove), ids?|id?: [..], target?: \"object\"|\"contents\", aligned?: bool (index and item are rows of the first target's stack: each other target edits its equal one, as the Appearance panel does for a mixed selection), savedBevel?: [[x, y]…] (as effect.apply)} → {ids}",
|
||||
has_doc,
|
||||
set_params
|
||||
),
|
||||
@@ -436,7 +436,7 @@ pub(crate) fn apply(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
// A custom bevel by name takes a copy of its points from the bevels file.
|
||||
if bevel_effect(id) {
|
||||
super::bevels::resolve_custom(s, &mut params, C)?;
|
||||
super::bevels::resolve_custom(s, p, &mut params, C)?;
|
||||
}
|
||||
// An SVG filter needs its <filter> element, and is named by its id.
|
||||
if id == effects::SVG_FILTER {
|
||||
@@ -733,7 +733,7 @@ fn set_params(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
load_texture(&mut params, C)?;
|
||||
}
|
||||
if edited.iter().any(|e| bevel_effect(e)) || kind == Some(effects::EXTRUDE) {
|
||||
super::bevels::resolve_custom(s, &mut params, C)?;
|
||||
super::bevels::resolve_custom(s, p, &mut params, C)?;
|
||||
}
|
||||
let targets = effect_targets(s, p, item, index, C)?;
|
||||
// The type each 3D effect already has, with nothing else to change, is no edit (and is not
|
||||
|
||||
@@ -190,6 +190,47 @@ fn the_bevels_file_is_made_opened_and_reloaded_when_saved() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// `effect.apply` and `effect.setParams` given a custom bevel by name record its points as
|
||||
/// `savedBevel`, so a replay draws that bevel after the bevels file no longer has it.
|
||||
#[test]
|
||||
fn a_replay_draws_the_custom_bevel_the_command_recorded() {
|
||||
let dir = folder("replay");
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
|
||||
let id = rect(&mut s, 20);
|
||||
s.attach_bevels_file(dir.join(bevels::FILE_NAME).to_string_lossy().to_string());
|
||||
s.execute("threeD.bevels.edit", &json!({})).unwrap();
|
||||
s.execute("document.activate", &json!({"index": 0})).unwrap();
|
||||
let apply = json!({"effect": effects::EXTRUDE, "ids": [id.0], "item": null, "params": {"bevel": {"custom": "Example Bevel"}}});
|
||||
s.execute("effect.apply", &apply).unwrap();
|
||||
let applied = params(&s, id);
|
||||
let (cmd, recorded) = s.journal.last().unwrap().clone();
|
||||
assert_eq!(cmd, "effect.apply");
|
||||
assert_eq!(recorded[bevels::SAVED_BEVEL], applied[effects::BEVEL_PROFILE]);
|
||||
let set = json!({"ids": [id.0], "index": 0, "item": null, "params": {"bevel": {"custom": "Example Bevel"}, "bevelWidth": 9}});
|
||||
s.execute("effect.setParams", &set).unwrap();
|
||||
let edited = params(&s, id);
|
||||
let (_, set_recorded) = s.journal.last().unwrap().clone();
|
||||
assert_eq!(set_recorded[bevels::SAVED_BEVEL], applied[effects::BEVEL_PROFILE]);
|
||||
// The bevels file no longer has the bevel: by name it is refused, and the replays draw it.
|
||||
s.attach_bevels_file(dir.join("Other.vectorcraft").to_string_lossy().to_string());
|
||||
assert!(s.execute("effect.apply", &apply).is_err());
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(s.doc().unwrap().doc.node(id).unwrap().appearance.effects.is_empty());
|
||||
s.replay("effect.apply", &recorded).unwrap();
|
||||
assert_eq!(params(&s, id), applied);
|
||||
s.replay("effect.setParams", &set_recorded).unwrap();
|
||||
assert_eq!(params(&s, id), edited);
|
||||
// Points given with the name are used in place of the file's.
|
||||
let notch = json!([[0.0, 0.0], [0.2, 0.6], [0.7, 0.4], [1.0, 1.0]]);
|
||||
let mut given = set.clone();
|
||||
given["savedBevel"] = notch.clone();
|
||||
s.execute("effect.setParams", &given).unwrap();
|
||||
assert_eq!((¶ms(&s, id)[effects::BEVEL_PROFILE], ¶ms(&s, id)[effects::BEVEL_NAME]), (¬ch, &json!("Example Bevel")));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_document_keeps_its_custom_bevels_without_the_bevels_file() {
|
||||
let mut s = Session::new();
|
||||
|
||||
@@ -246,6 +246,33 @@ fn a_custom_bevel_sends_its_name_and_points() {
|
||||
assert_eq!((&p[vectorcraft_effects::BEVEL_NAME], &p[vectorcraft_effects::BEVEL_PROFILE]), (&json!("Steps"), &steps));
|
||||
}
|
||||
|
||||
/// With one Extrude & Bevel on a custom bevel and one without, picking that custom bevel sends its
|
||||
/// name and points too, though the form already held them, so both effects take it.
|
||||
#[test]
|
||||
fn a_custom_bevel_picked_for_a_mixed_selection_reaches_every_effect() {
|
||||
let mut app = app();
|
||||
let notch = json!([[0.0, 0.0], [0.2, 0.6], [0.7, 0.4], [1.0, 1.0]]);
|
||||
let custom = json!({"bevel": "custom", "bevelName": "Notch", "bevelProfile": notch});
|
||||
app.run("effect.apply", json!({"effect": "threeD.extrude", "item": null, "params": custom})).unwrap();
|
||||
let a = app.session.active().unwrap().selection.objects[0].0;
|
||||
let b = app.run("shape.rectangle", json!({"x": 250, "y": 100, "width": 100, "height": 100})).unwrap()["id"].as_u64().unwrap();
|
||||
app.run("effect.apply", json!({"effect": "threeD.extrude", "item": null})).unwrap();
|
||||
app.run("select.set", json!({"ids": [a, b]})).unwrap();
|
||||
let mut fields = target(&app).map(|t| vectorcraft_effects::merged_params(&t.effect, &t.params)).unwrap().as_object().cloned().unwrap();
|
||||
super::mark_mixed(&app, &mut fields);
|
||||
assert_eq!((&fields["bevel"], &fields[vectorcraft_effects::BEVEL_NAME]), (&Value::Null, &json!("Notch")));
|
||||
let before = Value::Object(fields.clone());
|
||||
fields.insert("bevel".into(), json!("custom"));
|
||||
let changed = changed_settings(&before, &Value::Object(fields));
|
||||
assert_eq!(Value::Object(changed.clone()), json!({"bevel": "custom", "bevelName": "Notch", "bevelProfile": notch}));
|
||||
send(&mut app, &egui::Context::default(), changed, false);
|
||||
let st = app.session.active().unwrap();
|
||||
for id in [a, b] {
|
||||
let p = &st.doc.node(vectorcraft_doc::NodeId(id)).unwrap().appearance.effects[0].params;
|
||||
assert_eq!((&p["bevel"], &p[vectorcraft_effects::BEVEL_PROFILE]), (&json!("custom"), ¬ch), "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_panel_finds_a_3d_effect_on_a_fill() {
|
||||
let mut app = app();
|
||||
|
||||
@@ -491,9 +491,21 @@ pub(crate) fn mark_mixed(app: &VectorcraftApp, fields: &mut Map<String, Value>)
|
||||
}
|
||||
|
||||
/// The settings whose values differ between `before` and `after`, with their values in `after`.
|
||||
/// A change to a custom bevel (`bevel` `"custom"`, its name or its points) sends all three, so an
|
||||
/// effect of a mixed selection that had none takes the whole bevel.
|
||||
pub(crate) fn changed_settings(before: &Value, after: &Value) -> Map<String, Value> {
|
||||
let Some(after) = after.as_object() else { return Map::new() };
|
||||
after.iter().filter(|(k, v)| before.get(k.as_str()) != Some(*v)).map(|(k, v)| (k.clone(), v.clone())).collect()
|
||||
let mut changed: Map<String, Value> =
|
||||
after.iter().filter(|(k, v)| before.get(k.as_str()) != Some(*v)).map(|(k, v)| (k.clone(), v.clone())).collect();
|
||||
let custom = ["bevel", BEVEL_NAME, BEVEL_PROFILE];
|
||||
if after.get("bevel").and_then(Value::as_str) == Some("custom") && custom.iter().any(|k| changed.contains_key(*k)) {
|
||||
for k in custom {
|
||||
if let Some(v) = after.get(k) {
|
||||
changed.insert(k.into(), v.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
changed
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
+3
-1
@@ -3529,7 +3529,9 @@ returns each bevel's `name` and `points`, the `file` path, the `skipped` symbols
|
||||
and store it as `bevel: "custom"` with `bevelName` and `bevelProfile`, a copy of the points (`[[x, y], …]`, 2 to 64
|
||||
shares 0..1 of the width and height from `[0, 0]` to `[1, 1]`), so the document draws the bevel without the file. An
|
||||
unknown name, `custom` without `bevelProfile`, points that do not run from `[0, 0]` to `[1, 1]` within 0..1, or a
|
||||
`bevelName` that is not a string is refused and the document is not changed. The help page
|
||||
`bevelName` that is not a string is refused and the document is not changed. The command's journal entry records the
|
||||
points as `savedBevel`, and `savedBevel` given with `{"custom": name}` is used in place of the file's points, so a
|
||||
replay draws the same bevel after the file changes or drops the name. The help page
|
||||
[Add custom bevel paths](https://helpx.adobe.com/illustrator/desktop/special-effects-styles/create-3d-graphics/add-custom-bevel-paths.html)
|
||||
describes custom bevels drawn as open paths and kept as symbols in a bevels document.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user