78 lines
2.5 KiB
C#
78 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace DDD
|
|
{
|
|
public class PopupUi : BaseUi
|
|
{
|
|
protected UiInputBindingSo _uiInputBindingSo;
|
|
protected readonly List<(InputAction action, Action<InputAction.CallbackContext> handler)> _registeredHandlers = new();
|
|
|
|
protected override async void TryRegister()
|
|
{
|
|
base.TryRegister();
|
|
UiManager.Instance.RegisterPopupUI(this);
|
|
|
|
// So의 이름을 통일 : TestUi_UiInputBindingSo
|
|
string addressableKey = $"{GetType().Name}_{DataConstants.UiInputBindingSo}";
|
|
_uiInputBindingSo = await AssetManager.LoadAsset<UiInputBindingSo>(addressableKey);
|
|
Debug.Assert(_uiInputBindingSo != null, "_uiInputBindingSo != null");
|
|
|
|
foreach (var binding in _uiInputBindingSo.Bindings)
|
|
{
|
|
if (binding.InputAction == null) continue;
|
|
|
|
var action = binding.InputAction.action;
|
|
if (action == null) continue;
|
|
|
|
var handler = new Action<InputAction.CallbackContext>(ctx =>
|
|
{
|
|
if (UiManager.Instance.IsTopPopup(this))
|
|
{
|
|
OnInputPerformed(binding.ActionName, ctx);
|
|
}
|
|
});
|
|
|
|
action.Enable();
|
|
action.performed += handler;
|
|
|
|
_registeredHandlers.Add((action, 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(_uiInputBindingSo.InputActionMaps);
|
|
}
|
|
}
|
|
|
|
protected virtual void OnInputPerformed(string actionName, InputAction.CallbackContext ctx) { }
|
|
|
|
public InputActionMaps GetInputActionMaps() => _uiInputBindingSo.InputActionMaps;
|
|
}
|
|
} |