TimelineDragging.cs 29.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 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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.IMGUI.Controls;
using UnityEditor.Timeline;
using UnityEngine;
using UnityEngine.Timeline;
using UnityEngine.Playables;
using UnityObject = UnityEngine.Object;

namespace UnityEditor
{
    class TimelineDragging : TreeViewDragging
    {
        public delegate bool TypeResolver(IEnumerable<Type> types, Action<Type> onComplete, string format);

        private static readonly string k_SelectTrackWithBinding = LocalizationDatabase.GetLocalizedString("Add {0}");
        private static readonly string k_SelectTrackWithClip = LocalizationDatabase.GetLocalizedString("Add Clip With {0}");
        private static readonly string k_SelectClip = LocalizationDatabase.GetLocalizedString("Add {0}");


        const string k_GenericDragId = "TimelineDragging";
        readonly int kDragSensitivity = 2;
        readonly TimelineAsset m_Timeline;
        readonly TimelineWindow m_Window;

        class TimelineDragData
        {
            public TimelineDragData(List<TreeViewItem> draggedItems)
            {
                this.draggedItems = draggedItems;
            }

            public readonly List<TreeViewItem> draggedItems;
        }

        public TimelineDragging(TreeViewController treeView, TimelineWindow window, TimelineAsset data)
            : base(treeView)
        {
            m_Timeline = data;
            m_Window = window;
        }

        public override bool CanStartDrag(TreeViewItem targetItem, List<int> draggedItemIDs, Vector2 mouseDownPosition)
        {
            if (Event.current.modifiers != EventModifiers.None)
                return false;

            // Can only drag when starting in the track header area
            if (mouseDownPosition.x > m_Window.sequenceHeaderRect.xMax)
                return false;

            var trackBaseGUI = targetItem as TimelineTrackBaseGUI;

            if (trackBaseGUI == null || trackBaseGUI.track == null)
                return false;

            if (trackBaseGUI.track.lockedInHierarchy)
                return false;

            if (Event.current.type == EventType.MouseDrag && Mathf.Abs(Event.current.delta.y) < kDragSensitivity)
                return false;

            // Make sure dragged items are selected
            // TODO Use similar system than the SceneHierarchyWindow in order to handle selection between treeView and tracks.
            SelectionManager.Clear();
            var draggedTrackGUIs = m_Window.allTracks.Where(t => draggedItemIDs.Contains(t.id));
            foreach (var trackGUI in draggedTrackGUIs)
                SelectionManager.Add(trackGUI.track);

            return true;
        }

        public override void StartDrag(TreeViewItem draggedNode, List<int> draggedItemIDs)
        {
            DragAndDrop.PrepareStartDrag();
            var tvItems = SelectionManager.SelectedTrackGUI().Cast<TreeViewItem>().ToList();
            DragAndDrop.SetGenericData(k_GenericDragId, new TimelineDragData(tvItems));
            DragAndDrop.objectReferences = new UnityObject[] {};  // this IS required for dragging to work

            string title = draggedItemIDs.Count + (draggedItemIDs.Count > 1 ? "s" : ""); // title is only shown on OSX (at the cursor)

            TimelineGroupGUI groupGui = draggedNode as TimelineGroupGUI;
            if (groupGui != null)
            {
                title = groupGui.displayName;
            }
            DragAndDrop.StartDrag(title);
        }

        public static bool IsDraggingEvent()
        {
            return Event.current.type == EventType.DragUpdated ||
                Event.current.type == EventType.DragExited ||
                Event.current.type == EventType.DragPerform;
        }

