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.

TouchControl.cs 11KB

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