Example #1
0
        void HandleDragPerformEvent(Editor[] editors, Event evt, ref int targetIndex)
        {
            if (targetIndex != -1)
            {
                var draggingMode = DragAndDrop.GetGenericData(k_DraggingModeKey) as DraggingMode ? ;
                if (!draggingMode.HasValue || draggingMode.Value == DraggingMode.NotApplicable)
                {
                    targetIndex = -1;
                    return;
                }

                if (!editors[targetIndex].targets.All(t => t is Component))
                {
                    return;
                }

                var targetComponents = editors[targetIndex].targets.Cast <Component>().ToArray();

                if (draggingMode.Value == DraggingMode.Script)
                {
                    var scripts = DragAndDrop.objectReferences.Cast <MonoScript>();

                    // Ensure all script components can be added
                    var valid = true;
                    foreach (var targetComponent in targetComponents)
                    {
                        var gameObject = targetComponent.gameObject;
                        if (scripts.Any(s => !ComponentUtility.WarnCanAddScriptComponent(gameObject, s)))
                        {
                            valid = false;
                            break;
                        }
                    }

                    if (valid)
                    {
                        Undo.IncrementCurrentGroup();
                        var undoGroup = Undo.GetCurrentGroup();

                        // Add script components
                        var index           = 0;
                        var addedComponents = new Component[targetComponents.Length * scripts.Count()];
                        for (int i = 0; i < targetComponents.Length; i++)
                        {
                            var  targetComponent   = targetComponents[i];
                            var  gameObject        = targetComponent.gameObject;
                            bool targetIsTransform = targetComponent is Transform;
                            foreach (var script in scripts)
                            {
                                addedComponents[index++] = ObjectFactory.AddComponent(gameObject, script.GetClass());
                            }

                            // If the target is a Transform, the AddComponent might have replaced it with a RectTransform.
                            // Handle this possibility by updating the target component.
                            if (targetIsTransform)
                            {
                                targetComponents[i] = gameObject.transform;
                            }
                        }

                        // Move added components relative to target components
                        if (!ComponentUtility.MoveComponentsRelativeToComponents(addedComponents, targetComponents, m_TargetAbove))
                        {
                            // Ensure we have the same selection after calling RevertAllDownToGroup below (MoveComponentsRelativeToComponents can have opened a Prefab in Prefab Mode and changed selection to that root)
                            var wantedSelectedGameObject = Selection.activeGameObject;

                            // Revert added components if move operation fails (e.g. user has been shown the dialog with 'prefab instance restructuring is not posssible' or object is not editable)
                            Undo.RevertAllDownToGroup(undoGroup);

                            if (wantedSelectedGameObject != Selection.activeGameObject)
                            {
                                Selection.activeGameObject = wantedSelectedGameObject;
                            }
                        }
                    }
                }
                else
                {
                    // Handle dragging components
                    var sourceComponents = DragAndDrop.objectReferences.Cast <Component>().ToArray();
                    if (sourceComponents.Length == 0 || targetComponents.Length == 0)
                    {
                        return;
                    }

                    MoveOrCopyComponents(sourceComponents, targetComponents, EditorUtility.EventHasDragCopyModifierPressed(evt), false);
                }

                targetIndex = -1;
                DragAndDrop.AcceptDrag();
                evt.Use();
                EditorGUIUtility.ExitGUI();
            }
        }
