AcceptDrag() private method

private AcceptDrag ( ) : void
return void
Ejemplo n.º 1
0
        public void OnSceneDrag(SceneView sceneView)
        {
            GameObject go = target as GameObject;

            if (!PrefabUtility.IsPartOfPrefabAsset(go))
            {
                return;
            }

            var prefabAssetRoot = go.transform.root.gameObject;

            Event evt = Event.current;

            switch (evt.type)
            {
            case EventType.DragUpdated:

                Scene destinationScene = sceneView.customScene.IsValid() ? sceneView.customScene : SceneManager.GetActiveScene();
                if (dragObject == null)
                {
                    if (!EditorApplication.isPlaying || EditorSceneManager.IsPreviewScene(destinationScene))
                    {
                        dragObject      = (GameObject)PrefabUtility.InstantiatePrefab(prefabAssetRoot, destinationScene);
                        dragObject.name = go.name;
                    }
                    else
                    {
                        // Instatiate as regular GameObject in Play Mode so runtime logic
                        // won't run into restrictions on restructuring Prefab instances.
                        dragObject = Instantiate(prefabAssetRoot);
                        SceneManager.MoveGameObjectToScene(dragObject, destinationScene);
                    }
                    dragObject.hideFlags = HideFlags.HideInHierarchy;
                }

                if (HandleUtility.ignoreRaySnapObjects == null)
                {
                    HandleUtility.ignoreRaySnapObjects = dragObject.GetComponentsInChildren <Transform>();
                }

                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                object hit = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(evt.mousePosition));

                if (hit != null)
                {
                    RaycastHit rh     = (RaycastHit)hit;
                    float      offset = 0;
                    if (Tools.pivotMode == PivotMode.Center)
                    {
                        float geomOffset = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, rh.normal);
                        if (geomOffset != Mathf.Infinity)
                        {
                            offset = Vector3.Dot(dragObject.transform.position, rh.normal) - geomOffset;
                        }
                    }
                    dragObject.transform.position = Matrix4x4.identity.MultiplyPoint(rh.point + (rh.normal * offset));
                }
                else
                {
                    dragObject.transform.position = HandleUtility.GUIPointToWorldRay(evt.mousePosition).GetPoint(10);
                }

                // Use prefabs original z position when in 2D mode
                if (sceneView.in2DMode)
                {
                    Vector3 dragPosition = dragObject.transform.position;
                    dragPosition.z = prefabAssetRoot.transform.position.z;
                    dragObject.transform.position = dragPosition;
                }

                evt.Use();
                break;

            case EventType.DragPerform:

                var stage = StageNavigationManager.instance.currentStage;
                if (stage is PrefabStage)
                {
                    var prefabAssetThatIsAddedTo = AssetDatabase.LoadMainAssetAtPath(stage.assetPath);
                    if (PrefabUtility.CheckIfAddingPrefabWouldResultInCyclicNesting(prefabAssetThatIsAddedTo, go))
                    {
                        PrefabUtility.ShowCyclicNestingWarningDialog();
                        return;
                    }
                }

                Transform parent = sceneView.customParentForDraggedObjects;

                string uniqueName = GameObjectUtility.GetUniqueNameForSibling(parent, dragObject.name);
                if (parent != null)
                {
                    dragObject.transform.parent = parent;
                }
                dragObject.hideFlags = 0;
                Undo.RegisterCreatedObjectUndo(dragObject, "Place " + dragObject.name);
                EditorUtility.SetDirty(dragObject);
                DragAndDrop.AcceptDrag();
                Selection.activeObject             = dragObject;
                HandleUtility.ignoreRaySnapObjects = null;
                if (SceneView.mouseOverWindow != null)
                {
                    SceneView.mouseOverWindow.Focus();
                }
                if (!Application.IsPlaying(dragObject))
                {
                    dragObject.name = uniqueName;
                }
                dragObject = null;
                evt.Use();
                break;

            case EventType.DragExited:
                if (dragObject)
                {
                    UnityObject.DestroyImmediate(dragObject, false);
                    HandleUtility.ignoreRaySnapObjects = null;
                    dragObject = null;
                    evt.Use();
                }
                break;
            }
        }
Ejemplo n.º 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))
                {
                    var markerRect = new Rect(targetRect.x, markerY, targetRect.width, 3f);
                    if (!m_TargetAbove)
                    {
                        markerRect.y += 2f;
                    }

                    Styles.insertionMarker.Draw(markerRect, false, false, false, false);
                }
                break;
            }
        }
Ejemplo n.º 3
0
        public void OnSceneDrag(SceneView sceneView)
        {
            GameObject gameObject = this.target as GameObject;
            PrefabType prefabType = PrefabUtility.GetPrefabType(gameObject);

            if (prefabType != PrefabType.Prefab && prefabType != PrefabType.ModelPrefab)
            {
                return;
            }
            Event     current = Event.current;
            EventType type    = current.type;

            if (type != EventType.DragUpdated)
            {
                if (type != EventType.DragPerform)
                {
                    if (type == EventType.DragExited)
                    {
                        if (GameObjectInspector.dragObject)
                        {
                            UnityEngine.Object.DestroyImmediate(GameObjectInspector.dragObject, false);
                            HandleUtility.ignoreRaySnapObjects = null;
                            GameObjectInspector.dragObject     = null;
                            current.Use();
                        }
                    }
                }
                else
                {
                    string uniqueNameForSibling = GameObjectUtility.GetUniqueNameForSibling(null, GameObjectInspector.dragObject.name);
                    GameObjectInspector.dragObject.hideFlags = HideFlags.None;
                    Undo.RegisterCreatedObjectUndo(GameObjectInspector.dragObject, "Place " + GameObjectInspector.dragObject.name);
                    EditorUtility.SetDirty(GameObjectInspector.dragObject);
                    DragAndDrop.AcceptDrag();
                    Selection.activeObject             = GameObjectInspector.dragObject;
                    HandleUtility.ignoreRaySnapObjects = null;
                    EditorWindow.mouseOverWindow.Focus();
                    GameObjectInspector.dragObject.name = uniqueNameForSibling;
                    GameObjectInspector.dragObject      = null;
                    current.Use();
                }
            }
            else
            {
                if (GameObjectInspector.dragObject == null)
                {
                    GameObjectInspector.dragObject           = (GameObject)PrefabUtility.InstantiatePrefab(PrefabUtility.FindPrefabRoot(gameObject));
                    HandleUtility.ignoreRaySnapObjects       = GameObjectInspector.dragObject.GetComponentsInChildren <Transform>();
                    GameObjectInspector.dragObject.hideFlags = HideFlags.HideInHierarchy;
                    GameObjectInspector.dragObject.name      = gameObject.name;
                }
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                object obj = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(current.mousePosition));
                if (obj != null)
                {
                    RaycastHit raycastHit = (RaycastHit)obj;
                    float      d          = 0f;
                    if (Tools.pivotMode == PivotMode.Center)
                    {
                        float num = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, raycastHit.normal);
                        if (num != float.PositiveInfinity)
                        {
                            d = Vector3.Dot(GameObjectInspector.dragObject.transform.position, raycastHit.normal) - num;
                        }
                    }
                    GameObjectInspector.dragObject.transform.position = Matrix4x4.identity.MultiplyPoint(raycastHit.point + raycastHit.normal * d);
                }
                else
                {
                    GameObjectInspector.dragObject.transform.position = HandleUtility.GUIPointToWorldRay(current.mousePosition).GetPoint(10f);
                }
                if (sceneView.in2DMode)
                {
                    Vector3 position = GameObjectInspector.dragObject.transform.position;
                    position.z = PrefabUtility.FindPrefabRoot(gameObject).transform.position.z;
                    GameObjectInspector.dragObject.transform.position = position;
                }
                current.Use();
            }
        }
