/*
Yarn Spinner is licensed to you under the terms found in the file LICENSE.md.
*/
using UnityEngine;
using System.Collections.Generic;
#if USE_TMP
#else
using TextMeshProUGUI = Yarn.Unity.TMPShim;
using TMP_Text = Yarn.Unity.TMPShim;
#endif
#nullable enable
namespace Yarn.Unity
{
public static class InputSystemAvailability
{
#if USE_INPUTSYSTEM
internal const bool inputSystemInstalled = true;
#else
internal const bool inputSystemInstalled = false;
#endif
#if ENABLE_INPUT_SYSTEM
internal const bool enableInputSystem = true;
#else
internal const bool enableInputSystem = false;
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
internal const bool enableLegacyInput = true;
#else
internal const bool enableLegacyInput = false;
#endif
#if !ENABLE_LEGACY_INPUT_MANAGER
///
/// A dictionary mapping legacy keycodes to Input System keys.
///
static System.Lazy> lookup = new(() =>
{
var result = new Dictionary();
foreach (KeyCode keyCode in System.Enum.GetValues(typeof(KeyCode)))
{
// Attempt to automatically find the equivalent of keyCode by
// assuming that the string representation of a key (e.g. "Tab")
// is the same in both enums.
if (System.Enum.TryParse(keyCode.ToString(), true, out var value))
{
result[keyCode] = value;
}
}
// Manually map some remaining keys
result[KeyCode.Return] = UnityEngine.InputSystem.Key.Enter;
result[KeyCode.KeypadEnter] = UnityEngine.InputSystem.Key.NumpadEnter;
return result;
});
#endif
///
/// Gets a value indicating whether the key indicated by a was pressed this frame.
///
///
/// If the Legacy Input Manager is enabled, this method wraps . Otherwise, it attempts to find the equivalent of , and then checks with to find the key,
/// and queries its
/// property.
///
/// The to check for the state
/// of.
/// Whether the key was pressed this frame.
public static bool GetKeyDown(KeyCode key)
{
if (key == KeyCode.None)
{
// The 'none' key is never pressed
return false;
}
#if ENABLE_LEGACY_INPUT_MANAGER
// If we're using Legacy Input, read from it directly
return Input.GetKeyDown(key);
#else
if (lookup.Value.TryGetValue(key, out var lookupKey))
{
try
{
return UnityEngine.InputSystem.Keyboard.current[lookup.Value[key]].wasPressedThisFrame;
}
catch (System.ArgumentOutOfRangeException)
{
#if DEBUG
Debug.LogWarning($"Can't check if {key} is down: found Input System mapping {lookupKey}, but this key is not present in the current keyboard");
#endif
return false;
}
}
else
{
#if DEBUG
Debug.LogWarning($"Can't check if {key} is down: can't find a mapping from legacy keycode {key} to Unity Input System");
#endif
return false;
}
#endif
}
public static bool GetButtonDown(string? buttonName)
{
if (string.IsNullOrEmpty(buttonName))
{
return false;
}
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.GetButtonUp(buttonName);
#else
return false;
#endif
}
public static float GetAxis(string? axisName)
{
if (string.IsNullOrEmpty(axisName))
{
return 0;
}
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.GetAxis(axisName);
#else
return 0;
#endif
}
}
}