-
Notifications
You must be signed in to change notification settings - Fork 408
/
Copy pathBaseNodeView.cs
1182 lines (969 loc) · 36.6 KB
/
BaseNodeView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.Experimental.GraphView;
using UnityEngine.UIElements;
using UnityEditor;
using System.Reflection;
using System;
using System.Collections;
using System.Linq;
using UnityEditor.UIElements;
using System.Text.RegularExpressions;
using Status = UnityEngine.UIElements.DropdownMenuAction.Status;
using NodeView = UnityEditor.Experimental.GraphView.Node;
namespace GraphProcessor
{
[NodeCustomEditor(typeof(BaseNode))]
public class BaseNodeView : NodeView
{
public BaseNode nodeTarget;
public List< PortView > inputPortViews = new List< PortView >();
public List< PortView > outputPortViews = new List< PortView >();
public BaseGraphView owner { private set; get; }
protected Dictionary< string, List< PortView > > portsPerFieldName = new Dictionary< string, List< PortView > >();
public VisualElement controlsContainer;
protected VisualElement debugContainer;
protected VisualElement rightTitleContainer;
protected VisualElement topPortContainer;
protected VisualElement bottomPortContainer;
private VisualElement inputContainerElement;
VisualElement settings;
NodeSettingsView settingsContainer;
Button settingButton;
TextField titleTextField;
Label computeOrderLabel = new Label();
public event Action< PortView > onPortConnected;
public event Action< PortView > onPortDisconnected;
protected virtual bool hasSettings { get; set; }
public bool initializing = false; //Used for applying SetPosition on locked node at init.
readonly string baseNodeStyle = "GraphProcessorStyles/BaseNodeView";
bool settingsExpanded = false;
[System.NonSerialized]
List< IconBadge > badges = new List< IconBadge >();
private List<Node> selectedNodes = new List<Node>();
private float selectedNodesFarLeft;
private float selectedNodesNearLeft;
private float selectedNodesFarRight;
private float selectedNodesNearRight;
private float selectedNodesFarTop;
private float selectedNodesNearTop;
private float selectedNodesFarBottom;
private float selectedNodesNearBottom;
private float selectedNodesAvgHorizontal;
private float selectedNodesAvgVertical;
#region Initialization
public void Initialize(BaseGraphView owner, BaseNode node)
{
nodeTarget = node;
this.owner = owner;
if (!node.deletable)
capabilities &= ~Capabilities.Deletable;
// Note that the Renamable capability is useless right now as it haven't been implemented in Graphview
if (node.isRenamable)
capabilities |= Capabilities.Renamable;
owner.computeOrderUpdated += ComputeOrderUpdatedCallback;
node.onMessageAdded += AddMessageView;
node.onMessageRemoved += RemoveMessageView;
node.onPortsUpdated += a => schedule.Execute(_ => UpdatePortsForField(a)).ExecuteLater(0);
styleSheets.Add(Resources.Load<StyleSheet>(baseNodeStyle));
if (!string.IsNullOrEmpty(node.layoutStyle))
styleSheets.Add(Resources.Load<StyleSheet>(node.layoutStyle));
InitializeView();
InitializePorts();
InitializeDebug();
// If the standard Enable method is still overwritten, we call it
if (GetType().GetMethod(nameof(Enable), new Type[]{}).DeclaringType != typeof(BaseNodeView))
ExceptionToLog.Call(() => Enable());
else
ExceptionToLog.Call(() => Enable(false));
InitializeSettings();
RefreshExpandedState();
this.RefreshPorts();
RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);
RegisterCallback<DetachFromPanelEvent>(e => ExceptionToLog.Call(Disable));
OnGeometryChanged(null);
}
void InitializePorts()
{
var listener = owner.connectorListener;
foreach (var inputPort in nodeTarget.inputPorts)
{
AddPort(inputPort.fieldInfo, Direction.Input, listener, inputPort.portData);
}
foreach (var outputPort in nodeTarget.outputPorts)
{
AddPort(outputPort.fieldInfo, Direction.Output, listener, outputPort.portData);
}
}
void InitializeView()
{
controlsContainer = new VisualElement{ name = "controls" };
controlsContainer.AddToClassList("NodeControls");
mainContainer.Add(controlsContainer);
rightTitleContainer = new VisualElement{ name = "RightTitleContainer" };
titleContainer.Add(rightTitleContainer);
topPortContainer = new VisualElement { name = "TopPortContainer" };
this.Insert(0, topPortContainer);
bottomPortContainer = new VisualElement { name = "BottomPortContainer" };
this.Add(bottomPortContainer);
if (nodeTarget.showControlsOnHover)
{
bool mouseOverControls = false;
controlsContainer.style.display = DisplayStyle.None;
RegisterCallback<MouseOverEvent>(e => {
controlsContainer.style.display = DisplayStyle.Flex;
mouseOverControls = true;
});
RegisterCallback<MouseOutEvent>(e => {
var rect = GetPosition();
var graphMousePosition = owner.contentViewContainer.WorldToLocal(e.mousePosition);
if (rect.Contains(graphMousePosition) || !nodeTarget.showControlsOnHover)
return;
mouseOverControls = false;
schedule.Execute(_ => {
if (!mouseOverControls)
controlsContainer.style.display = DisplayStyle.None;
}).ExecuteLater(500);
});
}
Undo.undoRedoPerformed += UpdateFieldValues;
debugContainer = new VisualElement{ name = "debug" };
if (nodeTarget.debug)
mainContainer.Add(debugContainer);
initializing = true;
UpdateTitle();
SetPosition(nodeTarget.position);
SetNodeColor(nodeTarget.color);
AddInputContainer();
// Add renaming capability
if ((capabilities & Capabilities.Renamable) != 0)
SetupRenamableTitle();
}
void SetupRenamableTitle()
{
var titleLabel = this.Q("title-label") as Label;
titleTextField = new TextField{ isDelayed = true };
titleTextField.style.display = DisplayStyle.None;
titleLabel.parent.Insert(0, titleTextField);
titleLabel.RegisterCallback<MouseDownEvent>(e => {
if (e.clickCount == 2 && e.button == (int)MouseButton.LeftMouse)
OpenTitleEditor();
});
titleTextField.RegisterValueChangedCallback(e => CloseAndSaveTitleEditor(e.newValue));
titleTextField.RegisterCallback<MouseDownEvent>(e => {
if (e.clickCount == 2 && e.button == (int)MouseButton.LeftMouse)
CloseAndSaveTitleEditor(titleTextField.value);
});
titleTextField.RegisterCallback<FocusOutEvent>(e => CloseAndSaveTitleEditor(titleTextField.value));
void OpenTitleEditor()
{
// show title textbox
titleTextField.style.display = DisplayStyle.Flex;
titleLabel.style.display = DisplayStyle.None;
titleTextField.focusable = true;
titleTextField.SetValueWithoutNotify(title);
titleTextField.Focus();
titleTextField.SelectAll();
}
void CloseAndSaveTitleEditor(string newTitle)
{
owner.RegisterCompleteObjectUndo("Renamed node " + newTitle);
nodeTarget.SetCustomName(newTitle);
// hide title TextBox
titleTextField.style.display = DisplayStyle.None;
titleLabel.style.display = DisplayStyle.Flex;
titleTextField.focusable = false;
UpdateTitle();
}
}
void UpdateTitle()
{
title = (nodeTarget.GetCustomName() == null) ? nodeTarget.GetType().Name : nodeTarget.GetCustomName();
}
void InitializeSettings()
{
// Initialize settings button:
if (hasSettings)
{
CreateSettingButton();
settingsContainer = new NodeSettingsView();
settingsContainer.visible = false;
settings = new VisualElement();
// Add Node type specific settings
settings.Add(CreateSettingsView());
settingsContainer.Add(settings);
Add(settingsContainer);
var fields = nodeTarget.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach(var field in fields)
if(field.GetCustomAttribute(typeof(SettingAttribute)) != null)
AddSettingField(field);
}
}
void OnGeometryChanged(GeometryChangedEvent evt)
{
if (settingButton != null)
{
var settingsButtonLayout = settingButton.ChangeCoordinatesTo(settingsContainer.parent, settingButton.layout);
settingsContainer.style.top = settingsButtonLayout.yMax - 18f;
settingsContainer.style.left = settingsButtonLayout.xMin - layout.width + 20f;
}
}
// Workaround for bug in GraphView that makes the node selection border way too big
VisualElement selectionBorder, nodeBorder;
internal void EnableSyncSelectionBorderHeight()
{
if (selectionBorder == null || nodeBorder == null)
{
selectionBorder = this.Q("selection-border");
nodeBorder = this.Q("node-border");
schedule.Execute(() => {
selectionBorder.style.height = nodeBorder.localBound.height;
}).Every(17);
}
}
void CreateSettingButton()
{
settingButton = new Button(ToggleSettings){name = "settings-button"};
settingButton.Add(new Image { name = "icon", scaleMode = ScaleMode.ScaleToFit });
titleContainer.Add(settingButton);
}
void ToggleSettings()
{
settingsExpanded = !settingsExpanded;
if (settingsExpanded)
OpenSettings();
else
CloseSettings();
}
public void OpenSettings()
{
if (settingsContainer != null)
{
owner.ClearSelection();
owner.AddToSelection(this);
settingButton.AddToClassList("clicked");
settingsContainer.visible = true;
settingsExpanded = true;
}
}
public void CloseSettings()
{
if (settingsContainer != null)
{
settingButton.RemoveFromClassList("clicked");
settingsContainer.visible = false;
settingsExpanded = false;
}
}
void InitializeDebug()
{
ComputeOrderUpdatedCallback();
debugContainer.Add(computeOrderLabel);
}
#endregion
#region API
public List< PortView > GetPortViewsFromFieldName(string fieldName)
{
List< PortView > ret;
portsPerFieldName.TryGetValue(fieldName, out ret);
return ret;
}
public PortView GetFirstPortViewFromFieldName(string fieldName)
{
return GetPortViewsFromFieldName(fieldName)?.First();
}
public PortView GetPortViewFromFieldName(string fieldName, string identifier)
{
return GetPortViewsFromFieldName(fieldName)?.FirstOrDefault(pv => {
return (pv.portData.identifier == identifier) || (String.IsNullOrEmpty(pv.portData.identifier) && String.IsNullOrEmpty(identifier));
});
}
public PortView AddPort(FieldInfo fieldInfo, Direction direction, BaseEdgeConnectorListener listener, PortData portData)
{
PortView p = CreatePortView(direction, fieldInfo, portData, listener);
if (p.direction == Direction.Input)
{
inputPortViews.Add(p);
if (portData.vertical)
topPortContainer.Add(p);
else
inputContainer.Add(p);
}
else
{
outputPortViews.Add(p);
if (portData.vertical)
bottomPortContainer.Add(p);
else
outputContainer.Add(p);
}
p.Initialize(this, portData?.displayName);
List< PortView > ports;
portsPerFieldName.TryGetValue(p.fieldName, out ports);
if (ports == null)
{
ports = new List< PortView >();
portsPerFieldName[p.fieldName] = ports;
}
ports.Add(p);
return p;
}
protected virtual PortView CreatePortView(Direction direction, FieldInfo fieldInfo, PortData portData, BaseEdgeConnectorListener listener)
=> PortView.CreatePortView(direction, fieldInfo, portData, listener);
public void InsertPort(PortView portView, int index)
{
if (portView.direction == Direction.Input)
{
if (portView.portData.vertical)
topPortContainer.Insert(index, portView);
else
inputContainer.Insert(index, portView);
}
else
{
if (portView.portData.vertical)
bottomPortContainer.Insert(index, portView);
else
outputContainer.Insert(index, portView);
}
}
public void RemovePort(PortView p)
{
// Remove all connected edges:
var edgesCopy = p.GetEdges().ToList();
foreach (var e in edgesCopy)
owner.Disconnect(e, refreshPorts: false);
if (p.direction == Direction.Input)
{
if (inputPortViews.Remove(p))
p.RemoveFromHierarchy();
}
else
{
if (outputPortViews.Remove(p))
p.RemoveFromHierarchy();
}
List< PortView > ports;
portsPerFieldName.TryGetValue(p.fieldName, out ports);
ports.Remove(p);
}
private void SetValuesForSelectedNodes()
{
selectedNodes = new List<Node>();
owner.nodes.ForEach(node =>
{
if(node.selected) selectedNodes.Add(node);
});
if(selectedNodes.Count < 2) return; // No need for any of the calculations below
selectedNodesFarLeft = int.MinValue;
selectedNodesFarRight = int.MinValue;
selectedNodesFarTop = int.MinValue;
selectedNodesFarBottom = int.MinValue;
selectedNodesNearLeft = int.MaxValue;
selectedNodesNearRight = int.MaxValue;
selectedNodesNearTop = int.MaxValue;
selectedNodesNearBottom = int.MaxValue;
foreach(var selectedNode in selectedNodes)
{
var nodeStyle = selectedNode.style;
var nodeWidth = selectedNode.localBound.size.x;
var nodeHeight = selectedNode.localBound.size.y;
if(nodeStyle.left.value.value > selectedNodesFarLeft) selectedNodesFarLeft = nodeStyle.left.value.value;
if(nodeStyle.left.value.value + nodeWidth > selectedNodesFarRight) selectedNodesFarRight = nodeStyle.left.value.value + nodeWidth;
if(nodeStyle.top.value.value > selectedNodesFarTop) selectedNodesFarTop = nodeStyle.top.value.value;
if(nodeStyle.top.value.value + nodeHeight > selectedNodesFarBottom) selectedNodesFarBottom = nodeStyle.top.value.value + nodeHeight;
if(nodeStyle.left.value.value < selectedNodesNearLeft) selectedNodesNearLeft = nodeStyle.left.value.value;
if(nodeStyle.left.value.value + nodeWidth < selectedNodesNearRight) selectedNodesNearRight = nodeStyle.left.value.value + nodeWidth;
if(nodeStyle.top.value.value < selectedNodesNearTop) selectedNodesNearTop = nodeStyle.top.value.value;
if(nodeStyle.top.value.value + nodeHeight < selectedNodesNearBottom) selectedNodesNearBottom = nodeStyle.top.value.value + nodeHeight;
}
selectedNodesAvgHorizontal = (selectedNodesNearLeft + selectedNodesFarRight) / 2f;
selectedNodesAvgVertical = (selectedNodesNearTop + selectedNodesFarBottom) / 2f;
}
public static Rect GetNodeRect(Node node, float left = int.MaxValue, float top = int.MaxValue)
{
return new Rect(
new Vector2(left != int.MaxValue ? left : node.style.left.value.value, top != int.MaxValue ? top : node.style.top.value.value),
new Vector2(node.style.width.value.value, node.style.height.value.value)
);
}
public void AlignToLeft()
{
SetValuesForSelectedNodes();
if(selectedNodes.Count < 2) return;
foreach(var selectedNode in selectedNodes)
{
selectedNode.SetPosition(GetNodeRect(selectedNode, selectedNodesNearLeft));
}
}
public void AlignToCenter()
{
SetValuesForSelectedNodes();
if(selectedNodes.Count < 2) return;
foreach(var selectedNode in selectedNodes)
{
selectedNode.SetPosition(GetNodeRect(selectedNode, selectedNodesAvgHorizontal - selectedNode.localBound.size.x / 2f));
}
}
public void AlignToRight()
{
SetValuesForSelectedNodes();
if(selectedNodes.Count < 2) return;
foreach(var selectedNode in selectedNodes)
{
selectedNode.SetPosition(GetNodeRect(selectedNode, selectedNodesFarRight - selectedNode.localBound.size.x));
}
}
public void AlignToTop()
{
SetValuesForSelectedNodes();
if(selectedNodes.Count < 2) return;
foreach(var selectedNode in selectedNodes)
{
selectedNode.SetPosition(GetNodeRect(selectedNode, top: selectedNodesNearTop));
}
}
public void AlignToMiddle()
{
SetValuesForSelectedNodes();
if(selectedNodes.Count < 2) return;
foreach(var selectedNode in selectedNodes)
{
selectedNode.SetPosition(GetNodeRect(selectedNode, top: selectedNodesAvgVertical - selectedNode.localBound.size.y / 2f));
}
}
public void AlignToBottom()
{
SetValuesForSelectedNodes();
if(selectedNodes.Count < 2) return;
foreach(var selectedNode in selectedNodes)
{
selectedNode.SetPosition(GetNodeRect(selectedNode, top: selectedNodesFarBottom - selectedNode.localBound.size.y));
}
}
public void OpenNodeViewScript()
{
var script = NodeProvider.GetNodeViewScript(GetType());
if (script != null)
AssetDatabase.OpenAsset(script.GetInstanceID(), 0, 0);
}
public void OpenNodeScript()
{
var script = NodeProvider.GetNodeScript(nodeTarget.GetType());
if (script != null)
AssetDatabase.OpenAsset(script.GetInstanceID(), 0, 0);
}
public void ToggleDebug()
{
nodeTarget.debug = !nodeTarget.debug;
UpdateDebugView();
}
public void UpdateDebugView()
{
if (nodeTarget.debug)
mainContainer.Add(debugContainer);
else
mainContainer.Remove(debugContainer);
}
public void AddMessageView(string message, Texture icon, Color color)
=> AddBadge(new NodeBadgeView(message, icon, color));
public void AddMessageView(string message, NodeMessageType messageType)
{
IconBadge badge = null;
switch (messageType)
{
case NodeMessageType.Warning:
badge = new NodeBadgeView(message, EditorGUIUtility.IconContent("Collab.Warning").image, Color.yellow);
break ;
case NodeMessageType.Error:
badge = IconBadge.CreateError(message);
break ;
case NodeMessageType.Info:
badge = IconBadge.CreateComment(message);
break ;
default:
case NodeMessageType.None:
badge = new NodeBadgeView(message, null, Color.grey);
break ;
}
AddBadge(badge);
}
void AddBadge(IconBadge badge)
{
Add(badge);
badges.Add(badge);
badge.AttachTo(topContainer, SpriteAlignment.TopRight);
}
void RemoveBadge(Func<IconBadge, bool> callback)
{
badges.RemoveAll(b => {
if (callback(b))
{
b.Detach();
b.RemoveFromHierarchy();
return true;
}
return false;
});
}
public void RemoveMessageViewContains(string message) => RemoveBadge(b => b.badgeText.Contains(message));
public void RemoveMessageView(string message) => RemoveBadge(b => b.badgeText == message);
public void Highlight()
{
AddToClassList("Highlight");
}
public void UnHighlight()
{
RemoveFromClassList("Highlight");
}
#endregion
#region Callbacks & Overrides
void ComputeOrderUpdatedCallback()
{
//Update debug compute order
computeOrderLabel.text = "Compute order: " + nodeTarget.computeOrder;
}
public virtual void Enable(bool fromInspector = false) => DrawDefaultInspector(fromInspector);
public virtual void Enable() => DrawDefaultInspector(false);
public virtual void Disable() {}
Dictionary<string, List<(object value, VisualElement target)>> visibleConditions = new Dictionary<string, List<(object value, VisualElement target)>>();
Dictionary<string, VisualElement> hideElementIfConnected = new Dictionary<string, VisualElement>();
Dictionary<FieldInfo, List<VisualElement>> fieldControlsMap = new Dictionary<FieldInfo, List<VisualElement>>();
protected void AddInputContainer()
{
inputContainerElement = new VisualElement {name = "input-container"};
mainContainer.parent.Add(inputContainerElement);
inputContainerElement.SendToBack();
inputContainerElement.pickingMode = PickingMode.Ignore;
}
protected virtual void DrawDefaultInspector(bool fromInspector = false)
{
var fields = nodeTarget.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
// Filter fields from the BaseNode type since we are only interested in user-defined fields
// (better than BindingFlags.DeclaredOnly because we keep any inherited user-defined fields)
.Where(f => f.DeclaringType != typeof(BaseNode));
fields = nodeTarget.OverrideFieldOrder(fields).Reverse();
foreach (var field in fields)
{
//skip if the field is a node setting
if(field.GetCustomAttribute(typeof(SettingAttribute)) != null)
{
hasSettings = true;
continue;
}
//skip if the field is not serializable
bool serializeField = field.GetCustomAttribute(typeof(SerializeField)) != null;
if((!field.IsPublic && !serializeField) || field.IsNotSerialized)
{
AddEmptyField(field, fromInspector);
continue;
}
//skip if the field is an input/output and not marked as SerializedField
bool hasInputAttribute = field.GetCustomAttribute(typeof(InputAttribute)) != null;
bool hasInputOrOutputAttribute = hasInputAttribute || field.GetCustomAttribute(typeof(OutputAttribute)) != null;
bool showAsDrawer = !fromInspector && field.GetCustomAttribute(typeof(ShowAsDrawer)) != null;
if (!serializeField && hasInputOrOutputAttribute && !showAsDrawer)
{
AddEmptyField(field, fromInspector);
continue;
}
//skip if marked with NonSerialized or HideInInspector
if (field.GetCustomAttribute(typeof(System.NonSerializedAttribute)) != null || field.GetCustomAttribute(typeof(HideInInspector)) != null)
{
AddEmptyField(field, fromInspector);
continue;
}
// Hide the field if we want to display in in the inspector
var showInInspector = field.GetCustomAttribute<ShowInInspector>();
if (!serializeField && showInInspector != null && !showInInspector.showInNode && !fromInspector)
{
AddEmptyField(field, fromInspector);
continue;
}
var showInputDrawer = field.GetCustomAttribute(typeof(InputAttribute)) != null && field.GetCustomAttribute(typeof(SerializeField)) != null;
showInputDrawer |= field.GetCustomAttribute(typeof(InputAttribute)) != null && field.GetCustomAttribute(typeof(ShowAsDrawer)) != null;
showInputDrawer &= !fromInspector; // We can't show a drawer in the inspector
showInputDrawer &= !typeof(IList).IsAssignableFrom(field.FieldType);
string displayName = ObjectNames.NicifyVariableName(field.Name);
var inspectorNameAttribute = field.GetCustomAttribute<InspectorNameAttribute>();
if (inspectorNameAttribute != null)
displayName = inspectorNameAttribute.displayName;
var elem = AddControlField(field, displayName, showInputDrawer);
if (hasInputAttribute)
{
hideElementIfConnected[field.Name] = elem;
// Hide the field right away if there is already a connection:
if (portsPerFieldName.TryGetValue(field.Name, out var pvs))
if (pvs.Any(pv => pv.GetEdges().Count > 0))
elem.style.display = DisplayStyle.None;
}
}
}
protected virtual void SetNodeColor(Color color)
{
titleContainer.style.borderBottomColor = new StyleColor(color);
titleContainer.style.borderBottomWidth = new StyleFloat(color.a > 0 ? 5f : 0f);
}
private void AddEmptyField(FieldInfo field, bool fromInspector)
{
if (field.GetCustomAttribute(typeof(InputAttribute)) == null || fromInspector)
return;
if (field.GetCustomAttribute<VerticalAttribute>() != null)
return;
var box = new VisualElement {name = field.Name};
box.AddToClassList("port-input-element");
box.AddToClassList("empty");
inputContainerElement.Add(box);
}
void UpdateFieldVisibility(string fieldName, object newValue)
{
if (newValue == null)
return;
if (visibleConditions.TryGetValue(fieldName, out var list))
{
foreach (var elem in list)
{
if (newValue.Equals(elem.value))
elem.target.style.display = DisplayStyle.Flex;
else
elem.target.style.display = DisplayStyle.None;
}
}
}
void UpdateOtherFieldValueSpecific<T>(FieldInfo field, object newValue)
{
foreach (var inputField in fieldControlsMap[field])
{
var notify = inputField as INotifyValueChanged<T>;
if (notify != null)
notify.SetValueWithoutNotify((T)newValue);
}
}
static MethodInfo specificUpdateOtherFieldValue = typeof(BaseNodeView).GetMethod(nameof(UpdateOtherFieldValueSpecific), BindingFlags.NonPublic | BindingFlags.Instance);
void UpdateOtherFieldValue(FieldInfo info, object newValue)
{
// Warning: Keep in sync with FieldFactory CreateField
var fieldType = info.FieldType.IsSubclassOf(typeof(UnityEngine.Object)) ? typeof(UnityEngine.Object) : info.FieldType;
var genericUpdate = specificUpdateOtherFieldValue.MakeGenericMethod(fieldType);
genericUpdate.Invoke(this, new object[]{info, newValue});
}
object GetInputFieldValueSpecific<T>(FieldInfo field)
{
if (fieldControlsMap.TryGetValue(field, out var list))
{
foreach (var inputField in list)
{
if (inputField is INotifyValueChanged<T> notify)
return notify.value;
}
}
return null;
}
static MethodInfo specificGetValue = typeof(BaseNodeView).GetMethod(nameof(GetInputFieldValueSpecific), BindingFlags.NonPublic | BindingFlags.Instance);
object GetInputFieldValue(FieldInfo info)
{
// Warning: Keep in sync with FieldFactory CreateField
var fieldType = info.FieldType.IsSubclassOf(typeof(UnityEngine.Object)) ? typeof(UnityEngine.Object) : info.FieldType;
var genericUpdate = specificGetValue.MakeGenericMethod(fieldType);
return genericUpdate.Invoke(this, new object[]{info});
}
protected VisualElement AddControlField(string fieldName, string label = null, bool showInputDrawer = false, Action valueChangedCallback = null)
=> AddControlField(nodeTarget.GetType().GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance), label, showInputDrawer, valueChangedCallback);
Regex s_ReplaceNodeIndexPropertyPath = new Regex(@"(^nodes.Array.data\[)(\d+)(\])");
internal void SyncSerializedPropertyPathes()
{
int nodeIndex = owner.graph.nodes.FindIndex(n => n == nodeTarget);
// If the node is not found, then it means that it has been deleted from serialized data.
if (nodeIndex == -1)
return;
var nodeIndexString = nodeIndex.ToString();
foreach (var propertyField in this.Query<PropertyField>().ToList())
{
if(propertyField.bindingPath == null)
continue;
propertyField.Unbind();
// The property path look like this: nodes.Array.data[x].fieldName
// And we want to update the value of x with the new node index:
propertyField.bindingPath = s_ReplaceNodeIndexPropertyPath.Replace(propertyField.bindingPath, m => m.Groups[1].Value + nodeIndexString + m.Groups[3].Value);
propertyField.Bind(owner.serializedGraph);
}
}
protected SerializedProperty FindSerializedProperty(string fieldName)
{
int i = owner.graph.nodes.FindIndex(n => n == nodeTarget);
return owner.serializedGraph.FindProperty("nodes").GetArrayElementAtIndex(i).FindPropertyRelative(fieldName);
}
protected VisualElement AddControlField(FieldInfo field, string label = null, bool showInputDrawer = false, Action valueChangedCallback = null)
{
if (field == null)
return null;
var element = new PropertyField(FindSerializedProperty(field.Name), showInputDrawer ? "" : label);
element.Bind(owner.serializedGraph);
#if UNITY_2020_3 // In Unity 2020.3 the empty label on property field doesn't hide it, so we do it manually
if ((showInputDrawer || String.IsNullOrEmpty(label)) && element != null)
element.AddToClassList("DrawerField_2020_3");
#endif
if (typeof(IList).IsAssignableFrom(field.FieldType))
EnableSyncSelectionBorderHeight();
element.RegisterValueChangeCallback(e => {
UpdateFieldVisibility(field.Name, field.GetValue(nodeTarget));
valueChangedCallback?.Invoke();
NotifyNodeChanged();
});
// Disallow picking scene objects when the graph is not linked to a scene
if (element != null && !owner.graph.IsLinkedToScene())
{
var objectField = element.Q<ObjectField>();
if (objectField != null)
objectField.allowSceneObjects = false;
}
if (!fieldControlsMap.TryGetValue(field, out var inputFieldList))
inputFieldList = fieldControlsMap[field] = new List<VisualElement>();
inputFieldList.Add(element);
if(element != null)
{
if (showInputDrawer)
{
var box = new VisualElement {name = field.Name};
box.AddToClassList("port-input-element");
box.Add(element);
inputContainerElement.Add(box);
}
else
{
controlsContainer.Add(element);
}
element.name = field.Name;
}
else
{
// Make sure we create an empty placeholder if FieldFactory can not provide a drawer
if (showInputDrawer) AddEmptyField(field, false);
}
var visibleCondition = field.GetCustomAttribute(typeof(VisibleIf)) as VisibleIf;
if (visibleCondition != null)
{
// Check if target field exists:
var conditionField = nodeTarget.GetType().GetField(visibleCondition.fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (conditionField == null)
Debug.LogError($"[VisibleIf] Field {visibleCondition.fieldName} does not exists in node {nodeTarget.GetType()}");
else
{
visibleConditions.TryGetValue(visibleCondition.fieldName, out var list);
if (list == null)
list = visibleConditions[visibleCondition.fieldName] = new List<(object value, VisualElement target)>();
list.Add((visibleCondition.value, element));
UpdateFieldVisibility(visibleCondition.fieldName, conditionField.GetValue(nodeTarget));
}
}
return element;
}
void UpdateFieldValues()
{
foreach (var kp in fieldControlsMap)
UpdateOtherFieldValue(kp.Key, kp.Key.GetValue(nodeTarget));
}
protected void AddSettingField(FieldInfo field)
{
if (field == null)
return;
var label = field.GetCustomAttribute<SettingAttribute>().name;
var element = new PropertyField(FindSerializedProperty(field.Name));
element.Bind(owner.serializedGraph);
if (element != null)
{
settingsContainer.Add(element);
element.name = field.Name;
}
}
internal void OnPortConnected(PortView port)
{
if(port.direction == Direction.Input && inputContainerElement?.Q(port.fieldName) != null)
inputContainerElement.Q(port.fieldName).AddToClassList("empty");
if (hideElementIfConnected.TryGetValue(port.fieldName, out var elem))
elem.style.display = DisplayStyle.None;
onPortConnected?.Invoke(port);
}
internal void OnPortDisconnected(PortView port)
{
if (port.direction == Direction.Input && inputContainerElement?.Q(port.fieldName) != null)
{
inputContainerElement.Q(port.fieldName).RemoveFromClassList("empty");
if (nodeTarget.nodeFields.TryGetValue(port.fieldName, out var fieldInfo))
{
var valueBeforeConnection = GetInputFieldValue(fieldInfo.info);
if (valueBeforeConnection != null)
{
fieldInfo.info.SetValue(nodeTarget, valueBeforeConnection);
}
}
}
if (hideElementIfConnected.TryGetValue(port.fieldName, out var elem))
elem.style.display = DisplayStyle.Flex;
onPortDisconnected?.Invoke(port);
}
// TODO: a function to force to reload the custom behavior ports (if we want to do a button to add ports for example)
public virtual void OnRemoved() {}
public virtual void OnCreated() {}
public override void SetPosition(Rect newPos)
{
if (initializing || !nodeTarget.isLocked)
{
base.SetPosition(newPos);
if (!initializing)
owner.RegisterCompleteObjectUndo("Moved graph node");