Ejemplo n.º 4
0
            public bool MoveNext()
            {
                if (this.xPos > -1)
                {
                    if (ListViewShared.HasMouseDown(this.ilvState, this.rect))
                    {
                        this.ilvState.state.selectionChanged = true;
                        this.ilvState.state.row       = this.yPos;
                        this.ilvState.state.column    = this.xPos;
                        this.ilvState.state.scrollPos = ListViewShared.ListViewScrollToRow(this.ilvState, this.yPos);
                        if ((this.ilvState.wantsReordering || this.ilvState.wantsToStartCustomDrag) && (GUIUtility.hotControl == this.ilvState.state.ID))
                        {
                            DragAndDropDelay stateObject = (DragAndDropDelay)GUIUtility.GetStateObject(typeof(DragAndDropDelay), this.ilvState.state.ID);
                            stateObject.mouseDownPosition = Event.current.mousePosition;
                            this.ilvState.dragItem        = this.yPos;
                            ListViewShared.dragControlID  = this.ilvState.state.ID;
                        }
                    }
                    if (((this.ilvState.wantsReordering || this.ilvState.wantsToStartCustomDrag) && ((GUIUtility.hotControl == this.ilvState.state.ID) && (Event.current.type == EventType.MouseDrag))) && GUIClip.visibleRect.Contains(Event.current.mousePosition))
                    {
                        DragAndDropDelay delay2 = (DragAndDropDelay)GUIUtility.GetStateObject(typeof(DragAndDropDelay), this.ilvState.state.ID);
                        if (delay2.CanStartDrag())
                        {
                            DragAndDrop.PrepareStartDrag();
                            DragAndDrop.objectReferences = new Object[0];
                            DragAndDrop.paths            = null;
                            if (this.ilvState.wantsReordering)
                            {
                                this.ilvState.state.dropHereRect = new Rect(this.ilvState.rect.x, 0f, this.ilvState.rect.width, (float)(this.ilvState.state.rowHeight * 2));
                                DragAndDrop.StartDrag(this.dragTitle);
                            }
                            else if (this.ilvState.wantsToStartCustomDrag)
                            {
                                DragAndDrop.SetGenericData("CustomDragID", this.ilvState.state.ID);
                                DragAndDrop.StartDrag(this.dragTitle);
                            }
                        }
                        Event.current.Use();
                    }
                }
                this.xPos++;
                if (this.xPos > this.xTo)
                {
                    this.xPos = 0;
                    this.yPos++;
                    this.rect.x     = this.firstRect.x;
                    this.rect.width = this.colWidths[0];
                    if (this.yPos > this.yTo)
                    {
                        this.quiting = true;
                    }
                    else
                    {
                        this.rect.y += this.rect.height;
                    }
                }
                else
                {
                    if (this.xPos >= 1)
                    {
                        this.rect.x += this.colWidths[this.xPos - 1];
                    }
                    this.rect.width = this.colWidths[this.xPos];
                }
                this.element.row      = this.yPos;
                this.element.column   = this.xPos;
                this.element.position = this.rect;
                if (this.element.row >= this.ilvState.state.totalRows)
                {
                    this.quiting = true;
                }
                if ((this.isLayouted && (Event.current.type == EventType.Layout)) && ((this.yFrom + 1) == this.yPos))
                {
                    this.quiting = true;
                }
                if (this.isLayouted && (this.yPos != this.yFrom))
                {
                    GUILayout.EndHorizontal();
                }
                if (!this.quiting)
                {
                    if (this.isLayouted)
                    {
                        if (this.yPos != this.yFrom)
                        {
                            this.ilvStateL.group.ResetCursor();
                            this.ilvStateL.group.AddY();
                        }
                        else
                        {
                            this.ilvStateL.group.AddY((float)(this.ilvState.invisibleRows * this.ilvState.state.rowHeight));
                        }
                    }
                }
                else
                {
                    if (this.ilvState.state.drawDropHere && (Event.current.GetTypeForControl(this.ilvState.state.ID) == EventType.Repaint))
                    {
                        GUIStyle insertion = ListViewShared.Constants.insertion;
                        insertion.Draw(insertion.margin.Remove(this.ilvState.state.dropHereRect), false, false, false, false);
                    }
                    if (ListViewShared.ListViewKeyboard(this.ilvState, this.colWidths.Length))
                    {
                        this.ilvState.state.selectionChanged = true;
                    }
                    if (Event.current.GetTypeForControl(this.ilvState.state.ID) == EventType.MouseUp)
                    {
                        GUIUtility.hotControl = 0;
                    }
                    if (!this.ilvState.wantsReordering || (GUIUtility.hotControl != this.ilvState.state.ID))
                    {
                        if (!this.ilvState.wantsExternalFiles)
                        {
                            if (this.ilvState.wantsToAcceptCustomDrag && (ListViewShared.dragControlID != this.ilvState.state.ID))
                            {
                                switch (Event.current.type)
                                {
                                case EventType.DragUpdated:
                                {
                                    object genericData = DragAndDrop.GetGenericData("CustomDragID");
                                    if (GUIClip.visibleRect.Contains(Event.current.mousePosition) && (genericData != null))
                                    {
                                        DragAndDrop.visualMode = !this.ilvState.rect.Contains(Event.current.mousePosition) ? DragAndDropVisualMode.None : DragAndDropVisualMode.Move;
                                        Event.current.Use();
                                    }
                                    break;
                                }

                                case EventType.DragPerform:
                                {
                                    object obj3 = DragAndDrop.GetGenericData("CustomDragID");
                                    if (GUIClip.visibleRect.Contains(Event.current.mousePosition) && (obj3 != null))
                                    {
                                        this.ilvState.state.customDraggedFromID = (int)obj3;
                                        DragAndDrop.AcceptDrag();
                                        Event.current.Use();
                                    }
                                    GUIUtility.hotControl = 0;
                                    break;
                                }

                                case EventType.DragExited:
                                    GUIUtility.hotControl = 0;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            switch (Event.current.type)
                            {
                            case EventType.DragUpdated:
                                if ((GUIClip.visibleRect.Contains(Event.current.mousePosition) && (DragAndDrop.paths != null)) && (DragAndDrop.paths.Length != 0))
                                {
                                    DragAndDrop.visualMode = !this.ilvState.rect.Contains(Event.current.mousePosition) ? DragAndDropVisualMode.None : DragAndDropVisualMode.Copy;
                                    Event.current.Use();
                                    if (DragAndDrop.visualMode != DragAndDropVisualMode.None)
                                    {
                                        this.ilvState.state.dropHereRect = new Rect(this.ilvState.rect.x, (float)((Mathf.RoundToInt(Event.current.mousePosition.y / ((float)this.ilvState.state.rowHeight)) - 1) * this.ilvState.state.rowHeight), this.ilvState.rect.width, (float)this.ilvState.state.rowHeight);
                                        if (this.ilvState.state.dropHereRect.y >= (this.ilvState.state.rowHeight * this.ilvState.state.totalRows))
                                        {
                                            this.ilvState.state.dropHereRect.y = this.ilvState.state.rowHeight * (this.ilvState.state.totalRows - 1);
                                        }
                                        this.ilvState.state.drawDropHere = true;
                                    }
                                }
                                break;

                            case EventType.DragPerform:
                                if (GUIClip.visibleRect.Contains(Event.current.mousePosition))
                                {
                                    this.ilvState.state.fileNames = DragAndDrop.paths;
                                    DragAndDrop.AcceptDrag();
                                    Event.current.Use();
                                    this.ilvState.wantsExternalFiles = false;
                                    this.ilvState.state.drawDropHere = false;
                                    this.ilvState.state.draggedTo    = Mathf.RoundToInt(Event.current.mousePosition.y / ((float)this.ilvState.state.rowHeight));
                                    if (this.ilvState.state.draggedTo > this.ilvState.state.totalRows)
                                    {
                                        this.ilvState.state.draggedTo = this.ilvState.state.totalRows;
                                    }
                                    this.ilvState.state.row = this.ilvState.state.draggedTo;
                                }
                                GUIUtility.hotControl = 0;
                                break;

                            case EventType.DragExited:
                                this.ilvState.wantsExternalFiles = false;
                                this.ilvState.state.drawDropHere = false;
                                GUIUtility.hotControl            = 0;
                                break;
                            }
                        }
                    }
                    else
                    {
                        ListViewState state = this.ilvState.state;
                        switch (Event.current.type)
                        {
                        case EventType.DragUpdated:
                            DragAndDrop.visualMode = !this.ilvState.rect.Contains(Event.current.mousePosition) ? DragAndDropVisualMode.None : DragAndDropVisualMode.Move;
                            Event.current.Use();
                            if (DragAndDrop.visualMode != DragAndDropVisualMode.None)
                            {
                                state.dropHereRect.y = (Mathf.RoundToInt(Event.current.mousePosition.y / ((float)state.rowHeight)) - 1) * state.rowHeight;
                                if (state.dropHereRect.y >= (state.rowHeight * state.totalRows))
                                {
                                    state.dropHereRect.y = state.rowHeight * (state.totalRows - 1);
                                }
                                state.drawDropHere = true;
                            }
                            break;

                        case EventType.DragPerform:
                            if (GUIClip.visibleRect.Contains(Event.current.mousePosition))
                            {
                                this.ilvState.state.draggedFrom = this.ilvState.dragItem;
                                this.ilvState.state.draggedTo   = Mathf.RoundToInt(Event.current.mousePosition.y / ((float)state.rowHeight));
                                if (this.ilvState.state.draggedTo > this.ilvState.state.totalRows)
                                {
                                    this.ilvState.state.draggedTo = this.ilvState.state.totalRows;
                                }
                                if (this.ilvState.state.draggedTo > this.ilvState.state.draggedFrom)
                                {
                                    this.ilvState.state.row = this.ilvState.state.draggedTo - 1;
                                }
                                else
                                {
                                    this.ilvState.state.row = this.ilvState.state.draggedTo;
                                }
                                this.ilvState.state.selectionChanged = true;
                                DragAndDrop.AcceptDrag();
                                Event.current.Use();
                                this.ilvState.wantsReordering    = false;
                                this.ilvState.state.drawDropHere = false;
                            }
                            GUIUtility.hotControl = 0;
                            break;

                        case EventType.DragExited:
                            this.ilvState.wantsReordering    = false;
                            this.ilvState.state.drawDropHere = false;
                            GUIUtility.hotControl            = 0;
                            break;
                        }
                    }
                    if (this.ilvState.beganHorizontal)
                    {
                        EditorGUILayout.EndScrollView();
                        GUILayout.EndHorizontal();
                        this.ilvState.beganHorizontal = false;
                    }
                    if (this.isLayouted)
                    {
                        GUILayoutUtility.EndLayoutGroup();
                        EditorGUILayout.EndScrollView();
                    }
                    this.ilvState.wantsReordering    = false;
                    this.ilvState.wantsExternalFiles = false;
                }
                if (this.isLayouted)
                {
                    if (!this.quiting)
                    {
                        GUILayout.BeginHorizontal(GUIStyle.none, new GUILayoutOption[0]);
                    }
                    else
                    {
                        GUILayout.EndHorizontal();
                    }
                }
                return(!this.quiting);
            }
Ejemplo n.º 5
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();
            }
        }
        private void HandleMouseInput()
        {
            List <CubemapInfo> hdriList = this.m_LookDevView.envLibrary.hdriList;
            Vector2            pos      = new Vector2(Event.current.mousePosition.x, Event.current.mousePosition.y + this.m_ScrollPosition.y);
            Event     current           = Event.current;
            EventType typeForControl    = current.GetTypeForControl(this.m_LookDevView.hotControl);

            switch (typeForControl)
            {
            case EventType.MouseUp:
                if (this.m_SelectedCubemap != null)
                {
                    Rect gUIRect = this.m_GUIRect;
                    gUIRect.yMax += 16f;
                    if (!gUIRect.Contains(Event.current.mousePosition))
                    {
                        break;
                    }
                    int insertionIndex = this.IsPositionInInsertionArea(pos);
                    if (insertionIndex == -1)
                    {
                        int num2 = this.IsPositionInThumbnailArea(pos);
                        if ((num2 != -1) && this.m_LookDevView.config.enableShadowCubemap)
                        {
                            Undo.RecordObject(this.m_LookDevView.envLibrary, "Update shadow cubemap");
                            CubemapInfo info2 = this.m_LookDevView.envLibrary.hdriList[num2];
                            if (info2 != this.m_SelectedCubemapInfo)
                            {
                                info2.SetCubemapShadowInfo(this.m_SelectedCubemapInfo);
                            }
                            this.m_LookDevView.envLibrary.dirty = true;
                        }
                    }
                    else
                    {
                        this.ResetShadowCubemap();
                        this.m_LookDevView.envLibrary.InsertHDRI(this.m_SelectedCubemap, insertionIndex);
                    }
                    this.CancelSelection();
                }
                break;

            case EventType.MouseDrag:
                if (this.m_SelectedCubeMapOffsetIndex != -1)
                {
                    Undo.RecordObject(this.m_LookDevView.envLibrary, "");
                    CubemapInfo info = hdriList[this.m_SelectedCubeMapOffsetIndex];
                    info.angleOffset = this.ComputeAngleOffsetFromMouseCoord(pos) + this.m_SelectedCubeMapOffsetValue;
                    this.m_LookDevView.envLibrary.dirty = true;
                    Event.current.Use();
                }
                if (this.m_SelectedCubemapInfo != null)
                {
                    if (this.IsPositionInInsertionArea(pos) == -1)
                    {
                        this.m_HoveringCubeMapIndex = this.IsPositionInThumbnailArea(pos);
                    }
                    else
                    {
                        this.m_HoveringCubeMapIndex = -1;
                    }
                }
                this.m_LookDevView.Repaint();
                return;

            case EventType.KeyDown:
                if (Event.current.keyCode == KeyCode.Escape)
                {
                    this.CancelSelection();
                    this.m_LookDevView.Repaint();
                }
                return;

            case EventType.Repaint:
                if (this.m_SelectedCubeMapOffsetIndex != -1)
                {
                    EditorGUIUtility.AddCursorRect(this.m_displayRect, MouseCursor.SlideArrow);
                }
                return;

            case EventType.DragUpdated:
            {
                bool flag = false;
                foreach (UnityEngine.Object obj3 in DragAndDrop.objectReferences)
                {
                    Cubemap cubemap2 = obj3 as Cubemap;
                    if (cubemap2 != null)
                    {
                        flag = true;
                    }
                }
                DragAndDrop.visualMode = !flag ? DragAndDropVisualMode.Rejected : DragAndDropVisualMode.Link;
                if (flag)
                {
                    this.m_DragBeingPerformed = true;
                }
                current.Use();
                return;
            }

            case EventType.DragPerform:
            {
                int num3 = this.IsPositionInInsertionArea(pos);
                foreach (UnityEngine.Object obj2 in DragAndDrop.objectReferences)
                {
                    Cubemap cubemap = obj2 as Cubemap;
                    if (cubemap != null)
                    {
                        this.m_LookDevView.envLibrary.InsertHDRI(cubemap, num3);
                    }
                }
                DragAndDrop.AcceptDrag();
                this.m_DragBeingPerformed = false;
                current.Use();
                return;
            }

            default:
                if (typeForControl == EventType.DragExited)
                {
                }
                return;
            }
            this.m_LookDevView.Repaint();
            if ((this.m_SelectedCubeMapOffsetIndex != -1) && (Mathf.Abs(hdriList[this.m_SelectedCubeMapOffsetIndex].angleOffset) <= 10f))
            {
                Undo.RecordObject(this.m_LookDevView.envLibrary, "");
                hdriList[this.m_SelectedCubeMapOffsetIndex].angleOffset = 0f;
                this.m_LookDevView.envLibrary.dirty = true;
            }
            this.m_SelectedCubemapInfo            = null;
            this.m_SelectedShadowCubemapOwnerInfo = null;
            this.m_SelectedLightIconIndex         = -1;
            this.m_SelectedShadowInfo             = null;
            this.m_SelectedCubeMapOffsetIndex     = -1;
            this.m_HoveringCubeMapIndex           = -1;
            this.m_SelectedCubeMapOffsetValue     = 0f;
            GUIUtility.hotControl = 0;
        }
        public void OnSceneDrag(SceneView sceneView)
        {
            GameObject go         = target as GameObject;
            PrefabType prefabType = PrefabUtility.GetPrefabType(go);

            if (prefabType != PrefabType.Prefab && prefabType != PrefabType.ModelPrefab)
            {
                return;
            }

            Event evt = Event.current;

            switch (evt.type)
            {
            case EventType.DragUpdated:
                if (dragObject == null)
                {
                    dragObject           = (GameObject)PrefabUtility.InstantiatePrefab(PrefabUtility.FindPrefabRoot(go));
                    dragObject.hideFlags = HideFlags.HideInHierarchy;
                    dragObject.name      = go.name;
                }

                if (HandleUtility.ignoreRaySnapObjects == null)
                {
                    HandleUtility.ignoreRaySnapObjects = dragObject.GetComponentsInChildren <Transform>();
                }

                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                object hit = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(evt.mousePosition));
                if (hit != null)
                {
                    RaycastHit rh     = (RaycastHit)hit;
                    float      offset = 0;
                    if (Tools.pivotMode == PivotMode.Center)
                    {
                        float geomOffset = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, rh.normal);
                        if (geomOffset != Mathf.Infinity)
                        {
                            offset = Vector3.Dot(dragObject.transform.position, rh.normal) - geomOffset;
                        }
                    }
                    dragObject.transform.position = Matrix4x4.identity.MultiplyPoint(rh.point + (rh.normal * offset));
                }
                else
                {
                    dragObject.transform.position = HandleUtility.GUIPointToWorldRay(evt.mousePosition).GetPoint(10);
                }

                // Use prefabs original z position when in 2D mode
                if (sceneView.in2DMode)
                {
                    Vector3 dragPosition = dragObject.transform.position;
                    dragPosition.z = PrefabUtility.FindPrefabRoot(go).transform.position.z;
                    dragObject.transform.position = dragPosition;
                }

                evt.Use();
                break;

            case EventType.DragPerform:
                string uniqueName = GameObjectUtility.GetUniqueNameForSibling(null, dragObject.name);
                dragObject.hideFlags = 0;
                Undo.RegisterCreatedObjectUndo(dragObject, "Place " + dragObject.name);
                EditorUtility.SetDirty(dragObject);
                DragAndDrop.AcceptDrag();
                Selection.activeObject             = dragObject;
                HandleUtility.ignoreRaySnapObjects = null;
                if (SceneView.mouseOverWindow != null)
                {
                    SceneView.mouseOverWindow.Focus();
                }
                dragObject.name = uniqueName;
                dragObject      = null;
                evt.Use();
                break;

            case EventType.DragExited:
                if (dragObject)
                {
                    UnityObject.DestroyImmediate(dragObject, false);
                    HandleUtility.ignoreRaySnapObjects = null;
                    dragObject = null;
                    evt.Use();
                }
                break;
            }
        }
