/// <summary>
 /// To be invoked by a Refresh Button or when Tab Switching
 /// </summary>
 public void RefreshMapSize()
 {
     m_tileMap.MeshSettings.TilesX = (int)TilesXSlider.value;
     m_tileMap.MeshSettings.TilesY = (int)TilesYSlider.value;
     m_tileMap.DestroyMesh();
     m_tileMap.CreateMesh();
 }
    public override void OnInspectorGUI()
    {
        //		base.OnInspectorGUI();

        m_showMapSettings = EditorGUILayout.Foldout(m_showMapSettings, "Map Settings");
        if (m_showMapSettings)
        {
            m_activeInEditMode = EditorGUILayout.Toggle("Active In Edit Mode", m_activeInEditMode);

            m_tilesX = EditorGUILayout.IntField(
                new GUIContent("Tiles X", "The number of tiles on the X axis"),
                m_tilesX);
            m_tilesY = EditorGUILayout.IntField(
                new GUIContent("Tiles Y", "The number of tiles on the Y axis"),
                m_tilesY);
            m_tileResolution = EditorGUILayout.IntField(
                new GUIContent("Tile Resolution", "The number of pixels along each axis on one tile"),
                m_tileResolution);

            if (m_tileResolution != m_tileMap.MeshSettings.TileResolution)
            {
                EditorGUILayout.HelpBox("Changing tile resolution will clear the current tile setup.\n" +
                                        string.Format("Current tile resolution is {0}.", m_tileMap.MeshSettings.TileResolution), MessageType.Warning);
            }

            m_tileSize = EditorGUILayout.FloatField(
                new GUIContent("Tile Size", "The size of one tile in Unity units"),
                m_tileSize);
            m_meshMode = (MeshMode)EditorGUILayout.EnumPopup("Mesh Mode", m_meshMode);

            // these settings only apply to the single quad mode mesh
            if (m_meshMode == MeshMode.SingleQuad)
            {
                m_textureFormat     = (TextureFormat)EditorGUILayout.EnumPopup("Texture Format", m_textureFormat);
                m_textureFilterMode = (FilterMode)EditorGUILayout.EnumPopup("Filter Mode", m_textureFilterMode);
            }

            if (GUILayout.Button("Create/Recreate Mesh"))
            {
                bool canDelete = true;

                if (m_tileResolution != m_tileMap.MeshSettings.TileResolution)
                {
                    canDelete = ShowTileDeletionWarning();
                }

                if (canDelete)
                {
                    m_tileMap.MeshSettings = new TileMeshSettings(m_tilesX, m_tilesY, m_tileResolution, m_tileSize, m_meshMode, m_textureFormat);

                    // if settings didnt change the mesh wouldnt be created, force creation
                    if (!m_tileMap.HasMesh)
                    {
                        m_tileMap.CreateMesh();
                    }

                    m_activeInEditMode = true;
                }
            }
            if (GUILayout.Button("Destroy Mesh (keep data)"))
            {
                m_tileMap.DestroyMesh();

                m_activeInEditMode = false;
            }

            if (GUILayout.Button("Clear"))
            {
                if (ShowTileDeletionWarning())
                {
                    m_tileMap.DestroyMesh();
                    m_tileMap.ClearTiles();

                    m_activeInEditMode = false;
                }
            }

            if (m_tileMap.ActiveInEditMode != m_activeInEditMode)
            {
                m_tileMap.ActiveInEditMode = m_activeInEditMode;
                OnSceneGUI(); // force redraw map editor box
            }
        }

        m_showSortSettings = EditorGUILayout.Foldout(m_showSortSettings, "Sort Settings");
        if (m_showSortSettings)
        {
            TileMeshBehaviour mesh = m_tileMap.GetComponentInChildren <TileMeshBehaviour>();

            // When first creating tile map or after deleting the mesh,
            // it will not be accessible until it's created by user.
            if (mesh != null && mesh.renderer != null)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUI.BeginChangeCheck();

                string[] sortingLayers = GetSortingLayerNames();

                int currentLayer = 0;

                bool isLayerSet = mesh.renderer.sortingLayerName.Length > 0;

                if (!isLayerSet)
                {
                    currentLayer = FindStringIndex(ref sortingLayers, "Default");
                }
                else
                {
                    currentLayer = FindStringIndex(ref sortingLayers, mesh.renderer.sortingLayerName);
                }

                int chosenLayer = EditorGUILayout.Popup("Sorting Layer Name", Mathf.Max(currentLayer, 0), sortingLayers);

                if (EditorGUI.EndChangeCheck() || !isLayerSet)
                {
                    mesh.renderer.sortingLayerName = sortingLayers[chosenLayer];
                }

                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                EditorGUI.BeginChangeCheck();

                int order = EditorGUILayout.IntField("Sorting Order", mesh.renderer.sortingOrder);

                if (EditorGUI.EndChangeCheck())
                {
                    mesh.renderer.sortingOrder = order;
                }

                EditorGUILayout.EndHorizontal();
            }
            else
            {
                EditorGUILayout.HelpBox("Mesh has not been created.\nPlease use 'Create/Recreate Mesh' button in the Map Settings.", MessageType.Info, true);
            }
        }

        m_showPickerSettings = EditorGUILayout.Foldout(m_showPickerSettings, "Tile Picker Settings");
        if (m_showPickerSettings)
        {
            m_tilePickerXCount = Mathf.Clamp(EditorGUILayout.IntField(
                                                 new GUIContent("Items Per Row", "The number of items to draw in a row in the Tile Picker Window."),
                                                 m_tilePickerXCount), 1, int.MaxValue);

            m_tilePickerPosition.width = Mathf.Clamp(EditorGUILayout.FloatField(
                                                         new GUIContent("Picker Width", "The width of the Tile Picker Window."),
                                                         m_tilePickerPosition.width), 64f, float.MaxValue);

            m_tilePickerPosition.height = Mathf.Clamp(EditorGUILayout.FloatField(
                                                          new GUIContent("Picker Height", "The height of the Tile Picker Window."),
                                                          m_tilePickerPosition.height), 128f, float.MaxValue);
        }

        bool prominentImportArea = m_tileSheet.Ids.Count() == 0;

        m_showSprites = EditorGUILayout.Foldout(m_showSprites || prominentImportArea, "Sprites");
        if (m_showSprites || prominentImportArea)
        {
            ShowImportDropArea();
        }
        if (m_showSprites && !prominentImportArea)
        {
            if (GUILayout.Button("Delete all"))
            {
                foreach (var id in m_tileMap.TileSheet.Ids)
                {
                    m_tileMap.TileSheet.Remove(id);
                }
                m_thumbnailCache.Clear();
            }

            if (GUILayout.Button("Refresh thumbnails"))
            {
                m_thumbnailCache.Clear();
            }

            m_sortSpritesByName = GUILayout.Toggle(m_sortSpritesByName, "Sort sprites by name");

            var ids = m_tileSheet.Ids.ToList();
            ids.Sort();
            for (int i = 0; i < ids.Count; i++)
            {
                var sprite = m_tileSheet.Get(ids[i]);
                ShowSprite(sprite);

                // add separators, except below the last one
                // could probably find a better looking one
                if (i < (ids.Count - 1))
                {
                    GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));
                }
            }
        }

        EditorUtility.SetDirty(this);
    }
    public override void OnInspectorGUI()
    {
        //		base.OnInspectorGUI();

        m_showMapSettings = EditorGUILayout.Foldout(m_showMapSettings, "Map Settings");
        if (m_showMapSettings)
        {
            m_activeInEditMode = EditorGUILayout.Toggle("Active In Edit Mode", m_activeInEditMode);
            if (m_tileMap.ActiveInEditMode != m_activeInEditMode)
            {
                m_tileMap.ActiveInEditMode = m_activeInEditMode;
                OnSceneGUI(); // force redraw map editor box
            }

            m_tilesX = EditorGUILayout.IntField(
                new GUIContent("Tiles X", "The number of tiles on the X axis"),
                m_tilesX);
            m_tilesY = EditorGUILayout.IntField(
                new GUIContent("Tiles Y", "The number of tiles on the Y axis"),
                m_tilesY);
            m_tileResolution = EditorGUILayout.IntField(
                new GUIContent("Tile Resolution", "The number of pixels along each axis on one tile"),
                m_tileResolution);
            m_tileSize = EditorGUILayout.FloatField(
                new GUIContent("Tile Size", "The size of one tile in Unity units"),
                m_tileSize);
            m_meshMode = (MeshMode)EditorGUILayout.EnumPopup("Mesh Mode", m_meshMode);

            // these settings only apply to the single quad mode mesh
            if (m_meshMode == MeshMode.SingleQuad)
            {
                m_textureFormat     = (TextureFormat)EditorGUILayout.EnumPopup("Texture Format", m_textureFormat);
                m_textureFilterMode = (FilterMode)EditorGUILayout.EnumPopup("Filter Mode", m_textureFilterMode);
            }

            if (GUILayout.Button("Create/Recreate Mesh"))
            {
                m_tileMap.MeshSettings = new TileMeshSettings(m_tilesX, m_tilesY, m_tileResolution, m_tileSize, m_meshMode, m_textureFormat);

                // if settings didnt change the mesh wouldnt be created, force creation
                if (!m_tileMap.HasMesh)
                {
                    m_tileMap.CreateMesh();
                }
            }
            if (GUILayout.Button("Destroy Mesh (keep data)"))
            {
                m_tileMap.DestroyMesh();
            }
        }

        m_showPickerSettings = EditorGUILayout.Foldout(m_showPickerSettings, "Tile Picker Settings");
        if (m_showPickerSettings)
        {
            m_tilePickerXCount = Mathf.Clamp(EditorGUILayout.IntField(
                                                 new GUIContent("Items Per Row", "The number of items to draw in a row in the Tile Picker Window."),
                                                 m_tilePickerXCount), 1, int.MaxValue);

            m_tilePickerPosition.width = Mathf.Clamp(EditorGUILayout.FloatField(
                                                         new GUIContent("Picker Width", "The width of the Tile Picker Window."),
                                                         m_tilePickerPosition.width), 64f, float.MaxValue);

            m_tilePickerPosition.height = Mathf.Clamp(EditorGUILayout.FloatField(
                                                          new GUIContent("Picker Height", "The height of the Tile Picker Window."),
                                                          m_tilePickerPosition.height), 128f, float.MaxValue);
        }

        bool prominentImportArea = m_tileSheet.Ids.Count() == 0;

        m_showSprites = EditorGUILayout.Foldout(m_showSprites || prominentImportArea, "Sprites:");
        if (m_showSprites || prominentImportArea)
        {
            ShowImportDropArea();
        }
        if (m_showSprites && !prominentImportArea)
        {
            if (GUILayout.Button("Delete all"))
            {
                foreach (var id in m_tileMap.TileSheet.Ids)
                {
                    m_tileMap.TileSheet.Remove(id);
                }
                m_thumbnailCache.Clear();
            }

            if (GUILayout.Button("Refresh thumbnails"))
            {
                m_thumbnailCache.Clear();
            }

            m_sortSpritesByName = GUILayout.Toggle(m_sortSpritesByName, "Sort sprites by name");

            var ids = m_tileSheet.Ids.ToList();
            ids.Sort();
            for (int i = 0; i < ids.Count; i++)
            {
                var sprite = m_tileSheet.Get(ids[i]);
                ShowSprite(sprite);

                // add separators, except below the last one
                // could probably find a better looking one
                if (i < (ids.Count - 1))
                {
                    GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));
                }
            }
        }

        EditorUtility.SetDirty(this);
    }