Skip to content

Commit 228dedd

Browse files
committed
WIP: Hashmap based input overrides
1 parent 1db7728 commit 228dedd

File tree

6 files changed

+279
-213
lines changed

6 files changed

+279
-213
lines changed

editor/src/messages/portfolio/document/document_message_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2067,7 +2067,7 @@ impl DocumentMessageHandler {
20672067
/// Create a network interface with a single export
20682068
fn default_document_network_interface() -> NodeNetworkInterface {
20692069
let mut network_interface = NodeNetworkInterface::default();
2070-
network_interface.add_export(TaggedValue::ArtboardGroup(graphene_core::ArtboardGroup::EMPTY), -1, "".to_string(), &[]);
2070+
network_interface.add_export(TaggedValue::ArtboardGroup(graphene_core::ArtboardGroup::EMPTY), -1, "", &[]);
20712071
network_interface
20722072
}
20732073

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

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ pub struct NodePropertiesContext<'a> {
3838
pub document_name: &'a str,
3939
}
4040

41+
impl NodePropertiesContext<'_> {
42+
pub fn call_widget_override(&mut self, node_id: &NodeId, index: usize) -> Option<Vec<LayoutGroup>> {
43+
//let current_override = //Get mutable reference from transient metadata
44+
//let mut widget_override = std::mem::replace(&mut WidgetOverrideLambda(Box::new()), current_override);
45+
// let layout = widget_override.0(node_id, context);
46+
//let current_override = //Get mutable reference from transient metadata (now empty)
47+
//let empty_widget_override = std::mem::replace(&mut widget_override, current_override) // Put back the taken override
48+
// Some(layout)
49+
let Some(input_properties_row) = self.network_interface.input_properties_row(node_id, index, self.selection_network_path) else {
50+
log::error!("Could not get input properties row in call_widget_override");
51+
return None;
52+
};
53+
let Some(widget_override_lambda) = input_properties_row.widget_override.as_ref().and_then(|widget_override| INPUT_OVERRIDES.get(widget_override)) else {
54+
log::error!("Could not get widget override lambda in call_widget_override");
55+
return None;
56+
};
57+
widget_override_lambda(*node_id, index, self)
58+
.map_err(|error| {
59+
log::error!("Error in widget override lambda: {}", error);
60+
})
61+
.ok()
62+
}
63+
}
64+
4165
/// Acts as a description for a [DocumentNode] before it gets instantiated as one.
4266
#[derive(Clone)]
4367
pub struct DocumentNodeDefinition {
@@ -89,7 +113,10 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
89113
..Default::default()
90114
},
91115
persistent_node_metadata: DocumentNodePersistentMetadata {
92-
input_properties: vec![PropertiesRow::with_override("In", WidgetOverride::string("The identity node simply passes its data through."))],
116+
input_properties: vec![PropertiesRow::with_override(
117+
"In",
118+
WidgetOverride::String("The identity node simply passes its data through.".to_string()),
119+
)],
93120
output_names: vec!["Out".to_string()],
94121
..Default::default()
95122
},
@@ -112,7 +139,7 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
112139
persistent_node_metadata: DocumentNodePersistentMetadata {
113140
input_properties: vec![PropertiesRow::with_override(
114141
"In",
115-
WidgetOverride::string("The Monitor node is used by the editor to access the data flowing through it."),
142+
WidgetOverride::String("The Monitor node is used by the editor to access the data flowing through it.".to_string()),
116143
)],
117144
output_names: vec!["Out".to_string()],
118145
..Default::default()
@@ -285,7 +312,14 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
285312
..Default::default()
286313
},
287314
persistent_node_metadata: DocumentNodePersistentMetadata {
288-
input_properties: vec!["Artboards".into(), "Contents".into(), "Location".into(), "Dimensions".into(), "Background".into(), "Clip".into()],
315+
input_properties: vec![
316+
PropertiesRow::with_override("Artboards", WidgetOverride::Hidden),
317+
PropertiesRow::with_override("Contents", WidgetOverride::Hidden),
318+
"Location".into(),
319+
"Dimensions".into(),
320+
"Background".into(),
321+
"Clip".into(),
322+
],
289323
output_names: vec!["Out".to_string()],
290324
node_type_metadata: NodeTypePersistentMetadata::layer(IVec2::new(0, 0)),
291325
network_metadata: Some(NodeNetworkMetadata {
@@ -366,7 +400,7 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
366400
..Default::default()
367401
},
368402
persistent_node_metadata: DocumentNodePersistentMetadata {
369-
input_properties: vec!["api".into(), "path".into()],
403+
input_properties: vec![PropertiesRow::with_override("api", WidgetOverride::Hidden), "path".into()],
370404
output_names: vec!["Image Frame".to_string()],
371405
network_metadata: Some(NodeNetworkMetadata {
372406
persistent_metadata: NodeNetworkPersistentMetadata {
@@ -689,7 +723,10 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
689723
..Default::default()
690724
},
691725
persistent_node_metadata: DocumentNodePersistentMetadata {
692-
input_properties: vec![PropertiesRow::with_override("Image", WidgetOverride::string("Creates an embedded image with the given transform."))],
726+
input_properties: vec![PropertiesRow::with_override(
727+
"Image",
728+
WidgetOverride::String("Creates an embedded image with the given transform.".to_string()),
729+
)],
693730
output_names: vec!["Image".to_string()],
694731
network_metadata: Some(NodeNetworkMetadata {
695732
persistent_metadata: NodeNetworkPersistentMetadata {
@@ -754,7 +791,7 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
754791
input_properties: vec![
755792
"Clip".into(),
756793
"Seed".into(),
757-
"Scale".into(),
794+
PropertiesRow::with_override("Scale", WidgetOverride::Custom("noise_properties_scale".to_string())),
758795
"Noise Type".into(),
759796
"Domain Warp Type".into(),
760797
"Domain Warp Amplitude".into(),
@@ -817,7 +854,7 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
817854
..Default::default()
818855
},
819856
persistent_node_metadata: DocumentNodePersistentMetadata {
820-
input_properties: vec!["Image".into(), "Stencil".into()],
857+
input_properties: vec![PropertiesRow::with_override("Image", WidgetOverride::Hidden), "Stencil".into()],
821858
output_names: vec!["Image".to_string()],
822859
..Default::default()
823860
},
@@ -840,7 +877,11 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
840877
..Default::default()
841878
},
842879
persistent_node_metadata: DocumentNodePersistentMetadata {
843-
input_properties: vec!["Image".into(), "Insertion".into(), "Replace".into()],
880+
input_properties: vec![
881+
PropertiesRow::with_override("Image", WidgetOverride::Hidden),
882+
PropertiesRow::with_override("Insertion", WidgetOverride::Hidden),
883+
"Replace".into(),
884+
],
844885
output_names: vec!["Image".to_string()],
845886
..Default::default()
846887
},
@@ -1111,7 +1152,7 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
11111152
..Default::default()
11121153
},
11131154
persistent_node_metadata: DocumentNodePersistentMetadata {
1114-
input_properties: vec![PropertiesRow::with_override("Image", WidgetOverride::string("A bitmap image is embedded in this node"))],
1155+
input_properties: vec![PropertiesRow::with_override("Image", WidgetOverride::String("A bitmap image is embedded in this node".to_string()))],
11151156
output_names: vec!["Image".to_string()],
11161157
network_metadata: Some(NodeNetworkMetadata {
11171158
persistent_metadata: NodeNetworkPersistentMetadata {
@@ -2673,6 +2714,47 @@ pub static IMAGINATE_NODE: Lazy<DocumentNodeDefinition> = Lazy::new(|| DocumentN
26732714
description: Cow::Borrowed("TODO"),
26742715
});
26752716

2717+
static INPUT_OVERRIDES: once_cell::sync::Lazy<HashMap<String, Box<dyn Fn(NodeId, usize, &mut NodePropertiesContext) -> Result<Vec<LayoutGroup>, String> + Send + Sync>>> =
2718+
once_cell::sync::Lazy::new(static_input_properties);
2719+
2720+
/// Defines the logic for inputs to display a custom properties panel widget.
2721+
fn static_input_properties() -> HashMap<String, Box<dyn Fn(NodeId, usize, &mut NodePropertiesContext) -> Result<Vec<LayoutGroup>, String> + Send + Sync>> {
2722+
let mut map: HashMap<String, Box<dyn Fn(NodeId, usize, &mut NodePropertiesContext) -> Result<Vec<LayoutGroup>, String> + Send + Sync>> = HashMap::new();
2723+
map.insert("hidden".to_string(), Box::new(|_node_id, _index, _context| Ok(Vec::new())));
2724+
map.insert(
2725+
"string".to_string(),
2726+
Box::new(|node_id, index, context| {
2727+
let Some(value) = context.network_interface.input_metadata(&node_id, index, "string_properties", context.selection_network_path) else {
2728+
return Err(format!("Could not get string properties for node {}", node_id));
2729+
};
2730+
let Some(string) = value.as_str() else {
2731+
return Err(format!("Could not downcast string properties for node {}", node_id));
2732+
};
2733+
Ok(node_properties::string_properties(string.to_string()))
2734+
}),
2735+
);
2736+
map.insert(
2737+
"noise_properties_scale".to_string(),
2738+
Box::new(|node_id, index, context| {
2739+
let network = context.network_interface.network(context.selection_network_path).ok_or("network not found in noise_properties_scale")?;
2740+
let document_node = network.nodes.get(&node_id).ok_or("node not found in noise_properties_scale")?;
2741+
let current_noise_type = document_node.inputs.iter().find_map(|input| match input.as_value() {
2742+
Some(&TaggedValue::NoiseType(noise_type)) => Some(noise_type),
2743+
_ => None,
2744+
});
2745+
let coherent_noise_active = current_noise_type != Some(NoiseType::WhiteNoise);
2746+
let input_name = context
2747+
.network_interface
2748+
.input_name(&node_id, index, context.selection_network_path)
2749+
.ok_or("input name not found in noise_properties_scale")?;
2750+
2751+
let scale = node_properties::number_widget(document_node, node_id, 2, input_name, NumberInput::default().min(0.).disabled(!coherent_noise_active), true);
2752+
Ok(vec![scale.into()])
2753+
}),
2754+
);
2755+
map
2756+
}
2757+
26762758
pub fn resolve_document_node_type(identifier: &str) -> Option<&DocumentNodeDefinition> {
26772759
DOCUMENT_NODE_TYPES.iter().find(|definition| definition.identifier == identifier)
26782760
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::messages::portfolio::document::node_graph::document_node_definitions:
99
use crate::messages::portfolio::document::node_graph::utility_types::{ContextMenuData, Direction, FrontendGraphDataType};
1010
use crate::messages::portfolio::document::utility_types::document_metadata::LayerNodeIdentifier;
1111
use crate::messages::portfolio::document::utility_types::network_interface::{
12-
self, InputConnector, NodeNetworkInterface, NodeTemplate, NodeTypePersistentMetadata, OutputConnector, Previewing, PropertiesRow, TypeSource
12+
self, InputConnector, NodeNetworkInterface, NodeTemplate, NodeTypePersistentMetadata, OutputConnector, Previewing, TypeSource,
1313
};
1414
use crate::messages::portfolio::document::utility_types::nodes::{CollapsedLayers, LayerPanelEntry};
1515
use crate::messages::prelude::*;
@@ -100,11 +100,11 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
100100
responses.add(NodeGraphMessage::SelectedNodesSet { nodes: vec![new_layer_id] });
101101
}
102102
NodeGraphMessage::AddImport => {
103-
network_interface.add_import(graph_craft::document::value::TaggedValue::None, true, -1, String::new(), breadcrumb_network_path);
103+
network_interface.add_import(graph_craft::document::value::TaggedValue::None, true, -1, "", breadcrumb_network_path);
104104
responses.add(NodeGraphMessage::SendGraph);
105105
}
106106
NodeGraphMessage::AddExport => {
107-
network_interface.add_export(graph_craft::document::value::TaggedValue::None, -1, String::new(), breadcrumb_network_path);
107+
network_interface.add_export(graph_craft::document::value::TaggedValue::None, -1, "", breadcrumb_network_path);
108108
responses.add(NodeGraphMessage::SendGraph);
109109
}
110110
NodeGraphMessage::Init => {
@@ -2448,7 +2448,10 @@ fn frontend_inputs_lookup(breadcrumb_network_path: &[NodeId], network_interface:
24482448
}
24492449

24502450
// Get the name from the metadata here (since it also requires a reference to the `network_interface`)
2451-
let name = network_interface.input_properties_row(&node_id, index, breadcrumb_network_path).cloned().map(|properties_row|properties_row.input_name).filter(|s| !s.is_empty());
2451+
let name = network_interface
2452+
.input_name(&node_id, index, breadcrumb_network_path)
2453+
.filter(|s| !s.is_empty())
2454+
.map(|name| name.to_string());
24522455
// Get the output connector that feeds into this input (done here as well for simplicity)
24532456
let connector = OutputConnector::from_input(input);
24542457

0 commit comments

Comments
 (0)