1
1
use super :: { resolve_document_node_type, VectorDataModification } ;
2
2
use crate :: messages:: prelude:: * ;
3
3
4
+ use bezier_rs:: Subpath ;
4
5
use document_legacy:: document:: Document ;
5
6
use document_legacy:: { LayerId , Operation } ;
6
7
use graph_craft:: document:: value:: TaggedValue ;
7
8
use graph_craft:: document:: { generate_uuid, DocumentNode , DocumentNodeMetadata , NodeId , NodeInput , NodeNetwork , NodeOutput } ;
9
+ use graphene_core:: uuid:: ManipulatorGroupId ;
8
10
use graphene_core:: vector:: brush_stroke:: BrushStroke ;
9
11
use graphene_core:: vector:: style:: { Fill , FillType , Stroke } ;
10
12
use graphene_core:: Artboard ;
@@ -27,19 +29,7 @@ struct ModifyInputsContext<'a> {
27
29
}
28
30
impl < ' a > ModifyInputsContext < ' a > {
29
31
/// 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 {
43
33
Self {
44
34
outwards_links : document. document_network . collect_outwards_links ( ) ,
45
35
network : & mut document. document_network ,
@@ -50,12 +40,18 @@ impl<'a> ModifyInputsContext<'a> {
50
40
}
51
41
}
52
42
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 ( ) ?;
56
52
}
57
- self . layer_node = Some ( id) ;
58
- Some ( id )
53
+ document . layer_node = Some ( id) ;
54
+ Some ( document )
59
55
}
60
56
61
57
/// Updates the input of an existing node
@@ -132,6 +128,25 @@ impl<'a> ModifyInputsContext<'a> {
132
128
self . insert_node_before ( generate_uuid ( ) , layer, 0 , artboard_node, IVec2 :: new ( -8 , 0 ) )
133
129
}
134
130
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
+
135
150
fn shift_upstream ( & mut self , node_id : NodeId , shift : IVec2 ) {
136
151
let mut shift_nodes = HashSet :: new ( ) ;
137
152
let mut stack = vec ! [ node_id] ;
@@ -378,19 +393,19 @@ impl MessageHandler<GraphOperationMessage, (&mut Document, &mut NodeGraphMessage
378
393
fn process_message ( & mut self , message : GraphOperationMessage , responses : & mut VecDeque < Message > , ( document, node_graph) : ( & mut Document , & mut NodeGraphMessageHandler ) ) {
379
394
match message {
380
395
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) {
382
397
modify_inputs. fill_set ( fill) ;
383
398
} else {
384
399
responses. add ( Operation :: SetLayerFill { path : layer, fill } ) ;
385
400
}
386
401
}
387
402
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) {
389
404
modify_inputs. update_bounds ( old_bounds, new_bounds) ;
390
405
}
391
406
}
392
407
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) {
394
409
modify_inputs. stroke_set ( stroke) ;
395
410
} else {
396
411
responses. add ( Operation :: SetLayerStroke { path : layer, stroke } ) ;
@@ -404,7 +419,7 @@ impl MessageHandler<GraphOperationMessage, (&mut Document, &mut NodeGraphMessage
404
419
} => {
405
420
let parent_transform = document. multiply_transforms ( & layer[ ..layer. len ( ) - 1 ] ) . unwrap_or_default ( ) ;
406
421
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) {
408
423
modify_inputs. transform_change ( transform, transform_in, parent_transform, bounds, skip_rerender) ;
409
424
}
410
425
@@ -427,7 +442,7 @@ impl MessageHandler<GraphOperationMessage, (&mut Document, &mut NodeGraphMessage
427
442
let parent_transform = document. multiply_transforms ( & layer[ ..layer. len ( ) - 1 ] ) . unwrap_or_default ( ) ;
428
443
let current_transform = document. layer ( & layer) . ok ( ) . map ( |layer| layer. transform ) ;
429
444
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) {
431
446
modify_inputs. transform_set ( transform, transform_in, parent_transform, current_transform, bounds, skip_rerender) ;
432
447
}
433
448
let transform = transform. to_cols_array ( ) ;
@@ -442,41 +457,46 @@ impl MessageHandler<GraphOperationMessage, (&mut Document, &mut NodeGraphMessage
442
457
}
443
458
GraphOperationMessage :: TransformSetPivot { layer, pivot } => {
444
459
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) {
446
461
modify_inputs. pivot_set ( pivot, bounds) ;
447
462
}
448
463
449
464
let pivot = pivot. into ( ) ;
450
465
responses. add ( Operation :: SetPivot { layer_path : layer, pivot } ) ;
451
466
}
452
467
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) {
454
469
modify_inputs. vector_modify ( modification) ;
455
470
}
456
471
}
457
472
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) {
459
474
modify_inputs. brush_modify ( strokes) ;
460
475
}
461
476
}
462
477
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) ;
464
479
if let Some ( layer) = modify_inputs. create_layer ( id, modify_inputs. network . outputs [ 0 ] . node_id ) {
465
480
modify_inputs. insert_artboard ( artboard, layer) ;
466
481
}
467
482
}
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
+ }
468
489
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) {
471
491
modify_inputs. resize_artboard ( location, dimensions) ;
472
492
}
473
493
}
474
494
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) ;
476
496
modify_inputs. delete_layer ( id) ;
477
497
}
478
498
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) ;
480
500
let artboard_nodes = modify_inputs. network . nodes . iter ( ) . filter ( |( _, node) | node. name == "Artboard" ) . map ( |( id, _) | * id) . collect :: < Vec < _ > > ( ) ;
481
501
for id in artboard_nodes {
482
502
modify_inputs. delete_layer ( id) ;
0 commit comments