No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InputHelper.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #if INPUT_SYSTEM_ENABLED
  2. using UnityEngine;
  3. using UnityEngine.InputSystem;
  4. using UnityEngine.InputSystem.LowLevel;
  5. namespace XCharts.Runtime
  6. {
  7. public class InputHelper
  8. {
  9. public static Vector2 mousePosition
  10. {
  11. get
  12. {
  13. var value = Vector2.zero;
  14. if (null != Mouse.current)
  15. {
  16. value = Mouse.current.position.ReadValue();
  17. }
  18. else if (null != Touchscreen.current && Touchscreen.current.touches.Count > 0)
  19. {
  20. value = Touchscreen.current.touches[0].position.ReadValue();
  21. }
  22. return value;
  23. }
  24. }
  25. public static int touchCount
  26. {
  27. get
  28. {
  29. var value = 0;
  30. if (null != Touchscreen.current)
  31. {
  32. value = Touchscreen.current.touches.Count;
  33. }
  34. return value;
  35. }
  36. }
  37. public static Touch GetTouch(int v)
  38. {
  39. UnityEngine.TouchPhase PhaseConvert(TouchState state)
  40. {
  41. UnityEngine.TouchPhase temp = UnityEngine.TouchPhase.Began;
  42. switch (state.phase)
  43. {
  44. case UnityEngine.InputSystem.TouchPhase.Began:
  45. temp = UnityEngine.TouchPhase.Began;
  46. break;
  47. case UnityEngine.InputSystem.TouchPhase.Moved:
  48. temp = UnityEngine.TouchPhase.Moved;
  49. break;
  50. case UnityEngine.InputSystem.TouchPhase.Canceled:
  51. temp = UnityEngine.TouchPhase.Canceled;
  52. break;
  53. case UnityEngine.InputSystem.TouchPhase.Stationary:
  54. temp = UnityEngine.TouchPhase.Stationary;
  55. break;
  56. default:
  57. case UnityEngine.InputSystem.TouchPhase.Ended:
  58. case UnityEngine.InputSystem.TouchPhase.None:
  59. temp = UnityEngine.TouchPhase.Ended;
  60. break;
  61. }
  62. return temp;
  63. }
  64. var touch = Touchscreen.current.touches[v];
  65. var value = touch.ReadValue();
  66. //copy touchcontrol's touchstate data into touch
  67. return new Touch
  68. {
  69. deltaPosition = value.delta,
  70. fingerId = value.touchId,
  71. position = value.position,
  72. phase = PhaseConvert(value),
  73. pressure = value.pressure,
  74. radius = value.radius.magnitude,
  75. radiusVariance = value.radius.sqrMagnitude,
  76. type = value.isPrimaryTouch ? TouchType.Direct : TouchType.Indirect,
  77. tapCount = value.tapCount,
  78. deltaTime = Time.realtimeSinceStartup - (float)value.startTime,
  79. rawPosition = value.startPosition,
  80. };
  81. }
  82. public static bool GetKeyDown(KeyCode keyCode)
  83. {
  84. var value = false;
  85. if (null != Keyboard.current)
  86. {
  87. var key = Keyboard.current.spaceKey;
  88. switch (keyCode)
  89. {
  90. case KeyCode.Space:
  91. key = Keyboard.current.spaceKey;
  92. break;
  93. case KeyCode.L:
  94. key = Keyboard.current.lKey;
  95. break;
  96. default:
  97. Debug.LogError($"{nameof(InputHelper)}: not support {keyCode} yet , please add it yourself if needed");
  98. break;
  99. }
  100. value = key.wasPressedThisFrame;
  101. }
  102. return value;
  103. }
  104. }
  105. }
  106. #endif