using UnityEngine; using System.Collections.Generic; using Lean.Common; namespace Lean.Gui { /// This component allows you to automatically close the top-most LeanWindow when you press the specified key. [ExecuteInEditMode] [HelpURL(LeanGui.HelpUrlPrefix + "LeanWindowCloser")] [AddComponentMenu(LeanGui.ComponentMenuPrefix + "Window Closer")] public class LeanWindowCloser : MonoBehaviour { /// This stores all active and enabled LeanWindowCloser instances. public static List Instances = new List(); /// This allows you to set the key that must be pressed to close the window on top. public KeyCode CloseKey { set { closeKey = value; } get { return closeKey; } } [SerializeField] private KeyCode closeKey = KeyCode.Escape; /// If every window is closed and you press the close key, this window will be opened. This can be used to open an options menu. public LeanWindow EmptyWindow { set { emptyWindow = value; } get { return emptyWindow; } } [SerializeField] private LeanWindow emptyWindow; /// This stores a list of all opened windows, in order of opening, so they can be closed in reverse order. public List WindowOrder { get { if (windowOrder == null) windowOrder = new List(); return windowOrder; } } [SerializeField] private List windowOrder; public static void Register(LeanWindow window) { if (Instances.Count > 0 && window != null) { Instances[0].RegisterNow(window); } } /// This allows you to close all open LeanWindows. [ContextMenu("Close All")] public void CloseAll() { for (var i = WindowOrder.Count - 1; i >= 0; i--) // NOTE: Property { var window = windowOrder[i]; if (window != null && window.On == true) { window.TurnOff(); } } windowOrder.Clear(); } /// This allows you to close the top most LeanWindow. [ContextMenu("Close Top Most")] public void CloseTopMost() { for (var i = WindowOrder.Count - 1; i >= 0; i--) // NOTE: Property { var window = windowOrder[i]; windowOrder.RemoveAt(i); if (window != null && window.On == true) { window.TurnOff(); return; } } if (emptyWindow != null) { emptyWindow.TurnOn(); } } protected virtual void OnEnable() { Instances.Add(this); } protected virtual void OnDisable() { Instances.Remove(this); } protected virtual void Update() { if (this == Instances[0]) { if (LeanInput.GetDown(CloseKey) == true) { CloseTopMost(); } } } private void RegisterNow(LeanWindow window) { WindowOrder.Remove(window); // NOTE: Property windowOrder.Add(window); } } } #if UNITY_EDITOR namespace Lean.Gui.Editor { using TARGET = LeanWindowCloser; [UnityEditor.CanEditMultipleObjects] [UnityEditor.CustomEditor(typeof(TARGET))] public class LeanWindowCloser_Editor : LeanEditor { protected override void OnInspector() { TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts); Draw("closeKey", "This allows you to set the key that must be pressed to close the window on top."); Draw("emptyWindow", "If every window is closed and you press the close key, this window will be opened. This can be used to open an options menu."); Separator(); BeginDisabled(true); Draw("windowOrder", "This stores a list of all opened windows, in order of opening, so they can be closed in reverse order."); EndDisabled(); } } } #endif