Ejemplo n.º 8
0
        internal static Object DoObjectField(Rect position, Rect dropRect, int id, Object obj, System.Type objType, SerializedProperty property, ObjectFieldValidator validator, bool allowSceneObjects, GUIStyle style)
        {
            if (validator == null)
            {
                validator = ValidateObjectFieldAssignment;
            }
            Event     evt       = Event.current;
            EventType eventType = evt.type;

            // special case test, so we continue to ping/select objects with the object field disabled
            if (!GUI.enabled && GUIClip.enabled && (Event.current.rawType == EventType.MouseDown))
            {
                eventType = Event.current.rawType;
            }

            bool hasThumbnail = EditorGUIUtility.HasObjectThumbnail(objType);

            // Determine visual type
            ObjectFieldVisualType visualType = ObjectFieldVisualType.IconAndText;

            if (hasThumbnail && position.height <= kObjectFieldMiniThumbnailHeight && position.width <= kObjectFieldMiniThumbnailWidth)
            {
                visualType = ObjectFieldVisualType.MiniPreview;
            }
            else if (hasThumbnail && position.height > kSingleLineHeight)
            {
                visualType = ObjectFieldVisualType.LargePreview;
            }

            Vector2 oldIconSize = EditorGUIUtility.GetIconSize();

            if (visualType == ObjectFieldVisualType.IconAndText)
            {
                EditorGUIUtility.SetIconSize(new Vector2(12, 12));  // Have to be this small to fit inside a single line height ObjectField
            }
            else if (visualType == ObjectFieldVisualType.LargePreview)
            {
                EditorGUIUtility.SetIconSize(new Vector2(64, 64));
            }

            switch (eventType)
            {
            case EventType.DragExited:
                if (GUI.enabled)
                {
                    HandleUtility.Repaint();
                }

                break;

            case EventType.DragUpdated:
            case EventType.DragPerform:

                if (eventType == EventType.DragPerform)
                {
                    string errorString;
                    if (!ValidDroppedObject(DragAndDrop.objectReferences, objType, property, out errorString))
                    {
                        Object reference = DragAndDrop.objectReferences[0];
                        EditorUtility.DisplayDialog("Can't assign script", errorString, "OK");
                        break;
                    }
                }

                if (dropRect.Contains(Event.current.mousePosition) && GUI.enabled)
                {
                    Object[] references      = DragAndDrop.objectReferences;
                    Object   validatedObject = validator(references, objType, property, ObjectFieldValidatorOptions.None);

                    if (validatedObject != null)
                    {
                        // If scene objects are not allowed and object is a scene object then clear
                        if (!allowSceneObjects && !EditorUtility.IsPersistent(validatedObject))
                        {
                            validatedObject = null;
                        }
                    }

                    if (validatedObject != null)
                    {
                        DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
                        if (eventType == EventType.DragPerform)
                        {
                            if (property != null)
                            {
                                property.objectReferenceValue = validatedObject;
                            }
                            else
                            {
                                obj = validatedObject;
                            }

                            GUI.changed = true;
                            DragAndDrop.AcceptDrag();
                            DragAndDrop.activeControlID = 0;
                        }
                        else
                        {
                            DragAndDrop.activeControlID = id;
                        }
                        Event.current.Use();
                    }
                }
                break;

            case EventType.MouseDown:
                // Ignore right clicks
                if (Event.current.button != 0)
                {
                    break;
                }
                if (position.Contains(Event.current.mousePosition))
                {
                    // Get button rect for Object Selector
                    Rect buttonRect = GetButtonRect(visualType, position);

                    EditorGUIUtility.editingTextField = false;

                    if (buttonRect.Contains(Event.current.mousePosition))
                    {
                        if (GUI.enabled)
                        {
                            GUIUtility.keyboardControl = id;
                            ObjectSelector.get.Show(obj, objType, property, allowSceneObjects);
                            ObjectSelector.get.objectSelectorID = id;

                            evt.Use();
                            GUIUtility.ExitGUI();
                        }
                    }
                    else
                    {
                        Object    actualTargetObject = property != null ? property.objectReferenceValue : obj;
                        Component com = actualTargetObject as Component;
                        if (com)
                        {
                            actualTargetObject = com.gameObject;
                        }
                        if (showMixedValue)
                        {
                            actualTargetObject = null;
                        }

                        // One click shows where the referenced object is, or pops up a preview
                        if (Event.current.clickCount == 1)
                        {
                            GUIUtility.keyboardControl = id;

                            PingObjectOrShowPreviewOnClick(actualTargetObject, position);
                            evt.Use();
                        }
                        // Double click opens the asset in external app or changes selection to referenced object
                        else if (Event.current.clickCount == 2)
                        {
                            if (actualTargetObject)
                            {
                                AssetDatabase.OpenAsset(actualTargetObject);
                                GUIUtility.ExitGUI();
                            }
                            evt.Use();
                        }
                    }
                }
                break;

            case EventType.ExecuteCommand:
                string commandName = evt.commandName;
                if (commandName == ObjectSelector.ObjectSelectorUpdatedCommand && ObjectSelector.get.objectSelectorID == id && GUIUtility.keyboardControl == id && (property == null || !property.isScript))
                {
                    return(AssignSelectedObject(property, validator, objType, evt));
                }
                else if (commandName == ObjectSelector.ObjectSelectorClosedCommand && ObjectSelector.get.objectSelectorID == id && GUIUtility.keyboardControl == id && property != null && property.isScript)
                {
                    if (ObjectSelector.get.GetInstanceID() == 0)
                    {
                        // User canceled object selection; don't apply
                        evt.Use();
                        break;
                    }
                    return(AssignSelectedObject(property, validator, objType, evt));
                }
                break;

            case EventType.KeyDown:
                if (GUIUtility.keyboardControl == id)
                {
                    if (evt.keyCode == KeyCode.Backspace || (evt.keyCode == KeyCode.Delete && (evt.modifiers & EventModifiers.Shift) == 0))
                    {
                        if (property != null)
                        {
                            property.objectReferenceValue = null;
                        }
                        else
                        {
                            obj = null;
                        }

                        GUI.changed = true;
                        evt.Use();
                    }

                    // Apparently we have to check for the character being space instead of the keyCode,
                    // otherwise the Inspector will maximize upon pressing space.
                    if (evt.MainActionKeyForControl(id))
                    {
                        ObjectSelector.get.Show(obj, objType, property, allowSceneObjects);
                        ObjectSelector.get.objectSelectorID = id;
                        evt.Use();
                        GUIUtility.ExitGUI();
                    }
                }
                break;

            case EventType.Repaint:
                GUIContent temp;
                if (showMixedValue)
                {
                    temp = s_MixedValueContent;
                }
                else if (property != null)
                {
                    temp = EditorGUIUtility.TempContent(property.objectReferenceStringValue, AssetPreview.GetMiniThumbnail(property.objectReferenceValue));
                    obj  = property.objectReferenceValue;
                    if (obj != null)
                    {
                        Object[] references = { obj };
                        if (EditorSceneManager.preventCrossSceneReferences && CheckForCrossSceneReferencing(obj, property.serializedObject.targetObject))
                        {
                            if (!EditorApplication.isPlaying)
                            {
                                temp = s_SceneMismatch;
                            }
                            else
                            {
                                temp.text = temp.text + string.Format(" ({0})", GetGameObjectFromObject(obj).scene.name);
                            }
                        }
                        else if (validator(references, objType, property, ObjectFieldValidatorOptions.ExactObjectTypeValidation) == null)
                        {
                            temp = s_TypeMismatch;
                        }
                    }
                }
                else
                {
                    temp = EditorGUIUtility.ObjectContent(obj, objType);
                }

                switch (visualType)
                {
                case ObjectFieldVisualType.IconAndText:
                    BeginHandleMixedValueContentColor();
                    style.Draw(position, temp, id, DragAndDrop.activeControlID == id, position.Contains(Event.current.mousePosition));

                    Rect buttonRect = EditorStyles.objectFieldButton.margin.Remove(GetButtonRect(visualType, position));
                    EditorStyles.objectFieldButton.Draw(buttonRect, GUIContent.none, id, DragAndDrop.activeControlID == id, buttonRect.Contains(Event.current.mousePosition));
                    EndHandleMixedValueContentColor();
                    break;

                case ObjectFieldVisualType.LargePreview:
                    DrawObjectFieldLargeThumb(position, id, obj, temp);
                    break;

                case ObjectFieldVisualType.MiniPreview:
                    DrawObjectFieldMiniThumb(position, id, obj, temp);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                break;
            }

            EditorGUIUtility.SetIconSize(oldIconSize);

            return(obj);
        }
        private void ListArea(Rect rect, PresetLibrary lib, object newPresetObject)
        {
            if (lib != null)
            {
                int   num2;
                int   num3;
                Event current = Event.current;
                if ((this.m_PresetLibraryFileLocation == PresetFileLocation.ProjectFolder) && (current.type == EventType.Repaint))
                {
                    this.m_IsOpenForEdit = AssetDatabase.IsOpenForEdit(this.pathWithExtension);
                }
                else if (this.m_PresetLibraryFileLocation == PresetFileLocation.PreferencesFolder)
                {
                    this.m_IsOpenForEdit = true;
                }
                if (!this.m_IsOpenForEdit)
                {
                    Rect rect2 = new Rect(rect.x, rect.yMax - this.versionControlAreaHeight, rect.width, this.versionControlAreaHeight);
                    this.VersionControlArea(rect2);
                    rect.height -= this.versionControlAreaHeight;
                }
                for (int i = 0; i < 2; i++)
                {
                    this.gridWidth = !this.m_ShowedScrollBarLastFrame ? rect.width : (rect.width - 17f);
                    this.SetupGrid(this.gridWidth, lib.Count());
                    bool flag = this.m_Grid.height > rect.height;
                    if (flag == this.m_ShowedScrollBarLastFrame)
                    {
                        break;
                    }
                    this.m_ShowedScrollBarLastFrame = flag;
                }
                if ((this.m_ShowedScrollBarLastFrame || this.alwaysShowScrollAreaHorizontalLines) && (Event.current.type == EventType.Repaint))
                {
                    Rect rect3 = new RectOffset(1, 1, 1, 1).Add(rect);
                    rect3.height = 1f;
                    EditorGUI.DrawRect(rect3, new Color(0f, 0f, 0f, 0.3f));
                    rect3.y += rect.height + 1f;
                    EditorGUI.DrawRect(rect3, new Color(0f, 0f, 0f, 0.3f));
                }
                Rect viewRect = new Rect(0f, 0f, 1f, this.m_Grid.height);
                this.m_State.m_ScrollPosition = GUI.BeginScrollView(rect, this.m_State.m_ScrollPosition, viewRect);
                float gridStartY = 0f;
                int   maxIndex   = !this.m_ShowAddNewPresetItem ? (lib.Count() - 1) : lib.Count();
                bool  flag2      = this.m_Grid.IsVisibleInScrollView(rect.height, this.m_State.m_ScrollPosition.y, gridStartY, maxIndex, out num2, out num3);
                bool  flag3      = false;
                if (flag2)
                {
                    if (this.GetRenameOverlay().IsRenaming() && !this.GetRenameOverlay().isWaitingForDelay)
                    {
                        if (!this.m_State.m_RenameOverlay.OnGUI())
                        {
                            this.EndRename();
                            current.Use();
                        }
                        this.Repaint();
                    }
                    for (int j = num2; j <= num3; j++)
                    {
                        bool flag4;
                        Rect dragRect;
                        int  controlID  = j + 0xf4240;
                        Rect itemRect   = this.m_Grid.CalcRect(j, gridStartY);
                        Rect buttonRect = itemRect;
                        Rect position   = itemRect;
                        PresetLibraryEditorState.ItemViewMode itemViewMode = this.m_State.itemViewMode;
                        if ((itemViewMode != PresetLibraryEditorState.ItemViewMode.Grid) && (itemViewMode == PresetLibraryEditorState.ItemViewMode.List))
                        {
                            buttonRect.width = this.m_State.m_PreviewHeight * this.m_PreviewAspect;
                            position.x      += buttonRect.width + 8f;
                            position.width  -= buttonRect.width + 10f;
                            position.height  = 16f;
                            position.y       = itemRect.yMin + ((itemRect.height - 16f) * 0.5f);
                        }
                        if (this.m_ShowAddNewPresetItem && (j == lib.Count()))
                        {
                            this.CreateNewPresetButton(buttonRect, newPresetObject, lib, this.m_IsOpenForEdit);
                        }
                        else
                        {
                            flag4 = this.IsRenaming(j);
                            if (flag4)
                            {
                                Rect rect8 = position;
                                rect8.y--;
                                rect8.x--;
                                this.m_State.m_RenameOverlay.editFieldRect = rect8;
                            }
                            switch (current.type)
                            {
                            case EventType.MouseDown:
                                if ((current.button == 0) && itemRect.Contains(current.mousePosition))
                                {
                                    GUIUtility.hotControl = controlID;
                                    if (current.clickCount == 1)
                                    {
                                        this.m_ItemClickedCallback(current.clickCount, lib.GetPreset(j));
                                        current.Use();
                                    }
                                }
                                break;

                            case EventType.MouseUp:
                                if (GUIUtility.hotControl == controlID)
                                {
                                    GUIUtility.hotControl = 0;
                                    if (((current.button == 0) && itemRect.Contains(current.mousePosition)) && (Event.current.alt && this.m_IsOpenForEdit))
                                    {
                                        this.DeletePreset(j);
                                        current.Use();
                                    }
                                }
                                break;

                            case EventType.MouseMove:
                                if (!itemRect.Contains(current.mousePosition))
                                {
                                    goto Label_0812;
                                }
                                if (this.m_State.m_HoverIndex != j)
                                {
                                    this.m_State.m_HoverIndex = j;
                                    this.Repaint();
                                }
                                break;

                            case EventType.MouseDrag:
                                if ((GUIUtility.hotControl == controlID) && this.m_IsOpenForEdit)
                                {
                                    DragAndDropDelay stateObject = (DragAndDropDelay)GUIUtility.GetStateObject(typeof(DragAndDropDelay), controlID);
                                    if (stateObject.CanStartDrag())
                                    {
                                        DragAndDrop.PrepareStartDrag();
                                        DragAndDrop.SetGenericData("DraggingPreset", j);
                                        DragAndDrop.objectReferences = new Object[0];
                                        DragAndDrop.StartDrag(string.Empty);
                                        this.m_DragState.draggingIndex = j;
                                        this.m_DragState.dragUponIndex = j;
                                        GUIUtility.hotControl          = 0;
                                    }
                                    current.Use();
                                }
                                break;

                            case EventType.Repaint:
                                if ((this.m_State.m_HoverIndex == j) && !itemRect.Contains(current.mousePosition))
                                {
                                    goto Label_046D;
                                }
                                goto Label_0479;

                            case EventType.DragUpdated:
                            case EventType.DragPerform:
                                dragRect = this.GetDragRect(itemRect);
                                if (!dragRect.Contains(current.mousePosition))
                                {
                                    break;
                                }
                                this.m_DragState.dragUponIndex = j;
                                this.m_DragState.dragUponRect  = itemRect;
                                if (this.m_State.itemViewMode != PresetLibraryEditorState.ItemViewMode.List)
                                {
                                    goto Label_0694;
                                }
                                this.m_DragState.insertAfterIndex = ((current.mousePosition.y - dragRect.y) / dragRect.height) > 0.5f;
                                goto Label_06C5;

                            case EventType.DragExited:
                                if (this.m_DragState.IsDragging())
                                {
                                    this.ClearDragState();
                                    current.Use();
                                }
                                break;

                            case EventType.ContextClick:
                                if (itemRect.Contains(current.mousePosition))
                                {
                                    PresetContextMenu <T> .Show(this.m_IsOpenForEdit, j, newPresetObject, (PresetLibraryEditor <T>) this);

                                    current.Use();
                                }
                                break;
                            }
                        }
                        continue;
Label_046D:
                        this.m_State.m_HoverIndex = -1;
Label_0479:
                        if ((this.m_DragState.draggingIndex == j) || (GUIUtility.hotControl == controlID))
                        {
                            this.DrawHoverEffect(itemRect, false);
                        }
                        lib.Draw(buttonRect, j);
                        if (!flag4 && this.drawLabels)
                        {
                            GUI.Label(position, GUIContent.Temp(lib.GetName(j)));
                        }
                        if ((this.m_DragState.dragUponIndex == j) && (this.m_DragState.draggingIndex != this.m_DragState.dragUponIndex))
                        {
                            flag3 = true;
                        }
                        if (((GUIUtility.hotControl == 0) && Event.current.alt) && this.m_IsOpenForEdit)
                        {
                            EditorGUIUtility.AddCursorRect(itemRect, MouseCursor.ArrowMinus);
                        }
                        continue;
Label_0694:
                        this.m_DragState.insertAfterIndex = ((current.mousePosition.x - dragRect.x) / dragRect.width) > 0.5f;
Label_06C5:
                        if (current.type == EventType.DragPerform)
                        {
                            if (this.m_DragState.draggingIndex >= 0)
                            {
                                this.MovePreset(this.m_DragState.draggingIndex, this.m_DragState.dragUponIndex, this.m_DragState.insertAfterIndex);
                                DragAndDrop.AcceptDrag();
                            }
                            this.ClearDragState();
                        }
                        DragAndDrop.visualMode = DragAndDropVisualMode.Move;
                        current.Use();
                        continue;
Label_0812:
                        if (this.m_State.m_HoverIndex == j)
                        {
                            this.m_State.m_HoverIndex = -1;
                            this.Repaint();
                        }
                    }
                    if (flag3)
                    {
                        this.DrawDragInsertionMarker();
                    }
                }
                GUI.EndScrollView();
            }
        }
Ejemplo n.º 10
0
        private void ListArea(Rect rect, PresetLibrary lib, object newPresetObject)
        {
            if (lib == null)
            {
                return;
            }
            Event current = Event.current;

            if (this.m_PresetLibraryFileLocation == PresetFileLocation.ProjectFolder && current.type == EventType.Repaint)
            {
                this.m_IsOpenForEdit = AssetDatabase.IsOpenForEdit(this.pathWithExtension);
            }
            else
            {
                if (this.m_PresetLibraryFileLocation == PresetFileLocation.PreferencesFolder)
                {
                    this.m_IsOpenForEdit = true;
                }
            }
            if (!this.m_IsOpenForEdit)
            {
                Rect rect2 = new Rect(rect.x, rect.yMax - this.versionControlAreaHeight, rect.width, this.versionControlAreaHeight);
                this.VersionControlArea(rect2);
                rect.height -= this.versionControlAreaHeight;
            }
            for (int i = 0; i < 2; i++)
            {
                this.gridWidth = ((!this.m_ShowedScrollBarLastFrame) ? rect.width : (rect.width - 17f));
                this.SetupGrid(this.gridWidth, lib.Count());
                bool flag = this.m_Grid.height > rect.height;
                if (flag == this.m_ShowedScrollBarLastFrame)
                {
                    break;
                }
                this.m_ShowedScrollBarLastFrame = flag;
            }
            if ((this.m_ShowedScrollBarLastFrame || this.alwaysShowScrollAreaHorizontalLines) && Event.current.type == EventType.Repaint)
            {
                Rect rect3 = new RectOffset(1, 1, 1, 1).Add(rect);
                rect3.height = 1f;
                EditorGUI.DrawRect(rect3, new Color(0f, 0f, 0f, 0.3f));
                rect3.y += rect.height + 1f;
                EditorGUI.DrawRect(rect3, new Color(0f, 0f, 0f, 0.3f));
            }
            Rect viewRect = new Rect(0f, 0f, 1f, this.m_Grid.height);

            this.m_State.m_ScrollPosition = GUI.BeginScrollView(rect, this.m_State.m_ScrollPosition, viewRect);
            float num      = 0f;
            int   maxIndex = (!this.m_ShowAddNewPresetItem) ? (lib.Count() - 1) : lib.Count();
            int   num2;
            int   num3;
            bool  flag2 = this.m_Grid.IsVisibleInScrollView(rect.height, this.m_State.m_ScrollPosition.y, num, maxIndex, out num2, out num3);
            bool  flag3 = false;

            if (flag2)
            {
                if (this.GetRenameOverlay().IsRenaming() && !this.GetRenameOverlay().isWaitingForDelay)
                {
                    if (!this.m_State.m_RenameOverlay.OnGUI())
                    {
                        this.EndRename();
                        current.Use();
                    }
                    this.Repaint();
                }
                for (int j = num2; j <= num3; j++)
                {
                    int  num4  = j + 1000000;
                    Rect rect4 = this.m_Grid.CalcRect(j, num);
                    Rect rect5 = rect4;
                    Rect rect6 = rect4;
                    PresetLibraryEditorState.ItemViewMode itemViewMode = this.m_State.itemViewMode;
                    if (itemViewMode != PresetLibraryEditorState.ItemViewMode.Grid)
                    {
                        if (itemViewMode == PresetLibraryEditorState.ItemViewMode.List)
                        {
                            rect5.width  = this.m_State.m_PreviewHeight * this.m_PreviewAspect;
                            rect6.x     += rect5.width + 8f;
                            rect6.width -= rect5.width + 10f;
                            rect6.height = 16f;
                            rect6.y      = rect4.yMin + (rect4.height - 16f) * 0.5f;
                        }
                    }
                    if (this.m_ShowAddNewPresetItem && j == lib.Count())
                    {
                        this.CreateNewPresetButton(rect5, newPresetObject, lib, this.m_IsOpenForEdit);
                    }
                    else
                    {
                        bool flag4 = this.IsRenaming(j);
                        if (flag4)
                        {
                            Rect editFieldRect = rect6;
                            editFieldRect.y -= 1f;
                            editFieldRect.x -= 1f;
                            this.m_State.m_RenameOverlay.editFieldRect = editFieldRect;
                        }
                        switch (current.type)
                        {
                        case EventType.MouseDown:
                            if (current.button == 0 && rect4.Contains(current.mousePosition))
                            {
                                GUIUtility.hotControl = num4;
                                if (current.clickCount == 1)
                                {
                                    this.m_ItemClickedCallback(current.clickCount, lib.GetPreset(j));
                                    current.Use();
                                }
                            }
                            break;

                        case EventType.MouseUp:
                            if (GUIUtility.hotControl == num4)
                            {
                                GUIUtility.hotControl = 0;
                                if (current.button == 0 && rect4.Contains(current.mousePosition) && Event.current.alt && this.m_IsOpenForEdit)
                                {
                                    this.DeletePreset(j);
                                    current.Use();
                                }
                            }
                            break;

                        case EventType.MouseMove:
                            if (rect4.Contains(current.mousePosition))
                            {
                                if (this.m_State.m_HoverIndex != j)
                                {
                                    this.m_State.m_HoverIndex = j;
                                    this.Repaint();
                                }
                            }
                            else
                            {
                                if (this.m_State.m_HoverIndex == j)
                                {
                                    this.m_State.m_HoverIndex = -1;
                                    this.Repaint();
                                }
                            }
                            break;

                        case EventType.MouseDrag:
                            if (GUIUtility.hotControl == num4 && this.m_IsOpenForEdit)
                            {
                                DragAndDropDelay dragAndDropDelay = (DragAndDropDelay)GUIUtility.GetStateObject(typeof(DragAndDropDelay), num4);
                                if (dragAndDropDelay.CanStartDrag())
                                {
                                    DragAndDrop.PrepareStartDrag();
                                    DragAndDrop.SetGenericData("DraggingPreset", j);
                                    DragAndDrop.objectReferences = new UnityEngine.Object[0];
                                    DragAndDrop.StartDrag(string.Empty);
                                    this.m_DragState.draggingIndex = j;
                                    this.m_DragState.dragUponIndex = j;
                                    GUIUtility.hotControl          = 0;
                                }
                                current.Use();
                            }
                            break;

                        case EventType.Repaint:
                            if (this.m_State.m_HoverIndex == j)
                            {
                                if (!rect4.Contains(current.mousePosition))
                                {
                                    this.m_State.m_HoverIndex = -1;
                                }
                            }
                            if (this.m_DragState.draggingIndex == j || GUIUtility.hotControl == num4)
                            {
                                this.DrawHoverEffect(rect4, false);
                            }
                            lib.Draw(rect5, j);
                            if (!flag4 && this.drawLabels)
                            {
                                GUI.Label(rect6, GUIContent.Temp(lib.GetName(j)));
                            }
                            if (this.m_DragState.dragUponIndex == j && this.m_DragState.draggingIndex != this.m_DragState.dragUponIndex)
                            {
                                flag3 = true;
                            }
                            if (GUIUtility.hotControl == 0 && Event.current.alt && this.m_IsOpenForEdit)
                            {
                                EditorGUIUtility.AddCursorRect(rect4, MouseCursor.ArrowMinus);
                            }
                            break;

                        case EventType.DragUpdated:
                        case EventType.DragPerform:
                        {
                            Rect dragRect = this.GetDragRect(rect4);
                            if (dragRect.Contains(current.mousePosition))
                            {
                                this.m_DragState.dragUponIndex = j;
                                this.m_DragState.dragUponRect  = rect4;
                                if (this.m_State.itemViewMode == PresetLibraryEditorState.ItemViewMode.List)
                                {
                                    this.m_DragState.insertAfterIndex = ((current.mousePosition.y - dragRect.y) / dragRect.height > 0.5f);
                                }
                                else
                                {
                                    this.m_DragState.insertAfterIndex = ((current.mousePosition.x - dragRect.x) / dragRect.width > 0.5f);
                                }
                                bool flag5 = current.type == EventType.DragPerform;
                                if (flag5)
                                {
                                    if (this.m_DragState.draggingIndex >= 0)
                                    {
                                        this.MovePreset(this.m_DragState.draggingIndex, this.m_DragState.dragUponIndex, this.m_DragState.insertAfterIndex);
                                        DragAndDrop.AcceptDrag();
                                    }
                                    this.ClearDragState();
                                }
                                DragAndDrop.visualMode = DragAndDropVisualMode.Move;
                                current.Use();
                            }
                            break;
                        }

                        case EventType.DragExited:
                            if (this.m_DragState.IsDragging())
                            {
                                this.ClearDragState();
                                current.Use();
                            }
                            break;

                        case EventType.ContextClick:
                            if (rect4.Contains(current.mousePosition))
                            {
                                PresetLibraryEditor <T> .PresetContextMenu.Show(this.m_IsOpenForEdit, j, newPresetObject, this);

                                current.Use();
                            }
                            break;
                        }
                    }
                }
                if (flag3)
                {
                    this.DrawDragInsertionMarker();
                }
            }
            GUI.EndScrollView();
        }
Ejemplo n.º 11
0
            public bool MoveNext()
            {
                if (xPos > -1)
                {
                    if (ListViewShared.HasMouseDown(ilvState, rect))
                    {
                        ilvState.state.selectionChanged = true;
                        ilvState.state.row       = yPos;
                        ilvState.state.column    = xPos;
                        ilvState.state.scrollPos = ListViewShared.ListViewScrollToRow(ilvState, yPos); // this is about clicking on a row that is partially visible

                        if ((ilvState.wantsReordering || ilvState.wantsToStartCustomDrag) && (GUIUtility.hotControl == ilvState.state.ID))
                        {
                            DragAndDropDelay delay = (DragAndDropDelay)GUIUtility.GetStateObject(typeof(DragAndDropDelay), ilvState.state.ID);
                            delay.mouseDownPosition = Event.current.mousePosition;
                            ilvState.dragItem       = yPos;
                            dragControlID           = ilvState.state.ID;
                        }
                    }
                    // On Mouse drag, start drag & drop
                    if (!ListViewShared.isDragging &&
                        (ilvState.wantsReordering || ilvState.wantsToStartCustomDrag) &&
                        GUIUtility.hotControl == ilvState.state.ID &&
                        Event.current.type == EventType.MouseDrag &&
                        GUIClip.visibleRect.Contains(Event.current.mousePosition))
                    {
                        DragAndDropDelay delay = (DragAndDropDelay)GUIUtility.GetStateObject(typeof(DragAndDropDelay), ilvState.state.ID);

                        if (delay.CanStartDrag())
                        {
                            DragAndDrop.PrepareStartDrag();

                            DragAndDrop.objectReferences = new UnityEngine.Object[] {};  // this IS required for dragging to work
                            DragAndDrop.paths            = null;

                            if (ilvState.wantsReordering)
                            {
                                ilvState.state.dropHereRect = new Rect(ilvState.rect.x, 0, ilvState.rect.width, ilvState.state.rowHeight * 2);
                                DragAndDrop.StartDrag(dragTitle);
                            }
                            else if (ilvState.wantsToStartCustomDrag)
                            {
                                DragAndDrop.SetGenericData("CustomDragID", ilvState.state.ID);
                                DragAndDrop.StartDrag(dragTitle);
                            }

                            ListViewShared.isDragging = true;
                        }

                        Event.current.Use();
                    }
                }

                xPos++;

                if (xPos > xTo)
                {
                    xPos = 0;
                    yPos++;

                    rect.x     = firstRect.x;
                    rect.width = colWidths[0];

                    if (yPos > yTo)
                    {
                        quiting = true;
                    }
                    else // move vertically
                    {
                        rect.y += rect.height;
                    }
                }
                else // move horizontally
                {
                    if (xPos >= 1)
                    {
                        rect.x += colWidths[xPos - 1];
                    }

                    rect.width = colWidths[xPos];
                }

                element.row      = yPos;
                element.column   = xPos;
                element.position = rect;

                if (element.row >= ilvState.state.totalRows)
                {
                    quiting = true;
                }

                if (isLayouted && Event.current.type == EventType.Layout)
                {
                    // this is just "on layout event enumerate just first row" (so we get height of single row)
                    if (yFrom + 1 == yPos)
                    {
                        quiting = true;
                    }
                }

                if (isLayouted && yPos != yFrom)
                {
                    GUILayout.EndHorizontal();
                }

                if (quiting)
                {
                    if (ilvState.state.drawDropHere && Event.current.GetTypeForControl(ilvState.state.ID) == EventType.Repaint)
                    {
                        GUIStyle insertion = Constants.insertion;
                        insertion.Draw(insertion.margin.Remove(ilvState.state.dropHereRect), false, false, false, false);
                    }

                    if (ListViewShared.ListViewKeyboard(ilvState, colWidths.Length))
                    {
                        ilvState.state.selectionChanged = true;
                    }

                    if (Event.current.GetTypeForControl(ilvState.state.ID) == EventType.MouseUp)
                    {
                        GUIUtility.hotControl = 0;
                    }

                    if (ilvState.wantsReordering && (GUIUtility.hotControl == ilvState.state.ID))
                    {
                        ListViewState lv = ilvState.state;

                        switch (Event.current.type)
                        {
                        case EventType.DragUpdated:
                        {
                            DragAndDrop.visualMode = ilvState.rect.Contains(Event.current.mousePosition) ?
                                                     DragAndDropVisualMode.Move : DragAndDropVisualMode.None;

                            Event.current.Use();

                            if (DragAndDrop.visualMode != DragAndDropVisualMode.None)
                            {
                                lv.dropHereRect.y = (Mathf.RoundToInt(Event.current.mousePosition.y / lv.rowHeight) - 1) * lv.rowHeight;

                                if (lv.dropHereRect.y >= lv.rowHeight * lv.totalRows)
                                {
                                    lv.dropHereRect.y = lv.rowHeight * (lv.totalRows - 1);
                                }

                                lv.drawDropHere = true;
                            }

                            break;
                        }

                        case EventType.DragPerform:
                        {
                            if (GUIClip.visibleRect.Contains(Event.current.mousePosition))
                            {
                                ilvState.state.draggedFrom = ilvState.dragItem;
                                ilvState.state.draggedTo   = Mathf.RoundToInt(Event.current.mousePosition.y / lv.rowHeight);

                                if (ilvState.state.draggedTo > ilvState.state.totalRows)
                                {
                                    ilvState.state.draggedTo = ilvState.state.totalRows;
                                }

                                // the guy handling this would better actually rearrange items...
                                if (ilvState.state.draggedTo > ilvState.state.draggedFrom)
                                {
                                    ilvState.state.row = ilvState.state.draggedTo - 1;
                                }
                                else
                                {
                                    ilvState.state.row = ilvState.state.draggedTo;
                                }

                                ilvState.state.selectionChanged = true;

                                DragAndDrop.AcceptDrag();
                                Event.current.Use();
                                ilvState.wantsReordering    = false;
                                ilvState.state.drawDropHere = false;
                            }

                            GUIUtility.hotControl = 0;
                            break;
                        }

                        case EventType.DragExited:
                        {
                            ilvState.wantsReordering    = false;
                            ilvState.state.drawDropHere = false;

                            GUIUtility.hotControl = 0;
                            break;
                        }
                        }
                    }
                    else if (ilvState.wantsExternalFiles)
                    {
                        switch (Event.current.type)
                        {
                        case EventType.DragUpdated:
                        {
                            if ((GUIClip.visibleRect.Contains(Event.current.mousePosition)) &&
                                (DragAndDrop.paths != null) && (DragAndDrop.paths.Length != 0))             // dragging files from somewhere
                            {
                                DragAndDrop.visualMode = ilvState.rect.Contains(Event.current.mousePosition) ?
                                                         DragAndDropVisualMode.Copy : DragAndDropVisualMode.None;

                                Event.current.Use();

                                if (DragAndDrop.visualMode != DragAndDropVisualMode.None)
                                {
                                    ilvState.state.dropHereRect = new Rect(ilvState.rect.x,
                                                                           (Mathf.RoundToInt(Event.current.mousePosition.y / ilvState.state.rowHeight) - 1) * ilvState.state.rowHeight,
                                                                           ilvState.rect.width, ilvState.state.rowHeight);

                                    if (ilvState.state.dropHereRect.y >= ilvState.state.rowHeight * ilvState.state.totalRows)
                                    {
                                        ilvState.state.dropHereRect.y = ilvState.state.rowHeight * (ilvState.state.totalRows - 1);
                                    }

                                    ilvState.state.drawDropHere = true;
                                }
                            }
                            break;
                        }

                        case EventType.DragPerform:
                        {
                            if (GUIClip.visibleRect.Contains(Event.current.mousePosition))
                            {
                                ilvState.state.fileNames = DragAndDrop.paths;
                                DragAndDrop.AcceptDrag();
                                Event.current.Use();
                                ilvState.wantsExternalFiles = false;
                                ilvState.state.drawDropHere = false;
                                ilvState.state.draggedTo    = Mathf.RoundToInt(Event.current.mousePosition.y / ilvState.state.rowHeight);
                                if (ilvState.state.draggedTo > ilvState.state.totalRows)
                                {
                                    ilvState.state.draggedTo = ilvState.state.totalRows;
                                }
                                ilvState.state.row = ilvState.state.draggedTo;
                            }

                            GUIUtility.hotControl = 0;

                            break;
                        }

                        case EventType.DragExited:
                        {
                            ilvState.wantsExternalFiles = false;
                            ilvState.state.drawDropHere = false;

                            GUIUtility.hotControl = 0;
                            break;
                        }
                        }
                    }
                    else if (ilvState.wantsToAcceptCustomDrag && (dragControlID != ilvState.state.ID))
                    {
                        switch (Event.current.type)
                        {
                        case EventType.DragUpdated:
                        {
                            object data = DragAndDrop.GetGenericData("CustomDragID");

                            if (GUIClip.visibleRect.Contains(Event.current.mousePosition) && data != null)
                            {
                                DragAndDrop.visualMode = ilvState.rect.Contains(Event.current.mousePosition) ?
                                                         DragAndDropVisualMode.Move : DragAndDropVisualMode.None;

                                Event.current.Use();
                            }
                            break;
                        }

                        case EventType.DragPerform:
                        {
                            object data = DragAndDrop.GetGenericData("CustomDragID");

                            if (GUIClip.visibleRect.Contains(Event.current.mousePosition) && data != null)
                            {
                                ilvState.state.customDraggedFromID = (int)data;
                                DragAndDrop.AcceptDrag();
                                Event.current.Use();
                            }

                            GUIUtility.hotControl = 0;
                            break;
                        }

                        case EventType.DragExited:
                        {
                            GUIUtility.hotControl = 0;
                            break;
                        }
                        }
                    }

                    if (ilvState.beganHorizontal)
                    {
                        EditorGUILayout.EndScrollView();
                        GUILayout.EndHorizontal();
                        ilvState.beganHorizontal = false;
                    }

                    if (isLayouted)
                    {
                        GUILayoutUtility.EndLayoutGroup();
                        EditorGUILayout.EndScrollView();
                    }

                    ilvState.wantsReordering    = false;
                    ilvState.wantsExternalFiles = false;
                }
                else if (isLayouted)
                {
                    if (yPos != yFrom)
                    {
                        ilvStateL.group.ResetCursor();
                        ilvStateL.group.AddY();
                    }
                    else
                    {
                        ilvStateL.group.AddY(ilvState.invisibleRows * ilvState.state.rowHeight);
                    }
                }

                if (isLayouted)
                {
                    if (!quiting)
                    {
                        GUILayout.BeginHorizontal(GUIStyle.none); // for each row
                    }
                    else
                    {
                        GUILayout.EndHorizontal(); // the one used for drawing LVs background
                    }
                }

                return(!quiting);
            }
Ejemplo n.º 12
0
        static Object DoObjectField(Rect position, Rect dropRect, int id, Object obj, Object objBeingEdited, System.Type objType, System.Type additionalType, SerializedProperty property, ObjectFieldValidator validator, bool allowSceneObjects, GUIStyle style, GUIStyle buttonStyle, Action <Object> onObjectSelectorClosed = null, Action <Object> onObjectSelectedUpdated = null)
        {
            if (validator == null)
            {
                validator = ValidateObjectFieldAssignment;
            }
            if (property != null)
            {
                obj = property.objectReferenceValue;
            }
            Event     evt       = Event.current;
            EventType eventType = evt.type;

            // special case test, so we continue to ping/select objects with the object field disabled
            if (!GUI.enabled && GUIClip.enabled && (Event.current.rawType == EventType.MouseDown))
            {
                eventType = Event.current.rawType;
            }

            bool hasThumbnail = EditorGUIUtility.HasObjectThumbnail(objType);

            // Determine visual type
            ObjectFieldVisualType visualType = ObjectFieldVisualType.IconAndText;

            if (hasThumbnail && position.height <= kObjectFieldMiniThumbnailHeight && position.width <= kObjectFieldMiniThumbnailWidth)
            {
                visualType = ObjectFieldVisualType.MiniPreview;
            }
            else if (hasThumbnail && position.height > kSingleLineHeight)
            {
                visualType = ObjectFieldVisualType.LargePreview;
            }

            Vector2 oldIconSize = EditorGUIUtility.GetIconSize();

            if (visualType == ObjectFieldVisualType.IconAndText)
            {
                EditorGUIUtility.SetIconSize(new Vector2(12, 12));  // Have to be this small to fit inside a single line height ObjectField
            }
            else if (visualType == ObjectFieldVisualType.LargePreview)
            {
                EditorGUIUtility.SetIconSize(new Vector2(64, 64));
            }

            if ((eventType == EventType.MouseDown && Event.current.button == 1 ||
                 (eventType == EventType.ContextClick && visualType == ObjectFieldVisualType.IconAndText)) &&
                position.Contains(Event.current.mousePosition))
            {
                var actualObject = property != null ? property.objectReferenceValue : obj;
                var contextMenu  = new GenericMenu();

                if (FillPropertyContextMenu(property, null, contextMenu) != null)
                {
                    contextMenu.AddSeparator("");
                }
                contextMenu.AddItem(GUIContent.Temp("Properties..."), false, () => PropertyEditor.OpenPropertyEditor(actualObject));
                contextMenu.DropDown(position);
                Event.current.Use();
            }

            switch (eventType)
            {
            case EventType.DragExited:
                if (GUI.enabled)
                {
                    HandleUtility.Repaint();
                }

                break;

            case EventType.DragUpdated:
            case EventType.DragPerform:

                if (eventType == EventType.DragPerform)
                {
                    string errorString;
                    if (!ValidDroppedObject(DragAndDrop.objectReferences, objType, out errorString))
                    {
                        Object reference = DragAndDrop.objectReferences[0];
                        EditorUtility.DisplayDialog("Can't assign script", errorString, "OK");
                        break;
                    }
                }

                if (dropRect.Contains(Event.current.mousePosition) && GUI.enabled)
                {
                    Object[] references      = DragAndDrop.objectReferences;
                    Object   validatedObject = validator(references, objType, property, ObjectFieldValidatorOptions.None);

                    if (validatedObject != null)
                    {
                        // If scene objects are not allowed and object is a scene object then clear
                        if (!allowSceneObjects && !EditorUtility.IsPersistent(validatedObject))
                        {
                            validatedObject = null;
                        }
                    }

                    if (validatedObject != null)
                    {
                        DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
                        if (eventType == EventType.DragPerform)
                        {
                            if (property != null)
                            {
                                property.objectReferenceValue = validatedObject;
                            }
                            else
                            {
                                obj = validatedObject;
                            }

                            GUI.changed = true;
                            DragAndDrop.AcceptDrag();
                            DragAndDrop.activeControlID = 0;
                        }
                        else
                        {
                            DragAndDrop.activeControlID = id;
                        }
                        Event.current.Use();
                    }
                }
                break;

            case EventType.MouseDown:
                if (position.Contains(Event.current.mousePosition) && Event.current.button == 0)
                {
                    // Get button rect for Object Selector
                    Rect buttonRect = GetButtonRect(visualType, position);

                    EditorGUIUtility.editingTextField = false;

                    if (buttonRect.Contains(Event.current.mousePosition))
                    {
                        if (GUI.enabled)
                        {
                            GUIUtility.keyboardControl = id;
                            var types = additionalType == null ? new Type[] { objType } : new Type[] { objType, additionalType };
                            if (property != null)
                            {
                                ObjectSelector.get.Show(types, property, allowSceneObjects, onObjectSelectorClosed: onObjectSelectorClosed, onObjectSelectedUpdated: onObjectSelectedUpdated);
                            }
                            else
                            {
                                ObjectSelector.get.Show(obj, types, objBeingEdited, allowSceneObjects, onObjectSelectorClosed: onObjectSelectorClosed, onObjectSelectedUpdated: onObjectSelectedUpdated);
                            }
                            ObjectSelector.get.objectSelectorID = id;

                            evt.Use();
                            GUIUtility.ExitGUI();
                        }
                    }
                    else
                    {
                        Object    actualTargetObject = property != null ? property.objectReferenceValue : obj;
                        Component com = actualTargetObject as Component;
                        if (com)
                        {
                            actualTargetObject = com.gameObject;
                        }
                        if (showMixedValue)
                        {
                            actualTargetObject = null;
                        }

                        // One click shows where the referenced object is, or pops up a preview
                        if (Event.current.clickCount == 1)
                        {
                            GUIUtility.keyboardControl = id;

                            PingObjectOrShowPreviewOnClick(actualTargetObject, position);
                            var selectedMaterial = actualTargetObject as Material;
                            if (selectedMaterial != null)
                            {
                                PingObjectInSceneViewOnClick(selectedMaterial);
                            }
                            evt.Use();
                        }
                        // Double click opens the asset in external app or changes selection to referenced object
                        else if (Event.current.clickCount == 2)
                        {
                            if (actualTargetObject)
                            {
                                AssetDatabase.OpenAsset(actualTargetObject);
                                evt.Use();
                                GUIUtility.ExitGUI();
                            }
                        }
                    }
                }
                break;

            case EventType.ExecuteCommand:
                string commandName = evt.commandName;
                if (commandName == ObjectSelector.ObjectSelectorUpdatedCommand && ObjectSelector.get.objectSelectorID == id && GUIUtility.keyboardControl == id && (property == null || !property.isScript))
                {
                    return(AssignSelectedObject(property, validator, objType, evt));
                }
                else if (commandName == ObjectSelector.ObjectSelectorClosedCommand && ObjectSelector.get.objectSelectorID == id && GUIUtility.keyboardControl == id && property != null && property.isScript)
                {
                    if (ObjectSelector.get.GetInstanceID() == 0)
                    {
                        // User canceled object selection; don't apply
                        evt.Use();
                        break;
                    }
                    return(AssignSelectedObject(property, validator, objType, evt));
                }
                else if ((evt.commandName == EventCommandNames.Delete || evt.commandName == EventCommandNames.SoftDelete) && GUIUtility.keyboardControl == id)
                {
                    if (property != null)
                    {
                        property.objectReferenceValue = null;
                    }
                    else
                    {
                        obj = null;
                    }

                    GUI.changed = true;
                    evt.Use();
                }
                break;

            case EventType.ValidateCommand:
                if ((evt.commandName == EventCommandNames.Delete || evt.commandName == EventCommandNames.SoftDelete) && GUIUtility.keyboardControl == id)
                {
                    evt.Use();
                }
                break;

            case EventType.KeyDown:
                if (GUIUtility.keyboardControl == id)
                {
                    if (evt.keyCode == KeyCode.Backspace || (evt.keyCode == KeyCode.Delete && (evt.modifiers & EventModifiers.Shift) == 0))
                    {
                        if (property != null)
                        {
                            if (property.propertyPath.EndsWith("]"))
                            {
                                var  parentArrayPropertyPath = property.propertyPath.Substring(0, property.propertyPath.LastIndexOf(".Array.data[", StringComparison.Ordinal));
                                var  parentArrayProperty     = property.serializedObject.FindProperty(parentArrayPropertyPath);
                                bool isReorderableList       = PropertyHandler.s_reorderableLists.ContainsKey(ReorderableListWrapper.GetPropertyIdentifier(parentArrayProperty));

                                // If it's an element of an non-orderable array and it is displayed inside a list, remove that element from the array (cases 1379541 & 1335322)
                                if (!isReorderableList && GUI.isInsideList && GetInsideListDepth() == parentArrayProperty.depth)
                                {
                                    TargetChoiceHandler.DeleteArrayElement(property);
                                }
                                else
                                {
                                    property.objectReferenceValue = null;
                                }
                            }
                            else
                            {
                                property.objectReferenceValue = null;
                            }
                        }
                        else
                        {
                            obj = null;
                        }

                        GUI.changed = true;
                        evt.Use();
                    }

                    // Apparently we have to check for the character being space instead of the keyCode,
                    // otherwise the Inspector will maximize upon pressing space.
                    if (evt.MainActionKeyForControl(id))
                    {
                        var types = additionalType == null ? new Type[] { objType } : new Type[] { objType, additionalType };
                        if (property != null)
                        {
                            ObjectSelector.get.Show(types, property, allowSceneObjects);
                        }
                        else
                        {
                            ObjectSelector.get.Show(obj, types, objBeingEdited, allowSceneObjects);
                        }
                        ObjectSelector.get.objectSelectorID = id;
                        evt.Use();
                        GUIUtility.ExitGUI();
                    }
                }
                break;

            case EventType.Repaint:
                GUIContent temp;
                if (showMixedValue)
                {
                    temp = s_MixedValueContent;
                }
                else
                {
                    // If obj or objType are both null, we have to rely on
                    // property.objectReferenceStringValue to display None/Missing and the
                    // correct type. But if not, EditorGUIUtility.ObjectContent is more reliable.
                    // It can take a more specific object type specified as argument into account,
                    // and it gets the icon at the same time.
                    if (obj == null && objType == null && property != null)
                    {
                        temp = EditorGUIUtility.TempContent(property.objectReferenceStringValue);
                    }
                    else
                    {
                        // In order for ObjectContext to be able to distinguish between None/Missing,
                        // we need to supply an instanceID. For some reason, getting the instanceID
                        // from property.objectReferenceValue is not reliable, so we have to
                        // explicitly check property.objectReferenceInstanceIDValue if a property exists.
                        if (property != null)
                        {
                            temp = EditorGUIUtility.ObjectContent(obj, objType, property.objectReferenceInstanceIDValue);
                        }
                        else
                        {
                            temp = EditorGUIUtility.ObjectContent(obj, objType);
                        }
                    }

                    if (property != null)
                    {
                        if (obj != null)
                        {
                            Object[] references = { obj };
                            if (EditorSceneManager.preventCrossSceneReferences && CheckForCrossSceneReferencing(obj, property.serializedObject.targetObject))
                            {
                                if (!EditorApplication.isPlaying)
                                {
                                    temp = s_SceneMismatch;
                                }
                                else
                                {
                                    temp.text = temp.text + string.Format(" ({0})", GetGameObjectFromObject(obj).scene.name);
                                }
                            }
                            else if (validator(references, objType, property, ObjectFieldValidatorOptions.ExactObjectTypeValidation) == null)
                            {
                                temp = s_TypeMismatch;
                            }
                        }
                    }
                }

                switch (visualType)
                {
                case ObjectFieldVisualType.IconAndText:
                    BeginHandleMixedValueContentColor();
                    style.Draw(position, temp, id, DragAndDrop.activeControlID == id, position.Contains(Event.current.mousePosition));

                    Rect buttonRect = buttonStyle.margin.Remove(GetButtonRect(visualType, position));
                    buttonStyle.Draw(buttonRect, GUIContent.none, id, DragAndDrop.activeControlID == id, buttonRect.Contains(Event.current.mousePosition));
                    EndHandleMixedValueContentColor();
                    break;

                case ObjectFieldVisualType.LargePreview:
                    DrawObjectFieldLargeThumb(position, id, obj, temp);
                    break;

                case ObjectFieldVisualType.MiniPreview:
                    DrawObjectFieldMiniThumb(position, id, obj, temp);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                break;
            }

            EditorGUIUtility.SetIconSize(oldIconSize);

            return(obj);
        }
        private static void OnProjectWindowItemOnGUI(string guid, Rect selectionRect)
        {
            // Break - key modifier doen't pressed
            var activated = Event.current.alt;

            if (activated == false)
            {
                return;
            }

            // Break - OnGUI() call not for mouse target
            var within = selectionRect.Contains(Event.current.mousePosition);

            if (within == false)
            {
                return;
            }

            // Break - destination match one of sources
            var target          = AssetDatabase.GUIDToAssetPath(guid);
            var targetInSources = Array.IndexOf(DragAndDrop.paths, target) != -1;

            if (targetInSources)
            {
                return;
            }

            // Break - unity default moving
            var targetIsFolder = AssetDatabase.IsValidFolder(target);

            if (targetIsFolder)
            {
                foreach (var asset in DragAndDrop.objectReferences)
                {
                    if (AssetDatabase.IsMainAsset(asset))
                    {
                        return;
                    }
                }
            }

            // Break - there is Unity restriction to use GameObjects as SubAssets
            foreach (var obj in DragAndDrop.objectReferences)
            {
                if (obj is GameObject)
                {
                    return;
                }
            }

            if (Event.current.type == EventType.DragUpdated)
            {
                DragAndDrop.visualMode = DragAndDropVisualMode.Move;
                Event.current.Use();
            }
            else if (Event.current.type == EventType.DragPerform)
            {
                Move(DragAndDrop.objectReferences, target);
                DragAndDrop.AcceptDrag();
                Event.current.Use();
            }
        }
Ejemplo n.º 14
0
        void ListArea(Rect rect, PresetLibrary lib, object newPresetObject)
        {
            if (lib == null)
            {
                return;
            }

            Event evt = Event.current;

            if (m_PresetLibraryFileLocation == PresetFileLocation.ProjectFolder && evt.type == EventType.Repaint)
            {
                m_IsOpenForEdit = AssetDatabase.IsOpenForEdit(pathWithExtension, StatusQueryOptions.UseCachedIfPossible);
            }
            else if (m_PresetLibraryFileLocation == PresetFileLocation.PreferencesFolder)
            {
                m_IsOpenForEdit = true;
            }

            if (!m_IsOpenForEdit)
            {
                Rect versionControlRect = new Rect(rect.x, rect.yMax - versionControlAreaHeight, rect.width, versionControlAreaHeight);
                VersionControlArea(versionControlRect);
                rect.height -= versionControlAreaHeight;
            }

            // To ensure we setup grid to visible rect we need to run once to check if scrollbar is taking up screen estate.
            // To optimize the first width is based on the last frame and we therefore most likely will only run once.
            for (int i = 0; i < 2; i++)
            {
                gridWidth = m_ShowedScrollBarLastFrame ? rect.width - 17 : rect.width;
                SetupGrid(gridWidth, lib.Count());
                bool isShowingScrollBar = m_Grid.height > rect.height;
                if (isShowingScrollBar == m_ShowedScrollBarLastFrame)
                {
                    break;
                }
                else
                {
                    m_ShowedScrollBarLastFrame = isShowingScrollBar;
                }
            }

            // Draw horizontal lines for scrollview content to clip against
            if ((m_ShowedScrollBarLastFrame || alwaysShowScrollAreaHorizontalLines) && Event.current.type == EventType.Repaint)
            {
                Rect scrollEdgeRect = new RectOffset(1, 1, 1, 1).Add(rect);
                scrollEdgeRect.height = 1;
                EditorGUI.DrawRect(scrollEdgeRect, new Color(0, 0, 0, 0.3f));
                scrollEdgeRect.y += rect.height + 1;
                EditorGUI.DrawRect(scrollEdgeRect, new Color(0, 0, 0, 0.3f));
            }

            Rect contentRect = new Rect(0, 0, 1, m_Grid.height);

            m_State.m_ScrollPosition = GUI.BeginScrollView(rect, m_State.m_ScrollPosition, contentRect);
            {
                int   startIndex, endIndex;
                float yOffset                 = 0f;
                int   maxIndex                = m_ShowAddNewPresetItem ? lib.Count() : lib.Count() - 1;
                bool  isGridVisible           = m_Grid.IsVisibleInScrollView(rect.height, m_State.m_ScrollPosition.y, yOffset, maxIndex, out startIndex, out endIndex);
                bool  drawDragInsertionMarker = false;
                if (isGridVisible)
                {
                    // Handle renaming overlay before item handling because its needs mouse input first to end renaming if clicked outside
                    if (GetRenameOverlay().IsRenaming() && !GetRenameOverlay().isWaitingForDelay)
                    {
                        if (!m_State.m_RenameOverlay.OnGUI())
                        {
                            EndRename();
                            evt.Use();
                        }
                        Repaint();
                    }

                    for (int i = startIndex; i <= endIndex; ++i)
                    {
                        int itemControlID = i + 1000000;

                        Rect itemRect    = m_Grid.CalcRect(i, yOffset);
                        Rect previewRect = itemRect;
                        Rect labelRect   = itemRect;
                        switch (m_State.itemViewMode)
                        {
                        case PresetLibraryEditorState.ItemViewMode.List:
                            previewRect.width = m_State.m_PreviewHeight * m_PreviewAspect;
                            labelRect.x      += previewRect.width + 8f;
                            labelRect.width  -= previewRect.width + 10f;
                            labelRect.height  = kGridLabelHeight;
                            labelRect.y       = itemRect.yMin + (itemRect.height - kGridLabelHeight) * 0.5f;
                            break;

                        case PresetLibraryEditorState.ItemViewMode.Grid:
                            // only preview is shown: no label
                            break;
                        }

                        // Add new preset button
                        if (m_ShowAddNewPresetItem && i == lib.Count())
                        {
                            CreateNewPresetButton(previewRect, newPresetObject, lib, m_IsOpenForEdit);
                            continue;
                        }

                        // Rename overlay
                        bool isRenamingThisItem = IsRenaming(i);
                        if (isRenamingThisItem)
                        {
                            Rect renameRect = labelRect;
                            renameRect.y -= 1f; renameRect.x -= 1f; // adjustment to fit perfectly
                            m_State.m_RenameOverlay.editFieldRect = renameRect;
                        }

                        // Handle event
                        switch (evt.type)
                        {
                        case EventType.Repaint:
                            if (m_State.m_HoverIndex == i)
                            {
                                if (itemRect.Contains(evt.mousePosition))
                                {
                                    // TODO: We need a better hover effect so disabling for now...
                                    //if (!GetRenameOverlay().IsRenaming ())
                                    //  DrawHoverEffect (itemRect, false);
                                }
                                else
                                {
                                    m_State.m_HoverIndex = -1;
                                }
                            }

                            if (m_DragState.draggingIndex == i || GUIUtility.hotControl == itemControlID)
                            {
                                DrawHoverEffect(itemRect, false);
                            }

                            lib.Draw(previewRect, i);
                            if (!isRenamingThisItem && drawLabels)
                            {
                                GUI.Label(labelRect, GUIContent.Temp(lib.GetName(i)));
                            }

                            if (m_DragState.dragUponIndex == i && m_DragState.draggingIndex != m_DragState.dragUponIndex)
                            {
                                drawDragInsertionMarker = true;
                            }

                            // We delete presets on alt-click
                            if (GUIUtility.hotControl == 0 && Event.current.alt && m_IsOpenForEdit)
                            {
                                EditorGUIUtility.AddCursorRect(itemRect, MouseCursor.ArrowMinus);
                            }

                            break;

                        case EventType.MouseDown:
                            if (evt.button == 0 && itemRect.Contains(evt.mousePosition))
                            {
                                GUIUtility.hotControl = itemControlID;
                                if (evt.clickCount == 1)
                                {
                                    m_ItemClickedCallback(evt.clickCount, lib.GetPreset(i));
                                    evt.Use();
                                }
                            }
                            break;

                        case EventType.MouseDrag:
                            if (GUIUtility.hotControl == itemControlID && m_IsOpenForEdit)
                            {
                                DragAndDropDelay delay = (DragAndDropDelay)GUIUtility.GetStateObject(typeof(DragAndDropDelay), itemControlID);
                                if (delay.CanStartDrag())
                                {
                                    // Start drag
                                    DragAndDrop.PrepareStartDrag();
                                    DragAndDrop.SetGenericData("DraggingPreset", i);
                                    DragAndDrop.StartDrag("");
                                    m_DragState.draggingIndex = i;
                                    m_DragState.dragUponIndex = i;
                                    GUIUtility.hotControl     = 0;
                                }
                                evt.Use();
                            }
                            break;

                        case EventType.DragUpdated:
                        case EventType.DragPerform:
                        {
                            Rect dragRect = GetDragRect(itemRect);
                            if (dragRect.Contains(evt.mousePosition))
                            {
                                m_DragState.dragUponIndex = i;
                                m_DragState.dragUponRect  = itemRect;

                                if (m_State.itemViewMode == PresetLibraryEditorState.ItemViewMode.List)
                                {
                                    m_DragState.insertAfterIndex = ((evt.mousePosition.y - dragRect.y) / dragRect.height) > 0.5f;
                                }
                                else
                                {
                                    m_DragState.insertAfterIndex = ((evt.mousePosition.x - dragRect.x) / dragRect.width) > 0.5f;
                                }

                                bool perform = evt.type == EventType.DragPerform;
                                if (perform)
                                {
                                    if (m_DragState.draggingIndex >= 0)
                                    {
                                        MovePreset(m_DragState.draggingIndex, m_DragState.dragUponIndex, m_DragState.insertAfterIndex);
                                        DragAndDrop.AcceptDrag();
                                    }
                                    ClearDragState();
                                }
                                DragAndDrop.visualMode = DragAndDropVisualMode.Move;
                                evt.Use();
                            }
                        }
                        break;

                        case EventType.DragExited:
                            if (m_DragState.IsDragging())
                            {
                                ClearDragState();
                                evt.Use();
                            }
                            break;

                        case EventType.MouseUp:
                            if (GUIUtility.hotControl == itemControlID)
                            {
                                GUIUtility.hotControl = 0;
                                if (evt.button == 0 && itemRect.Contains(evt.mousePosition))
                                {
                                    if (Event.current.alt && m_IsOpenForEdit)
                                    {
                                        DeletePreset(i);
                                        evt.Use();
                                    }
                                }
                            }
                            break;

                        case EventType.ContextClick:
                            if (itemRect.Contains(evt.mousePosition))
                            {
                                PresetContextMenu.Show(m_IsOpenForEdit, i, newPresetObject, this);
                                evt.Use();
                            }
                            break;

                        case EventType.MouseMove:
                            if (itemRect.Contains(evt.mousePosition))
                            {
                                if (m_State.m_HoverIndex != i)
                                {
                                    m_State.m_HoverIndex = i;
                                    Repaint();
                                }
                            }
                            else if (m_State.m_HoverIndex == i)
                            {
                                m_State.m_HoverIndex = -1;
                                Repaint();
                            }

                            break;
                        }
                    } // end foreach item

                    // Draw above all items
                    if (drawDragInsertionMarker)
                    {
                        DrawDragInsertionMarker();
                    }
                }
            } GUI.EndScrollView();
        }
Ejemplo n.º 15
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;
        }
Ejemplo n.º 16
0
        public void OnSceneDrag(SceneView sceneView)
        {
            GameObject target = this.target as GameObject;

            switch (PrefabUtility.GetPrefabType(target))
            {
            case PrefabType.Prefab:
            case PrefabType.ModelPrefab:
            {
                Event     current = Event.current;
                EventType type    = current.type;
                if (type != EventType.DragUpdated)
                {
                    if (type == EventType.DragPerform)
                    {
                        string uniqueNameForSibling = GameObjectUtility.GetUniqueNameForSibling(null, dragObject.name);
                        dragObject.hideFlags = HideFlags.None;
                        Undo.RegisterCreatedObjectUndo(dragObject, "Place " + dragObject.name);
                        EditorUtility.SetDirty(dragObject);
                        DragAndDrop.AcceptDrag();
                        Selection.activeObject             = dragObject;
                        HandleUtility.ignoreRaySnapObjects = null;
                        EditorWindow.mouseOverWindow.Focus();
                        dragObject.name = uniqueNameForSibling;
                        dragObject      = null;
                        current.Use();
                        return;
                    }
                    if ((type == EventType.DragExited) && (dragObject != null))
                    {
                        UnityEngine.Object.DestroyImmediate(dragObject, false);
                        HandleUtility.ignoreRaySnapObjects = null;
                        dragObject = null;
                        current.Use();
                    }
                }
                else
                {
                    if (dragObject == null)
                    {
                        dragObject = (GameObject)PrefabUtility.InstantiatePrefab(PrefabUtility.FindPrefabRoot(target));
                        HandleUtility.ignoreRaySnapObjects = dragObject.GetComponentsInChildren <Transform>();
                        dragObject.hideFlags = HideFlags.HideInHierarchy;
                        dragObject.name      = target.name;
                    }
                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                    object obj3 = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(current.mousePosition));
                    if (obj3 != null)
                    {
                        RaycastHit hit = (RaycastHit)obj3;
                        float      num = 0f;
                        if (Tools.pivotMode == PivotMode.Center)
                        {
                            float num2 = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, hit.normal);
                            if (num2 != float.PositiveInfinity)
                            {
                                num = Vector3.Dot(dragObject.transform.position, hit.normal) - num2;
                            }
                        }
                        dragObject.transform.position = Matrix4x4.identity.MultiplyPoint(hit.point + ((Vector3)(hit.normal * num)));
                    }
                    else
                    {
                        dragObject.transform.position = HandleUtility.GUIPointToWorldRay(current.mousePosition).GetPoint(10f);
                    }
                    if (sceneView.in2DMode)
                    {
                        Vector3 position = dragObject.transform.position;
                        position.z = PrefabUtility.FindPrefabRoot(target).transform.position.z;
                        dragObject.transform.position = position;
                    }
                    current.Use();
                }
                break;
            }
            }
        }
