forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdgeControl.cs
More file actions
344 lines (300 loc) · 10.3 KB
/
EdgeControl.cs
File metadata and controls
344 lines (300 loc) · 10.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
// 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 UnityEngine;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.Experimental.UIElements.GraphView
{
internal
enum LineType
{
Bezier,
PolyLine,
StraightLine,
}
internal
class EdgeControl : VisualElement
{
private Orientation m_Orientation;
public Orientation orientation
{
get { return m_Orientation; }
set
{
if (m_Orientation == value)
return;
m_Orientation = value;
Dirty(ChangeType.Repaint);
}
}
private LineType m_LineType;
public LineType lineType
{
get { return m_LineType; }
set
{
if (m_LineType == value)
return;
m_LineType = value;
PointsChanged();
}
}
private Color m_EdgeColor;
public Color edgeColor
{
get { return m_EdgeColor; }
set
{
if (m_EdgeColor == value)
return;
m_EdgeColor = value;
Dirty(ChangeType.Repaint);
}
}
private Color m_StartCapColor;
public Color startCapColor
{
get { return m_StartCapColor; }
set
{
if (m_StartCapColor == value)
return;
m_StartCapColor = value;
Dirty(ChangeType.Repaint);
}
}
private Color m_EndCapColor;
public Color endCapColor
{
get { return m_EndCapColor; }
set
{
if (m_EndCapColor == value)
return;
m_EndCapColor = value;
Dirty(ChangeType.Repaint);
}
}
private float m_CapRadius = 5;
public float capRadius
{
get { return m_CapRadius; }
set
{
if (m_CapRadius == value)
return;
m_CapRadius = value;
Dirty(ChangeType.Repaint);
}
}
private int m_EdgeWidth = 2;
public int edgeWidth
{
get { return m_EdgeWidth; }
set
{
if (m_EdgeWidth == value)
return;
m_EdgeWidth = value;
Dirty(ChangeType.Repaint);
}
}
private float m_InterceptWidth = 5;
public float interceptWidth
{
get { return m_InterceptWidth; }
set { m_InterceptWidth = value; }
}
private Vector2 m_From;
public Vector2 from
{
get { return m_From; }
set
{
if (m_From != value)
{
m_From = value;
PointsChanged();
}
}
}
private Vector2 m_To;
public Vector2 to
{
get { return m_To; }
set
{
if (m_To != value)
{
m_To = value;
PointsChanged();
}
}
}
private bool m_TangentsDirty;
private Vector3[] m_ControlPoints;
public Vector3[] controlPoints
{
get
{
if (m_TangentsDirty || m_ControlPoints == null)
{
CacheLineData();
m_TangentsDirty = false;
}
return m_ControlPoints;
}
}
private Vector3[] m_RenderPoints;
public Vector3[] renderPoints
{
get
{
if (m_TangentsDirty || m_RenderPoints == null)
{
CacheLineData();
m_TangentsDirty = false;
}
return m_RenderPoints;
}
}
protected virtual void DrawEdge()
{
Vector3[] points = controlPoints;
switch (lineType)
{
case LineType.Bezier:
Handles.DrawBezier(points[0], points[3], points[1], points[2], edgeColor, null, edgeWidth);
break;
case LineType.PolyLine:
case LineType.StraightLine:
Handles.color = edgeColor;
Handles.DrawAAPolyLine(edgeWidth, renderPoints);
break;
default:
throw new ArgumentOutOfRangeException("Unsupported LineType: " + lineType);
}
}
protected virtual void DrawEndpoint(Vector2 pos)
{
Handles.DrawSolidDisc(pos, new Vector3(0.0f, 0.0f, -1.0f), capRadius);
}
public override void DoRepaint()
{
// Edges do NOT call base.DoRepaint. It would create a visual artifact.
Color oldColor = Handles.color;
DrawEdge();
Handles.color = startCapColor;
DrawEndpoint(from);
Handles.color = endCapColor;
DrawEndpoint(to);
Handles.color = oldColor;
}
public override bool ContainsPoint(Vector2 localPoint)
{
// bounding box check succeeded, do more fine grained check by measuring distance to bezier points
// exclude endpoints
if (Vector2.Distance(from, localPoint) <= 2 * capRadius ||
Vector2.Distance(to, localPoint) <= 2 * capRadius)
{
return false;
}
Vector3[] allPoints = renderPoints;
float minDistance = Mathf.Infinity;
for (var i = 0; i < allPoints.Length; i++)
{
Vector3 currentPoint = allPoints[i];
float distance = Vector3.Distance(currentPoint, localPoint);
minDistance = Mathf.Min(minDistance, distance);
if (minDistance < interceptWidth)
return true;
}
return false;
}
public override bool Overlaps(Rect rect)
{
Vector3[] allPoints = renderPoints;
for (int a = 0; a < allPoints.Length - 1; a++)
{
var segmentA = new Vector2(allPoints[a].x, allPoints[a].y);
var segmentB = new Vector2(allPoints[a + 1].x, allPoints[a + 1].y);
if (RectUtils.IntersectsSegment(rect, segmentA, segmentB))
return true;
}
return false;
}
private void PointsChanged()
{
m_TangentsDirty = true;
layout = new Rect(Vector2.Min(m_To, m_From), new Vector2(Mathf.Abs(m_From.x - m_To.x), Mathf.Abs(m_From.y - m_To.y)));
Dirty(ChangeType.Repaint);
}
private void CacheLineData()
{
// Don't store the values in the actual `to` and `from` member as this will trigger infinite repaints if to and from are switched.
Vector2 usedTo = to;
Vector2 usedFrom = from;
if (orientation == Orientation.Horizontal && from.x < to.x)
{
usedTo = from;
usedFrom = to;
}
if (lineType == LineType.StraightLine)
{
if (m_ControlPoints == null || m_ControlPoints.Length != 2)
m_ControlPoints = new Vector3[2];
m_ControlPoints[0] = usedTo;
m_ControlPoints[1] = usedFrom;
m_RenderPoints = m_ControlPoints;
return;
}
if (m_ControlPoints == null || m_ControlPoints.Length != 4)
m_ControlPoints = new Vector3[4];
m_ControlPoints[0] = usedTo;
m_ControlPoints[3] = usedFrom;
switch (lineType)
{
case LineType.Bezier:
const float minTangent = 30;
float weight = .5f;
float weight2 = 1 - weight;
float y = 0;
float cleverness = Mathf.Clamp01(((usedTo - usedFrom).magnitude - 10) / 50);
if (orientation == Orientation.Horizontal)
{
m_ControlPoints[1] = usedTo + new Vector2((usedFrom.x - usedTo.x) * weight + minTangent, y) * cleverness;
m_ControlPoints[2] = usedFrom + new Vector2((usedFrom.x - usedTo.x) * -weight2 - minTangent, -y) * cleverness;
}
else
{
float tangentSize = usedTo.y - usedFrom.y + 100.0f;
tangentSize = Mathf.Min((usedTo - usedFrom).magnitude, tangentSize);
if (tangentSize < 0.0f)
tangentSize = -tangentSize;
m_ControlPoints[1] = usedTo + new Vector2(0, tangentSize * -0.5f);
m_ControlPoints[2] = usedFrom + new Vector2(0, tangentSize * 0.5f);
}
m_RenderPoints = Handles.MakeBezierPoints(m_ControlPoints[0], m_ControlPoints[3], m_ControlPoints[1], m_ControlPoints[2], 20);
break;
case LineType.PolyLine:
if (orientation == Orientation.Horizontal)
{
m_ControlPoints[2] = new Vector2((usedTo.x + usedFrom.x) / 2, usedFrom.y);
m_ControlPoints[1] = new Vector2((usedTo.x + usedFrom.x) / 2, usedTo.y);
}
else
{
m_ControlPoints[2] = new Vector2(usedFrom.x, (usedTo.y + usedFrom.y) / 2);
m_ControlPoints[1] = new Vector2(usedTo.x, (usedTo.y + usedFrom.y) / 2);
}
m_RenderPoints = m_ControlPoints;
break;
case LineType.StraightLine:
break;
default:
throw new ArgumentOutOfRangeException("Unsupported LineType: " + lineType);
}
}
}
}