private void OnSceneGUIDelegate(SceneView sceneView)
        {
            Event evt = Event.current;

            if (evt.type != EventType.DragUpdated && evt.type != EventType.DragPerform && evt.type != EventType.DragExited && evt.type != EventType.Repaint)
            {
                return;
            }

            Grid activeGrid = GetActiveGrid();

            if (activeGrid == null || DragAndDrop.objectReferences.Length == 0)
            {
                return;
            }

            Vector3    localMouse        = GridEditorUtility.ScreenToLocal(activeGrid.transform, evt.mousePosition);
            Vector3Int mouseGridPosition = activeGrid.LocalToCell(localMouse);

            switch (evt.type)
            {
            //TODO: Cache this
            case EventType.DragUpdated:
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                List <TileBase> tiles = TileDragAndDrop.GetValidTiles(DragAndDrop.objectReferences);
                instance.m_HoverData = TileDragAndDrop.CreateHoverData(null, null, tiles);
                if (instance.m_HoverData.Count > 0)
                {
                    Event.current.Use();
                    GUI.changed = true;
                }
                break;

            case EventType.DragPerform:
                if (instance.m_HoverData.Count > 0)
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                    Dictionary <Vector2Int, TileBase> tileSheet = TileDragAndDrop.ConvertToTileSheet(instance.m_HoverData);
                    Tilemap tilemap = GetOrCreateActiveTilemap();
                    tilemap.ClearAllEditorPreviewTiles();
                    foreach (KeyValuePair <Vector2Int, TileBase> item in tileSheet)
                    {
                        Vector3Int position = new Vector3Int(mouseGridPosition.x + item.Key.x, mouseGridPosition.y + item.Key.y, 0);
                        tilemap.SetTile(position, item.Value);
                    }
                    instance.m_HoverData = null;
                    GUI.changed          = true;
                    Event.current.Use();
                }
                break;

            case EventType.Repaint:
                if (instance.m_HoverData != null)
                {
                    Tilemap map = Selection.activeGameObject.GetComponentInParent <Tilemap>();

                    if (map != null)
                    {
                        map.ClearAllEditorPreviewTiles();
                    }

                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                    foreach (KeyValuePair <Vector2Int, Object> item in instance.m_HoverData)
                    {
                        Vector3Int gridPos = mouseGridPosition + new Vector3Int(item.Key.x, item.Key.y, 0);
                        if (item.Value is TileBase)
                        {
                            TileBase tile = item.Value as TileBase;
                            if (map != null)
                            {
                                map.SetEditorPreviewTile(gridPos, tile);
                            }
                        }
                    }
                }
                break;
            }

            if (instance.m_HoverData != null && (
                    Event.current.type == EventType.DragExited ||
                    Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape))
            {
                if (instance.m_HoverData.Count > 0)
                {
                    Tilemap map = Selection.activeGameObject.GetComponentInParent <Tilemap>();
                    if (map != null)
                    {
                        map.ClearAllEditorPreviewTiles();
                    }

                    Event.current.Use();
                }

                instance.m_HoverData = null;
            }
        }
 public void HandleDragAndDrop()
 {
     if (DragAndDrop.objectReferences.Length != 0 && this.guiRect.Contains(Event.current.mousePosition))
     {
         EventType type = Event.current.type;
         if (type != EventType.DragUpdated)
         {
             if (type != EventType.DragPerform)
             {
                 if (type != EventType.Repaint)
                 {
                 }
             }
             else
             {
                 if (this.m_HoverData == null || this.m_HoverData.Count == 0)
                 {
                     return;
                 }
                 this.RegisterUndo();
                 bool       flag = GridPaintPaletteClipboard.TilemapIsEmpty(this.tilemap);
                 Vector2Int mouseGridPosition = base.mouseGridPosition;
                 DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                 Dictionary <Vector2Int, TileBase> dictionary = TileDragAndDrop.ConvertToTileSheet(this.m_HoverData);
                 foreach (KeyValuePair <Vector2Int, TileBase> current in dictionary)
                 {
                     this.SetTile(this.tilemap, mouseGridPosition + current.Key, current.Value, Color.white, Matrix4x4.identity);
                 }
                 this.OnPaletteChanged();
                 this.m_PaletteNeedsSave = true;
                 this.FlushHoverData();
                 GUI.changed = true;
                 this.SavePaletteIfNecessary();
                 if (flag)
                 {
                     this.ResetPreviewInstance();
                     this.FrameEntirePalette();
                 }
                 Event.current.Use();
                 GUIUtility.ExitGUI();
             }
         }
         else
         {
             List <Texture2D> validSpritesheets  = TileDragAndDrop.GetValidSpritesheets(DragAndDrop.objectReferences);
             List <Sprite>    validSingleSprites = TileDragAndDrop.GetValidSingleSprites(DragAndDrop.objectReferences);
             List <TileBase>  validTiles         = TileDragAndDrop.GetValidTiles(DragAndDrop.objectReferences);
             this.m_HoverData = TileDragAndDrop.CreateHoverData(validSpritesheets, validSingleSprites, validTiles);
             if (this.m_HoverData != null && this.m_HoverData.Count > 0)
             {
                 DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                 Event.current.Use();
                 GUI.changed = true;
             }
         }
         if (this.m_HoverData != null && (Event.current.type == EventType.DragExited || (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape)))
         {
             DragAndDrop.visualMode = DragAndDropVisualMode.None;
             this.FlushHoverData();
             Event.current.Use();
         }
     }
 }
        public void HandleDragAndDrop()
        {
            if (DragAndDrop.objectReferences.Length == 0 || !guiRect.Contains(Event.current.mousePosition))
            {
                return;
            }

            switch (Event.current.type)
            {
            //TODO: Cache this
            case EventType.DragUpdated:
            {
                List <Texture2D> sheets  = TileDragAndDrop.GetValidSpritesheets(DragAndDrop.objectReferences);
                List <Sprite>    sprites = TileDragAndDrop.GetValidSingleSprites(DragAndDrop.objectReferences);
                List <TileBase>  tiles   = TileDragAndDrop.GetValidTiles(DragAndDrop.objectReferences);
                m_HoverData = TileDragAndDrop.CreateHoverData(sheets, sprites, tiles);

                if (m_HoverData != null && m_HoverData.Count > 0)
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                    Event.current.Use();
                    GUI.changed = true;
                }
            }
            break;

            case EventType.DragPerform:
            {
                if (m_HoverData == null || m_HoverData.Count == 0)
                {
                    return;
                }

                RegisterUndo();

                bool wasEmpty = TilemapIsEmpty(tilemap);

                Vector2Int targetPosition = mouseGridPosition;
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                Dictionary <Vector2Int, TileBase> tileSheet = TileDragAndDrop.ConvertToTileSheet(m_HoverData);
                foreach (KeyValuePair <Vector2Int, TileBase> item in tileSheet)
                {
                    SetTile(tilemap, targetPosition + item.Key, item.Value, Color.white, Matrix4x4.identity);
                }

                OnPaletteChanged();

                m_PaletteNeedsSave = true;
                FlushHoverData();
                GUI.changed = true;
                SavePaletteIfNecessary();

                if (wasEmpty)
                {
                    ResetPreviewInstance();
                    FrameEntirePalette();
                }

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

            case EventType.Repaint:
                // Handled in Render()
                break;
            }

            if (m_HoverData != null && (
                    Event.current.type == EventType.DragExited ||
                    Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape))
            {
                DragAndDrop.visualMode = DragAndDropVisualMode.None;
                FlushHoverData();
                Event.current.Use();
            }
        }