Ejemplo n.º 17
0
        private void ListArea(Rect rect, PresetLibrary lib, object newPresetObject)
        {
            if ((UnityEngine.Object)lib == (UnityEngine.Object)null)
            {
                return;
            }
            Event current = Event.current;

            if (this.m_PresetLibraryFileLocation == PresetFileLocation.ProjectFolder && current.type == EventType.Repaint)
            {
                this.m_IsOpenForEdit = AssetDatabase.IsOpenForEdit(this.pathWithExtension);
            }
            else if (this.m_PresetLibraryFileLocation == PresetFileLocation.PreferencesFolder)
            {
                this.m_IsOpenForEdit = true;
            }
            if (!this.m_IsOpenForEdit)
            {
                this.VersionControlArea(new Rect(rect.x, rect.yMax - this.versionControlAreaHeight, rect.width, this.versionControlAreaHeight));
                rect.height -= this.versionControlAreaHeight;
            }
            for (int index = 0; index < 2; ++index)
            {
                this.gridWidth = !this.m_ShowedScrollBarLastFrame ? rect.width : rect.width - 17f;
                this.SetupGrid(this.gridWidth, lib.Count());
                bool flag = (double)this.m_Grid.height > (double)rect.height;
                if (flag != this.m_ShowedScrollBarLastFrame)
                {
                    this.m_ShowedScrollBarLastFrame = flag;
                }
                else
                {
                    break;
                }
            }
            if ((this.m_ShowedScrollBarLastFrame || this.alwaysShowScrollAreaHorizontalLines) && Event.current.type == EventType.Repaint)
            {
                Rect rect1 = new RectOffset(1, 1, 1, 1).Add(rect);
                rect1.height = 1f;
                EditorGUI.DrawRect(rect1, new Color(0.0f, 0.0f, 0.0f, 0.3f));
                rect1.y += rect.height + 1f;
                EditorGUI.DrawRect(rect1, new Color(0.0f, 0.0f, 0.0f, 0.3f));
            }
            Rect viewRect = new Rect(0.0f, 0.0f, 1f, this.m_Grid.height);

            this.m_State.m_ScrollPosition = GUI.BeginScrollView(rect, this.m_State.m_ScrollPosition, viewRect);
            float num      = 0.0f;
            int   maxIndex = !this.m_ShowAddNewPresetItem ? lib.Count() - 1 : lib.Count();
            int   startIndex;
            int   endIndex;
            bool  flag1 = this.m_Grid.IsVisibleInScrollView(rect.height, this.m_State.m_ScrollPosition.y, num, maxIndex, out startIndex, out endIndex);
            bool  flag2 = false;

            if (flag1)
            {
                if (this.GetRenameOverlay().IsRenaming() && !this.GetRenameOverlay().isWaitingForDelay)
                {
                    if (!this.m_State.m_RenameOverlay.OnGUI())
                    {
                        this.EndRename();
                        current.Use();
                    }
                    this.Repaint();
                }
                for (int index = startIndex; index <= endIndex; ++index)
                {
                    int  controlID = index + 1000000;
                    Rect rect1     = this.m_Grid.CalcRect(index, num);
                    Rect rect2     = rect1;
                    Rect position  = rect1;
                    switch (this.m_State.itemViewMode)
                    {
                    case PresetLibraryEditorState.ItemViewMode.List:
                        rect2.width     = this.m_State.m_PreviewHeight * this.m_PreviewAspect;
                        position.x     += rect2.width + 8f;
                        position.width -= rect2.width + 10f;
                        position.height = 16f;
                        position.y      = rect1.yMin + (float)(((double)rect1.height - 16.0) * 0.5);
                        break;
                    }
                    if (this.m_ShowAddNewPresetItem && index == lib.Count())
                    {
                        this.CreateNewPresetButton(rect2, newPresetObject, lib, this.m_IsOpenForEdit);
                    }
                    else
                    {
                        bool flag3 = this.IsRenaming(index);
                        if (flag3)
                        {
                            Rect rect3 = position;
                            --rect3.y;
                            --rect3.x;
                            this.m_State.m_RenameOverlay.editFieldRect = rect3;
                        }
                        switch (current.type)
                        {
                        case EventType.MouseDown:
                            if (current.button == 0 && rect1.Contains(current.mousePosition))
                            {
                                GUIUtility.hotControl = controlID;
                                if (current.clickCount == 1)
                                {
                                    this.m_ItemClickedCallback(current.clickCount, lib.GetPreset(index));
                                    current.Use();
                                    continue;
                                }
                                continue;
                            }
                            continue;

                        case EventType.MouseUp:
                            if (GUIUtility.hotControl == controlID)
                            {
                                GUIUtility.hotControl = 0;
                                if (current.button == 0 && rect1.Contains(current.mousePosition) && (Event.current.alt && this.m_IsOpenForEdit))
                                {
                                    this.DeletePreset(index);
                                    current.Use();
                                    continue;
                                }
                                continue;
                            }
                            continue;

                        case EventType.MouseMove:
                            if (rect1.Contains(current.mousePosition))
                            {
                                if (this.m_State.m_HoverIndex != index)
                                {
                                    this.m_State.m_HoverIndex = index;
                                    this.Repaint();
                                    continue;
                                }
                                continue;
                            }
                            if (this.m_State.m_HoverIndex == index)
                            {
                                this.m_State.m_HoverIndex = -1;
                                this.Repaint();
                                continue;
                            }
                            continue;

                        case EventType.MouseDrag:
                            if (GUIUtility.hotControl == controlID && this.m_IsOpenForEdit)
                            {
                                if (((DragAndDropDelay)GUIUtility.GetStateObject(typeof(DragAndDropDelay), controlID)).CanStartDrag())
                                {
                                    DragAndDrop.PrepareStartDrag();
                                    DragAndDrop.SetGenericData("DraggingPreset", (object)index);
                                    DragAndDrop.objectReferences = new UnityEngine.Object[0];
                                    DragAndDrop.StartDrag(string.Empty);
                                    this.m_DragState.draggingIndex = index;
                                    this.m_DragState.dragUponIndex = index;
                                    GUIUtility.hotControl          = 0;
                                }
                                current.Use();
                                continue;
                            }
                            continue;

                        case EventType.Repaint:
                            if (this.m_State.m_HoverIndex == index && !rect1.Contains(current.mousePosition))
                            {
                                this.m_State.m_HoverIndex = -1;
                            }
                            if (this.m_DragState.draggingIndex == index || GUIUtility.hotControl == controlID)
                            {
                                this.DrawHoverEffect(rect1, false);
                            }
                            lib.Draw(rect2, index);
                            if (!flag3 && this.drawLabels)
                            {
                                GUI.Label(position, GUIContent.Temp(lib.GetName(index)));
                            }
                            if (this.m_DragState.dragUponIndex == index && this.m_DragState.draggingIndex != this.m_DragState.dragUponIndex)
                            {
                                flag2 = true;
                            }
                            if (GUIUtility.hotControl == 0 && Event.current.alt && this.m_IsOpenForEdit)
                            {
                                EditorGUIUtility.AddCursorRect(rect1, MouseCursor.ArrowMinus);
                                continue;
                            }
                            continue;

                        case EventType.DragUpdated:
                        case EventType.DragPerform:
                            Rect dragRect = this.GetDragRect(rect1);
                            if (dragRect.Contains(current.mousePosition))
                            {
                                this.m_DragState.dragUponIndex    = index;
                                this.m_DragState.dragUponRect     = rect1;
                                this.m_DragState.insertAfterIndex = this.m_State.itemViewMode != PresetLibraryEditorState.ItemViewMode.List ? ((double)current.mousePosition.x - (double)dragRect.x) / (double)dragRect.width > 0.5 : ((double)current.mousePosition.y - (double)dragRect.y) / (double)dragRect.height > 0.5;
                                if (current.type == EventType.DragPerform)
                                {
                                    if (this.m_DragState.draggingIndex >= 0)
                                    {
                                        this.MovePreset(this.m_DragState.draggingIndex, this.m_DragState.dragUponIndex, this.m_DragState.insertAfterIndex);
                                        DragAndDrop.AcceptDrag();
                                    }
                                    this.ClearDragState();
                                }
                                DragAndDrop.visualMode = DragAndDropVisualMode.Move;
                                current.Use();
                                continue;
                            }
                            continue;

                        case EventType.DragExited:
                            if (this.m_DragState.IsDragging())
                            {
                                this.ClearDragState();
                                current.Use();
                                continue;
                            }
                            continue;

                        case EventType.ContextClick:
                            if (rect1.Contains(current.mousePosition))
                            {
                                PresetLibraryEditor <T> .PresetContextMenu.Show(this.m_IsOpenForEdit, index, newPresetObject, this);

                                current.Use();
                                continue;
                            }
                            continue;

                        default:
                            continue;
                        }
                    }
                }
                if (flag2)
                {
                    this.DrawDragInsertionMarker();
                }
            }
            GUI.EndScrollView();
        }
