private void RenderDragAndDropPreview()
 {
     if (this.activeDragAndDrop && this.m_HoverData != null && this.m_HoverData.Count != 0)
     {
         RectInt minMaxRect = TileDragAndDrop.GetMinMaxRect(this.m_HoverData.Keys.ToList <Vector2Int>());
         minMaxRect.position   += base.mouseGridPosition;
         DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
         GridEditorUtility.DrawGridMarquee(this.grid, new BoundsInt(new Vector3Int(minMaxRect.xMin, minMaxRect.yMin, 0), new Vector3Int(minMaxRect.width, minMaxRect.height, 1)), Color.white);
     }
 }
Beispiel #2
0
        public static Dictionary <Vector2Int, UnityEngine.Object> CreateHoverData(List <Texture2D> sheetTextures, List <Sprite> singleSprites, List <TileBase> tiles)
        {
            Dictionary <Vector2Int, UnityEngine.Object> dictionary = new Dictionary <Vector2Int, UnityEngine.Object>();
            Vector2Int vector2Int = new Vector2Int(0, 0);
            int        num        = 0;

            if (sheetTextures != null)
            {
                foreach (Texture2D current in sheetTextures)
                {
                    Dictionary <Vector2Int, UnityEngine.Object> dictionary2 = TileDragAndDrop.CreateHoverData(current);
                    foreach (KeyValuePair <Vector2Int, UnityEngine.Object> current2 in dictionary2)
                    {
                        dictionary.Add(current2.Key + vector2Int, current2.Value);
                    }
                    Vector2Int min = TileDragAndDrop.GetMinMaxRect(dictionary2.Keys.ToList <Vector2Int>()).min;
                    vector2Int += new Vector2Int(0, min.y - 1);
                }
            }
            if (vector2Int.x > 0)
            {
                vector2Int = new Vector2Int(0, vector2Int.y - 1);
            }
            if (singleSprites != null)
            {
                num = Mathf.FloorToInt(Mathf.Sqrt((float)singleSprites.Count));
                foreach (Sprite current3 in singleSprites)
                {
                    dictionary.Add(vector2Int, current3);
                    vector2Int += new Vector2Int(1, 0);
                    if (vector2Int.x > num)
                    {
                        vector2Int = new Vector2Int(0, vector2Int.y - 1);
                    }
                }
            }
            if (vector2Int.x > 0)
            {
                vector2Int = new Vector2Int(0, vector2Int.y - 1);
            }
            if (tiles != null)
            {
                num = Math.Max(Mathf.FloorToInt(Mathf.Sqrt((float)tiles.Count)), num);
                foreach (TileBase current4 in tiles)
                {
                    dictionary.Add(vector2Int, current4);
                    vector2Int += new Vector2Int(1, 0);
                    if (vector2Int.x > num)
                    {
                        vector2Int = new Vector2Int(0, vector2Int.y - 1);
                    }
                }
            }
            return(dictionary);
        }
Beispiel #3
0
        public static Dictionary <Vector2Int, UnityEngine.Object> CreateHoverData(Texture2D sheet)
        {
            Dictionary <Vector2Int, UnityEngine.Object> dictionary = new Dictionary <Vector2Int, UnityEngine.Object>();
            List <Sprite> spritesFromTexture = TileDragAndDrop.GetSpritesFromTexture(sheet);
            Vector2Int    cellPixelSize      = TileDragAndDrop.EstimateGridPixelSize(spritesFromTexture);

            foreach (Sprite current in spritesFromTexture)
            {
                Vector2Int gridPosition = TileDragAndDrop.GetGridPosition(current, cellPixelSize);
                dictionary[gridPosition] = current;
            }
            return(dictionary);
        }
        private void RenderDragAndDropPreview()
        {
            if (!activeDragAndDrop || m_HoverData == null || m_HoverData.Count == 0)
            {
                return;
            }

            RectInt rect = TileDragAndDrop.GetMinMaxRect(m_HoverData.Keys.ToList());

            rect.position         += mouseGridPosition;
            DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
            GridEditorUtility.DrawGridMarquee(grid, new BoundsInt(new Vector3Int(rect.xMin, rect.yMin, 0), new Vector3Int(rect.width, rect.height, 1)), Color.white);
        }
