92 lines
3.2 KiB
C#
92 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace DDD
|
|
{
|
|
public static class EnumExtensions
|
|
{
|
|
public static IEnumerable<T> GetFlags<T>(this T input) where T : Enum
|
|
{
|
|
foreach (T value in Enum.GetValues(typeof(T)))
|
|
{
|
|
int intValue = Convert.ToInt32(value);
|
|
int inputValue = Convert.ToInt32(input);
|
|
if (intValue != 0 && (inputValue & intValue) == intValue) yield return value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public abstract class PopupUi<T> : BasePopupUi where T : Enum
|
|
{
|
|
protected BaseUiActionsInputBindingSo<T> _baseUiActionsInputBindingSo;
|
|
protected readonly List<(InputAction action, Action<InputAction.CallbackContext> handler)> _registeredHandlers = new();
|
|
public override InputActionMaps InputActionMaps => _baseUiActionsInputBindingSo.InputActionMaps;
|
|
|
|
private bool _isTopPopup => UiManager.Instance.IsTopPopup(this);
|
|
|
|
private const string InputBindingSo = "InputBindingSo";
|
|
|
|
protected override async void TryRegister()
|
|
{
|
|
base.TryRegister();
|
|
|
|
UiManager.Instance.RegisterPopupUI(this);
|
|
|
|
string addressableKey = $"{GetType().Name}_{typeof(T).Name}_{InputBindingSo}";
|
|
_baseUiActionsInputBindingSo = await AssetManager.LoadAsset<BaseUiActionsInputBindingSo<T>>(addressableKey);
|
|
|
|
Debug.Assert(_baseUiActionsInputBindingSo != null, $"InputBindingSo not found: {addressableKey}");
|
|
|
|
foreach (var actionEnum in _baseUiActionsInputBindingSo.BindingActions.GetFlags())
|
|
{
|
|
if (actionEnum.Equals(default(T))) continue;
|
|
|
|
var inputAction = InputManager.Instance.GetAction(_baseUiActionsInputBindingSo.InputActionMaps, actionEnum.ToString());
|
|
if (inputAction == null) continue;
|
|
|
|
var handler = new Action<InputAction.CallbackContext>(context =>
|
|
{
|
|
if (!_isTopPopup) return;
|
|
|
|
OnInputPerformed(actionEnum, context);
|
|
});
|
|
|
|
inputAction.performed += handler;
|
|
_registeredHandlers.Add((inputAction, handler));
|
|
}
|
|
}
|
|
|
|
protected override void TryUnregister()
|
|
{
|
|
base.TryUnregister();
|
|
UiManager.Instance.UnregisterPopupUI(this);
|
|
|
|
foreach (var (action, handler) in _registeredHandlers)
|
|
{
|
|
if (action != null)
|
|
{
|
|
action.performed -= handler;
|
|
action.Disable();
|
|
}
|
|
}
|
|
|
|
_registeredHandlers.Clear();
|
|
}
|
|
|
|
public override void Open()
|
|
{
|
|
base.Open();
|
|
|
|
transform.SetAsLastSibling();
|
|
|
|
if (UiManager.Instance.IsTopPopup(this))
|
|
{
|
|
InputManager.Instance.SwitchCurrentActionMap(_baseUiActionsInputBindingSo.InputActionMaps);
|
|
}
|
|
}
|
|
|
|
protected abstract void OnInputPerformed(T actionEnum, InputAction.CallbackContext ctx);
|
|
}
|
|
} |