forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualElementAttacher.cs
More file actions
260 lines (226 loc) · 7.74 KB
/
Copy pathVisualElementAttacher.cs
File metadata and controls
260 lines (226 loc) · 7.74 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.Experimental.UIElements.GraphView
{
public class Attacher
{
public VisualElement target { get; private set; }
public VisualElement element { get; private set; }
public SpriteAlignment alignment
{
get { return m_Alignment; }
set
{
if (m_Alignment != value)
{
m_Alignment = value;
if (isAttached)
{
AlignOnTarget();
}
}
}
}
public Vector2 offset
{
get { return m_Offset; }
set
{
if (m_Offset != value)
{
m_Offset = value;
if (isAttached)
{
AlignOnTarget();
}
}
}
}
public float distance
{
get { return m_Distance; }
set
{
if (m_Distance != value)
{
m_Distance = value;
if (isAttached)
{
AlignOnTarget();
}
}
}
}
private bool isAttached
{
get { return target != null && element != null && m_WatchedObjects != null && m_WatchedObjects.Count > 0; }
}
private Rect m_LastTargetWorldBounds = Rect.zero;
private Rect m_ElementSize = Rect.zero;
private List<VisualElement> m_WatchedObjects;
private Vector2 m_Offset;
private SpriteAlignment m_Alignment;
private float m_Distance;
public Attacher(VisualElement anchored, VisualElement target, SpriteAlignment alignment)
{
this.distance = 6.0f;
this.target = target;
this.element = anchored;
this.alignment = alignment;
Reattach();
}
public void Detach()
{
UnregisterCallbacks();
m_ElementSize = m_LastTargetWorldBounds = Rect.zero;
}
public void Reattach()
{
RegisterCallbacks();
UpdateTargetBounds();
AlignOnTarget();
}
private void RegisterCallbacks()
{
if (m_WatchedObjects != null && m_WatchedObjects.Count > 0)
{
UnregisterCallbacks();
}
VisualElement commonAncestor = target.FindCommonAncestor(element);
if (commonAncestor == target)
{
Debug.Log("Attacher: Target is already parent of anchored element.");
}
else if (commonAncestor == element)
{
Debug.Log("Attacher: An element can't be anchored to one of its descendants");
}
else if (commonAncestor == null)
{
Debug.Log("Attacher: The element and its target must be in the same visual tree hierarchy");
}
else
{
if (m_WatchedObjects == null)
m_WatchedObjects = new List<VisualElement>();
VisualElement v = target;
while (v != commonAncestor)
{
m_WatchedObjects.Add(v);
v.RegisterCallback<PostLayoutEvent>(OnTargetLayout);
v = v.shadow.parent;
}
v = element;
while (v != commonAncestor)
{
m_WatchedObjects.Add(v);
v.RegisterCallback<PostLayoutEvent>(OnTargetLayout);
v = v.shadow.parent;
}
}
}
private void UnregisterCallbacks()
{
foreach (VisualElement v in m_WatchedObjects)
{
{
v.UnregisterCallback<PostLayoutEvent>(OnTargetLayout);
}
}
m_WatchedObjects.Clear();
}
private void OnTargetLayout(PostLayoutEvent evt)
{
if (UpdateTargetBounds() || UpdateElementSize())
{
AlignOnTarget();
}
}
private bool UpdateTargetBounds()
{
Rect targetRect = target.worldBound;
if (m_LastTargetWorldBounds == targetRect)
{
return false;
}
m_LastTargetWorldBounds = targetRect;
return true;
}
private bool UpdateElementSize()
{
Rect elemRect = element.worldBound;
elemRect.position = Vector2.zero;
if (m_ElementSize == elemRect)
{
return false;
}
m_ElementSize = elemRect;
return true;
}
private void AlignOnTarget()
{
Rect currentRect = new Rect(element.style.positionLeft, element.style.positionTop, element.style.width, element.style.height);
Rect targetRect = target.rect;
targetRect = target.ChangeCoordinatesTo(element.shadow.parent, targetRect);
float centerY = 0;
//align Vertically
switch (alignment)
{
case SpriteAlignment.TopLeft:
case SpriteAlignment.TopCenter:
case SpriteAlignment.TopRight:
centerY = targetRect.y - currentRect.height * 0.5f - distance;
break;
case SpriteAlignment.LeftCenter:
case SpriteAlignment.RightCenter:
case SpriteAlignment.Center:
centerY = targetRect.center.y;
break;
case SpriteAlignment.BottomLeft:
case SpriteAlignment.BottomCenter:
case SpriteAlignment.BottomRight:
centerY = targetRect.yMax + currentRect.height * 0.5f + distance;
break;
}
float centerX = 0;
//alignHorizontally
switch (alignment)
{
case SpriteAlignment.TopLeft:
case SpriteAlignment.LeftCenter:
case SpriteAlignment.BottomLeft:
centerX = targetRect.x - currentRect.width * 0.5f - distance;
break;
case SpriteAlignment.TopCenter:
case SpriteAlignment.Center:
case SpriteAlignment.BottomCenter:
centerX = targetRect.center.x;
break;
case SpriteAlignment.TopRight:
case SpriteAlignment.RightCenter:
case SpriteAlignment.BottomRight:
centerX = targetRect.xMax + currentRect.width * 0.5f + distance;
break;
}
currentRect.center = new Vector2(centerX, centerY) + offset;
m_ElementSize.width = currentRect.width;
m_ElementSize.height = currentRect.height;
//we don't want the layout to be overwritten before styling has been applied
if (currentRect.width > 0)
{
element.layout = currentRect;
}
else
{
element.style.positionLeft = currentRect.xMin;
element.style.positionTop = currentRect.yMin;
}
m_LastTargetWorldBounds = target.worldBound;
}
}
}