/// <summary> /// New version of ShouldStartDrag implemented first in PointerInputModule. This version differs in that /// for ray based pointers it makes a decision about whether a drag should start based on the angular change /// the pointer has made so far, as seen from the camera. This also works when the world space ray is /// translated rather than rotated, since the beginning and end of the movement are considered as angle from /// the same point. /// </summary> private bool ShouldStartDrag(PointerEventData pointerEvent) { if (!pointerEvent.useDragThreshold) { return(true); } if (!pointerEvent.IsSyncVRPointer()) { // Same as original behaviour for canvas based pointers return((pointerEvent.pressPosition - pointerEvent.position).sqrMagnitude >= eventSystem.pixelDragThreshold * eventSystem.pixelDragThreshold); } else { #if UNITY_ANDROID && !UNITY_EDITOR // On android allow swiping to start drag if (useSwipeScroll && ((Vector3)pointerEvent.SyncVRGetSwipeStart() - Input.mousePosition).magnitude > swipeDragThreshold) { return(true); } #endif // When it's not a screen space pointer we have to look at the angle it moved rather than the pixels distance // For gaze based pointing screen-space distance moved will always be near 0 Vector3 cameraPos = pointerEvent.pressEventCamera.transform.position; Vector3 pressDir = (pointerEvent.pointerPressRaycast.worldPosition - cameraPos).normalized; Vector3 currentDir = (pointerEvent.pointerCurrentRaycast.worldPosition - cameraPos).normalized; return(Vector3.Dot(pressDir, currentDir) < Mathf.Cos(Mathf.Deg2Rad * (angleDragThreshold))); } }
/// <summary> /// The purpose of this function is to allow us to switch between using the standard IsPointerMoving /// method for mouse driven pointers, but to always return true when it's a ray based pointer. /// All real-world ray-based input devices are always moving so for simplicity we just return true /// for them. /// /// If PointerEventData.IsPointerMoving was virtual we could just override that in /// OVRRayPointerEventData. /// </summary> /// <param name="pointerEvent"></param> /// <returns></returns> static bool IsPointerMoving(PointerEventData pointerEvent) { if (pointerEvent.IsSyncVRPointer()) { return(true); } else { return(pointerEvent.IsPointerMoving()); } }
/// <summary> /// Exactly the same as the code from PointerInputModule, except that we call our own /// IsPointerMoving. /// /// This would also not be necessary if PointerEventData.IsPointerMoving was virtual /// </summary> /// <param name="pointerEvent"></param> protected override void ProcessDrag(PointerEventData pointerEvent) { Vector2 originalPosition = pointerEvent.position; bool moving = IsPointerMoving(pointerEvent); if (moving && pointerEvent.pointerDrag != null && !pointerEvent.dragging && ShouldStartDrag(pointerEvent)) { if (pointerEvent.IsSyncVRPointer()) { //adjust the position used based on swiping action. Allowing the user to //drag items by swiping on the GearVR touchpad pointerEvent.position = SwipeAdjustedPosition(originalPosition, pointerEvent); } ExecuteEvents.Execute(pointerEvent.pointerDrag, pointerEvent, ExecuteEvents.beginDragHandler); pointerEvent.dragging = true; } // Drag notification if (pointerEvent.dragging && moving && pointerEvent.pointerDrag != null) { if (pointerEvent.IsSyncVRPointer()) { pointerEvent.position = SwipeAdjustedPosition(originalPosition, pointerEvent); } // Before doing drag we should cancel any pointer down state // And clear selection! if (pointerEvent.pointerPress != pointerEvent.pointerDrag) { ExecuteEvents.Execute(pointerEvent.pointerPress, pointerEvent, ExecuteEvents.pointerUpHandler); pointerEvent.eligibleForClick = false; pointerEvent.pointerPress = null; pointerEvent.rawPointerPress = null; } ExecuteEvents.Execute(pointerEvent.pointerDrag, pointerEvent, ExecuteEvents.dragHandler); } }
/// <summary> /// Perform a raycast using the worldSpaceRay in eventData. /// </summary> /// <param name="eventData"></param> /// <param name="resultAppendList"></param> public override void Raycast(PointerEventData eventData, List <RaycastResult> resultAppendList) { // This function is closely based on PhysicsRaycaster.Raycast if (eventCamera == null) { return; } if (!eventData.IsSyncVRPointer()) { return; } var ray = eventData.SyncVRGetRay(); float dist = eventCamera.farClipPlane - eventCamera.nearClipPlane; var hits = Physics.RaycastAll(ray, dist, finalEventMask); if (hits.Length > 1) { System.Array.Sort(hits, (r1, r2) => r1.distance.CompareTo(r2.distance)); } if (hits.Length != 0) { for (int b = 0, bmax = hits.Length; b < bmax; ++b) { var result = new RaycastResult { gameObject = hits[b].collider.gameObject, module = this, distance = hits[b].distance, index = resultAppendList.Count, worldPosition = hits[0].point, worldNormal = hits[0].normal, }; resultAppendList.Add(result); } } }