Açıklama Yok
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.

InputInteractionContext.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using System;
  2. using UnityEngine.InputSystem.LowLevel;
  3. namespace UnityEngine.InputSystem
  4. {
  5. /// <summary>
  6. /// Information passed to <see cref="IInputInteraction">interactions</see>
  7. /// when their associated controls trigger.
  8. /// </summary>
  9. /// <seealso cref="IInputInteraction.Process"/>
  10. public struct InputInteractionContext
  11. {
  12. /// <summary>
  13. /// The action associated with the binding.
  14. /// </summary>
  15. /// <remarks>
  16. /// If the binding is not associated with an action, this is <c>null</c>.
  17. /// </remarks>
  18. /// <seealso cref="InputBinding.action"/>
  19. public InputAction action => m_State.GetActionOrNull(ref m_TriggerState);
  20. /// <summary>
  21. /// The bound control that changed its state to trigger the binding associated
  22. /// with the interaction.
  23. /// </summary>
  24. /// <remarks>
  25. /// In case the binding associated with the interaction is a composite, this is
  26. /// one of the controls that are part of the composite.
  27. /// </remarks>
  28. /// <seealso cref="InputBinding.path"/>
  29. public InputControl control => m_State.GetControl(ref m_TriggerState);
  30. /// <summary>
  31. /// The phase the interaction is currently in.
  32. /// </summary>
  33. /// <remarks>
  34. /// Each interaction on a binding has its own phase independent of the action the binding is applied to.
  35. /// If an interaction gets to "drive" an action at a particular point in time, its phase will determine
  36. /// the phase of the action.
  37. /// </remarks>
  38. /// <seealso cref="InputAction.phase"/>
  39. /// <seealso cref="Started"/>
  40. /// <seealso cref="Waiting"/>
  41. /// <seealso cref="Performed"/>
  42. /// <seealso cref="Canceled"/>
  43. public InputActionPhase phase => m_TriggerState.phase;
  44. /// <summary>
  45. /// Time stamp of the input event that caused <see cref="control"/> to trigger a change in the
  46. /// state of <see cref="action"/>.
  47. /// </summary>
  48. /// <seealso cref="InputEvent.time"/>
  49. public double time => m_TriggerState.time;
  50. /// <summary>
  51. /// Timestamp of the <see cref="InputEvent"/> that caused the interaction to transition
  52. /// to <see cref="InputActionPhase.Started"/>.
  53. /// </summary>
  54. /// <seealso cref="InputEvent.time"/>
  55. public double startTime => m_TriggerState.startTime;
  56. /// <summary>
  57. /// Whether the interaction's <see cref="IInputInteraction.Process"/> method has been called because
  58. /// a timer set by <see cref="SetTimeout"/> has expired.
  59. /// </summary>
  60. /// <seealso cref="SetTimeout"/>
  61. public bool timerHasExpired
  62. {
  63. get => (m_Flags & Flags.TimerHasExpired) != 0;
  64. internal set
  65. {
  66. if (value)
  67. m_Flags |= Flags.TimerHasExpired;
  68. else
  69. m_Flags &= ~Flags.TimerHasExpired;
  70. }
  71. }
  72. /// <summary>
  73. /// True if the interaction is waiting for input
  74. /// </summary>
  75. /// <remarks>
  76. /// By default, an interaction will return this this phase after every time it has been performed
  77. /// (<see cref="InputActionPhase.Performed"/>). This can be changed by using <see cref="PerformedAndStayStarted"/>
  78. /// or <see cref="PerformedAndStayPerformed"/>.
  79. /// </remarks>
  80. /// <seealso cref="InputActionPhase.Waiting"/>
  81. public bool isWaiting => phase == InputActionPhase.Waiting;
  82. /// <summary>
  83. /// True if the interaction has been started.
  84. /// </summary>
  85. /// <seealso cref="InputActionPhase.Started"/>
  86. /// <seealso cref="Started"/>
  87. public bool isStarted => phase == InputActionPhase.Started;
  88. /// <summary>
  89. /// Compute the current level of control actuation.
  90. /// </summary>
  91. /// <returns>The current level of control actuation (usually [0..1]) or -1 if the control is actuated
  92. /// but does not support computing magnitudes.</returns>
  93. /// <seealso cref="ControlIsActuated"/>
  94. /// <seealso cref="InputControl.EvaluateMagnitude()"/>
  95. public float ComputeMagnitude()
  96. {
  97. return m_TriggerState.magnitude;
  98. }
  99. /// <summary>
  100. /// Return true if the control that triggered the interaction has been actuated beyond the given threshold.
  101. /// </summary>
  102. /// <param name="threshold">Threshold that must be reached for the control to be considered actuated. If this is zero,
  103. /// the threshold must be exceeded. If it is any positive value, the value must be at least matched.</param>
  104. /// <returns>True if the trigger control is actuated.</returns>
  105. /// <seealso cref="InputControlExtensions.IsActuated"/>
  106. /// <seealso cref="ComputeMagnitude"/>
  107. public bool ControlIsActuated(float threshold = 0)
  108. {
  109. return InputActionState.IsActuated(ref m_TriggerState, threshold);
  110. }
  111. /// <summary>
  112. /// Mark the interaction has having begun.
  113. /// </summary>
  114. /// <remarks>
  115. /// Note that this affects the current interaction only. There may be multiple interactions on a binding
  116. /// and arbitrary many interactions may concurrently be in started state. However, only one interaction
  117. /// (usually the one that starts first) is allowed to drive the action's state as a whole. If an interaction
  118. /// that is currently driving an action is canceled, however, the next interaction in the list that has
  119. /// been started will take over and continue driving the action.
  120. ///
  121. /// <example>
  122. /// <code>
  123. /// public class MyInteraction : IInputInteraction&lt;float&gt;
  124. /// {
  125. /// public void Process(ref IInputInteractionContext context)
  126. /// {
  127. /// if (context.isWaiting &amp;&amp; context.ControlIsActuated())
  128. /// {
  129. /// // We've waited for input and got it. Start the interaction.
  130. /// context.Started();
  131. /// }
  132. /// else if (context.isStarted &amp;&amp; !context.ControlIsActuated())
  133. /// {
  134. /// // Interaction has been completed.
  135. /// context.Performed();
  136. /// }
  137. /// }
  138. ///
  139. /// public void Reset()
  140. /// {
  141. /// // No reset code needed. We're not keeping any state locally in the interaction.
  142. /// }
  143. /// }
  144. /// </code>
  145. /// </example>
  146. /// </remarks>
  147. public void Started()
  148. {
  149. m_TriggerState.startTime = time;
  150. m_State.ChangePhaseOfInteraction(InputActionPhase.Started, ref m_TriggerState);
  151. }
  152. public void Performed()
  153. {
  154. if (m_TriggerState.phase == InputActionPhase.Waiting)
  155. m_TriggerState.startTime = time;
  156. m_State.ChangePhaseOfInteraction(InputActionPhase.Performed, ref m_TriggerState);
  157. }
  158. public void PerformedAndStayStarted()
  159. {
  160. if (m_TriggerState.phase == InputActionPhase.Waiting)
  161. m_TriggerState.startTime = time;
  162. m_State.ChangePhaseOfInteraction(InputActionPhase.Performed, ref m_TriggerState,
  163. phaseAfterPerformed: InputActionPhase.Started);
  164. }
  165. public void PerformedAndStayPerformed()
  166. {
  167. if (m_TriggerState.phase == InputActionPhase.Waiting)
  168. m_TriggerState.startTime = time;
  169. m_State.ChangePhaseOfInteraction(InputActionPhase.Performed, ref m_TriggerState,
  170. phaseAfterPerformed: InputActionPhase.Performed);
  171. }
  172. public void Canceled()
  173. {
  174. if (m_TriggerState.phase != InputActionPhase.Canceled)
  175. m_State.ChangePhaseOfInteraction(InputActionPhase.Canceled, ref m_TriggerState);
  176. }
  177. /// <summary>
  178. /// Put the interaction back into <see cref="InputActionPhase.Waiting"/> state.
  179. /// </summary>
  180. /// <seealso cref="InputAction.phase"/>
  181. /// <seealso cref="InputActionPhase"/>
  182. /// <seealso cref="Started"/>
  183. /// <seealso cref="Performed"/>
  184. /// <seealso cref="Canceled"/>
  185. public void Waiting()
  186. {
  187. if (m_TriggerState.phase != InputActionPhase.Waiting)
  188. m_State.ChangePhaseOfInteraction(InputActionPhase.Waiting, ref m_TriggerState);
  189. }
  190. /// <summary>
  191. /// Start a timeout that triggers within <paramref name="seconds"/>.
  192. /// </summary>
  193. /// <param name="seconds">Number of seconds before the timeout is triggered.</param>
  194. /// <remarks>
  195. /// An interaction might wait a set amount of time for something to happen and then
  196. /// do something depending on whether it did or did not happen. By calling this method,
  197. /// a timeout is installed such that in the input update that the timer expires in, the
  198. /// interaction's <see cref="IInputInteraction.Process"/> method is called with <see cref="timerHasExpired"/>
  199. /// being true.
  200. ///
  201. /// Changing the phase of the interaction while a timeout is running will implicitly cancel
  202. /// the timeout.
  203. ///
  204. /// <example>
  205. /// <code>
  206. /// // Let's say we're writing a Process() method for an interaction that,
  207. /// // after a control has been actuated, waits for 1 second for it to be
  208. /// // released again. If that happens, the interaction performs. If not,
  209. /// // it cancels.
  210. /// public void Process(ref InputInteractionContext context)
  211. /// {
  212. /// // timerHasExpired will be true if we get called when our timeout
  213. /// // has expired.
  214. /// if (context.timerHasExpired)
  215. /// {
  216. /// // The user did not release the control quickly enough.
  217. /// // Our interaction is not successful, so cancel.
  218. /// context.Canceled();
  219. /// return;
  220. /// }
  221. ///
  222. /// if (context.ControlIsActuated())
  223. /// {
  224. /// if (!context.isStarted)
  225. /// {
  226. /// // The control has been actuated. We want to give the user a max
  227. /// // of 1 second to release it. So we start the interaction now and then
  228. /// // set the timeout.
  229. /// context.Started();
  230. /// context.SetTimeout(1);
  231. /// }
  232. /// }
  233. /// else
  234. /// {
  235. /// // Control has been released. If we're currently waiting for a release,
  236. /// // it has come in time before out timeout expired. In other words, the
  237. /// // interaction has been successfully performed. We call Performed()
  238. /// // which implicitly removes our ongoing timeout.
  239. /// if (context.isStarted)
  240. /// context.Performed();
  241. /// }
  242. /// }
  243. /// </code>
  244. /// </example>
  245. /// </remarks>
  246. /// <seealso cref="timerHasExpired"/>
  247. public void SetTimeout(float seconds)
  248. {
  249. m_State.StartTimeout(seconds, ref m_TriggerState);
  250. }
  251. /// <summary>
  252. /// Override the default timeout value used by <see cref="InputAction.GetTimeoutCompletionPercentage"/>.
  253. /// </summary>
  254. /// <param name="seconds">Amount of total successive timeouts TODO</param>
  255. /// <exception cref="ArgumentException"></exception>
  256. /// <remarks>
  257. /// By default, timeout completion will be entirely determine by the timeout that is currently
  258. /// running, if any. However, some interactions (such as <see cref="Interactions.MultiTapInteraction"/>)
  259. /// will have to run multiple timeouts in succession. Thus, completion of a single timeout is not
  260. /// the same as completion of the interaction.
  261. ///
  262. /// You can use this method to account for this.
  263. ///
  264. /// Whenever a timeout completes, the timeout duration will automatically be accumulated towards
  265. /// the total timeout completion time.
  266. ///
  267. /// <example>
  268. /// <code>
  269. /// // Let's say we're starting our first timeout and we know that we will run three timeouts
  270. /// // in succession of 2 seconds each. By calling SetTotalTimeoutCompletionTime(), we can account for this.
  271. /// SetTotalTimeoutCompletionTime(3 * 2);
  272. ///
  273. /// // Start the first timeout. When this timeout expires, it will automatically
  274. /// // count one second towards the total timeout completion time.
  275. /// SetTimeout(2);
  276. /// </code>
  277. /// </example>
  278. /// </remarks>
  279. /// <seealso cref="InputAction.GetTimeoutCompletionPercentage"/>
  280. public void SetTotalTimeoutCompletionTime(float seconds)
  281. {
  282. if (seconds <= 0)
  283. throw new ArgumentException("Seconds must be a positive value", nameof(seconds));
  284. m_State.SetTotalTimeoutCompletionTime(seconds, ref m_TriggerState);
  285. }
  286. /// <summary>
  287. /// Read the value of the binding that triggered processing of the interaction.
  288. /// </summary>
  289. /// <typeparam name="TValue">Type of value to read from the binding. Must match the value type of the control
  290. /// or composite in effect for the binding.</typeparam>
  291. /// <returns>Value read from the binding.</returns>
  292. public TValue ReadValue<TValue>()
  293. where TValue : struct
  294. {
  295. return m_State.ReadValue<TValue>(m_TriggerState.bindingIndex, m_TriggerState.controlIndex);
  296. }
  297. internal InputActionState m_State;
  298. internal Flags m_Flags;
  299. internal InputActionState.TriggerState m_TriggerState;
  300. internal int mapIndex => m_TriggerState.mapIndex;
  301. internal int controlIndex => m_TriggerState.controlIndex;
  302. internal int bindingIndex => m_TriggerState.bindingIndex;
  303. internal int interactionIndex => m_TriggerState.interactionIndex;
  304. [Flags]
  305. internal enum Flags
  306. {
  307. TimerHasExpired = 1 << 1
  308. }
  309. }
  310. }