Skip to content

Commit 7b76b8c

Browse files
committed
Initial work migrating vector layers to document graph
1 parent d541fa3 commit 7b76b8c

File tree

4 files changed

+66
-31
lines changed

4 files changed

+66
-31
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::messages::prelude::*;
22

3+
use bezier_rs::Subpath;
34
use graph_craft::document::NodeId;
45
use graphene_core::uuid::ManipulatorGroupId;
56
use graphene_core::vector::brush_stroke::BrushStroke;
@@ -58,6 +59,10 @@ pub enum GraphOperationMessage {
5859
id: NodeId,
5960
artboard: Artboard,
6061
},
62+
NewVectorLayer {
63+
id: NodeId,
64+
subpaths: Vec<Subpath<ManipulatorGroupId>>,
65+
},
6166
ResizeArtboard {
6267
id: NodeId,
6368
location: IVec2,

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

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use super::{resolve_document_node_type, VectorDataModification};
22
use crate::messages::prelude::*;
33

4+
use bezier_rs::Subpath;
45
use document_legacy::document::Document;
56
use document_legacy::{LayerId, Operation};
67
use graph_craft::document::value::TaggedValue;
78
use graph_craft::document::{generate_uuid, DocumentNode, DocumentNodeMetadata, NodeId, NodeInput, NodeNetwork, NodeOutput};
9+
use graphene_core::uuid::ManipulatorGroupId;
810
use graphene_core::vector::brush_stroke::BrushStroke;
911
use graphene_core::vector::style::{Fill, FillType, Stroke};
1012
use graphene_core::Artboard;
@@ -27,19 +29,7 @@ struct ModifyInputsContext<'a> {
2729
}
2830
impl<'a> ModifyInputsContext<'a> {
2931
/// Get the node network from the document
30-
fn new(layer: &'a [LayerId], document: &'a mut Document, node_graph: &'a mut NodeGraphMessageHandler, responses: &'a mut VecDeque<Message>) -> Option<Self> {
31-
document.layer_mut(layer).ok().and_then(|layer| layer.as_layer_network_mut().ok()).map(|network| Self {
32-
outwards_links: network.collect_outwards_links(),
33-
network,
34-
node_graph,
35-
responses,
36-
layer,
37-
layer_node: None,
38-
})
39-
}
40-
41-
/// Get the node network from the document
42-
fn new_doc(document: &'a mut Document, node_graph: &'a mut NodeGraphMessageHandler, responses: &'a mut VecDeque<Message>) -> Self {
32+
fn new(document: &'a mut Document, node_graph: &'a mut NodeGraphMessageHandler, responses: &'a mut VecDeque<Message>) -> Self {
4333
Self {
4434
outwards_links: document.document_network.collect_outwards_links(),
4535
network: &mut document.document_network,
@@ -50,12 +40,18 @@ impl<'a> ModifyInputsContext<'a> {
5040
}
5141
}
5242

53-
fn locate_layer(&mut self, mut id: NodeId) -> Option<NodeId> {
54-
while self.network.nodes.get(&id)?.name != "Layer" {
55-
id = self.outwards_links.get(&id)?.first().copied()?;
43+
/// Get the node network from the document
44+
fn new_layer(layer: &'a [LayerId], document: &'a mut Document, node_graph: &'a mut NodeGraphMessageHandler, responses: &'a mut VecDeque<Message>) -> Option<Self> {
45+
let mut document = Self::new(document, node_graph, responses);
46+
let Some(mut id) = layer.last().copied() else {
47+
error!("Tried to modify root layer");
48+
return None;
49+
};
50+
while document.network.nodes.get(&id)?.name != "Layer" {
51+
id = document.outwards_links.get(&id)?.first().copied()?;
5652
}
57-
self.layer_node = Some(id);
58-
Some(id)
53+
document.layer_node = Some(id);
54+
Some(document)
5955
}
6056

6157
/// Updates the input of an existing node
@@ -132,6 +128,25 @@ impl<'a> ModifyInputsContext<'a> {
132128
self.insert_node_before(generate_uuid(), layer, 0, artboard_node, IVec2::new(-8, 0))
133129
}
134130

131+
fn insert_vector_data(&mut self, subpaths: Vec<Subpath<ManipulatorGroupId>>, layer: NodeId) {
132+
let path_generator = {
133+
let node_type = resolve_document_node_type("Path Generator").expect("Path Generator node does not exist");
134+
node_type.to_document_node_default_inputs([Some(NodeInput::value(TaggedValue::Subpaths(subpaths), false))], Default::default())
135+
};
136+
let transform = resolve_document_node_type("Transform").expect("Transform node does not exist").default_document_node();
137+
let fill = resolve_document_node_type("Fill").expect("Fill node does not exist").default_document_node();
138+
let stroke = resolve_document_node_type("Stroke").expect("Stroke node does not exist").default_document_node();
139+
140+
let stroke_id = generate_uuid();
141+
self.insert_node_before(stroke_id, layer, 0, stroke, IVec2::new(-8, 0));
142+
let fill_id = generate_uuid();
143+
self.insert_node_before(fill_id, stroke_id, 0, fill, IVec2::new(-8, 0));
144+
let transform_id = generate_uuid();
145+
self.insert_node_before(transform_id, fill_id, 0, transform, IVec2::new(-8, 0));
146+
let path_generator_id = generate_uuid();
147+
self.insert_node_before(path_generator_id, transform_id, 0, path_generator, IVec2::new(-8, 0));
148+
}
149+
135150
fn shift_upstream(&mut self, node_id: NodeId, shift: IVec2) {
136151
let mut shift_nodes = HashSet::new();
137152
let mut stack = vec![node_id];
@@ -378,19 +393,19 @@ impl MessageHandler<GraphOperationMessage, (&mut Document, &mut NodeGraphMessage
378393
fn process_message(&mut self, message: GraphOperationMessage, responses: &mut VecDeque<Message>, (document, node_graph): (&mut Document, &mut NodeGraphMessageHandler)) {
379394
match message {
380395
GraphOperationMessage::FillSet { layer, fill } => {
381-
if let Some(mut modify_inputs) = ModifyInputsContext::new(&layer, document, node_graph, responses) {
396+
if let Some(mut modify_inputs) = ModifyInputsContext::new_layer(&layer, document, node_graph, responses) {
382397
modify_inputs.fill_set(fill);
383398
} else {
384399
responses.add(Operation::SetLayerFill { path: layer, fill });
385400
}
386401
}
387402
GraphOperationMessage::UpdateBounds { layer, old_bounds, new_bounds } => {
388-
if let Some(mut modify_inputs) = ModifyInputsContext::new(&layer, document, node_graph, responses) {
403+
if let Some(mut modify_inputs) = ModifyInputsContext::new_layer(&layer, document, node_graph, responses) {
389404
modify_inputs.update_bounds(old_bounds, new_bounds);
390405
}
391406
}
392407
GraphOperationMessage::StrokeSet { layer, stroke } => {
393-
if let Some(mut modify_inputs) = ModifyInputsContext::new(&layer, document, node_graph, responses) {
408+
if let Some(mut modify_inputs) = ModifyInputsContext::new_layer(&layer, document, node_graph, responses) {
394409
modify_inputs.stroke_set(stroke);
395410
} else {
396411
responses.add(Operation::SetLayerStroke { path: layer, stroke });
@@ -404,7 +419,7 @@ impl MessageHandler<GraphOperationMessage, (&mut Document, &mut NodeGraphMessage
404419
} => {
405420
let parent_transform = document.multiply_transforms(&layer[..layer.len() - 1]).unwrap_or_default();
406421
let bounds = LayerBounds::new(document, &layer);
407-
if let Some(mut modify_inputs) = ModifyInputsContext::new(&layer, document, node_graph, responses) {
422+
if let Some(mut modify_inputs) = ModifyInputsContext::new_layer(&layer, document, node_graph, responses) {
408423
modify_inputs.transform_change(transform, transform_in, parent_transform, bounds, skip_rerender);
409424
}
410425

@@ -427,7 +442,7 @@ impl MessageHandler<GraphOperationMessage, (&mut Document, &mut NodeGraphMessage
427442
let parent_transform = document.multiply_transforms(&layer[..layer.len() - 1]).unwrap_or_default();
428443
let current_transform = document.layer(&layer).ok().map(|layer| layer.transform);
429444
let bounds = LayerBounds::new(document, &layer);
430-
if let Some(mut modify_inputs) = ModifyInputsContext::new(&layer, document, node_graph, responses) {
445+
if let Some(mut modify_inputs) = ModifyInputsContext::new_layer(&layer, document, node_graph, responses) {
431446
modify_inputs.transform_set(transform, transform_in, parent_transform, current_transform, bounds, skip_rerender);
432447
}
433448
let transform = transform.to_cols_array();
@@ -442,41 +457,46 @@ impl MessageHandler<GraphOperationMessage, (&mut Document, &mut NodeGraphMessage
442457
}
443458
GraphOperationMessage::TransformSetPivot { layer, pivot } => {
444459
let bounds = LayerBounds::new(document, &layer);
445-
if let Some(mut modify_inputs) = ModifyInputsContext::new(&layer, document, node_graph, responses) {
460+
if let Some(mut modify_inputs) = ModifyInputsContext::new_layer(&layer, document, node_graph, responses) {
446461
modify_inputs.pivot_set(pivot, bounds);
447462
}
448463

449464
let pivot = pivot.into();
450465
responses.add(Operation::SetPivot { layer_path: layer, pivot });
451466
}
452467
GraphOperationMessage::Vector { layer, modification } => {
453-
if let Some(mut modify_inputs) = ModifyInputsContext::new(&layer, document, node_graph, responses) {
468+
if let Some(mut modify_inputs) = ModifyInputsContext::new_layer(&layer, document, node_graph, responses) {
454469
modify_inputs.vector_modify(modification);
455470
}
456471
}
457472
GraphOperationMessage::Brush { layer, strokes } => {
458-
if let Some(mut modify_inputs) = ModifyInputsContext::new(&layer, document, node_graph, responses) {
473+
if let Some(mut modify_inputs) = ModifyInputsContext::new_layer(&layer, document, node_graph, responses) {
459474
modify_inputs.brush_modify(strokes);
460475
}
461476
}
462477
GraphOperationMessage::NewArtboard { id, artboard } => {
463-
let mut modify_inputs = ModifyInputsContext::new_doc(document, node_graph, responses);
478+
let mut modify_inputs = ModifyInputsContext::new(document, node_graph, responses);
464479
if let Some(layer) = modify_inputs.create_layer(id, modify_inputs.network.outputs[0].node_id) {
465480
modify_inputs.insert_artboard(artboard, layer);
466481
}
467482
}
483+
GraphOperationMessage::NewVectorLayer { id, subpaths } => {
484+
let mut modify_inputs = ModifyInputsContext::new(document, node_graph, responses);
485+
if let Some(layer) = modify_inputs.create_layer(id, modify_inputs.network.outputs[0].node_id) {
486+
modify_inputs.insert_vector_data(subpaths, layer);
487+
}
488+
}
468489
GraphOperationMessage::ResizeArtboard { id, location, dimensions } => {
469-
let mut modify_inputs = ModifyInputsContext::new_doc(document, node_graph, responses);
470-
if modify_inputs.locate_layer(id).is_some() {
490+
if let Some(mut modify_inputs) = ModifyInputsContext::new_layer(&[id], document, node_graph, responses) {
471491
modify_inputs.resize_artboard(location, dimensions);
472492
}
473493
}
474494
GraphOperationMessage::DeleteArtboard { id } => {
475-
let mut modify_inputs = ModifyInputsContext::new_doc(document, node_graph, responses);
495+
let mut modify_inputs = ModifyInputsContext::new(document, node_graph, responses);
476496
modify_inputs.delete_layer(id);
477497
}
478498
GraphOperationMessage::ClearArtboards => {
479-
let mut modify_inputs = ModifyInputsContext::new_doc(document, node_graph, responses);
499+
let mut modify_inputs = ModifyInputsContext::new(document, node_graph, responses);
480500
let artboard_nodes = modify_inputs.network.nodes.iter().filter(|(_, node)| node.name == "Artboard").map(|(id, _)| *id).collect::<Vec<_>>();
481501
for id in artboard_nodes {
482502
modify_inputs.delete_layer(id);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,6 +1920,11 @@ impl DocumentNodeType {
19201920
let inputs = self.inputs.iter().map(|default| input_override.next().unwrap_or_default().unwrap_or_else(|| default.default.clone()));
19211921
self.to_document_node(inputs, metadata)
19221922
}
1923+
1924+
/// Converts the [DocumentNodeType] type to a [DocumentNode], completly default
1925+
pub fn default_document_node(&self) -> DocumentNode {
1926+
self.to_document_node(self.inputs.iter().map(|input| input.default.clone()), DocumentNodeMetadata::default())
1927+
}
19231928
}
19241929

19251930
pub fn wrap_network_in_scope(mut network: NodeNetwork) -> NodeNetwork {

editor/src/messages/tool/common_functionality/graph_modification_utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ use std::collections::VecDeque;
1212

1313
/// Create a new vector layer from a vector of [`bezier_rs::Subpath`].
1414
pub fn new_vector_layer(subpaths: Vec<Subpath<ManipulatorGroupId>>, layer_path: Vec<LayerId>, responses: &mut VecDeque<Message>) {
15+
responses.add(GraphOperationMessage::NewVectorLayer {
16+
id: *layer_path.last().unwrap(),
17+
subpaths: subpaths.clone(),
18+
});
19+
1520
let network = node_graph::new_vector_network(subpaths);
1621
new_custom_layer(network, layer_path, responses);
1722
}

0 commit comments

Comments
 (0)