Ejemplo n.º 18
0
        internal static Object DoDropField(Rect position, int id, System.Type objType, ObjectFieldValidator validator, bool allowSceneObjects, GUIStyle style)
        {
            if (validator == null)
            {
                validator = ValidateObjectFieldAssignment;
            }
            Event     evt       = Event.current;
            EventType eventType = evt.type;

            // special case test, so we continue to ping/select objects with the object field disabled
            if (!GUI.enabled && GUIClip.enabled && (Event.current.rawType == EventType.MouseDown))
            {
                eventType = Event.current.rawType;
            }

            switch (eventType)
            {
            case EventType.DragExited:
                if (GUI.enabled)
                {
                    HandleUtility.Repaint();
                }
                break;

            case EventType.DragUpdated:
            case EventType.DragPerform:

                if (position.Contains(Event.current.mousePosition) && GUI.enabled)
                {
                    Object[] references      = DragAndDrop.objectReferences;
                    Object   validatedObject = validator(references, objType, null, ObjectFieldValidatorOptions.None);

                    if (validatedObject != null)
                    {
                        // If scene objects are not allowed and object is a scene object then clear
                        if (!allowSceneObjects && !EditorUtility.IsPersistent(validatedObject))
                        {
                            validatedObject = null;
                        }
                    }

                    if (validatedObject != null)
                    {
                        DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
                        if (eventType == EventType.DragPerform)
                        {
                            GUI.changed = true;
                            DragAndDrop.AcceptDrag();
                            DragAndDrop.activeControlID = 0;
                            Event.current.Use();
                            return(validatedObject);
                        }
                        else
                        {
                            DragAndDrop.activeControlID = id;
                            Event.current.Use();
                        }
                    }
                }
                break;

            case EventType.Repaint:
                style.Draw(position, GUIContent.none, id, DragAndDrop.activeControlID == id);
                break;
            }
            return(null);
        }