Beispiel #5
0
        public static List <Texture2D> GetValidSpritesheets(UnityEngine.Object[] objects)
        {
            List <Texture2D> list = new List <Texture2D>();

            for (int i = 0; i < objects.Length; i++)
            {
                UnityEngine.Object @object = objects[i];
                if (@object is Texture2D)
                {
                    Texture2D     texture2D          = @object as Texture2D;
                    List <Sprite> spritesFromTexture = TileDragAndDrop.GetSpritesFromTexture(texture2D);
                    if (spritesFromTexture.Count <Sprite>() > 1 && TileDragAndDrop.AllSpritesAreSameSize(spritesFromTexture))
                    {
                        list.Add(texture2D);
                    }
                }
            }
            return(list);
        }
Beispiel #6
0
        public static Vector2Int EstimateGridPixelSize(List <Sprite> sprites)
        {
            Vector2Int result;

            if (!sprites.Any <Sprite>())
            {
                result = new Vector2Int(0, 0);
            }
            else if (sprites.Count == 1)
            {
                result = Vector2Int.FloorToInt(sprites[0].rect.size);
            }
            else
            {
                Vector2    min        = TileDragAndDrop.GetMin(sprites, new Vector2(-3.40282347E+38f, -3.40282347E+38f));
                Vector2    min2       = TileDragAndDrop.GetMin(sprites, min);
                Vector2Int vector2Int = Vector2Int.FloorToInt(min2 - min);
                vector2Int.x = Math.Max(Mathf.FloorToInt(sprites[0].rect.width), vector2Int.x);
                vector2Int.y = Math.Max(Mathf.FloorToInt(sprites[0].rect.height), vector2Int.y);
                result       = vector2Int;
            }
            return(result);
        }
Beispiel #7
0
        public static List <Sprite> GetValidSingleSprites(UnityEngine.Object[] objects)
        {
            List <Sprite> list = new List <Sprite>();

            for (int i = 0; i < objects.Length; i++)
            {
                UnityEngine.Object @object = objects[i];
                if (@object is Sprite)
                {
                    list.Add(@object as Sprite);
                }
                else if (@object is Texture2D)
                {
                    Texture2D     texture            = @object as Texture2D;
                    List <Sprite> spritesFromTexture = TileDragAndDrop.GetSpritesFromTexture(texture);
                    if (spritesFromTexture.Count == 1 || !TileDragAndDrop.AllSpritesAreSameSize(spritesFromTexture))
                    {
                        list.AddRange(spritesFromTexture);
                    }
                }
            }
            return(list);
        }
        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;
            }
        }
