Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

InputActionTrace.cs 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using Unity.Collections.LowLevel.Unsafe;
  7. using UnityEngine.InputSystem.LowLevel;
  8. ////REVIEW: why not switch to this being the default mechanism? seems like this could allow us to also solve
  9. //// the actions-update-when-not-expected problem; plus give us access to easy polling
  10. ////REVIEW: should this automatically unsubscribe itself on disposal?
  11. ////TODO: make it possible to persist this same way that it should be possible to persist InputEventTrace
  12. ////TODO: make this one thread-safe
  13. ////TODO: add random access capability
  14. ////TODO: protect traces against controls changing configuration (if state layouts change, we're affected)
  15. namespace UnityEngine.InputSystem.Utilities
  16. {
  17. /// <summary>
  18. /// Records the triggering of actions into a sequence of events that can be replayed at will.
  19. /// </summary>
  20. /// <remarks>
  21. /// This is an alternate way to the callback-based responses (such as <see cref="InputAction.performed"/>)
  22. /// of <see cref="InputAction">input actions</see>. Instead of executing response code right away whenever
  23. /// an action triggers, an <see cref="RecordAction">event is recorded</see> which can then be queried on demand.
  24. ///
  25. /// The recorded data will stay valid even if the bindings on the actions are changed (e.g. by enabling a different
  26. /// set of bindings through altering <see cref="InputAction.bindingMask"/> or <see cref="InputActionMap.devices"/> or
  27. /// when modifying the paths of bindings altogether). Note, however, that when this happens, a trace will have
  28. /// to make a private copy of the data that stores the binding resolution state. This means that there can be
  29. /// GC allocation spike when reconfiguring actions that have recorded data in traces.
  30. ///
  31. /// <example>
  32. /// <code>
  33. /// var trace = new InputActionTrace();
  34. ///
  35. /// // Subscribe trace to single action.
  36. /// // (Use UnsubscribeFrom to unsubscribe)
  37. /// trace.SubscribeTo(myAction);
  38. ///
  39. /// // Subscribe trace to entire action map.
  40. /// // (Use UnsubscribeFrom to unsubscribe)
  41. /// trace.SubscribeTo(myActionMap);
  42. ///
  43. /// // Subscribe trace to all actions in the system.
  44. /// trace.SubscribeToAll();
  45. ///
  46. /// // Record a single triggering of an action.
  47. /// myAction.performed +=
  48. /// ctx =>
  49. /// {
  50. /// if (ctx.ReadValue&lt;float&gt;() &gt; 0.5f)
  51. /// trace.RecordAction(ctx);
  52. /// };
  53. ///
  54. /// // Output trace to console.
  55. /// Debug.Log(string.Join(",\n", trace));
  56. ///
  57. /// // Walk through all recorded actions and then clear trace.
  58. /// foreach (var record in trace)
  59. /// {
  60. /// Debug.Log($"{record.action} was {record.phase} by control {record.control} at {record.time}");
  61. ///
  62. /// // To read out the value, you either have to know the value type or read the
  63. /// // value out as a generic byte buffer. Here we assume that the value type is
  64. /// // float.
  65. ///
  66. /// Debug.Log("Value: " + record.ReadValue&lt;float&gt;());
  67. ///
  68. /// // An alternative is read the value as an object. In this case, you don't have
  69. /// // to know the value type but there will be a boxed object allocation.
  70. /// Debug.Log("Value: " + record.ReadValueAsObject());
  71. /// }
  72. /// trace.Clear();
  73. ///
  74. /// // Unsubscribe trace from everything.
  75. /// trace.UnsubscribeFromAll();
  76. ///
  77. /// // Release memory held by trace.
  78. /// trace.Dispose();
  79. /// </code>
  80. /// </example>
  81. /// </remarks>
  82. /// <seealso cref="InputAction.started"/>
  83. /// <seealso cref="InputAction.performed"/>
  84. /// <seealso cref="InputAction.canceled"/>
  85. /// <seealso cref="InputSystem.onActionChange"/>
  86. public sealed class InputActionTrace : IEnumerable<InputActionTrace.ActionEventPtr>, IDisposable
  87. {
  88. ////REVIEW: this is of limited use without having access to ActionEvent
  89. /// <summary>
  90. /// Directly access the underlying raw memory queue.
  91. /// </summary>
  92. public InputEventBuffer buffer => m_EventBuffer;
  93. /// <summary>
  94. /// Returns the number of events in the associated event buffer.
  95. /// </summary>
  96. public int count => m_EventBuffer.eventCount;
  97. /// <summary>
  98. /// Constructs a new default initialized <c>InputActionTrace</c>.
  99. /// </summary>
  100. /// <remarks>
  101. /// When you use this constructor, the new InputActionTrace object does not start recording any actions.
  102. /// To record actions, you must explicitly set them up after creating the object.
  103. /// Alternatively, you can use one of the other constructor overloads which begin recording actions immediately.
  104. /// </remarks>
  105. /// <seealso cref="SubscribeTo(InputAction)"/>
  106. /// <seealso cref="SubscribeTo(InputActionMap)"/>
  107. /// <seealso cref="SubscribeToAll"/>
  108. public InputActionTrace()
  109. {
  110. }
  111. /// <summary>
  112. /// Constructs a new <c>InputActionTrace</c> that records <paramref name="action"/>.
  113. /// </summary>
  114. /// <param name="action">The action to be recorded.</param>
  115. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="action"/> is <c>null</c>.</exception>
  116. public InputActionTrace(InputAction action)
  117. {
  118. if (action == null)
  119. throw new ArgumentNullException(nameof(action));
  120. SubscribeTo(action);
  121. }
  122. /// <summary>
  123. /// Constructs a new <c>InputActionTrace</c> that records all actions in <paramref name="actionMap"/>.
  124. /// </summary>
  125. /// <param name="actionMap">The action-map containing actions to be recorded.</param>
  126. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="action"/> is <c>null</c>.</exception>
  127. public InputActionTrace(InputActionMap actionMap)
  128. {
  129. if (actionMap == null)
  130. throw new ArgumentNullException(nameof(actionMap));
  131. SubscribeTo(actionMap);
  132. }
  133. /// <summary>
  134. /// Record any action getting triggered anywhere.
  135. /// </summary>
  136. /// <remarks>
  137. /// This does not require the trace to actually hook into every single action or action map in the system.
  138. /// Instead, the trace will listen to <see cref="InputSystem.onActionChange"/> and automatically record
  139. /// every triggered action.
  140. /// </remarks>
  141. /// <seealso cref="SubscribeTo(InputAction)"/>
  142. /// <seealso cref="SubscribeTo(InputActionMap)"/>
  143. public void SubscribeToAll()
  144. {
  145. if (m_SubscribedToAll)
  146. return;
  147. HookOnActionChange();
  148. m_SubscribedToAll = true;
  149. // Remove manually created subscriptions.
  150. while (m_SubscribedActions.length > 0)
  151. UnsubscribeFrom(m_SubscribedActions[m_SubscribedActions.length - 1]);
  152. while (m_SubscribedActionMaps.length > 0)
  153. UnsubscribeFrom(m_SubscribedActionMaps[m_SubscribedActionMaps.length - 1]);
  154. }
  155. /// <summary>
  156. /// Unsubscribes from all actions currently being recorded.
  157. /// </summary>
  158. /// <seealso cref="UnsubscribeFrom(InputAction)"/>
  159. /// <seealso cref="UnsubscribeFrom(InputActionMap)"/>
  160. public void UnsubscribeFromAll()
  161. {
  162. // Only unhook from OnActionChange if we don't have any recorded actions. If we do have
  163. // any, we still need the callback to be notified about when binding data changes.
  164. if (count == 0)
  165. UnhookOnActionChange();
  166. m_SubscribedToAll = false;
  167. while (m_SubscribedActions.length > 0)
  168. UnsubscribeFrom(m_SubscribedActions[m_SubscribedActions.length - 1]);
  169. while (m_SubscribedActionMaps.length > 0)
  170. UnsubscribeFrom(m_SubscribedActionMaps[m_SubscribedActionMaps.length - 1]);
  171. }
  172. /// <summary>
  173. /// Subscribes to <paramref name="action"/>.
  174. /// </summary>
  175. /// <param name="action">The action to be recorded.</param>
  176. /// <remarks>
  177. /// **Note:** This method does not prevent you from subscribing to the same action multiple times.
  178. /// If you subscribe to the same action multiple times, your event buffer will contain duplicate entries.
  179. /// </remarks>
  180. /// <exception cref="ArgumentNullException">If <paramref name="action"/> is <c>null</c>.</exception>
  181. /// <seealso cref="SubscribeTo(InputActionMap)"/>
  182. /// <seealso cref="SubscribeToAll"/>
  183. public void SubscribeTo(InputAction action)
  184. {
  185. if (action == null)
  186. throw new ArgumentNullException(nameof(action));
  187. if (m_CallbackDelegate == null)
  188. m_CallbackDelegate = RecordAction;
  189. action.performed += m_CallbackDelegate;
  190. action.started += m_CallbackDelegate;
  191. action.canceled += m_CallbackDelegate;
  192. m_SubscribedActions.AppendWithCapacity(action);
  193. }
  194. /// <summary>
  195. /// Subscribes to all actions contained within <paramref name="actionMap"/>.
  196. /// </summary>
  197. /// <param name="actionMap">The action-map containing all actions to be recorded.</param>
  198. /// <remarks>
  199. /// **Note:** This method does not prevent you from subscribing to the same action multiple times.
  200. /// If you subscribe to the same action multiple times, your event buffer will contain duplicate entries.
  201. /// </remarks>
  202. /// <exception cref="ArgumentNullException">Thrown if <paramref name="actionMap"/> is null.</exception>
  203. /// <seealso cref="SubscribeTo(InputAction)"/>
  204. /// <seealso cref="SubscribeToAll"/>
  205. public void SubscribeTo(InputActionMap actionMap)
  206. {
  207. if (actionMap == null)
  208. throw new ArgumentNullException(nameof(actionMap));
  209. if (m_CallbackDelegate == null)
  210. m_CallbackDelegate = RecordAction;
  211. actionMap.actionTriggered += m_CallbackDelegate;
  212. m_SubscribedActionMaps.AppendWithCapacity(actionMap);
  213. }
  214. /// <summary>
  215. /// Unsubscribes from an action, if that action was previously subscribed to.
  216. /// </summary>
  217. /// <param name="action">The action to unsubscribe from.</param>
  218. /// <remarks>
  219. /// **Note:** This method has no side effects if you attempt to unsubscribe from an action that you have not previously subscribed to.
  220. /// </remarks>
  221. /// <exception cref="ArgumentNullException">Thrown if <paramref name="action"/> is <c>null</c>.</exception>
  222. /// <seealso cref="UnsubscribeFrom(InputActionMap)"/>
  223. /// <seealso cref="UnsubscribeFromAll"/>
  224. public void UnsubscribeFrom(InputAction action)
  225. {
  226. if (action == null)
  227. throw new ArgumentNullException(nameof(action));
  228. if (m_CallbackDelegate == null)
  229. return;
  230. action.performed -= m_CallbackDelegate;
  231. action.started -= m_CallbackDelegate;
  232. action.canceled -= m_CallbackDelegate;
  233. var index = m_SubscribedActions.IndexOfReference(action);
  234. if (index != -1)
  235. m_SubscribedActions.RemoveAtWithCapacity(index);
  236. }
  237. /// <summary>
  238. /// Unsubscribes from all actions included in <paramref name="actionMap"/>.
  239. /// </summary>
  240. /// <param name="actionMap">The action-map containing actions to unsubscribe from.</param>
  241. /// <remarks>
  242. /// **Note:** This method has no side effects if you attempt to unsubscribe from an action-map that you have not previously subscribed to.
  243. /// </remarks>
  244. /// <exception cref="ArgumentNullException">Thrown if <paramref name="actionMap"/> is <c>null</c>.</exception>
  245. /// <seealso cref="UnsubscribeFrom(InputAction)"/>
  246. /// <seealso cref="UnsubscribeFromAll"/>
  247. public void UnsubscribeFrom(InputActionMap actionMap)
  248. {
  249. if (actionMap == null)
  250. throw new ArgumentNullException(nameof(actionMap));
  251. if (m_CallbackDelegate == null)
  252. return;
  253. actionMap.actionTriggered -= m_CallbackDelegate;
  254. var index = m_SubscribedActionMaps.IndexOfReference(actionMap);
  255. if (index != -1)
  256. m_SubscribedActionMaps.RemoveAtWithCapacity(index);
  257. }
  258. /// <summary>
  259. /// Record the triggering of an action as an <see cref="ActionEventPtr">action event</see>.
  260. /// </summary>
  261. /// <param name="context"></param>
  262. /// <see cref="InputAction.performed"/>
  263. /// <see cref="InputAction.started"/>
  264. /// <see cref="InputAction.canceled"/>
  265. /// <see cref="InputActionMap.actionTriggered"/>
  266. public unsafe void RecordAction(InputAction.CallbackContext context)
  267. {
  268. // Find/add state.
  269. var stateIndex = m_ActionMapStates.IndexOfReference(context.m_State);
  270. if (stateIndex == -1)
  271. stateIndex = m_ActionMapStates.AppendWithCapacity(context.m_State);
  272. // Make sure we get notified if there's a change to binding setups.
  273. HookOnActionChange();
  274. // Allocate event.
  275. var valueSizeInBytes = context.valueSizeInBytes;
  276. var eventPtr =
  277. (ActionEvent*)m_EventBuffer.AllocateEvent(ActionEvent.GetEventSizeWithValueSize(valueSizeInBytes));
  278. // Initialize event.
  279. ref var triggerState = ref context.m_State.actionStates[context.m_ActionIndex];
  280. eventPtr->baseEvent.type = ActionEvent.Type;
  281. eventPtr->baseEvent.time = triggerState.time;
  282. eventPtr->stateIndex = stateIndex;
  283. eventPtr->controlIndex = triggerState.controlIndex;
  284. eventPtr->bindingIndex = triggerState.bindingIndex;
  285. eventPtr->interactionIndex = triggerState.interactionIndex;
  286. eventPtr->startTime = triggerState.startTime;
  287. eventPtr->phase = triggerState.phase;
  288. // Store value.
  289. // NOTE: If the action triggered from a composite, this stores the value as
  290. // read from the composite.
  291. // NOTE: Also, the value we store is a fully processed value.
  292. var valueBuffer = eventPtr->valueData;
  293. context.ReadValue(valueBuffer, valueSizeInBytes);
  294. }
  295. /// <summary>
  296. /// Clears all recorded data.
  297. /// </summary>
  298. /// <remarks>
  299. /// **Note:** This method does not unsubscribe any actions that the instance is listening to, so after clearing the recorded data, new input on those subscribed actions will continue to be recorded.
  300. /// </remarks>
  301. public void Clear()
  302. {
  303. m_EventBuffer.Reset();
  304. m_ActionMapStates.ClearWithCapacity();
  305. }
  306. ~InputActionTrace()
  307. {
  308. DisposeInternal();
  309. }
  310. /// <inheritdoc/>
  311. public override string ToString()
  312. {
  313. if (count == 0)
  314. return "[]";
  315. var str = new StringBuilder();
  316. str.Append('[');
  317. var isFirst = true;
  318. foreach (var eventPtr in this)
  319. {
  320. if (!isFirst)
  321. str.Append(",\n");
  322. str.Append(eventPtr.ToString());
  323. isFirst = false;
  324. }
  325. str.Append(']');
  326. return str.ToString();
  327. }
  328. /// <inheritdoc/>
  329. public void Dispose()
  330. {
  331. UnsubscribeFromAll();
  332. DisposeInternal();
  333. }
  334. private void DisposeInternal()
  335. {
  336. // Nuke clones we made of InputActionMapStates.
  337. for (var i = 0; i < m_ActionMapStateClones.length; ++i)
  338. m_ActionMapStateClones[i].Dispose();
  339. m_EventBuffer.Dispose();
  340. m_ActionMapStates.Clear();
  341. m_ActionMapStateClones.Clear();
  342. if (m_ActionChangeDelegate != null)
  343. {
  344. InputSystem.onActionChange -= m_ActionChangeDelegate;
  345. m_ActionChangeDelegate = null;
  346. }
  347. }
  348. /// <summary>
  349. /// Returns an enumerator that enumerates all action events recorded for this instance.
  350. /// </summary>
  351. /// <returns>Enumerator instance, never <c>null</c>.</returns>
  352. /// <seealso cref="ActionEventPtr"/>
  353. public IEnumerator<ActionEventPtr> GetEnumerator()
  354. {
  355. return new Enumerator(this);
  356. }
  357. IEnumerator IEnumerable.GetEnumerator()
  358. {
  359. return GetEnumerator();
  360. }
  361. private bool m_SubscribedToAll;
  362. private bool m_OnActionChangeHooked;
  363. private InlinedArray<InputAction> m_SubscribedActions;
  364. private InlinedArray<InputActionMap> m_SubscribedActionMaps;
  365. private InputEventBuffer m_EventBuffer;
  366. private InlinedArray<InputActionState> m_ActionMapStates;
  367. private InlinedArray<InputActionState> m_ActionMapStateClones;
  368. private Action<InputAction.CallbackContext> m_CallbackDelegate;
  369. private Action<object, InputActionChange> m_ActionChangeDelegate;
  370. private void HookOnActionChange()
  371. {
  372. if (m_OnActionChangeHooked)
  373. return;
  374. if (m_ActionChangeDelegate == null)
  375. m_ActionChangeDelegate = OnActionChange;
  376. InputSystem.onActionChange += m_ActionChangeDelegate;
  377. m_OnActionChangeHooked = true;
  378. }
  379. private void UnhookOnActionChange()
  380. {
  381. if (!m_OnActionChangeHooked)
  382. return;
  383. InputSystem.onActionChange -= m_ActionChangeDelegate;
  384. m_OnActionChangeHooked = false;
  385. }
  386. private void OnActionChange(object actionOrMapOrAsset, InputActionChange change)
  387. {
  388. // If we're subscribed to all actions, check if an action got triggered.
  389. if (m_SubscribedToAll)
  390. {
  391. switch (change)
  392. {
  393. case InputActionChange.ActionStarted:
  394. case InputActionChange.ActionPerformed:
  395. case InputActionChange.ActionCanceled:
  396. Debug.Assert(actionOrMapOrAsset is InputAction, "Expected an action");
  397. var triggeredAction = (InputAction)actionOrMapOrAsset;
  398. var actionIndex = triggeredAction.m_ActionIndexInState;
  399. var stateForAction = triggeredAction.m_ActionMap.m_State;
  400. var context = new InputAction.CallbackContext
  401. {
  402. m_State = stateForAction,
  403. m_ActionIndex = actionIndex,
  404. };
  405. RecordAction(context);
  406. return;
  407. }
  408. }
  409. // We're only interested in changes to the binding resolution state of actions.
  410. if (change != InputActionChange.BoundControlsAboutToChange)
  411. return;
  412. // Grab the associated action map(s).
  413. if (actionOrMapOrAsset is InputAction action)
  414. CloneActionStateBeforeBindingsChange(action.m_ActionMap);
  415. else if (actionOrMapOrAsset is InputActionMap actionMap)
  416. CloneActionStateBeforeBindingsChange(actionMap);
  417. else if (actionOrMapOrAsset is InputActionAsset actionAsset)
  418. foreach (var actionMapInAsset in actionAsset.actionMaps)
  419. CloneActionStateBeforeBindingsChange(actionMapInAsset);
  420. else
  421. Debug.Assert(false, "Expected InputAction, InputActionMap or InputActionAsset");
  422. }
  423. private void CloneActionStateBeforeBindingsChange(InputActionMap actionMap)
  424. {
  425. // Grab the state.
  426. var state = actionMap.m_State;
  427. if (state == null)
  428. {
  429. // Bindings have not been resolved yet for this action map. We shouldn't even be
  430. // on the notification list in this case, but just in case, ignore.
  431. return;
  432. }
  433. // See if we're using the given state.
  434. var stateIndex = m_ActionMapStates.IndexOfReference(state);
  435. if (stateIndex == -1)
  436. return;
  437. // Yes, we are so make our own private copy of its current state.
  438. // NOTE: We do not put these local InputActionMapStates on the global list.
  439. var clone = state.Clone();
  440. m_ActionMapStateClones.Append(clone);
  441. m_ActionMapStates[stateIndex] = clone;
  442. }
  443. /// <summary>
  444. /// A wrapper around <see cref="ActionEvent"/> that automatically translates all the
  445. /// information in events into their high-level representations.
  446. /// </summary>
  447. /// <remarks>
  448. /// For example, instead of returning <see cref="ActionEvent.controlIndex">control indices</see>,
  449. /// it automatically resolves and returns the respective <see cref="InputControl">controls</see>.
  450. /// </remarks>
  451. public unsafe struct ActionEventPtr
  452. {
  453. internal InputActionState m_State;
  454. internal ActionEvent* m_Ptr;
  455. /// <summary>
  456. /// The <see cref="InputAction"/> associated with this action event.
  457. /// </summary>
  458. public InputAction action => m_State.GetActionOrNull(m_Ptr->bindingIndex);
  459. /// <summary>
  460. /// The <see cref="InputActionPhase"/> associated with this action event.
  461. /// </summary>
  462. /// <seealso cref="InputAction.phase"/>
  463. /// <seealso cref="InputAction.CallbackContext.phase"/>
  464. public InputActionPhase phase => m_Ptr->phase;
  465. /// <summary>
  466. /// The <see cref="InputControl"/> instance associated with this action event.
  467. /// </summary>
  468. public InputControl control => m_State.controls[m_Ptr->controlIndex];
  469. /// <summary>
  470. /// The <see cref="IInputInteraction"/> instance associated with this action event if applicable, or <c>null</c> if the action event is not associated with an input interaction.
  471. /// </summary>
  472. public IInputInteraction interaction
  473. {
  474. get
  475. {
  476. var index = m_Ptr->interactionIndex;
  477. if (index == InputActionState.kInvalidIndex)
  478. return null;
  479. return m_State.interactions[index];
  480. }
  481. }
  482. /// <summary>
  483. /// The time, in seconds since your game or app started, that the event occurred.
  484. /// </summary>
  485. /// <remarks>
  486. /// Times are in seconds and progress linearly in real-time. The timeline is the same as for <see cref="Time.realtimeSinceStartup"/>.
  487. /// </remarks>
  488. public double time => m_Ptr->baseEvent.time;
  489. /// <summary>
  490. /// The time, in seconds since your game or app started, that the <see cref="phase"/> transitioned into <see cref="InputActionPhase.Started"/>.
  491. /// </summary>
  492. public double startTime => m_Ptr->startTime;
  493. /// <summary>
  494. /// The duration, in seconds, that has elapsed between when this event was generated and when the
  495. /// action <see cref="phase"/> transitioned to <see cref="InputActionPhase.Started"/> and has remained active.
  496. /// </summary>
  497. public double duration => time - startTime;
  498. /// <summary>
  499. /// The size, in bytes, of the value associated with this action event.
  500. /// </summary>
  501. public int valueSizeInBytes => m_Ptr->valueSizeInBytes;
  502. /// <summary>
  503. /// Reads the value associated with this event as an <c>object</c>.
  504. /// </summary>
  505. /// <returns><c>object</c> representing the value of this action event.</returns>
  506. /// <seealso cref="ReadOnlyArray{TValue}"/>
  507. /// <seealso cref="ReadValue(void*, int)"/>
  508. public object ReadValueAsObject()
  509. {
  510. if (m_Ptr == null)
  511. throw new InvalidOperationException("ActionEventPtr is invalid");
  512. var valuePtr = m_Ptr->valueData;
  513. // Check if the value came from a composite.
  514. var bindingIndex = m_Ptr->bindingIndex;
  515. if (m_State.bindingStates[bindingIndex].isPartOfComposite)
  516. {
  517. // Yes, so have to put the value/struct data we read into a boxed
  518. // object based on the value type of the composite.
  519. var compositeBindingIndex = m_State.bindingStates[bindingIndex].compositeOrCompositeBindingIndex;
  520. var compositeIndex = m_State.bindingStates[compositeBindingIndex].compositeOrCompositeBindingIndex;
  521. var composite = m_State.composites[compositeIndex];
  522. Debug.Assert(composite != null, "NULL composite instance");
  523. var valueType = composite.valueType;
  524. if (valueType == null)
  525. throw new InvalidOperationException($"Cannot read value from Composite '{composite}' which does not have a valueType set");
  526. return Marshal.PtrToStructure(new IntPtr(valuePtr), valueType);
  527. }
  528. // Expecting action to only trigger from part bindings or bindings outside of composites.
  529. Debug.Assert(!m_State.bindingStates[bindingIndex].isComposite, "Action should not have triggered directly from a composite binding");
  530. // Read value through InputControl.
  531. var valueSizeInBytes = m_Ptr->valueSizeInBytes;
  532. return control.ReadValueFromBufferAsObject(valuePtr, valueSizeInBytes);
  533. }
  534. /// <summary>
  535. /// Reads the value associated with this event into the contiguous memory buffer defined by <c>[buffer, buffer + bufferSize)</c>.
  536. /// </summary>
  537. /// <param name="buffer">Pointer to the contiguous memory buffer to write value data to.</param>
  538. /// <param name="bufferSize">The size, in bytes, of the contiguous buffer pointed to by <paramref name="buffer"/>.</param>
  539. /// <exception cref="NullReferenceException">If <paramref name="buffer"/> is <c>null</c>.</exception>
  540. /// <exception cref="ArgumentException">If the given <paramref name="bufferSize"/> is less than the number of bytes required to write the event value to <paramref name="buffer"/>.</exception>
  541. /// <seealso cref="ReadValueAsObject"/>
  542. /// <seealso cref="ReadValue{TValue}"/>
  543. public void ReadValue(void* buffer, int bufferSize)
  544. {
  545. var valueSizeInBytes = m_Ptr->valueSizeInBytes;
  546. ////REVIEW: do we want more checking than this?
  547. if (bufferSize < valueSizeInBytes)
  548. throw new ArgumentException(
  549. $"Expected buffer of at least {valueSizeInBytes} bytes but got buffer of just {bufferSize} bytes instead",
  550. nameof(bufferSize));
  551. UnsafeUtility.MemCpy(buffer, m_Ptr->valueData, valueSizeInBytes);
  552. }
  553. /// <summary>
  554. /// Reads the value associated with this event as an object of type <typeparamref name="TValue"/>.
  555. /// </summary>
  556. /// <typeparam name="TValue">The event value type to be used.</typeparam>
  557. /// <returns>Object of type <typeparamref name="TValue"/>.</returns>
  558. /// <exception cref="InvalidOperationException">In case the size of <typeparamref name="TValue"/> does not match the size of the value associated with this event.</exception>
  559. public TValue ReadValue<TValue>()
  560. where TValue : struct
  561. {
  562. var valueSizeInBytes = m_Ptr->valueSizeInBytes;
  563. ////REVIEW: do we want more checking than this?
  564. if (UnsafeUtility.SizeOf<TValue>() != valueSizeInBytes)
  565. throw new InvalidOperationException(
  566. $"Cannot read a value of type '{typeof(TValue).Name}' with size {UnsafeUtility.SizeOf<TValue>()} from event on action '{action}' with value size {valueSizeInBytes}");
  567. var result = new TValue();
  568. var resultPtr = UnsafeUtility.AddressOf(ref result);
  569. UnsafeUtility.MemCpy(resultPtr, m_Ptr->valueData, valueSizeInBytes);
  570. return result;
  571. }
  572. /// <inheritdoc/>
  573. public override string ToString()
  574. {
  575. if (m_Ptr == null)
  576. return "<null>";
  577. var actionName = action.actionMap != null ? $"{action.actionMap.name}/{action.name}" : action.name;
  578. return $"{{ action={actionName} phase={phase} time={time} control={control} value={ReadValueAsObject()} interaction={interaction} duration={duration} }}";
  579. }
  580. }
  581. private unsafe struct Enumerator : IEnumerator<ActionEventPtr>
  582. {
  583. private readonly InputActionTrace m_Trace;
  584. private readonly ActionEvent* m_Buffer;
  585. private readonly int m_EventCount;
  586. private ActionEvent* m_CurrentEvent;
  587. private int m_CurrentIndex;
  588. public Enumerator(InputActionTrace trace)
  589. {
  590. m_Trace = trace;
  591. m_Buffer = (ActionEvent*)trace.m_EventBuffer.bufferPtr.data;
  592. m_EventCount = trace.m_EventBuffer.eventCount;
  593. m_CurrentEvent = null;
  594. m_CurrentIndex = 0;
  595. }
  596. public bool MoveNext()
  597. {
  598. if (m_CurrentIndex == m_EventCount)
  599. return false;
  600. if (m_CurrentEvent == null)
  601. {
  602. m_CurrentEvent = m_Buffer;
  603. return m_CurrentEvent != null;
  604. }
  605. Debug.Assert(m_CurrentEvent != null);
  606. ++m_CurrentIndex;
  607. if (m_CurrentIndex == m_EventCount)
  608. return false;
  609. m_CurrentEvent = (ActionEvent*)InputEvent.GetNextInMemory((InputEvent*)m_CurrentEvent);
  610. return true;
  611. }
  612. public void Reset()
  613. {
  614. m_CurrentEvent = null;
  615. m_CurrentIndex = 0;
  616. }
  617. public void Dispose()
  618. {
  619. }
  620. public ActionEventPtr Current
  621. {
  622. get
  623. {
  624. var state = m_Trace.m_ActionMapStates[m_CurrentEvent->stateIndex];
  625. return new ActionEventPtr
  626. {
  627. m_State = state,
  628. m_Ptr = m_CurrentEvent,
  629. };
  630. }
  631. }
  632. object IEnumerator.Current => Current;
  633. }
  634. }
  635. }