119 lines
3.1 KiB
C#
119 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace DDD
|
|
{
|
|
public static class InputActionMapExtensions
|
|
{
|
|
public static string ToName(this InputActionMaps map)
|
|
{
|
|
return map.ToString();
|
|
}
|
|
}
|
|
|
|
public enum InputActionMaps
|
|
{
|
|
None = 0,
|
|
Ui = 1,
|
|
Restaurant = 2,
|
|
RestaurantUi = 3
|
|
}
|
|
|
|
[Flags]
|
|
public enum RestaurantActions
|
|
{
|
|
None = 0,
|
|
Move = 1 << 0,
|
|
Dash = 1 << 1,
|
|
OpenManagementUi = 1 << 2,
|
|
}
|
|
|
|
[Flags]
|
|
public enum RestaurantUiActions
|
|
{
|
|
None = 0,
|
|
Submit = 1 << 0,
|
|
Cancel = 1 << 1,
|
|
PreviousTab = 1 << 2,
|
|
NextTab = 1 << 3,
|
|
Interact1 = 1 << 4,
|
|
Interact2 = 1 << 5,
|
|
}
|
|
|
|
public class InputManager : Singleton<InputManager>, IManager
|
|
{
|
|
private PlayerInput _currentPlayerInput;
|
|
|
|
public InputActionMaps CurrentInputActionMap { get; private set; }
|
|
|
|
private readonly Dictionary<(InputActionMaps, string), InputAction> _cachedActions = new();
|
|
|
|
public void PreInit()
|
|
{
|
|
_currentPlayerInput = GetComponent<PlayerInput>();
|
|
|
|
_cachedActions.Clear();
|
|
foreach (var actionMap in _currentPlayerInput.actions.actionMaps)
|
|
{
|
|
if (!Enum.TryParse(actionMap.name, out InputActionMaps mapEnum)) continue;
|
|
|
|
foreach (var action in actionMap.actions)
|
|
{
|
|
_cachedActions[(mapEnum, action.name)] = action;
|
|
}
|
|
}
|
|
}
|
|
|
|
public Task Init()
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public void PostInit()
|
|
{
|
|
|
|
}
|
|
|
|
private bool IsNullCurrentPlayerInput()
|
|
{
|
|
if (_currentPlayerInput && _currentPlayerInput.enabled) return false;
|
|
|
|
Debug.Log("CurrentPlayerInput가 할당되지 않았습니다.");
|
|
return true;
|
|
}
|
|
|
|
public void SwitchCurrentActionMap(InputActionMaps inputActionMaps)
|
|
{
|
|
if (IsNullCurrentPlayerInput() || inputActionMaps == InputActionMaps.None)
|
|
{
|
|
CurrentInputActionMap = InputActionMaps.None;
|
|
return;
|
|
}
|
|
|
|
CurrentInputActionMap = inputActionMaps;
|
|
_currentPlayerInput.SwitchCurrentActionMap(inputActionMaps.ToName());
|
|
}
|
|
|
|
public InputAction GetAction(InputActionMaps map, string actionName)
|
|
{
|
|
if (_cachedActions.TryGetValue((map, actionName), out var action))
|
|
{
|
|
return action;
|
|
}
|
|
|
|
Debug.LogError($"[InputManager] Action '{actionName}' in Map '{map}' not found!");
|
|
return null;
|
|
}
|
|
|
|
public InputAction GetAction<TActionEnum>(InputActionMaps map, TActionEnum actionEnum)
|
|
where TActionEnum : Enum
|
|
{
|
|
return GetAction(map, actionEnum.ToString());
|
|
}
|
|
|
|
public InputActionMaps GetCurrentActionMap() => CurrentInputActionMap;
|
|
}
|
|
} |