        public static bool ResolveType(IEnumerable<System.Type> types, Action<Type> onComplete, string formatString)
        {
            if (!types.Any() || onComplete == null)
                return false;

            if (types.Count() == 1)
            {
                onComplete(types.First());
                return true;
            }

            var menu = new GenericMenu();

            var builtInTypes = types.Where(TypeUtility.IsBuiltIn).OrderBy(TypeUtility.GetDisplayName).ToArray();
            var customTypes = types.Where(x => !TypeUtility.IsBuiltIn(x)).OrderBy(TypeUtility.GetDisplayName).ToArray();

            foreach (var t in builtInTypes)
            {
                menu.AddItem(new GUIContent(string.Format(formatString, TypeUtility.GetDisplayName(t))), false, s => onComplete((System.Type)s), t);
            }

            if (builtInTypes.Length != 0 && customTypes.Length != 0)
                menu.AddSeparator(string.Empty);

            foreach (var t in customTypes)
            {
                menu.AddItem(new GUIContent(string.Format(formatString, TypeUtility.GetDisplayName(t))), false, s => onComplete((System.Type)s), t);
            }

            menu.ShowAsContext();
            return true;
        }

        public override bool DragElement(TreeViewItem targetItem, Rect targetItemRect, int row)
        {
            if (TimelineWindow.instance.state.editSequence.isReadOnly)
                return false;
            // the drop rect contains the row rect plus additional spacing. The base drag element overlaps 1/2 the height of the next track
            // which interferes with track bindings
            var targetTrack = targetItem as TimelineGroupGUI;
            if (row > 0 && targetTrack != null && !targetTrack.dropRect.Contains(Event.current.mousePosition))
                return false;

            return base.DragElement(targetItem, targetItemRect, row);
        }

        TreeViewItem GetNextItem(TreeViewItem item)
        {
            if (item == null)
                return null;

            if (item.parent == null)
            {
                int row = m_Window.treeView.data.GetRow(item.id);
                var items = m_Window.treeView.data.GetRows();
                if (items.Count > row + 1)
                    return items[row + 1];
                return null;
            }

            var children = item.parent.children;
            if (children == null)
                return null;

            for (int i = 0; i < children.Count - 1; i++)
            {
                if (children[i] == item)
                    return children[i + 1];
            }
            return null;
        }

        private static TrackAsset GetTrack(TreeViewItem item)
        {
            TimelineTrackBaseGUI baseGui = item as TimelineTrackBaseGUI;
            if (baseGui == null)
                return null;
            return baseGui.track;
        }

        // The drag and drop may be over an expanded group but might be between tracks
        private void HandleNestedItemGUI(ref TreeViewItem parentItem, ref TreeViewItem targetItem, ref TreeViewItem insertBefore)
        {
            const float kTopPad = 5;
            const float kBottomPad = 5;

            insertBefore = null;

            if (!ShouldUseHierarchyDragAndDrop())
                return;

            var targetTrack = targetItem as TimelineGroupGUI;
            if (targetTrack == null)
                return;

            var mousePosition = Event.current.mousePosition;

            var dropBefore = targetTrack.rowRect.yMin + kTopPad > mousePosition.y;
            var dropAfter = !(targetTrack.track is GroupTrack) && (targetTrack.rowRect.yMax - kBottomPad < mousePosition.y);

            targetTrack.drawInsertionMarkerBefore = dropBefore;
            targetTrack.drawInsertionMarkerAfter = dropAfter;

            if (dropBefore)
            {
                targetItem = parentItem;
                parentItem = targetItem != null ? targetItem.parent : null;
                insertBefore = targetTrack;
            }
            else if (dropAfter)
            {
                targetItem = parentItem;
                parentItem = targetItem != null ? targetItem.parent : null;
                insertBefore = GetNextItem(targetTrack);
            }
            else if (targetTrack.track is GroupTrack)
            {
                targetTrack.isDropTarget = true;
            }
        }

