From 4848a49dbb02ab5fb360dab023d8c1aa220ba619 Mon Sep 17 00:00:00 2001 From: yusufmntm Date: Wed, 25 Nov 2026 03:45:28 +0300 Subject: [PATCH 1/3] feat(engine): Create Object button for raw shapes (#614) Properties shows a Create Object button when raw (merge-drawing) shapes are selected; it runs the new modify.create_object command, also in Modify > Combine Objects and the stage context menu. One raw shape becomes a drawing object in place; several are united as Union does. One undo step. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 5 ++ crates/engine/src/cmds_group.rs | 58 ++++++++++++++++++- crates/engine/src/group_tests.rs | 96 ++++++++++++++++++++++++++++++++ docs/guide/drawing.md | 9 ++- docs/guide/reference/commands.md | 4 +- docs/parity.md | 4 +- src/menus.rs | 1 + src/panels.rs | 5 ++ src/stage.rs | 2 +- 9 files changed, 175 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1350047..e523cd07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased +* **Create Object in Properties** (#614). With raw (merge-drawing) shapes selected, Properties + now shows a **Create Object** button (also **Modify › Combine Objects › Create Object**, + `modify.create_object`) that turns one or more selected raw shapes on the active layer—including + a partial marquee selection—into a drawing object in one undo step (or a group of the merged + pieces when they have several colours or keep lines, as Union does). * **Timeline and panel bugs from the tracker** (M8, backlog P-009 and P-010, ADR 0131). * **Several layers selected at once** (#512). **Ctrl**-click a layer's name to add it or take it out, **Shift**-click for every layer from the active one, or **Shift+Up** and diff --git a/crates/engine/src/cmds_group.rs b/crates/engine/src/cmds_group.rs index 40fec49e..8d301a31 100644 --- a/crates/engine/src/cmds_group.rs +++ b/crates/engine/src/cmds_group.rs @@ -51,6 +51,15 @@ pub fn specs() -> Vec { c("modify.intersect", "Intersect", "Modify/Combine Objects", None, "{} combine with op intersect", true, |s, _| combine(s, Op::Intersect)), c("modify.punch", "Punch", "Modify/Combine Objects", None, "{} combine with op punch", true, |s, _| combine(s, Op::Punch)), c("modify.crop", "Crop", "Modify/Combine Objects", None, "{} combine with op crop", true, |s, _| combine(s, Op::Crop)), + c( + "modify.create_object", + "Create Object", + "Modify/Combine Objects", + None, + "{} turns the selected raw shapes on the active layer into a drawing object (a group of the merged pieces when they have more than one colour or keep lines) → {created}", + true, + create_object, + ), ] } @@ -299,10 +308,14 @@ fn result_of(s: &mut Session, pieces: Vec>) -> Option { const TOO_LARGE: &str = "the shapes are too complex to combine at once"; -fn combine(s: &mut Session, op: Op) -> Result { +/// A keyframe's elements and the selected shapes among them, bottom to top, with their index. +type SelectedShapes = (Vec>, Vec<(usize, Arc)>); + +/// The selected shapes on the active layer, bottom to top, with their index in the keyframe. +fn selected_shapes(s: &mut Session, one_layer_err: &str) -> Result { let sel = s.selection.on(s.layer); if s.selected_layers().len() > 1 { - return Err(EngineError::State("Combine Objects works on one layer: select shapes on one layer".into())); + return Err(EngineError::State(one_layer_err.into())); } let els = s.editable_keyframe()?.elements.clone(); // The selected shapes, bottom to top, with their index. @@ -313,9 +326,48 @@ fn combine(s: &mut Session, op: Op) -> Result { _ => return Err(EngineError::State("Combine Objects works on shapes and drawing objects: break symbols, text and groups apart first (Ctrl+B)".into())), } } + Ok((els, shapes)) +} + +fn create_object(s: &mut Session, _: &Value) -> Result { + let (els, shapes) = selected_shapes(s, "Create Object works on one layer: select raw shapes on one layer")?; + if shapes.is_empty() { + return Err(EngineError::State("select raw shapes to turn into a drawing object".into())); + } + if !shapes.iter().any(|(_, e)| crate::merge::mergeable(e).is_some()) { + return Err(EngineError::State("the selection is drawing objects already: select raw shapes to turn into a drawing object".into())); + } + if shapes.len() == 1 { + let Some((i, e)) = shapes.into_iter().next() else { + return Err(EngineError::State("select raw shapes to turn into a drawing object".into())); + }; + let mut obj = Arc::unwrap_or_clone(e); + if let ElementKind::Shape(sh) = &mut obj.kind { + sh.merge = false; + } + let id = obj.id; + let mut out = els; + if let Some(slot) = out.get_mut(i) { + *slot = Arc::new(obj); + } + s.editable_keyframe()?.elements = out; + s.select_only([id]); + return Ok(json!({"created": [id.0]})); + } + let created = combine_shapes(s, Op::Union, els, shapes)?; + Ok(json!({"created": created.iter().map(|e| e.0).collect::>()})) +} + +fn combine(s: &mut Session, op: Op) -> Result { + let (els, shapes) = selected_shapes(s, "Combine Objects works on one layer: select shapes on one layer")?; if shapes.len() < 2 { return Err(EngineError::State("select two or more shapes to combine".into())); } + let created = combine_shapes(s, op, els, shapes)?; + Ok(json!({"op": format!("{op:?}").to_ascii_lowercase(), "created": created.iter().map(|e| e.0).collect::>()})) +} + +fn combine_shapes(s: &mut Session, op: Op, els: Vec>, shapes: Vec<(usize, Arc)>) -> Result> { let shape = |e: &Element| match &e.kind { ElementKind::Shape(sh) => Some(sh.clone()), _ => None, @@ -386,7 +438,7 @@ fn combine(s: &mut Session, op: Op) -> Result { } s.editable_keyframe()?.elements = out; s.select_only(created.iter().copied()); - Ok(json!({"op": format!("{op:?}").to_ascii_lowercase(), "created": created.iter().map(|e| e.0).collect::>()})) + Ok(created) } /// The area `path` fills even-odd, from its flattening: each loop's area, added or taken away by diff --git a/crates/engine/src/group_tests.rs b/crates/engine/src/group_tests.rs index aece6a7d..3ae2ffca 100644 --- a/crates/engine/src/group_tests.rs +++ b/crates/engine/src/group_tests.rs @@ -327,6 +327,102 @@ fn combine_refuses_what_it_cannot_combine() { assert!(s.execute("modify.punch", &json!({})).is_err()); } +#[test] +fn create_object_turns_one_raw_shape_into_a_drawing_object_and_undoes() { + let mut s = Session::new(anicraft_doc::Document::default()); + run(&mut s, "draw.object_drawing", json!({"on": false})); + run(&mut s, "draw.rect", json!({"x": 20, "y": 30, "w": 100, "h": 60, "fill": "#ff0000", "stroke": null})); + run(&mut s, "edit.select_all", json!({})); + assert!(matches!(&top(&s)[0].kind, ElementKind::Shape(sh) if sh.merge), "starts as raw"); + let before_bounds = s.selection_bounds(); + let before_pic = picture(&s); + let v = run(&mut s, "modify.create_object", json!({})); + let created: Vec = v["created"].as_array().into_iter().flatten().filter_map(Value::as_u64).collect(); + assert_eq!(created.len(), 1, "{v}"); + let els = top(&s); + assert_eq!(els.len(), 1); + assert!(matches!(&els[0].kind, ElementKind::Shape(sh) if !sh.merge), "became a drawing object"); + assert_eq!(s.selection.id_list(), created, "new drawing object stays selected"); + let after_bounds = s.selection_bounds(); + assert!(after_bounds.min.distance(before_bounds.min) < 1e-3 && after_bounds.max.distance(before_bounds.max) < 1e-3, "bounds {after_bounds:?} vs {before_bounds:?}"); + assert_eq!(picture(&s), before_pic, "look unchanged"); + // Undo restores the raw shape. + run(&mut s, "edit.undo", json!({})); + assert_eq!(top(&s).len(), 1); + assert!(matches!(&top(&s)[0].kind, ElementKind::Shape(sh) if sh.merge), "undo restores raw"); + // Partial raw selection cut out by a marquee becomes a drawing object, and undo heals the cut. + run(&mut s, "raw.select_rect", json!({"x0": 70, "y0": 0, "x1": 150, "y1": 120})); + let total_before: f64 = areas(&s).into_iter().sum(); + let v = run(&mut s, "modify.create_object", json!({})); + let cut_id = v["created"][0].as_u64().unwrap_or(0); + assert_eq!(s.selection.id_list(), vec![cut_id]); + let els = top(&s); + assert_eq!(els.len(), 2); + assert!(els.iter().any(|e| e.id.0 == cut_id && matches!(&e.kind, ElementKind::Shape(sh) if !sh.merge)), "marquee part became drawing object"); + assert!(els.iter().any(|e| e.id.0 != cut_id && matches!(&e.kind, ElementKind::Shape(sh) if sh.merge)), "rest stays raw"); + let total_after: f64 = areas(&s).into_iter().sum(); + assert!((total_after - total_before).abs() < 1.0, "{total_after} vs {total_before}"); + run(&mut s, "edit.undo", json!({})); + assert_eq!(top(&s).len(), 1, "undo heals the marquee cut in one step"); + assert!(matches!(&top(&s)[0].kind, ElementKind::Shape(sh) if sh.merge)); +} + +#[test] +fn create_object_combines_two_separate_raw_shapes() { + // Two separate raw shapes of the same fill become one drawing object. + let mut s = Session::new(anicraft_doc::Document::default()); + run(&mut s, "draw.object_drawing", json!({"on": false})); + run(&mut s, "draw.rect", json!({"x": 0, "y": 0, "w": 60, "h": 60, "fill": "#ff0000", "stroke": null})); + run(&mut s, "draw.rect", json!({"x": 120, "y": 0, "w": 60, "h": 60, "fill": "#ff0000", "stroke": null})); + run(&mut s, "edit.select_all", json!({})); + assert_eq!(top(&s).len(), 2); + let before_bounds = s.selection_bounds(); + let before_area: f64 = areas(&s).into_iter().sum(); + let v = run(&mut s, "modify.create_object", json!({})); + let created: Vec = v["created"].as_array().into_iter().flatten().filter_map(Value::as_u64).collect(); + assert_eq!(created.len(), 1, "{v}"); + let els = top(&s); + assert_eq!(els.len(), 1); + assert!(matches!(&els[0].kind, ElementKind::Shape(sh) if !sh.merge), "one drawing object"); + assert_eq!(s.selection.id_list(), created); + let after_bounds = s.selection_bounds(); + assert!(after_bounds.min.distance(before_bounds.min) < 1e-3 && after_bounds.max.distance(before_bounds.max) < 1e-3); + assert!((areas(&s).into_iter().sum::() - before_area).abs() < 1.0); + run(&mut s, "edit.undo", json!({})); + assert_eq!(top(&s).len(), 2); + // Two separate raw shapes of different fills become a group per Union's rule. + let mut s = Session::new(anicraft_doc::Document::default()); + run(&mut s, "draw.object_drawing", json!({"on": false})); + run(&mut s, "draw.rect", json!({"x": 0, "y": 0, "w": 60, "h": 60, "fill": "#ff0000", "stroke": null})); + run(&mut s, "draw.rect", json!({"x": 120, "y": 0, "w": 60, "h": 60, "fill": "#0000ff", "stroke": null})); + run(&mut s, "edit.select_all", json!({})); + let before_area: f64 = areas(&s).into_iter().sum(); + let v = run(&mut s, "modify.create_object", json!({})); + let created: Vec = v["created"].as_array().into_iter().flatten().filter_map(Value::as_u64).collect(); + assert_eq!(created.len(), 1, "{v}"); + assert_eq!(top(&s).len(), 1); + assert!(matches!(top(&s)[0].kind, ElementKind::Group { .. }), "multi-colour union becomes a group"); + assert_eq!(s.selection.id_list(), created); + assert!((areas(&s).into_iter().sum::() - before_area).abs() < 1.0); +} + +#[test] +fn create_object_refuses_drawing_objects_and_non_shapes() { + let mut s = Session::new(anicraft_doc::Document::default()); + run(&mut s, "draw.rect", json!({"x": 0, "y": 0, "w": 80, "h": 80, "fill": "#ff0000", "object": true})); + run(&mut s, "edit.select_all", json!({})); + let before = s.doc.clone(); + assert!(matches!(s.execute("modify.create_object", &json!({})), Err(crate::EngineError::State(_))), "only a drawing object selected"); + assert_eq!(s.doc, before, "document unchanged on refusal"); + // A symbol in the selection is refused like Combine Objects. + run(&mut s, "modify.convert_to_symbol", json!({"name": "Sym"})); + run(&mut s, "draw.rect", json!({"x": 100, "y": 0, "w": 50, "h": 50, "fill": "#00ff00", "object": false})); + run(&mut s, "edit.select_all", json!({})); + let before = s.doc.clone(); + assert!(matches!(s.execute("modify.create_object", &json!({})), Err(crate::EngineError::State(_))), "symbol in selection"); + assert_eq!(s.doc, before, "document unchanged on symbol refusal"); +} + #[test] fn ctrl_drag_pulls_a_sharp_corner_out_of_a_line() { // What the Selection tool's Ctrl-drag runs (issue #200): a point where the edge was grabbed, diff --git a/docs/guide/drawing.md b/docs/guide/drawing.md index 60025b6b..461584df 100644 --- a/docs/guide/drawing.md +++ b/docs/guide/drawing.md @@ -70,8 +70,10 @@ Properties. The setting applies to the Rectangle, Oval, PolyStar, Line, Pen, Pen tools. The Eraser only erases merged drawing. Drawing objects, symbols and text are left alone by -it. To turn a drawing object back into merged drawing, select it and choose **Modify › Break -Apart** (**Ctrl+B**): it merges with the merged shapes it touches. +it. To turn merged shapes into a drawing object, select them and click **Create Object** in +Properties (or choose **Modify › Combine Objects › Create Object**). To turn a drawing object +back into merged drawing, select it and choose **Modify › Break Apart** (**Ctrl+B**): it +merges with the merged shapes it touches. To change a drawing object's shape, **double-click** it (or select it and press **Ctrl+E**). It opens in place, as a group does (see [Grouping](#grouping)): the rest of the stage dims, and @@ -567,6 +569,9 @@ topmost shape is the one on top in the stacking order. * **Punch** cuts the top shape's area out of the shapes under it, and removes the top shape. * **Crop** keeps only the parts of the shapes under it that are inside the top shape, and removes the top shape. +* **Create Object** turns one or more selected merged shapes on the active layer into a drawing + object (or a group of the merged pieces when they have several colours or keep lines). With + raw shapes selected, **Create Object** also appears in **Properties**. The top shape cuts with its fill, so it needs one. Break symbols, text and groups apart first (**Ctrl+B**). diff --git a/docs/guide/reference/commands.md b/docs/guide/reference/commands.md index 6050b4c2..06b4dc81 100644 --- a/docs/guide/reference/commands.md +++ b/docs/guide/reference/commands.md @@ -1,6 +1,6 @@ # Command reference -Everything you can do in AniCraft is a command: the menus, the shortcuts, the command palette (**Ctrl+K**, macOS **⌘K**), `anicraft-cli` and AI assistants over MCP all run the same 399 commands, listed here by menu. The id is what scripts and [working headless](../working-headless.md) use; the parameters are what a command takes as JSON. Commands that change the document are one undo step each. Your own shortcuts (**Help › Keyboard Shortcuts**) replace the defaults shown here; see the [shortcut reference](shortcuts.md). +Everything you can do in AniCraft is a command: the menus, the shortcuts, the command palette (**Ctrl+K**, macOS **⌘K**), `anicraft-cli` and AI assistants over MCP all run the same 400 commands, listed here by menu. The id is what scripts and [working headless](../working-headless.md) use; the parameters are what a command takes as JSON. Commands that change the document are one undo step each. Your own shortcuts (**Help › Keyboard Shortcuts**) replace the defaults shown here; see the [shortcut reference](shortcuts.md). This page is generated from the command registry, so it always matches the version of AniCraft it came with. @@ -222,6 +222,8 @@ This page is generated from the command registry, so it always matches the versi Parameters: `{} combine with op punch` - **Crop** `modify.crop` · Modify › Combine Objects · changes the document Parameters: `{} combine with op crop` +- **Create Object** `modify.create_object` · Modify › Combine Objects · changes the document + Parameters: `{} turns the selected raw shapes on the active layer into a drawing object (a group of the merged pieces when they have more than one colour or keep lines) → {created}` - **Rotate 90° CW** `modify.rotate_cw` · Modify › Transform · **Ctrl+Shift+9** (macOS **⇧⌘9**) · changes the document Parameters: `{origin?: selection|registration|center (default: the mode set with modify.transform_origin; selection unless changed)} about the centre of the whole selection, or each selected object about its own point` - **Rotate 90° CCW** `modify.rotate_ccw` · Modify › Transform · **Ctrl+Shift+7** (macOS **⇧⌘7**) · changes the document diff --git a/docs/parity.md b/docs/parity.md index 52d2226c..a2226f3c 100644 --- a/docs/parity.md +++ b/docs/parity.md @@ -137,10 +137,10 @@ not this table, says what matters most. | Feature | Status | Notes | |---|---|---| | Merge drawing: raw strokes and fills that cut and join | **done** | [ADR 0031](adr/0031-merge-drawing.md), [ADR 0041](adr/0041-selection-tool-on-raw-drawings.md), [ADR 0042](adr/0042-subselection-and-merging-everywhere.md), [ADR 0045](adr/0045-raw-points-and-raw-shapes-inside-symbols.md); [drawing](guide/drawing.md). Open bug: a hairline between merged curved fills ([#182](https://github.com/storytold/anicraft/issues/182), P-013). | -| Object drawing | **done** | Edit inside by double-click. Editing without opening is missing ([#181](https://github.com/storytold/anicraft/issues/181)). | +| Object drawing | **done** | Edit inside by double-click; **Create Object** in Properties or Modify › Combine Objects turns raw shapes into a drawing object ([#614](https://github.com/storytold/anicraft/issues/614)). Editing without opening is missing ([#181](https://github.com/storytold/anicraft/issues/181)). | | Groups, nested, edited in place | **done** | [ADR 0073](adr/0073-groups-combine-objects-and-selection-modifiers.md). | | Break Apart | **done** | | -| Combine Objects: Union, Intersect, Punch, Crop | **done** | [ADR 0073](adr/0073-groups-combine-objects-and-selection-modifiers.md). One layer at a time. | +| Combine Objects: Union, Intersect, Punch, Crop | **done** | [ADR 0073](adr/0073-groups-combine-objects-and-selection-modifiers.md), plus **Create Object** for raw shapes ([#614](https://github.com/storytold/anicraft/issues/614)). One layer at a time. | | Arrange (front, back, forward, backward) | **done** | | | Align, distribute | **done** | | | Match Size, Space evenly | **missing** | P-017. | diff --git a/src/menus.rs b/src/menus.rs index 4492295c..a0edbc87 100644 --- a/src/menus.rs +++ b/src/menus.rs @@ -465,6 +465,7 @@ fn enabled(app: &App, id: &str) -> bool { "modify.group" => !app.s.selection.is_empty(), "modify.ungroup" => app.s.selected_elements().iter().any(|(_, e)| matches!(e.kind, anicraft_engine::doc::ElementKind::Group { .. })), "modify.union" | "modify.intersect" | "modify.punch" | "modify.crop" => app.s.selected_elements().iter().filter(|(_, e)| matches!(e.kind, anicraft_engine::doc::ElementKind::Shape(_))).count() >= 2, + "modify.create_object" => selected_shape(app, |sh| sh.merge), "scene.prev" | "scene.move_up" => app.s.scene > 0, "view.guides_clear" => !app.s.guides().is_empty(), "scene.next" | "scene.move_down" => app.s.scene + 1 < app.s.doc.scenes.len(), diff --git a/src/panels.rs b/src/panels.rs index 712ff95d..a4df7838 100644 --- a/src/panels.rs +++ b/src/panels.rs @@ -1432,6 +1432,11 @@ fn selection_props(app: &mut App, ui: &mut egui::Ui, e: &anicraft_engine::doc::E } ui.add_space(6.0); ui.horizontal_wrapped(|ui| { + if all.iter().any(|el| matches!(&el.kind, ElementKind::Shape(sh) if sh.merge)) + && ui.button(tr!("Create Object")).on_hover_text(tr!("Turn the selected raw shapes into a drawing object")).clicked() + { + app.invoke("modify.create_object"); + } for (label, id) in [(tr!("Convert to Symbol"), "modify.convert_to_symbol"), (tr!("Break Apart"), "modify.break_apart"), (tr!("Duplicate"), "edit.duplicate"), (tr!("Delete"), "edit.delete")] { if ui.button(label).clicked() { app.invoke(id); diff --git a/src/stage.rs b/src/stage.rs index 2118cbe6..9ad2cb84 100644 --- a/src/stage.rs +++ b/src/stage.rs @@ -980,7 +980,7 @@ pub fn show(app: &mut App, ui: &mut egui::Ui) { } // Modify › Combine Objects (ADR 0073), as in Animate's menu. ui.menu_button(tr!("Combine Objects"), |ui| { - for id in ["modify.union", "modify.intersect", "modify.punch", "modify.crop"] { + for id in ["modify.union", "modify.intersect", "modify.punch", "modify.crop", "modify.create_object"] { crate::menus::cmd_item(app, ui, id); } }); From e920a5b0728253a0829cc095020aaa8c1c331ffb Mon Sep 17 00:00:00 2001 From: yusufmntm Date: Thu, 26 Nov 2026 15:24:02 +0300 Subject: [PATCH 2/3] fix(engine): Create Object and Modify > Combine refuse a group past the nesting limit When the result of Create Object, Union, Punch or Crop is a group and the open groups are already MAX_GROUP_DEPTH deep, refuse with the same error the group command gives, so reopening the file can't drop the artwork. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmds_group.rs | 3 ++ crates/engine/src/group_tests.rs | 52 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/crates/engine/src/cmds_group.rs b/crates/engine/src/cmds_group.rs index 8d301a31..abe8e581 100644 --- a/crates/engine/src/cmds_group.rs +++ b/crates/engine/src/cmds_group.rs @@ -419,6 +419,9 @@ fn combine_shapes(s: &mut Session, op: Op, els: Vec>, shapes: Vec<( } } } + if open_groups(s) + 1 > MAX_GROUP_DEPTH && results.iter().any(|(_, r)| r.as_ref().is_some_and(|e| matches!(e.kind, ElementKind::Group { .. }))) { + return Err(EngineError::State(format!("groups nest at most {MAX_GROUP_DEPTH} deep"))); + } // The sources go; each result takes its source's place. let gone: HashSet = shapes.iter().map(|x| x.0).collect(); let mut created = Vec::new(); diff --git a/crates/engine/src/group_tests.rs b/crates/engine/src/group_tests.rs index 3ae2ffca..c432dbd2 100644 --- a/crates/engine/src/group_tests.rs +++ b/crates/engine/src/group_tests.rs @@ -423,6 +423,58 @@ fn create_object_refuses_drawing_objects_and_non_shapes() { assert_eq!(s.doc, before, "document unchanged on symbol refusal"); } +#[test] +fn combine_and_create_object_refuse_groups_past_max_depth() { + let mut s = Session::new(anicraft_doc::Document::default()); + run(&mut s, "draw.rect", json!({"x": 0, "y": 0, "w": 40, "h": 40, "fill": "#ff0000", "stroke": null})); + for _ in 0..anicraft_doc::MAX_GROUP_DEPTH { + run(&mut s, "edit.select_all", json!({})); + let g = id_of(&run(&mut s, "modify.group", json!({})), "group"); + run(&mut s, "symbol.edit", json!({"id": g.0})); + } + assert_eq!(crate::cmds_group::open_groups(&s), anicraft_doc::MAX_GROUP_DEPTH); + // Two disjoint raw shapes with different fills would produce a group at depth MAX_GROUP_DEPTH + 1. + run(&mut s, "edit.select_all", json!({})); + run(&mut s, "edit.delete", json!({})); + run(&mut s, "draw.object_drawing", json!({"on": false})); + run(&mut s, "draw.rect", json!({"x": 0, "y": 0, "w": 40, "h": 40, "fill": "#ff0000", "stroke": null})); + run(&mut s, "draw.rect", json!({"x": 80, "y": 0, "w": 40, "h": 40, "fill": "#0000ff", "stroke": null})); + run(&mut s, "edit.select_all", json!({})); + let before = s.doc.clone(); + for cmd in ["modify.create_object", "modify.union"] { + assert!(matches!(s.execute(cmd, &json!({})), Err(crate::EngineError::State(ref m)) if m.contains("groups nest at most")), "{cmd} should refuse past max depth"); + assert_eq!(s.doc, before, "{cmd} leaves document unchanged on refusal"); + } + // Punch and Crop on a stroked shape (fill + stroke produces a group) must also refuse. + run(&mut s, "edit.select_all", json!({})); + run(&mut s, "edit.delete", json!({})); + run(&mut s, "draw.object_drawing", json!({"on": true})); + run(&mut s, "draw.rect", json!({"x": 0, "y": 0, "w": 100, "h": 100, "fill": "#ff0000", "stroke": "#000000"})); + run(&mut s, "draw.rect", json!({"x": 50, "y": 50, "w": 100, "h": 100, "fill": "#0000ff", "stroke": null})); + run(&mut s, "edit.select_all", json!({})); + let before = s.doc.clone(); + for cmd in ["modify.punch", "modify.crop"] { + assert!(matches!(s.execute(cmd, &json!({})), Err(crate::EngineError::State(ref m)) if m.contains("groups nest at most")), "{cmd} should refuse past max depth"); + assert_eq!(s.doc, before, "{cmd} leaves document unchanged on refusal"); + } + // Results that stay a single drawing object (not a group) remain allowed at MAX_GROUP_DEPTH. + run(&mut s, "edit.select_all", json!({})); + run(&mut s, "edit.delete", json!({})); + run(&mut s, "draw.object_drawing", json!({"on": false})); + run(&mut s, "draw.rect", json!({"x": 0, "y": 0, "w": 40, "h": 40, "fill": "#ff0000", "stroke": null})); + run(&mut s, "draw.rect", json!({"x": 80, "y": 0, "w": 40, "h": 40, "fill": "#ff0000", "stroke": null})); + run(&mut s, "edit.select_all", json!({})); + run(&mut s, "modify.create_object", json!({})); + assert_eq!(top(&s).len(), 1); + assert!(matches!(&top(&s)[0].kind, ElementKind::Shape(sh) if !sh.merge), "same-fill create_object produces a drawing object"); + // Single-shape Create Object also stays allowed at MAX_GROUP_DEPTH. + run(&mut s, "draw.rect", json!({"x": 200, "y": 0, "w": 40, "h": 40, "fill": "#00ff00", "stroke": null})); + let raw_id = top(&s).iter().find(|e| matches!(&e.kind, ElementKind::Shape(sh) if sh.merge)).map(|e| e.id).unwrap_or_else(|| panic!("raw")); + run(&mut s, "element.select", json!({"ids": [raw_id.0]})); + run(&mut s, "modify.create_object", json!({})); + assert!(top(&s).iter().all(|e| matches!(&e.kind, ElementKind::Shape(sh) if !sh.merge)), "single raw shape became a drawing object"); +} + #[test] fn ctrl_drag_pulls_a_sharp_corner_out_of_a_line() { // What the Selection tool's Ctrl-drag runs (issue #200): a point where the edge was grabbed, From 0e04b6fb506118ebfb397279fef03d0fb2394d33 Mon Sep 17 00:00:00 2001 From: yusufmntm Date: Thu, 26 Nov 2026 20:14:12 +0300 Subject: [PATCH 3/3] fix(engine): Create Object refuses a warped bitmap fill instead of unwarping it The merge writes its pieces without a warp. Outlines keep their bent shape, but a bitmap fill is drawn through the warp mesh only while the warp is there, so it showed unwarped and lost its pins. Refuse the selection, before any change, and point to Remove Warp. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmds_group.rs | 5 ++- crates/engine/src/group_tests.rs | 59 ++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmds_group.rs b/crates/engine/src/cmds_group.rs index abe8e581..2f4a1b27 100644 --- a/crates/engine/src/cmds_group.rs +++ b/crates/engine/src/cmds_group.rs @@ -10,7 +10,7 @@ use crate::merge::{Part, remerge_with}; use crate::params::*; use crate::{CommandSpec, EngineError, Result, Session}; -use anicraft_doc::{ColorEffect, Element, ElementId, ElementKind, LayerId, MAX_GROUP_DEPTH, Shape}; +use anicraft_doc::{ColorEffect, Element, ElementId, ElementKind, LayerId, MAX_GROUP_DEPTH, Paint, Shape}; use anicraft_geom::planar::{self, Item}; use anicraft_geom::{Affine, FillRule, Path, Rect, Vec2}; use serde_json::{Value, json}; @@ -337,6 +337,9 @@ fn create_object(s: &mut Session, _: &Value) -> Result { if !shapes.iter().any(|(_, e)| crate::merge::mergeable(e).is_some()) { return Err(EngineError::State("the selection is drawing objects already: select raw shapes to turn into a drawing object".into())); } + if shapes.iter().any(|(_, e)| matches!(&e.kind, ElementKind::Shape(sh) if !sh.merge && sh.warp.is_some() && matches!(sh.fill, Some(Paint::Bitmap { .. })))) { + return Err(EngineError::State("Create Object can't keep the warp of a warped bitmap fill: remove its warp first (Modify › Asset Warp › Remove Warp)".into())); + } if shapes.len() == 1 { let Some((i, e)) = shapes.into_iter().next() else { return Err(EngineError::State("select raw shapes to turn into a drawing object".into())); diff --git a/crates/engine/src/group_tests.rs b/crates/engine/src/group_tests.rs index c432dbd2..9333189d 100644 --- a/crates/engine/src/group_tests.rs +++ b/crates/engine/src/group_tests.rs @@ -475,6 +475,65 @@ fn combine_and_create_object_refuse_groups_past_max_depth() { assert!(top(&s).iter().all(|e| matches!(&e.kind, ElementKind::Shape(sh) if !sh.merge)), "single raw shape became a drawing object"); } +#[test] +fn create_object_refuses_warped_bitmap_fill_but_allows_warped_solid_fill() { + // Mixed selection with a raw shape and a warped bitmap-fill drawing object: refused, + // leaving the document, selection and undo history untouched. + let mut s = Session::new(anicraft_doc::Document::default()); + let bm = anicraft_doc::BitmapId(s.doc.alloc_id()); + let mut rgba = Vec::with_capacity(40 * 20 * 4); + for y in 0..20u32 { + for x in 0..40u32 { + rgba.extend_from_slice(&[(x * 6) as u8, (y * 12) as u8, 128, 255]); + } + } + let png = anicraft_render::encode_png(40, 20, &rgba).unwrap_or_else(|e| panic!("encode_png: {e}")); + s.doc.bitmaps.push(Arc::new(anicraft_doc::Bitmap::from_png(bm, "ramp.png", png).unwrap_or_else(|| panic!("bitmap")))); + let bmp = id_of(&run(&mut s, "library.place", json!({"bitmap": bm.0, "x": 300, "y": 230})), "element"); + run(&mut s, "warp.pin_add", json!({"element": bmp.0, "x": 285, "y": 230})); + let pin = run(&mut s, "warp.pin_add", json!({"element": bmp.0, "x": 315, "y": 230}))["pin"].as_u64().unwrap_or_else(|| panic!("pin")); + run(&mut s, "warp.pin_move", json!({"element": bmp.0, "pin": pin, "x": 315, "y": 270})); + assert!( + top(&s).iter().any(|e| e.id == bmp && matches!(&e.kind, ElementKind::Shape(sh) if !sh.merge && sh.warp.is_some() && matches!(sh.fill, Some(anicraft_doc::Paint::Bitmap { .. })))), + "placed bitmap must be a warped bitmap-fill drawing object" + ); + run(&mut s, "draw.rect", json!({"x": 20, "y": 20, "w": 60, "h": 60, "fill": "#ff0000", "stroke": null, "object": false})); + run(&mut s, "edit.select_all", json!({})); + let before_doc = s.doc.clone(); + let before_sel = s.selection.id_list(); + let before_hist = s.history_len(); + assert!( + matches!(s.execute("modify.create_object", &json!({})), Err(crate::EngineError::State(ref m)) if m.contains("warped bitmap")), + "warped bitmap fill in mixed selection must be refused" + ); + assert_eq!(s.doc, before_doc, "document unchanged on warped bitmap refusal"); + assert_eq!(s.selection.id_list(), before_sel, "selection unchanged on warped bitmap refusal"); + assert_eq!(s.history_len(), before_hist, "no undo step recorded on warped bitmap refusal"); + + // Mixed selection with a raw shape and a warped solid-fill drawing object: still converts, + // preserving the bent outline. + let mut s = Session::new(anicraft_doc::Document::default()); + let solid = id_of(&run(&mut s, "draw.rect", json!({"x": 100, "y": 100, "w": 200, "h": 80, "fill": "#3080ff", "stroke": null, "object": true})), "element"); + run(&mut s, "warp.pin_add", json!({"element": solid.0, "x": 110, "y": 140})); + let pin = run(&mut s, "warp.pin_add", json!({"element": solid.0, "x": 290, "y": 140}))["pin"].as_u64().unwrap_or_else(|| panic!("pin")); + run(&mut s, "warp.pin_move", json!({"element": solid.0, "pin": pin, "x": 290, "y": 200})); + let bent_bounds = top(&s)[0].bounds(&s.doc); + assert!(bent_bounds.max.y > 180.0, "solid drawing object was bent downward: {bent_bounds:?}"); + run(&mut s, "draw.rect", json!({"x": 10, "y": 10, "w": 50, "h": 50, "fill": "#3080ff", "stroke": null, "object": false})); + run(&mut s, "edit.select_all", json!({})); + let before_area: f64 = areas(&s).into_iter().sum(); + let v = run(&mut s, "modify.create_object", json!({})); + let created: Vec = v["created"].as_array().into_iter().flatten().filter_map(Value::as_u64).collect(); + assert_eq!(created.len(), 1, "{v}"); + let els = top(&s); + assert_eq!(els.len(), 1); + assert!(matches!(&els[0].kind, ElementKind::Shape(sh) if !sh.merge), "warped solid + raw same fill becomes a drawing object"); + assert_eq!(s.selection.id_list(), created); + assert!(els[0].bounds(&s.doc).max.y >= bent_bounds.max.y - 1.0, "bent outline kept after Create Object"); + let after_area: f64 = areas(&s).into_iter().sum(); + assert!((after_area - before_area).abs() < 1.0, "combined area preserved: {after_area} vs {before_area}"); +} + #[test] fn ctrl_drag_pulls_a_sharp_corner_out_of_a_line() { // What the Selection tool's Ctrl-drag runs (issue #200): a point where the edge was grabbed,