forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdge.cs
More file actions
324 lines (269 loc) · 10.2 KB
/
Copy pathEdge.cs
File metadata and controls
324 lines (269 loc) · 10.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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleEnums;
using UnityEngine.Experimental.UIElements.StyleSheets;
namespace UnityEditor.Experimental.UIElements.GraphView
{
public class Edge : GraphElement
{
private const float k_EndPointRadius = 4.0f;
private const float k_InterceptWidth = 6.0f;
private const string k_EdgeWidthProperty = "edge-width";
private const string k_SelectedEdgeColorProperty = "selected-edge-color";
private const string k_GhostEdgeColorProperty = "ghost-edge-color";
private const string k_EdgeColorProperty = "edge-color";
private GraphView m_GraphView;
private Port m_OutputPort;
private Port m_InputPort;
private Vector2 m_CandidatePosition;
public bool isGhostEdge { get; set; }
public Port output
{
get { return m_OutputPort; }
set
{
if (m_OutputPort != null && value != m_OutputPort)
{
m_OutputPort.UpdateCapColor();
}
m_OutputPort = value;
OnPortChanged(false);
}
}
public Port input
{
get { return m_InputPort; }
set
{
if (m_InputPort != null && value != m_InputPort)
{
m_InputPort.UpdateCapColor();
}
m_InputPort = value;
OnPortChanged(true);
}
}
EdgeControl m_EdgeControl;
public EdgeControl edgeControl
{
get
{
if (m_EdgeControl == null)
{
m_EdgeControl = CreateEdgeControl();
}
return m_EdgeControl;
}
}
public Vector2 candidatePosition
{
get { return m_CandidatePosition; }
set
{
m_CandidatePosition = value;
UpdateEdgeControl();
}
}
StyleValue<int> m_EdgeWidth;
public int edgeWidth
{
get
{
return m_EdgeWidth.GetSpecifiedValueOrDefault(2);
}
}
StyleValue<Color> m_SelectedColor;
public Color selectedColor
{
get
{
return m_SelectedColor.GetSpecifiedValueOrDefault(new Color(240 / 255f, 240 / 255f, 240 / 255f));
}
}
StyleValue<Color> m_DefaultColor;
public Color defaultColor
{
get
{
return m_DefaultColor.GetSpecifiedValueOrDefault(new Color(146 / 255f, 146 / 255f, 146 / 255f));
}
}
StyleValue<Color> m_GhostColor;
public Color ghostColor
{
get
{
return m_GhostColor.GetSpecifiedValueOrDefault(new Color(85 / 255f, 85 / 255f, 85 / 255f));
}
}
protected Vector2[] PointsAndTangents
{
get { return edgeControl.controlPoints; }
}
public Edge()
{
clippingOptions = ClippingOptions.NoClipping;
ClearClassList();
AddToClassList("edge");
style.positionType = PositionType.Absolute;
Add(edgeControl);
capabilities |= Capabilities.Selectable | Capabilities.Deletable;
this.AddManipulator(new EdgeManipulator());
this.AddManipulator(new ContextualMenuManipulator(null));
}
public override bool Overlaps(Rect rectangle)
{
if (!UpdateEdgeControl())
return false;
return edgeControl.Overlaps(this.ChangeCoordinatesTo(edgeControl, rectangle));
}
public override bool ContainsPoint(Vector2 localPoint)
{
if (!UpdateEdgeControl())
return false;
return edgeControl.ContainsPoint(this.ChangeCoordinatesTo(edgeControl, localPoint));
}
public virtual void OnPortChanged(bool isInput)
{
}
public bool UpdateEdgeControl()
{
// bounding box check succeeded, do more fine grained check by measuring distance to bezier points
if (m_OutputPort == null && m_InputPort == null)
return false;
if (m_GraphView == null)
m_GraphView = GetFirstOfType<GraphView>();
if (m_GraphView == null)
return false;
Vector2 from = Vector2.zero;
Vector2 to = Vector2.zero;
GetFromToPoints(ref from, ref to);
edgeControl.from = from;
edgeControl.to = to;
edgeControl.drawFromCap = m_OutputPort == null;
edgeControl.drawToCap = m_InputPort == null;
edgeControl.UpdateLayout();
return true;
}
public override void DoRepaint()
{
// Edges do NOT call base.DoRepaint. It would create a visual artifact.
DrawEdge();
}
protected void GetFromToPoints(ref Vector2 from, ref Vector2 to)
{
if (m_OutputPort == null && m_InputPort == null)
{
return;
}
if (m_GraphView == null)
m_GraphView = GetFirstOfType<GraphView>();
if (m_OutputPort != null)
{
from = m_OutputPort.GetGlobalCenter();
from = this.WorldToLocal(from);
}
else
{
from = this.WorldToLocal(new Vector2(m_CandidatePosition.x, m_CandidatePosition.y));
}
if (m_InputPort != null)
{
to = m_InputPort.GetGlobalCenter();
to = this.WorldToLocal(to);
}
else
{
to = this.WorldToLocal(new Vector2(m_CandidatePosition.x, m_CandidatePosition.y));
}
}
protected override void OnStyleResolved(ICustomStyle styles)
{
base.OnStyleResolved(styles);
styles.ApplyCustomProperty(k_EdgeWidthProperty, ref m_EdgeWidth);
styles.ApplyCustomProperty(k_SelectedEdgeColorProperty, ref m_SelectedColor);
styles.ApplyCustomProperty(k_GhostEdgeColorProperty, ref m_GhostColor);
styles.ApplyCustomProperty(k_EdgeColorProperty, ref m_DefaultColor);
}
public override void OnDataChanged()
{
base.OnDataChanged();
EdgePresenter edgePresenter = GetPresenter<EdgePresenter>();
if (edgePresenter != null)
{
if (output == null || output.presenter != edgePresenter.output)
{
GraphView view = GetFirstAncestorOfType<GraphView>();
if (view != null)
{
output = view.Query().OfType<Port>().Where(t => t.presenter == edgePresenter.output);
}
}
if (input == null || input.presenter != edgePresenter.input)
{
GraphView view = GetFirstAncestorOfType<GraphView>();
if (view != null)
{
input = view.Query().OfType<Port>().Where(t => t.presenter == edgePresenter.input);
}
}
if (edgePresenter.output != null || edgePresenter.input != null)
edgeControl.orientation = edgePresenter.output != null ? edgePresenter.output.orientation : edgePresenter.input.orientation;
}
}
protected virtual void DrawEdge()
{
if (!UpdateEdgeControl())
return;
if (selected)
{
if (isGhostEdge)
Debug.Log("Selected Ghost Edge: this should never be");
edgeControl.inputColor = selectedColor;
edgeControl.outputColor = selectedColor;
edgeControl.edgeWidth = edgeWidth;
if (m_InputPort != null)
m_InputPort.capColor = selectedColor;
if (m_OutputPort != null)
m_OutputPort.capColor = selectedColor;
}
else
{
if (m_InputPort != null)
m_InputPort.UpdateCapColor();
if (m_OutputPort != null)
m_OutputPort.UpdateCapColor();
edgeControl.inputColor = m_InputPort == null ? m_OutputPort.portColor : m_InputPort.portColor;
edgeControl.outputColor = m_OutputPort == null ? m_InputPort.portColor : m_OutputPort.portColor;
edgeControl.edgeWidth = edgeWidth;
var edgePresenter = GetPresenter<EdgePresenter>();
if (edgePresenter == null)
{
edgeControl.toCapColor = m_InputPort == null ? m_OutputPort.portColor : m_InputPort.portColor;
edgeControl.fromCapColor = m_OutputPort == null ? m_InputPort.portColor : m_OutputPort.portColor;
}
else
{
edgeControl.toCapColor = edgePresenter.input == null ? m_OutputPort.portColor : m_InputPort.portColor;
edgeControl.fromCapColor = edgePresenter.output == null ? m_InputPort.portColor : m_OutputPort.portColor;
}
if (isGhostEdge)
{
edgeControl.inputColor = new Color(edgeControl.inputColor.r, edgeControl.inputColor.g, edgeControl.inputColor.b, 0.5f);
edgeControl.outputColor = new Color(edgeControl.outputColor.r, edgeControl.outputColor.g, edgeControl.outputColor.b, 0.5f);
}
}
}
protected virtual EdgeControl CreateEdgeControl()
{
return new EdgeControl
{
capRadius = k_EndPointRadius,
interceptWidth = k_InterceptWidth
};
}
}
}