forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniMap.cs
More file actions
368 lines (297 loc) · 13.7 KB
/
Copy pathMiniMap.cs
File metadata and controls
368 lines (297 loc) · 13.7 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
// 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;
namespace UnityEditor.Experimental.UIElements.GraphView
{
public class MiniMap : GraphElement
{
public float maxHeight { get; set; }
public float maxWidth { get; set; }
float m_PreviousContainerWidth = -1;
float m_PreviousContainerHeight = -1;
readonly Label m_Label;
Dragger m_Dragger;
readonly Color m_ViewportColor = new Color(1.0f, 1.0f, 0.0f, 0.35f);
readonly Color m_SelectedChildrenColor = new Color(1.0f, 1.0f, 1.0f, 0.5f);
// Various rects used by the MiniMap
Rect m_ViewportRect; // Rect that represents the current viewport
Rect m_ContentRect; // Rect that represents the rect needed to encompass all Graph Elements
Rect m_ContentRectLocal; // Rect that represents the rect needed to encompass all Graph Elements in local coords
int titleBarOffset { get { return (int)style.paddingTop; } }
private bool m_Anchored;
public bool anchored
{
get { return m_Anchored; }
set
{
if (m_Anchored == value)
return;
m_Anchored = value;
if (m_Anchored)
{
capabilities &= ~Capabilities.Movable;
ResetPositionProperties();
AddToClassList("anchored");
}
else
{
capabilities |= Capabilities.Movable;
RemoveFromClassList("anchored");
}
Resize();
}
}
public MiniMap()
{
clippingOptions = ClippingOptions.NoClipping;
capabilities = Capabilities.Movable;
m_Dragger = new Dragger { clampToParentEdges = true };
this.AddManipulator(m_Dragger);
anchored = false;
maxWidth = 200;
maxHeight = 180;
m_Label = new Label("Floating Minimap");
Add(m_Label);
RegisterCallback<MouseDownEvent>(OnMouseDown);
m_Label.RegisterCallback<MouseDownEvent>(EatMouseDown);
this.AddManipulator(new ContextualMenuManipulator(BuildContextualMenu));
AddStyleSheetPath("StyleSheets/GraphView/Minimap.uss");
}
private GraphView m_GraphView;
private GraphView graphView
{
get
{
if (m_GraphView == null) m_GraphView = GetFirstAncestorOfType<GraphView>();
return m_GraphView;
}
}
void ToggleAnchorState(ContextualMenu.MenuAction a)
{
anchored = !anchored;
}
public virtual void BuildContextualMenu(ContextualMenuPopulateEvent evt)
{
evt.menu.AppendAction(anchored ? "Make floating" : "Anchor", ToggleAnchorState, ContextualMenu.MenuAction.AlwaysEnabled);
}
void Resize()
{
if (parent == null)
return;
style.width = maxWidth;
style.height = maxHeight;
// Relocate if partially visible on bottom or right side (left/top not checked, only bottom/right affected by a size change)
if (style.positionLeft + style.width > parent.layout.x + parent.layout.width)
{
var newPosition = layout;
newPosition.x -= style.positionLeft + style.width - (parent.layout.x + parent.layout.width);
layout = newPosition;
}
if (style.positionTop + style.height > parent.layout.y + parent.layout.height)
{
var newPosition = layout;
newPosition.y -= style.positionTop + style.height - (parent.layout.y + parent.layout.height);
layout = newPosition;
}
var newMiniMapPos = layout;
newMiniMapPos.width = style.width;
newMiniMapPos.height = style.height;
newMiniMapPos.x = Mathf.Max(parent.layout.x, newMiniMapPos.x);
newMiniMapPos.y = Mathf.Max(parent.layout.y, newMiniMapPos.y);
layout = newMiniMapPos;
}
static void ChangeToMiniMapCoords(ref Rect rect, float factor, Vector3 translation)
{
// Apply factor
rect.width *= factor;
rect.height *= factor;
rect.x *= factor;
rect.y *= factor;
// Apply translation
rect.x += translation.x;
rect.y += translation.y;
}
void CalculateRects(VisualElement container)
{
m_ContentRect = graphView.CalculateRectToFitAll(container);
m_ContentRectLocal = m_ContentRect;
// Retrieve viewport rectangle as if zoom and pan were inactive
Matrix4x4 containerInvTransform = container.worldTransform.inverse;
Vector4 containerInvTranslation = containerInvTransform.GetColumn(3);
var containerInvScale = new Vector2(containerInvTransform.m00, containerInvTransform.m11);
m_ViewportRect = parent.rect;
// Bring back viewport coordinates to (0,0), scale 1:1
m_ViewportRect.x += containerInvTranslation.x;
m_ViewportRect.y += containerInvTranslation.y;
m_ViewportRect.x += (parent.worldBound.x * containerInvScale.x);
m_ViewportRect.y += (parent.worldBound.y * containerInvScale.y);
m_ViewportRect.width *= containerInvScale.x;
m_ViewportRect.height *= containerInvScale.y;
// Update label with new value
var containerZoomFactor = container.worldTransform.m00;
m_Label.text = "MiniMap " + string.Format("{0:F2}", containerZoomFactor) + "x";
// Adjust rects for MiniMap
// Encompass viewport rectangle (as if zoom and pan were inactive)
var totalRect = RectUtils.Encompass(m_ContentRect, m_ViewportRect);
var minimapFactor = layout.width / totalRect.width;
// Transform each rect to MiniMap coordinates
ChangeToMiniMapCoords(ref totalRect, minimapFactor, Vector3.zero);
var minimapTranslation = new Vector3(-totalRect.x, titleBarOffset - totalRect.y);
ChangeToMiniMapCoords(ref m_ViewportRect, minimapFactor, minimapTranslation);
ChangeToMiniMapCoords(ref m_ContentRect, minimapFactor, minimapTranslation);
// Diminish and center everything to fit vertically
if (totalRect.height > (layout.height - titleBarOffset))
{
float totalRectFactor = (layout.height - titleBarOffset) / totalRect.height;
float totalRectOffsetX = (layout.width - (totalRect.width * totalRectFactor)) / 2.0f;
float totalRectOffsetY = titleBarOffset - ((totalRect.y + minimapTranslation.y) * totalRectFactor);
m_ContentRect.width *= totalRectFactor;
m_ContentRect.height *= totalRectFactor;
m_ContentRect.y *= totalRectFactor;
m_ContentRect.x += totalRectOffsetX;
m_ContentRect.y += totalRectOffsetY;
m_ViewportRect.width *= totalRectFactor;
m_ViewportRect.height *= totalRectFactor;
m_ViewportRect.y *= totalRectFactor;
m_ViewportRect.x += totalRectOffsetX;
m_ViewportRect.y += totalRectOffsetY;
}
}
Rect CalculateElementRect(GraphElement elem)
{
// TODO: Should Edges be displayed at all?
// TODO: Maybe edges need their own capabilities flag.
if (elem is Edge || elem is Port)
{
return new Rect(0, 0, 0, 0);
}
Rect rect = elem.ChangeCoordinatesTo(graphView.contentViewContainer, elem.rect);
rect.x = m_ContentRect.x + ((rect.x - m_ContentRectLocal.x) * m_ContentRect.width / m_ContentRectLocal.width);
rect.y = m_ContentRect.y + ((rect.y - m_ContentRectLocal.y) * m_ContentRect.height / m_ContentRectLocal.height);
rect.width *= m_ContentRect.width / m_ContentRectLocal.width;
rect.height *= m_ContentRect.height / m_ContentRectLocal.height;
// Clip using a minimal 2 pixel wide frame around edges
// (except yMin since we already have the titleBar offset which is enough for clipping)
var xMin = 2;
var yMin = 0;
var xMax = layout.width - 2;
var yMax = layout.height - 2;
if (rect.x < xMin)
{
if (rect.x < xMin - rect.width)
return new Rect(0, 0, 0, 0);
rect.width -= xMin - rect.x;
rect.x = xMin;
}
if (rect.x + rect.width >= xMax)
{
if (rect.x >= xMax)
return new Rect(0, 0, 0, 0);
rect.width -= rect.x + rect.width - xMax;
}
if (rect.y < yMin + titleBarOffset)
{
if (rect.y < yMin + titleBarOffset - rect.height)
return new Rect(0, 0, 0, 0);
rect.height -= yMin + titleBarOffset - rect.y;
rect.y = yMin + titleBarOffset;
}
if (rect.y + rect.height >= yMax)
{
if (rect.y >= yMax)
return new Rect(0, 0, 0, 0);
rect.height -= rect.y + rect.height - yMax;
}
return rect;
}
private static Vector3[] s_CachedRect = new Vector3[4];
public override void DoRepaint()
{
var gView = graphView;
VisualElement container = gView.contentViewContainer;
// Retrieve all container relative information
Matrix4x4 containerTransform = gView.viewTransform.matrix;
var containerScale = new Vector2(containerTransform.m00, containerTransform.m11);
float containerWidth = parent.layout.width / containerScale.x;
float containerHeight = parent.layout.height / containerScale.y;
if (Mathf.Abs(containerWidth - m_PreviousContainerWidth) > Mathf.Epsilon ||
Mathf.Abs(containerHeight - m_PreviousContainerHeight) > Mathf.Epsilon)
{
m_PreviousContainerWidth = containerWidth;
m_PreviousContainerHeight = containerHeight;
Resize();
}
// Refresh MiniMap rects
CalculateRects(container);
// Let the base call draw the background and so on
base.DoRepaint();
// Display elements in the MiniMap
Color currentColor = Handles.color;
gView.graphElements.ForEach(elem =>
{
if (elem is Edge || elem is Port)
return;
var rect = CalculateElementRect(elem);
Handles.color = elem.elementTypeColor;
s_CachedRect[0].Set(rect.xMin, rect.yMin, 0.0f);
s_CachedRect[1].Set(rect.xMax, rect.yMin, 0.0f);
s_CachedRect[2].Set(rect.xMax, rect.yMax, 0.0f);
s_CachedRect[3].Set(rect.xMin, rect.yMax, 0.0f);
Handles.DrawSolidRectangleWithOutline(s_CachedRect, elem.elementTypeColor, elem.elementTypeColor);
if (elem.selected)
DrawRectangleOutline(rect, m_SelectedChildrenColor);
});
// Draw viewport outline
DrawRectangleOutline(m_ViewportRect, m_ViewportColor);
Handles.color = currentColor;
}
void DrawRectangleOutline(Rect rect, Color color)
{
Color currentColor = Handles.color;
Handles.color = color;
// Draw viewport outline
Vector3[] points = new Vector3[5];
points[0] = new Vector3(rect.x, rect.y, 0.0f);
points[1] = new Vector3(rect.x + rect.width, rect.y, 0.0f);
points[2] = new Vector3(rect.x + rect.width, rect.y + rect.height, 0.0f);
points[3] = new Vector3(rect.x, rect.y + rect.height, 0.0f);
points[4] = new Vector3(rect.x, rect.y, 0.0f);
Handles.DrawPolyLine(points);
Handles.color = currentColor;
}
private void EatMouseDown(MouseDownEvent e)
{
// The minimap should not let any left mouse down go through when it's not movable.
if (e.button == (int)MouseButton.LeftMouse &&
(capabilities & Capabilities.Movable) == 0)
{
e.StopPropagation();
}
}
private void OnMouseDown(MouseDownEvent e)
{
var gView = graphView;
// Refresh MiniMap rects
CalculateRects(gView.contentViewContainer);
var mousePosition = e.localMousePosition;
gView.graphElements.ForEach(child =>
{
if (child == null)
return;
var selectable = child.GetFirstOfType<ISelectable>();
if (selectable == null || !selectable.IsSelectable())
return;
if (CalculateElementRect(child).Contains(mousePosition))
{
gView.ClearSelection();
gView.AddToSelection(selectable);
gView.FrameSelection();
e.StopPropagation();
}
});
EatMouseDown(e);
}
}
}