Exemple #4
0
        private void OnSceneGUIDelegate(SceneView sceneView)
        {
            Event current = Event.current;

            if (current.type == EventType.DragUpdated || current.type == EventType.DragPerform || current.type == EventType.DragExited || current.type == EventType.Repaint)
            {
                Grid activeGrid = TileDragAndDropManager.GetActiveGrid();
                if (!(activeGrid == null) && DragAndDrop.objectReferences.Length != 0)
                {
                    Vector3    localPosition = GridEditorUtility.ScreenToLocal(activeGrid.transform, current.mousePosition);
                    Vector3Int a             = activeGrid.LocalToCell(localPosition);
                    EventType  type          = current.type;
                    if (type != EventType.DragUpdated)
                    {
                        if (type != EventType.DragPerform)
                        {
                            if (type == EventType.Repaint)
                            {
                                if (ScriptableSingleton <TileDragAndDropManager> .instance.m_HoverData != null)
                                {
                                    Tilemap componentInParent = Selection.activeGameObject.GetComponentInParent <Tilemap>();
                                    if (componentInParent != null)
                                    {
                                        componentInParent.ClearAllEditorPreviewTiles();
                                    }
                                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                                    foreach (KeyValuePair <Vector2Int, UnityEngine.Object> current2 in ScriptableSingleton <TileDragAndDropManager> .instance.m_HoverData)
                                    {
                                        Vector3Int position = a + new Vector3Int(current2.Key.x, current2.Key.y, 0);
                                        if (current2.Value is TileBase)
                                        {
                                            TileBase tile = current2.Value as TileBase;
                                            if (componentInParent != null)
                                            {
                                                componentInParent.SetEditorPreviewTile(position, tile);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else if (ScriptableSingleton <TileDragAndDropManager> .instance.m_HoverData.Count > 0)
                        {
                            DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                            Dictionary <Vector2Int, TileBase> dictionary = TileDragAndDrop.ConvertToTileSheet(ScriptableSingleton <TileDragAndDropManager> .instance.m_HoverData);
                            Tilemap orCreateActiveTilemap = TileDragAndDropManager.GetOrCreateActiveTilemap();
                            orCreateActiveTilemap.ClearAllEditorPreviewTiles();
                            foreach (KeyValuePair <Vector2Int, TileBase> current3 in dictionary)
                            {
                                Vector3Int position2 = new Vector3Int(a.x + current3.Key.x, a.y + current3.Key.y, 0);
                                orCreateActiveTilemap.SetTile(position2, current3.Value);
                            }
                            ScriptableSingleton <TileDragAndDropManager> .instance.m_HoverData = null;
                            GUI.changed = true;
                            Event.current.Use();
                        }
                    }
                    else
                    {
                        DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                        List <TileBase> validTiles = TileDragAndDrop.GetValidTiles(DragAndDrop.objectReferences);
                        ScriptableSingleton <TileDragAndDropManager> .instance.m_HoverData = TileDragAndDrop.CreateHoverData(null, null, validTiles);
                        if (ScriptableSingleton <TileDragAndDropManager> .instance.m_HoverData.Count > 0)
                        {
                            Event.current.Use();
                            GUI.changed = true;
                        }
                    }
                    if (ScriptableSingleton <TileDragAndDropManager> .instance.m_HoverData != null && (Event.current.type == EventType.DragExited || (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape)))
                    {
                        if (ScriptableSingleton <TileDragAndDropManager> .instance.m_HoverData.Count > 0)
                        {
                            Tilemap componentInParent2 = Selection.activeGameObject.GetComponentInParent <Tilemap>();
                            if (componentInParent2 != null)
                            {
                                componentInParent2.ClearAllEditorPreviewTiles();
                            }
                            Event.current.Use();
                        }
                        ScriptableSingleton <TileDragAndDropManager> .instance.m_HoverData = null;
                    }
                }
            }
        }