ProjectDDD/Assets/_DDD/_Scripts/GameUi/PopupUi.cs

118 lines
4.2 KiB
C#
Raw Normal View History

2025-07-22 07:46:37 +00:00
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
2025-07-21 07:53:39 +00:00
namespace DDD
{
public static class EnumExtensions
2025-07-21 07:53:39 +00:00
{
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;
2025-07-22 07:46:37 +00:00
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";
2025-07-21 07:53:39 +00:00
2025-07-22 07:46:37 +00:00
protected override async void TryRegister()
{
base.TryRegister();
2025-07-22 07:46:37 +00:00
UiManager.Instance.RegisterPopupUI(this);
string addressableKey = $"{GetType().Name}_{typeof(T).Name}_{InputBindingSo}";
_baseUiActionsInputBindingSo = await AssetManager.LoadAsset<BaseUiActionsInputBindingSo<T>>(addressableKey);
2025-08-04 11:01:43 +00:00
Debug.Assert(_baseUiActionsInputBindingSo != null, $"{GetType().Name} class InputBindingSo not found: {addressableKey}");
2025-07-22 07:46:37 +00:00
foreach (var actionEnum in _baseUiActionsInputBindingSo.BindingActions.GetFlags())
2025-07-22 07:46:37 +00:00
{
if (actionEnum.Equals(default(T))) continue;
var inputAction = InputManager.Instance.GetAction(_baseUiActionsInputBindingSo.InputActionMaps, actionEnum.ToString());
if (inputAction == null) continue;
2025-07-22 07:46:37 +00:00
2025-08-04 11:01:43 +00:00
var startedHandler = new Action<InputAction.CallbackContext>(context =>
2025-07-22 07:46:37 +00:00
{
2025-08-04 11:01:43 +00:00
OnInputStarted(actionEnum, context);
});
inputAction.started += startedHandler;
2025-07-22 07:46:37 +00:00
2025-08-04 11:01:43 +00:00
var performedHandler = new Action<InputAction.CallbackContext>(context =>
{
OnInputPerformed(actionEnum, context);
});
2025-08-04 11:01:43 +00:00
inputAction.performed += performedHandler;
2025-07-22 07:46:37 +00:00
2025-08-04 11:01:43 +00:00
var canceledHandler = new Action<InputAction.CallbackContext>(context =>
{
OnInputCanceled(actionEnum, context);
});
inputAction.canceled += canceledHandler;
_registeredHandlers.Add((inputAction, startedHandler));
_registeredHandlers.Add((inputAction, performedHandler));
_registeredHandlers.Add((inputAction, canceledHandler));
2025-07-22 07:46:37 +00:00
}
}
protected override void TryUnregister()
{
base.TryUnregister();
UiManager.Instance.UnregisterPopupUI(this);
foreach (var (action, handler) in _registeredHandlers)
{
if (action != null)
{
2025-08-04 11:01:43 +00:00
action.started -= handler;
2025-07-22 07:46:37 +00:00
action.performed -= handler;
2025-08-04 11:01:43 +00:00
action.canceled -= handler;
2025-07-22 07:46:37 +00:00
}
}
_registeredHandlers.Clear();
}
2025-08-05 04:00:01 +00:00
public override void Open(OpenPopupUiEvent evt)
2025-07-22 07:46:37 +00:00
{
2025-08-05 04:00:01 +00:00
base.Open(evt);
2025-07-22 07:46:37 +00:00
transform.SetAsLastSibling();
if (UiManager.Instance.IsTopPopup(this))
{
InputManager.Instance.SwitchCurrentActionMap(_baseUiActionsInputBindingSo.InputActionMaps);
2025-07-22 07:46:37 +00:00
}
}
2025-08-05 04:00:01 +00:00
protected virtual bool OnInputStarted(T actionEnum, InputAction.CallbackContext context)
2025-08-04 11:01:43 +00:00
{
2025-08-05 04:00:01 +00:00
return _isTopPopup;
2025-08-04 11:01:43 +00:00
}
2025-08-05 04:00:01 +00:00
protected virtual bool OnInputPerformed(T actionEnum, InputAction.CallbackContext context)
2025-08-04 11:01:43 +00:00
{
2025-08-05 04:00:01 +00:00
return _isTopPopup;
2025-08-04 11:01:43 +00:00
}
2025-08-05 04:00:01 +00:00
protected virtual bool OnInputCanceled(T actionEnum, InputAction.CallbackContext context)
2025-08-04 11:01:43 +00:00
{
2025-08-05 04:00:01 +00:00
return _isTopPopup;
2025-08-04 11:01:43 +00:00
}
2025-07-21 07:53:39 +00:00
}
}