InputBindingCompositeContext.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.Collections.Generic;
  3. namespace UnityEngine.InputSystem
  4. {
  5. /// <summary>
  6. /// Contextual data made available when processing values of composite bindings.
  7. /// </summary>
  8. /// <remarks>
  9. /// An instance of this struct is passed to <see
  10. /// cref="InputBindingComposite{TValue}.ReadValue(InputBindingComposite)"/>.
  11. /// Use it to access contextual data such as the value for individual part bindings.
  12. ///
  13. /// Note that an instance of this struct should never be held on to past the duration
  14. /// of the call to <c>ReadValue</c>. The data it retrieves is only valid during
  15. /// the callback.
  16. /// </remarks>
  17. /// <seealso cref="InputBindingComposite"/>
  18. /// <seealso cref="InputBindingComposite{TValue}"/>
  19. /// <seealso cref="InputBindingComposite{TValue}.ReadValue(ref InputBindingCompositeContext)"/>
  20. public struct InputBindingCompositeContext
  21. {
  22. /// <summary>
  23. /// Read the value of the giving part binding.
  24. /// </summary>
  25. /// <param name="partNumber">Number of the part to read. This is assigned
  26. /// automatically by the input system and should be treated as an opaque
  27. /// identifier. See the example below.</param>
  28. /// <typeparam name="TValue">Type of value to read. This must match the
  29. /// value type expected from controls bound to the part.</typeparam>
  30. /// <returns>The value read from the part bindings.</returns>
  31. /// <exception cref="InvalidOperationException">The given <typeparamref name="TValue"/>
  32. /// value type does not match the actual value type of the control(s) bound
  33. /// to the part.</exception>
  34. /// <remarks>
  35. /// If no control is bound to the given part, the return value will always
  36. /// be <c>default(TValue)</c>. If a single control is bound to the part, the
  37. /// value will be that of the control. If multiple controls are bound to a
  38. /// part, the return value will be that greatest one according to <c>IComparable</c>
  39. /// implemented by <typeparamref name="TValue"/>.
  40. ///
  41. /// Note that this method only works with values that are <c>IComparable</c>.
  42. /// To read a value type that is not <c>IComparable</c> or to supply a custom
  43. /// comparer, use <see cref="ReadValue{TValue,TComparer}(int,TComparer)"/>.
  44. ///
  45. /// If an invalid <paramref name="partNumber"/> is supplied, the return value
  46. /// will simply be <c>default(TValue)</c>. No exception is thrown.
  47. ///
  48. /// <example>
  49. /// <code>
  50. /// public class MyComposite : InputBindingComposite&lt;float&gt;
  51. /// {
  52. /// // Defines a "part" binding for the composite. Each part can be
  53. /// // bound to arbitrary many times (including not at all). The "layout"
  54. /// // property of the attribute we supply determines what kind of
  55. /// // control is expected to be bound to the part.
  56. /// //
  57. /// // When initializing a composite instance, the input system will
  58. /// // automatically assign part numbers and store them in the fields
  59. /// // we define here.
  60. /// [InputControl(layout = "Button")]
  61. /// public int firstPart;
  62. ///
  63. /// // Defines a second part.
  64. /// [InputControl(layout = "Vector2")]
  65. /// public int secondPart;
  66. ///
  67. /// public override float ReadValue(ref InputBindingCompositeContext context)
  68. /// {
  69. /// // Read the button.
  70. /// var firstValue = context.ReadValue&lt;float&gt;();
  71. ///
  72. /// // Read the vector.
  73. /// var secondValue = context.ReadValue&lt;Vector2&gt;();
  74. ///
  75. /// // Perform some computation based on the inputs. Here, we just
  76. /// // scale the vector by the value we got from the button.
  77. /// return secondValue * firstValue;
  78. /// }
  79. /// }
  80. /// </code>
  81. /// </example>
  82. /// </remarks>
  83. /// <seealso cref="ReadValue{TValue,TComparer}(int,TComparer)"/>
  84. /// <seealso cref="InputControl{TValue}.ReadValue"/>
  85. public unsafe TValue ReadValue<TValue>(int partNumber)
  86. where TValue : struct, IComparable<TValue>
  87. {
  88. if (m_State == null)
  89. return default;
  90. return m_State.ReadCompositePartValue<TValue, DefaultComparer<TValue>>
  91. (m_BindingIndex, partNumber, null, out _);
  92. }
  93. /// <summary>
  94. /// Same as <see cref="ReadValue{TValue}(int)"/> but also return the control
  95. /// from which the value was read.
  96. /// </summary>
  97. /// <param name="partNumber">Number of the part to read. This is assigned
  98. /// automatically by the input system and should be treated as an opaque
  99. /// identifier.</param>
  100. /// <param name="sourceControl">Receives the <see cref="InputControl"/> from
  101. /// which the value was read. If multiple controls are bound to the given part,
  102. /// this is the control whose value was ultimately selected. Will be set to
  103. /// <c>null</c> if <paramref name="partNumber"/> is not a valid part or if no
  104. /// controls are bound to the part.</param>
  105. /// <typeparam name="TValue">Type of value to read. This must match the
  106. /// value type expected from controls bound to the part.</typeparam>
  107. /// <returns>The value read from the part bindings.</returns>
  108. /// <remarks>
  109. /// Like <see cref="ReadValue{TValue}(int)"/>, this method relies on using <c>IComparable</c>
  110. /// implemented by <typeparamref name="TValue"/> to determine the greatest value
  111. /// if multiple controls are bound to the specified part.
  112. /// </remarks>
  113. /// <seealso cref="ReadValue{TValue}(int)"/>
  114. public unsafe TValue ReadValue<TValue>(int partNumber, out InputControl sourceControl)
  115. where TValue : struct, IComparable<TValue>
  116. {
  117. if (m_State == null)
  118. {
  119. sourceControl = null;
  120. return default;
  121. }
  122. var value = m_State.ReadCompositePartValue<TValue, DefaultComparer<TValue>>(m_BindingIndex, partNumber,
  123. null, out var controlIndex);
  124. if (controlIndex != InputActionState.kInvalidIndex)
  125. sourceControl = m_State.controls[controlIndex];
  126. else
  127. sourceControl = null;
  128. return value;
  129. }
  130. /// <summary>
  131. /// Read the value of the given part bindings and use the given <paramref name="comparer"/>
  132. /// to determine which value to return if multiple controls are bound to the part.
  133. /// </summary>
  134. /// <param name="partNumber">Number of the part to read. This is assigned
  135. /// automatically by the input system and should be treated as an opaque
  136. /// identifier.</param>
  137. /// <param name="comparer">Instance of <typeparamref name="TComparer"/> for comparing
  138. /// multiple values.</param>
  139. /// <typeparam name="TValue">Type of value to read. This must match the
  140. /// value type expected from controls bound to the part.</typeparam>
  141. /// <returns>The value read from the part bindings.</returns>
  142. /// <typeparam name="TComparer">Comparer to use if multiple controls are bound to
  143. /// the given part. All values will be compared using <c>TComparer.Compare</c> and
  144. /// the greatest value will be returned.</typeparam>
  145. /// <returns>The value read from the part bindings.</returns>
  146. /// <remarks>
  147. /// This method is a useful alternative to <see cref="ReadValue{TValue}(int)"/> for
  148. /// value types that do not implement <c>IComparable</c> or when the default comparison
  149. /// behavior is undesirable.
  150. ///
  151. /// <example>
  152. /// <code>
  153. /// public class CompositeWithVector2Part : InputBindingComposite&lt;Vector2&gt;
  154. /// {
  155. /// [InputControl(layout = "Vector2")]
  156. /// public int part;
  157. ///
  158. /// public override Vector2 ReadValue(ref InputBindingCompositeContext context)
  159. /// {
  160. /// // Return the Vector3 with the greatest magnitude.
  161. /// return context.ReadValue&lt;Vector2, Vector2MagnitudeComparer&gt;(part);
  162. /// }
  163. /// }
  164. /// </code>
  165. /// </example>
  166. /// </remarks>
  167. /// <seealso cref="Utilities.Vector2MagnitudeComparer"/>
  168. /// <seealso cref="Utilities.Vector3MagnitudeComparer"/>
  169. public unsafe TValue ReadValue<TValue, TComparer>(int partNumber, TComparer comparer = default)
  170. where TValue : struct
  171. where TComparer : IComparer<TValue>
  172. {
  173. if (m_State == null)
  174. return default;
  175. return m_State.ReadCompositePartValue<TValue, TComparer>(
  176. m_BindingIndex, partNumber, null, out _, comparer);
  177. }
  178. /// <summary>
  179. /// Like <see cref="ReadValue{TValue,TComparer}(int,TComparer)"/> but also return
  180. /// the control from which the value has ultimately been read.
  181. /// </summary>
  182. /// <param name="partNumber">Number of the part to read. This is assigned
  183. /// automatically by the input system and should be treated as an opaque
  184. /// identifier.</param>
  185. /// <param name="sourceControl">Receives the <see cref="InputControl"/> from
  186. /// which the value was read. If multiple controls are bound to the given part,
  187. /// this is the control whose value was ultimately selected. Will be set to
  188. /// <c>null</c> if <paramref name="partNumber"/> is not a valid part or if no
  189. /// controls are bound to the part.</param>
  190. /// <param name="comparer">Instance of <typeparamref name="TComparer"/> for comparing
  191. /// multiple values.</param>
  192. /// <typeparam name="TValue">Type of value to read. This must match the
  193. /// value type expected from controls bound to the part.</typeparam>
  194. /// <returns>The value read from the part bindings.</returns>
  195. /// <typeparam name="TComparer">Comparer to use if multiple controls are bound to
  196. /// the given part. All values will be compared using <c>TComparer.Compare</c> and
  197. /// the greatest value will be returned.</typeparam>
  198. /// <returns>The value read from the part bindings.</returns>
  199. public unsafe TValue ReadValue<TValue, TComparer>(int partNumber, out InputControl sourceControl, TComparer comparer = default)
  200. where TValue : struct
  201. where TComparer : IComparer<TValue>
  202. {
  203. if (m_State == null)
  204. {
  205. sourceControl = null;
  206. return default;
  207. }
  208. var value = m_State.ReadCompositePartValue<TValue, TComparer>(m_BindingIndex, partNumber, null,
  209. out var controlIndex, comparer);
  210. if (controlIndex != InputActionState.kInvalidIndex)
  211. sourceControl = m_State.controls[controlIndex];
  212. else
  213. sourceControl = null;
  214. return value;
  215. }
  216. /// <summary>
  217. /// Like <see cref="ReadValue{TValue}(int)"/> but treat bound controls as buttons. This means
  218. /// that custom <see cref="Controls.ButtonControl.pressPoint"/> are respected and that floating-point
  219. /// values from non-ButtonControls will be compared to <see cref="InputSettings.defaultButtonPressPoint"/>.
  220. /// </summary>
  221. /// <param name="partNumber">Number of the part to read. This is assigned
  222. /// automatically by the input system and should be treated as an opaque
  223. /// identifier.</param>
  224. /// <returns>True if any button bound to the part is pressed.</returns>
  225. /// <remarks>
  226. /// This method expects all controls bound to the part to be of type <c>InputControl&lt;float&gt;</c>.
  227. ///
  228. /// This method is different from just calling <see cref="ReadValue{TValue}(int)"/> with a <c>float</c>
  229. /// parameter and comparing the result to <see cref="InputSettings.defaultButtonPressPoint"/> in that
  230. /// custom press points set on individual ButtonControls will be respected.
  231. /// </remarks>
  232. /// <seealso cref="Controls.ButtonControl"/>
  233. /// <seealso cref="InputSettings.defaultButtonPressPoint"/>
  234. public unsafe bool ReadValueAsButton(int partNumber)
  235. {
  236. if (m_State == null)
  237. return default;
  238. var buttonValue = false;
  239. m_State.ReadCompositePartValue<float, DefaultComparer<float>>(m_BindingIndex, partNumber, &buttonValue,
  240. out _);
  241. return buttonValue;
  242. }
  243. internal InputActionState m_State;
  244. internal int m_BindingIndex;
  245. private struct DefaultComparer<TValue> : IComparer<TValue>
  246. where TValue : IComparable<TValue>
  247. {
  248. public int Compare(TValue x, TValue y)
  249. {
  250. return x.CompareTo(y);
  251. }
  252. }
  253. }
  254. }