        public override DragAndDropVisualMode DoDrag(TreeViewItem parentItem, TreeViewItem targetItem, bool perform, DropPosition dropPos)
        {
            m_Window.isDragging = false;

            var retMode = DragAndDropVisualMode.None;

            var trackDragData = DragAndDrop.GetGenericData(k_GenericDragId) as TimelineDragData;

            if (trackDragData != null)
            {
                retMode = HandleTrackDrop(parentItem, targetItem, perform, dropPos);
                if (retMode == DragAndDropVisualMode.Copy && targetItem != null && Event.current.type == EventType.DragUpdated)
                {
                    var targetActor = targetItem as TimelineGroupGUI;
                    if (targetActor != null)
                        targetActor.isDropTarget = true;
                }
            }
            else if (DragAndDrop.objectReferences.Any())
            {
                var objectsBeingDropped = DragAndDrop.objectReferences.OfType<UnityObject>();
                var director = m_Window.state.editSequence.director;

                if (ShouldUseHierarchyDragAndDrop())
                {
                    // for object drawing
                    var originalTarget = targetItem;
                    TreeViewItem insertBeforeItem = null;
                    HandleNestedItemGUI(ref parentItem, ref targetItem, ref insertBeforeItem);
                    var track = GetTrack(targetItem);
                    var parent = GetTrack(parentItem);
                    var insertBefore = GetTrack(insertBeforeItem);
                    retMode = HandleHierarchyPaneDragAndDrop(objectsBeingDropped, track, perform, m_Timeline, director, ResolveType, insertBefore);

                    // fallback to old clip behaviour
                    if (retMode == DragAndDropVisualMode.None)
                    {
                        retMode = HandleClipPaneObjectDragAndDrop(objectsBeingDropped, track, perform, m_Timeline, parent, director, m_Window.state.timeAreaShownRange.x, ResolveType, insertBefore);
                    }

                    // if we are rejected, clear any drop markers
                    if (retMode == DragAndDropVisualMode.Rejected && targetItem != null)
                    {
                        ClearInsertionMarkers(originalTarget);
                        ClearInsertionMarkers(targetItem);
                        ClearInsertionMarkers(parentItem);
                        ClearInsertionMarkers(insertBeforeItem);
                    }
                }
                else
                {
                    var candidateTime = TimelineHelpers.GetCandidateTime(m_Window.state, Event.current.mousePosition);
                    retMode = HandleClipPaneObjectDragAndDrop(objectsBeingDropped, GetTrack(targetItem), perform, m_Timeline, GetTrack(parentItem), director, candidateTime, ResolveType);
                }
            }

            m_Window.isDragging = false;

            return retMode;
        }

        void ClearInsertionMarkers(TreeViewItem item)
        {
            var trackGUI = item as TimelineTrackBaseGUI;
            if (trackGUI != null)
            {
                trackGUI.drawInsertionMarkerAfter = false;
                trackGUI.drawInsertionMarkerBefore = false;
                trackGUI.isDropTarget = false;
            }
        }

        bool ShouldUseHierarchyDragAndDrop()
        {
            return m_Window.state.IsEditingAnEmptyTimeline() || m_Window.state.sequencerHeaderWidth > Event.current.mousePosition.x;
        }

