Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

graphics: shape handles change the shape's Size, Shift keeps the ratio

ReReddofdb0ce944ddc
diff --git a/crates/ui-egui/src/panels/graphics.rs b/crates/ui-egui/src/panels/graphics.rs
index 09aead4f5..349353843 100644
--- a/crates/ui-egui/src/panels/graphics.rs
+++ b/crates/ui-egui/src/panels/graphics.rs
@@ -186,7 +186,8 @@ fn handle_points(quad: &[Pos2; 4]) -> [Pos2; 8] {
}
/// Dragging handle `n` of `quad` from `start` to `cur`: the box grows towards the pointer while
-/// the opposite corner / edge stays put. A corner scales both axes alike; an edge only its own.
+/// the opposite corner / edge stays put. A corner scales each axis by the pointer's travel along
+/// it (both alike with `keep_ratio`, Shift); an edge scales only its own.
struct HandleScale {
/// The handle opposite the dragged one, which does not move.
pin: Pos2,
@@ -195,10 +196,12 @@ struct HandleScale {
uy: egui::Vec2,
fx: f32,
fy: f32,
+ /// Width and height scale apart (an edge, or a corner without Shift).
+ apart: bool,
}
impl HandleScale {
- fn new(quad: &[Pos2; 4], n: usize, start: Pos2, cur: Pos2) -> Self {
+ fn new(quad: &[Pos2; 4], n: usize, start: Pos2, cur: Pos2, keep_ratio: bool) -> Self {
let pts = handle_points(quad);
let n = n.min(7);
let pin = if n < 4 { pts[(n + 2) % 4] } else { pts[4 + (n - 4 + 2) % 4] };
@@ -209,14 +212,15 @@ impl HandleScale {
if from.abs() < 1.0 { 1.0 } else { ((cur - pin).dot(dir) / from).max(0.01) }
};
let (fx, fy) = match n {
- 0..=3 => {
+ 0..=3 if keep_ratio => {
let f = along((start - pin).normalized());
(f, f)
}
+ 0..=3 => (along(ux), along(uy)),
4 | 6 => (1.0, along(uy)),
_ => (along(ux), 1.0),
};
- Self { pin, ux, uy, fx, fy }
+ Self { pin, ux, uy, fx, fy, apart: n >= 4 || !keep_ratio }
}
/// Where a screen point of the layer ends up.
@@ -231,9 +235,29 @@ impl HandleScale {
/// Premiere Pro 26 with the Program monitor at Fit.
const NEAR_ANCHOR: f32 = 60.0;
+/// Box `from` (`[x0, y0, x1, y1]`) with handle `n` moved by (`dx`, `dy`): that side or corner
+/// follows, the opposite one stays, and the box keeps at least `min` across instead of flipping.
+fn drag_sides(from: [f32; 4], n: usize, dx: f32, dy: f32, min: f32) -> [f32; 4] {
+ let [mut x0, mut y0, mut x1, mut y1] = from;
+ if matches!(n, 0 | 3 | 7) {
+ x0 = (x0 + dx).min(x1 - min);
+ }
+ if matches!(n, 1 | 2 | 5) {
+ x1 = (x1 + dx).max(x0 + min);
+ }
+ if matches!(n, 0 | 1 | 4) {
+ y0 = (y0 + dy).min(y1 - min);
+ }
+ if matches!(n, 2 | 3 | 6) {
+ y1 = (y1 + dy).max(y0 + min);
+ }
+ [x0, y0, x1, y1]
+}
+
/// What dragging handle `n` of a layer from `start` to `cur` does, as Premiere Pro does it: point
/// text scales about its anchor point, a paragraph-text box is resized (its text re-wraps at the
-/// same size), and a shape stretches away from its opposite side.
+/// same size), a shape's Size follows the handle (Scale untouched), and a path stretches away from
+/// its opposite side.
enum HandleDrag {
Stretch(HandleScale),
/// Scale both axes by `f` about the anchor point, which is at `anchor` on screen.
@@ -248,36 +272,54 @@ enum HandleDrag {
from: [f32; 4],
tall: bool,
},
+ /// The shape's box becomes `rect` (in the layer's pixels; it was `from`, with Size `size`).
+ Size {
+ rect: [f32; 4],
+ from: [f32; 4],
+ size: (f32, f32),
+ },
}
impl HandleDrag {
- fn new(v: &LayerView, n: usize, start: Pos2, cur: Pos2) -> Self {
+ fn new(v: &LayerView, n: usize, start: Pos2, cur: Pos2, keep_ratio: bool) -> Self {
let n = n.min(7);
let quad = v.quad();
- let Some(t) = text_of(&v.spec) else { return Self::Stretch(HandleScale::new(&quad, n, start, cur)) };
+ let (dx, dy) = match (v.to_local(start), v.to_local(cur)) {
+ (Some(a), Some(b)) => (b.0 - a.0, b.1 - a.1),
+ _ => (0.0, 0.0),
+ };
+ let Some(t) = text_of(&v.spec) else {
+ let LayerContent::Shape(s) = &v.spec.content else { return Self::Stretch(HandleScale::new(&quad, n, start, cur, keep_ratio)) };
+ // a path is its points, so it stretches; the other shapes are drawn from their Size
+ if s.shape == 3 {
+ return Self::Stretch(HandleScale::new(&quad, n, start, cur, keep_ratio));
+ }
+ let from = v.local;
+ let mut rect = drag_sides(from, n, dx, dy, 1.0);
+ let (w, h) = (from[2] - from[0], from[3] - from[1]);
+ if keep_ratio && n < 4 && w > 0.0 && h > 0.0 {
+ // Shift: the side that moved further sets the scale of both; the opposite corner stays
+ let (fw, fh) = ((rect[2] - rect[0]) / w, (rect[3] - rect[1]) / h);
+ let f = if (fw - 1.0).abs() >= (fh - 1.0).abs() { fw } else { fh };
+ let f = f.max(1.0 / w).max(1.0 / h);
+ if matches!(n, 0 | 3) {
+ rect[0] = rect[2] - w * f;
+ } else {
+ rect[2] = rect[0] + w * f;
+ }
+ if matches!(n, 0 | 1) {
+ rect[1] = rect[3] - h * f;
+ } else {
+ rect[3] = rect[1] + h * f;
+ }
+ }
+ return Self::Size { rect, from, size: s.size };
+ };
if is_paragraph(t) {
// a box without a height is as tall as its text
let from = if t.box_height > 0.0 { [0.0, 0.0, t.box_width, t.box_height] } else { [0.0, v.local[1], t.box_width, v.local[3]] };
- let (dx, dy) = match (v.to_local(start), v.to_local(cur)) {
- (Some(a), Some(b)) => (b.0 - a.0, b.1 - a.1),
- _ => (0.0, 0.0),
- };
- // the handle follows the pointer and the opposite side stays; the box does not flip
- let min = (t.size * 0.5).max(1.0);
- let [mut x0, mut y0, mut x1, mut y1] = from;
- if matches!(n, 0 | 3 | 7) {
- x0 = (x0 + dx).min(x1 - min);
- }
- if matches!(n, 1 | 2 | 5) {
- x1 = (x1 + dx).max(x0 + min);
- }
- if matches!(n, 0 | 1 | 4) {
- y0 = (y0 + dy).min(y1 - min);
- }
- if matches!(n, 2 | 3 | 6) {
- y1 = (y1 + dy).max(y0 + min);
- }
- return Self::Resize { rect: [x0, y0, x1, y1], from, tall: t.box_height > 0.0 || !matches!(n, 5 | 7) };
+ let rect = drag_sides(from, n, dx, dy, (t.size * 0.5).max(1.0));
+ return Self::Resize { rect, from, tall: t.box_height > 0.0 || !matches!(n, 5 | 7) };
}
// Every handle scales both axes alike. Only the pointer's travel along one side of the box
// counts: along its width for the two side handles, along its height for all the others.
@@ -305,12 +347,14 @@ impl HandleDrag {
match self {
Self::Stretch(h) => v.quad().map(|q| h.apply(q)),
Self::Scale { anchor, f } => v.quad().map(|q| *anchor + (q - *anchor) * *f),
- Self::Resize { rect: b, .. } => [(b[0], b[1]), (b[2], b[1]), (b[2], b[3]), (b[0], b[3])].map(|(x, y)| sp(&v.to_screen, x, y)),
+ Self::Resize { rect: b, .. } | Self::Size { rect: b, .. } => {
+ [(b[0], b[1]), (b[2], b[1]), (b[2], b[3]), (b[0], b[3])].map(|(x, y)| sp(&v.to_screen, x, y))
+ }
}
}
/// The `graphics.set` properties that carry out the drag.
- fn props(&self, v: &LayerView, n: usize) -> Value {
+ fn props(&self, v: &LayerView) -> Value {
let tr = &v.spec.transform;
match self {
Self::Stretch(h) => {
@@ -322,8 +366,7 @@ impl HandleDrag {
"scale_width": tr.scale.x * 100.0 * h.fx as f64,
"position": [tr.position.x + delta.x, tr.position.y + delta.y],
});
- if n >= 4 {
- // an edge stretches one axis only
+ if h.apart {
props["uniform_scale"] = json!(false);
}
props
@@ -341,6 +384,18 @@ impl HandleDrag {
}
props
}
+ Self::Size { rect, from, size } => {
+ // The box scales with Size about the layer's origin, so the origin moves to `o` (in
+ // the old layer pixels) to put the box at `rect`. The anchor point is renumbered so
+ // that it, the Position and the side that was not dragged stay where they are.
+ let f = |a: f32, b: f32| if b > 0.0 { a / b } else { 1.0 };
+ let (fx, fy) = (f(rect[2] - rect[0], from[2] - from[0]), f(rect[3] - rect[1], from[3] - from[1]));
+ let o = (rect[0] - from[0] * fx, rect[1] - from[1] * fy);
+ json!({
+ "size": [size.0 * fx, size.1 * fy],
+ "anchor": [tr.anchor.x - o.0 as f64, tr.anchor.y - o.1 as f64],
+ })
+ }
}
}
}
@@ -561,7 +616,7 @@ pub fn monitor_overlay(app: &mut FilmcraftApp, ui: &mut egui::Ui, pic: Rect, fra
}
DragKind::Handle(n) => {
if let Some(v) = v {
- let h = HandleDrag::new(v, n, d.start, cur);
+ let h = HandleDrag::new(v, n, d.start, cur, ui.input(|i| i.modifiers.shift));
painter.add(egui::Shape::closed_line(h.quad(v).to_vec(), Stroke::new(1.0, Color32::WHITE)));
}
}
@@ -614,7 +669,7 @@ pub fn monitor_overlay(app: &mut FilmcraftApp, ui: &mut egui::Ui, pic: Rect, fra
}
DragKind::Handle(n) if moved => {
if let Some(v) = v {
- let props = HandleDrag::new(v, n, d.start, cur).props(v, n);
+ let props = HandleDrag::new(v, n, d.start, cur, ui.input(|i| i.modifiers.shift)).props(v);
actions.push(("graphics.set".into(), json!({"clip": v.clip.0, "layer": v.layer, "props": props})));
}
}
diff --git a/crates/ui-egui/tests/graphics_handles_ui.rs b/crates/ui-egui/tests/graphics_handles_ui.rs
index 538798521..b7553926b 100644
--- a/crates/ui-egui/tests/graphics_handles_ui.rs
+++ b/crates/ui-egui/tests/graphics_handles_ui.rs
@@ -1,6 +1,6 @@
//! Headless UI tests of the handles of graphic layers in the Program monitor: point text scales
-//! about its anchor point, a paragraph-text box is resized, a shape stretches away from its
-//! opposite side (docs/graphics.md).
+//! about its anchor point, a paragraph-text box is resized, a shape's Size follows the handle
+//! while its opposite side and anchor point stay (docs/graphics.md).
//!
//! With `FILMCRAFT_UI_SHOTS=<dir>` the tests also render the UI with wgpu and save PNGs there.
@@ -130,6 +130,14 @@ impl Driver {
self.frames(4);
}
+ /// Press or let go of Shift, as a keyboard reports it (the control channel's drags carry
+ /// modifiers only on their press and release).
+ fn hold_shift(&mut self, on: bool) {
+ let m = if on { egui::Modifiers::SHIFT } else { egui::Modifiers::NONE };
+ self.harness.input_mut().events.push(egui::Event::ModifiersChanged(m));
+ self.frames(1);
+ }
+
fn drag_handle(&mut self, pre: &str, n: usize, dx: f64, dy: f64) {
let h = self.handle(pre, n);
self.drag(h, (dx, dy));
@@ -372,6 +380,58 @@ fn shape_corner_handles_scale_away_from_the_opposite_corner() {
assert!(bl2.0 < bl.0 - 20.0 && bl2.1 > bl.1 + 5.0, "bottom-left follows the pointer: {bl:?} -> {bl2:?}");
}
+/// Width and height of a layer's box in its own pixels.
+fn box_size(l: &Value) -> (f64, f64) {
+ let b = &l["localBounds"];
+ (num(&b[2]) - num(&b[0]), num(&b[3]) - num(&b[1]))
+}
+
+#[test]
+fn shape_handles_change_its_size_and_shift_keeps_the_ratio() {
+ let mut d = Driver::new();
+ let pre = d.shape();
+ let clip: u64 = pre.split('.').nth(2).unwrap().parse().unwrap();
+ // a corner straight right: only wider, as in Premiere; the Size changes, not the Scale
+ let (tl, br, anchor) = (d.handle(&pre, 0), d.handle(&pre, 2), d.at(&pre, "anchor"));
+ let before = d.layer(clip);
+ d.drag_handle(&pre, 2, 60.0, 0.0);
+ let l = d.layer(clip);
+ assert_eq!((num(&l["scale"]), num(&l["scaleWidth"])), (100.0, 100.0), "{l}");
+ let (w, h) = box_size(&l);
+ assert!(w > 510.0 && (h - 300.0).abs() < 1e-3, "wider, as tall: {w} x {h}");
+ let (tl2, br2) = (d.handle(&pre, 0), d.handle(&pre, 2));
+ assert!(near(tl, tl2), "top-left stays put: {tl:?} -> {tl2:?}");
+ assert!(near(br2, (br.0 + 60.0, br.1)), "bottom-right follows the pointer: {br:?} -> {br2:?}");
+ assert!(near(d.at(&pre, "anchor"), anchor), "the anchor point stays put");
+ assert_eq!(l["position"], before["position"]);
+
+ // with Shift both sides grow alike from the opposite corner
+ d.hold_shift(true);
+ d.drag_handle(&pre, 2, 40.0, 0.0);
+ d.hold_shift(false);
+ let l2 = d.layer(clip);
+ let (w2, h2) = box_size(&l2);
+ assert!(h2 > 310.0, "taller too: {w2} x {h2}");
+ assert!((w2 / h2 - w / h).abs() < 1e-3, "same proportions: {w}x{h} -> {w2}x{h2}");
+ assert!(near(d.handle(&pre, 0), tl), "top-left still stays put");
+ assert_eq!(num(&l2["scale"]), 100.0);
+ // one undo step per drag
+ d.exec("edit.undo", json!({}));
+ assert_eq!(box_size(&d.layer(clip)), (w, h));
+}
+
+#[test]
+fn polygon_handles_keep_the_opposite_corner() {
+ let mut d = Driver::new();
+ let r = d.exec("graphics.newPolygon", json!({"sides": 5, "position": [800, 500], "size": [300, 300]}));
+ let pre = d.select(r["clip"].as_u64().unwrap());
+ // a pentagon's box is not centred on the shape's origin; the top-left corner stays anyway
+ let (tl, br) = (d.handle(&pre, 0), d.handle(&pre, 2));
+ d.drag_handle(&pre, 2, 50.0, 40.0);
+ assert!(near(d.handle(&pre, 0), tl), "top-left stays put");
+ assert!(near(d.handle(&pre, 2), (br.0 + 50.0, br.1 + 40.0)), "bottom-right follows the pointer");
+}
+
#[test]
fn shape_edge_handles_stretch_one_axis() {
let mut d = Driver::new();
diff --git a/docs/graphics.md b/docs/graphics.md
index 433d491b2..fd04667d1 100644
--- a/docs/graphics.md
+++ b/docs/graphics.md
@@ -116,7 +116,7 @@ duration (a track is added if needed).
| Tool | On the Program monitor |
|---|---|
| Type (T) | Click empty picture: new point-text layer with a caret. Drag on empty picture: new paragraph-text layer with that box. Click a text layer: caret there. The box of the text being typed into is red. Type; ←/→ (⌥ word, ⌘ line), ↑/↓, Home/End, Shift to select, ⌘A, ⌘C/⌘X/⌘V, Return = new line, Backspace/Delete, Esc = stop editing. Drag inside the edited text to select. |
-| Selection (V) | Click a layer to select it (box with handles and anchor point; only selected layers have a box, and a layer whose visibility is off has none and cannot be clicked); drag to move; drag the anchor point to move it alone; drag a handle to scale point text about its anchor, resize a paragraph-text box, or stretch a shape away from its opposite side (see *Point text and paragraph text*); double-click a text layer to edit it. |
+| Selection (V) | Click a layer to select it (box with handles and anchor point; only selected layers have a box, and a layer whose visibility is off has none and cannot be clicked); drag to move; drag the anchor point to move it alone; drag a handle to scale point text about its anchor, resize a paragraph-text box (see *Point text and paragraph text*), or change a shape's Size: the dragged side or corner follows the pointer while the opposite one and the anchor point stay, Scale is untouched, and Shift on a corner keeps the proportions (a path, which has no Size, stretches instead); double-click a text layer to edit it. |
| Rectangle / Ellipse | Drag to draw a shape layer. |
| Pen (P) | Click to place points; click the first point (or Return) to close the path; Esc cancels. |