diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b9e8ea8..0b1ccb09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ Rectangle or Oval tool grows the shape symmetrically about the press point, and combines with Shift for a centred square or circle. +* **Alt while rotating with Free Transform turns about the opposite corner** (#629): holding Alt + while dragging just outside a corner rotates the selection about the corner diagonally opposite + the one grabbed instead of the transformation point, and works together with Shift's 15° snap. * **Flash 8 to CS4 `.fla` files open** (backlog P-039, ADR 0120, #163; by rcoenen, #478 and #481): the binary `.fla` of the Flash 8 to CS4 era, which AniCraft used to refuse, now opens like an Animate file, untitled, so Save asks for a new `.anicraft` name. diff --git a/docs/guide/drawing.md b/docs/guide/drawing.md index 0d3193d9..634decb1 100644 --- a/docs/guide/drawing.md +++ b/docs/guide/drawing.md @@ -431,7 +431,8 @@ With the **Free Transform** tool (**Q**), drag a handle (a corner or the middle scales about the transformation point instead), the line between two handles to **skew** (the opposite edge stays where it is; **Alt** skews about the transformation point), just outside a corner to **rotate** (the pointer turns -into a curved arrow there; **Shift** snaps to 15°), or inside the box to **move**. +into a curved arrow there; **Shift** snaps to 15°, **Alt** rotates about the opposite corner), +or inside the box to **move**. One selected object shows **its own box**: turn a symbol, a group, text or a drawing object and its box turns with it, so you can see how it stands, and its handles stretch it along its diff --git a/src/app.rs b/src/app.rs index da900de5..73329302 100644 --- a/src/app.rs +++ b/src/app.rs @@ -146,9 +146,9 @@ impl Tool { Tool::Bind => tr_noop!("Shows the points of shapes with bones: a square follows one bone, a triangle several. Click a bone to see the points it moves (filled). Click a point, or drag a box around points, to make them follow only that bone; Shift shares the bone with the ones they follow, Ctrl takes them from it. Turn on Paint Weights in Properties to brush the bone's influence on, off or smooth, shown as a heat map."), Tool::Warp => tr_noop!("Click a shape or a bitmap to pin it (the first pin covers it with a warp mesh). Drag a pin to bend the artwork around the other pins; with one pin it moves. Click a pin and press Delete to remove it. A pin moved at another frame keys it, and pins tween."), Tool::GradientTransform => tr_noop!("Click a shape with a gradient or bitmap fill to show its handles. Drag the centre to move the fill, the square to change its width, the circle to change its size, the knob to rotate it (Shift: 15° steps) and, on a radial gradient, the triangle to move its focal point. A bitmap fill also has a square for its height and diamonds to skew it. Each drag is one undo step."), - Tool::Transform => tr_noop!("Drag a handle to scale (Shift keeps proportions, Alt scales from the centre), a line between handles to skew (Alt skews about the centre), just outside a corner to rotate (Shift snaps 15°), inside to move."), Tool::Rect => tr_noop!("Drag to draw a rectangle. Shift draws a square, Alt draws from the centre."), Tool::Oval => tr_noop!("Drag to draw an oval. Shift draws a circle, Alt draws from the centre."), + Tool::Transform => tr_noop!("Drag a handle to scale (Shift keeps proportions, Alt scales from the centre), a line between handles to skew (Alt skews about the centre), just outside a corner to rotate (Shift snaps 15°, Alt rotates about the opposite corner), inside to move."), Tool::Star => tr_noop!("Drag from the centre to draw a star or a polygon (Shape in the options)."), Tool::Line => tr_noop!("Drag to draw a line. Shift snaps to 45°."), Tool::PaintBucket => tr_noop!("Click inside an outline to fill it (on the layer's lines), or on a fill to recolour it. The gap size closes small openings."), diff --git a/src/stage.rs b/src/stage.rs index 8603f49d..02ce5fbd 100644 --- a/src/stage.rs +++ b/src/stage.rs @@ -1445,10 +1445,22 @@ pub fn show(app: &mut App, ui: &mut egui::Ui) { // The transformation point (ADR 0111): the instance's own, the one held for the // selection, or the box's centre; or, when each object is transformed about its own // point (ADR 0109), every object's (past a few hundred objects the box alone shows, - // so a huge selection stays quick). + // so a huge selection stays quick). While Alt is held during a rotate drag, show the + // opposite-corner pivot actually in use. let mode = app.s.style.transform_origin; let own = !multi && mode != anicraft_engine::TransformOrigin::Selection && app.s.selection.len() <= MAX_ORIGIN_MARKS; - let points: Vec = if own { anicraft_engine::transform_origins(&app.s, mode).into_iter().map(|(_, at)| to_screen(at)).collect() } else { vec![to_screen(frame.point)] }; + let alt_pivot = match app.drag { + Drag::Transform { kind: XfKind::Rotate, start, bounds, .. } if alt && mode == anicraft_engine::TransformOrigin::Selection => Some(app.xf.drag.unwrap_or_else(|| crate::xform::Frame::upright(bounds)).rotate_pivot(start, true)), + Drag::MultiTransform { kind: XfKind::Rotate, start, bounds, .. } if alt => Some(crate::xform::Frame::upright(bounds).rotate_pivot(start, true)), + _ => None, + }; + let points: Vec = if let Some(p) = alt_pivot { + vec![to_screen(p)] + } else if own { + anicraft_engine::transform_origins(&app.s, mode).into_iter().map(|(_, at)| to_screen(at)).collect() + } else { + vec![to_screen(frame.point)] + }; for c in points { painter.circle(c, 4.0, Color32::WHITE, Stroke::new(1.0, T.selection)); painter.line_segment([c - vec2(6.0, 0.0), c + vec2(6.0, 0.0)], Stroke::new(1.0, T.selection)); @@ -4904,7 +4916,8 @@ pub(crate) fn xf_total(kind: XfKind, b: [f32; 4], start: Pos2, cur: Pos2, shift: } /// [`xf_total`] with the transformation point `about` (default: the box's centre): what a -/// rotation turns about, and what Alt scales and skews about (ADR 0111). +/// rotation turns about (Alt turns about the corner opposite `start`), and what Alt scales and +/// skews about (ADR 0111). pub(crate) fn xf_total_about(kind: XfKind, b: [f32; 4], about: Option, start: Pos2, cur: Pos2, shift: bool, alt: bool) -> Affine { let [x0, y0, x1, y1] = b; let (cx, cy) = about.map_or(((x0 + x1) / 2.0, (y0 + y1) / 2.0), |p| (p.x, p.y)); @@ -4962,14 +4975,20 @@ pub(crate) fn xf_total_about(kind: XfKind, b: [f32; 4], about: Option, sta } } XfKind::Rotate => { - let a0 = (start.y - cy).atan2(start.x - cx); - let a1 = (cur.y - cy).atan2(cur.x - cx); + let (px, py) = if alt { + let corners = [(pos2(x0, y0), (x1, y1)), (pos2(x1, y0), (x0, y1)), (pos2(x1, y1), (x0, y0)), (pos2(x0, y1), (x1, y0))]; + corners.into_iter().min_by(|(a, _), (b, _)| a.distance_sq(start).total_cmp(&b.distance_sq(start))).map_or((cx, cy), |(_, opp)| opp) + } else { + (cx, cy) + }; + let a0 = (start.y - py).atan2(start.x - px); + let a1 = (cur.y - py).atan2(cur.x - px); let mut a = a1 - a0; if shift { let step = 15f32.to_radians(); a = (a / step).round() * step; } - about(cx, cy, Affine::rotate(a)) + about(px, py, Affine::rotate(a)) } } } @@ -5282,6 +5301,48 @@ mod xf_tests { assert!((angle / 15.0 - (angle / 15.0).round()).abs() < 1e-3, "{angle}"); } + #[test] + fn alt_rotate_pivots_on_the_opposite_corner_for_each_corner() { + // B is [0, 0, 100, 50]: each corner grabbed just outside, with its opposite corner fixed. + let cases = [ + ((0.0, 0.0), (100.0, 50.0), pos2(-12.0, -12.0), pos2(20.0, -40.0)), + ((100.0, 0.0), (0.0, 50.0), pos2(112.0, -12.0), pos2(120.0, 35.0)), + ((100.0, 50.0), (0.0, 0.0), pos2(112.0, 62.0), pos2(80.0, 95.0)), + ((0.0, 50.0), (100.0, 0.0), pos2(-12.0, 62.0), pos2(-20.0, 15.0)), + ]; + for ((gx, gy), (ox, oy), start, cur) in cases { + let m = xf_total(XfKind::Rotate, B, start, cur, false, true); + assert!(apply(&m, ox, oy).distance(Vec2::new(ox, oy)) < 1e-3, "opposite corner ({ox}, {oy}) stayed fixed"); + assert!(apply(&m, gx, gy).distance(Vec2::new(gx, gy)) > 1.0, "grabbed corner ({gx}, {gy}) moved"); + let expected = (cur.y - oy).atan2(cur.x - ox) - (start.y - oy).atan2(start.x - ox); + let diff = (m.decompose().rotation - expected + std::f32::consts::PI).rem_euclid(std::f32::consts::TAU) - std::f32::consts::PI; + assert!(diff.abs() < 1e-3, "angle matched sweep about ({ox}, {oy}): got {}, expected {expected}", m.decompose().rotation); + } + // Without Alt, a given transformation point stays fixed; with Alt, the opposite corner wins. + let custom = pos2(20.0, 10.0); + let plain = xf_total_about(XfKind::Rotate, B, Some(custom), pos2(112.0, 62.0), pos2(80.0, 95.0), false, false); + assert!(apply(&plain, custom.x, custom.y).distance(Vec2::new(custom.x, custom.y)) < 1e-3, "custom pivot stays fixed without Alt"); + assert!(apply(&plain, 0.0, 0.0).distance(Vec2::new(0.0, 0.0)) > 1.0, "corner moves without Alt"); + let with_alt = xf_total_about(XfKind::Rotate, B, Some(custom), pos2(112.0, 62.0), pos2(80.0, 95.0), false, true); + assert!(apply(&with_alt, 0.0, 0.0).distance(Vec2::new(0.0, 0.0)) < 1e-3, "opposite corner stays fixed with Alt"); + assert!(apply(&with_alt, custom.x, custom.y).distance(Vec2::new(custom.x, custom.y)) > 1.0, "custom pivot moves with Alt"); + } + + #[test] + fn alt_shift_rotate_snaps_about_the_opposite_corner() { + let start = pos2(112.0, 62.0); + let cur = pos2(80.0, 105.0); + let m = xf_total(XfKind::Rotate, B, start, cur, true, true); + assert!(apply(&m, 0.0, 0.0).distance(Vec2::new(0.0, 0.0)) < 1e-3, "opposite corner stays fixed"); + assert!(apply(&m, 100.0, 50.0).distance(Vec2::new(100.0, 50.0)) > 1.0, "grabbed corner moves"); + let angle = m.decompose().rotation.to_degrees(); + assert!(angle.abs() > 1.0, "snapped to a non-zero step: {angle}"); + assert!((angle / 15.0 - (angle / 15.0).round()).abs() < 1e-3, "snapped to 15°: {angle}"); + let raw = ((cur.y.atan2(cur.x) - start.y.atan2(start.x)).to_degrees() / 15.0).round() * 15.0; + let diff = (angle - raw + 180.0).rem_euclid(360.0) - 180.0; + assert!(diff.abs() < 1e-3, "snapped angle {angle} matched {raw}"); + } + #[test] fn dragging_the_top_edge_shears_sideways_about_the_bottom_edge() { // B is 100 x 50: dragging the top edge 10 right moves the top edge 10 right. @@ -5637,6 +5698,111 @@ mod pointer_tests { assert_eq!(steps(&app), undo + 1); } + /// #629: holding Alt while rotating with Free Transform turns the selection about the corner + /// opposite the one grabbed, and commits that same pivot through `modify.transform`. + #[test] + fn alt_rotate_drag_on_stage_pivots_on_the_opposite_corner() { + let (mut app, ctx, at) = scene(); + app.run("edit.select_all", json!({})); + app.run("edit.delete", json!({})); + app.run("draw.rect", json!({"x": 300, "y": 200, "w": 200, "h": 100, "fill": "#3366ff", "stroke": null, "object": true})); + app.run("modify.convert_to_symbol", json!({"name": "Card"})); + app.tool = Tool::Transform; + let mut t = 2.0; + let mut frame = |app: &mut App, modifiers: Modifiers, events: Vec| { + t += 0.05; + let mut all = vec![Event::ModifiersChanged(modifiers)]; + all.extend(events); + let input = egui::RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(1600.0, 940.0))), time: Some(t), events: all, ..Default::default() }; + let mut out = ctx.run_ui(input, |ui| app.frame(ui)); + out.textures_delta.clear(); + }; + frame(&mut app, Modifiers::NONE, Vec::new()); + let instance = |app: &App| app.s.selected_elements().first().map(|(_, e)| e.transform).expect("the instance is selected"); + let box_of = |app: &App| crate::xform::frame(app).expect("a selection has a box"); + let before_box = box_of(&app); + let [_, tr, _, bl] = before_box.corners(); + let outside = tr + (tr - before_box.handle(0, 0)).normalized() * (14.0 / app.view.zoom); + let (a, b) = (at(outside.x, outside.y), at(outside.x + 40.0, outside.y + 120.0)); + let (before, undo) = (instance(&app).decompose(), app.s.history().undo_len()); + frame(&mut app, Modifiers::ALT, vec![Event::PointerMoved(a)]); + frame(&mut app, Modifiers::ALT, vec![Event::PointerButton { pos: a, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::ALT }]); + for i in 1..=5 { + frame(&mut app, Modifiers::ALT, vec![Event::PointerMoved(a + (b - a) * (i as f32 / 5.0))]); + } + frame(&mut app, Modifiers::ALT, vec![Event::PointerButton { pos: b, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::ALT }]); + frame(&mut app, Modifiers::NONE, Vec::new()); + let after = instance(&app).decompose(); + let after_box = box_of(&app); + assert!((after.rotation - before.rotation).abs().to_degrees() > 5.0, "rotated with Alt: {before:?} to {after:?}"); + assert!(after_box.corners()[3].distance(bl) < 1.0, "opposite corner stayed fixed: {:?} vs {bl:?}", after_box.corners()[3]); + assert!(after_box.corners()[1].distance(tr) > 5.0, "grabbed corner moved: {:?} vs {tr:?}", after_box.corners()[1]); + assert_eq!(app.s.history().undo_len(), undo + 1, "one undo step"); + } + + /// #629: in Center or Registration mode, Alt while rotating still turns each object about its + /// own point even past `MAX_ORIGIN_MARKS`, so the overlay must not jump to the opposite corner. + #[test] + fn alt_rotate_pivot_overlay_respects_origin_mode_past_marker_limit() { + let (mut app, ctx, at) = scene(); + app.run("edit.select_all", json!({})); + app.run("edit.delete", json!({})); + app.s.begin_gesture("setup"); + for i in 0..=MAX_ORIGIN_MARKS { + let (x, y) = (100 + (i % 25) * 12, 100 + (i / 25) * 10); + let _ = app.s.execute("draw.rect", &json!({"x": x, "y": y, "w": 8, "h": 6, "fill": "#3366ff", "stroke": null, "object": true})); + } + app.s.end_gesture(); + app.run("edit.select_all", json!({})); + assert!(app.s.selection.len() > MAX_ORIGIN_MARKS); + app.tool = Tool::Transform; + let mut t = 2.0; + let mut frame = |app: &mut App, modifiers: Modifiers, events: Vec| { + t += 0.05; + let mut all = vec![Event::ModifiersChanged(modifiers)]; + all.extend(events); + let input = egui::RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(1600.0, 940.0))), time: Some(t), events: all, ..Default::default() }; + let mut out = ctx.run_ui(input, |ui| app.frame(ui)); + out.textures_delta.clear(); + out.shapes + .iter() + .filter_map(|s| match &s.shape { + egui::Shape::Circle(c) if (c.radius - 4.0).abs() < 1e-3 && c.fill == Color32::WHITE && c.stroke == Stroke::new(1.0, T.selection) => Some(c.center), + _ => None, + }) + .collect::>() + }; + frame(&mut app, Modifiers::NONE, Vec::new()); + + let mut alt_drag_marks = |app: &mut App, mode: anicraft_engine::TransformOrigin| { + app.run("modify.transform_origin", json!({"mode": mode.key()})); + frame(app, Modifiers::NONE, Vec::new()); + let before_box = crate::xform::frame(app).expect("a selection has a box"); + let [_, tr, _, bl] = before_box.corners(); + let outside = tr + (tr - before_box.handle(0, 0)).normalized() * (14.0 / app.view.zoom); + let (a, b) = (at(outside.x, outside.y), at(outside.x + 20.0, outside.y + 40.0)); + frame(app, Modifiers::ALT, vec![Event::PointerMoved(a)]); + frame(app, Modifiers::ALT, vec![Event::PointerButton { pos: a, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::ALT }]); + let marks = frame(app, Modifiers::ALT, vec![Event::PointerMoved(b)]); + assert!(matches!(app.drag, Drag::Transform { kind: XfKind::Rotate, .. }), "in rotate drag for {mode:?}"); + let cur_box = crate::xform::frame(app).expect("a selection has a box"); + frame(app, Modifiers::ALT, vec![Event::PointerButton { pos: b, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::ALT }]); + frame(app, Modifiers::NONE, Vec::new()); + app.run("edit.undo", json!({})); + (marks, at(cur_box.point.x, cur_box.point.y), at(bl.x, bl.y)) + }; + + for mode in [anicraft_engine::TransformOrigin::Center, anicraft_engine::TransformOrigin::Registration] { + let (marks, box_pt, opp) = alt_drag_marks(&mut app, mode); + assert_eq!(marks.len(), 1, "past MAX_ORIGIN_MARKS one fallback mark shows for {mode:?}"); + assert!(marks[0].distance(box_pt) < marks[0].distance(opp), "{mode:?} must keep the box point, not jump to the opposite corner: mark={:?}, box_pt={box_pt:?}, opp={opp:?}", marks[0]); + } + + let (marks, box_pt, opp) = alt_drag_marks(&mut app, anicraft_engine::TransformOrigin::Selection); + assert_eq!(marks.len(), 1, "Selection mode shows the opposite-corner pivot"); + assert!(marks[0].distance(opp) < marks[0].distance(box_pt), "Selection mode shows the opposite corner even past MAX_ORIGIN_MARKS: mark={:?}, box_pt={box_pt:?}, opp={opp:?}", marks[0]); + } + /// A double-click: press, release, `gap` seconds, press, release (each its own frame). fn double_click(gap: f64) -> App { let (mut app, ctx, at) = scene(); diff --git a/src/xform.rs b/src/xform.rs index dd2893f1..6357dc6f 100644 --- a/src/xform.rs +++ b/src/xform.rs @@ -3,8 +3,8 @@ //! One selected element shows its own box, turned, scaled and skewed as it is, and its handles //! act along the box's own axes (#175); several elements, or raw pieces, show the upright box //! of the selection. The transformation point (the white circle) is what a rotation turns -//! about, and what Alt scales and skews about. Drag it to move it; double-click it to put it -//! back at the centre. A symbol instance keeps its point in the document +//! about (Alt turns about the opposite corner), and what Alt scales and skews about. Drag it +//! to move it; double-click it to put it back at the centre. A symbol instance keeps its point in the document //! (`modify.transformation_point`); anything else has one for the selection while Free //! Transform is held, kept here (#302, #280, #178, #183). //! @@ -174,18 +174,28 @@ impl Frame { [tl, tr, br, bl].iter().any(|c| c.distance(p) <= ROTATE_ZONE).then_some(XfKind::Rotate) } + /// What a rotation drag started at stage point `start` turns about: the transformation point, + /// or with `alt` the corner of the box opposite the one nearest `start`. + pub fn rotate_pivot(&self, start: Pos2, alt: bool) -> Vec2 { + if !alt { + return self.point; + } + let [tl, tr, br, bl] = self.corners(); + let s = Vec2::new(start.x, start.y); + [(tl, br), (tr, bl), (br, tl), (bl, tr)].into_iter().min_by(|(a, _), (b, _)| a.distance(s).total_cmp(&b.distance(s))).map_or(self.point, |(_, opp)| opp) + } + /// The whole transform (stage space) of a drag of `kind` from stage point `start` to /// `cur`. Scaling and skewing act along the box's own axes; a rotation turns about the - /// transformation point, and Alt scales and skews about it. + /// transformation point (Alt turns about the opposite corner), and Alt scales and skews about it. pub fn total(&self, kind: XfKind, start: Pos2, cur: Pos2, shift: bool, alt: bool) -> Affine { match kind { XfKind::Pivot => Affine::IDENTITY, XfKind::Move => crate::stage::xf_total(kind, self.b, start, cur, shift, alt), // A turn is a turn on the stage, however the box is scaled or skewed. XfKind::Rotate => { - let c = self.point; - let span = [c.x - 1.0, c.y - 1.0, c.x + 1.0, c.y + 1.0]; - crate::stage::xf_total(kind, span, start, cur, shift, alt) + let p = self.rotate_pivot(start, alt); + crate::stage::xf_total_about(kind, self.b, Some(pos2(p.x, p.y)), start, cur, shift, false) } XfKind::Scale { .. } | XfKind::Skew { .. } => { let Some(inv) = self.m.inverse() else { return Affine::IDENTITY }; @@ -350,6 +360,30 @@ mod tests { assert_eq!(u.total(XfKind::Scale { hx: 1, hy: 1 }, pos2(100.0, 50.0), pos2(200.0, 100.0), false, false), crate::stage::xf_total(XfKind::Scale { hx: 1, hy: 1 }, u.b, pos2(100.0, 50.0), pos2(200.0, 100.0), false, false)); } + #[test] + fn alt_rotation_turns_about_the_opposite_corner_even_on_a_turned_box() { + let f = Frame { point: Vec2::new(150.0, 60.0), ..turned() }; + let centre = f.handle(0, 0); + let [tl, tr, br, bl] = f.corners(); + for (grabbed, opposite) in [(tl, br), (tr, bl), (br, tl), (bl, tr)] { + let out = grabbed + (grabbed - centre).normalized() * 14.0; + let start = pos2(out.x, out.y); + let cur = pos2(out.x + 25.0, out.y + 40.0); + let m = f.total(XfKind::Rotate, start, cur, false, true); + assert!(near(m.apply(opposite), opposite), "opposite corner {opposite:?} stayed fixed"); + assert!(m.apply(grabbed).distance(grabbed) > 1.0, "grabbed corner {grabbed:?} moved"); + } + // Shift + Alt still snaps to 15° about the opposite corner. + let out = tr + (tr - centre).normalized() * 14.0; + let m = f.total(XfKind::Rotate, pos2(out.x, out.y), pos2(out.x + 60.0, out.y - 10.0), true, true); + assert!(near(m.apply(bl), bl), "opposite corner stayed fixed with Shift+Alt"); + let turn = m.decompose().rotation.to_degrees(); + assert!((turn / 15.0 - (turn / 15.0).round()).abs() < 1e-3 && turn.abs() > 1.0, "{turn}"); + // An upright frame matches `xf_total` with Alt too. + let u = Frame::upright([0.0, 0.0, 100.0, 50.0]); + assert_eq!(u.total(XfKind::Rotate, pos2(112.0, 62.0), pos2(80.0, 95.0), false, true), crate::stage::xf_total(XfKind::Rotate, u.b, pos2(112.0, 62.0), pos2(80.0, 95.0), false, true)); + } + #[test] fn the_dragged_point_snaps_to_the_boxs_handles_and_centre() { let f = turned();