2025-07-22 07:46:37 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2025-08-19 10:13:31 +00:00
|
|
|
using Sirenix.OdinInspector;
|
2025-07-22 07:46:37 +00:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
2025-07-21 07:53:39 +00:00
|
|
|
namespace DDD
|
|
|
|
{
|
2025-08-20 09:08:41 +00:00
|
|
|
public abstract class PopupUi<TInputEnum, TViewModel> : BasePopupUi
|
|
|
|
where TInputEnum : Enum
|
|
|
|
where TViewModel : SimpleViewModel
|
2025-07-23 03:24:52 +00:00
|
|
|
{
|
2025-08-20 09:08:41 +00:00
|
|
|
[SerializeField, Required] protected BaseUiActionsInputBinding<TInputEnum> _uiActionsInputBinding;
|
|
|
|
|
2025-07-22 07:46:37 +00:00
|
|
|
protected readonly List<(InputAction action, Action<InputAction.CallbackContext> handler)> _registeredHandlers = new();
|
2025-08-20 09:08:41 +00:00
|
|
|
protected TViewModel _viewModel;
|
|
|
|
|
|
|
|
protected override void Awake()
|
|
|
|
{
|
|
|
|
base.Awake();
|
|
|
|
|
|
|
|
_viewModel = GetComponent<TViewModel>();
|
|
|
|
}
|
2025-08-21 09:15:51 +00:00
|
|
|
|
2025-08-24 11:44:32 +00:00
|
|
|
protected override void OnOpenedEvents()
|
2025-08-20 09:08:41 +00:00
|
|
|
{
|
2025-08-24 11:44:32 +00:00
|
|
|
base.OnOpenedEvents();
|
2025-08-20 09:08:41 +00:00
|
|
|
|
|
|
|
if (_viewModel && _bindingContext != null)
|
|
|
|
{
|
|
|
|
_bindingContext.SetDataContext(_viewModel);
|
|
|
|
_viewModel.PropertyChanged += OnViewModelPropertyChanged;
|
|
|
|
}
|
2025-07-23 03:24:52 +00:00
|
|
|
|
2025-08-20 09:08:41 +00:00
|
|
|
// PopupUi의 입력 바인딩 등록
|
2025-08-19 10:13:31 +00:00
|
|
|
foreach (var actionEnum in _uiActionsInputBinding.BindingActions.GetFlags())
|
2025-07-22 07:46:37 +00:00
|
|
|
{
|
2025-08-20 09:08:41 +00:00
|
|
|
if (actionEnum.Equals(default(TInputEnum))) continue;
|
|
|
|
|
|
|
|
var inputAction =
|
|
|
|
InputManager.Instance.GetAction(_uiActionsInputBinding.InputActionMaps, actionEnum.ToString());
|
2025-07-23 03:24:52 +00:00
|
|
|
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 =>
|
|
|
|
{
|
2025-07-23 03:24:52 +00:00
|
|
|
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
|
|
|
}
|
2025-08-20 09:08:41 +00:00
|
|
|
|
|
|
|
InputActionMaps = _uiActionsInputBinding.InputActionMaps;
|
2025-07-22 07:46:37 +00:00
|
|
|
}
|
2025-08-20 09:08:41 +00:00
|
|
|
|
2025-08-24 11:44:32 +00:00
|
|
|
protected override void OnClosedEvents()
|
2025-07-22 07:46:37 +00:00
|
|
|
{
|
2025-08-24 11:44:32 +00:00
|
|
|
base.OnClosedEvents();
|
2025-08-21 09:15:51 +00:00
|
|
|
|
|
|
|
if (_viewModel != null)
|
|
|
|
{
|
|
|
|
_viewModel.PropertyChanged -= OnViewModelPropertyChanged;
|
|
|
|
}
|
2025-08-20 09:08:41 +00:00
|
|
|
|
|
|
|
// 입력 핸들러 해제
|
2025-07-22 07:46:37 +00:00
|
|
|
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-20 09:08:41 +00:00
|
|
|
|
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
|
|
|
|
2025-08-20 09:08:41 +00:00
|
|
|
_viewModel?.Initialize();
|
2025-07-22 07:46:37 +00:00
|
|
|
|
2025-08-20 09:08:41 +00:00
|
|
|
if (IsTopPopup)
|
2025-07-22 07:46:37 +00:00
|
|
|
{
|
2025-08-19 10:13:31 +00:00
|
|
|
InputManager.Instance.SwitchCurrentActionMap(_uiActionsInputBinding.InputActionMaps);
|
2025-07-22 07:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-08-20 09:08:41 +00:00
|
|
|
public override void Close()
|
2025-08-04 11:01:43 +00:00
|
|
|
{
|
2025-08-20 09:08:41 +00:00
|
|
|
base.Close();
|
|
|
|
|
|
|
|
_viewModel?.Cleanup();
|
2025-08-04 11:01:43 +00:00
|
|
|
}
|
|
|
|
|
2025-08-20 09:08:41 +00:00
|
|
|
// 입력 처리 메서드들
|
|
|
|
protected virtual bool OnInputStarted(TInputEnum actionEnum, InputAction.CallbackContext context) => IsTopPopup;
|
|
|
|
protected virtual bool OnInputPerformed(TInputEnum actionEnum, InputAction.CallbackContext context) => IsTopPopup;
|
|
|
|
protected virtual bool OnInputCanceled(TInputEnum actionEnum, InputAction.CallbackContext context) => IsTopPopup;
|
2025-07-21 07:53:39 +00:00
|
|
|
}
|
|
|
|
}
|