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