Skip to content

Commit 7b5ae89

Browse files
committed
Identity from value input
1 parent a182a73 commit 7b5ae89

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
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
@@ -903,6 +903,69 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
903903
};
904904
responses.add(FrontendMessage::UpdateWirePathInProgress { wire_path: Some(wire_path) });
905905
}
906+
} else if self.disconnecting.is_some() {
907+
// Disconnecting with no upstream node, create new value node.
908+
let to_connector = network_interface.input_connector_from_click(ipp.mouse.position, selection_network_path);
909+
if let Some(to_connector) = &to_connector {
910+
let Some(input_position) = network_interface.input_position(to_connector, selection_network_path) else {
911+
log::error!("Could not get input position for connector: {to_connector:?}");
912+
return;
913+
};
914+
self.wire_in_progress_to_connector = Some(input_position);
915+
}
916+
// Not hovering over a node input or node output, insert the node
917+
else {
918+
// Disconnect if the wire was previously connected to an input
919+
if let Some(disconnecting) = self.disconnecting.take() {
920+
let mut position = if let Some(to_connector) = self.wire_in_progress_to_connector { to_connector } else { point };
921+
// Offset to drag from center of node
922+
position = position - DVec2::new(24. * 3., 24.);
923+
924+
// Offset to account for division rounding error
925+
if position.x < 0. {
926+
position.x = position.x - 1.;
927+
}
928+
if position.y < 0. {
929+
position.y = position.y - 1.;
930+
}
931+
932+
let Some(input) = network_interface.take_input(&disconnecting, breadcrumb_network_path) else {
933+
return;
934+
};
935+
936+
let drag_start = DragStart {
937+
start_x: point.x,
938+
start_y: point.y,
939+
round_x: 0,
940+
round_y: 0,
941+
};
942+
943+
self.drag_start = Some((drag_start, false));
944+
self.node_has_moved_in_drag = false;
945+
self.update_node_graph_hints(responses);
946+
947+
let node_id = NodeId::new();
948+
responses.add(NodeGraphMessage::CreateNodeFromContextMenu {
949+
node_id: Some(node_id),
950+
node_type: "Identity".to_string(),
951+
xy: Some(((position.x / 24.) as i32, (position.y / 24.) as i32)),
952+
});
953+
954+
responses.add(NodeGraphMessage::SetInput {
955+
input_connector: InputConnector::node(node_id, 0),
956+
input,
957+
});
958+
959+
responses.add(NodeGraphMessage::CreateWire {
960+
output_connector: OutputConnector::Node { node_id, output_index: 0 },
961+
input_connector: disconnecting,
962+
});
963+
responses.add(NodeGraphMessage::SelectedNodesSet { nodes: vec![node_id] });
964+
// Update the frontend that the node is disconnected
965+
responses.add(NodeGraphMessage::RunDocumentGraph);
966+
responses.add(NodeGraphMessage::SendGraph);
967+
}
968+
}
906969
} else if let Some((drag_start, dragged)) = &mut self.drag_start {
907970
if drag_start.start_x != point.x || drag_start.start_y != point.y {
908971
*dragged = true;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,24 @@ impl NodeNetworkInterface {
479479
resolve_document_node_type(metadata.persistent_metadata.reference.as_ref()?)
480480
}
481481

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

0 commit comments

Comments
 (0)