diff --git a/editor/src/messages/portfolio/document/node_graph/node_graph_message_handler.rs b/editor/src/messages/portfolio/document/node_graph/node_graph_message_handler.rs index 3c16ce2ec6..149b3f1596 100644 --- a/editor/src/messages/portfolio/document/node_graph/node_graph_message_handler.rs +++ b/editor/src/messages/portfolio/document/node_graph/node_graph_message_handler.rs @@ -650,6 +650,7 @@ impl<'a> MessageHandler> for NodeG insert_node_input_index, } => { network_interface.insert_node_between(&node_id, &input_connector, insert_node_input_index, selection_network_path); + network_interface.connect_read_node_to_map(&node_id, selection_network_path); } NodeGraphMessage::InsertDotOnWire { node_id, input_connector, x, y } => { network_interface.insert_dot_on_wire(node_id, &input_connector, IVec2::new(x, y), selection_network_path); diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/characterization_tests.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/characterization_tests.rs index 8563d61f8d..ac1ca702e7 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/characterization_tests.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/characterization_tests.rs @@ -4,6 +4,7 @@ use super::{ }; use crate::consts::{GRID_SIZE, WIRE_DRAG_NODE_EXPAND_DELAY_MILLISECONDS}; use crate::messages::input_mapper::utility_types::pointer::EditorPointerState; +use crate::messages::portfolio::document::node_graph::document_node_definitions::resolve_proto_node_type; use crate::messages::portfolio::document::node_graph::utility_types::{ContextMenuData, Direction}; use crate::messages::portfolio::document::utility_types::wires::GraphWireStyle; use crate::test_utils::test_prelude::*; @@ -3547,3 +3548,106 @@ async fn cannot_preview_node_without_outputs() { network_interface.toggle_preview(custom_node, &[]); assert_eq!(network_interface.previewing(&[]), Previewing::No); } + +fn map_definition() -> DefinitionIdentifier { + DefinitionIdentifier::ProtoNode(graphene_std::graphic::map::IDENTIFIER) +} + +async fn layer_with_chain_and_unwired_map(editor: &mut EditorTestUtils) -> (NodeId, NodeId, NodeId) { + let (layer, near, ..) = layer_with_chain(editor).await; + editor + .active_document_mut() + .network_interface + .create_wire(&OutputConnector::primary_output(layer), &InputConnector::Export(0), &[]); + editor.handle_message(NodeGraphMessage::RunDocumentGraph).await; + + let map = editor.create_node_by_name_at(map_definition(), 0, 30).await; + (layer, near, map) +} + +async fn drop_map_onto_chain(editor: &mut EditorTestUtils, layer: NodeId, map: NodeId) { + editor + .handle_message(NodeGraphMessage::InsertNodeBetween { + node_id: map, + input_connector: InputConnector::layer_secondary_input(layer), + insert_node_input_index: 0, + }) + .await; +} + +#[tokio::test] +async fn dropping_a_map_node_onto_a_wire_feeds_it_the_matching_read_node() { + let mut editor = EditorTestUtils::create(); + editor.new_document().await; + let (layer, near, map) = layer_with_chain_and_unwired_map(&mut editor).await; + + drop_map_onto_chain(&mut editor, layer, map).await; + + let network_interface = &editor.active_document().network_interface; + let Some(NodeInput::Node { node_id: read_node, .. }) = network_interface.input_from_connector(&InputConnector::node_at_index(map, 1), &[]).cloned() else { + panic!("The Map node's mapped input should be fed by a new node"); + }; + assert_eq!( + network_interface.reference(&read_node, &[]), + Some(DefinitionIdentifier::ProtoNode(graphene_std::context::read_vector::IDENTIFIER)) + ); + + let map_position = network_interface.position(&map, &[]).unwrap(); + let near_position = network_interface.position(&near, &[]).unwrap(); + let near_bottom = near_position.y + network_interface.node_height(&near, &[]) as i32; + let read_node_position = network_interface.position(&read_node, &[]).unwrap(); + assert_eq!(read_node_position.x, map_position.x - crate::consts::NODE_CHAIN_WIDTH); + assert!( + read_node_position.y > near_bottom, + "The Read node should sit below the Map node's content source rather than overlap it" + ); + assert_invariants(&editor, "after dropping a Map node onto a wire"); +} + +#[test] +fn map_content_types_resolve_to_the_read_node_of_their_element() { + use super::layout::read_node_for_content; + use super::resolved_types::TypeSource; + use graph_craft::{item, list}; + use graphene_std::context::{read_graphic, read_number, read_vector}; + use graphene_std::list::Bundle; + use graphene_std::text_nodes::read_string; + use graphene_std::{Graphic, Vector}; + + assert_eq!(read_node_for_content(&TypeSource::Compiled(list!(Vector))), Some(read_vector::IDENTIFIER)); + assert_eq!(read_node_for_content(&TypeSource::Compiled(item!(Graphic))), Some(read_graphic::IDENTIFIER)); + assert_eq!(read_node_for_content(&TypeSource::Compiled(list!(f64))), Some(read_number::IDENTIFIER)); + assert_eq!(read_node_for_content(&TypeSource::TaggedValue(list!(String))), Some(read_string::IDENTIFIER)); + assert_eq!(read_node_for_content(&TypeSource::Compiled(item!(Bundle))), Some(read_vector::IDENTIFIER)); + + assert_eq!( + read_node_for_content(&TypeSource::Compiled(list!(i64))), + None, + "Read Number yields f64, which a Map over integers can't take" + ); + assert_eq!(read_node_for_content(&TypeSource::Compiled(list!(bool))), None); + assert_eq!(read_node_for_content(&TypeSource::Unknown), None); +} + +#[tokio::test] +async fn dropping_a_map_node_onto_a_wire_keeps_its_wired_mapped_input() { + let mut editor = EditorTestUtils::create(); + editor.new_document().await; + let (layer, _, map) = layer_with_chain_and_unwired_map(&mut editor).await; + + let mapped_source = NodeId::new(); + let read_vector = resolve_proto_node_type(graphene_std::context::read_vector::IDENTIFIER).unwrap().default_node_template(); + let network_interface = &mut editor.active_document_mut().network_interface; + network_interface.insert_node(mapped_source, read_vector, &[]); + network_interface.create_wire(&OutputConnector::primary_output(mapped_source), &InputConnector::node_at_index(map, 1), &[]); + let node_count = editor.active_document().network_interface.document_network().nodes.len(); + + drop_map_onto_chain(&mut editor, layer, map).await; + + let network_interface = &editor.active_document().network_interface; + assert_eq!( + network_interface.input_from_connector(&InputConnector::node_at_index(map, 1), &[]), + Some(&NodeInput::node(mapped_source, 0)) + ); + assert_eq!(network_interface.document_network().nodes.len(), node_count); +} diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/layout.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/layout.rs index 0efb863637..12beb19fed 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/layout.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/layout.rs @@ -1,4 +1,9 @@ +use super::resolved_types::TypeSource; use super::*; +use graph_craft::{ProtoNodeIdentifier, concrete}; +use graphene_std::raster_types::{CPU, Raster}; +use graphene_std::vector::style::Gradient; +use graphene_std::{Color, Graphic}; impl NodeNetworkInterface { /// The top left corner of a node in node graph grid coordinates. @@ -1155,6 +1160,41 @@ impl NodeNetworkInterface { self.create_wire(&upstream_output, &InputConnector::node_at_index(*node_id, insert_node_input_index), network_path); } + /// Feeds an unconnected Map node's mapped input with the Read node matching the element type of its content. + pub fn connect_read_node_to_map(&mut self, node_id: &NodeId, network_path: &[NodeId]) { + if self.reference(node_id, network_path) != Some(DefinitionIdentifier::ProtoNode(graphene_std::graphic::map::IDENTIFIER)) { + return; + } + + let mapped_input = InputConnector::node_at_index(*node_id, 1); + if !matches!(self.input_from_connector(&mapped_input, network_path), Some(NodeInput::Value { .. })) { + return; + } + + let Some(content_source) = self.upstream_output_connector(&InputConnector::primary_input(*node_id), network_path) else { + return; + }; + let Some(mut read_node_template) = read_node_for_content(&self.output_type(&content_source, network_path)) + .and_then(resolve_proto_node_type) + .map(|definition| definition.default_node_template()) + else { + return; + }; + let Some(map_position) = self.position(node_id, network_path) else { + return; + }; + + let below_content_source = content_source + .node_id() + .and_then(|source| Some(self.position(&source, network_path)?.y + self.node_height(&source, network_path) as i32 + 1)); + let read_node_position = IVec2::new(map_position.x - NODE_CHAIN_WIDTH, below_content_source.unwrap_or(map_position.y + 2)); + read_node_template.node_type_metadata = NodeTypePersistentMetadata::node(read_node_position); + + let read_node_id = NodeId::new(); + self.insert_node(read_node_id, read_node_template, network_path); + self.create_wire(&OutputConnector::primary_output(read_node_id), &mapped_input, network_path); + } + /// Inserts the freshly-created `node_id` onto the wire feeding `input_connector`: the previous upstream becomes the /// new node's primary (index 0) input, and the new node feeds `input_connector`. /// @@ -1575,3 +1615,30 @@ impl NodeNetworkInterface { self.layer_chain(layer, network_path) == intended_chain } } + +/// The Read node yielding each element of a Map node's content, unbundling a whole-list `Item>` wire to its `T`. +pub(super) fn read_node_for_content(content_type: &TypeSource) -> Option { + use graphene_std::context::{read_color, read_gradient, read_graphic, read_number, read_raster, read_vector}; + use graphene_std::text_nodes::read_string; + + let bundle_element = content_type.compiled_nested_type().and_then(Type::bundle_element_name); + let element_type = content_type.compiled_element_type()?; + + [ + (concrete!(Vector), read_vector::IDENTIFIER), + (concrete!(Graphic), read_graphic::IDENTIFIER), + (concrete!(Raster), read_raster::IDENTIFIER), + (concrete!(Color), read_color::IDENTIFIER), + (concrete!(Gradient), read_gradient::IDENTIFIER), + (concrete!(f64), read_number::IDENTIFIER), + (concrete!(String), read_string::IDENTIFIER), + ] + .into_iter() + .find_map(|(candidate, read_node)| { + let matches = match (bundle_element, &candidate) { + (Some(bundle_element), Type::Concrete(descriptor)) => descriptor.name == bundle_element, + _ => candidate == *element_type, + }; + matches.then_some(read_node) + }) +}