Skip to content

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

graphics: a corner handle scales the layer away from the opposite corner

justjohn12345c80d23fd2e58
diff --git a/crates/ui-egui/src/panels/graphics.rs b/crates/ui-egui/src/panels/graphics.rs
index 6a4c16ba0..d885dbcd5 100644
--- a/crates/ui-egui/src/panels/graphics.rs
+++ b/crates/ui-egui/src/panels/graphics.rs
@@ -150,8 +150,8 @@ fn next_boundary(s: &str, i: usize) -> usize {
#[derive(Clone, Copy, Debug)]
enum DragKind {
Move,
- /// A corner handle: scales about the layer's anchor.
- Scale,
+ /// Corner handle `n` of the layer's box: scales it with the opposite corner held in place.
+ Scale(usize),
NewShape,
TextSelect,
}
@@ -164,6 +164,13 @@ struct DragState {
start: Pos2,
}
+/// Dragging corner `n` of `quad` from `start` to `cur`: the corner that stays put (the opposite
+/// one) and the scale factor, so the box grows towards the pointer.
+fn corner_scale(quad: &[Pos2; 4], n: usize, start: Pos2, cur: Pos2) -> (Pos2, f32) {
+ let pin = quad[(n + 2) % 4];
+ (pin, ((cur - pin).length() / (start - pin).length().max(1.0)).max(0.01))
+}
+
const TEXT_EDIT_ID: &str = "gfx-text-edit";
fn end_edit(app: &mut FilmcraftApp, ui: &egui::Ui) {
@@ -276,9 +283,9 @@ pub fn monitor_overlay(app: &mut FilmcraftApp, ui: &mut egui::Ui, pic: Rect, fra
let handle = views
.iter()
.filter(|v| Some(v.clip) == sel_clip && sel_layers.contains(&v.layer))
- .find_map(|v| v.quad().iter().position(|c| (*c - p).length() <= 7.0).map(|_| (v.clip, v.layer)));
- if let Some((c, l)) = handle {
- drag = Some(DragState { kind: DragKind::Scale, clip: c, layer: l, start: p });
+ .find_map(|v| v.quad().iter().position(|c| (*c - p).length() <= 7.0).map(|n| (v.clip, v.layer, n)));
+ if let Some((c, l, n)) = handle {
+ drag = Some(DragState { kind: DragKind::Scale(n), clip: c, layer: l, start: p });
} else if tool == Tool::Selection
&& let Some(v) = views.iter().find(|v| v.hit(p))
{
@@ -324,11 +331,10 @@ pub fn monitor_overlay(app: &mut FilmcraftApp, ui: &mut egui::Ui, pic: Rect, fra
crate::panels::monitor_view::draw_snap_lines(&painter, pic, &snap_off.1);
}
}
- DragKind::Scale => {
+ DragKind::Scale(n) => {
if let Some(v) = v {
- let a = sp(&v.to_screen, v.spec.transform.anchor.x as f32, v.spec.transform.anchor.y as f32);
- let f = ((cur - a).length() / (d.start - a).length().max(1.0)).max(0.01);
- painter.add(egui::Shape::closed_line(v.quad().iter().map(|q| a + (*q - a) * f).collect(), Stroke::new(1.0, Color32::WHITE)));
+ let (pin, f) = corner_scale(&v.quad(), n, d.start, cur);
+ painter.add(egui::Shape::closed_line(v.quad().iter().map(|q| pin + (*q - pin) * f).collect(), Stroke::new(1.0, Color32::WHITE)));
}
}
DragKind::NewShape => {
@@ -367,14 +373,19 @@ pub fn monitor_overlay(app: &mut FilmcraftApp, ui: &mut egui::Ui, pic: Rect, fra
));
}
}
- DragKind::Scale if moved => {
+ DragKind::Scale(n) if moved => {
if let Some(v) = v {
+ // the layer scales about its anchor, so it also moves to keep the pinned corner still
let a = sp(&v.to_screen, v.spec.transform.anchor.x as f32, v.spec.transform.anchor.y as f32);
- let f = ((cur - a).length() / (d.start - a).length().max(1.0)).max(0.01) as f64;
- let s = v.spec.transform.scale;
+ let (pin, f) = corner_scale(&v.quad(), n, d.start, cur);
+ let off = (pin - a) * (1.0 - f);
+ let lin = Affine { e: 0.0, f: 0.0, ..v.canvas_to_screen };
+ let delta = lin.inverse().map(|i| i.apply(Vec2::new(off.x as f64, off.y as f64))).unwrap_or_default();
+ let (s, p0, f) = (v.spec.transform.scale, v.spec.transform.position, f as f64);
actions.push((
"graphics.set".into(),
- json!({"clip": v.clip.0, "layer": v.layer, "props": {"scale": s.y * 100.0 * f, "scale_width": s.x * 100.0 * f}}),
+ json!({"clip": v.clip.0, "layer": v.layer, "props": {
+ "scale": s.y * 100.0 * f, "scale_width": s.x * 100.0 * f, "position": [p0.x + delta.x, p0.y + delta.y]}}),
));
}
}
diff --git a/crates/ui-egui/tests/graphics_handles_ui.rs b/crates/ui-egui/tests/graphics_handles_ui.rs
new file mode 100644
index 000000000..2c8b18a61
--- /dev/null
+++ b/crates/ui-egui/tests/graphics_handles_ui.rs
@@ -0,0 +1,87 @@
+//! Headless UI test of corner-handle scaling of graphic layers in the Program monitor.
+
+use std::sync::mpsc::{Sender, channel};
+
+use egui_kittest::Harness;
+use filmcraft_engine::Session;
+use filmcraft_ui_egui::FilmcraftApp;
+use filmcraft_ui_egui::control::ControlRequest;
+use serde_json::{Value, json};
+
+struct Driver {
+ harness: Harness<'static, FilmcraftApp>,
+ tx: Sender<ControlRequest>,
+}
+
+impl Driver {
+ fn new() -> Self {
+ let mut s = Session::default();
+ s.execute("file.openDemoProject", json!({})).unwrap();
+ let (tx, rx) = channel();
+ let app = FilmcraftApp::new(s).with_control(rx);
+ let harness = Harness::builder().with_size(egui::vec2(1600.0, 980.0)).with_step_dt(1.0 / 60.0).with_max_steps(10_000).build_eframe(move |_cc| app);
+ let mut d = Driver { harness, tx };
+ d.frames(4);
+ d
+ }
+
+ fn frames(&mut self, n: usize) {
+ for _ in 0..n {
+ // the control channel's clicks and keys enter through the input hook
+ let ctx = self.harness.ctx.clone();
+ let mut raw = std::mem::take(self.harness.input_mut());
+ eframe::App::raw_input_hook(self.harness.state_mut(), &ctx, &mut raw);
+ *self.harness.input_mut() = raw;
+ self.harness.step();
+ }
+ }
+
+ fn call(&mut self, method: &str, params: Value) -> Value {
+ let (req, reply) = ControlRequest::new(method, params.clone());
+ self.tx.send(req).unwrap();
+ for _ in 0..600 {
+ self.frames(1);
+ if let Ok(v) = reply.try_recv() {
+ return v;
+ }
+ }
+ panic!("no reply to {method} {params}");
+ }
+
+ fn ok(&mut self, method: &str, params: Value) -> Value {
+ let v = self.call(method, params.clone());
+ assert_eq!(v["ok"], json!(true), "{method} {params} failed: {v}");
+ v["result"].clone()
+ }
+
+ fn exec(&mut self, command: &str, params: Value) -> Value {
+ self.ok("engine.execute", json!({"command": command, "params": params}))
+ }
+}
+
+impl Driver {
+ fn rect(&mut self, id: &str) -> [f64; 4] {
+ let v = self.ok("ui.elements", json!({"prefix": id}));
+ let e = v.as_array().unwrap().iter().find(|e| e["id"] == json!(id)).unwrap_or_else(|| panic!("no element {id}")).clone();
+ let r = &e["rect"];
+ [r[0].as_f64().unwrap(), r[1].as_f64().unwrap(), r[2].as_f64().unwrap(), r[3].as_f64().unwrap()]
+ }
+}
+
+#[test]
+fn corner_handle_scales_away_from_the_opposite_corner() {
+ let mut d = Driver::new();
+ let r = d.exec("graphics.newText", json!({"text": "Title", "position": [600, 500], "size": 120}));
+ let clip = r["clip"].as_u64().unwrap();
+ d.exec("graphics.selectLayer", json!({"clip": clip, "layers": [0]}));
+ d.ok("ui.set", json!({"tool": "Selection"}));
+ d.frames(4);
+ let pre = format!("program.layer.{clip}.0.handle.");
+ let (tl, br) = (d.rect(&format!("{pre}0")), d.rect(&format!("{pre}2")));
+ let (bx, by) = (br[0] + br[2] / 2.0, br[1] + br[3] / 2.0);
+ d.ok("ui.drag", json!({"from": {"x": bx, "y": by}, "to": {"x": bx + 60.0, "y": by + 30.0}}));
+ d.frames(4);
+ let (tl2, br2) = (d.rect(&format!("{pre}0")), d.rect(&format!("{pre}2")));
+ assert!((tl2[0] - tl[0]).abs() < 1.5 && (tl2[1] - tl[1]).abs() < 1.5, "top-left stays put: {tl:?} -> {tl2:?}");
+ assert!(br2[0] > br[0] + 20.0 && br2[1] > br[1] + 5.0, "bottom-right follows the pointer down and right: {br:?} -> {br2:?}");
+}