        public static DragAndDropVisualMode HandleHierarchyPaneDragAndDrop(IEnumerable<UnityObject> objectsBeingDropped, TrackAsset targetTrack, bool perform, TimelineAsset timeline, PlayableDirector director, TypeResolver typeResolver, TrackAsset insertBefore = null)
        {
            if (timeline == null)
                return DragAndDropVisualMode.Rejected;

            // if we are over a target track, defer to track binding system (implemented in TrackGUIs), unless we are a groupTrack
            if (targetTrack != null && (targetTrack as GroupTrack) == null)
                return DragAndDropVisualMode.None;

            if (targetTrack != null && targetTrack.lockedInHierarchy)
                return DragAndDropVisualMode.Rejected;

            var tracksWithBinding = objectsBeingDropped.SelectMany(TypeUtility.GetTracksCreatableFromObject).Distinct();
            if (!tracksWithBinding.Any())
                return DragAndDropVisualMode.None;

            if (perform)
            {
                System.Action<Type> onResolve = trackType =>
                {
                    foreach (var obj in objectsBeingDropped)
                    {
                        if (!obj.IsPrefab() && TypeUtility.IsTrackCreatableFromObject(obj, trackType))
                        {
                            var newTrack = TimelineHelpers.CreateTrack(timeline, trackType, targetTrack, string.Empty);
                            if (insertBefore != null)
                            {
                                if (targetTrack != null)
                                    targetTrack.MoveLastTrackBefore(insertBefore);
                                else
                                    timeline.MoveLastTrackBefore(insertBefore);
                            }

                            TimelineHelpers.Bind(newTrack, obj, director);
                        }
                    }
                    TimelineEditor.Refresh(RefreshReason.ContentsAddedOrRemoved);
                };
                typeResolver(tracksWithBinding, onResolve, k_SelectTrackWithBinding);
            }

            return DragAndDropVisualMode.Copy;
        }

        public static DragAndDropVisualMode HandleClipPaneObjectDragAndDrop(IEnumerable<UnityObject> objectsBeingDropped, TrackAsset targetTrack, bool perform, TimelineAsset timeline, TrackAsset parent, PlayableDirector director, double candidateTime, TypeResolver typeResolver, TrackAsset insertBefore = null)
        {
            if (timeline == null)
                return DragAndDropVisualMode.Rejected;

            // locked tracks always reject
            if (targetTrack != null && targetTrack.lockedInHierarchy)
                return DragAndDropVisualMode.Rejected;

            // treat group tracks as having no track
            if (targetTrack is GroupTrack)
            {
                parent = targetTrack;
                targetTrack = null;
            }

            // Special case for monoscripts, since they describe the type
            if (objectsBeingDropped.Any(o => o is MonoScript))
                return HandleClipPaneMonoScriptDragAndDrop(objectsBeingDropped.OfType<MonoScript>(), targetTrack, perform, timeline, parent, director, candidateTime);

            // no unity objects, or explicit exceptions
            if (!objectsBeingDropped.Any() || objectsBeingDropped.Any(o => !ValidateObjectDrop(o)))
                return DragAndDropVisualMode.Rejected;

            // reject scene references if we have no context
            if (director == null && objectsBeingDropped.Any(o => o.IsSceneObject()))
                return DragAndDropVisualMode.Rejected;

            var validTrackTypes = objectsBeingDropped.SelectMany(o => TypeUtility.GetTrackTypesForObject(o)).Distinct().ToList();
            // special case for playable assets
            if (objectsBeingDropped.Any(o => TypeUtility.IsConcretePlayableAsset(o.GetType())))
            {
                var playableAssets = objectsBeingDropped.OfType<IPlayableAsset>().Where(o => TypeUtility.IsConcretePlayableAsset(o.GetType()));
                return HandleClipPanePlayableAssetDragAndDrop(playableAssets, targetTrack, perform, timeline, parent, director, candidateTime, typeResolver);
            }

            var markerTypes = objectsBeingDropped.SelectMany(o => TypeUtility.MarkerTypesWithFieldForObject(o)).Distinct();

            // Markers support all tracks
            if (!markerTypes.Any())
            {
                // No tracks support this object
                if (!validTrackTypes.Any())
                    return DragAndDropVisualMode.Rejected;

                // no tracks for this object
                if (targetTrack != null && !validTrackTypes.Contains(targetTrack.GetType()))
                    return DragAndDropVisualMode.Rejected;
            }

            // there is no target track, dropping to empty space, or onto a group
            if (perform)
            {
                // choose track and then clip
                if (targetTrack == null)
                {
                    var createdTrack = HandleTrackAndItemCreation(objectsBeingDropped, candidateTime, typeResolver, timeline, parent, validTrackTypes, insertBefore);
                    if (!createdTrack)
                    {
                        timeline.CreateMarkerTrack();
                        HandleItemCreation(objectsBeingDropped, timeline.markerTrack, candidateTime, typeResolver, true); // menu is always popped if ambiguous choice
                    }
                }
                // just choose clip/marker
                else
                {
                    HandleItemCreation(objectsBeingDropped, targetTrack, candidateTime, typeResolver, true); // menu is always popped if ambiguous choice
                }
            }

            return DragAndDropVisualMode.Copy;
        }