Ejemplo n.º 19
0
        public void OnSceneDrag(SceneView sceneView)
        {
            GameObject target = this.target as GameObject;

            switch (PrefabUtility.GetPrefabType((UnityEngine.Object)target))
            {
            case PrefabType.Prefab:
            case PrefabType.ModelPrefab:
                Event current = Event.current;
                switch (current.type)
                {
                case EventType.DragUpdated:
                    if ((UnityEngine.Object)GameObjectInspector.dragObject == (UnityEngine.Object)null)
                    {
                        GameObjectInspector.dragObject           = (GameObject)PrefabUtility.InstantiatePrefab((UnityEngine.Object)PrefabUtility.FindPrefabRoot(target));
                        HandleUtility.ignoreRaySnapObjects       = GameObjectInspector.dragObject.GetComponentsInChildren <Transform>();
                        GameObjectInspector.dragObject.hideFlags = HideFlags.HideInHierarchy;
                        GameObjectInspector.dragObject.name      = target.name;
                    }
                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                    object obj = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(current.mousePosition));
                    if (obj != null)
                    {
                        RaycastHit raycastHit = (RaycastHit)obj;
                        float      num1       = 0.0f;
                        if (Tools.pivotMode == PivotMode.Center)
                        {
                            float num2 = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, raycastHit.normal);
                            if ((double)num2 != double.PositiveInfinity)
                            {
                                num1 = Vector3.Dot(GameObjectInspector.dragObject.transform.position, raycastHit.normal) - num2;
                            }
                        }
                        GameObjectInspector.dragObject.transform.position = Matrix4x4.identity.MultiplyPoint(raycastHit.point + raycastHit.normal * num1);
                    }
                    else
                    {
                        GameObjectInspector.dragObject.transform.position = HandleUtility.GUIPointToWorldRay(current.mousePosition).GetPoint(10f);
                    }
                    if (sceneView.in2DMode)
                    {
                        Vector3 position = GameObjectInspector.dragObject.transform.position;
                        position.z = PrefabUtility.FindPrefabRoot(target).transform.position.z;
                        GameObjectInspector.dragObject.transform.position = position;
                    }
                    current.Use();
                    return;

                case EventType.DragPerform:
                    string uniqueNameForSibling = GameObjectUtility.GetUniqueNameForSibling((Transform)null, GameObjectInspector.dragObject.name);
                    GameObjectInspector.dragObject.hideFlags = HideFlags.None;
                    Undo.RegisterCreatedObjectUndo((UnityEngine.Object)GameObjectInspector.dragObject, "Place " + GameObjectInspector.dragObject.name);
                    EditorUtility.SetDirty((UnityEngine.Object)GameObjectInspector.dragObject);
                    DragAndDrop.AcceptDrag();
                    Selection.activeObject             = (UnityEngine.Object)GameObjectInspector.dragObject;
                    HandleUtility.ignoreRaySnapObjects = (Transform[])null;
                    EditorWindow.mouseOverWindow.Focus();
                    GameObjectInspector.dragObject.name = uniqueNameForSibling;
                    GameObjectInspector.dragObject      = (GameObject)null;
                    current.Use();
                    return;

                case EventType.DragExited:
                    if (!(bool)((UnityEngine.Object)GameObjectInspector.dragObject))
                    {
                        return;
                    }
                    UnityEngine.Object.DestroyImmediate((UnityEngine.Object)GameObjectInspector.dragObject, false);
                    HandleUtility.ignoreRaySnapObjects = (Transform[])null;
                    GameObjectInspector.dragObject     = (GameObject)null;
                    current.Use();
                    return;

                default:
                    return;
                }
            }
        }
