-
Notifications
You must be signed in to change notification settings - Fork 364
/
EdgeView.cs
64 lines (51 loc) · 1.64 KB
/
EdgeView.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
using UnityEditor.Experimental.GraphView;
using UnityEngine.UIElements;
using UnityEngine;
namespace GraphProcessor
{
public class EdgeView : Edge
{
public bool isConnected = false;
public SerializableEdge serializedEdge { get { return userData as SerializableEdge; } }
readonly string edgeStyle = "GraphProcessorStyles/EdgeView";
protected BaseGraphView owner => ((input ?? output) as PortView).owner.owner;
public EdgeView() : base()
{
styleSheets.Add(Resources.Load<StyleSheet>(edgeStyle));
RegisterCallback<MouseDownEvent>(OnMouseDown);
}
public override void OnPortChanged(bool isInput)
{
base.OnPortChanged(isInput);
UpdateEdgeSize();
}
public void UpdateEdgeSize()
{
if (input == null && output == null)
return;
PortData inputPortData = (input as PortView)?.portData;
PortData outputPortData = (output as PortView)?.portData;
for (int i = 1; i < 20; i++)
RemoveFromClassList($"edge_{i}");
int maxPortSize = Mathf.Max(inputPortData?.sizeInPixel ?? 0, outputPortData?.sizeInPixel ?? 0);
if (maxPortSize > 0)
AddToClassList($"edge_{Mathf.Max(1, maxPortSize - 6)}");
}
protected override void OnCustomStyleResolved(ICustomStyle styles)
{
base.OnCustomStyleResolved(styles);
UpdateEdgeControl();
}
void OnMouseDown(MouseDownEvent e)
{
if (e.clickCount == 2)
{
// Empirical offset:
var position = e.mousePosition;
position += new Vector2(-10f, -28);
Vector2 mousePos = owner.ChangeCoordinatesTo(owner.contentViewContainer, position);
owner.AddRelayNode(input as PortView, output as PortView, mousePos);
}
}
}
}