        static bool HandleTrackAndItemCreation(IEnumerable<UnityEngine.Object> objectsBeingDropped, double candidateTime, TypeResolver typeResolver, TimelineAsset timeline, TrackAsset parent, IEnumerable<Type> validTrackTypes, TrackAsset insertBefore = null)
        {
            Action<Type> onResolved = t =>
            {
                var newTrack = TimelineHelpers.CreateTrack(timeline, t, parent, string.Empty);
                if (insertBefore != null)
                {
                    if (parent != null)
                        parent.MoveLastTrackBefore(insertBefore);
                    else
                        timeline.MoveLastTrackBefore(insertBefore);
                }
                HandleItemCreation(objectsBeingDropped, newTrack, candidateTime, typeResolver, validTrackTypes.Count() == 1); // menu is popped if ambiguous clip choice and unambiguous track choice
            };
            return typeResolver(validTrackTypes, t => onResolved(t), k_SelectTrackWithClip); // Did it create a track
        }

        static void HandleItemCreation(IEnumerable<UnityEngine.Object> objectsBeingDropped, TrackAsset targetTrack, double candidateTime, TypeResolver typeResolver, bool allowMenu)
        {
            var assetTypes = objectsBeingDropped.Select(o =>
                TypeUtility.GetAssetTypesForObject(targetTrack.GetType(), o)
                    .Union(TypeUtility.MarkerTypesWithFieldForObject(o))).ToList();
            Action<Type> onCreateItem = assetType =>
            {
                if (typeof(PlayableAsset).IsAssignableFrom(assetType))
                {
                    TimelineHelpers.CreateClipsFromObjects(assetType, targetTrack, candidateTime,
                        objectsBeingDropped);
                }
                else
                {
                    TimelineHelpers.CreateMarkersFromObjects(assetType, targetTrack, candidateTime, objectsBeingDropped);
                }
            };

            var flatAssetTypes = assetTypes.SelectMany(x => x).Distinct();
            // If there is a one to one mapping between assets and timeline types, no need to go through the type resolution, not ambiguous.
            if (assetTypes.All(x => x.Count() <= 1))
            {
                foreach (var type in flatAssetTypes)
                {
                    onCreateItem(type);
                }
            }
            else
            {
                if (!allowMenu) // If we already popped a menu, and are presented with an ambiguous choice, take the first entry
                {
                    flatAssetTypes = new[] {flatAssetTypes.First()};
                }

                typeResolver(flatAssetTypes, onCreateItem, k_SelectClip);
            }
        }

        /// Handles drag and drop of a mono script.
        public static DragAndDropVisualMode HandleClipPaneMonoScriptDragAndDrop(IEnumerable<MonoScript> scriptsBeingDropped, TrackAsset targetTrack, bool perform, TimelineAsset timeline, TrackAsset parent, PlayableDirector director, double candidateTime)
        {
            var playableAssetTypes = scriptsBeingDropped.Select(s => s.GetClass()).Where(TypeUtility.IsConcretePlayableAsset).Distinct();
            if (!playableAssetTypes.Any())
                return DragAndDropVisualMode.Rejected;

            var targetTrackType = typeof(PlayableTrack);
            if (targetTrack != null)
                targetTrackType = targetTrack.GetType();

            var trackAssetsTypes = TypeUtility.GetPlayableAssetsHandledByTrack(targetTrackType);
            var supportedTypes = trackAssetsTypes.Intersect(playableAssetTypes);
            if (!supportedTypes.Any())
                return DragAndDropVisualMode.Rejected;

            if (perform)
            {
                if (targetTrack == null)
                    targetTrack = TimelineHelpers.CreateTrack(timeline, targetTrackType, parent, string.Empty);
                TimelineHelpers.CreateClipsFromTypes(supportedTypes, targetTrack, candidateTime);
            }

            return DragAndDropVisualMode.Copy;
        }