Ejemplo n.º 20
0
        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();
        }
Ejemplo n.º 21
0
        private void HandleMouseInput()
        {
            List <CubemapInfo> cubemapList = m_LookDevView.envLibrary.hdriList;

            Vector2 scrollFixedPosition = new Vector2(Event.current.mousePosition.x, Event.current.mousePosition.y + m_ScrollPosition.y);

            Event evt = Event.current;

            switch (evt.GetTypeForControl(m_LookDevView.hotControl))
            {
            // Update overlay position for next repaint event
            case EventType.MouseDrag:
            {
                if (m_SelectedCubeMapOffsetIndex != -1)
                {
                    Undo.RecordObject(m_LookDevView.envLibrary, "");
                    CubemapInfo info = cubemapList[m_SelectedCubeMapOffsetIndex];
                    info.angleOffset = ComputeAngleOffsetFromMouseCoord(scrollFixedPosition) + m_SelectedCubeMapOffsetValue;
                    m_LookDevView.envLibrary.dirty = true;
                    Event.current.Use();
                }

                if (m_SelectedCubemapInfo != null)
                {
                    if (IsPositionInInsertionArea(scrollFixedPosition) == -1)
                    {
                        m_HoveringCubeMapIndex = IsPositionInThumbnailArea(scrollFixedPosition);
                    }
                    else
                    {
                        m_HoveringCubeMapIndex = -1;
                    }
                }

                m_LookDevView.Repaint();
                break;
            }

            // Handles environment drop
            case EventType.MouseUp:
            {
                if (m_SelectedCubemap != null)
                {
                    // The rect needs to include an extra slot when moving to last position
                    Rect extendedGUIRect = m_GUIRect;
                    extendedGUIRect.yMax += EditorGUI.kSingleLineHeight;

                    if (extendedGUIRect.Contains(Event.current.mousePosition))
                    {
                        int insertionRectIndex = IsPositionInInsertionArea(scrollFixedPosition);
                        if (insertionRectIndex != -1)
                        {
                            // Must be called before we do any modification to HDRI list
                            ResetShadowCubemap();

                            m_LookDevView.envLibrary.InsertHDRI(m_SelectedCubemap, insertionRectIndex);
                        }
                        else
                        {
                            int thumbnailRectIndex = IsPositionInThumbnailArea(scrollFixedPosition);
                            if (thumbnailRectIndex != -1 && m_LookDevView.config.enableShadowCubemap)
                            {
                                Undo.RecordObject(m_LookDevView.envLibrary, "Update shadow cubemap");
                                CubemapInfo cubemapInfo = m_LookDevView.envLibrary.hdriList[thumbnailRectIndex];

                                // We don't want the user to drop a cubemap on itself and reset the shadows (it would happen all the time by mistake)
                                if (cubemapInfo != m_SelectedCubemapInfo)
                                {
                                    cubemapInfo.SetCubemapShadowInfo(m_SelectedCubemapInfo);
                                }
                                m_LookDevView.envLibrary.dirty = true;
                            }
                        }
                        CancelSelection();
                    }
                }

                m_LookDevView.Repaint();

                if (m_SelectedCubeMapOffsetIndex != -1)
                {
                    // Fall back to zero when near the center
                    if (Mathf.Abs(cubemapList[m_SelectedCubeMapOffsetIndex].angleOffset) <= 10.0f)
                    {
                        Undo.RecordObject(m_LookDevView.envLibrary, "");
                        cubemapList[m_SelectedCubeMapOffsetIndex].angleOffset = 0.0f;
                        m_LookDevView.envLibrary.dirty = true;
                    }
                }
                m_SelectedCubemapInfo            = null;
                m_SelectedShadowCubemapOwnerInfo = null;
                m_SelectedLightIconIndex         = -1;
                m_SelectedShadowInfo             = null;
                m_SelectedCubeMapOffsetIndex     = -1;
                m_HoveringCubeMapIndex           = -1;
                m_SelectedCubeMapOffsetValue     = 0.0f;

                GUIUtility.hotControl = 0;

                break;
            }

            // Escape closes the window
            case EventType.KeyDown:
            {
                if (Event.current.keyCode == KeyCode.Escape)
                {
                    CancelSelection();
                    m_LookDevView.Repaint();
                }
                break;
            }

            case EventType.DragPerform:
            {
                int insertionIndex = IsPositionInInsertionArea(scrollFixedPosition);

                foreach (UnityEngine.Object o in DragAndDrop.objectReferences)
                {
                    Cubemap cubemap = o as Cubemap;
                    if (cubemap)
                    {
                        // When insertion outside the list the index is -1 which mean in InsertHDRI that it will be add at the end
                        m_LookDevView.envLibrary.InsertHDRI(cubemap, insertionIndex);
                    }
                }

                DragAndDrop.AcceptDrag();
                m_DragBeingPerformed = false;
                evt.Use();
                break;
            }

            case EventType.DragUpdated:
            {
                bool hasCubemap = false;
                foreach (UnityEngine.Object o in DragAndDrop.objectReferences)
                {
                    Cubemap cubemap = o as Cubemap;
                    if (cubemap)
                    {
                        hasCubemap = true;
                    }
                }
                DragAndDrop.visualMode = hasCubemap ? DragAndDropVisualMode.Link : DragAndDropVisualMode.Rejected;
                if (hasCubemap)
                {
                    m_DragBeingPerformed = true;
                }
                evt.Use();
            }
            break;

            case EventType.DragExited:
                break;

            case EventType.Repaint:

                if (m_SelectedCubeMapOffsetIndex != -1)
                {
                    EditorGUIUtility.AddCursorRect(m_displayRect, MouseCursor.SlideArrow);
                }
                break;
            }
        }