mirror of
https://github.com/storytold/AniCraft.git
synced 2026-12-01 02:24:23 +01:00
fix(engine): review fixes on the merged outside PRs (M8)
camera.set's held-frame note can't overflow and is in the command's text (#674); library.new_symbol answers {symbol} like its siblings (#668); Optimize leaves an outline far past the fitter's limit alone, and Straighten keeps a thin closed fill instead of flattening it to nothing (#623).
This commit is contained in:
1 parent
584d0f9115
commit
baba8fa92c
5 files changed
+41
-8
No files matched your search
@@ -24,7 +24,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Camera",
|
||||
"",
|
||||
None,
|
||||
"{x?, y? (camera centre, stage px), dx?, dy? (pan by), zoom? (%, 5–2000), zoom_by? (factor), rotation? (°), rotate_by? (°), reset?: bool, frame?, and the colour effect as element.set_color_effect takes it: style?: none|brightness|tint|advanced, brightness? (%), tint? (#rrggbb), tint_amount? (%), red_multiplier? … blue_offset? (the camera has no opacity: no alpha values)}",
|
||||
"{x?, y? (camera centre, stage px), dx?, dy? (pan by), zoom? (%, 5–2000), zoom_by? (factor), rotation? (°), rotate_by? (°), reset?: bool, frame?, and the colour effect as element.set_color_effect takes it: style?: none|brightness|tint|advanced, brightness? (%), tint? (#rrggbb), tint_amount? (%), red_multiplier? … blue_offset? (the camera has no opacity: no alpha values)}. On a frame a keyframe holds (no keyframe of its own, no tween) the edit lands on that keyframe, as an edit on the stage does, and the result says so: {keyframe (its frame), note}; insert a keyframe first to key the camera at the frame",
|
||||
true,
|
||||
set,
|
||||
),
|
||||
@@ -141,7 +141,7 @@ fn set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let stage = s.doc.stage;
|
||||
// On a held frame (inside a keyframe's span, no tween) the edit lands on that keyframe, as
|
||||
// any edit does: say so, so a caller keying the camera over time sees it was not keyed here.
|
||||
let held = s.timeline().layer(id).and_then(|l| l.keyframe_at(frame)).filter(|k| k.start != frame && k.tween.is_none() && k.motion.is_none()).map(|k| (k.start, k.start + k.duration.max(1) - 1));
|
||||
let held = s.timeline().layer(id).and_then(|l| l.keyframe_at(frame)).filter(|k| k.start != frame && k.tween.is_none() && k.motion.is_none()).map(|k| (k.start, k.end().saturating_sub(1)));
|
||||
// A camera is created on a keyframe that has none (a blank camera keyframe is at rest).
|
||||
// (Its id is taken only when it is used, so a no-op leaves the document unchanged.)
|
||||
let fresh = ElementId(s.doc.next_id.max(1));
|
||||
|
||||
@@ -22,7 +22,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"New Symbol…",
|
||||
"Insert",
|
||||
Some("Ctrl+F8"),
|
||||
"{name? (default: the next free “Symbol n”), kind?: graphic|movie_clip|button (default: graphic), folder?: Library folder id (default: the top level), edit?: bool (default: true: open the new symbol for editing)} → {id, name, kind}: creates an empty symbol in the Library without placing it on the stage",
|
||||
"{name? (default: the next free “Symbol n”), kind?: graphic|movie_clip|button (default: graphic), folder?: Library folder id (default: the top level), edit?: bool (default: true: open the new symbol for editing)} → {symbol, name, kind}: creates an empty symbol in the Library without placing it on the stage",
|
||||
true,
|
||||
new_symbol,
|
||||
),
|
||||
@@ -202,7 +202,7 @@ fn new_symbol(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if edit {
|
||||
crate::editing::edit(s, &json!({"symbol": id.0}))?;
|
||||
}
|
||||
Ok(json!({"id": id.0, "name": name, "kind": kind_key(kind)}))
|
||||
Ok(json!({"symbol": id.0, "name": name, "kind": kind_key(kind)}))
|
||||
}
|
||||
|
||||
fn new_folder(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
@@ -275,7 +275,7 @@ fn new_symbol_creates_an_empty_symbol_enters_it_and_undoes_in_one_step() {
|
||||
let before_stage = s.elements_on(s.layer).len();
|
||||
// Explicit name and kind; edit defaults to true and enters the new empty symbol.
|
||||
let v = run(&mut s, "library.new_symbol", json!({"name": "Hero", "kind": "movie_clip"}));
|
||||
let id = SymbolId(v["id"].as_u64().unwrap());
|
||||
let id = SymbolId(v["symbol"].as_u64().unwrap());
|
||||
assert_eq!((v["name"].as_str(), v["kind"].as_str()), (Some("Hero"), Some("movie_clip")));
|
||||
assert_eq!(s.doc.library.len(), before_lib + 1);
|
||||
assert_eq!(s.editing_symbol(), Some(id), "edit defaults to true");
|
||||
@@ -306,7 +306,7 @@ fn new_symbol_creates_an_empty_symbol_enters_it_and_undoes_in_one_step() {
|
||||
// Folder placement works and duplicates are checked per folder (case aside).
|
||||
let f = folder(&run(&mut s, "library.new_folder", json!({"name": "Cast"})));
|
||||
let in_folder = run(&mut s, "library.new_symbol", json!({"name": "Hero", "folder": f, "edit": false}));
|
||||
assert_eq!(path(&s, in_folder["id"].as_u64().unwrap()), "Cast/Hero");
|
||||
assert_eq!(path(&s, in_folder["symbol"].as_u64().unwrap()), "Cast/Hero");
|
||||
// Refusals: duplicate name (case-insensitive), empty/whitespace, slash, unknown kind, missing folder.
|
||||
let lib_before_err = s.doc.library.len();
|
||||
assert!(err(&mut s, "library.new_symbol", json!({"name": "hero"})).contains("already has"));
|
||||
|
||||
@@ -433,7 +433,7 @@ fn set_on_a_held_frame_reports_the_keyframe_it_edited() {
|
||||
// Frame 10 is held by the camera's one keyframe (frames 1–20): the edit lands on that keyframe.
|
||||
let v = run(&mut s, "camera.set", json!({"zoom": 200, "frame": 10}));
|
||||
assert_eq!(v["keyframe"], 0, "{v}");
|
||||
assert!(v["note"].as_str().is_some_and(|n| n.contains("frame 11") && n.contains("frame 1 ")), "{v}");
|
||||
assert!(v["note"].as_str().is_some_and(|n| !n.is_empty()), "{v}");
|
||||
assert_eq!(s.doc.timeline(0).layers[0].keyframes.len(), 1, "no keyframe was added");
|
||||
let zoom = |s: &mut Session, f: u32| run(s, "camera.info", json!({"frame": f}))["zoom"].as_f64().unwrap();
|
||||
assert!((zoom(&mut s, 0) - zoom(&mut s, 19)).abs() < 1e-6, "the whole span changed together");
|
||||
|
||||
@@ -290,6 +290,11 @@ fn optimize_subpath(sub: &Subpath, tolerance: f32) -> Subpath {
|
||||
if pts.len() < 2 {
|
||||
return Subpath { start: sub.start, segs: sub.segs.clone(), closed: sub.closed };
|
||||
}
|
||||
// Far past what the fitter takes: thinning that many points first is itself slow (the
|
||||
// simplifier is quadratic on a zigzag), so such an outline stays as it is.
|
||||
if pts.len() > MAX_SUBPATH_POINTS.saturating_mul(4) {
|
||||
return Subpath { start: sub.start, segs: sub.segs.clone(), closed: sub.closed };
|
||||
}
|
||||
// Cap points fed to the least-squares fitter so hostile paths stay bounded.
|
||||
if pts.len() > MAX_SUBPATH_POINTS {
|
||||
pts = simplify(&pts, (tolerance * 0.05).max(1e-3));
|
||||
@@ -581,7 +586,16 @@ fn straighten_subpath(sub: &Subpath, s: f32) -> Subpath {
|
||||
let start = segs.first().map_or(sub.start, |seg| match *seg {
|
||||
Seg::Line(a, _) | Seg::Cubic([a, ..]) => a,
|
||||
});
|
||||
Subpath { start, segs, closed: sub.closed }
|
||||
let cand = Subpath { start, segs, closed: sub.closed };
|
||||
// A thin closed fill (a leaf of two shallow curves) would lose its area to its chords and
|
||||
// vanish from the stage: like Smooth, Straighten leaves such an outline as it is.
|
||||
if sub.closed {
|
||||
let orig_area = sub.enclosed_area();
|
||||
if orig_area > AREA_EPS && cand.enclosed_area() < orig_area * MIN_SMOOTH_AREA_RATIO {
|
||||
return Subpath { start: sub.start, segs: sub.segs.clone(), closed: true };
|
||||
}
|
||||
}
|
||||
cand
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -896,4 +910,23 @@ mod tests {
|
||||
}
|
||||
assert!(b.width() > 0.5 && b.height() > 0.5, "loop stays visible: {b:?}");
|
||||
}
|
||||
|
||||
/// A thin leaf, two shallow curves between the same two points, is within Straighten's
|
||||
/// reach of its chords on both sides: straightened it would be a line there and back, a
|
||||
/// fill with no area, gone from the stage.
|
||||
#[test]
|
||||
fn straighten_keeps_a_thin_closed_fill() {
|
||||
let mut leaf = Path::new();
|
||||
leaf.move_to(Vec2::new(0.0, 0.0));
|
||||
leaf.cubic_to(Vec2::new(30.0, -8.0), Vec2::new(70.0, -8.0), Vec2::new(100.0, 0.0));
|
||||
leaf.cubic_to(Vec2::new(70.0, 8.0), Vec2::new(30.0, 8.0), Vec2::new(0.0, 0.0));
|
||||
leaf.close();
|
||||
let area = |p: &Path| extract_subpaths(p).iter().map(Subpath::enclosed_area).sum::<f64>();
|
||||
let before = area(&leaf);
|
||||
assert!(before > 100.0, "the leaf has an area: {before}");
|
||||
for strength in [0.25, 0.5, 1.0] {
|
||||
let after = area(&straighten(&leaf, strength));
|
||||
assert!(after > before * 0.25, "strength {strength}: {before} became {after}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user