        public static DragAndDropVisualMode HandleClipPanePlayableAssetDragAndDrop(IEnumerable<IPlayableAsset> assetsBeingDropped, TrackAsset targetTrack, bool perform, TimelineAsset timeline, TrackAsset parent, PlayableDirector director, double candidateTime, TypeResolver typeResolver)
        {
            // get the list of supported track types
            var assetTypes = assetsBeingDropped.Select(x => x.GetType()).Distinct();
            IEnumerable<Type> supportedTypes = null;
            if (targetTrack == null)
            {
                supportedTypes = TypeUtility.AllTrackTypes().Where(t => TypeUtility.GetPlayableAssetsHandledByTrack(t).Intersect(assetTypes).Any()).ToList();
            }
            else
            {
                supportedTypes = Enumerable.Empty<Type>();
                var trackAssetTypes = TypeUtility.GetPlayableAssetsHandledByTrack(targetTrack.GetType());
                if (trackAssetTypes.Intersect(assetTypes).Any())
                    supportedTypes = new[] {targetTrack.GetType()};
            }

            if (!supportedTypes.Any())
                return DragAndDropVisualMode.Rejected;

            if (perform)
            {
                Action<Type> onResolved = (t) =>
                {
                    if (targetTrack == null)
                        targetTrack = TimelineHelpers.CreateTrack(timeline, t, parent, string.Empty);

                    var clipTypes = TypeUtility.GetPlayableAssetsHandledByTrack(targetTrack.GetType());
                    foreach (var asset in assetsBeingDropped)
                    {
                        if (clipTypes.Contains(asset.GetType()))
                            TimelineHelpers.CreateClipOnTrackFromPlayableAsset(asset, targetTrack, candidateTime);
                    }
                };

                typeResolver(supportedTypes, onResolved, k_SelectTrackWithClip);
            }


            return DragAndDropVisualMode.Copy;
        }

        static bool ValidateObjectDrop(UnityObject obj)
        {
            // legacy animation clips are not supported at all
            AnimationClip clip = obj as AnimationClip;
            if (clip != null && clip.legacy)
                return false;

            return !(obj is TimelineAsset);
        }

