forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.cs
More file actions
462 lines (385 loc) · 14.3 KB
/
Node.cs
File metadata and controls
462 lines (385 loc) · 14.3 KB
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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace UnityEditor.Experimental.GraphView
{
public class Node : GraphElement
{
public VisualElement mainContainer { get; private set; }
public VisualElement titleContainer { get; private set; }
public VisualElement inputContainer { get; private set; }
public VisualElement outputContainer { get; private set; }
public VisualElement titleButtonContainer { get; private set; }
private VisualElement m_InputContainerParent;
private VisualElement m_OutputContainerParent;
//This directly contains input and output containers
public VisualElement topContainer { get; private set; }
public VisualElement extensionContainer { get; private set; }
private VisualElement m_CollapsibleArea;
private GraphView m_GraphView;
// TODO Maybe make protected and move to GraphElement!
private GraphView graphView
{
get
{
if (m_GraphView == null)
{
m_GraphView = GetFirstAncestorOfType<GraphView>();
}
return m_GraphView;
}
}
private bool m_Expanded;
public virtual bool expanded
{
get { return m_Expanded; }
set
{
if (m_Expanded == value)
return;
m_Expanded = value;
RefreshExpandedState();
}
}
public void RefreshExpandedState()
{
UpdateExpandedButtonState();
bool hasPorts = RefreshPorts();
VisualElement contents = mainContainer.Q("contents");
if (contents == null)
{
return;
}
VisualElement divider = contents.Q("divider");
if (divider != null)
{
SetElementVisible(divider, hasPorts);
}
UpdateCollapsibleAreaVisibility();
}
void UpdateCollapsibleAreaVisibility()
{
if (m_CollapsibleArea == null)
{
return;
}
bool displayBottom = expanded && extensionContainer.childCount > 0;
if (displayBottom)
{
if (m_CollapsibleArea.parent == null)
{
VisualElement contents = mainContainer.Q("contents");
if (contents == null)
{
return;
}
contents.Add(m_CollapsibleArea);
}
m_CollapsibleArea.BringToFront();
}
else
{
if (m_CollapsibleArea.parent != null)
{
m_CollapsibleArea.RemoveFromHierarchy();
}
}
}
private readonly Label m_TitleLabel;
public override string title
{
get { return m_TitleLabel != null ? m_TitleLabel.text : string.Empty; }
set { if (m_TitleLabel != null) m_TitleLabel.text = value; }
}
protected readonly VisualElement m_CollapseButton;
protected readonly VisualElement m_ButtonContainer;
private const string k_ExpandedStyleClass = "expanded";
private const string k_CollapsedStyleClass = "collapsed";
private void UpdateExpandedButtonState()
{
RemoveFromClassList(m_Expanded ? k_CollapsedStyleClass : k_ExpandedStyleClass);
AddToClassList(m_Expanded ? k_ExpandedStyleClass : k_CollapsedStyleClass);
}
public override Rect GetPosition()
{
if (resolvedStyle.position == Position.Absolute)
return new Rect(resolvedStyle.left, resolvedStyle.top, layout.width, layout.height);
return layout;
}
public override void SetPosition(Rect newPos)
{
style.position = Position.Absolute;
style.left = newPos.x;
style.top = newPos.y;
}
protected virtual void OnPortRemoved(Port port)
{}
public virtual Port InstantiatePort(Orientation orientation, Direction direction, Port.Capacity capacity, Type type)
{
return Port.Create<Edge>(orientation, direction, capacity, type);
}
private void SetElementVisible(VisualElement element, bool isVisible)
{
const string k_HiddenClassList = "hidden";
element.visible = isVisible;
if (isVisible)
{
element.RemoveFromClassList(k_HiddenClassList);
}
else
{
element.AddToClassList(k_HiddenClassList);
}
}
private bool AllElementsHidden(VisualElement element)
{
for (int i = 0; i < element.childCount; ++i)
{
if (element[i].visible)
return false;
}
return true;
}
private int ShowPorts(bool show, IList<Port> currentPorts)
{
int count = 0;
foreach (var port in currentPorts)
{
if ((show || port.connected) && !port.collapsed)
{
SetElementVisible(port, true);
count++;
}
else
{
SetElementVisible(port, false);
}
}
return count;
}
public bool RefreshPorts()
{
bool expandedState = expanded;
// Refresh the lists after all additions and everything took place
var updatedInputs = inputContainer.Query<Port>().ToList();
var updatedOutputs = outputContainer.Query<Port>().ToList();
foreach (Port input in updatedInputs)
{
// Make sure we don't register these more than once.
input.OnConnect -= OnPortConnectAction;
input.OnDisconnect -= OnPortConnectAction;
input.OnConnect += OnPortConnectAction;
input.OnDisconnect += OnPortConnectAction;
}
foreach (Port output in updatedOutputs)
{
// Make sure we don't register these more than once.
output.OnConnect -= OnPortConnectAction;
output.OnDisconnect -= OnPortConnectAction;
output.OnConnect += OnPortConnectAction;
output.OnDisconnect += OnPortConnectAction;
}
int inputCount = ShowPorts(expandedState, updatedInputs);
int outputCount = ShowPorts(expandedState, updatedOutputs);
VisualElement divider = topContainer.Q("divider");
bool outputVisible = outputCount > 0 || !AllElementsHidden(outputContainer);
bool inputVisible = inputCount > 0 || !AllElementsHidden(inputContainer);
// Show output container only if we have one or more child
if (outputVisible)
{
if (outputContainer.hierarchy.parent != m_OutputContainerParent)
{
m_OutputContainerParent.hierarchy.Add(outputContainer);
outputContainer.BringToFront();
}
}
else
{
if (outputContainer.hierarchy.parent == m_OutputContainerParent)
{
outputContainer.RemoveFromHierarchy();
}
}
if (inputVisible)
{
if (inputContainer.hierarchy.parent != m_InputContainerParent)
{
m_InputContainerParent.hierarchy.Add(inputContainer);
inputContainer.SendToBack();
}
}
else
{
if (inputContainer.hierarchy.parent == m_InputContainerParent)
{
inputContainer.RemoveFromHierarchy();
}
}
if (divider != null)
{
SetElementVisible(divider, inputVisible && outputVisible);
}
return inputVisible || outputVisible;
}
private void OnPortConnectAction(Port port)
{
bool canCollapse = false;
var updatedInputs = inputContainer.Query<Port>().ToList();
var updatedOutputs = outputContainer.Query<Port>().ToList();
foreach (Port input in updatedInputs)
{
if (!input.connected)
{
canCollapse = true;
break;
}
}
if (!canCollapse)
{
foreach (Port output in updatedOutputs)
{
if (!output.connected)
{
canCollapse = true;
break;
}
}
}
if (m_CollapseButton != null)
{
if (canCollapse)
m_CollapseButton.pseudoStates &= ~PseudoStates.Disabled;
else
m_CollapseButton.pseudoStates |= PseudoStates.Disabled;
}
}
protected virtual void ToggleCollapse()
{
expanded = !expanded;
}
protected void UseDefaultStyling()
{
AddStyleSheetPath("StyleSheets/GraphView/Node.uss");
}
public Node() : this("UXML/GraphView/Node.uxml")
{
UseDefaultStyling();
}
public Node(string uiFile)
{
var tpl = EditorGUIUtility.Load(uiFile) as VisualTreeAsset;
tpl.CloneTree(this);
VisualElement main = this;
VisualElement borderContainer = main.Q(name: "node-border");
if (borderContainer != null)
{
borderContainer.style.overflow = Overflow.Hidden;
mainContainer = borderContainer;
var selection = main.Q(name: "selection-border");
if (selection != null)
{
selection.style.overflow = Overflow.Visible; //fixes issues with selection border being clipped when zooming out
}
}
else
{
mainContainer = main;
}
titleContainer = main.Q(name: "title");
inputContainer = main.Q(name: "input");
if (inputContainer != null)
{
m_InputContainerParent = inputContainer.hierarchy.parent;
}
m_CollapsibleArea = main.Q(name: "collapsible-area");
extensionContainer = main.Q("extension");
VisualElement output = main.Q(name: "output");
outputContainer = output;
if (outputContainer != null)
{
m_OutputContainerParent = outputContainer.hierarchy.parent;
topContainer = output.parent;
}
m_TitleLabel = main.Q<Label>(name: "title-label");
titleButtonContainer = main.Q(name: "title-button-container");
m_CollapseButton = main.Q(name: "collapse-button");
m_CollapseButton.AddManipulator(new Clickable(ToggleCollapse));
elementTypeColor = new Color(0.9f, 0.9f, 0.9f, 0.5f);
if (main != this)
{
Add(main);
}
AddToClassList("node");
capabilities |= Capabilities.Selectable | Capabilities.Movable | Capabilities.Deletable | Capabilities.Ascendable;
usageHints = UsageHints.DynamicTransform;
m_Expanded = true;
UpdateExpandedButtonState();
UpdateCollapsibleAreaVisibility();
this.AddManipulator(new ContextualMenuManipulator(BuildContextualMenu));
style.position = Position.Absolute;
}
void AddConnectionsToDeleteSet(VisualElement container, ref HashSet<GraphElement> toDelete)
{
List<GraphElement> toDeleteList = new List<GraphElement>();
container.Query<Port>().ForEach(elem =>
{
if (elem.connected)
{
foreach (Edge c in elem.connections)
{
if ((c.capabilities & Capabilities.Deletable) == 0)
continue;
toDeleteList.Add(c);
}
}
});
toDelete.UnionWith(toDeleteList);
}
void DisconnectAll(DropdownMenuAction a)
{
HashSet<GraphElement> toDelete = new HashSet<GraphElement>();
AddConnectionsToDeleteSet(inputContainer, ref toDelete);
AddConnectionsToDeleteSet(outputContainer, ref toDelete);
toDelete.Remove(null);
if (graphView != null)
{
graphView.DeleteElements(toDelete);
}
else
{
Debug.Log("Disconnecting nodes that are not in a GraphView will not work.");
}
}
DropdownMenuAction.Status DisconnectAllStatus(DropdownMenuAction a)
{
VisualElement[] containers =
{
inputContainer, outputContainer
};
foreach (var container in containers)
{
var currentInputs = container.Query<Port>().ToList();
foreach (var elem in currentInputs)
{
if (elem.connected)
{
return DropdownMenuAction.Status.Normal;
}
}
}
return DropdownMenuAction.Status.Disabled;
}
public virtual void BuildContextualMenu(ContextualMenuPopulateEvent evt)
{
if (evt.target is Node)
{
evt.menu.AppendAction("Disconnect all", DisconnectAll, DisconnectAllStatus);
evt.menu.AppendSeparator();
}
}
}
}