122 lines
4.7 KiB
C#
122 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace SepCore.InputModule.Runtime
|
|
{
|
|
public sealed class InputModuleDefaultPromptMap : IInputPromptMap
|
|
{
|
|
private readonly InputActionAsset _asset;
|
|
private readonly Dictionary<(InputActionId, InputDeviceKind), InputPrompt> _entries;
|
|
|
|
public InputModuleDefaultPromptMap(InputActionAsset asset = null)
|
|
{
|
|
_asset = asset;
|
|
_entries = new Dictionary<(InputActionId, InputDeviceKind), InputPrompt>();
|
|
PopulateKeyboardMouse();
|
|
PopulateGamepad();
|
|
PopulateTouch();
|
|
}
|
|
|
|
public bool TryGetPrompt(InputActionId actionId, InputDeviceKind deviceKind, out InputPrompt prompt)
|
|
{
|
|
if (_asset != null && TryDeriveFromOverride(actionId, deviceKind, out prompt))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return _entries.TryGetValue((actionId, deviceKind), out prompt);
|
|
}
|
|
|
|
private bool TryDeriveFromOverride(InputActionId actionId, InputDeviceKind deviceKind, out InputPrompt prompt)
|
|
{
|
|
prompt = InputPrompt.None;
|
|
|
|
InputAction action = _asset.FindAction(actionId.ToString());
|
|
if (action == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
string devicePrefix = GetDevicePrefix(deviceKind);
|
|
if (devicePrefix == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
for (int i = 0; i < action.bindings.Count; i++)
|
|
{
|
|
InputBinding binding = action.bindings[i];
|
|
|
|
if (binding.isComposite || binding.isPartOfComposite)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (binding.overridePath == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
string effectivePath = binding.effectivePath;
|
|
if (effectivePath != null && effectivePath.StartsWith(devicePrefix, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
string label = InputControlPath.ToHumanReadableString(effectivePath);
|
|
prompt = new InputPrompt(label);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private static string GetDevicePrefix(InputDeviceKind deviceKind)
|
|
{
|
|
switch (deviceKind)
|
|
{
|
|
case InputDeviceKind.KeyboardMouse: return "<Keyboard>";
|
|
case InputDeviceKind.Gamepad: return "<Gamepad>";
|
|
case InputDeviceKind.Touch: return "<Touchscreen>";
|
|
default: return null;
|
|
}
|
|
}
|
|
|
|
private void PopulateKeyboardMouse()
|
|
{
|
|
Add(InputActionId.Pause, InputDeviceKind.KeyboardMouse, "Esc", "keyboard_esc");
|
|
Add(InputActionId.Navigate, InputDeviceKind.KeyboardMouse, "WASD");
|
|
Add(InputActionId.Confirm, InputDeviceKind.KeyboardMouse, "Enter", "keyboard_enter");
|
|
Add(InputActionId.Cancel, InputDeviceKind.KeyboardMouse, "Esc", "keyboard_esc");
|
|
Add(InputActionId.Move, InputDeviceKind.KeyboardMouse, "移动", "keyboard_w|keyboard_a|keyboard_s|keyboard_d");
|
|
Add(InputActionId.Sprint, InputDeviceKind.KeyboardMouse, "L Shift", "keyboard_shift");
|
|
Add(InputActionId.Interact, InputDeviceKind.KeyboardMouse, "E", "keyboard_e");
|
|
}
|
|
|
|
private void PopulateGamepad()
|
|
{
|
|
Add(InputActionId.Pause, InputDeviceKind.Gamepad, "Menu", "gamepad_menu");
|
|
Add(InputActionId.Navigate, InputDeviceKind.Gamepad, "L Stick", "gamepad_ls");
|
|
Add(InputActionId.Confirm, InputDeviceKind.Gamepad, "A", "gamepad_a");
|
|
Add(InputActionId.Cancel, InputDeviceKind.Gamepad, "B", "gamepad_b");
|
|
Add(InputActionId.Move, InputDeviceKind.Gamepad, "移动", "gamepad_ls");
|
|
Add(InputActionId.Sprint, InputDeviceKind.Gamepad, "L3", "gamepad_press_ls");
|
|
Add(InputActionId.Interact, InputDeviceKind.Gamepad, "A", "gamepad_a");
|
|
}
|
|
|
|
private void PopulateTouch()
|
|
{
|
|
Add(InputActionId.Pause, InputDeviceKind.Touch, "Pause");
|
|
Add(InputActionId.Navigate, InputDeviceKind.Touch, "Swipe");
|
|
Add(InputActionId.Confirm, InputDeviceKind.Touch, "Tap");
|
|
Add(InputActionId.Cancel, InputDeviceKind.Touch, "Back");
|
|
Add(InputActionId.Move, InputDeviceKind.Touch, "Joystick");
|
|
Add(InputActionId.Interact, InputDeviceKind.Touch, "Tap");
|
|
}
|
|
|
|
private void Add(InputActionId actionId, InputDeviceKind deviceKind, string textLabel, string spriteName = null)
|
|
{
|
|
_entries[(actionId, deviceKind)] = new InputPrompt(textLabel, spriteName);
|
|
}
|
|
}
|
|
}
|