Skip to content

Commit be143ca

Browse files
committed
node from exposed input
1 parent 38dcbe9 commit be143ca

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

editor/src/messages/portfolio/document/node_graph/node_graph_message_handler.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,69 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
929929
};
930930
responses.add(FrontendMessage::UpdateWirePathInProgress { wire_path: Some(wire_path) });
931931
}
932+
} else if self.disconnecting.is_some() {
933+
// Disconnecting with no upstream node, create new value node.
934+
let to_connector = network_interface.input_connector_from_click(ipp.mouse.position, selection_network_path);
935+
if let Some(to_connector) = &to_connector {
936+
let Some(input_position) = network_interface.input_position(to_connector, selection_network_path) else {
937+
log::error!("Could not get input position for connector: {to_connector:?}");
938+
return;
939+
};
940+
self.wire_in_progress_to_connector = Some(input_position);
941+
}
942+
// Not hovering over a node input or node output, insert the node
943+
else {
944+
// Disconnect if the wire was previously connected to an input
945+
if let Some(disconnecting) = self.disconnecting.take() {
946+
let mut position = if let Some(to_connector) = self.wire_in_progress_to_connector { to_connector } else { point };
947+
// Offset to drag from center of node
948+
position = position - DVec2::new(24. * 3., 24.);
949+
950+
// Offset to account for division rounding error
951+
if position.x < 0. {
952+
position.x = position.x - 1.;
953+
}
954+
if position.y < 0. {
955+
position.y = position.y - 1.;
956+
}
957+
958+
let Some(input) = network_interface.take_input(&disconnecting, breadcrumb_network_path) else {
959+
return;
960+
};
961+
962+
let drag_start = DragStart {
963+
start_x: point.x,
964+
start_y: point.y,
965+
round_x: 0,
966+
round_y: 0,
967+
};
968+
969+
self.drag_start = Some((drag_start, false));
970+
self.node_has_moved_in_drag = false;
971+
self.update_node_graph_hints(responses);
972+
973+
let node_id = NodeId::new();
974+
responses.add(NodeGraphMessage::CreateNodeFromContextMenu {
975+
node_id: Some(node_id),
976+
node_type: "Identity".to_string(),
977+
xy: Some(((position.x / 24.) as i32, (position.y / 24.) as i32)),
978+
});
979+
980+
responses.add(NodeGraphMessage::SetInput {
981+
input_connector: InputConnector::node(node_id, 0),
982+
input,
983+
});
984+
985+
responses.add(NodeGraphMessage::CreateWire {
986+
output_connector: OutputConnector::Node { node_id, output_index: 0 },
987+
input_connector: disconnecting,
988+
});
989+
responses.add(NodeGraphMessage::SelectedNodesSet { nodes: vec![node_id] });
990+
// Update the frontend that the node is disconnected
991+
responses.add(NodeGraphMessage::RunDocumentGraph);
992+
responses.add(NodeGraphMessage::SendGraph);
993+
}
994+
}
932995
} else if let Some((drag_start, dragged)) = &mut self.drag_start {
933996
if drag_start.start_x != point.x || drag_start.start_y != point.y {
934997
*dragged = true;

editor/src/messages/portfolio/document/utility_types/network_interface.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,24 @@ impl NodeNetworkInterface {
480480
}
481481
}
482482

483+
pub fn take_input(&mut self, input_connector: &InputConnector, network_path: &[NodeId]) -> Option<NodeInput> {
484+
let Some(network) = self.network_mut(network_path) else {
485+
log::error!("Could not get network in input_from_connector");
486+
return None;
487+
};
488+
let input = match input_connector {
489+
InputConnector::Node { node_id, input_index } => {
490+
let Some(node) = network.nodes.get_mut(node_id) else {
491+
log::error!("Could not get node {node_id} in input_from_connector");
492+
return None;
493+
};
494+
node.inputs.get_mut(*input_index)
495+
}
496+
InputConnector::Export(export_index) => network.exports.get_mut(*export_index),
497+
};
498+
input.map(|input| std::mem::replace(input, NodeInput::value(TaggedValue::None, true)))
499+
}
500+
483501
/// Try and get the [`Type`] for any [`InputConnector`] based on the `self.resolved_types`.
484502
fn node_type_from_compiled(&mut self, input_connector: &InputConnector, network_path: &[NodeId]) -> Option<(Type, TypeSource)> {
485503
let (node_id, input_index) = match *input_connector {
@@ -1173,7 +1191,7 @@ impl NodeNetworkInterface {
11731191
};
11741192

11751193
if display_name.is_empty() {
1176-
if is_layer && *reference == Some("Merge".to_string()) {
1194+
if is_layer {
11771195
"Untitled Layer".to_string()
11781196
} else {
11791197
reference.clone().unwrap_or("Untitled Node".to_string())

0 commit comments

Comments
 (0)