diff --git a/crates/ui-egui/src/function/function_drag.rs b/crates/ui-egui/src/function/function_drag.rs index d8ee7cb..1117a3c 100644 --- a/crates/ui-egui/src/function/function_drag.rs +++ b/crates/ui-egui/src/function/function_drag.rs @@ -3,6 +3,7 @@ use egui::Vec2; pub(super) enum FunctionDrag { Node(u32, Vec2), Wire(u32), + InputWire(u32, usize), Box { start: egui::Pos2 }, Pan(egui::PointerButton), } diff --git a/crates/ui-egui/src/function/input.rs b/crates/ui-egui/src/function/input.rs index e0a805e..b5a376d 100644 --- a/crates/ui-egui/src/function/input.rs +++ b/crates/ui-egui/src/function/input.rs @@ -38,7 +38,7 @@ pub(super) fn handle( app.run("graph.annotation.select", serde_json::json!({})); } - // Gestures: wires from output dots, panning + // Gestures: wires from output dots and unconnected inputs, panning let near = |pointer: Pos2| crate::graph::port_hit::nearest_by(ports, pointer, |port| (port.position, port.radius)).map(|port| (port.node, port.slot)); if editor.drag.is_none() && GraphCamera::pan_started(ui, canvas_bounds) { editor.drag = Some(FunctionDrag::Pan(crate::graph::gesture::pan_button(ui))); @@ -47,9 +47,17 @@ pub(super) fn handle( && !app.graph.is_dragging() && let Some(pointer_position) = pointer && ui.rect_contains_pointer(canvas_bounds) - && let Some((id, None)) = near(pointer_position) + && let Some((id, slot)) = near(pointer_position) { - editor.drag = Some(FunctionDrag::Wire(id)); + match slot { + None => editor.drag = Some(FunctionDrag::Wire(id)), + Some(input) => { + let connected = editor.current(app).and_then(|graph| graph.node(id)).is_some_and(|node| node.inputs.get(input).is_some_and(Option::is_some)); + if !connected { + editor.drag = Some(FunctionDrag::InputWire(id, input)); + } + } + } } if editor.drag.is_none() @@ -76,6 +84,11 @@ pub(super) fn handle( crate::graph::wire_geometry::bezier(&painter, source_id.position, pointer_position, Stroke::new(2.0, tokens.accent)); } } + Some(FunctionDrag::InputWire(id, input)) => { + if let (Some(pointer_position), Some(destination)) = (pointer, ports.iter().find(|port| port.node == *id && port.slot == Some(*input))) { + crate::graph::wire_geometry::bezier(&painter, pointer_position, destination.position, Stroke::new(2.0, tokens.accent)); + } + } Some(FunctionDrag::Box { start }) => { if let Some(pointer) = pointer { crate::graph::node_selection::paint_box(&painter, *start, pointer, tokens.accent); @@ -113,6 +126,15 @@ pub(super) fn handle( editor.edit(app, FunctionEdit::Connect { from, to, input }, false); } } + Some(FunctionDrag::InputWire(to, input)) => { + // Dropped anywhere but on another node's output, the input stays unconnected + if let Some(pointer) = pointer + && let Some((from, None)) = near(pointer) + && from != to + { + editor.edit(app, FunctionEdit::Connect { from, to, input }, false); + } + } _ => {} } } diff --git a/crates/ui-egui/src/graph/canvas.rs b/crates/ui-egui/src/graph/canvas.rs index 19966e9..f57ee14 100644 --- a/crates/ui-egui/src/graph/canvas.rs +++ b/crates/ui-egui/src/graph/canvas.rs @@ -324,6 +324,11 @@ fn canvas(app: &mut MaterialCraftApp, ui: &mut Ui) { bezier(&painter, src.pos, p, Stroke::new(2.0, theme.accent)); } } + Some(GraphDrag::InputWire { to }) => { + if let (Some(p), Some(dst)) = (pointer, app.graph.ports.iter().find(|h| !h.output && h.node == to.0 && h.port == to.1)) { + bezier(&painter, p, dst.pos, Stroke::new(2.0, theme.accent)); + } + } _ => {} } diff --git a/crates/ui-egui/src/graph/graph_drag.rs b/crates/ui-egui/src/graph/graph_drag.rs index d2e73bb..ea463ae 100644 --- a/crates/ui-egui/src/graph/graph_drag.rs +++ b/crates/ui-egui/src/graph/graph_drag.rs @@ -14,6 +14,10 @@ pub(super) enum GraphDrag { from: (NodeId, String), detached: Option<(NodeId, String)>, }, + /// Pulled from an unconnected input: it connects where it is dropped on an output + InputWire { + to: (NodeId, String), + }, /// Pressed on a wire: a click selects it, a drag pulls a reroute dot out of it WirePress { wire: WireRef, diff --git a/crates/ui-egui/src/graph/input.rs b/crates/ui-egui/src/graph/input.rs index c2a9687..9f72bae 100644 --- a/crates/ui-egui/src/graph/input.rs +++ b/crates/ui-egui/src/graph/input.rs @@ -56,6 +56,8 @@ pub(super) fn handle( } else if let Some(connection) = app.session.graph().ok().and_then(|graph| graph.input_of(port.node, &port.port).cloned()) { app.graph.drag = Some(GraphDrag::Wire { from: (connection.from, connection.from_port.clone()), detached: Some((port.node, port.port.clone())) }); + } else { + app.graph.drag = Some(GraphDrag::InputWire { to: (port.node, port.port.clone()) }); } } else if let Some((id, _)) = comments.iter().rev().find(|(_, bounds)| bounds.contains(position)) { super::annotation_input::begin_comment(app, *id); @@ -116,7 +118,7 @@ pub(super) fn handle( } } GraphDrag::Wire { from, detached } => { - let target = pointer.and_then(|position| nearest_port(&app.graph.ports, position, 12.0)).filter(|port| !port.output).cloned(); + let target = drop_target(&app.graph.ports, pointer, false); match target { Some(target) => { let mut parameters = json!({"from": from.0.0, "fromPort": from.1, "to": target.node.0, "toPort": target.port}); @@ -137,6 +139,12 @@ pub(super) fn handle( } } } + GraphDrag::InputWire { to } => { + // Dropped anywhere but on another node's output, the input stays unconnected + if let Some(source) = drop_target(&app.graph.ports, pointer, true).filter(|source| source.node != to.0) { + app.run("graph.connect", json!({"from": source.node.0, "fromPort": source.port, "to": to.0.0, "toPort": to.1})); + } + } GraphDrag::Frame { .. } | GraphDrag::Comment { .. } => super::annotation_input::finish(app, drag), } } @@ -245,3 +253,8 @@ pub(super) fn handle( } } } + +/// The port a wire dropped at `pointer` lands on: the nearest port within reach, if it is an output (`output`) or an input +fn drop_target(ports: &[PortHit], pointer: Option, output: bool) -> Option { + pointer.and_then(|position| nearest_port(ports, position, 12.0)).filter(|port| port.output == output).cloned() +} diff --git a/crates/ui-egui/src/graph/interaction_tests.rs b/crates/ui-egui/src/graph/interaction_tests.rs index 8588328..3e07d79 100644 --- a/crates/ui-egui/src/graph/interaction_tests.rs +++ b/crates/ui-egui/src/graph/interaction_tests.rs @@ -122,15 +122,33 @@ fn standalone_function_canvas_wires_constants_and_marks_an_output() { function.output = Some(multiply); materialcraft_engine::execute(&mut session, "graph.function.set", &json!({"function": function})).unwrap(); let mut frames = Frames::new(session); + let port = + |frames: &Frames, node: u32, slot: &str| frames.app.graph.ports.iter().find(|port| port.node.0 == u64::from(node) && port.port == slot).unwrap().pos; + let wire = |frames: &mut Frames, (from, from_slot): (u32, &str), (to, to_slot): (u32, &str)| { + let (start, end) = (port(frames, from, from_slot), port(frames, to, to_slot)); + drag(frames, start, end); + }; - for (source, slot) in [(first, "0"), (second, "1")] { - let from = frames.app.graph.ports.iter().find(|port| port.node.0 == u64::from(source) && port.output).unwrap().pos; - let to = frames.app.graph.ports.iter().find(|port| port.node.0 == u64::from(multiply) && port.port == slot).unwrap().pos; - drag(&mut frames, from, to); - } + // An unconnected input dropped on empty canvas or on its own node's output stays unconnected + let input = port(&frames, multiply, "1"); + let empty = input + vec2(0.0, 160.0); + assert!(!frames.app.graph.rects.iter().any(|(_, rect)| rect.expand(12.0).contains(empty)), "the drop point must be empty canvas"); + let before = frames.app.session.pkg.clone(); + drag(&mut frames, input, empty); + wire(&mut frames, (multiply, "1"), (multiply, "output")); + assert_eq!(frames.app.session.pkg, before); + + // From an output to an input, and from an unconnected input to an output + wire(&mut frames, (first, "output"), (multiply, "0")); + wire(&mut frames, (multiply, "1"), (second, "output")); let function = frames.app.session.graph().unwrap().function.as_ref().unwrap(); assert_eq!(function.node(multiply).unwrap().inputs, vec![Some(first), Some(second)]); assert_eq!(function.output, Some(multiply)); + + // Pressing a connected input does not pull a wire + wire(&mut frames, (multiply, "0"), (second, "output")); + let function = frames.app.session.graph().unwrap().function.as_ref().unwrap(); + assert_eq!(function.node(multiply).unwrap().inputs, vec![Some(first), Some(second)]); } #[test] @@ -794,3 +812,58 @@ fn function_links_swap_and_delete_without_editing_the_parent_graph() { assert_eq!(frames.app.session.history(), history); } } + +#[test] +fn wires_pulled_from_an_unconnected_input_connect_on_an_output() { + let mut frames = Frames::new(new_session()); + let source = NodeId(frames.app.run("graph.node.add", json!({"def": "uniform", "at": [0, 0]})).unwrap()["id"].as_u64().unwrap()); + let target = NodeId(frames.app.run("graph.node.add", json!({"def": "blend", "at": [320, 0]})).unwrap()["id"].as_u64().unwrap()); + frames.app.ui.frame_all = true; + frames.run(Vec::new(), Modifiers::NONE); + let ports = |frames: &Frames, node: NodeId, output: bool| { + frames.app.graph.ports.iter().filter(|port| port.node == node && port.output == output).cloned().collect::>() + }; + let (inputs, output) = (ports(&frames, target, false), ports(&frames, source, true)[0].clone()); + let wires = |frames: &Frames| { + frames + .app + .session + .graph() + .unwrap() + .connections + .iter() + .map(|wire| (wire.from, wire.from_port.clone(), wire.to, wire.to_port.clone())) + .collect::>() + }; + let empty = frames.app.graph.view.left_bottom() + vec2(24.0, -24.0); + assert!(!frames.app.graph.rects.iter().any(|(_, rect)| rect.expand(12.0).contains(empty)), "the drop point must be empty canvas"); + + // A click, and drops on empty canvas, on another input and on the node's own output leave the input unconnected + let history = frames.app.session.history(); + frames.click(inputs[0].pos, Modifiers::NONE); + frames.time += 1.0; + for end in [empty, inputs[1].pos, ports(&frames, target, true)[0].pos] { + drag(&mut frames, inputs[0].pos, end); + } + assert!(wires(&frames).is_empty()); + assert!(frames.app.graph.search.is_none(), "an input wire dropped on empty canvas does not open the node search"); + assert_eq!(frames.app.session.history(), history, "a missed wire must not add an undo step"); + + // While dragging, the wire is pulled from the input; dropped on the output, it connects + let midway = inputs[0].pos.lerp(output.pos, 0.5); + frames.run(vec![Event::PointerMoved(inputs[0].pos)], Modifiers::NONE); + frames.run(vec![Event::PointerButton { pos: inputs[0].pos, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }], Modifiers::NONE); + frames.run(vec![Event::PointerMoved(midway)], Modifiers::NONE); + assert!(matches!(&frames.app.graph.drag, Some(super::GraphDrag::InputWire { to }) if *to == (target, inputs[0].port.clone()))); + frames.run(vec![Event::PointerMoved(output.pos)], Modifiers::NONE); + frames.run(vec![Event::PointerButton { pos: output.pos, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }], Modifiers::NONE); + assert_eq!(wires(&frames), [(source, output.port.clone(), target, inputs[0].port.clone())]); + + // The second input too; then the connected first input is unplugged by pulling its wire, as before + drag(&mut frames, inputs[1].pos, output.pos); + assert_eq!(wires(&frames)[1], (source, output.port.clone(), target, inputs[1].port.clone())); + drag(&mut frames, inputs[0].pos, empty); + assert_eq!(wires(&frames), [(source, output.port.clone(), target, inputs[1].port.clone())]); + frames.app.session.undo().unwrap(); + assert_eq!(wires(&frames).len(), 2); +} diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index f8f0a6c..d50f78b 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -25,7 +25,7 @@ automation + tests), weighted by how much of the day each area carries. **Remain | Area | Weight | Done | What works | Main gaps | |---|---:|---:|---|---| -| Graph editing | 15 | 75% | Pan/zoom canvas with grid; node cards with headers by category, live thumbnails and size/format labels; typed ports; bezier wires; drag to wire, re-route and unplug; select wires (click, Shift/Ctrl+click), delete them (Delete, Backspace, wire menu) and swap their sources (X); reroute dots pulled out of a wire (drag), dropped on it (double-click) or inserted from its menu; drop a wire on empty canvas to add a wired node; box/Ctrl/Shift selection; move; frames (move with contents, resize), comments (inline edit), navigation pins; node search (Space, double-click); library drag and drop; align and distribute; copy/cut/paste/duplicate (with or without links); delete and relink; disable; select upstream/downstream; create graph from selection; snapshot undo with merged slider drags | Dot-node chains editing, wire tension preferences, compact node display mode, multi-graph tabs, minimap | +| Graph editing | 15 | 75% | Pan/zoom canvas with grid; node cards with headers by category, live thumbnails and size/format labels; typed ports; bezier wires; drag to wire (from an output or an unconnected input), re-route and unplug; select wires (click, Shift/Ctrl+click), delete them (Delete, Backspace, wire menu) and swap their sources (X); reroute dots pulled out of a wire (drag), dropped on it (double-click) or inserted from its menu; drop a wire on empty canvas to add a wired node; box/Ctrl/Shift selection; move; frames (move with contents, resize), comments (inline edit), navigation pins; node search (Space, double-click); library drag and drop; align and distribute; copy/cut/paste/duplicate (with or without links); delete and relink; disable; select upstream/downstream; create graph from selection; snapshot undo with merged slider drags | Dot-node chains editing, wire tension preferences, compact node display mode, multi-graph tabs, minimap | | Atomic nodes | 12 | 95% | Bitmap, Blend (16 modes, masks, alpha), Blur, Channels Shuffle, Curve, Directional Blur, Directional Warp, Distance (with color propagation), Emboss, FX-Map (grid, quadtree and scatter modes with per-instance randomness), Gradient (Dynamic), Gradient Map, Grayscale Conversion, HSL, Levels, Normal, Pixel Processor, Value Processor, Sharpen, SVG (path data, fill rules, stroke), Text (own stroke font), Transformation 2D, Uniform Color, Warp, inputs, outputs, dots, graph instances | FX-Map driven by per-iteration functions, SVG arcs and SVG files, font choice for Text | | Node library | 20 | 45% | 227 nodes: noises (tileable 3D Perlin, simplex and Worley, cracks, stones, dirt, grunge maps and more), patterns (tile generator and sampler, bricks, hexagons, weave, shape splatter, paint strokes, scratches), filters (adjustments, blending, blurs, channels, effects, normal map tools, tiling, transforms), the flood fill family, whole-material and height blending, weathering (rust, moss, leaks, edge damage, edge wear, dust) | The rest of the reference library's ~400 graphs: more grunge and scan processing, mesh-based generators and bakers, spline tools, SDF functions | | Function graphs | 10 | 70% | A compiled, 4-wide evaluator with ~55 ops (constants, $pos/$size, exposed variables, samplers, vectors, swizzles, math, logic, comparison, noise, random, if/else); pixel and value processors; a visual function editor; dynamic parameters (a function on any numeric, color, bool or enum parameter, evaluated from the graph's inputs) | Loops, function graphs as reusable package items |