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.

SlowTapInteraction.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. ////REVIEW: this is confusing when considered next to HoldInteraction; also it's confusingly named
  10. namespace UnityEngine.InputSystem.Interactions
  11. {
  12. /// <summary>
  13. /// Performs the action if the control is pressed and held for at least the set
  14. /// duration (which defaults to <see cref="InputSettings.defaultSlowTapTime"/>)
  15. /// and then released.
  16. /// </summary>
  17. [DisplayName("Long Tap")]
  18. public class SlowTapInteraction : IInputInteraction
  19. {
  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.defaultSlowTapTime"/>) 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. ////REVIEW: this seems stupid; shouldn't a slow tap just be anything that takes longer than TapTime?
  37. private float durationOrDefault => duration > 0.0f ? duration : InputSystem.settings.defaultSlowTapTime;
  38. private float pressPointOrDefault => pressPoint > 0 ? pressPoint : ButtonControl.s_GlobalDefaultButtonPressPoint;
  39. private double m_SlowTapStartTime;
  40. public void Process(ref InputInteractionContext context)
  41. {
  42. if (context.isWaiting && context.ControlIsActuated(pressPointOrDefault))
  43. {
  44. m_SlowTapStartTime = context.time;
  45. context.Started();
  46. return;
  47. }
  48. if (context.isStarted && !context.ControlIsActuated(pressPointOrDefault))
  49. {
  50. if (context.time - m_SlowTapStartTime >= durationOrDefault)
  51. context.Performed();
  52. else
  53. ////REVIEW: does it matter to cancel right after expiration of 'duration' or is it enough to cancel on button up like here?
  54. context.Canceled();
  55. }
  56. }
  57. public void Reset()
  58. {
  59. m_SlowTapStartTime = 0.0;
  60. }
  61. }
  62. #if UNITY_EDITOR
  63. internal class SlowTapInteractionEditor : InputParameterEditor<SlowTapInteraction>
  64. {
  65. protected override void OnEnable()
  66. {
  67. m_DurationSetting.Initialize("Min Tap Duration",
  68. "Minimum time (in seconds) that a control has to be held for it to register as a slow tap. If the control is released "
  69. + "before this time, the slow tap is canceled.",
  70. "Default Slow Tap Time",
  71. () => target.duration, x => target.duration = x, () => InputSystem.settings.defaultSlowTapTime);
  72. m_PressPointSetting.Initialize("Press Point",
  73. "The amount of actuation a control requires before being considered pressed. If not set, default to "
  74. + "'Default Button Press Point' in the global input settings.",
  75. "Default Button Press Point",
  76. () => target.pressPoint, v => target.pressPoint = v,
  77. () => InputSystem.settings.defaultButtonPressPoint);
  78. }
  79. public override void OnGUI()
  80. {
  81. m_DurationSetting.OnGUI();
  82. m_PressPointSetting.OnGUI();
  83. }
  84. #if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
  85. public override void OnDrawVisualElements(VisualElement root, Action onChangedCallback)
  86. {
  87. m_DurationSetting.OnDrawVisualElements(root, onChangedCallback);
  88. m_PressPointSetting.OnDrawVisualElements(root, onChangedCallback);
  89. }
  90. #endif
  91. private CustomOrDefaultSetting m_DurationSetting;
  92. private CustomOrDefaultSetting m_PressPointSetting;
  93. }
  94. #endif
  95. }