Example #2
0
        void HandleEditorDragging(Editor[] editors, int editorIndex, Rect targetRect, float markerY, bool bottomTarget)
        {
            var evt = Event.current;

            switch (evt.type)
            {
            case EventType.DragUpdated:
                if (targetRect.Contains(evt.mousePosition))
                {
                    var draggingMode = DragAndDrop.GetGenericData(k_DraggingModeKey) as DraggingMode ? ;
                    if (!draggingMode.HasValue)
                    {
                        var draggedObjects = DragAndDrop.objectReferences;

                        if (draggedObjects.Length == 0)
                        {
                            draggingMode = DraggingMode.NotApplicable;
                        }
                        else if (draggedObjects.All(o => o is Component && !(o is Transform)))
                        {
                            draggingMode = DraggingMode.Component;
                        }
                        else if (draggedObjects.All(o => o is MonoScript))
                        {
                            draggingMode = DraggingMode.Script;
                        }
                        else
                        {
                            draggingMode = DraggingMode.NotApplicable;
                        }

                        DragAndDrop.SetGenericData(k_DraggingModeKey, draggingMode);
                    }

                    if (draggingMode.Value != DraggingMode.NotApplicable)
                    {
                        if (bottomTarget)
                        {
                            m_TargetAbove = false;
                            m_TargetIndex = m_LastIndex;
                        }
                        else
                        {
                            m_TargetAbove = evt.mousePosition.y < targetRect.y + targetRect.height / 2f;
                            m_TargetIndex = editorIndex;

                            if (m_TargetAbove)
                            {
                                m_TargetIndex++;
                                while (m_TargetIndex < editors.Length && m_InspectorWindow.ShouldCullEditor(editors, m_TargetIndex))
                                {
                                    m_TargetIndex++;
                                }

                                if (m_TargetIndex == editors.Length)
                                {
                                    m_TargetIndex = -1;
                                    return;
                                }
                            }
                        }

                        if (m_TargetAbove && m_InspectorWindow.EditorHasLargeHeader(m_TargetIndex, editors))
                        {
                            m_TargetIndex--;
                            while (m_TargetIndex >= 0 && m_InspectorWindow.ShouldCullEditor(editors, m_TargetIndex))
                            {
                                m_TargetIndex--;
                            }

                            if (m_TargetIndex == -1)
                            {
                                return;
                            }

                            m_TargetAbove = false;
                        }

                        if (draggingMode.Value == DraggingMode.Script)
                        {
                            // Validate dragging scripts
                            // Always allow script dragging, instead fail during DragPerform with a dialog box
                            DragAndDrop.visualMode = DragAndDropVisualMode.Link;
                        }
                        else
                        {
                            // Validate dragging components
                            var valid = false;
                            if (editors[m_TargetIndex].targets.All(t => t is Component))
                            {
                                var targetComponents = editors[m_TargetIndex].targets.Cast <Component>().ToArray();
                                var sourceComponents = DragAndDrop.objectReferences.Cast <Component>().ToArray();
                                valid = MoveOrCopyComponents(sourceComponents, targetComponents, EditorUtility.EventHasDragCopyModifierPressed(evt), true);
                            }

                            if (valid)
                            {
                                DragAndDrop.visualMode = EditorUtility.EventHasDragCopyModifierPressed(evt) ? DragAndDropVisualMode.Copy : DragAndDropVisualMode.Move;
                            }
                            else
                            {
                                DragAndDrop.visualMode = DragAndDropVisualMode.None;
                                m_TargetIndex          = -1;
                                return;
                            }
                        }

                        evt.Use();
                    }
                }
                else
                {
                    m_TargetIndex = -1;
                }

                break;

            case EventType.DragPerform:
                if (m_TargetIndex != -1)
                {
                    var draggingMode = DragAndDrop.GetGenericData(k_DraggingModeKey) as DraggingMode ? ;
                    if (!draggingMode.HasValue || draggingMode.Value == DraggingMode.NotApplicable)
                    {
                        m_TargetIndex = -1;
                        return;
                    }

                    if (!editors[m_TargetIndex].targets.All(t => t is Component))
                    {
                        return;
                    }

                    var targetComponents = editors[m_TargetIndex].targets.Cast <Component>().ToArray();

                    if (draggingMode.Value == DraggingMode.Script)
                    {
                        var scripts = DragAndDrop.objectReferences.Cast <MonoScript>();

                        // Ensure all script components can be added
                        var valid = true;
                        foreach (var targetComponent in targetComponents)
                        {
                            var gameObject = targetComponent.gameObject;
                            if (scripts.Any(s => !ComponentUtility.WarnCanAddScriptComponent(targetComponent.gameObject, s)))
                            {
                                valid = false;
                                break;
                            }
                        }

                        if (valid)
                        {
                            Undo.IncrementCurrentGroup();
                            var undoGroup = Undo.GetCurrentGroup();

                            // Add script components
                            var index           = 0;
                            var addedComponents = new Component[targetComponents.Length * scripts.Count()];
                            for (int i = 0; i < targetComponents.Length; i++)
                            {
                                var  targetComponent   = targetComponents[i];
                                var  gameObject        = targetComponent.gameObject;
                                bool targetIsTransform = targetComponent is Transform;
                                foreach (var script in scripts)
                                {
                                    addedComponents[index++] = ObjectFactory.AddComponent(gameObject, script.GetClass());
                                }

                                // If the target is a Transform, the AddComponent might have replaced it with a RectTransform.
                                // Handle this possibility by updating the target component.
                                if (targetIsTransform)
                                {
                                    targetComponents[i] = gameObject.transform;
                                }
                            }

                            // Move added components relative to target components
                            if (!ComponentUtility.MoveComponentsRelativeToComponents(addedComponents, targetComponents, m_TargetAbove))
                            {
                                // Revert added components if move operation fails (e.g. user aborts when asked to break prefab instance)
                                Undo.RevertAllDownToGroup(undoGroup);
                            }
                        }
                    }
                    else
                    {
                        // Handle dragging components
                        var sourceComponents = DragAndDrop.objectReferences.Cast <Component>().ToArray();
                        if (sourceComponents.Length == 0 || targetComponents.Length == 0)
                        {
                            return;
                        }

                        MoveOrCopyComponents(sourceComponents, targetComponents, EditorUtility.EventHasDragCopyModifierPressed(evt), false);
                    }

                    m_TargetIndex = -1;
                    DragAndDrop.AcceptDrag();
                    evt.Use();
                    EditorGUIUtility.ExitGUI();
                }
                break;

            case EventType.DragExited:
                m_TargetIndex = -1;
                break;

            case EventType.Repaint:
                if (m_TargetIndex != -1 && targetRect.Contains(evt.mousePosition))
                {
                    Styles.insertionMarker.Draw(GetMarkerRect(targetRect, markerY, m_TargetAbove), false, false, false, false);
                }
                break;
            }
        }