        public DragAndDropVisualMode HandleTrackDrop(TreeViewItem parentItem, TreeViewItem targetItem, bool perform, DropPosition dropPos)
        {
            ((TimelineTreeView)m_Window.treeView.gui).showInsertionMarker = false;
            var trackDragData = (TimelineDragData)DragAndDrop.GetGenericData(k_GenericDragId);
            bool validDrag = ValidDrag(targetItem, trackDragData.draggedItems);
            if (!validDrag)
                return DragAndDropVisualMode.None;


            var draggedTracks = trackDragData.draggedItems.OfType<TimelineGroupGUI>().Select(x => x.track).ToList();
            if (draggedTracks.Count == 0)
                return DragAndDropVisualMode.None;

            if (parentItem != null)
            {
                var parentActor = parentItem as TimelineGroupGUI;
                if (parentActor != null && parentActor.track != null)
                {
                    if (parentActor.track.lockedInHierarchy)
                        return DragAndDropVisualMode.Rejected;

                    if (draggedTracks.Any(x => !TimelineCreateUtilities.ValidateParentTrack(parentActor.track, x.GetType())))
                        return DragAndDropVisualMode.Rejected;
                }
            }

            var insertAfterItem = targetItem as TimelineGroupGUI;
            if (insertAfterItem != null && insertAfterItem.track != null)
            {
                ((TimelineTreeView)m_Window.treeView.gui).showInsertionMarker = true;
            }

            if (dropPos == DropPosition.Upon)
            {
                var groupGUI = targetItem as TimelineGroupGUI;
                if (groupGUI != null)
                    groupGUI.isDropTarget = true;
            }

            if (perform)
            {
                PlayableAsset targetParent = m_Timeline;
                var parentActor = parentItem as TimelineGroupGUI;

                if (parentActor != null && parentActor.track != null)
                    targetParent = parentActor.track;

                TrackAsset siblingTrack = insertAfterItem != null ? insertAfterItem.track : null;

                // where the user drops after the last track, make sure to place it after all the tracks
                if (targetParent == m_Timeline && dropPos == DropPosition.Below && siblingTrack == null)
                {
                    siblingTrack = m_Timeline.GetRootTracks().LastOrDefault(x => !draggedTracks.Contains(x));
                }

                if (TrackExtensions.ReparentTracks(TrackExtensions.FilterTracks(draggedTracks).ToList(), targetParent, siblingTrack, dropPos == DropPosition.Above))
                {
                    m_Window.state.Refresh();
                }
            }

            return DragAndDropVisualMode.Move;
        }

        public static void HandleBindingDragAndDrop(TrackAsset dropTarget, Type requiredBindingType)
        {
            var objectBeingDragged = DragAndDrop.objectReferences[0];

            var action = BindingUtility.GetBindingAction(requiredBindingType, objectBeingDragged);
            DragAndDrop.visualMode = action == BindingAction.DoNotBind
                ? DragAndDropVisualMode.Rejected
                : DragAndDropVisualMode.Link;

            if (action == BindingAction.DoNotBind || Event.current.type != EventType.DragPerform)
                return;

            var director = TimelineEditor.inspectedDirector;

            switch (action)
            {
                case BindingAction.BindDirectly:
                {
                    BindingUtility.Bind(director, dropTarget, objectBeingDragged);
                    break;
                }
                case BindingAction.BindToExistingComponent:
                {
                    var gameObjectBeingDragged = objectBeingDragged as GameObject;
                    Debug.Assert(gameObjectBeingDragged != null, "The object being dragged was detected as being a GameObject");

                    BindingUtility.Bind(director, dropTarget, gameObjectBeingDragged.GetComponent(requiredBindingType));
                    break;
                }
                case BindingAction.BindToMissingComponent:
                {
                    var gameObjectBeingDragged = objectBeingDragged as GameObject;
                    Debug.Assert(gameObjectBeingDragged != null, "The object being dragged was detected as being a GameObject");

                    var typeNameOfComponent = requiredBindingType.ToString().Split(".".ToCharArray()).Last();
                    var bindMenu = new GenericMenu();

                    bindMenu.AddItem(
                        EditorGUIUtility.TextContent("Create " + typeNameOfComponent + " on " + gameObjectBeingDragged.name),
                        false,
                        nullParam => BindingUtility.Bind(director, dropTarget, Undo.AddComponent(gameObjectBeingDragged, requiredBindingType)),
                        null);

                    bindMenu.AddSeparator("");
                    bindMenu.AddItem(EditorGUIUtility.TrTextContent("Cancel"), false, userData => {}, null);
                    bindMenu.ShowAsContext();

                    break;
                }
                default:
                {
                    //no-op
                    return;
                }
            }

            DragAndDrop.AcceptDrag();
        }

        static bool ValidDrag(TreeViewItem target, List<TreeViewItem> draggedItems)
        {
            TreeViewItem currentParent = target;
            while (currentParent != null)
            {
                if (draggedItems.Contains(currentParent))
                    return false;
                currentParent = currentParent.parent;
            }

            // dragging into the sequence itself
            return true;
        }
    }
}