using NEG.UI.Area; using NEG.UI.Popup; using NEG.UI.UnityUi.Buttons.Reaction; using NEG.UI.UnityUi.Buttons.Settings; using NEG.UI.UnityUi.Popup; using NEG.UI.UnityUi.Window; using NEG.Utils; using System; using UnityEngine; using UnityEngine.Assertions; using UnityEngine.EventSystems; using UnityEngine.SceneManagement; using Object = UnityEngine.Object; namespace NEG.UI.UnityUi { /// /// Implements ui using UnityUI and Unity Event System with New Input System. /// You have to provide prefabs within resources: /// - UI/PopupCanvas - prefab with canvas to create popups (will be created on every scene) /// /// - UI/DefaultPopupPrefab - prefab of default popup with 2 options (has to have /// component) /// /// NEG_UI_DISABLE_WARNING_DEFAULT_SELECTION /// public class MonoUiManager : UiManager, IDisposable { private readonly GameObject canvasPrefab; //TODO: editor to auto add slots, buttons private readonly MonoDefaultPopup defaultPopupPrefab; private readonly UiInputModule inputModule; public MonoUiManager(IArea startArea, Type inputModuleType, ButtonSettings defaultUiSettings) : base(startArea) { Instance = this; var popupCanvas = Resources.Load("UI/PopupCanvas"); var defaultPopup = Resources.Load("UI/DefaultPopupPrefab"); Assert.IsNotNull(popupCanvas, "No canvas prefab was provided. Please check MonoUiManager class documentation"); Assert.IsNotNull(defaultPopup, "No popup prefab was provided. Please check MonoUiManager class documentation"); Assert.IsNotNull(popupCanvas.GetComponent()); Assert.IsNotNull(defaultPopup.GetComponent()); canvasPrefab = popupCanvas; defaultPopupPrefab = defaultPopup.GetComponent(); SpawnDefaultPopup(); SceneManager.activeSceneChanged += (_, _) => SpawnDefaultPopup(); BehavioursFactory = new KeyBasedFactory(); BehavioursFactory.FireRegistration(); inputModule = (UiInputModule)Activator.CreateInstance(inputModuleType); DefaultUiSettings = defaultUiSettings; } //TODO: use default unity selection //TODO: window snaping to slots public static new MonoUiManager Instance { get; private set; } public ButtonSettings DefaultUiSettings { get; } public KeyBasedFactory BehavioursFactory { get; } public override void Dispose() { base.Dispose(); Instance = null; } protected override void UpdatePopupsState(bool forceShow, int priority = 0, PopupData data = null) { base.UpdatePopupsState(forceShow, priority, data); if (inputModule.CurrentSelectionSource != SelectionSource.Direction) return; if (CurrentPopup == null && (EventSystem.current.currentSelectedGameObject == null || !EventSystem.current.currentSelectedGameObject.activeInHierarchy)) { if (((MonoWindow)CurrentMainWindow).DefaultSelectedItem == null) return; EventSystem.current.SetSelectedGameObject(((MonoWindow)CurrentMainWindow).DefaultSelectedItem); } } private void SpawnDefaultPopup() { var canvas = Object.Instantiate(canvasPrefab); canvas.name = "DefaultPopupCanvas"; SetDefaultPopup(Object.Instantiate(defaultPopupPrefab, canvas.transform)); currentDefaultPopup.Close(true); } } }