InputProxy.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. #if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
  5. using UnityEngine.InputSystem;
  6. #endif
  7. namespace SoftKitty
  8. {
  9. /// Please comment out the whole region of the input system you're not using.
  10. public class InputProxy : MonoBehaviour
  11. {
  12. #if ENABLE_LEGACY_INPUT_MANAGER
  13. public static Vector3 mousePosition
  14. {
  15. get
  16. {
  17. return Input.mousePosition;
  18. }
  19. }
  20. public static bool GetMouseButton(int _button)
  21. {
  22. return Input.GetMouseButton(_button);
  23. }
  24. public static bool GetMouseButtonDown(int _button)
  25. {
  26. return Input.GetMouseButtonDown(_button);
  27. }
  28. public static bool GetMouseButtonUp(int _button)
  29. {
  30. return Input.GetMouseButtonUp(_button);
  31. }
  32. public static bool GetKey(KeyCode _key)
  33. {
  34. return Input.GetKey(_key);
  35. }
  36. public static bool GetKeyUp(KeyCode _key)
  37. {
  38. return Input.GetKeyUp(_key);
  39. }
  40. public static bool GetKeyDown(KeyCode _key)
  41. {
  42. return Input.GetKeyDown(_key);
  43. }
  44. #else
  45. private static InputAction[] mouseButtonActions = new InputAction[3]{
  46. new InputAction(binding: "<Mouse>/leftButton"),
  47. new InputAction(binding: "<Mouse>/rightButton"),
  48. new InputAction(binding: "<Mouse>/middleButton")
  49. };
  50. private static Dictionary<KeyCode, Key> lookup;
  51. public static Vector3 mousePosition
  52. {
  53. get
  54. {
  55. return Mouse.current.position.ReadValue();
  56. }
  57. }
  58. public static bool GetMouseButton(int _button)
  59. {
  60. if (!mouseButtonActions[_button].enabled) mouseButtonActions[_button].Enable();
  61. return mouseButtonActions[_button].IsPressed();
  62. }
  63. public static bool GetMouseButtonDown(int _button)
  64. {
  65. if (!mouseButtonActions[_button].enabled) mouseButtonActions[_button].Enable();
  66. return mouseButtonActions[_button].WasPressedThisFrame();
  67. }
  68. public static bool GetMouseButtonUp(int _button)
  69. {
  70. if (!mouseButtonActions[_button].enabled) mouseButtonActions[_button].Enable();
  71. return mouseButtonActions[_button].WasReleasedThisFrame();
  72. }
  73. public static bool GetKey(KeyCode _key)
  74. {
  75. return Keyboard.current[GetKeyByKeyCode(_key)].IsPressed();
  76. }
  77. public static bool GetKeyUp(KeyCode _key)
  78. {
  79. return Keyboard.current[GetKeyByKeyCode(_key)].wasReleasedThisFrame;
  80. }
  81. public static bool GetKeyDown(KeyCode _key)
  82. {
  83. return Keyboard.current[GetKeyByKeyCode(_key)].wasPressedThisFrame;
  84. }
  85. private static Key GetKeyByKeyCode(KeyCode _key)
  86. {
  87. if (lookup == null)
  88. {
  89. lookup = new Dictionary<KeyCode, Key>();
  90. foreach (KeyCode keyCode in System.Enum.GetValues(typeof(KeyCode)))
  91. {
  92. var textVersion = keyCode.ToString().Replace("Alpha", "Digit").Replace("Keypad", "Numpad");
  93. if (System.Enum.TryParse<Key>(textVersion, true, out var value))
  94. lookup[keyCode] = value;
  95. }
  96. lookup[KeyCode.Return] = Key.Enter;
  97. lookup[KeyCode.KeypadEnter] = Key.NumpadEnter;
  98. }
  99. if (lookup.ContainsKey(_key))
  100. return lookup[_key];
  101. else
  102. return Key.None;
  103. }
  104. #endif
  105. }
  106. }