1
1
use crate :: messages:: layout:: utility_types:: layout_widget:: { Layout , LayoutGroup , Widget , WidgetCallback , WidgetHolder , WidgetLayout } ;
2
- use crate :: messages:: layout:: utility_types:: widgets:: button_widgets:: BreadcrumbTrailButtons ;
2
+ use crate :: messages:: layout:: utility_types:: widgets:: button_widgets:: { BreadcrumbTrailButtons , TextButton } ;
3
3
use crate :: messages:: prelude:: * ;
4
4
5
5
use document_legacy:: document:: Document ;
@@ -65,6 +65,8 @@ pub struct FrontendNode {
65
65
pub exposed_inputs : Vec < NodeGraphInput > ,
66
66
pub outputs : Vec < FrontendGraphDataType > ,
67
67
pub position : ( i32 , i32 ) ,
68
+ pub disabled : bool ,
69
+ pub output : bool ,
68
70
}
69
71
70
72
// (link_start, link_end, link_end_input_index)
@@ -92,11 +94,13 @@ impl FrontendNodeType {
92
94
}
93
95
}
94
96
95
- #[ derive( Debug , Clone , Eq , PartialEq , Default , serde:: Serialize , serde:: Deserialize ) ]
97
+ #[ derive( Debug , Clone , PartialEq , Default , serde:: Serialize , serde:: Deserialize ) ]
96
98
pub struct NodeGraphMessageHandler {
97
99
pub layer_path : Option < Vec < document_legacy:: LayerId > > ,
98
100
pub nested_path : Vec < graph_craft:: document:: NodeId > ,
99
101
pub selected_nodes : Vec < graph_craft:: document:: NodeId > ,
102
+ #[ serde( skip) ]
103
+ pub widgets : [ LayoutGroup ; 2 ] ,
100
104
}
101
105
102
106
impl NodeGraphMessageHandler {
@@ -124,8 +128,20 @@ impl NodeGraphMessageHandler {
124
128
network
125
129
}
126
130
131
+ /// Send the cached layout for the bar at the top of the node panel to the frontend
132
+ fn send_node_bar_layout ( & self , responses : & mut VecDeque < Message > ) {
133
+ responses. push_back (
134
+ LayoutMessage :: SendLayout {
135
+ layout : Layout :: WidgetLayout ( WidgetLayout :: new ( self . widgets . to_vec ( ) ) ) ,
136
+ layout_target : crate :: messages:: layout:: utility_types:: misc:: LayoutTarget :: NodeGraphBar ,
137
+ }
138
+ . into ( ) ,
139
+ ) ;
140
+ }
141
+
127
142
/// Collect the addresses of the currently viewed nested node e.g. Root -> MyFunFilter -> Exposure
128
- fn collect_nested_addresses ( & self , document : & Document , responses : & mut VecDeque < Message > ) {
143
+ fn collect_nested_addresses ( & mut self , document : & Document , responses : & mut VecDeque < Message > ) {
144
+ // Build path list
129
145
let mut path = vec ! [ "Root" . to_string( ) ] ;
130
146
let mut network = self . get_root_network ( document) ;
131
147
for node_id in & self . nested_path {
@@ -137,24 +153,56 @@ impl NodeGraphMessageHandler {
137
153
}
138
154
let nesting = path. len ( ) ;
139
155
140
- responses. push_back (
141
- LayoutMessage :: SendLayout {
142
- layout : Layout :: WidgetLayout ( WidgetLayout :: new ( vec ! [ LayoutGroup :: Row {
143
- widgets: vec![ WidgetHolder :: new( Widget :: BreadcrumbTrailButtons ( BreadcrumbTrailButtons {
144
- labels: path,
145
- on_update: WidgetCallback :: new( move |input: & u64 | {
146
- NodeGraphMessage :: ExitNestedNetwork {
147
- depth_of_nesting: nesting - ( * input as usize ) - 1 ,
148
- }
149
- . into( )
150
- } ) ,
156
+ // Update UI
157
+ self . widgets [ 0 ] = LayoutGroup :: Row {
158
+ widgets : vec ! [ WidgetHolder :: new( Widget :: BreadcrumbTrailButtons ( BreadcrumbTrailButtons {
159
+ labels: path. clone( ) ,
160
+ on_update: WidgetCallback :: new( move |input: & u64 | {
161
+ NodeGraphMessage :: ExitNestedNetwork {
162
+ depth_of_nesting: nesting - ( * input as usize ) - 1 ,
163
+ }
164
+ . into( )
165
+ } ) ,
166
+ ..Default :: default ( )
167
+ } ) ) ] ,
168
+ } ;
169
+
170
+ self . send_node_bar_layout ( responses) ;
171
+ }
172
+
173
+ /// Updates the buttons for disable and preview
174
+ fn update_selection_action_buttons ( & mut self , document : & mut Document , responses : & mut VecDeque < Message > ) {
175
+ if let Some ( network) = self . get_active_network_mut ( document) {
176
+ let mut widgets = Vec :: new ( ) ;
177
+
178
+ // Show an enable or disable nodes button if there is a selection
179
+ if !self . selected_nodes . is_empty ( ) {
180
+ let is_enabled = self . selected_nodes . iter ( ) . any ( |id| !network. disabled . contains ( id) ) ;
181
+ let enable_button = WidgetHolder :: new ( Widget :: TextButton ( TextButton {
182
+ label : if is_enabled { "Disable" } else { "Enable" } . to_string ( ) ,
183
+ on_update : WidgetCallback :: new ( move |_| NodeGraphMessage :: SetSelectedEnabled { enabled : !is_enabled } . into ( ) ) ,
184
+ ..Default :: default ( )
185
+ } ) ) ;
186
+ widgets. push ( enable_button) ;
187
+ }
188
+
189
+ // If only one node is selected then show the preview or stop previewing button
190
+ if self . selected_nodes . len ( ) == 1 {
191
+ let is_output = network. output == self . selected_nodes [ 0 ] ;
192
+ // Don't show stop previewing on the output node
193
+ if !( is_output && network. previous_output . filter ( |& id| id != self . selected_nodes [ 0 ] ) . is_none ( ) ) {
194
+ let output_button = WidgetHolder :: new ( Widget :: TextButton ( TextButton {
195
+ label : if is_output { "Stop Previewing" } else { "Preview" } . to_string ( ) ,
196
+ on_update : WidgetCallback :: new ( move |_| NodeGraphMessage :: SetSelectedOutput { output : !is_output } . into ( ) ) ,
151
197
..Default :: default ( )
152
- } ) ) ] ,
153
- } ] ) ) ,
154
- layout_target : crate :: messages :: layout :: utility_types :: misc :: LayoutTarget :: NodeGraphBar ,
198
+ } ) ) ;
199
+ widgets . push ( output_button ) ;
200
+ }
155
201
}
156
- . into ( ) ,
157
- ) ;
202
+
203
+ self . widgets [ 1 ] = LayoutGroup :: Row { widgets } ;
204
+ }
205
+ self . send_node_bar_layout ( responses) ;
158
206
}
159
207
160
208
pub fn collate_properties ( & self , node_graph_frame : & NodeGraphFrameLayer , context : & mut NodePropertiesContext , sections : & mut Vec < LayoutGroup > ) {
@@ -237,14 +285,17 @@ impl NodeGraphMessageHandler {
237
285
. collect ( ) ,
238
286
outputs : node_type. outputs . to_vec ( ) ,
239
287
position : node. metadata . position ,
288
+ output : network. output == * id,
289
+ disabled : network. disabled . contains ( id) ,
240
290
} )
241
291
}
242
292
log:: debug!( "Frontend Nodes:\n {:#?}\n \n Links:\n {:#?}" , nodes, links) ;
243
293
responses. push_back ( FrontendMessage :: UpdateNodeGraph { nodes, links } . into ( ) ) ;
244
294
}
245
295
246
- /// Updates the frontend's selection state inline with the backend
247
- fn update_selected ( & self , responses : & mut VecDeque < Message > ) {
296
+ /// Updates the frontend's selection state in line with the backend
297
+ fn update_selected ( & mut self , document : & mut Document , responses : & mut VecDeque < Message > ) {
298
+ self . update_selection_action_buttons ( document, responses) ;
248
299
responses. push_back (
249
300
FrontendMessage :: UpdateNodeGraphSelection {
250
301
selected : self . selected_nodes . clone ( ) ,
@@ -388,6 +439,7 @@ impl MessageHandler<NodeGraphMessage, (&mut Document, &InputPreprocessorMessageH
388
439
) ]
389
440
. into_iter ( )
390
441
. collect ( ) ,
442
+ ..Default :: default ( )
391
443
} ;
392
444
393
445
network. nodes . insert (
@@ -413,6 +465,7 @@ impl MessageHandler<NodeGraphMessage, (&mut Document, &InputPreprocessorMessageH
413
465
responses. push_back ( DocumentMessage :: NodeGraphFrameGenerate . into ( ) ) ;
414
466
}
415
467
}
468
+ self . update_selected ( document, responses) ;
416
469
}
417
470
NodeGraphMessage :: DeleteSelectedNodes => {
418
471
if let Some ( network) = self . get_active_network_mut ( document) {
@@ -425,6 +478,7 @@ impl MessageHandler<NodeGraphMessage, (&mut Document, &InputPreprocessorMessageH
425
478
responses. push_back ( DocumentMessage :: NodeGraphFrameGenerate . into ( ) ) ;
426
479
}
427
480
}
481
+ self . update_selected ( document, responses) ;
428
482
}
429
483
NodeGraphMessage :: DisconnectNodes { node_id, input_index } => {
430
484
let Some ( network) = self . get_active_network_mut ( document) else {
@@ -443,7 +497,7 @@ impl MessageHandler<NodeGraphMessage, (&mut Document, &InputPreprocessorMessageH
443
497
Self :: send_graph ( network, responses) ;
444
498
}
445
499
NodeGraphMessage :: DoubleClickNode { node } => {
446
- self . selected_nodes = Vec :: new ( ) ;
500
+ self . selected_nodes . clear ( ) ;
447
501
if let Some ( network) = self . get_active_network_mut ( document) {
448
502
if network. nodes . get ( & node) . and_then ( |node| node. implementation . get_network ( ) ) . is_some ( ) {
449
503
self . nested_path . push ( node) ;
@@ -453,7 +507,7 @@ impl MessageHandler<NodeGraphMessage, (&mut Document, &InputPreprocessorMessageH
453
507
Self :: send_graph ( network, responses) ;
454
508
}
455
509
self . collect_nested_addresses ( document, responses) ;
456
- self . update_selected ( responses) ;
510
+ self . update_selected ( document , responses) ;
457
511
}
458
512
NodeGraphMessage :: DuplicateSelectedNodes => {
459
513
if let Some ( network) = self . get_active_network_mut ( document) {
@@ -476,18 +530,19 @@ impl MessageHandler<NodeGraphMessage, (&mut Document, &InputPreprocessorMessageH
476
530
}
477
531
self . selected_nodes = new_selected;
478
532
Self :: send_graph ( network, responses) ;
479
- self . update_selected ( responses) ;
533
+ self . update_selected ( document , responses) ;
480
534
}
481
535
}
482
536
NodeGraphMessage :: ExitNestedNetwork { depth_of_nesting } => {
483
- self . selected_nodes = Vec :: new ( ) ;
537
+ self . selected_nodes . clear ( ) ;
484
538
for _ in 0 ..depth_of_nesting {
485
539
self . nested_path . pop ( ) ;
486
540
}
487
541
if let Some ( network) = self . get_active_network_mut ( document) {
488
542
Self :: send_graph ( network, responses) ;
489
543
}
490
544
self . collect_nested_addresses ( document, responses) ;
545
+ self . update_selected ( document, responses) ;
491
546
}
492
547
NodeGraphMessage :: ExposeInput { node_id, input_index, new_exposed } => {
493
548
let Some ( network) = self . get_active_network_mut ( document) else {
@@ -542,6 +597,7 @@ impl MessageHandler<NodeGraphMessage, (&mut Document, &InputPreprocessorMessageH
542
597
responses. push_back ( FrontendMessage :: UpdateNodeTypes { node_types } . into ( ) ) ;
543
598
}
544
599
self . collect_nested_addresses ( document, responses) ;
600
+ self . update_selected ( document, responses) ;
545
601
}
546
602
NodeGraphMessage :: PasteNodes { serialized_nodes } => {
547
603
let Some ( network) = self . get_active_network_mut ( document) else {
@@ -567,10 +623,11 @@ impl MessageHandler<NodeGraphMessage, (&mut Document, &InputPreprocessorMessageH
567
623
}
568
624
569
625
Self :: send_graph ( network, responses) ;
570
- self . update_selected ( responses) ;
626
+ self . update_selected ( document , responses) ;
571
627
}
572
628
NodeGraphMessage :: SelectNodes { nodes } => {
573
629
self . selected_nodes = nodes;
630
+ self . update_selection_action_buttons ( document, responses) ;
574
631
responses. push_back ( PropertiesPanelMessage :: ResendActiveProperties . into ( ) ) ;
575
632
}
576
633
NodeGraphMessage :: SetInputValue { node, input_index, value } => {
@@ -615,6 +672,31 @@ impl MessageHandler<NodeGraphMessage, (&mut Document, &InputPreprocessorMessageH
615
672
}
616
673
}
617
674
}
675
+ NodeGraphMessage :: SetSelectedEnabled { enabled } => {
676
+ if let Some ( network) = self . get_active_network_mut ( document) {
677
+ if enabled {
678
+ network. disabled . retain ( |id| !self . selected_nodes . contains ( id) ) ;
679
+ } else {
680
+ network. disabled . extend ( & self . selected_nodes ) ;
681
+ }
682
+ Self :: send_graph ( network, responses) ;
683
+ }
684
+ self . update_selection_action_buttons ( document, responses) ;
685
+ responses. push_back ( DocumentMessage :: NodeGraphFrameGenerate . into ( ) ) ;
686
+ }
687
+ NodeGraphMessage :: SetSelectedOutput { output } => {
688
+ if let Some ( network) = self . get_active_network_mut ( document) {
689
+ if output {
690
+ network. previous_output = Some ( network. previous_output . unwrap_or ( network. output ) ) ;
691
+ network. output = self . selected_nodes [ 0 ] ;
692
+ } else if let Some ( output) = network. previous_output . take ( ) {
693
+ network. output = output
694
+ }
695
+ Self :: send_graph ( network, responses) ;
696
+ }
697
+ self . update_selection_action_buttons ( document, responses) ;
698
+ responses. push_back ( DocumentMessage :: NodeGraphFrameGenerate . into ( ) ) ;
699
+ }
618
700
NodeGraphMessage :: ShiftNode { node_id } => {
619
701
let Some ( network) = self . get_active_network_mut ( document) else {
620
702
warn ! ( "No network" ) ;
0 commit comments