/// <summary>
    /// This overrides the default InspectorGUI
    /// </summary>
    public override void OnInspectorGUI()
    {
        //Here we check once more if the Terrain has a TerrainData attached to it.
        //OnEnable() doesn't trigger if you just drag&drop the data to the inspector and
        //the Island Generator GUI doesn't show up. OnInspectorGUI() triggers more often,
        //so here we'll start checking whether or not Terrain has TerrainData asset in it.
        if (terraini == null && t != null && !isTerrain)
        {
            terraini = t.terrainData as Object;

            //Terraindata detected, we can draw the GUI.
            if (terraini != null)
            {
                terrainReso = t.terrainData.heightmapResolution;
                isTerrain   = true;
            }
        }

        //If there is no Terrain or TerrainData, this draws an error message in the Inspector
        //rather than the actual Generator GUI
        if (!isTerrain)
        {
            EditorGUILayout.BeginVertical("box");
            EditorGUILayout.LabelField("No Terrain Found!", centerLabel);
            EditorGUILayout.LabelField("Please add Terrain Component\nto this Game Object.", centerLabel);
            EditorGUILayout.Space();
            EditorGUILayout.Space();
            EditorGUILayout.EndVertical();
        }

        //If the Terrain AND TerrainData is found, the actual Generator GUI is drawn
        else
        {
            terrainReso = t.terrainData.heightmapResolution;

            //This is the actual Generator script where we can input and output values from the GUI
            IslandGenerator script = (IslandGenerator)target;

            /***BEGIN GENERATOR BOX***/
            EditorGUILayout.BeginVertical("box");

            //If the Thread th1 is still running, it means shore calculation is still in progress,
            //so further map generation should be disabled for that time
            if (script.th1 != null && script.th1.ThreadState == System.Threading.ThreadState.Running)
            {
                GUI.enabled = false;
            }

            EditorGUILayout.LabelField("Generate Island", centerLabel);

            usedSeeds = script.usedSeeds;

            //This checks if user selects a seed from the Used Seeds list and replaces the current seed with it.
            EditorGUI.BeginChangeCheck();
            selected = EditorGUILayout.Popup("Used Seeds", selected, usedSeeds.ToArray());

            if (EditorGUI.EndChangeCheck())
            {
                script.seed = usedSeeds[selected];
            }

            script.seed              = EditorGUILayout.TextField(seedContent, script.seed);
            script.useRandomSeed     = EditorGUILayout.Toggle(useSeedContent, script.useRandomSeed);
            script.randomFillPercent = EditorGUILayout.IntSlider(randomFillContent, script.randomFillPercent, 30, 70);

            //If the shore radius changes, shores needs to be calculated again before adding them,
            //so the Add Shores Button will be disabled if the value here changes.
            EditorGUI.BeginChangeCheck();
            script.maxRadius = EditorGUILayout.IntSlider(maxRadiusContent, script.maxRadius, 25, 200);
            if (EditorGUI.EndChangeCheck())
            {
                shoresCalculated = false;
                script.prog      = "0%";
            }

            script.mapSmoothTimes = EditorGUILayout.IntField(smoothTimesContent, script.mapSmoothTimes);

            //Button to Generate basemap.
            if (GUILayout.Button(generateContent))
            {
                if (terrainReso < 129)
                {
                    Debug.LogWarning("Terrain Resolution is too small! Please use resolutions of 129 and higher.");
                }
                else
                {
                    Undo.RegisterUndo(terraini, "Generated New Map");
                    shoresCalculated = false;
                    script.StartGeneration();
                    script.prog = "0%";

                    //If the Used Seeds list doesn't contains current seed already
                    //we add that seed to the list.
                    if (!usedSeeds.Contains(script.seed))
                    {
                        usedSeeds.Insert(0, script.seed);
                    }
                    //Else if the seed exists in the list (e.g. same custom seed is used twice)
                    //we move the seed to the top.
                    else
                    {
                        usedSeeds.RemoveAt(usedSeeds.IndexOf(script.seed));
                        usedSeeds.Insert(0, script.seed);
                    }

                    //If there is more than 10 seeds, remove the oldest seed.
                    if (usedSeeds.Count > 10)
                    {
                        usedSeeds.RemoveAt(usedSeeds.Count - 1);
                    }

                    //After the button is pressed, change focus of the list to the latest seed.
                    selected = 0;
                }
            }

            EditorGUILayout.LabelField("Status", script.prog);

            EditorGUILayout.BeginHorizontal();

            if (GUILayout.Button(calculateShoresContent))
            {
                script.CalculateShores();
            }

            //Progress of shore calculation is handled in the actual script, and when it reaches 100%
            //the "Add Shores" button can be enabled.
            if (script.prog == "100%")
            {
                shoresCalculated = true;
            }

            if (shoresCalculated && GUILayout.Button(addShoresContent))
            {
                Undo.RegisterUndo(terraini, "Added Shores");
                script.SmoothShores();

                //Smooths the map as many times as the user defines.
                for (int i = 0; i < script.mapSmoothTimes; i++)
                {
                    script.BlendHeights();
                }
            }
            EditorGUILayout.EndHorizontal();

            if (GUILayout.Button(smoothOnceContent))
            {
                Undo.RegisterUndo(terraini, "Applied Smooth");
                script.BlendHeights();
            }

            GUI.enabled = true;

            if ((script.th1 != null && script.th1.ThreadState == System.Threading.ThreadState.Running) && GUILayout.Button("Abort"))
            {
                script.aborted   = true;
                shoresCalculated = false;
            }

            EditorGUILayout.EndVertical();
            /***END GENERATOR BOX***/

            EditorGUILayout.Space();

            /***BEGIN PERLIN NOISE BOX***/
            EditorGUILayout.BeginVertical("box");

            EditorGUILayout.LabelField("Add Perlin Noise", centerLabel);
            script.perlinHeight = EditorGUILayout.FloatField(heightContent, script.perlinHeight);
            script.perlinScale  = EditorGUILayout.FloatField(scaleContent, script.perlinScale);

            if (GUILayout.Button(addPerlinContent))
            {
                Undo.RegisterUndo(terraini, "Applied Perlin Noise");
                script.PerlinNoise();
            }
            if (GUILayout.Button(lowerSeaContent))
            {
                Undo.RegisterUndo(terraini, "Lowered Heightmap");
                script.ResetSeaFloor();
            }
            EditorGUILayout.EndVertical();
            /***END PERLIN NOISE BOX***/

            EditorGUILayout.Space();

            /***BEGIN PRESETS BOX***/
            EditorGUILayout.BeginVertical("box");

            EditorGUILayout.LabelField("Some preset noises  ", centerLabel);

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Many Low Bumps"))
            {
                Undo.RegisterUndo(terraini, "Applied Perlin Noise");
                script.perlinHeight = 0.001f;
                script.perlinScale  = 75;
                script.PerlinNoise();
            }
            if (GUILayout.Button("Many High Bumps"))
            {
                Undo.RegisterUndo(terraini, "Applied Perlin Noise");
                script.perlinHeight = 0.005f;
                script.perlinScale  = 100;
                script.PerlinNoise();
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Few Low Bumps"))
            {
                Undo.RegisterUndo(terraini, "Applied Perlin Noise");
                script.perlinHeight = 0.01f;
                script.perlinScale  = 20;
                script.PerlinNoise();
            }
            if (GUILayout.Button("Few High Bumps"))
            {
                Undo.RegisterUndo(terraini, "Applied Perlin Noise");
                script.perlinHeight = 0.015f;
                script.perlinScale  = 25;
                script.PerlinNoise();
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.EndVertical();
            /***END BOX***/
        }
    }