forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectangleSelector.cs
More file actions
228 lines (188 loc) · 8.3 KB
/
Copy pathRectangleSelector.cs
File metadata and controls
228 lines (188 loc) · 8.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
// 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 System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleEnums;
namespace UnityEditor.Experimental.UIElements.GraphView
{
public class RectangleSelector : MouseManipulator
{
private readonly RectangleSelect m_Rectangle;
bool m_Active;
public RectangleSelector()
{
activators.Add(new ManipulatorActivationFilter {button = MouseButton.LeftMouse});
activators.Add(new ManipulatorActivationFilter { button = MouseButton.LeftMouse, modifiers = EventModifiers.Shift });
m_Rectangle = new RectangleSelect();
m_Rectangle.style.positionType = PositionType.Absolute;
m_Rectangle.style.positionTop = 0;
m_Rectangle.style.positionLeft = 0;
m_Rectangle.style.positionBottom = 0;
m_Rectangle.style.positionRight = 0;
m_Active = false;
}
// get the axis aligned bound
public Rect ComputeAxisAlignedBound(Rect position, Matrix4x4 transform)
{
Vector3 min = transform.MultiplyPoint3x4(position.min);
Vector3 max = transform.MultiplyPoint3x4(position.max);
return Rect.MinMaxRect(Math.Min(min.x, max.x), Math.Min(min.y, max.y), Math.Max(min.x, max.x), Math.Max(min.y, max.y));
}
protected override void RegisterCallbacksOnTarget()
{
var graphView = target as GraphView;
if (graphView == null)
{
throw new InvalidOperationException("Manipulator can only be added to a GraphView");
}
target.RegisterCallback<MouseDownEvent>(OnMouseDown);
target.RegisterCallback<MouseUpEvent>(OnMouseUp);
target.RegisterCallback<MouseMoveEvent>(OnMouseMove);
target.RegisterCallback<MouseCaptureOutEvent>(OnMouseCaptureOutEvent);
}
protected override void UnregisterCallbacksFromTarget()
{
target.UnregisterCallback<MouseDownEvent>(OnMouseDown);
target.UnregisterCallback<MouseUpEvent>(OnMouseUp);
target.UnregisterCallback<MouseMoveEvent>(OnMouseMove);
target.UnregisterCallback<MouseCaptureOutEvent>(OnMouseCaptureOutEvent);
}
void OnMouseCaptureOutEvent(MouseCaptureOutEvent e)
{
if (m_Active)
{
m_Rectangle.RemoveFromHierarchy();
m_Active = false;
}
}
private void OnMouseDown(MouseDownEvent e)
{
if (m_Active)
{
e.StopImmediatePropagation();
return;
}
var graphView = target as GraphView;
if (graphView == null)
return;
if (CanStartManipulation(e))
{
if (!e.shiftKey)
{
graphView.ClearSelection();
}
graphView.Add(m_Rectangle);
m_Rectangle.start = e.localMousePosition;
m_Rectangle.end = m_Rectangle.start;
m_Active = true;
target.TakeMouseCapture(); // We want to receive events even when mouse is not over ourself.
e.StopPropagation();
}
}
private void OnMouseUp(MouseUpEvent e)
{
if (!m_Active)
return;
var graphView = target as GraphView;
if (graphView == null)
return;
if (!CanStopManipulation(e))
return;
graphView.Remove(m_Rectangle);
m_Rectangle.end = e.localMousePosition;
var selectionRect = new Rect()
{
min = new Vector2(Math.Min(m_Rectangle.start.x, m_Rectangle.end.x), Math.Min(m_Rectangle.start.y, m_Rectangle.end.y)),
max = new Vector2(Math.Max(m_Rectangle.start.x, m_Rectangle.end.x), Math.Max(m_Rectangle.start.y, m_Rectangle.end.y))
};
selectionRect = ComputeAxisAlignedBound(selectionRect, graphView.viewTransform.matrix.inverse);
List<ISelectable> selection = graphView.selection;
// a copy is necessary because Add To selection might cause a SendElementToFront which will change the order.
List<ISelectable> newSelection = new List<ISelectable>();
graphView.graphElements.ForEach(child =>
{
var localSelRect = graphView.contentViewContainer.ChangeCoordinatesTo(child, selectionRect);
if (child.IsSelectable() && child.Overlaps(localSelRect))
{
newSelection.Add(child);
}
});
foreach (var selectable in newSelection)
{
if (selection.Contains(selectable))
{
if (e.shiftKey) // invert selection on shift only
graphView.RemoveFromSelection(selectable);
}
else
graphView.AddToSelection(selectable);
}
m_Active = false;
target.ReleaseMouseCapture();
e.StopPropagation();
}
private void OnMouseMove(MouseMoveEvent e)
{
if (!m_Active)
return;
m_Rectangle.end = e.localMousePosition;
e.StopPropagation();
}
private class RectangleSelect : VisualElement
{
public Vector2 start { get; set; }
public Vector2 end { get; set; }
public override void DoRepaint()
{
VisualElement t = parent;
Vector2 screenStart = start;
Vector2 screenEnd = end;
// Avoid drawing useless information
if (start == end)
return;
// Apply offset
screenStart += t.layout.position;
screenEnd += t.layout.position;
var r = new Rect
{
min = new Vector2(Math.Min(screenStart.x, screenEnd.x), Math.Min(screenStart.y, screenEnd.y)),
max = new Vector2(Math.Max(screenStart.x, screenEnd.x), Math.Max(screenStart.y, screenEnd.y))
};
var lineColor = new Color(1.0f, 0.6f, 0.0f, 1.0f);
var segmentSize = 5f;
Vector3[] points =
{
new Vector3(r.xMin, r.yMin, 0.0f),
new Vector3(r.xMax, r.yMin, 0.0f),
new Vector3(r.xMax, r.yMax, 0.0f),
new Vector3(r.xMin, r.yMax, 0.0f)
};
DrawDottedLine(points[0], points[1], segmentSize, lineColor);
DrawDottedLine(points[1], points[2], segmentSize, lineColor);
DrawDottedLine(points[2], points[3], segmentSize, lineColor);
DrawDottedLine(points[3], points[0], segmentSize, lineColor);
var str = "(" + String.Format("{0:0}", start.x) + ", " + String.Format("{0:0}", start.y) + ")";
GUI.skin.label.Draw(new Rect(screenStart.x, screenStart.y - 18.0f, 200.0f, 20.0f), new GUIContent(str), 0);
str = "(" + String.Format("{0:0}", end.x) + ", " + String.Format("{0:0}", end.y) + ")";
GUI.skin.label.Draw(new Rect(screenEnd.x - 80.0f, screenEnd.y + 5.0f, 200.0f, 20.0f), new GUIContent(str), 0);
}
private void DrawDottedLine(Vector3 p1, Vector3 p2, float segmentsLength, Color col)
{
HandleUtility.ApplyWireMaterial();
GL.Begin(GL.LINES);
GL.Color(col);
float length = Vector3.Distance(p1, p2); // ignore z component
int count = Mathf.CeilToInt(length / segmentsLength);
for (int i = 0; i < count; i += 2)
{
GL.Vertex((Vector3.Lerp(p1, p2, i * segmentsLength / length)));
GL.Vertex((Vector3.Lerp(p1, p2, (i + 1) * segmentsLength / length)));
}
GL.End();
}
}
}
}