Example #3
0
        private void HandleEditorDragging(int editorIndex, Rect targetRect, float markerY, bool bottomTarget, ActiveEditorTracker tracker)
        {
            Event     current = Event.current;
            EventType type    = current.type;

            switch (type)
            {
            case EventType.Repaint:
                if (this.m_TargetIndex != -1 && targetRect.Contains(current.mousePosition))
                {
                    Rect position = new Rect(targetRect.x, markerY, targetRect.width, 3f);
                    if (!this.m_TargetAbove)
                    {
                        position.y += 2f;
                    }
                    EditorDragging.Styles.insertionMarker.Draw(position, false, false, false, false);
                }
                return;

            case EventType.Layout:
IL_26:
                if (type != EventType.DragExited)
                {
                    return;
                }
                this.m_TargetIndex = -1;
                return;

            case EventType.DragUpdated:
                if (targetRect.Contains(current.mousePosition))
                {
                    EditorDragging.DraggingMode?draggingMode = DragAndDrop.GetGenericData("InspectorEditorDraggingMode") as EditorDragging.DraggingMode?;
                    if (!draggingMode.HasValue)
                    {
                        UnityEngine.Object[] objectReferences = DragAndDrop.objectReferences;
                        if (objectReferences.Length == 0)
                        {
                            draggingMode = new EditorDragging.DraggingMode?(EditorDragging.DraggingMode.NotApplicable);
                        }
                        else if (objectReferences.All((UnityEngine.Object o) => o is Component && !(o is Transform)))
                        {
                            draggingMode = new EditorDragging.DraggingMode?(EditorDragging.DraggingMode.Component);
                        }
                        else if (objectReferences.All((UnityEngine.Object o) => o is MonoScript))
                        {
                            draggingMode = new EditorDragging.DraggingMode?(EditorDragging.DraggingMode.Script);
                        }
                        else
                        {
                            draggingMode = new EditorDragging.DraggingMode?(EditorDragging.DraggingMode.NotApplicable);
                        }
                        DragAndDrop.SetGenericData("InspectorEditorDraggingMode", draggingMode);
                    }
                    if (draggingMode.Value != EditorDragging.DraggingMode.NotApplicable)
                    {
                        Editor[]             activeEditors     = tracker.activeEditors;
                        UnityEngine.Object[] objectReferences2 = DragAndDrop.objectReferences;
                        if (bottomTarget)
                        {
                            this.m_TargetAbove = false;
                            this.m_TargetIndex = this.m_LastIndex;
                        }
                        else
                        {
                            this.m_TargetAbove = (current.mousePosition.y < targetRect.y + targetRect.height / 2f);
                            this.m_TargetIndex = editorIndex;
                            if (this.m_TargetAbove)
                            {
                                this.m_TargetIndex++;
                                while (this.m_TargetIndex < activeEditors.Length && this.m_InspectorWindow.ShouldCullEditor(activeEditors, this.m_TargetIndex))
                                {
                                    this.m_TargetIndex++;
                                }
                                if (this.m_TargetIndex == activeEditors.Length)
                                {
                                    this.m_TargetIndex = -1;
                                    return;
                                }
                            }
                        }
                        if (this.m_TargetAbove && this.m_InspectorWindow.EditorHasLargeHeader(this.m_TargetIndex, activeEditors))
                        {
                            this.m_TargetIndex--;
                            while (this.m_TargetIndex >= 0 && this.m_InspectorWindow.ShouldCullEditor(activeEditors, this.m_TargetIndex))
                            {
                                this.m_TargetIndex--;
                            }
                            if (this.m_TargetIndex == -1)
                            {
                                return;
                            }
                            this.m_TargetAbove = false;
                        }
                        if (draggingMode.Value == EditorDragging.DraggingMode.Script)
                        {
                            DragAndDrop.visualMode = DragAndDropVisualMode.Link;
                        }
                        else
                        {
                            bool flag = false;
                            if (activeEditors[this.m_TargetIndex].targets.All((UnityEngine.Object t) => t is Component))
                            {
                                Component[] targetComponents = activeEditors[this.m_TargetIndex].targets.Cast <Component>().ToArray <Component>();
                                Component[] sourceComponents = DragAndDrop.objectReferences.Cast <Component>().ToArray <Component>();
                                flag = this.MoveOrCopyComponents(sourceComponents, targetComponents, EditorUtility.EventHasDragCopyModifierPressed(current), true);
                            }
                            if (!flag)
                            {
                                DragAndDrop.visualMode = DragAndDropVisualMode.None;
                                this.m_TargetIndex     = -1;
                                return;
                            }
                            DragAndDrop.visualMode = ((!EditorUtility.EventHasDragCopyModifierPressed(current)) ? DragAndDropVisualMode.Move : DragAndDropVisualMode.Copy);
                        }
                        current.Use();
                    }
                }
                else
                {
                    this.m_TargetIndex = -1;
                }
                return;

            case EventType.DragPerform:
                if (this.m_TargetIndex != -1)
                {
                    EditorDragging.DraggingMode?draggingMode2 = DragAndDrop.GetGenericData("InspectorEditorDraggingMode") as EditorDragging.DraggingMode?;
                    if (!draggingMode2.HasValue || draggingMode2.Value == EditorDragging.DraggingMode.NotApplicable)
                    {
                        this.m_TargetIndex = -1;
                    }
                    else
                    {
                        Editor[] activeEditors2 = tracker.activeEditors;
                        if (activeEditors2[this.m_TargetIndex].targets.All((UnityEngine.Object t) => t is Component))
                        {
                            Component[] array = activeEditors2[this.m_TargetIndex].targets.Cast <Component>().ToArray <Component>();
                            if (draggingMode2.Value == EditorDragging.DraggingMode.Script)
                            {
                                IEnumerable <MonoScript> enumerable = DragAndDrop.objectReferences.Cast <MonoScript>();
                                bool        flag2  = true;
                                Component[] array2 = array;
                                for (int i = 0; i < array2.Length; i++)
                                {
                                    Component  targetComponent = array2[i];
                                    GameObject gameObject      = targetComponent.gameObject;
                                    if (enumerable.Any((MonoScript s) => !ComponentUtility.WarnCanAddScriptComponent(targetComponent.gameObject, s)))
                                    {
                                        flag2 = false;
                                        break;
                                    }
                                }
                                if (flag2)
                                {
                                    Undo.IncrementCurrentGroup();
                                    int         currentGroup = Undo.GetCurrentGroup();
                                    int         num          = 0;
                                    Component[] array3       = new Component[array.Length * enumerable.Count <MonoScript>()];
                                    Component[] array4       = array;
                                    for (int j = 0; j < array4.Length; j++)
                                    {
                                        Component  component   = array4[j];
                                        GameObject gameObject2 = component.gameObject;
                                        foreach (MonoScript current2 in enumerable)
                                        {
                                            array3[num++] = Undo.AddComponent(gameObject2, current2.GetClass());
                                        }
                                    }
                                    if (!ComponentUtility.MoveComponentsRelativeToComponents(array3, array, this.m_TargetAbove))
                                    {
                                        Undo.RevertAllDownToGroup(currentGroup);
                                    }
                                }
                            }
                            else
                            {
                                Component[] array5 = DragAndDrop.objectReferences.Cast <Component>().ToArray <Component>();
                                if (array5.Length == 0 || array.Length == 0)
                                {
                                    return;
                                }
                                this.MoveOrCopyComponents(array5, array, EditorUtility.EventHasDragCopyModifierPressed(current), false);
                            }
                            this.m_TargetIndex = -1;
                            DragAndDrop.AcceptDrag();
                            current.Use();
                            GUIUtility.ExitGUI();
                        }
                    }
                }
                return;
            }
            goto IL_26;
        }