forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPort.cs
More file actions
571 lines (476 loc) · 18.2 KB
/
Copy pathPort.cs
File metadata and controls
571 lines (476 loc) · 18.2 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
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
// 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.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleSheets;
namespace UnityEditor.Experimental.UIElements.GraphView
{
public class Port : GraphElement
{
private const string k_PortColorProperty = "port-color";
private const string k_DisabledPortColorProperty = "disabled-port-color";
protected EdgeConnector m_EdgeConnector;
protected VisualElement m_ConnectorBox;
protected Label m_ConnectorText;
protected VisualElement m_ConnectorBoxCap;
internal Color capColor
{
get
{
if (m_ConnectorBoxCap == null)
return Color.black;
return m_ConnectorBoxCap.style.backgroundColor;
}
set
{
if (m_ConnectorBoxCap != null)
m_ConnectorBoxCap.style.backgroundColor = value;
}
}
public string portName
{
get { return m_ConnectorText.text; }
set { m_ConnectorText.text = value; }
}
private bool m_portCapLit;
public bool portCapLit
{
get
{
return m_portCapLit;
}
set
{
if (value == m_portCapLit)
return;
m_portCapLit = value;
UpdateCapColor();
}
}
public Direction direction
{
get { return m_Direction; }
private set
{
if (m_Direction != value)
{
RemoveFromClassList(m_Direction.ToString().ToLower());
m_Direction = value;
AddToClassList(m_Direction.ToString().ToLower());
}
}
}
public Orientation orientation { get; private set; }
private string m_VisualClass;
public string visualClass
{
get { return m_VisualClass; }
set
{
if (value == m_VisualClass)
return;
// Clean whatever class we previously had
if (!string.IsNullOrEmpty(m_VisualClass))
RemoveFromClassList(m_VisualClass);
else
ManageTypeClassList(m_PortType, RemoveFromClassList);
m_VisualClass = value;
// Add the given class if not null or empty. Use the auto class otherwise.
if (!string.IsNullOrEmpty(m_VisualClass))
AddToClassList(m_VisualClass);
else
ManageTypeClassList(m_PortType, AddToClassList);
}
}
private Type m_PortType;
public Type portType
{
get { return m_PortType; }
set
{
if (m_PortType == value)
return;
ManageTypeClassList(m_PortType, RemoveFromClassList);
m_PortType = value;
Type genericClass = typeof(PortSource<>);
Type constructedClass = genericClass.MakeGenericType(m_PortType);
source = Activator.CreateInstance(constructedClass);
if (string.IsNullOrEmpty(m_ConnectorText.text))
m_ConnectorText.text = m_PortType.Name;
ManageTypeClassList(m_PortType, AddToClassList);
}
}
private void ManageTypeClassList(Type type, Action<string> classListAction)
{
// If there's an visual class explicitly set, don't set an automatic one.
if (type == null || !string.IsNullOrEmpty(m_VisualClass))
return;
if (type.IsSubclassOf(typeof(Component)))
classListAction("typeComponent");
else if (type.IsSubclassOf(typeof(GameObject)))
classListAction("typeGameObject");
else
classListAction("type" + type.Name);
}
public EdgeConnector edgeConnector
{
get { return m_EdgeConnector; }
}
public object source { get; set; }
private bool m_Highlight = true;
public bool highlight
{
get
{
// TODO: Remove when removing presenters.
PortPresenter portPresenter = GetPresenter<PortPresenter>();
if (portPresenter == null)
return m_Highlight;
return portPresenter.highlight;
}
set
{
// TODO: Remove when removing presenters.
PortPresenter portPresenter = GetPresenter<PortPresenter>();
if (portPresenter != null)
portPresenter.highlight = value;
if (m_Highlight == value)
return;
m_Highlight = value;
UpdateConnectorColor();
}
}
private HashSet<Edge> m_Connections;
private Direction m_Direction;
public virtual IEnumerable<Edge> connections
{
get
{
return m_Connections;
}
}
public virtual bool connected
{
get
{
// TODO: Remove when removing presenters.
PortPresenter portPresenter = GetPresenter<PortPresenter>();
if (portPresenter != null)
return portPresenter.connected;
return m_Connections.Count > 0;
}
}
public virtual bool collapsed
{
get
{
// TODO: Remove when removing presenters.
PortPresenter portPresenter = GetPresenter<PortPresenter>();
if (portPresenter != null)
return portPresenter.collapsed;
return false;
}
}
StyleValue<Color> m_PortColor;
public Color portColor
{
get
{
return m_PortColor.GetSpecifiedValueOrDefault(new Color(240 / 255f, 240 / 255f, 240 / 255f));
}
}
StyleValue<Color> m_DisabledPortColor;
public Color disabledPortColor
{
get
{
return m_PortColor.GetSpecifiedValueOrDefault(new Color(70 / 255f, 70 / 255f, 70 / 255f));
}
}
internal Action<Port> OnConnect;
internal Action<Port> OnDisconnect;
public virtual void Connect(Edge edge)
{
if (edge == null)
{
throw new ArgumentException("The value passed to Port.Connect is null");
}
// TODO: Remove when removing presenters.
var presenter = GetPresenter<PortPresenter>();
if (presenter != null)
{
var edgePresenter = edge.GetPresenter<EdgePresenter>();
presenter.Connect(edgePresenter);
}
else
{
if (!m_Connections.Contains(edge))
{
m_Connections.Add(edge);
}
}
OnConnect?.Invoke(this);
}
public virtual void Disconnect(Edge edge)
{
if (edge == null)
{
throw new ArgumentException("The value passed to PortPresenter.Disconnect is null");
}
// TODO: Remove when removing presenters.
var presenter = GetPresenter<PortPresenter>();
if (presenter != null)
{
var edgePresenter = edge.GetPresenter<EdgePresenter>();
presenter.Disconnect(edgePresenter);
}
else
{
m_Connections.Remove(edge);
}
OnDisconnect?.Invoke(this);
}
public virtual void DisconnectAll()
{
// TODO: Remove when removing presenters.
var presenter = GetPresenter<PortPresenter>();
if (presenter != null)
{
foreach (var edge in m_Connections)
{
var edgePresenter = edge.GetPresenter<EdgePresenter>();
presenter.Disconnect(edgePresenter);
}
}
m_Connections.Clear();
OnDisconnect?.Invoke(this);
}
private class DefaultEdgeConnectorListener : IEdgeConnectorListener
{
private GraphViewChange m_GraphViewChange;
private List<Edge> m_EdgesToCreate;
public DefaultEdgeConnectorListener()
{
m_EdgesToCreate = new List<Edge>();
m_GraphViewChange.edgesToCreate = m_EdgesToCreate;
}
public void OnDropOutsidePort(Edge edge, Vector2 position) {}
public void OnDrop(GraphView graphView, Edge edge)
{
m_EdgesToCreate.Clear();
m_EdgesToCreate.Add(edge);
var edgesToCreate = m_EdgesToCreate;
if (graphView.graphViewChanged != null)
{
edgesToCreate = graphView.graphViewChanged(m_GraphViewChange).edgesToCreate;
}
foreach (Edge e in edgesToCreate)
{
graphView.AddElement(e);
edge.input.Connect(e);
edge.output.Connect(e);
}
}
}
// TODO: Remove when removing presenters.
protected class DefaultEdgePresenterConnectorListener<TEdgePresenter> : IEdgeConnectorListener where TEdgePresenter : EdgePresenter
{
public void OnDropOutsidePort(Edge edge, Vector2 position) {}
public void OnDrop(GraphView graphView, Edge edge)
{
if (graphView == null || edge == null)
return;
if (graphView.presenter == null)
return;
// Check if the edge already has a presenter then do not create it
EdgePresenter edgePresenter = edge.GetPresenter<EdgePresenter>();
if (edgePresenter == null)
{
edgePresenter = ScriptableObject.CreateInstance<TEdgePresenter>();
}
edgePresenter.output = edge.output.GetPresenter<PortPresenter>();
edgePresenter.input = edge.input.GetPresenter<PortPresenter>();
edgePresenter.output.Connect(edgePresenter);
edgePresenter.input.Connect(edgePresenter);
graphView.presenter.AddElement(edgePresenter);
}
}
// TODO This is a workaround to avoid having a generic type for the port as generic types mess with USS.
public static Port Create<TEdge>(Orientation orientation, Direction direction, Type type) where TEdge : Edge, new()
{
var connectorListener = new DefaultEdgeConnectorListener();
var port = new Port(orientation, direction, type)
{
m_EdgeConnector = new EdgeConnector<TEdge>(connectorListener),
};
port.AddManipulator(port.m_EdgeConnector);
return port;
}
// TODO: Remove when removing presenters.
public static Port Create<TEdgePresenter, TEdge>(PortPresenter presenter)
where TEdgePresenter : EdgePresenter
where TEdge : Edge, new()
{
var connectorListener = new DefaultEdgePresenterConnectorListener<TEdgePresenter>();
var port = new Port(Orientation.Horizontal, Direction.Input, typeof(object))
{
m_EdgeConnector = new EdgeConnector<TEdge>(connectorListener),
presenter = presenter
};
port.AddManipulator(port.m_EdgeConnector);
return port;
}
// TODO: Remove when removing presenters.
// TODO: Remove!
protected virtual VisualElement CreateConnector()
{
return new VisualElement();
}
protected Port(Orientation portOrientation, Direction portDirection, Type type)
{
// currently we don't want to be styled as .graphElement since we're contained in a Node
ClearClassList();
var tpl = EditorGUIUtility.Load("UXML/GraphView/Port.uxml") as VisualTreeAsset;
tpl.CloneTree(this, null);
m_ConnectorBox = this.Q(name: "connector");
m_ConnectorText = this.Q<Label>(name: "type");
m_ConnectorText.clippingOptions = ClippingOptions.NoClipping;
m_ConnectorBoxCap = this.Q(name: "cap");
m_Connections = new HashSet<Edge>();
orientation = portOrientation;
direction = portDirection;
portType = type;
AddToClassList(portDirection.ToString().ToLower());
}
private void UpdateConnector()
{
if (m_EdgeConnector == null)
return;
var portPresenter = GetPresenter<PortPresenter>();
if (m_EdgeConnector.target == null || !m_EdgeConnector.target.HasMouseCapture()) // if the edge connector has capture, it means that an edge is being created. so don't remove the manipulator at the moment.
{
if (!portPresenter.connected || portPresenter.direction != Direction.Input)
{
this.AddManipulator(m_EdgeConnector);
}
else
{
this.RemoveManipulator(m_EdgeConnector);
}
}
}
public Node node
{
get { return GetFirstAncestorOfType<Node>(); }
}
public bool IsConnectable()
{
// TODO: Remove when removing presenters.
PortPresenter portPresenter = presenter as PortPresenter;
if (portPresenter != null)
return portPresenter.IsConnectable();
return true;
}
// TODO: Remove when removing presenters.
public override void OnDataChanged()
{
UpdateConnector();
var portPresenter = GetPresenter<PortPresenter>();
Type presenterPortType = portPresenter.portType;
Type genericClass = typeof(PortSource<>);
try
{
Type constructedClass = genericClass.MakeGenericType(presenterPortType);
portPresenter.source = Activator.CreateInstance(constructedClass);
}
catch (Exception e)
{
Debug.Log("Couldn't build PortSouce<" + (presenterPortType == null ? "null" : presenterPortType.Name) + "> " + e.Message);
}
string portName = string.IsNullOrEmpty(portPresenter.name) ? presenterPortType.Name : portPresenter.name;
m_ConnectorText.text = portName;
portPresenter.capabilities &= ~Capabilities.Selectable;
// Cache some stuff for easier access from the outside.
direction = portPresenter.direction;
orientation = portPresenter.orientation;
portType = portPresenter.portType;
source = portPresenter.source;
}
public override Vector3 GetGlobalCenter()
{
return m_ConnectorBox.LocalToWorld(m_ConnectorBox.rect.center);
}
public override bool ContainsPoint(Vector2 localPoint)
{
Rect lRect = m_ConnectorBox.layout;
Rect boxRect;
if (direction == Direction.Input)
{
boxRect = new Rect(-lRect.xMin, -lRect.yMin,
lRect.width + lRect.xMin, rect.height);
boxRect.width += m_ConnectorText.layout.xMin - lRect.xMax;
}
else
{
boxRect = new Rect(0, -lRect.yMin,
rect.width - lRect.xMin, rect.height);
float leftSpace = lRect.xMin - m_ConnectorText.layout.xMax;
boxRect.xMin -= leftSpace;
boxRect.width += leftSpace;
}
return boxRect.Contains(this.ChangeCoordinatesTo(m_ConnectorBox, localPoint));
}
internal void UpdateCapColor()
{
if (portCapLit || connected)
{
m_ConnectorBoxCap.style.backgroundColor = portColor;
}
else
{
m_ConnectorBoxCap.style.backgroundColor = StyleValue<Color>.nil;
}
}
private void UpdateConnectorColor()
{
if (m_ConnectorBox == null)
return;
m_ConnectorBox.style.borderColor = highlight ? m_PortColor.value : m_DisabledPortColor.value;
}
protected internal override void ExecuteDefaultAction(EventBase evt)
{
base.ExecuteDefaultAction(evt);
if (m_ConnectorBox == null || m_ConnectorBoxCap == null)
{
return;
}
if (evt.GetEventTypeId() == MouseEnterEvent.TypeId())
{
m_ConnectorBoxCap.style.backgroundColor = portColor;
}
else if (evt.GetEventTypeId() == MouseLeaveEvent.TypeId())
{
UpdateCapColor();
}
else if (evt.GetEventTypeId() == MouseUpEvent.TypeId())
{
// When an edge connect ends, we need to clear out the hover states
var mouseUp = (MouseUpEvent)evt;
if (!layout.Contains(mouseUp.localMousePosition))
{
UpdateCapColor();
}
}
}
protected override void OnStyleResolved(ICustomStyle styles)
{
base.OnStyleResolved(styles);
styles.ApplyCustomProperty(k_PortColorProperty, ref m_PortColor);
styles.ApplyCustomProperty(k_DisabledPortColorProperty, ref m_DisabledPortColor);
UpdateConnectorColor();
}
}
}