Merge pull request #423 from cdinhoo/codex/effect-input-control-fixes

This commit is contained in:
Claude
2026-10-10 12:50:18 +00:00
12 changed files with 663 additions and 25 deletions
+92 -12
View File
@@ -37,6 +37,21 @@ const DIAL_ROW: f32 = 64.0;
const PARAM_NAME_X: f32 = 58.0;
const TREE_INDENT: f32 = 14.0;
/// Selecting any descendant activates its owning effect without replacing the property
/// selection used by keyframe editing, copy and delete.
fn effect_active(g: &PropGroup, layer: effectcraft_engine::project::LayerId, selected: &[(effectcraft_engine::project::LayerId, u64)]) -> bool {
fn contains(g: &PropGroup, uid: u64, depth: usize) -> bool {
if g.uid == uid {
return true;
}
if depth >= 16 {
return false;
}
g.children.iter().any(|n| n.uid() == uid || matches!(n, Node::Group(child) if contains(child, uid, depth + 1)))
}
selected.iter().any(|(l, uid)| *l == layer && contains(g, *uid, 0))
}
fn selected_layer(app: &EffectcraftApp) -> Option<Layer> {
let comp = app.session.active_comp()?;
let id = app.session.state.selected_layers.first()?;
@@ -165,6 +180,10 @@ fn prop_row(
let mut dims = vec![];
let cy = r.center().y;
let uid = prop.uid;
if ui.input(|i| i.pointer.primary_pressed()) && ui.rect_contains_pointer(r) {
let add = ui.input(|i| i.modifiers.shift || i.modifiers.command);
actions.push(("prop.select".into(), json!({"layer": layer.id.0, "prop": uid, "add": add})));
}
let swr = Rect::from_center_size(pos2(r.min.x + indent, cy), vec2(14.0, 14.0));
if !prop.static_only && !matches!(prop.ui, ParamUi::Hidden) {
let resp = ui.interact(swr, egui::Id::new(("ec-sw", uid)), Sense::click());
@@ -211,6 +230,8 @@ fn prop_row(
let name_clip = Rect::from_min_max(pos2(swr.max.x + 6.0, r.min.y), pos2(vx - 6.0, r.max.y));
app.auto.add(&format!("effectControls.prop.{uid}.name"), name_clip, &prop.name);
p.with_clip_rect(name_clip.intersect(p.clip_rect())).text(pos2(swr.max.x + 6.0, cy), Align2::LEFT_CENTER, &prop.name, Tokens::ui(12.0), t.text);
ui.interact(name_clip, egui::Id::new(("ec-prop-name", uid)), Sense::click());
app.auto.add(&format!("effectControls.prop.{uid}.name"), name_clip, &prop.name);
let merge = format!("ec-{uid}");
let set =
|actions: &mut Actions, v: serde_json::Value| actions.push(("prop.set".into(), json!({"layer": layer.id.0, "prop": uid, "value": v, "merge": merge})));
@@ -972,7 +993,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
// Clicking empty space deselects the effects.
let bg = ui.interact(body, egui::Id::new("ec-bg"), Sense::click());
if bg.clicked() {
app.session.state.selected_props.retain(|(l, u)| !(*l == layer.id && fx.iter().any(|g| g.uid == *u)));
app.session.state.selected_props.retain(|(l, u)| !(*l == layer.id && fx.iter().any(|g| effect_active(g, layer.id, &[(*l, *u)]))));
}
let has_clip = !app.session.state.effect_clipboard.is_empty();
let drag_id = egui::Id::new("ec-drag-effect");
@@ -986,7 +1007,8 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
y += ROW + 2.0;
let open = !app.ui.fx_closed.contains(&g.uid);
let selected = app.session.state.selected_props.iter().any(|(l, u)| *l == layer.id && *u == g.uid);
bp.rect_filled(r, 0.0, if selected { Color32::from_rgb(0x2f, 0x3a, 0x52) } else { Color32::from_rgb(0x2a, 0x2a, 0x2a) });
let active = effect_active(g, layer.id, &app.session.state.selected_props);
bp.rect_filled(r, 0.0, if active { Color32::from_rgb(0x2f, 0x3a, 0x52) } else { Color32::from_rgb(0x2a, 0x2a, 0x2a) });
let fxr = Rect::from_center_size(pos2(r.min.x + 14.0, r.center().y), vec2(16.0, 16.0));
if widgets::icon_toggle(ui, fxr, Icon::Fx, g.enabled, &t, egui::Id::new(("ec-fx", g.uid)), Sense::click()).clicked() {
actions.push(("effect.toggle".into(), json!({"layer": layer.id.0, "effect": g.uid})));
@@ -1028,6 +1050,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
egui::Id::new(("ec-hdr", g.uid)),
Sense::click_and_drag(),
);
hresp.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::SelectableLabel, true, active, &g.name));
app.auto.add(&format!("effectControls.effect.{}", g.uid), r, &g.name);
if hresp.clicked() || hresp.drag_started() || hresp.secondary_clicked() {
let add = ui.input(|i| i.modifiers.shift || i.modifiers.command);
@@ -1077,6 +1100,11 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
let er = Rect::from_min_size(pos2(body.min.x, y), vec2(body.width(), eh));
y += eh;
if er.max.y >= body.min.y && er.min.y <= body.max.y {
// Curves, Levels and other custom editors do not use prop_row.
// Clicking their controls must activate the owner as well.
if ui.input(|i| i.pointer.primary_pressed()) && ui.rect_contains_pointer(er) {
actions.push(("prop.select".into(), json!({"layer":layer.id.0,"prop":g.uid})));
}
match effect.as_str() {
"ec.color.curves" => curves_editor(app, ui, &bp, &layer, g, &ectx, er, &mut actions),
effectcraft_engine::effects::warp_stab::ID => warp_editor(app, ui, &bp, &layer, g, er, &mut actions),
@@ -1153,11 +1181,29 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
// ---------------------------------------------------------------------------------------------
// Viewer hook
/// Collect point controls through visible parameter groups. Hidden optional slots do not
/// leave stray viewport handles. The depth cap also bounds malformed project trees.
fn visible_points<'a>(g: &'a PropGroup, scope: &FxScope<'_>, layer: &Layer, ectx: &EvalCtx, depth: usize, out: &mut Vec<&'a Property>) {
if depth > 16 {
return;
}
for node in &g.children {
match node {
Node::Prop(p) if matches!(p.ui, ParamUi::Point) && scope.shown(layer, ectx, &p.match_id) => out.push(p),
Node::Group(child) if child.enabled && scope.shown(layer, ectx, &child.match_id) => {
visible_points(child, &scope.sub(&child.match_id), layer, ectx, depth + 1, out)
}
_ => {}
}
}
}
/// Effect Controls' part of the Composition viewer, called by the viewer after its own
/// interaction (so these controls sit on top):
///
/// - while a crosshair / eyedropper pick is armed, the next click in the viewer sets the point
/// parameter (converted to the layer's space) or samples the frame's colour; Esc cancels;
/// - while a crosshair pick is armed, its handle previews the cursor position without editing
/// the property; the next click sets the point in layer space, and Esc cancels;
/// - an eyedropper pick samples the frame's colour on click;
/// - otherwise, the point parameters of the selected effects are drawn as draggable ⊕ controls.
///
/// `l2c` is the viewer's layer→comp matrix (it knows the 3D view).
@@ -1172,6 +1218,7 @@ pub fn viewer_hook(
let t = app.tokens;
let ctx = ui.ctx().clone();
let mut actions: Actions = vec![];
let mut point_preview = None;
if let Some(pick) = app.ui.fx_pick.clone() {
let Some(layer) = ectx.comp.layer(effectcraft_engine::project::LayerId(pick.layer)).cloned() else {
app.ui.fx_pick = None;
@@ -1197,6 +1244,9 @@ pub fn viewer_hook(
ctx.set_cursor_icon(egui::CursorIcon::Crosshair);
let c = map.to_comp(hp);
app.pointer_comp = Some([c[0] as f32, c[1] as f32]);
if pick.kind == "point" && fw::comp_to_layer(&l2c(ectx, &layer), c).is_some() {
point_preview = Some(hp);
}
if pick.kind == "color"
&& keyer.is_none()
&& let Some(img) = &app.viewer_image
@@ -1238,23 +1288,51 @@ pub fn viewer_hook(
app.ui.fx_pick = None;
app.ui.status.clear();
}
} else if app.ui.viewer.show_layer_controls {
}
let picking = app.ui.fx_pick.is_some();
let point_pick = app.ui.fx_pick.clone().filter(|p| p.kind == "point");
if app.ui.viewer.show_layer_controls || point_pick.is_some() {
// ⊕ controls for the point parameters of the selected effects.
let sel = app.session.state.selected_props.clone();
let mut sel = app.session.state.selected_props.clone();
if let Some(pick) = &point_pick {
sel.push((effectcraft_engine::project::LayerId(pick.layer), pick.prop));
}
let selected = app.session.state.selected_layers.clone();
for layer in ectx.comp.layers.iter().filter(|l| selected.contains(&l.id) && l.is_active_at(ectx.time)) {
for layer in ectx
.comp
.layers
.iter()
.filter(|l| (selected.contains(&l.id) || point_pick.as_ref().is_some_and(|p| p.layer == l.id.0)) && l.is_active_at(ectx.time))
{
let Some(fx) = layer.effects() else { continue };
let m = l2c(ectx, layer);
// Masks are in layer space, effect points in effect space.
let fm = super::viewer::from_effect_space(ectx, layer, m);
for g in fx.groups().filter(|g| g.enabled && sel.iter().any(|(l, u)| *l == layer.id && *u == g.uid)) {
if matches!(&g.kind, GroupKind::Effect { effect } if effect == super::fx_editors::RESHAPE) {
let picked_prop = point_pick.as_ref().filter(|p| p.layer == layer.id.0).map(|p| p.prop);
for g in fx.groups().filter(|g| g.enabled && effect_active(g, layer.id, &sel)) {
// Puppet owns its pin handles and hit testing. Generic crosshairs would
// overlap the pins and steal their selection/rotation/scale gestures.
if matches!(&g.kind, GroupKind::Effect { effect } if effect == effectcraft_engine::effects::puppet::ID) {
continue;
}
if !picking && app.ui.viewer.show_layer_controls && matches!(&g.kind, GroupKind::Effect { effect } if effect == super::fx_editors::RESHAPE) {
super::fx_editors::reshape_overlay(app, ui, painter, map, ectx, layer, g, &m, &mut actions);
}
let pts: Vec<&Property> = g.props().filter(|p| matches!(p.ui, ParamUi::Point)).collect();
let scope = FxScope { effect: &g.match_id, root: g, path: String::new() };
let mut pts = Vec::new();
visible_points(g, &scope, layer, ectx, 0, &mut pts);
// An explicit pick stays visible even when View > Show Layer Controls is off.
if !app.ui.viewer.show_layer_controls {
pts.retain(|pr| picked_prop == Some(pr.uid));
}
let screen: Vec<egui::Pos2> = pts
.iter()
.map(|pr| {
if picked_prop == Some(pr.uid)
&& let Some(pos) = point_preview
{
return pos;
}
let v = ectx.value(layer, pr).as_vec2();
map.to_screen(fw::layer_to_comp(&fm, v))
})
@@ -1266,11 +1344,13 @@ pub fn viewer_hook(
for (pr, s) in pts.iter().zip(screen) {
let hr = Rect::from_center_size(s, vec2(16.0, 16.0));
let id = egui::Id::new(("viewer-fx-pt", pr.uid));
let resp = ui.interact(hr, id, Sense::drag());
let col = if resp.hovered() || resp.dragged() { t.accent } else { Color32::WHITE };
// The picker owns the click. Preview handles must not steal it or start a drag.
let resp = (!picking).then(|| ui.interact(hr, id, Sense::drag()));
let col = if picked_prop == Some(pr.uid) || resp.as_ref().is_some_and(|r| r.hovered() || r.dragged()) { t.accent } else { Color32::WHITE };
fw::draw_crosshair(painter, s + vec2(1.0, 1.0), 5.0, Color32::from_black_alpha(160));
fw::draw_crosshair(painter, s, 5.0, col);
app.auto.add(&format!("viewer.effectPoint.{}", pr.uid), hr, &pr.name);
let Some(resp) = resp else { continue };
if resp.hovered() {
ctx.set_cursor_icon(egui::CursorIcon::Move);
resp.clone().on_hover_text(format!("{}: {}", g.name, pr.name));
+150
View File
@@ -424,3 +424,153 @@ fn extractor_offers_the_exr_layers_and_channels() {
assert_eq!(values(&h)[0], "depth.Z");
let _ = std::fs::remove_file(path);
}
#[test]
fn point_picker_handle_follows_hover_then_commits_once_across_effects() {
for (effect, parameter) in [("ec.control.point", "point"), ("ec.blur.ccradialfast", "center"), ("ec.generate.gradientramp", "start")] {
let mut s = Session::default();
s.execute("comp.new", json!({"width":640,"height":360,"duration":1})).unwrap();
let lid = s.execute("layer.newSolid", json!({"width":200,"height":100,"color":"#406080"})).unwrap()["layer"].as_u64().unwrap();
s.execute("prop.set", json!({"layer":lid,"path":"transform/scale","value":[200,200]})).unwrap();
let fx = s.execute("effect.apply", json!({"layer":lid,"effect":effect})).unwrap()["effects"][0].as_u64().unwrap();
let point = layer(&s, lid).effects().unwrap().find_group(fx).unwrap().get(parameter).unwrap().uid;
s.execute("prop.set", json!({"layer":lid,"prop":point,"value":[100,50]})).unwrap();
let mut app = EffectcraftApp::new(s);
app.show_panel(PanelKind::EffectControls);
app.ui.viewer.zoom = Some(0.85);
app.ui.viewer.pan = [25.0, -10.0];
let mut h = Harness::builder().with_size(egui::vec2(1700.0, 1100.0)).build_eframe(|_| app);
settle(&mut h);
let id = format!("viewer.effectPoint.{point}");
let original = rect_of(&h, &id).center();
let pick_button = rect_of(&h, &format!("effectControls.prop.{point}.crosshair")).center();
click(&mut h, pick_button);
assert!(h.state().ui.fx_pick.is_some(), "{effect}: crosshair arms the picker");
assert_eq!(rect_of(&h, &id).center(), original, "the handle remains visible before entering the viewer");
let undo = h.state().session.history.undo.len();
for comp in [[350.0, 200.0], [370.0, 220.0]] {
let pos = effectcraft_ui_egui::panels::viewer::comp_to_screen(&h.ctx, comp).unwrap();
hover(&mut h, pos);
assert!(rect_of(&h, &id).center().distance(pos) < 0.1, "{effect}: handle must follow the pointer before clicking");
assert_eq!(layer(&h.state().session, lid).props.find(point).unwrap().value.as_vec2(), [100.0, 50.0], "hover is only a preview");
assert_eq!(h.state().session.history.undo.len(), undo, "hover must not add undo entries");
}
let pos = effectcraft_ui_egui::panels::viewer::comp_to_screen(&h.ctx, [370.0, 220.0]).unwrap();
click(&mut h, pos);
assert!(h.state().ui.fx_pick.is_none());
let after = layer(&h.state().session, lid).props.find(point).unwrap().value.as_vec2();
assert!((after[0] - 125.0).abs() < 1.0 && (after[1] - 70.0).abs() < 1.0, "{effect}: click uses layer space: {after:?}");
assert_eq!(h.state().session.history.undo.len(), undo + 1, "one click is one edit");
h.state_mut().session.execute("edit.undo", json!({})).unwrap();
assert_eq!(layer(&h.state().session, lid).props.find(point).unwrap().value.as_vec2(), [100.0, 50.0]);
assert_eq!(layer(&h.state().session, lid).props.prop("transform/position").unwrap().value.as_vec2(), [320.0, 180.0]);
}
}
#[test]
fn point_picker_preview_cancels_and_works_with_layer_controls_hidden() {
let (app, x) = app();
let mut h = Harness::builder().with_size(egui::vec2(1700.0, 1100.0)).build_eframe(|_| app);
settle(&mut h);
let value = layer(&h.state().session, x.small).props.find(x.point).unwrap().value.clone();
let undo = h.state().session.history.undo.len();
h.state_mut().ui.viewer.show_layer_controls = false;
let pick_button = rect_of(&h, &format!("effectControls.prop.{}.crosshair", x.point)).center();
click(&mut h, pick_button);
let id = format!("viewer.effectPoint.{}", x.point);
let original = rect_of(&h, &id).center();
let pos = effectcraft_ui_egui::panels::viewer::comp_to_screen(&h.ctx, [370.0, 220.0]).unwrap();
hover(&mut h, pos);
assert!(rect_of(&h, &id).center().distance(pos) < 0.1, "explicit picks show a handle even with layer controls hidden");
hover(&mut h, pick_button);
assert_eq!(rect_of(&h, &id).center(), original, "leaving the viewer restores the current position");
hover(&mut h, pos);
h.key_press(egui::Key::Escape);
h.run_steps(3);
assert!(h.state().ui.fx_pick.is_none());
assert!(!ids(&h).contains(&id), "cancel removes the temporary handle");
assert_eq!(layer(&h.state().session, x.small).props.find(x.point).unwrap().value, value);
assert_eq!(h.state().session.history.undo.len(), undo, "cancel leaves the project unchanged");
}
#[test]
fn radial_center_stays_draggable_when_its_property_is_selected() {
let (mut app, x) = app();
app.session.execute("effect.apply", json!({"layer":x.small,"effect":"ec.blur.ccradialfast"})).unwrap();
let l = layer(&app.session, x.small);
let effect = l.effects().unwrap().groups().find(|g| g.match_id == "ec.blur.ccradialfast").unwrap();
let point = effect.get("center").unwrap().uid;
app.session.state.selected_props = vec![(LayerId(x.small), point)];
let mut h = Harness::builder().with_size(egui::vec2(1700.0, 1100.0)).build_eframe(|_| app);
settle(&mut h);
let id = format!("viewer.effectPoint.{point}");
assert!(ids(&h).contains(&id), "selected point must keep its handle");
let from = rect_of(&h, &id).center();
let to = from + egui::vec2(40.0, 20.0);
h.event(egui::Event::PointerMoved(from));
h.step();
h.event(egui::Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
h.step();
for i in 1..=5 {
h.event(egui::Event::PointerMoved(from + (to - from) * (i as f32 / 5.0)));
h.step();
}
h.event(egui::Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
h.step();
let after = layer(&h.state().session, x.small).props.find(point).unwrap().value.as_vec2();
assert!(after[0] > 100.0 && after[1] > 50.0, "drag updates Center: {after:?}");
let position = layer(&h.state().session, x.small).props.prop("transform/position").unwrap().value.as_vec3();
assert_eq!(position[..2], [320.0, 180.0], "dragging the effect must not move the layer");
h.state_mut().session.execute("edit.undo", json!({})).unwrap();
assert_eq!(layer(&h.state().session, x.small).props.find(point).unwrap().value.as_vec2(), [100.0, 50.0], "one Undo reverses the gesture");
}
#[test]
fn clicking_an_effect_parameter_activates_its_header_and_handles() {
let (app, x) = app();
let mut h = Harness::builder().with_size(egui::vec2(1700.0, 1100.0)).build_eframe(|_| app);
settle(&mut h);
let name = rect_of(&h, &format!("effectControls.prop.{}.name", x.point)).center();
click(&mut h, name);
assert_eq!(h.state().session.state.selected_props, vec![(LayerId(x.small), x.point)]);
assert!(ids(&h).contains(&format!("viewer.effectPoint.{}", x.point)), "clicking the parameter activates the owner effect");
}
#[test]
fn non_point_parameters_activate_handles_across_effect_families() {
for (effect, parameter, points) in [
("ec.distort.cclens", "size", vec!["center"]),
("ec.generate.gradientramp", "startColor", vec!["start", "end"]),
("ec.generate.beam", "length", vec!["startPoint", "endPoint"]),
] {
let mut s = Session::default();
s.execute("comp.new", json!({"width":320,"height":180,"duration":1})).unwrap();
let lid = s.execute("layer.newSolid", json!({"color":"#406080"})).unwrap()["layer"].as_u64().unwrap();
let uid = s.execute("effect.apply", json!({"layer":lid,"effect":effect})).unwrap()["effects"][0].as_u64().unwrap();
let l = layer(&s, lid);
let g = l.effects().unwrap().find_group(uid).unwrap();
let prop = g.prop(parameter).unwrap().uid;
let point_ids: Vec<_> = points.iter().map(|name| g.prop(name).unwrap().uid).collect();
s.state.selected_props.clear();
let mut app = EffectcraftApp::new(s);
app.show_panel(PanelKind::EffectControls);
let mut h = Harness::builder().with_size(egui::vec2(1700.0, 1100.0)).build_eframe(|_| app);
settle(&mut h);
let name = rect_of(&h, &format!("effectControls.prop.{prop}.name")).center();
click(&mut h, name);
assert_eq!(h.state().session.state.selected_props, vec![(LayerId(lid), prop)], "{effect}");
for point in point_ids {
assert!(ids(&h).contains(&format!("viewer.effectPoint.{point}")), "{effect}: all its point handles activate");
}
}
}
#[test]
fn custom_effect_editors_activate_their_owner() {
let (app, x) = app();
let mut h = Harness::builder().with_size(egui::vec2(1700.0, 1100.0)).build_eframe(|_| app);
settle(&mut h);
let graph = rect_of(&h, &format!("effectControls.effect.{}.curves.graph", x.curves)).center();
click(&mut h, graph);
assert!(h.state().session.state.selected_props.contains(&(LayerId(x.small), x.curves)));
}