forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionDropper.cs
More file actions
277 lines (223 loc) · 9.83 KB
/
Copy pathSelectionDropper.cs
File metadata and controls
277 lines (223 loc) · 9.83 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
// 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 System.Linq;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.Experimental.UIElements.GraphView
{
public delegate void DropEvent(IMGUIEvent evt, List<ISelectable> selection, IDropTarget dropTarget);
// TODO: Should stay internal when GraphView becomes public
internal class DragAndDropDelay
{
const float k_StartDragTreshold = 4.0f;
Vector2 mouseDownPosition { get; set; }
public void Init(Vector2 mousePosition)
{
mouseDownPosition = mousePosition;
}
public bool CanStartDrag(Vector2 mousePosition)
{
return Vector2.Distance(mouseDownPosition, mousePosition) > k_StartDragTreshold;
}
}
// Manipulates movable objects, can also initiate a Drag and Drop operation
// FIXME: update this code once we have support for drag and drop events in UIElements.
public class SelectionDropper : Manipulator
{
readonly DragAndDropDelay m_DragAndDropDelay;
// FIXME: remove this
public event DropEvent OnDrop;
bool m_Active;
public Vector2 panSpeed { get; set; }
public MouseButton activateButton { get; set; }
public bool clampToParentEdges { get; set; }
// selectedElement is used to store a unique selection candidate for cases where user clicks on an item not to
// drag it but just to reset the selection -- we only know this after the manipulation has ended
GraphElement selectedElement { get; set; }
ISelection selectionContainer { get; set; }
public SelectionDropper(DropEvent handler)
{
m_Active = true;
OnDrop += handler;
m_DragAndDropDelay = new DragAndDropDelay();
activateButton = MouseButton.LeftMouse;
panSpeed = new Vector2(1, 1);
}
protected override void RegisterCallbacksOnTarget()
{
target.RegisterCallback<MouseDownEvent>(OnMouseDown);
target.RegisterCallback<MouseMoveEvent>(OnMouseMove);
target.RegisterCallback<MouseUpEvent>(OnMouseUp);
target.RegisterCallback<IMGUIEvent>(OnIMGUIEvent);
}
protected override void UnregisterCallbacksFromTarget()
{
target.UnregisterCallback<MouseDownEvent>(OnMouseDown);
target.UnregisterCallback<MouseMoveEvent>(OnMouseMove);
target.UnregisterCallback<MouseUpEvent>(OnMouseUp);
target.UnregisterCallback<IMGUIEvent>(OnIMGUIEvent);
}
bool m_AddedByMouseDown;
bool m_Dragging;
protected void OnMouseDown(MouseDownEvent e)
{
if (m_Active)
{
e.StopImmediatePropagation();
return;
}
if (MouseCaptureController.IsMouseCaptureTaken())
return;
m_Active = false;
m_Dragging = false;
m_AddedByMouseDown = false;
if (target == null)
return;
selectionContainer = target.GetFirstAncestorOfType<ISelection>();
if (selectionContainer == null)
return;
selectedElement = target.GetFirstOfType<GraphElement>();
if (selectedElement == null)
return;
// Since we didn't drag after all, update selection with current element only
if (!selectionContainer.selection.Contains(selectedElement))
{
if (!e.ctrlKey)
selectionContainer.ClearSelection();
selectionContainer.AddToSelection(selectedElement);
m_AddedByMouseDown = true;
}
if (e.button == (int)activateButton)
{
// avoid starting a manipulation on a non movable object
if (!selectedElement.IsDroppable())
return;
// Reset drag and drop
m_DragAndDropDelay.Init(e.localMousePosition);
m_Active = true;
target.TakeMouseCapture();
e.StopPropagation();
}
}
protected void OnMouseMove(MouseMoveEvent e)
{
if (m_Active && !m_Dragging && selectionContainer != null)
{
// Keep a copy of the selection
var selection = selectionContainer.selection.ToList();
if (selection.Count > 0)
{
var ce = selection[0] as GraphElement;
bool canStartDrag = ce != null && ce.IsDroppable();
if (canStartDrag && m_DragAndDropDelay.CanStartDrag(e.localMousePosition))
{
DragAndDrop.PrepareStartDrag();
DragAndDrop.objectReferences = new UnityEngine.Object[] {}; // this IS required for dragging to work
DragAndDrop.SetGenericData("DragSelection", selection);
m_Dragging = true;
DragAndDrop.StartDrag("");
DragAndDrop.visualMode = e.ctrlKey ? DragAndDropVisualMode.Copy : DragAndDropVisualMode.Move;
}
e.StopPropagation();
}
}
}
protected void OnMouseUp(MouseUpEvent e)
{
if (!m_Active || selectionContainer == null)
return;
if (e.button == (int)activateButton)
{
// Since we didn't drag after all, update selection with current element only
if (!e.ctrlKey)
{
selectionContainer.ClearSelection();
selectionContainer.AddToSelection(selectedElement);
}
else if (!m_AddedByMouseDown && !m_Dragging)
{
selectionContainer.RemoveFromSelection(selectedElement);
}
target.ReleaseMouseCapture();
e.StopPropagation();
}
m_Active = false;
m_AddedByMouseDown = false;
m_Dragging = false;
}
public IDropTarget prevDropTarget;
protected void OnIMGUIEvent(IMGUIEvent e)
{
if (!m_Active || selectionContainer == null)
return;
// Keep a copy of the selection
var selection = selectionContainer.selection.ToList();
Event evt = e.imguiEvent;
switch (evt.type)
{
case EventType.DragUpdated:
{
if (target.HasMouseCapture() && evt.button == (int)activateButton && selection.Count > 0)
{
selectedElement = null;
// TODO: Replace with a temp drawing or something...maybe manipulator could fake position
// all this to let operation know which element sits under cursor...or is there another way to draw stuff that is being dragged?
if (OnDrop != null)
{
var pickElem = target.panel.Pick(target.LocalToWorld(evt.mousePosition));
IDropTarget dropTarget = pickElem != null ? pickElem.GetFirstAncestorOfType<IDropTarget>() : null;
if (prevDropTarget != dropTarget && prevDropTarget != null)
{
using (IMGUIEvent eexit = IMGUIEvent.GetPooled(e.imguiEvent))
{
eexit.imguiEvent.type = EventType.DragExited;
OnDrop(eexit, selection, prevDropTarget);
}
}
OnDrop(e, selection, dropTarget);
prevDropTarget = dropTarget;
}
DragAndDrop.visualMode = evt.control ? DragAndDropVisualMode.Copy : DragAndDropVisualMode.Move;
}
break;
}
case EventType.DragExited:
{
if (OnDrop != null && prevDropTarget != null)
{
OnDrop(e, selection, prevDropTarget);
}
DragAndDrop.visualMode = DragAndDropVisualMode.None;
DragAndDrop.SetGenericData("DragSelection", null);
prevDropTarget = null;
m_Active = false;
target.ReleaseMouseCapture();
break;
}
case EventType.DragPerform:
{
if (m_Active && evt.button == (int)activateButton && selection.Count > 0)
{
if (selection.Count > 0)
{
if (OnDrop != null)
{
var pickElem = target.panel.Pick(target.LocalToWorld(evt.mousePosition));
IDropTarget dropTarget = pickElem != null ? pickElem.GetFirstAncestorOfType<IDropTarget>() : null;
OnDrop(e, selection, dropTarget);
}
DragAndDrop.visualMode = DragAndDropVisualMode.None;
DragAndDrop.SetGenericData("DragSelection", null);
}
}
prevDropTarget = null;
m_Active = false;
target.ReleaseMouseCapture();
break;
}
}
}
}
}