Няма описание
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using UnityEngine.InputSystem.LowLevel;
  3. ////REVIEW: should we keep an explicit playback status? ATM calling ResumeHaptics() will re-issue last set motor speed regardless of pause state
  4. namespace UnityEngine.InputSystem.Haptics
  5. {
  6. /// <summary>
  7. /// Common implementation of dual motor rumbling.
  8. /// </summary>
  9. /// <remarks>
  10. /// This struct is meant for use in devices that implement <see cref="IDualMotorRumble"/>.
  11. /// </remarks>
  12. internal struct DualMotorRumble
  13. {
  14. /// <summary>
  15. /// Normalized [0..1] speed of the low-frequency (usually left) motor.
  16. /// </summary>
  17. /// <value>Speed of left motor.</value>
  18. public float lowFrequencyMotorSpeed { get; private set; }
  19. /// <summary>
  20. /// Normalized [0..1] speed of the high-frequency (usually right) motor.
  21. /// </summary>
  22. /// <value>Speed of right motor.</value>
  23. public float highFrequencyMotorSpeed { get; private set; }
  24. /// <summary>
  25. /// Whether either of the motors is currently set to non-zero speeds.
  26. /// </summary>
  27. /// <value>True if the motors are currently turned on.</value>
  28. /// <remarks>
  29. /// Does not take pausing into account, i.e. <see cref="lowFrequencyMotorSpeed"/> and/or
  30. /// <see cref="highFrequencyMotorSpeed"/> may be non-zero but haptics on the device
  31. /// may actually be paused with <see cref="PauseHaptics"/>.
  32. /// </remarks>
  33. public bool isRumbling =>
  34. !Mathf.Approximately(lowFrequencyMotorSpeed, 0f)
  35. || !Mathf.Approximately(highFrequencyMotorSpeed, 0f);
  36. /// <summary>
  37. /// Reset motor speeds to zero but retain current values for <see cref="lowFrequencyMotorSpeed"/>
  38. /// and <see cref="highFrequencyMotorSpeed"/>.
  39. /// </summary>
  40. /// <param name="device">Device to send command to.</param>
  41. /// <exception cref="ArgumentNullException"><paramref name="device"/> is null.</exception>
  42. public void PauseHaptics(InputDevice device)
  43. {
  44. if (device == null)
  45. throw new ArgumentNullException("device");
  46. if (!isRumbling)
  47. return;
  48. var command = DualMotorRumbleCommand.Create(0f, 0f);
  49. device.ExecuteCommand(ref command);
  50. }
  51. /// <summary>
  52. /// Resume haptics by setting motor speeds to the current values of <see cref="lowFrequencyMotorSpeed"/>
  53. /// and <see cref="highFrequencyMotorSpeed"/>.
  54. /// </summary>
  55. /// <param name="device">Device to send command to.</param>
  56. /// <exception cref="ArgumentNullException"><paramref name="device"/> is null.</exception>
  57. public void ResumeHaptics(InputDevice device)
  58. {
  59. if (device == null)
  60. throw new ArgumentNullException("device");
  61. if (!isRumbling)
  62. return;
  63. SetMotorSpeeds(device, lowFrequencyMotorSpeed, highFrequencyMotorSpeed);
  64. }
  65. /// <summary>
  66. /// Reset haptics by setting both <see cref="lowFrequencyMotorSpeed"/> and <see cref="highFrequencyMotorSpeed"/>
  67. /// to zero.
  68. /// </summary>
  69. /// <param name="device">Device to send command to.</param>
  70. /// <exception cref="ArgumentNullException"><paramref name="device"/> is null.</exception>
  71. public void ResetHaptics(InputDevice device)
  72. {
  73. if (device == null)
  74. throw new ArgumentNullException("device");
  75. if (!isRumbling)
  76. return;
  77. SetMotorSpeeds(device, 0.0f, 0.0f);
  78. }
  79. /// <summary>
  80. /// Set the speed of the low-frequency (usually left) and high-frequency (usually right) motor
  81. /// on <paramref name="device"/>. Updates <see cref="lowFrequencyMotorSpeed"/> and
  82. /// <see cref="highFrequencyMotorSpeed"/>.
  83. /// </summary>
  84. /// <param name="device">Device to send command to.</param>
  85. /// <param name="lowFrequency">Speed of the low-frequency (left) motor. Normalized [0..1] value
  86. /// with 1 indicating maximum speed and 0 indicating the motor is turned off. Will automatically
  87. /// be clamped into range.</param>
  88. /// <param name="highFrequency">Speed of the high-frequency (right) motor. Normalized [0..1] value
  89. /// with 1 indicating maximum speed and 0 indicating the motor is turned off. Will automatically
  90. /// be clamped into range.</param>
  91. /// <remarks>
  92. /// Sends <see cref="DualMotorRumbleCommand"/> to <paramref name="device"/>.
  93. /// </remarks>
  94. /// <exception cref="ArgumentNullException"><paramref name="device"/> is null.</exception>
  95. public void SetMotorSpeeds(InputDevice device, float lowFrequency, float highFrequency)
  96. {
  97. if (device == null)
  98. throw new ArgumentNullException("device");
  99. lowFrequencyMotorSpeed = Mathf.Clamp(lowFrequency, 0.0f, 1.0f);
  100. highFrequencyMotorSpeed = Mathf.Clamp(highFrequency, 0.0f, 1.0f);
  101. var command = DualMotorRumbleCommand.Create(lowFrequencyMotorSpeed, highFrequencyMotorSpeed);
  102. device.ExecuteCommand(ref command);
  103. }
  104. }
  105. }