TouchControl.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using UnityEngine.InputSystem.Utilities;
  2. using Unity.Collections.LowLevel.Unsafe;
  3. using UnityEngine.InputSystem.Layouts;
  4. using UnityEngine.InputSystem.LowLevel;
  5. using UnityEngine.Scripting;
  6. ////TODO: enforce memory layout of TouchControl to be that of TouchState (build that into the layout system? "freeze"/final layout?)
  7. namespace UnityEngine.InputSystem.Controls
  8. {
  9. /// <summary>
  10. /// A control representing a touch contact.
  11. /// </summary>
  12. /// <remarks>
  13. /// Note that unlike most other control types, <c>TouchControls</c> do not have
  14. /// a flexible memory layout. They are hardwired to <see cref="TouchState"/> and
  15. /// will not work correctly with a different memory layouts. Additional fields may
  16. /// be appended to the struct but what's there in the struct has to be located
  17. /// at exactly those memory addresses.
  18. /// </remarks>
  19. [InputControlLayout(stateType = typeof(TouchState))]
  20. [Preserve]
  21. public class TouchControl : InputControl<TouchState>
  22. {
  23. /// <summary>
  24. /// Button that indicates whether there is currently an ongoing touch
  25. /// contact on the control. When touch is ongoing, button will be 1,
  26. /// otherwise button will be 0.
  27. /// </summary>
  28. /// <value>Control representing an ongoing touch contact.</value>
  29. /// <remarks>
  30. /// This control simply monitors <see cref="phase"/> and will read as 1 whenever
  31. /// the phase is <see cref="TouchPhase.Began"/>, <see cref="TouchPhase.Moved"/>,
  32. /// or <see cref="TouchPhase.Stationary"/>.
  33. /// </remarks>
  34. /// <seealso cref="phase"/>
  35. public TouchPressControl press { get; private set; }
  36. /// <summary>
  37. /// The ID of the touch contact as reported by the underlying system.
  38. /// </summary>
  39. /// <value>Control reading out the ID of the touch.</value>
  40. /// <remarks>
  41. /// Each touch contact that is made with the screen receives its own unique ID which is
  42. /// normally assigned by the underlying platform.
  43. ///
  44. /// Note a platform may reuse touch IDs after their respective touches have finished.
  45. /// This means that the guarantee of uniqueness is only made with respect to currently
  46. /// ongoing touches.
  47. /// </remarks>
  48. /// <seealso cref="TouchState.touchId"/>
  49. public IntegerControl touchId { get; private set; }
  50. /// <summary>
  51. /// Absolute screen-space position on the touch surface.
  52. /// </summary>
  53. /// <value>Control representing the screen-space of the touch.</value>
  54. /// <seealso cref="TouchState.position"/>
  55. public Vector2Control position { get; private set; }
  56. /// <summary>
  57. /// Screen-space motion delta of the touch.
  58. /// </summary>
  59. /// <value>Control representing the screen-space motion delta of the touch.</value>
  60. /// <remarks>
  61. /// This is either supplied directly by the underlying platform or computed on the
  62. /// fly by <see cref="Touchscreen"/> from the last known position of the touch.
  63. ///
  64. /// Note that deltas have behaviors attached to them different from most other
  65. /// controls. See <see cref="Pointer.delta"/> for details.
  66. /// </remarks>
  67. /// <seealso cref="TouchState.delta"/>
  68. public Vector2Control delta { get; private set; }
  69. /// <summary>
  70. /// Normalized pressure of the touch against the touch surface.
  71. /// </summary>
  72. /// <value>Control representing the pressure level of the touch.</value>
  73. /// <remarks>
  74. /// Not all touchscreens are pressure-sensitive. If unsupported, this control will remain
  75. /// at default value.
  76. ///
  77. /// In general, touch pressure is supported on mobile platforms only.
  78. ///
  79. /// Note that it is possible for the value to go above 1 even though it is considered normalized. The reason is
  80. /// that calibration on the system can put the maximum pressure point below the physically supported maximum value.
  81. /// </remarks>
  82. /// <seealso cref="TouchState.pressure"/>
  83. /// <seealso cref="Pointer.pressure"/>
  84. public AxisControl pressure { get; private set; }
  85. /// <summary>
  86. /// Screen-space radius of the touch.
  87. /// </summary>
  88. /// <value>Control representing the horizontal and vertical extents of the touch contact.</value>
  89. /// <remarks>
  90. /// If supported by the device, this reports the size of the touch contact based on its
  91. /// <see cref="position"/> center point. If not supported, this will be <c>default(Vector2)</c>.
  92. /// </remarks>
  93. /// <seealso cref="Pointer.radius"/>
  94. public Vector2Control radius { get; private set; }
  95. /// <summary>
  96. /// Current phase of the touch.
  97. /// </summary>
  98. /// <value>Control representing the current phase of the touch.</value>
  99. /// <remarks>
  100. /// This will be <see cref="TouchPhase.None"/> if no touch has been registered on the control
  101. /// yet or if the control has been reset to its default state.
  102. /// </remarks>
  103. /// <seealso cref="isInProgress"/>
  104. public TouchPhaseControl phase { get; private set; }
  105. /// <summary>
  106. /// Whether the touch comes from a source other than direct contact with the touch surface.
  107. /// </summary>
  108. /// <value>Control indicating whether the touch was generated indirectly.</value>
  109. /// <remarks>
  110. /// Indirect touches can be generated with a stylus, for example.
  111. /// </remarks>
  112. public ButtonControl indirectTouch { get; private set; }
  113. /// <summary>
  114. /// Whether the touch has performed a tap.
  115. /// </summary>
  116. /// <value>Control that indicates whether the touch has tapped the screen.</value>
  117. /// <remarks>
  118. /// A tap is defined as a touch that begins and ends within <see cref="InputSettings.defaultTapTime"/> and
  119. /// stays within <see cref="InputSettings.tapRadius"/> of its <see cref="startPosition"/>. If this
  120. /// is the case for a touch, this button is set to 1 at the time the touch goes to <see cref="phase"/>
  121. /// <see cref="TouchPhase.Ended"/>.
  122. ///
  123. /// The button resets to 0 only when another touch is started on the control or when the control
  124. /// is reset.
  125. /// </remarks>
  126. /// <seealso cref="tapCount"/>
  127. /// <seealso cref="InputSettings.defaultTapTime"/>
  128. public ButtonControl tap { get; private set; }
  129. /// <summary>
  130. /// Number of times that the touch has been tapped in succession.
  131. /// </summary>
  132. /// <value>Control that indicates how many taps have been performed one after the other.</value>
  133. /// <remarks>
  134. /// Successive taps have to come within <see cref="InputSettings.multiTapDelayTime"/> for them
  135. /// to increase the tap count. I.e. if a new tap finishes within that time after <see cref="startTime"/>
  136. /// of the previous touch, the tap count is increased by one. If more than <see cref="InputSettings.multiTapDelayTime"/>
  137. /// passes after a tap with no successive tap, the tap count is reset to zero.
  138. /// </remarks>
  139. public IntegerControl tapCount { get; private set; }
  140. /// <summary>
  141. /// Time in seconds on the same timeline as <c>Time.realTimeSinceStartup</c> when the touch began.
  142. /// </summary>
  143. /// <value>Control representing the start time of the touch.</value>
  144. /// <remarks>
  145. /// This is the value of <see cref="InputEvent.time"/> when the touch starts with
  146. /// <see cref="phase"/> <see cref="TouchPhase.Began"/>.
  147. /// </remarks>
  148. /// <seealso cref="InputEvent.time"/>
  149. public DoubleControl startTime { get; private set; }
  150. /// <summary>
  151. /// Screen-space position where the touch started.
  152. /// </summary>
  153. /// <value>Control representing the start position of the touch.</value>
  154. /// <seealso cref="position"/>
  155. public Vector2Control startPosition { get; private set; }
  156. /// <summary>
  157. /// Whether a touch on the control is currently is progress.
  158. /// </summary>
  159. /// <value>If true, a touch is in progress, i.e. has a <see cref="phase"/> of
  160. /// <see cref="TouchPhase.Began"/>, <see cref="TouchPhase.Moved"/>, or <see
  161. /// cref="TouchPhase.Canceled"/>.</value>
  162. public bool isInProgress
  163. {
  164. get
  165. {
  166. switch (phase.ReadValue())
  167. {
  168. case TouchPhase.Began:
  169. case TouchPhase.Moved:
  170. case TouchPhase.Stationary:
  171. return true;
  172. }
  173. return false;
  174. }
  175. }
  176. /// <summary>
  177. /// Default-initialize the touch control.
  178. /// </summary>
  179. /// <remarks>
  180. /// Sets the <see cref="InputStateBlock.format"/> to <c>"TOUC"</c>.
  181. /// </remarks>
  182. public TouchControl()
  183. {
  184. m_StateBlock.format = new FourCC('T', 'O', 'U', 'C');
  185. }
  186. /// <inheritdoc />
  187. protected override void FinishSetup()
  188. {
  189. press = GetChildControl<TouchPressControl>("press");
  190. touchId = GetChildControl<IntegerControl>("touchId");
  191. position = GetChildControl<Vector2Control>("position");
  192. delta = GetChildControl<Vector2Control>("delta");
  193. pressure = GetChildControl<AxisControl>("pressure");
  194. radius = GetChildControl<Vector2Control>("radius");
  195. phase = GetChildControl<TouchPhaseControl>("phase");
  196. indirectTouch = GetChildControl<ButtonControl>("indirectTouch");
  197. tap = GetChildControl<ButtonControl>("tap");
  198. tapCount = GetChildControl<IntegerControl>("tapCount");
  199. startTime = GetChildControl<DoubleControl>("startTime");
  200. startPosition = GetChildControl<Vector2Control>("startPosition");
  201. ////TODO: throw if state layouts of the controls doesn't match TouchState
  202. base.FinishSetup();
  203. }
  204. /// <inheritdoc />
  205. public override unsafe TouchState ReadUnprocessedValueFromState(void* statePtr)
  206. {
  207. var valuePtr = (TouchState*)((byte*)statePtr + (int)m_StateBlock.byteOffset);
  208. return *valuePtr;
  209. }
  210. /// <inheritdoc />
  211. public override unsafe void WriteValueIntoState(TouchState value, void* statePtr)
  212. {
  213. var valuePtr = (TouchState*)((byte*)statePtr + (int)m_StateBlock.byteOffset);
  214. UnsafeUtility.MemCpy(valuePtr, UnsafeUtility.AddressOf(ref value), UnsafeUtility.SizeOf<TouchState>());
  215. }
  216. }
  217. }