Beispiel #9
0
        public static Dictionary <Vector2Int, TileBase> ConvertToTileSheet(Dictionary <Vector2Int, UnityEngine.Object> sheet)
        {
            Dictionary <Vector2Int, TileBase> dictionary = new Dictionary <Vector2Int, TileBase>();
            string text = (!ProjectBrowser.s_LastInteractedProjectBrowser) ? "Assets" : ProjectBrowser.s_LastInteractedProjectBrowser.GetActiveFolderPath();
            Dictionary <Vector2Int, TileBase> result;

            if (sheet.Values.ToList <UnityEngine.Object>().FindAll((UnityEngine.Object obj) => obj is TileBase).Count == sheet.Values.Count)
            {
                foreach (KeyValuePair <Vector2Int, UnityEngine.Object> current in sheet)
                {
                    dictionary.Add(current.Key, current.Value as TileBase);
                }
                result = dictionary;
            }
            else
            {
                TileDragAndDrop.UserTileCreationMode userTileCreationMode = TileDragAndDrop.UserTileCreationMode.Overwrite;
                string text2 = "";
                bool   flag  = sheet.Count > 1;
                if (flag)
                {
                    bool flag2 = false;
                    text2 = EditorUtility.SaveFolderPanel("Generate tiles into folder ", text, "");
                    text2 = FileUtil.GetProjectRelativePath(text2);
                    foreach (UnityEngine.Object current2 in sheet.Values)
                    {
                        if (current2 is Sprite)
                        {
                            string path = FileUtil.CombinePaths(new string[]
                            {
                                text2,
                                string.Format("{0}.{1}", current2.name, TileDragAndDrop.k_TileExtension)
                            });
                            if (File.Exists(path))
                            {
                                flag2 = true;
                                break;
                            }
                        }
                    }
                    if (flag2)
                    {
                        int num = EditorUtility.DisplayDialogComplex("Overwrite?", string.Format("Assets exist at {0}. Do you wish to overwrite existing assets?", text2), "Overwrite", "Create New Copy", "Reuse");
                        if (num != 0)
                        {
                            if (num != 1)
                            {
                                if (num == 2)
                                {
                                    userTileCreationMode = TileDragAndDrop.UserTileCreationMode.Reuse;
                                }
                            }
                            else
                            {
                                userTileCreationMode = TileDragAndDrop.UserTileCreationMode.CreateUnique;
                            }
                        }
                        else
                        {
                            userTileCreationMode = TileDragAndDrop.UserTileCreationMode.Overwrite;
                        }
                    }
                }
                else
                {
                    text2 = EditorUtility.SaveFilePanelInProject("Generate new tile", sheet.Values.First <UnityEngine.Object>().name, TileDragAndDrop.k_TileExtension, "Generate new tile", text);
                }
                if (string.IsNullOrEmpty(text2))
                {
                    result = dictionary;
                }
                else
                {
                    int num2 = 0;
                    EditorUtility.DisplayProgressBar(string.Concat(new object[]
                    {
                        "Generating Tile Assets (",
                        num2,
                        "/",
                        sheet.Count,
                        ")"
                    }), "Generating tiles", 0f);
                    foreach (KeyValuePair <Vector2Int, UnityEngine.Object> current3 in sheet)
                    {
                        string   text3 = "";
                        TileBase tileBase;
                        if (current3.Value is Sprite)
                        {
                            tileBase = TileDragAndDrop.CreateTile(current3.Value as Sprite);
                            text3    = ((!flag) ? text2 : FileUtil.CombinePaths(new string[]
                            {
                                text2,
                                string.Format("{0}.{1}", tileBase.name, TileDragAndDrop.k_TileExtension)
                            }));
                            if (userTileCreationMode != TileDragAndDrop.UserTileCreationMode.CreateUnique)
                            {
                                if (userTileCreationMode != TileDragAndDrop.UserTileCreationMode.Overwrite)
                                {
                                    if (userTileCreationMode == TileDragAndDrop.UserTileCreationMode.Reuse)
                                    {
                                        if (File.Exists(text3))
                                        {
                                            tileBase = AssetDatabase.LoadAssetAtPath <TileBase>(text3);
                                        }
                                        else
                                        {
                                            AssetDatabase.CreateAsset(tileBase, text3);
                                        }
                                    }
                                }
                                else
                                {
                                    AssetDatabase.CreateAsset(tileBase, text3);
                                }
                            }
                            else
                            {
                                if (File.Exists(text3))
                                {
                                    text3 = AssetDatabase.GenerateUniqueAssetPath(text3);
                                }
                                AssetDatabase.CreateAsset(tileBase, text3);
                            }
                        }
                        else
                        {
                            tileBase = (current3.Value as TileBase);
                        }
                        EditorUtility.DisplayProgressBar(string.Concat(new object[]
                        {
                            "Generating Tile Assets (",
                            num2,
                            "/",
                            sheet.Count,
                            ")"
                        }), "Generating " + text3, (float)num2++ / (float)sheet.Count);
                        dictionary.Add(current3.Key, tileBase);
                    }
                    EditorUtility.ClearProgressBar();
                    AssetDatabase.Refresh();
                    result = dictionary;
                }
            }
            return(result);
        }
        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();
            }
        }
 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();
         }
     }
 }
Beispiel #12
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;
                    }
                }
            }
        }