Update graph UI and improve simplicity and robustness of sending graph and layer panel updates (#1564)

* WIP

* Fix loading the structure of layers

* Fix broken indents

* Remove debugging stuff

* Fix displaying errors and node graph UI fixes/improvements

* Fix compilation failure

---------

Co-authored-by: 0hypercube <0hypercube@gmail.com>
This commit is contained in:
Keavon Chambers
2024-01-13 04:15:36 -08:00
committed by GitHub
co-authored by 0hypercube
parent 83116aa744
commit aab0fcf84c
33 changed files with 836 additions and 813 deletions
@@ -87,7 +87,6 @@ pub enum DocumentMessage {
RenameDocument {
new_name: String,
},
RenderDocument,
RenderRulers,
RenderScrollbars,
SaveDocument,
@@ -85,7 +85,7 @@ pub struct DocumentMessageHandler {
#[serde(skip)]
layer_range_selection_reference: Option<LayerNodeIdentifier>,
#[serde(skip)]
pub metadata: DocumentMetadata,
pub document_metadata: DocumentMetadata,
}
impl Default for DocumentMessageHandler {
@@ -121,7 +121,7 @@ impl Default for DocumentMessageHandler {
graph_view_overlay_open: false,
snapping_state: SnappingState::default(),
layer_range_selection_reference: None,
metadata: Default::default(),
document_metadata: Default::default(),
}
}
}
@@ -245,7 +245,13 @@ impl MessageHandler<DocumentMessage, DocumentInputs<'_>> for DocumentMessageHand
self.navigation_handler.process_message(
message,
responses,
(&self.metadata, document_bounds, ipp, self.selected_visible_layers_bounding_box_viewport(), &mut self.navigation),
(
&self.document_metadata,
document_bounds,
ipp,
self.selected_visible_layers_bounding_box_viewport(),
&mut self.navigation,
),
);
}
#[remain::unsorted]
@@ -259,7 +265,7 @@ impl MessageHandler<DocumentMessage, DocumentInputs<'_>> for DocumentMessageHand
executor,
document_name: self.name.as_str(),
document_network: &self.network,
document_metadata: &mut self.metadata,
document_metadata: &mut self.document_metadata,
};
self.properties_panel_message_handler
.process_message(message, responses, (persistent_data, properties_panel_message_handler_data));
@@ -271,7 +277,7 @@ impl MessageHandler<DocumentMessage, DocumentInputs<'_>> for DocumentMessageHand
responses,
NodeGraphHandlerData {
document_network: &mut self.network,
document_metadata: &mut self.metadata,
document_metadata: &mut self.document_metadata,
document_id,
document_name: self.name.as_str(),
collapsed: &mut self.collapsed,
@@ -281,13 +287,13 @@ impl MessageHandler<DocumentMessage, DocumentInputs<'_>> for DocumentMessageHand
);
}
#[remain::unsorted]
GraphOperation(message) => GraphOperationMessageHandler.process_message(message, responses, (&mut self.network, &mut self.metadata, &mut self.collapsed, &mut self.node_graph_handler)),
GraphOperation(message) => GraphOperationMessageHandler.process_message(message, responses, (&mut self.network, &mut self.document_metadata, &mut self.collapsed, &mut self.node_graph_handler)),
// Messages
AbortTransaction => {
if !self.undo_in_progress {
self.undo(responses);
responses.extend([RenderDocument.into(), DocumentStructureChanged.into()]);
responses.add(OverlaysMessage::Draw);
}
}
AlignSelectedLayers { axis, aggregate } => {
@@ -375,12 +381,9 @@ impl MessageHandler<DocumentMessage, DocumentInputs<'_>> for DocumentMessageHand
DocumentStructureChanged => {
self.update_layers_panel_options_bar_widgets(responses);
self.document_metadata.load_structure(&self.network);
let data_buffer: RawBuffer = self.serialize_root();
responses.add(FrontendMessage::UpdateDocumentLayerStructure { data_buffer });
if self.graph_view_overlay_open {
responses.add(NodeGraphMessage::SendGraph { should_rerender: false });
}
}
DuplicateSelectedLayers => {
// TODO: Reimplement selected layer duplication
@@ -414,7 +417,7 @@ impl MessageHandler<DocumentMessage, DocumentInputs<'_>> for DocumentMessageHand
self.graph_view_overlay_open = open;
if open {
responses.add(NodeGraphMessage::SendGraph { should_rerender: false });
responses.add(NodeGraphMessage::SendGraph);
}
responses.add(FrontendMessage::TriggerGraphViewOverlay { open });
}
@@ -503,7 +506,7 @@ impl MessageHandler<DocumentMessage, DocumentInputs<'_>> for DocumentMessageHand
});
}
// Nudge resize
else if let Some([existing_top_left, existing_bottom_right]) = self.metadata.bounding_box_document(layer) {
else if let Some([existing_top_left, existing_bottom_right]) = self.document_metadata.bounding_box_document(layer) {
let size = existing_bottom_right - existing_top_left;
let new_size = size + if opposite_corner { -delta } else { delta };
let enlargement_factor = new_size / size;
@@ -572,19 +575,15 @@ impl MessageHandler<DocumentMessage, DocumentInputs<'_>> for DocumentMessageHand
}
Redo => {
responses.add(SelectToolMessage::Abort);
responses.add(DocumentHistoryForward);
responses.add(DocumentMessage::DocumentHistoryForward);
responses.add(BroadcastEvent::DocumentIsDirty);
responses.add(RenderDocument);
responses.add(DocumentStructureChanged);
responses.add(OverlaysMessage::Draw);
}
RenameDocument { new_name } => {
self.name = new_name;
responses.add(PortfolioMessage::UpdateOpenDocumentsList);
responses.add(NodeGraphMessage::UpdateNewNodeGraph);
}
RenderDocument => {
responses.add(OverlaysMessage::Draw);
}
RenderRulers => {
let document_transform_scale = self.navigation_handler.snapped_scale(self.navigation.zoom);
@@ -753,11 +752,10 @@ impl MessageHandler<DocumentMessage, DocumentInputs<'_>> for DocumentMessageHand
Undo => {
self.undo_in_progress = true;
responses.add(BroadcastEvent::ToolAbort);
responses.add(DocumentHistoryBackward);
responses.add(DocumentMessage::DocumentHistoryBackward);
responses.add(BroadcastEvent::DocumentIsDirty);
responses.add(RenderDocument);
responses.add(DocumentStructureChanged);
responses.add(UndoFinished);
responses.add(OverlaysMessage::Draw);
responses.add(DocumentMessage::UndoFinished);
}
UndoFinished => self.undo_in_progress = false,
UngroupSelectedLayers => {
@@ -786,7 +784,7 @@ impl MessageHandler<DocumentMessage, DocumentInputs<'_>> for DocumentMessageHand
responses.add(DocumentMessage::CommitTransaction);
}
UpdateDocumentTransform { transform } => {
self.metadata.document_to_viewport = transform;
self.document_metadata.document_to_viewport = transform;
responses.add(DocumentMessage::RenderRulers);
responses.add(DocumentMessage::RenderScrollbars);
responses.add(NodeGraphMessage::RunDocumentGraph);
@@ -813,35 +811,43 @@ impl MessageHandler<DocumentMessage, DocumentInputs<'_>> for DocumentMessageHand
impl DocumentMessageHandler {
pub fn layer_visible(&self, layer: LayerNodeIdentifier) -> bool {
!layer.ancestors(&self.metadata).any(|layer| self.network.disabled.contains(&layer.to_node()))
!layer.ancestors(&self.document_metadata).any(|layer| self.network.disabled.contains(&layer.to_node()))
}
pub fn selected_visible_layers(&self) -> impl Iterator<Item = LayerNodeIdentifier> + '_ {
self.metadata.selected_layers().filter(|&layer| self.layer_visible(layer))
self.document_metadata.selected_layers().filter(|&layer| self.layer_visible(layer))
}
/// Runs an intersection test with all layers and a viewport space quad
pub fn intersect_quad<'a>(&'a self, viewport_quad: graphene_core::renderer::Quad, network: &'a NodeNetwork) -> impl Iterator<Item = LayerNodeIdentifier> + 'a {
let document_quad = self.metadata.document_to_viewport.inverse() * viewport_quad;
self.metadata
let document_quad = self.document_metadata.document_to_viewport.inverse() * viewport_quad;
self.document_metadata
.root()
.decendants(&self.metadata)
.decendants(&self.document_metadata)
.filter(|&layer| self.layer_visible(layer))
.filter(|&layer| !is_artboard(layer, network))
.filter_map(|layer| self.metadata.click_target(layer).map(|targets| (layer, targets)))
.filter(move |(layer, target)| target.iter().any(move |target| target.intersect_rectangle(document_quad, self.metadata.transform_to_document(*layer))))
.filter_map(|layer| self.document_metadata.click_target(layer).map(|targets| (layer, targets)))
.filter(move |(layer, target)| {
target
.iter()
.any(move |target| target.intersect_rectangle(document_quad, self.document_metadata.transform_to_document(*layer)))
})
.map(|(layer, _)| layer)
}
/// Find all of the layers that were clicked on from a viewport space location
pub fn click_xray(&self, viewport_location: DVec2) -> impl Iterator<Item = LayerNodeIdentifier> + '_ {
let point = self.metadata.document_to_viewport.inverse().transform_point2(viewport_location);
self.metadata
let point = self.document_metadata.document_to_viewport.inverse().transform_point2(viewport_location);
self.document_metadata
.root()
.decendants(&self.metadata)
.decendants(&self.document_metadata)
.filter(|&layer| self.layer_visible(layer))
.filter_map(|layer| self.metadata.click_target(layer).map(|targets| (layer, targets)))
.filter(move |(layer, target)| target.iter().any(|target: &ClickTarget| target.intersect_point(point, self.metadata.transform_to_document(*layer))))
.filter_map(|layer| self.document_metadata.click_target(layer).map(|targets| (layer, targets)))
.filter(move |(layer, target)| {
target
.iter()
.any(|target: &ClickTarget| target.intersect_point(point, self.document_metadata.transform_to_document(*layer)))
})
.map(|(layer, _)| layer)
}
@@ -853,7 +859,7 @@ impl DocumentMessageHandler {
/// Get the combined bounding box of the click targets of the selected visible layers in viewport space
pub fn selected_visible_layers_bounding_box_viewport(&self) -> Option<[DVec2; 2]> {
self.selected_visible_layers()
.filter_map(|layer| self.metadata.bounding_box_viewport(layer))
.filter_map(|layer| self.document_metadata.bounding_box_viewport(layer))
.reduce(graphene_core::renderer::Quad::combine_bounds)
}
@@ -862,7 +868,7 @@ impl DocumentMessageHandler {
}
pub fn metadata(&self) -> &DocumentMetadata {
&self.metadata
&self.document_metadata
}
pub fn serialize_document(&self) -> String {
@@ -878,7 +884,7 @@ impl DocumentMessageHandler {
pub fn with_name(name: String, ipp: &InputPreprocessorMessageHandler, responses: &mut VecDeque<Message>) -> Self {
let mut document = Self { name, ..Self::default() };
let transform = document.navigation_handler.calculate_offset_transform(ipp.viewport_bounds.size() / 2., DVec2::ZERO, 0., 1.);
document.metadata.document_to_viewport = transform;
document.document_metadata.document_to_viewport = transform;
responses.add(DocumentMessage::UpdateDocumentTransform { transform });
document
@@ -897,23 +903,24 @@ impl DocumentMessageHandler {
std::iter::empty()
}
fn serialize_structure(&self, folder: LayerNodeIdentifier, structure: &mut Vec<LayerNodeIdentifier>, data: &mut Vec<LayerNodeIdentifier>, path: &mut Vec<LayerNodeIdentifier>) {
/// Called recursively by the entry function [`serialize_root`].
fn serialize_structure(&self, folder: LayerNodeIdentifier, structure_section: &mut Vec<u64>, data_section: &mut Vec<u64>, path: &mut Vec<LayerNodeIdentifier>) {
let mut space = 0;
for layer_node in folder.children(self.metadata()) {
data.push(layer_node);
data_section.push(layer_node.to_node().0);
space += 1;
if layer_node.has_children(self.metadata()) && !self.collapsed.contains(&layer_node) {
path.push(layer_node);
// TODO: Skip if folder is not expanded.
structure.push(LayerNodeIdentifier::new_unchecked(NodeId(space)));
self.serialize_structure(layer_node, structure, data, path);
structure_section.push(space);
self.serialize_structure(layer_node, structure_section, data_section, path);
space = 0;
path.pop();
}
}
structure.push(LayerNodeIdentifier::new_unchecked(NodeId(space | 1 << 63)));
structure_section.push(space | 1 << 63);
}
/// Serializes the layer structure into a condensed 1D structure.
@@ -921,17 +928,18 @@ impl DocumentMessageHandler {
/// # Format
/// It is a string of numbers broken into three sections:
///
/// | Data | Description | Length |
/// |--------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------|------------------|
/// | `4,` `2, 1, -2, -0,` `16533113728871998040,3427872634365736244,18115028555707261608,15878401910454357952,449479075714955186` | Encoded example data | |
/// | `L` = `4` = `structure.len()` | `L`, the length of the **Structure** section | First value |
/// | **Structure** section = `2, 1, -2, -0` | The **Structure** section | Next `L` values |
/// | **Data** section = `16533113728871998040, 3427872634365736244, 18115028555707261608, 15878401910454357952, 449479075714955186` | The **Data** section (layer IDs) | Remaining values |
/// | Data | Description | Length |
/// |------------------------------------------------------------------------------------------------------------------------------ |---------------------------------------------------------------|------------------|
/// | `4,` `2, 1, -2, -0,` `16533113728871998040,3427872634365736244,18115028555707261608,15878401910454357952,449479075714955186` | Encoded example data | |
/// | _____________________________________________________________________________________________________________________________ | _____________________________________________________________ | ________________ |
/// | **Length** section: `4` | Length of the **Structure** section (`L` = `structure.len()`) | First value |
/// | **Structure** section: `2, 1, -2, -0` | The **Structure** section | Next `L` values |
/// | **Data** section: `16533113728871998040, 3427872634365736244, 18115028555707261608, 15878401910454357952, 449479075714955186` | The **Data** section (layer IDs) | Remaining values |
///
/// The data section lists the layer IDs for all folders/layers in the tree as read from top to bottom.
/// The structure section lists signed numbers. The sign indicates a folder indentation change (`+` is down a level, `-` is up a level).
/// The numbers in the structure block encode the indentation. For example:
/// - `2` means read two element from the data section, then place a `[`.
/// - `2` means read two elements from the data section, then place a `[`.
/// - `-x` means read `x` elements from the data section and then insert a `]`.
///
/// ```text
@@ -949,14 +957,16 @@ impl DocumentMessageHandler {
/// [3427872634365736244,18115028555707261608,449479075714955186]
/// ```
pub fn serialize_root(&self) -> RawBuffer {
let mut structure = vec![LayerNodeIdentifier::ROOT];
let mut data = Vec::new();
self.serialize_structure(self.metadata().root(), &mut structure, &mut data, &mut vec![]);
let mut structure_section = vec![LayerNodeIdentifier::ROOT.to_node().0];
let mut data_section = Vec::new();
self.serialize_structure(self.metadata().root(), &mut structure_section, &mut data_section, &mut vec![]);
structure[0] = LayerNodeIdentifier::new_unchecked(NodeId(structure.len() as u64 - 1));
structure.extend(data);
// Remove the ROOT element. Prepend `L`, the length (excluding the ROOT) of the structure section (which happens to be where the ROOT element was).
structure_section[0] = structure_section.len() as u64 - 1;
// Append the data section to the end.
structure_section.extend(data_section);
structure.iter().map(|id| id.to_node().0).collect::<Vec<_>>().as_slice().into()
structure_section.as_slice().into()
}
/// Places a document into the history system
@@ -1000,9 +1010,6 @@ impl DocumentMessageHandler {
if self.document_redo_history.len() > crate::consts::MAX_UNDO_HISTORY_LEN {
self.document_redo_history.pop_front();
}
responses.add(DocumentMessage::DocumentStructureChanged);
responses.add(NodeGraphMessage::SendGraph { should_rerender: true });
}
pub fn redo(&mut self, responses: &mut VecDeque<Message>) {
@@ -1018,9 +1025,6 @@ impl DocumentMessageHandler {
if self.document_undo_history.len() > crate::consts::MAX_UNDO_HISTORY_LEN {
self.document_undo_history.pop_front();
}
responses.add(DocumentMessage::DocumentStructureChanged);
responses.add(NodeGraphMessage::SendGraph { should_rerender: true });
}
pub fn current_hash(&self) -> Option<u64> {
@@ -1410,7 +1414,6 @@ impl DocumentMessageHandler {
Redo,
SelectAllLayers,
DeselectAllLayers,
RenderDocument,
SaveDocument,
SetSnapping,
DebugPrintDocument,
@@ -83,8 +83,8 @@ impl<'a> ModifyInputsContext<'a> {
pub fn insert_node_before(&mut self, new_id: NodeId, node_id: NodeId, input_index: usize, mut document_node: DocumentNode, offset: IVec2) -> Option<NodeId> {
assert!(!self.document_network.nodes.contains_key(&new_id), "Creating already existing node");
let post_node = self.document_network.nodes.get_mut(&node_id)?;
let post_node = self.document_network.nodes.get_mut(&node_id)?;
post_node.inputs[input_index] = NodeInput::node(new_id, 0);
document_node.metadata.position = post_node.metadata.position + offset;
self.document_network.nodes.insert(new_id, document_node);
@@ -153,7 +153,6 @@ impl<'a> ModifyInputsContext<'a> {
};
let new_child = LayerNodeIdentifier::new(new_id, self.document_network);
parent.push_front_child(self.document_metadata, new_child);
self.responses.add(DocumentMessage::DocumentStructureChanged);
}
new_id
@@ -181,7 +180,7 @@ impl<'a> ModifyInputsContext<'a> {
],
Default::default(),
);
self.responses.add(NodeGraphMessage::SendGraph { should_rerender: true });
self.responses.add(NodeGraphMessage::RunDocumentGraph);
self.insert_node_before(NodeId(generate_uuid()), layer, 0, artboard_node, IVec2::new(-8, 0))
}
@@ -202,7 +201,7 @@ impl<'a> ModifyInputsContext<'a> {
self.insert_node_before(transform_id, fill_id, 0, transform, IVec2::new(-8, 0));
let shape_id = NodeId(generate_uuid());
self.insert_node_before(shape_id, transform_id, 0, shape, IVec2::new(-8, 0));
self.responses.add(NodeGraphMessage::SendGraph { should_rerender: true });
self.responses.add(NodeGraphMessage::RunDocumentGraph);
}
fn insert_text(&mut self, text: String, font: Font, size: f32, layer: NodeId) {
@@ -227,7 +226,7 @@ impl<'a> ModifyInputsContext<'a> {
self.insert_node_before(transform_id, fill_id, 0, transform, IVec2::new(-8, 0));
let text_id = NodeId(generate_uuid());
self.insert_node_before(text_id, transform_id, 0, text, IVec2::new(-8, 0));
self.responses.add(NodeGraphMessage::SendGraph { should_rerender: true });
self.responses.add(NodeGraphMessage::RunDocumentGraph);
}
fn insert_image_data(&mut self, image_frame: ImageFrame<Color>, layer: NodeId) {
@@ -243,7 +242,7 @@ impl<'a> ModifyInputsContext<'a> {
let image_id = NodeId(generate_uuid());
self.insert_node_before(image_id, transform_id, 0, image, IVec2::new(-8, 0));
self.responses.add(NodeGraphMessage::SendGraph { should_rerender: true });
self.responses.add(NodeGraphMessage::RunDocumentGraph);
}
fn shift_upstream(&mut self, node_id: NodeId, shift: IVec2) {
@@ -317,11 +316,6 @@ impl<'a> ModifyInputsContext<'a> {
if !skip_rerender {
self.responses.add(NodeGraphMessage::RunDocumentGraph);
} else {
// Code was removed from here which cleared the frame
}
if existing_node_id.is_none() {
self.responses.add(NodeGraphMessage::SendGraph { should_rerender: false });
}
}
@@ -567,8 +561,7 @@ impl<'a> ModifyInputsContext<'a> {
self.document_metadata.retain_selected_nodes(|id| !delete_nodes.contains(id));
self.responses.add(BroadcastEvent::SelectionChanged);
self.responses.add(DocumentMessage::DocumentStructureChanged);
self.responses.add(NodeGraphMessage::SendGraph { should_rerender: true });
self.responses.add(NodeGraphMessage::RunDocumentGraph);
}
}
@@ -702,7 +695,7 @@ impl MessageHandler<GraphOperationMessage, (&mut NodeNetwork, &mut DocumentMetad
}
}
modify_inputs.responses.add(NodeGraphMessage::SendGraph { should_rerender: true });
modify_inputs.responses.add(NodeGraphMessage::RunDocumentGraph);
}
load_network_structure(document_network, document_metadata, collapsed);
@@ -35,7 +35,7 @@ pub enum NodeGraphMessage {
node_id: NodeId,
input_index: usize,
},
DoubleClickNode {
EnterNestedNetwork {
node: NodeId,
},
DuplicateSelectedNodes,
@@ -68,9 +68,7 @@ pub enum NodeGraphMessage {
SelectedNodesSet {
nodes: Vec<NodeId>,
},
SendGraph {
should_rerender: bool,
},
SendGraph,
SetInputValue {
node_id: NodeId,
input_index: usize,
@@ -86,6 +84,7 @@ pub enum NodeGraphMessage {
input_index: usize,
value: TaggedValue,
},
/// Move all the downstream nodes to the right in the graph to allow space for a newly inserted node
ShiftNode {
node_id: NodeId,
},
@@ -4,7 +4,9 @@ use crate::application::generate_uuid;
use crate::messages::input_mapper::utility_types::macros::action_keys;
use crate::messages::layout::utility_types::widget_prelude::*;
use crate::messages::portfolio::document::utility_types::document_metadata::{DocumentMetadata, LayerNodeIdentifier};
use crate::messages::portfolio::document::utility_types::layer_panel::{LayerClassification, LayerPanelEntry};
use crate::messages::prelude::*;
use graph_craft::document::value::TaggedValue;
use graph_craft::document::{DocumentNode, NodeId, NodeInput, NodeNetwork, NodeOutput, Source};
use graph_craft::proto::GraphErrors;
@@ -68,6 +70,7 @@ pub struct FrontendGraphInput {
name: String,
#[serde(rename = "resolvedType")]
resolved_type: Option<String>,
connected: Option<NodeId>,
}
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, specta::Type)]
@@ -77,13 +80,14 @@ pub struct FrontendGraphOutput {
name: String,
#[serde(rename = "resolvedType")]
resolved_type: Option<String>,
connected: Option<NodeId>,
}
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, specta::Type)]
pub struct FrontendNode {
pub id: graph_craft::document::NodeId,
#[serde(rename = "isLayer")]
pub is_layer: bool,
pub id: graph_craft::document::NodeId,
pub alias: String,
pub name: String,
#[serde(rename = "primaryInput")]
@@ -274,14 +278,9 @@ impl NodeGraphMessageHandler {
}
}
fn send_graph(&self, network: &NodeNetwork, graph_view_overlay_open: bool, responses: &mut VecDeque<Message>) {
responses.add(PropertiesPanelMessage::Refresh);
fn send_graph(&self, network: &NodeNetwork, graph_view_overlay_open: bool, document_metadata: &mut DocumentMetadata, collapsed: &Vec<LayerNodeIdentifier>, responses: &mut VecDeque<Message>) {
document_metadata.load_structure(&network);
if !graph_view_overlay_open {
return;
}
// List of links in format (link_start, link_end, link_end_input_index)
let links = network
.nodes
.iter()
@@ -306,52 +305,111 @@ impl NodeGraphMessageHandler {
})
.collect::<Vec<_>>();
let connected_node_to_output_lookup = links.iter().map(|link| ((link.link_start, link.link_start_output_index), link.link_end)).collect::<HashMap<_, _>>();
let mut nodes = Vec::new();
for (id, node) in &network.nodes {
let node_path = vec![*id];
for (&node_id, node) in &network.nodes {
let node_path = vec![node_id];
// TODO: This should be based on the graph runtime type inference system in order to change the colors of node connectors to match the data type in use
let Some(node_type) = document_node_types::resolve_document_node_type(&node.name) else {
let Some(document_node_definition) = document_node_types::resolve_document_node_type(&node.name) else {
warn!("Node '{}' does not exist in library", node.name);
continue;
};
// Inputs
let mut inputs = node.inputs.iter().zip(node_type.inputs.iter().enumerate().map(|(index, input_type)| {
let index = node.inputs.iter().take(index).filter(|input| input.is_exposed()).count();
FrontendGraphInput {
data_type: input_type.data_type,
name: input_type.name.to_string(),
resolved_type: self.resolved_types.inputs.get(&Source { node: node_path.clone(), index }).map(|input| format!("{input:?}")),
}
}));
let mut inputs = {
let frontend_graph_inputs = document_node_definition.inputs.iter().enumerate().map(|(index, input_type)| {
// Convert the index in all inputs to the index in only the exposed inputs
let index = node.inputs.iter().take(index).filter(|input| input.is_exposed()).count();
FrontendGraphInput {
data_type: input_type.data_type,
name: input_type.name.to_string(),
resolved_type: self.resolved_types.inputs.get(&Source { node: node_path.clone(), index }).map(|input| format!("{input:?}")),
connected: None,
}
});
node.inputs.iter().zip(frontend_graph_inputs).map(|(node_input, mut frontend_graph_input)| {
if let NodeInput::Node { node_id: connected_node_id, .. } = node_input {
frontend_graph_input.connected = Some(*connected_node_id);
}
(node_input, frontend_graph_input)
})
};
let primary_input = inputs.next().filter(|(input, _)| input.is_exposed()).map(|(_, input_type)| input_type);
let exposed_inputs = inputs.filter(|(input, _)| input.is_exposed()).map(|(_, input_type)| input_type).collect();
// Outputs
let mut outputs = node_type.outputs.iter().enumerate().map(|(index, output_type)| FrontendGraphOutput {
let mut outputs = document_node_definition.outputs.iter().enumerate().map(|(index, output_type)| FrontendGraphOutput {
data_type: output_type.data_type,
name: output_type.name.to_string(),
resolved_type: self.resolved_types.outputs.get(&Source { node: node_path.clone(), index }).map(|output| format!("{output:?}")),
connected: connected_node_to_output_lookup.get(&(node_id, index)).copied(),
});
let primary_output = if node.has_primary_output { outputs.next() } else { None };
let primary_output = node.has_primary_output.then(|| outputs.next()).flatten();
let exposed_outputs = outputs.collect::<Vec<_>>();
// Errors
let errors = self.node_graph_errors.iter().find(|error| error.node_path.starts_with(&node_path)).map(|error| error.error.clone());
nodes.push(FrontendNode {
id: node_id,
is_layer: node.is_layer(),
id: *id,
alias: node.alias.clone(),
name: node.name.clone(),
primary_input,
exposed_inputs,
primary_output,
exposed_outputs: outputs.collect::<Vec<_>>(),
exposed_outputs,
position: node.metadata.position.into(),
previewed: network.outputs_contain(*id),
disabled: network.disabled.contains(id),
previewed: network.outputs_contain(node_id),
disabled: network.disabled.contains(&node_id),
errors: errors.map(|e| format!("{e:?}")),
})
});
if node.is_layer() {
let layer = LayerNodeIdentifier::new(node_id, network);
let layer_classification = {
if document_metadata.is_artboard(layer) {
LayerClassification::Artboard
} else if document_metadata.is_folder(layer) {
LayerClassification::Folder
} else {
LayerClassification::Layer
}
// TODO: Maybe switch to this below if perhaps it's simpler?
// if node.is_artboard() {
// LayerClassification::Artboard
// } else if node.is_folder(network) {
// LayerClassification::Folder
// } else {
// LayerClassification::Layer
// }
};
let data = LayerPanelEntry {
id: node_id,
layer_classification,
expanded: layer.has_children(document_metadata) && !collapsed.contains(&layer),
depth: layer.ancestors(document_metadata).count() - 1,
parent_id: layer.parent(document_metadata).map(|parent| parent.to_node()),
// TODO: Remove and take this from the graph data in the frontend similar to thumbnail?
name: network.nodes.get(&node_id).map(|node| node.alias.clone()).unwrap_or_default(),
// TODO: Remove and take this from the graph data in the frontend similar to thumbnail?
tooltip: if cfg!(debug_assertions) { format!("Layer ID: {node_id}") } else { "".into() },
// TODO: Remove and take this from the graph data in the frontend similar to thumbnail?
disabled: network.disabled.contains(&node_id),
};
responses.add(FrontendMessage::UpdateDocumentLayerDetails { data });
}
}
responses.add(FrontendMessage::UpdateNodeGraph { nodes, links });
responses.add(DocumentMessage::DocumentStructureChanged);
if graph_view_overlay_open {
responses.add(FrontendMessage::UpdateNodeGraph { nodes, links });
}
responses.add(PropertiesPanelMessage::Refresh);
}
/// Updates the frontend's selection state in line with the backend
@@ -474,7 +532,7 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
fn process_message(&mut self, message: NodeGraphMessage, responses: &mut VecDeque<Message>, data: NodeGraphHandlerData<'a>) {
let NodeGraphHandlerData {
document_network,
document_metadata: metadata,
document_metadata,
document_id,
collapsed,
graph_view_overlay_open,
@@ -487,15 +545,14 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
on: BroadcastEvent::SelectionChanged,
send: Box::new(NodeGraphMessage::SelectedNodesUpdated.into()),
});
load_network_structure(document_network, metadata, collapsed);
responses.add(DocumentMessage::DocumentStructureChanged);
load_network_structure(document_network, document_metadata, collapsed);
}
NodeGraphMessage::SelectedNodesUpdated => {
self.update_selection_action_buttons(document_network, metadata, responses);
self.update_selected(document_network, metadata, responses);
if metadata.selected_layers().count() <= 1 {
self.update_selection_action_buttons(document_network, document_metadata, responses);
self.update_selected(document_network, document_metadata, responses);
if document_metadata.selected_layers().count() <= 1 {
responses.add(DocumentMessage::SetRangeSelectionLayer {
new_layer: metadata.selected_layers().next(),
new_layer: document_metadata.selected_layers().next(),
});
}
responses.add(NodeGraphMessage::RunDocumentGraph);
@@ -520,15 +577,15 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
error!("Failed to find actual index of connector index {input_node_connector_index} on node {input_node:#?}");
return;
};
responses.add(DocumentMessage::DocumentStructureChanged);
responses.add(DocumentMessage::StartTransaction);
let input = NodeInput::node(output_node, output_node_connector_index);
responses.add(NodeGraphMessage::SetNodeInput { node_id, input_index, input });
let should_rerender = network.connected_to_output(node_id);
responses.add(NodeGraphMessage::SendGraph { should_rerender });
if network.connected_to_output(node_id) {
responses.add(NodeGraphMessage::RunDocumentGraph);
}
}
NodeGraphMessage::Copy => {
let Some(network) = document_network.nested_network(&self.network) else {
@@ -537,7 +594,7 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
};
// Collect the selected nodes
let new_ids = &metadata.selected_nodes().copied().enumerate().map(|(new, old)| (old, NodeId(new as u64))).collect();
let new_ids = &document_metadata.selected_nodes().copied().enumerate().map(|(new, old)| (old, NodeId(new as u64))).collect();
let copied_nodes: Vec<_> = Self::copy_nodes(network, new_ids).collect();
// Prefix to show that this is nodes
@@ -564,28 +621,24 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
graph_craft::document::DocumentNodeMetadata::position((x, y)),
);
responses.add(NodeGraphMessage::InsertNode { node_id, document_node });
responses.add(NodeGraphMessage::SendGraph { should_rerender: false });
}
NodeGraphMessage::Cut => {
responses.add(NodeGraphMessage::Copy);
responses.add(NodeGraphMessage::DeleteSelectedNodes { reconnect: true });
}
NodeGraphMessage::DeleteNode { node_id, reconnect } => {
self.remove_node(document_network, metadata, node_id, responses, reconnect);
self.remove_node(document_network, document_metadata, node_id, responses, reconnect);
}
NodeGraphMessage::DeleteSelectedNodes { reconnect } => {
responses.add(DocumentMessage::StartTransaction);
for node_id in metadata.selected_nodes().copied() {
for node_id in document_metadata.selected_nodes().copied() {
responses.add(NodeGraphMessage::DeleteNode { node_id, reconnect });
}
responses.add(NodeGraphMessage::SendGraph { should_rerender: false });
if let Some(network) = document_network.nested_network(&self.network) {
// Only generate node graph if one of the selected nodes is connected to the output
if metadata.selected_nodes().any(|&node_id| network.connected_to_output(node_id)) {
if document_metadata.selected_nodes().any(|&node_id| network.connected_to_output(node_id)) {
responses.add(NodeGraphMessage::RunDocumentGraph);
}
}
@@ -615,34 +668,35 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
}
responses.add(NodeGraphMessage::SetNodeInput { node_id, input_index, input });
let should_rerender = network.connected_to_output(node_id);
responses.add(NodeGraphMessage::SendGraph { should_rerender });
if network.connected_to_output(node_id) {
responses.add(NodeGraphMessage::RunDocumentGraph);
}
}
NodeGraphMessage::DoubleClickNode { node } => {
NodeGraphMessage::EnterNestedNetwork { node } => {
if let Some(network) = document_network.nested_network(&self.network) {
if network.nodes.get(&node).and_then(|node| node.implementation.get_network()).is_some() {
self.network.push(node);
}
}
if let Some(network) = document_network.nested_network(&self.network) {
self.send_graph(network, graph_view_overlay_open, responses);
self.send_graph(network, graph_view_overlay_open, document_metadata, collapsed, responses);
}
self.update_selected(document_network, metadata, responses);
self.update_selected(document_network, document_metadata, responses);
}
NodeGraphMessage::DuplicateSelectedNodes => {
if let Some(network) = document_network.nested_network(&self.network) {
responses.add(DocumentMessage::StartTransaction);
let new_ids = &metadata.selected_nodes().map(|&id| (id, NodeId(generate_uuid()))).collect();
let new_ids = &document_metadata.selected_nodes().map(|&id| (id, NodeId(generate_uuid()))).collect();
metadata.clear_selected_nodes();
document_metadata.clear_selected_nodes();
responses.add(BroadcastEvent::SelectionChanged);
// Copy the selected nodes
let copied_nodes = Self::copy_nodes(network, new_ids).collect::<Vec<_>>();
// Select the new nodes
metadata.add_selected_nodes(copied_nodes.iter().map(|(node_id, _)| *node_id));
document_metadata.add_selected_nodes(copied_nodes.iter().map(|(node_id, _)| *node_id));
responses.add(BroadcastEvent::SelectionChanged);
for (node_id, mut document_node) in copied_nodes {
@@ -653,22 +707,20 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
responses.add(NodeGraphMessage::InsertNode { node_id, document_node });
}
self.send_graph(network, graph_view_overlay_open, responses);
self.update_selected(document_network, metadata, responses);
responses.add(NodeGraphMessage::SendGraph { should_rerender: false });
self.update_selected(document_network, document_metadata, responses);
}
}
NodeGraphMessage::ExitNestedNetwork { depth_of_nesting } => {
metadata.clear_selected_nodes();
document_metadata.clear_selected_nodes();
responses.add(BroadcastEvent::SelectionChanged);
for _ in 0..depth_of_nesting {
self.network.pop();
}
if let Some(network) = document_network.nested_network(&self.network) {
self.send_graph(network, graph_view_overlay_open, responses);
self.send_graph(network, graph_view_overlay_open, document_metadata, collapsed, responses);
}
self.update_selected(document_network, metadata, responses);
self.update_selected(document_network, document_metadata, responses);
}
NodeGraphMessage::ExposeInput { node_id, input_index, new_exposed } => {
let Some(network) = document_network.nested_network(&self.network) else {
@@ -696,8 +748,6 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
}
responses.add(NodeGraphMessage::SetNodeInput { node_id, input_index, input });
let should_rerender = network.connected_to_output(node_id);
responses.add(NodeGraphMessage::SendGraph { should_rerender });
responses.add(PropertiesPanelMessage::Refresh);
}
NodeGraphMessage::InsertNode { node_id, document_node } => {
@@ -711,12 +761,12 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
return;
};
for node_id in metadata.selected_nodes() {
for node_id in document_metadata.selected_nodes() {
if let Some(node) = network.nodes.get_mut(node_id) {
node.metadata.position += IVec2::new(displacement_x, displacement_y)
}
}
self.send_graph(network, graph_view_overlay_open, responses);
self.send_graph(network, graph_view_overlay_open, document_metadata, collapsed, responses);
}
NodeGraphMessage::PasteNodes { serialized_nodes } => {
let Some(network) = document_network.nested_network(&self.network) else {
@@ -762,29 +812,26 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
let nodes = new_ids.values().copied().collect();
responses.add(NodeGraphMessage::SelectedNodesSet { nodes });
responses.add(NodeGraphMessage::SendGraph { should_rerender: false });
}
NodeGraphMessage::RunDocumentGraph => responses.add(PortfolioMessage::SubmitGraphRender { document_id }),
NodeGraphMessage::RunDocumentGraph => {
responses.add(PortfolioMessage::SubmitGraphRender { document_id });
}
NodeGraphMessage::SelectedNodesAdd { nodes } => {
metadata.add_selected_nodes(nodes);
document_metadata.add_selected_nodes(nodes);
responses.add(BroadcastEvent::SelectionChanged);
}
NodeGraphMessage::SelectedNodesRemove { nodes } => {
metadata.retain_selected_nodes(|node| !nodes.contains(node));
document_metadata.retain_selected_nodes(|node| !nodes.contains(node));
responses.add(BroadcastEvent::SelectionChanged);
}
NodeGraphMessage::SelectedNodesSet { nodes } => {
metadata.set_selected_nodes(nodes);
document_metadata.set_selected_nodes(nodes);
responses.add(BroadcastEvent::SelectionChanged);
responses.add(PropertiesPanelMessage::Refresh);
}
NodeGraphMessage::SendGraph { should_rerender } => {
NodeGraphMessage::SendGraph => {
if let Some(network) = document_network.nested_network(&self.network) {
self.send_graph(network, graph_view_overlay_open, responses);
if should_rerender {
responses.add(NodeGraphMessage::RunDocumentGraph);
}
self.send_graph(network, graph_view_overlay_open, document_metadata, collapsed, responses);
}
}
NodeGraphMessage::SetInputValue { node_id, input_index, value } => {
@@ -811,7 +858,7 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
let structure_changed = node_input.as_node().is_some() || input.as_node().is_some();
*node_input = input;
if structure_changed {
load_network_structure(document_network, metadata, collapsed);
load_network_structure(document_network, document_metadata, collapsed);
}
}
}
@@ -837,6 +884,7 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
}
}
}
// Move all the downstream nodes to the right in the graph to allow space for a newly inserted node
NodeGraphMessage::ShiftNode { node_id } => {
let Some(network) = document_network.nested_network_mut(&self.network) else {
warn!("No network");
@@ -883,14 +931,15 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
stack.extend(outwards_links.get(&id).unwrap_or(&Vec::new()).iter().copied())
}
}
responses.add(NodeGraphMessage::SendGraph { should_rerender: false });
self.send_graph(network, graph_view_overlay_open, document_metadata, collapsed, responses);
}
NodeGraphMessage::ToggleSelectedHidden => {
if let Some(network) = document_network.nested_network(&self.network) {
responses.add(DocumentMessage::StartTransaction);
let new_hidden = !metadata.selected_nodes().any(|id| network.disabled.contains(id));
for &node_id in metadata.selected_nodes() {
let new_hidden = !document_metadata.selected_nodes().any(|id| network.disabled.contains(id));
for &node_id in document_metadata.selected_nodes() {
responses.add(NodeGraphMessage::SetHidden { node_id, hidden: new_hidden });
}
}
@@ -908,14 +957,13 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
} else if !network.inputs.contains(&node_id) && !network.original_outputs().iter().any(|output| output.node_id == node_id) {
network.disabled.push(node_id);
}
self.send_graph(network, graph_view_overlay_open, responses);
// Only generate node graph if one of the selected nodes is connected to the output
if network.connected_to_output(node_id) {
responses.add(NodeGraphMessage::RunDocumentGraph);
}
}
self.update_selection_action_buttons(document_network, metadata, responses);
self.update_selection_action_buttons(document_network, document_metadata, responses);
}
NodeGraphMessage::SetName { node_id, name } => {
responses.add(DocumentMessage::StartTransaction);
@@ -925,7 +973,8 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
if let Some(network) = document_network.nested_network_mut(&self.network) {
if let Some(node) = network.nodes.get_mut(&node_id) {
node.alias = name;
responses.add(NodeGraphMessage::SendGraph { should_rerender: false });
self.send_graph(network, graph_view_overlay_open, document_metadata, collapsed, responses);
}
}
}
@@ -944,37 +993,30 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
} else {
return;
}
self.send_graph(network, graph_view_overlay_open, responses);
}
self.update_selection_action_buttons(document_network, metadata, responses);
self.update_selection_action_buttons(document_network, document_metadata, responses);
responses.add(NodeGraphMessage::RunDocumentGraph);
}
NodeGraphMessage::UpdateNewNodeGraph => {
if let Some(network) = document_network.nested_network(&self.network) {
metadata.clear_selected_nodes();
document_metadata.clear_selected_nodes();
responses.add(BroadcastEvent::SelectionChanged);
self.send_graph(network, graph_view_overlay_open, responses);
self.send_graph(network, graph_view_overlay_open, document_metadata, collapsed, responses);
let node_types = document_node_types::collect_node_types();
responses.add(FrontendMessage::UpdateNodeTypes { node_types });
}
self.update_selected(document_network, metadata, responses);
self.update_selected(document_network, document_metadata, responses);
}
NodeGraphMessage::UpdateTypes { resolved_types, node_graph_errors } => {
let changed = self.resolved_types != resolved_types || self.node_graph_errors != node_graph_errors;
self.resolved_types = resolved_types;
self.node_graph_errors = node_graph_errors;
if changed {
if let Some(network) = document_network.nested_network(&self.network) {
self.send_graph(network, graph_view_overlay_open, responses)
}
}
}
}
self.has_selection = metadata.has_selected_nodes();
self.has_selection = document_metadata.has_selected_nodes();
}
fn actions(&self) -> ActionList {
@@ -39,15 +39,14 @@ pub enum LayerClassification {
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, specta::Type)]
pub struct LayerPanelEntry {
pub id: NodeId,
pub name: String,
pub tooltip: String,
#[serde(rename = "layerClassification")]
pub layer_classification: LayerClassification,
pub selected: bool,
pub expanded: bool,
pub disabled: bool,
#[serde(rename = "parentId")]
pub parent_id: Option<NodeId>,
pub id: NodeId,
pub depth: usize,
pub thumbnail: String,
}
@@ -1,5 +1,3 @@
pub use super::layer_panel::LayerPanelEntry;
use glam::DVec2;
use serde::{Deserialize, Serialize};
use std::fmt;