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.

TapInteraction.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.ComponentModel;
  3. using UnityEngine.InputSystem.Controls;
  4. using UnityEngine.Scripting;
  5. #if UNITY_EDITOR
  6. using UnityEngine.InputSystem.Editor;
  7. using UnityEngine.UIElements;
  8. #endif
  9. namespace UnityEngine.InputSystem.Interactions
  10. {
  11. /// <summary>
  12. /// Performs the action if the control is pressed held for at least the set
  13. /// duration (which defaults to <see cref="InputSettings.defaultTapTime"/>)
  14. /// and then released.
  15. /// </summary>
  16. [DisplayName("Tap")]
  17. public class TapInteraction : IInputInteraction
  18. {
  19. ////REVIEW: this should be called tapTime
  20. /// <summary>
  21. /// The time in seconds within which the control needs to be pressed and released to perform the interaction.
  22. /// </summary>
  23. /// <remarks>
  24. /// If this value is equal to or smaller than zero, the input system will use (<see cref="InputSettings.defaultTapTime"/>) instead.
  25. /// </remarks>
  26. public float duration;
  27. /// <summary>
  28. /// The press point required to perform the interaction.
  29. /// </summary>
  30. /// <remarks>
  31. /// For analog controls (such as trigger axes on a gamepad), the control needs to be engaged by at least this
  32. /// value to perform the interaction.
  33. /// If this value is equal to or smaller than zero, the input system will use (<see cref="InputSettings.defaultButtonPressPoint"/>) instead.
  34. /// </remarks>
  35. public float pressPoint;
  36. private float durationOrDefault => duration > 0.0 ? duration : InputSystem.settings.defaultTapTime;
  37. private float pressPointOrDefault => pressPoint > 0 ? pressPoint : ButtonControl.s_GlobalDefaultButtonPressPoint;
  38. private float releasePointOrDefault => pressPointOrDefault * ButtonControl.s_GlobalDefaultButtonReleaseThreshold;
  39. private double m_TapStartTime;
  40. ////TODO: make sure 2d doesn't move too far
  41. public void Process(ref InputInteractionContext context)
  42. {
  43. if (context.timerHasExpired)
  44. {
  45. context.Canceled();
  46. return;
  47. }
  48. if (context.isWaiting && context.ControlIsActuated(pressPointOrDefault))
  49. {
  50. m_TapStartTime = context.time;
  51. // Set timeout slightly after duration so that if tap comes in exactly at the expiration
  52. // time, it still counts as a valid tap.
  53. context.Started();
  54. context.SetTimeout(durationOrDefault + 0.00001f);
  55. return;
  56. }
  57. if (context.isStarted && !context.ControlIsActuated(releasePointOrDefault))
  58. {
  59. if (context.time - m_TapStartTime <= durationOrDefault)
  60. {
  61. context.Performed();
  62. }
  63. else
  64. {
  65. ////REVIEW: does it matter to cancel right after expiration of 'duration' or is it enough to cancel on button up like here?
  66. context.Canceled();
  67. }
  68. }
  69. }
  70. public void Reset()
  71. {
  72. m_TapStartTime = 0;
  73. }
  74. }
  75. #if UNITY_EDITOR
  76. internal class TapInteractionEditor : InputParameterEditor<TapInteraction>
  77. {
  78. protected override void OnEnable()
  79. {
  80. m_DurationSetting.Initialize("Max Tap Duration",
  81. "Time (in seconds) within with a control has to be released again for it to register as a tap. If the control is held "
  82. + "for longer than this time, the tap is canceled.",
  83. "Default Tap Time",
  84. () => target.duration, x => target.duration = x, () => InputSystem.settings.defaultTapTime);
  85. m_PressPointSetting.Initialize("Press Point",
  86. "The amount of actuation a control requires before being considered pressed. If not set, default to "
  87. + "'Default Button Press Point' in the global input settings.",
  88. "Default Button Press Point",
  89. () => target.pressPoint, v => target.pressPoint = v,
  90. () => InputSystem.settings.defaultButtonPressPoint);
  91. }
  92. public override void OnGUI()
  93. {
  94. m_DurationSetting.OnGUI();
  95. m_PressPointSetting.OnGUI();
  96. }
  97. #if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
  98. public override void OnDrawVisualElements(VisualElement root, Action onChangedCallback)
  99. {
  100. m_DurationSetting.OnDrawVisualElements(root, onChangedCallback);
  101. m_PressPointSetting.OnDrawVisualElements(root, onChangedCallback);
  102. }
  103. #endif
  104. private CustomOrDefaultSetting m_DurationSetting;
  105. private CustomOrDefaultSetting m_PressPointSetting;
  106. }
  107. #endif
  108. }