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.

TouchInputModule.cs 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Text;
  3. using UnityEngine.Serialization;
  4. namespace UnityEngine.EventSystems
  5. {
  6. [Obsolete("TouchInputModule is no longer required as Touch input is now handled in StandaloneInputModule.")]
  7. [AddComponentMenu("Event/Touch Input Module")]
  8. public class TouchInputModule : PointerInputModule
  9. {
  10. protected TouchInputModule()
  11. {}
  12. private Vector2 m_LastMousePosition;
  13. private Vector2 m_MousePosition;
  14. private PointerEventData m_InputPointerEvent;
  15. [SerializeField]
  16. [FormerlySerializedAs("m_AllowActivationOnStandalone")]
  17. private bool m_ForceModuleActive;
  18. [Obsolete("allowActivationOnStandalone has been deprecated. Use forceModuleActive instead (UnityUpgradable) -> forceModuleActive")]
  19. public bool allowActivationOnStandalone
  20. {
  21. get { return m_ForceModuleActive; }
  22. set { m_ForceModuleActive = value; }
  23. }
  24. public bool forceModuleActive
  25. {
  26. get { return m_ForceModuleActive; }
  27. set { m_ForceModuleActive = value; }
  28. }
  29. public override void UpdateModule()
  30. {
  31. if (!eventSystem.isFocused)
  32. {
  33. if (m_InputPointerEvent != null && m_InputPointerEvent.pointerDrag != null && m_InputPointerEvent.dragging)
  34. ExecuteEvents.Execute(m_InputPointerEvent.pointerDrag, m_InputPointerEvent, ExecuteEvents.endDragHandler);
  35. m_InputPointerEvent = null;
  36. }
  37. m_LastMousePosition = m_MousePosition;
  38. m_MousePosition = input.mousePosition;
  39. }
  40. public override bool IsModuleSupported()
  41. {
  42. return forceModuleActive || input.touchSupported;
  43. }
  44. public override bool ShouldActivateModule()
  45. {
  46. if (!base.ShouldActivateModule())
  47. return false;
  48. if (m_ForceModuleActive)
  49. return true;
  50. if (UseFakeInput())
  51. {
  52. bool wantsEnable = input.GetMouseButtonDown(0);
  53. wantsEnable |= (m_MousePosition - m_LastMousePosition).sqrMagnitude > 0.0f;
  54. return wantsEnable;
  55. }
  56. return input.touchCount > 0;
  57. }
  58. private bool UseFakeInput()
  59. {
  60. return !input.touchSupported;
  61. }
  62. public override void Process()
  63. {
  64. if (UseFakeInput())
  65. FakeTouches();
  66. else
  67. ProcessTouchEvents();
  68. }
  69. /// <summary>
  70. /// For debugging touch-based devices using the mouse.
  71. /// </summary>
  72. private void FakeTouches()
  73. {
  74. var pointerData = GetMousePointerEventData(0);
  75. var leftPressData = pointerData.GetButtonState(PointerEventData.InputButton.Left).eventData;
  76. // fake touches... on press clear delta
  77. if (leftPressData.PressedThisFrame())
  78. leftPressData.buttonData.delta = Vector2.zero;
  79. ProcessTouchPress(leftPressData.buttonData, leftPressData.PressedThisFrame(), leftPressData.ReleasedThisFrame());
  80. // only process move if we are pressed...
  81. if (input.GetMouseButton(0))
  82. {
  83. ProcessMove(leftPressData.buttonData);
  84. ProcessDrag(leftPressData.buttonData);
  85. }
  86. }
  87. /// <summary>
  88. /// Process all touch events.
  89. /// </summary>
  90. private void ProcessTouchEvents()
  91. {
  92. for (int i = 0; i < input.touchCount; ++i)
  93. {
  94. Touch touch = input.GetTouch(i);
  95. if (touch.type == TouchType.Indirect)
  96. continue;
  97. bool released;
  98. bool pressed;
  99. var pointer = GetTouchPointerEventData(touch, out pressed, out released);
  100. ProcessTouchPress(pointer, pressed, released);
  101. if (!released)
  102. {
  103. ProcessMove(pointer);
  104. ProcessDrag(pointer);
  105. }
  106. else
  107. RemovePointerData(pointer);
  108. }
  109. }
  110. protected void ProcessTouchPress(PointerEventData pointerEvent, bool pressed, bool released)
  111. {
  112. var currentOverGo = pointerEvent.pointerCurrentRaycast.gameObject;
  113. // PointerDown notification
  114. if (pressed)
  115. {
  116. pointerEvent.eligibleForClick = true;
  117. pointerEvent.delta = Vector2.zero;
  118. pointerEvent.dragging = false;
  119. pointerEvent.useDragThreshold = true;
  120. pointerEvent.pressPosition = pointerEvent.position;
  121. pointerEvent.pointerPressRaycast = pointerEvent.pointerCurrentRaycast;
  122. DeselectIfSelectionChanged(currentOverGo, pointerEvent);
  123. if (pointerEvent.pointerEnter != currentOverGo)
  124. {
  125. // send a pointer enter to the touched element if it isn't the one to select...
  126. HandlePointerExitAndEnter(pointerEvent, currentOverGo);
  127. pointerEvent.pointerEnter = currentOverGo;
  128. }
  129. // search for the control that will receive the press
  130. // if we can't find a press handler set the press
  131. // handler to be what would receive a click.
  132. var newPressed = ExecuteEvents.ExecuteHierarchy(currentOverGo, pointerEvent, ExecuteEvents.pointerDownHandler);
  133. // didnt find a press handler... search for a click handler
  134. if (newPressed == null)
  135. newPressed = ExecuteEvents.GetEventHandler<IPointerClickHandler>(currentOverGo);
  136. // Debug.Log("Pressed: " + newPressed);
  137. float time = Time.unscaledTime;
  138. if (newPressed == pointerEvent.lastPress)
  139. {
  140. var diffTime = time - pointerEvent.clickTime;
  141. if (diffTime < 0.3f)
  142. ++pointerEvent.clickCount;
  143. else
  144. pointerEvent.clickCount = 1;
  145. pointerEvent.clickTime = time;
  146. }
  147. else
  148. {
  149. pointerEvent.clickCount = 1;
  150. }
  151. pointerEvent.pointerPress = newPressed;
  152. pointerEvent.rawPointerPress = currentOverGo;
  153. pointerEvent.clickTime = time;
  154. // Save the drag handler as well
  155. pointerEvent.pointerDrag = ExecuteEvents.GetEventHandler<IDragHandler>(currentOverGo);
  156. if (pointerEvent.pointerDrag != null)
  157. ExecuteEvents.Execute(pointerEvent.pointerDrag, pointerEvent, ExecuteEvents.initializePotentialDrag);
  158. m_InputPointerEvent = pointerEvent;
  159. }
  160. // PointerUp notification
  161. if (released)
  162. {
  163. // Debug.Log("Executing pressup on: " + pointer.pointerPress);
  164. ExecuteEvents.Execute(pointerEvent.pointerPress, pointerEvent, ExecuteEvents.pointerUpHandler);
  165. // Debug.Log("KeyCode: " + pointer.eventData.keyCode);
  166. // see if we mouse up on the same element that we clicked on...
  167. var pointerUpHandler = ExecuteEvents.GetEventHandler<IPointerClickHandler>(currentOverGo);
  168. // PointerClick and Drop events
  169. if (pointerEvent.pointerPress == pointerUpHandler && pointerEvent.eligibleForClick)
  170. {
  171. ExecuteEvents.Execute(pointerEvent.pointerPress, pointerEvent, ExecuteEvents.pointerClickHandler);
  172. }
  173. else if (pointerEvent.pointerDrag != null && pointerEvent.dragging)
  174. {
  175. ExecuteEvents.ExecuteHierarchy(currentOverGo, pointerEvent, ExecuteEvents.dropHandler);
  176. }
  177. pointerEvent.eligibleForClick = false;
  178. pointerEvent.pointerPress = null;
  179. pointerEvent.rawPointerPress = null;
  180. if (pointerEvent.pointerDrag != null && pointerEvent.dragging)
  181. ExecuteEvents.Execute(pointerEvent.pointerDrag, pointerEvent, ExecuteEvents.endDragHandler);
  182. pointerEvent.dragging = false;
  183. pointerEvent.pointerDrag = null;
  184. // send exit events as we need to simulate this on touch up on touch device
  185. ExecuteEvents.ExecuteHierarchy(pointerEvent.pointerEnter, pointerEvent, ExecuteEvents.pointerExitHandler);
  186. pointerEvent.pointerEnter = null;
  187. m_InputPointerEvent = pointerEvent;
  188. }
  189. }
  190. public override void DeactivateModule()
  191. {
  192. base.DeactivateModule();
  193. ClearSelection();
  194. }
  195. public override string ToString()
  196. {
  197. var sb = new StringBuilder();
  198. sb.AppendLine(UseFakeInput() ? "Input: Faked" : "Input: Touch");
  199. if (UseFakeInput())
  200. {
  201. var pointerData = GetLastPointerEventData(kMouseLeftId);
  202. if (pointerData != null)
  203. sb.AppendLine(pointerData.ToString());
  204. }
  205. else
  206. {
  207. foreach (var pointerEventData in m_PointerData)
  208. sb.AppendLine(pointerEventData.ToString());
  209. }
  210. return sb.ToString();
  211. }
  212. }
  213. }