101 lines
3.9 KiB
C#
101 lines
3.9 KiB
C#
using NEG.UI.Area;
|
|
using NEG.UI.Popup;
|
|
using NEG.UI.UnityUi.Buttons.Behaviours;
|
|
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
|
|
{
|
|
/// <summary>
|
|
/// Implements ui using UnityUI and Unity Event System with New Input System.
|
|
/// <para>You have to provide prefabs within resources:</para>
|
|
/// <para> - UI/PopupCanvas - prefab with canvas to create popups (will be created on every scene)</para>
|
|
/// <para>
|
|
/// - UI/DefaultPopupPrefab - prefab of default popup with 2 options (has to have
|
|
/// <see cref="MonoDefaultPopup" /> component)
|
|
/// </para>
|
|
/// NEG_UI_DISABLE_WARNING_DEFAULT_SELECTION
|
|
/// </summary>
|
|
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<GameObject>("UI/PopupCanvas");
|
|
var defaultPopup = Resources.Load<GameObject>("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<Canvas>());
|
|
Assert.IsNotNull(defaultPopup.GetComponent<MonoDefaultPopup>());
|
|
|
|
canvasPrefab = popupCanvas;
|
|
defaultPopupPrefab = defaultPopup.GetComponent<MonoDefaultPopup>();
|
|
|
|
SpawnDefaultPopup();
|
|
|
|
SceneManager.activeSceneChanged += (_, _) => SpawnDefaultPopup();
|
|
|
|
BehavioursFactory = new KeyBasedFactory<string, ButtonElementBehaviour>();
|
|
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<string, ButtonElementBehaviour> 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);
|
|
}
|
|
}
|
|
} |