protected override void CloseScope()
 {
     horizontalScope.Dispose();
 }
    // Called when updating this editor window
    void OnGUI()
    {
        windowScrollPosition = EditorGUILayout.BeginScrollView(windowScrollPosition);

        Catalog = (LevelEditorCatalog)EditorGUILayout.ObjectField("Object Catalog", Catalog, typeof(LevelEditorCatalog), false);

        if (!CatalogLoaded)
        {
            EditorGUILayout.HelpBox("Load a catalog (TileEditorCatalog) to access all functionality.\nTo create a catalog, use the Create menu in the Assets folder and look for Level Editor/Level Editor Catalog.", MessageType.Warning);
        }

        using (new EditorGUILayout.HorizontalScope())
        {
            GUILayout.Label("Object Placement", EditorStyles.boldLabel, GUILayout.Width(position.width - 65));
            if (GUILayout.Button("Reload"))
            {
                CheckCatalog();
            }
        }

        using (new EditorGUI.DisabledGroupScope(!CatalogLoaded))
        {
            using (new EditorGUILayout.HorizontalScope())
            {
                float buttonWidth = (position.width - 3f) * 0.2f - 4f;
                if (GUILayout.Toggle(PlacingMode == PlacingModes.Off, placingModes[0], "button", GUILayout.Width(buttonWidth)))
                {
                    PlacingMode       = PlacingModes.Off;
                    arcPlacingStarted = wallPlacingStarted = false;
                    RemoveGhosts();
                }
                if (GUILayout.Toggle(PlacingMode == PlacingModes.Single, placingModes[1], "button", GUILayout.Width(buttonWidth)))
                {
                    PlacingMode       = PlacingModes.Single;
                    arcPlacingStarted = wallPlacingStarted = false;
                }
                using (new EditorGUI.DisabledGroupScope(!CatalogLoaded || !inventory[selectedItem].isArc))
                {
                    if (GUILayout.Toggle(PlacingMode == PlacingModes.Arc, placingModes[2], "button", GUILayout.Width(buttonWidth)))
                    {
                        PlacingMode        = PlacingModes.Arc;
                        wallPlacingStarted = false;
                    }
                    if (GUILayout.Toggle(PlacingMode == PlacingModes.Wall, placingModes[3], "button", GUILayout.Width(buttonWidth)))
                    {
                        PlacingMode       = PlacingModes.Wall;
                        arcPlacingStarted = false;
                    }
                    if (GUILayout.Toggle(PlacingMode == PlacingModes.Ring, placingModes[4], "button", GUILayout.Width(buttonWidth)))
                    {
                        PlacingMode       = PlacingModes.Ring;
                        arcPlacingStarted = wallPlacingStarted = false;
                    }
                }
            }
            //placingMode = GUILayout.SelectionGrid(placingMode, placingModes, placingModes.Length, "button");
            if (PlacingMode != PlacingModes.Off)
            {
                Tools.current = Tool.None;
            }
            else if (Tools.current == Tool.None)
            {
                Tools.current = Tool.Move;
            }
        }

        #region Rotation controls
        EditorGUILayout.BeginHorizontal();
        using (new EditorGUI.DisabledGroupScope(!CatalogLoaded))
        {
            autoRotateEnabled = EditorGUILayout.Toggle("Auto rotate", autoRotateEnabled);
        }
        using (new EditorGUI.DisabledGroupScope(true))
            GUILayout.Button(new GUIContent("↺", "Rotate 90° CCW"), GUILayout.Width(20));
        if (GUILayout.Button("Rotate Selection", GUILayout.ExpandWidth(false)))
        {
            RotateSelection();
        }
        using (new EditorGUI.DisabledGroupScope(true))
            GUILayout.Button(new GUIContent("↻", "Rotate 90° CW"), GUILayout.Width(20));
        EditorGUILayout.EndHorizontal();
        #endregion Rotation controls

        #region Radial and angular snapping controls
        using (new EditorGUILayout.HorizontalScope())
        {
            using (new EditorGUI.DisabledGroupScope(!CatalogLoaded))
            {
                GUILayout.Label("Snapping:", GUILayout.Width(146));
            }
            using (new EditorGUILayout.HorizontalScope())
            {
                using (new EditorGUI.DisabledGroupScope(!CatalogLoaded))
                {
                    var snapHS = new EditorGUILayout.HorizontalScope(GUILayout.MaxWidth(57));
                    radialSnapEnabled = GUI.Toggle(snapHS.rect, radialSnapEnabled, "", "button");
                    GUILayout.Label("  Radial");
                    snapHS.Dispose();
                    snapHS             = new EditorGUILayout.HorizontalScope(GUILayout.MaxWidth(57));
                    angularSnapEnabled = GUI.Toggle(snapHS.rect, angularSnapEnabled, "", "button");
                    GUILayout.Label(" Angular");
                    snapHS.Dispose();

                    GUILayout.FlexibleSpace();
                    drawGuides = EditorGUILayout.ToggleLeft("Guides", drawGuides, GUILayout.Width(60));
                }
            }
        }

        using (new EditorGUI.DisabledGroupScope(!CatalogLoaded))
        {
            using (new EditorGUILayout.HorizontalScope())
            {
                GUILayout.Label("Angular Divisions", GUILayout.Width(146));
                using (new EditorGUILayout.HorizontalScope())
                {
                    selectedAngularDensity = EditorGUILayout.IntPopup(selectedAngularDensity, new string[] { "Auto", "6", "12", "24", "48", "96", "192", "384", "768", "Custom..." }, new int[] { 0, 6, 12, 24, 48, 96, 192, 384, 768, -1 });
                    if (selectedAngularDensity == -1)
                    {
                        customAngularDensity = EditorGUILayout.IntField(customAngularDensity);
                    }
                }
            }

            using (new EditorGUILayout.HorizontalScope())
            {
                GUILayout.Label("Radial Thickness", GUILayout.Width(146));
                using (new EditorGUILayout.HorizontalScope())
                {
                    selectedRadialThickness = EditorGUILayout.Popup(selectedRadialThickness, new string[] { "0.25 m", "50 m", "0.75 m", "1 m", "2 m", "Custom..." });
                    switch (selectedRadialThickness)
                    {
                    case 0:
                        radialThickness = 0.25f;
                        break;

                    case 1:
                        radialThickness = 0.5f;
                        break;

                    case 2:
                        radialThickness = 0.75f;
                        break;

                    case 3:
                        radialThickness = 1f;
                        break;

                    case 4:
                        radialThickness = 2f;
                        break;

                    case 5:
                        radialThickness = EditorGUILayout.Slider(radialThickness, 0.1f, 10);
                        break;

                    default:
                        break;
                    }
                }
            }
        }
        #endregion Radial and angular snapping controls


        EditorGUILayout.Space();


        #region Catalog
        if (CatalogLoaded)
        {
            Color defColor  = GUI.backgroundColor;
            Color highlight = EditorStyles.label.focused.textColor.Lighter().Lighter().Lighter().Lighter();

            // Prefab selector scroll view
            using (var scrollView = new EditorGUILayout.ScrollViewScope(itemSelectorScrollPosition))
            {
                GUI.backgroundColor = Color.gray;
                using (var v = new EditorGUILayout.VerticalScope())
                {
                    GUI.Box(v.rect, "");


                    // Here we populate the list view ...
                    int n = 0;
                    while (n < inventory.Count)
                    {
                        // ... row by row ...
                        using (var H = new EditorGUILayout.HorizontalScope())
                        {
                            int w = 0;
                            do
                            {
                                // ... element by element
                                using (var h = new EditorGUILayout.HorizontalScope("button", GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(false), GUILayout.MaxWidth(catalogIconSize), GUILayout.MaxHeight(catalogIconSize), GUILayout.Width(catalogIconSize), GUILayout.Height(catalogIconSize)))
                                {
                                    // A checkbox disguised as a button with the functionality of a radio button

                                    Texture2D  tex         = Catalog[n].GetIcon();
                                    string     displayName = Catalog[n].displayName;
                                    GUIContent content     = new GUIContent(tex, displayName);

                                    // The toggle with an image
                                    if (n == selectedItem)
                                    {
                                        GUI.backgroundColor = highlight;
                                    }
                                    if (GUI.Toggle(h.rect, n == selectedItem ? true : false, content, "button"))
                                    {
                                        if (selectedItem != n)
                                        {
                                            if (ghost != null)
                                            {
                                                DestroyImmediate(ghost.gameObject);
                                            }
                                            selectedItem = n;
                                            if (!inventory[selectedItem].isArc && PlacingMode != PlacingModes.Off)
                                            {
                                                PlacingMode = PlacingModes.Single;
                                            }
                                        }
                                    }
                                    GUI.backgroundColor = Color.gray;

                                    // and some text over it
                                    GUIStyle style = new GUIStyle(EditorStyles.boldLabel);
                                    style.normal.textColor = n == selectedItem ? highlight : Color.white;
                                    style.wordWrap         = true;
                                    GUILayout.Label(displayName, style);
                                }
                                w += catalogIconSize;
                                n++;
                            } while (n < inventory.Count && w < (position.width - catalogIconSize));
                        }
                    }

                    itemSelectorScrollPosition = scrollView.scrollPosition;
                }
                GUI.backgroundColor = defColor;
            }
        }
        #endregion Catalog

        GUILayout.Label("Level Parameters", EditorStyles.boldLabel);

        using (new EditorGUI.DisabledGroupScope(!CatalogLoaded))
        {
            LookForBasicObjects();

            using (new EditorGUILayout.HorizontalScope())
            {
                levelObjectsFoldoutState = EditorGUILayout.Foldout(levelObjectsFoldoutState, "Basic objects", true);
                if (GUILayout.Button("Prepare Level", GUILayout.Width(100)))
                {
                    PrepareLevel();
                }
            }
            if (levelObjectsFoldoutState)
            {
                player = (GameObject)EditorGUILayout.ObjectField("Player", player, typeof(GameObject), true);
                if (player != null)
                {
                    if (PrefabUtility.GetPrefabType(player) == PrefabType.Prefab)
                    {
                        player = null;
                    }
                }
                camera = (Camera)EditorGUILayout.ObjectField("Camera", camera, typeof(Camera), true);
                if (camera != null)
                {
                    if (PrefabUtility.GetPrefabType(camera) == PrefabType.Prefab)
                    {
                        camera = null;
                    }
                }
                forceField = (GameObject)EditorGUILayout.ObjectField("Force Field", forceField, typeof(GameObject), true);
                if (forceField != null)
                {
                    if (PrefabUtility.GetPrefabType(forceField) == PrefabType.Prefab)
                    {
                        forceField = null;
                    }
                }
                lights = (GameObject)EditorGUILayout.ObjectField("Spotlights", lights, typeof(GameObject), true);
                if (lights != null)
                {
                    if (PrefabUtility.GetPrefabType(lights) == PrefabType.Prefab)
                    {
                        lights = null;
                    }
                }
            }
        }

        EditorGUILayout.EndScrollView();
    }