ProjectDDD/Assets/_DDD/_Scripts/InputSystem/InputManager.cs

119 lines
3.1 KiB
C#
Raw Normal View History

2025-07-09 09:45:11 +00:00
using System;
using System.Collections.Generic;
2025-07-22 07:46:37 +00:00
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.InputSystem;
namespace DDD
{
2025-07-22 07:46:37 +00:00
public static class InputActionMapExtensions
{
2025-07-22 07:46:37 +00:00
public static string ToName(this InputActionMaps map)
{
return map.ToString();
}
}
2025-07-09 09:45:11 +00:00
2025-07-22 07:46:37 +00:00
public enum InputActionMaps
{
2025-07-09 09:45:11 +00:00
None = 0,
2025-07-22 07:46:37 +00:00
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,
}
2025-07-22 07:46:37 +00:00
public class InputManager : Singleton<InputManager>, IManager
{
private PlayerInput _currentPlayerInput;
public InputActionMaps CurrentInputActionMap { get; private set; }
private readonly Dictionary<(InputActionMaps, string), InputAction> _cachedActions = new();
2025-07-22 07:46:37 +00:00
public void PreInit()
{
2025-07-22 07:46:37 +00:00
_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;
}
}
}
2025-07-22 07:46:37 +00:00
public Task Init()
{
2025-07-22 07:46:37 +00:00
return Task.CompletedTask;
}
2025-07-22 07:46:37 +00:00
public void PostInit()
{
2025-07-22 07:46:37 +00:00
}
2025-07-22 07:46:37 +00:00
private bool IsNullCurrentPlayerInput()
{
2025-07-22 07:46:37 +00:00
if (_currentPlayerInput && _currentPlayerInput.enabled) return false;
2025-07-22 07:46:37 +00:00
Debug.Log("CurrentPlayerInput가 할당되지 않았습니다.");
return true;
}
public void SwitchCurrentActionMap(InputActionMaps inputActionMaps)
{
if (IsNullCurrentPlayerInput() || inputActionMaps == InputActionMaps.None)
{
CurrentInputActionMap = InputActionMaps.None;
return;
}
CurrentInputActionMap = inputActionMaps;
2025-07-22 07:46:37 +00:00
_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;
}
}