public static void OnSwipeInvoke(object sender, InputEventArg arg) { if (OnSwipe != null) { OnSwipe.Invoke(null, arg); } }
private void CheckSwipeEditor() { if (Input.GetMouseButtonDown(0)) //pressed { startPos = Input.mousePosition; holdTime = 0; } else if (Input.GetMouseButton(0)) //held { holdTime += Time.deltaTime; } else if (Input.GetMouseButtonUp(0)) //de-pressed { Vector2 endPos = Input.mousePosition; Vector2 deltaPos = endPos - startPos; //Debug.Log("" + (deltaPos.magnitude > swipeDistanceThreshold) + " and " + (holdTime < swipeTimeThreshold)); if (deltaPos.magnitude > swipeDistanceThreshold && holdTime < swipeTimeThreshold) { OnSwipe?.Invoke(deltaPos, endPos); } else { OnRelease?.Invoke(); } } }
protected override void SwipeDetection() { if (Input.GetMouseButtonDown(0)) { lastPosition = firstPosition; } else if (Input.GetMouseButtonUp(0)) { lastPosition = Input.mousePosition; if (Mathf.Abs(lastPosition.y - firstPosition.y) > dragDistance) { if (lastPosition.y > firstPosition.y) { OnSwipeUp?.Invoke(); } else { OnSwipeDown?.Invoke(); } Debug.Log("OnSwipe"); OnSwipe?.Invoke(); } else { Debug.Log("Tap"); } OnResetPosition(); } }
private void FireSwipeEvent() { Vector2 diff = endPos - startPos; Direction dir; if (Mathf.Abs(diff.x) > Mathf.Abs(diff.y)) { dir = diff.x > 0 ? Direction.Right : Direction.Left; } else { dir = diff.y > 0 ? Direction.Up : Direction.Down; } GameObject hitGameObject = GetHitGameObject(startPos); SwipeEventArgs args = new SwipeEventArgs(startPos, diff, dir, hitGameObject); OnSwipe?.Invoke(this, args); if (hitGameObject != null) { if (hitGameObject.TryGetComponent(out ISwipeable swipeable)) { swipeable.OnSwipe(args); } } }
//Place your public methods here private void MouseDebug() { // this is used convert touch pos to world pos Vector3 VScreen = new Vector3(); VScreen.x = Input.mousePosition.x; VScreen.y = Input.mousePosition.y; VScreen.z = Camera.main.transform.position.z; if (Input.GetMouseButtonDown(0)) { // get the mouse down and store the touch location moveData.firstTouch = Camera.main.ScreenToWorldPoint(VScreen) * -1; } else if (Input.GetMouseButtonUp(0)) { // get the mouse up and store the touch location moveData.lastTouch = Camera.main.ScreenToWorldPoint(VScreen) * -1; } // get the distance between the first touch and the last touch float dist = Vector3.Distance(moveData.firstTouch, moveData.lastTouch); // if touch is greater than the minimum distance to classify as a swipe if (dist > minimumSwipeDistance / 50 && moveData.lastTouch != Vector2.zero) { // then invoke the Onswipe event. OnSwipe?.Invoke(this, new directionMoveArgs(moveData, useCardinalDirectionForSwipe)); // zero data moveData.zero(); } }
public void OnDrag(PointerEventData eventData) { if (_isCompleteSwipe == true) { return; } Vector2 difference = (eventData.position - _startSwipePosition); Vector2 directionNormalize = difference.normalized; if (difference.magnitude < 8) { return; } Vector3 swipeDirection; if (Mathf.Abs(directionNormalize.x) > Mathf.Abs(directionNormalize.y)) { swipeDirection = directionNormalize.x > 0 ? transform.right : -transform.right; } else { swipeDirection = directionNormalize.y > 0 ? Vector3.up : -Vector3.up; } OnSwipe?.Invoke(swipeDirection); _isCompleteSwipe = true; }
private void CheckForHorizontalMovement(float movementAmount) { if ((totalSwipeAmount + movementAmount) < maxSwipeAmount && (totalSwipeAmount + movementAmount) > -maxSwipeAmount) { totalSwipeAmount += movementAmount; OnSwipe?.Invoke(movementAmount); } }
void Update() { if (Input.touches.Length < 0) { return; } if (Input.touches.Length > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { this._downPossition = Input.GetTouch(0).position; } if (this._downPossition == Vector2.zero) { return; } else if (this._dragTimer > 0) { this._dragTimer -= Time.deltaTime; } if (Input.GetTouch(0).phase == TouchPhase.Moved) { this._currentPossiton = Input.GetTouch(0).position; float distance = this.GetSwipeDistance(this._downPossition, this._currentPossiton); if (distance != 0 && this._dragTimer > 0) { this._swiping = true; OnMove?.Invoke(distance); } else if (this._dragTimer <= 0 && !this._swiping) { OnDrag?.Invoke(this._currentPossiton); } } if (Input.GetTouch(0).phase == TouchPhase.Ended) { this._upPossition = Input.GetTouch(0).position; int direction = this.GetSwipeDirection(this._downPossition, this._upPossition); if (direction != 0) { this._swiping = false; OnSwipe?.Invoke(direction); } else if (!this._swiping) { OnTouch?.Invoke(this._downPossition); } this._dragTimer = this._dragTimerReset; this._downPossition = Vector2.zero; } }
/// <summary> /// Fired when item is fully swiped /// </summary> public override void OnSwiped(RecyclerView.ViewHolder p0, int p1) { // Notify that the item has changed so the UI will place it in initial position mAdapter.NotifyItemChanged(p0.AdapterPosition); // Experimental - Debugging always shows p1 = 16 when swiping to the right for some reason // So I assume its the correct value, but its not really safe to just do that // So if any bugs happen, fix there OnSwipe.Invoke(p0.AdapterPosition, p1 == 16); // Don't notify about removal here, because user can cancel it // Just a note that it was there initially and may be needed in the future //mAdapter.NotifyItemRemoved(p0.AdapterPosition); }
protected override void SwipeDetection() { if (Input.touchCount == 1) { Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began) { firstPosition = touch.position; lastPosition = touch.position; } else if (touch.phase == TouchPhase.Moved) { lastPosition = touch.position; isTouchMoved = true; } else if (touch.phase == TouchPhase.Ended) { firstPosition = touch.position; if (Mathf.Abs(lastPosition.y - firstPosition.y) > dragDistance) { if (isTouchMoved) { if (lastPosition.y > firstPosition.y) { OnSwipeUp?.Invoke(); } else { OnSwipeDown?.Invoke(); } OnSwipe?.Invoke(); isTouchMoved = false; } } } else { Debug.Log("Tap"); } OnResetPosition(); } }
private void ProcessTouch() { Vector2 diff = CustomTouchData.DistanceDiff; if (CustomTouchData.DistanceDiff.sqrMagnitude < _minSwipeDistance) //stopped { //Debug.Log($"stopped:: {CustomTouchData.DistanceDiff.sqrMagnitude}"); return; } float timeDiff = CustomTouchData.TimeDiff; SwipeDirection swipeDir = GetDirection(diff.normalized); float speed = (diff.sqrMagnitude * 100) / (timeDiff + 0.1f); //Debug.Log($"processing touch::: {swipeDir} : {speed}"); SaveCurrentTouch(); OnSwipe?.Invoke(swipeDir, new Vector2(diff.sqrMagnitude, speed)); }
private void CheckSwipeTouch() { if (Input.touches.Length > 0) { Touch touch = Input.GetTouch(0); switch (touch.phase) { case TouchPhase.Began: startPos = touch.position; holdTime = 0; break; case TouchPhase.Moved: holdTime += Time.deltaTime; break; case TouchPhase.Stationary: break; case TouchPhase.Ended: Vector2 endPos = touch.position; Vector2 deltaPos = endPos - startPos; if (deltaPos.magnitude > swipeDistanceThreshold && holdTime < swipeTimeThreshold) { OnSwipe?.Invoke(deltaPos, endPos); } else { OnRelease?.Invoke(); } break; case TouchPhase.Canceled: break; default: break; } } }
private void HandlePanUpdated(object sender, PanUpdatedEventArgs e) { View Content = (View)sender; switch (e.StatusType) { case GestureStatus.Running: try { translateX = e.TotalX; translateY = e.TotalY; } catch (Exception err) { System.Diagnostics.Debug.WriteLine("SwipeGestureRecognizer" + err.Message); } break; case GestureStatus.Completed: if (translateX < 0 && Math.Abs(translateX) > Math.Abs(translateY)) { OnSwipe?.Invoke(sender, Direction.left); } else if (translateX > 0 && translateX > Math.Abs(translateY)) { OnSwipe?.Invoke(sender, Direction.right); } else if (translateY < 0 && Math.Abs(translateY) > Math.Abs(translateX)) { OnSwipe?.Invoke(sender, Direction.up); } else if (translateY > 0 && translateY > Math.Abs(translateX)) { OnSwipe?.Invoke(sender, Direction.down); } break; } }
private void Hold(Vector2 position) { if (!swiped && !isHolding) { Vector2 delta = position - lastTouchPosition; lastTouchPosition = position; if (delta.sqrMagnitude > minSwipeSpeed * minSwipeSpeed) { swiped = true; Direction swipeDirection = GetSwipeDirection(delta); OnSwipe?.Invoke(swipeDirection); return; } holdingTime += Time.deltaTime; if (hitCell != null && holdingTime >= minHoldTime) { isHolding = true; OnHold?.Invoke(hitCell); } } }
void Update() { if (CanSwipe) { if (Input.GetMouseButtonDown(0)) { startPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); if (OnSwipeStarted != null) { OnSwipeStarted.Invoke(startPosition); } } else if (Input.GetMouseButton(0)) { Vector3 currentPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector3 movement = startPosition - currentPosition; if (movement != Vector3.zero) { if (OnSwipe != null) { OnSwipe.Invoke(movement); } } } else if (Input.GetMouseButtonUp(0)) { Vector3 currentPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector3 movement = startPosition - currentPosition; if (OnSwipeEnded != null) { OnSwipeEnded.Invoke(movement); } } } }
public void OnEndDrag(PointerEventData eventData) { mEndPos = mMainCam.ScreenToWorldPoint(eventData.position); Vector2 diff = mEndPos - mStartPos; if (diff.sqrMagnitude < m_MinSwipeStrength) { Debug.Log("swipe strength too small to recognize"); } else { diff = diff.normalized; float angleInDegree = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg; angleInDegree = angleInDegree < 0 ? 360 + angleInDegree : angleInDegree; Direction swipeDir = Direction.UP; if (angleInDegree >= 40 && angleInDegree < 140) { swipeDir = Direction.UP; } else if (angleInDegree >= 140 && angleInDegree < 230) { swipeDir = Direction.LEFT; } else if (angleInDegree >= 230 && angleInDegree < 320) { swipeDir = Direction.DOWN; } else if (angleInDegree >= 320 || (angleInDegree >= 0 && angleInDegree < 40)) { swipeDir = Direction.RIGHT; } //Debug.Log ("swipe dir :: " + swipeDir + " , " + angleInDegree); OnSwipe?.Invoke(swipeDir); } }
void Update() { if (Input.touchCount > 0) { touch = Input.GetTouch(0); switch (touch.phase) { case TouchPhase.Began: beginTouchPosition = touch.position; sendingSwipesIsEnabled = true; break; case TouchPhase.Moved: endTouchPosition = touch.position; if (!ShouldDiscardSwipe(beginTouchPosition) && sendingSwipesIsEnabled) { Direction direction = DetectSwipe(); var data = ToData(direction); sendingSwipesIsEnabled = false; OnSwipe?.Invoke(data); } break; case TouchPhase.Ended: endTouchPosition = touch.position; if (!ShouldDiscardSwipe(beginTouchPosition)) { Direction direction = DetectSwipe(); var data = ToData(direction); OnReleaseSwipe?.Invoke(data); } break; } } }
private void ProcessSwipe(PointerEventData eventData) { _lastPosition = eventData.position; _inputLog.Add(new InputStamp { Position = _lastPosition, Time = Time.time }); var minSwipeTime = Time.time - SwipeTime; while (_inputLog[0].Time < minSwipeTime) { _inputLog.RemoveAt(0); } var diff = eventData.position - _inputLog[0].Position; var swipeLength = diff.magnitude; var swipeLengthRatio = swipeLength / Screen.width; if (swipeLengthRatio >= SwipeLength) { OnSwipe?.Invoke(diff.normalized); Debug.Log("Swipe " + swipeLengthRatio + " for " + (Time.time - _inputLog[0].Time) + " seconds."); } }
void Update() { if (!isEnabled) { return; } //#if !UNITY_ANDROID && !UNITY_IOS if (Input.GetMouseButtonDown(0)) { isTouching = true; startTouchPos = Input.mousePosition; } else if (Input.GetMouseButtonUp(0)) { isTouching = false; } else if (isTouching) { Vector3 currentPos = Input.mousePosition; if (startTouchPos.x - currentPos.x > swipeTreshold) { isTouching = false; OnSwipeLeft.Invoke(); } else if (startTouchPos.x - currentPos.x < -swipeTreshold) { isTouching = false; OnSwipeRight.Invoke(); } } if (Input.GetKeyDown(KeyCode.LeftArrow)) { OnSwipeLeft.Invoke(); } else if (Input.GetKeyDown(KeyCode.RightArrow)) { OnSwipeRight.Invoke(); } //#else // if (Input.touchCount <= 0) // return; // Touch touch = Input.GetTouch(0); // switch (touch.phase) // { // case TouchPhase.Began: // isTouching = true; // startTouchPos = touch.position; // break; // case TouchPhase.Ended: // case TouchPhase.Canceled: // isTouching = false; // break; // case TouchPhase.Moved: // Vector3 currentPos = touch.position; // if (isTouching && startTouchPos.x - currentPos.x > swipeTreshold) // { // isTouching = false; // OnSwipeLeft.Invoke(); // } // else if (isTouching && startTouchPos.x - currentPos.x < -swipeTreshold) // { // isTouching = false; // OnSwipeRight.Invoke(); // } // break; // } //#endif }
private void UpdateTouches() { if (input == null || input.TouchCount() == 0) { return; } if (input.TouchCount() == 1) { TouchInfo touchInfo = input.GetTouch(0); if (currentTouch.GestureType == GestureType.None) { actionStartTime = Time.realtimeSinceStartup; } touchInfo.StartTime = actionStartTime; if (touchInfo.Phase == TouchPhase.Began) { Touch touch = new Touch { TouchInfo = touchInfo, GestureType = GestureType.None, SwipeDirection = SwipeDirection.None, SwipeLength = 0, SwipeVector = Vector2.zero }; currentTouch = touch; OnTouchStart?.Invoke(touch); } if (touchInfo.Phase == TouchPhase.Moved) { OnTouch?.Invoke(new Touch { TouchInfo = touchInfo, GestureType = GestureType.None, }); if (touchInfo.PositionDelta.magnitude >= settings.GetSwipeLengthInPixels()) { Touch touch = new Touch { TouchInfo = touchInfo, GestureType = GestureType.Swipe, SwipeDirection = GetSwipeDirection(touchInfo.Position - touchInfo.PositionDelta, touchInfo.Position), SwipeLength = touchInfo.PositionDelta.magnitude, SwipeVector = touchInfo.PositionDelta }; switch (currentTouch.GestureType) { case GestureType.None: OnSwipeStart?.Invoke(touch); break; case GestureType.Swipe: OnSwipe?.Invoke(touch); break; } currentTouch = touch; return; } } if (touchInfo.Phase == TouchPhase.Stationary) { OnTouch?.Invoke(new Touch { TouchInfo = touchInfo, GestureType = GestureType.None, }); touchInfo.ActionTime = Time.realtimeSinceStartup - touchInfo.StartTime; currentTouch = new Touch { TouchInfo = touchInfo, GestureType = GestureType.Tap, SwipeDirection = SwipeDirection.None, SwipeLength = 0, SwipeVector = Vector2.zero }; } if (touchInfo.Phase == TouchPhase.Ended) { OnTouchEnd?.Invoke(new Touch { TouchInfo = touchInfo, GestureType = GestureType.None, }); switch (currentTouch.GestureType) { case GestureType.Swipe: OnSwipeEnd?.Invoke(currentTouch); break; case GestureType.Tap when currentTouch.TouchInfo.ActionTime <= settings.TapTime: OnTap?.Invoke(currentTouch); break; } } } if (input.TouchCount() == 2) { TouchInfo touch0 = input.GetTouch(0); TouchInfo touch1 = input.GetTouch(1); if (currentTouch.GestureType == GestureType.None) { actionStartTime = Time.realtimeSinceStartup; } touch0.StartTime = actionStartTime; if (touch0.Phase == TouchPhase.Began || touch1.Phase == TouchPhase.Began) { Pinch pinch = new Pinch { TouchInfos = new [] { touch0, touch1 }, GestureType = GestureType.None, PinchVector = touch0.Position - touch1.Position }; currentPinch = pinch; OnPinchStart?.Invoke(currentPinch); } if (touch0.Phase != TouchPhase.Stationary || touch0.Phase == TouchPhase.Moved || touch1.Phase != TouchPhase.Stationary || touch1.Phase == TouchPhase.Moved) { Pinch pinch = new Pinch { TouchInfos = new [] { touch0, touch1 }, GestureType = GestureType.Pinch, PinchVector = touch0.Position - touch1.Position }; currentPinch = pinch; OnPinch?.Invoke(currentPinch); } if (touch0.Phase == TouchPhase.Ended || touch1.Phase == TouchPhase.Ended) { Pinch pinch = new Pinch { TouchInfos = new [] { touch0, touch1 }, GestureType = GestureType.Pinch, PinchVector = touch0.Position - touch1.Position }; currentPinch = pinch; OnPinchEnd?.Invoke(currentPinch); } } }
public void OnSwipeScreen(InputAction.CallbackContext context) { OnSwipe?.Invoke(this, context.ReadValue <float>()); }
public override void Update() { if (Input.touchCount == 2) { theTouchs[0] = Input.GetTouch(0); theTouchs[1] = Input.GetTouch(1); if (theTouchs[0].phase == TouchPhase.Began) { pinchHasFinished = false; // get the touch began and store the touch location pinchData[0].firstTouch = theTouchs[0].position; } if (theTouchs[1].phase == TouchPhase.Began) { pinchData[1].firstTouch = theTouchs[1].position; } if (theTouchs[0].phase == TouchPhase.Moved || theTouchs[1].phase == TouchPhase.Moved) { Vector2 pos = theTouchs[0].position; pinchData[0].lastTouch = pos; pinchData[1].lastTouch = theTouchs[1].position; float distance = Vector3.Distance(pinchData[0].firstTouch, pinchData[0].lastTouch); float distance2 = Vector3.Distance(pinchData[1].firstTouch, pinchData[1].lastTouch); if (distance > minimumPinchDistance && distance2 > minimumPinchDistance) { // then invoke the Onswipe event. OnPinch?.Invoke(this, new pinchMoveArgs(pinchData)); } } else if (theTouchs[0].phase == TouchPhase.Stationary && theTouchs[1].phase == TouchPhase.Stationary) { theTouchs[0].phase = TouchPhase.Ended; theTouchs[1].phase = TouchPhase.Ended; } // zero data else if (theTouchs[0].phase == TouchPhase.Ended || theTouchs[1].phase == TouchPhase.Ended) { pinchData[0].zero(); pinchData[1].zero(); return; } } else if (Input.touchCount == 1 && pinchHasFinished) { // get the touch data if one finger is touching the screen theTouch = Input.GetTouch(0); if (theTouch.phase == TouchPhase.Began) { // get the touch began and store the touch location moveData.firstTouch = theTouch.position; // Camera.main.ScreenToWorldPoint(VScreen); } if (theTouch.phase == TouchPhase.Ended) { // get the touch Ended and store the touch location moveData.lastTouch = theTouch.position; // Camera.main.ScreenToWorldPoint(VScreen); // get the time the the touch ended timeTouchEnded = Time.time; // get the distance the finger moved between the first and last touch. float distance = Vector3.Distance(moveData.firstTouch, moveData.lastTouch); // check if it moved over the mimimum distance if (distance > minimumSwipeDistance) { // then invoke the Onswipe event. OnSwipe?.Invoke(this, new directionMoveArgs(moveData, useCardinalDirectionForSwipe)); // zero data moveData.zero(); } else if (Time.time - timeTouchEnded < touchPressTime) { // if the not a swipe OnPress?.Invoke(this, EventArgs.Empty); } } } // mouse debuging if (mouseTesting) { MouseDebug(); } base.Update(); }
protected override void CacheInput() { // Queue 0 IsScreenTap = GetIsScreenTap(); IsScreenTapDown = GetIsScreenTapDown(); IsScreenTapUp = GetIsScreenTapUp(); IsMultipleTouch = GetIsMultipleTouch(); DeltaTouchPosition = GetDeltaTouchPosition(); TouchPosition = GetTouchPosition(); SwipeVector = GetSwipeVector(); DoubleSwipeVector = GetDoubleSwipeVector(); // Queue 1 IsTap = GetIsTap(); IsTapDown = GetIsTapDown(); IsTapUp = GetIsTapUp(); // Queue 2 HorizontalSwipe = GetHorizontalSwipe(); VerticalSwipe = GetVerticalSwipe(); HorizontalDoubleSwipe = GetHorizontalDoubleSwipe(); VerticalDoubleSwipe = GetVerticalDoubleSwipe(); Zoom = GetZoom(); ZoomPoint = GetZoomPoint(); Rotation = GetRotation(); IsZoom = Mathf.Abs(Zoom) > 0; IsSwipe = Mathf.Abs(HorizontalSwipe) > 0 || Mathf.Abs(VerticalSwipe) > 0; IsDoubleSwipe = Mathf.Abs(HorizontalDoubleSwipe) > 0 || Mathf.Abs(VerticalDoubleSwipe) > 0; //Events if (IsTapDown) { OnTapDown?.Invoke(); } if (IsTapUp) { OnTapUp?.Invoke(); } if (IsScreenTapDown) { OnScreenTapDown?.Invoke(); } if (IsScreenTapUp) { OnScreenTapUp?.Invoke(); } if (IsSwipe) { OnSwipe?.Invoke(); } if (IsDoubleSwipe) { OnDoubleSwipe?.Invoke(); } if (IsZoom) { OnZoom?.Invoke(); } //Debug.Log($"FRAME {Time.frameCount}: IsTapDown = {IsTapDown}"); }
private void LeanTouch_OnFingerSwipe(LeanFinger finger) { OnSwipe?.Invoke(finger.GetWorldPosition(10f), finger.SwipeScaledDelta); }
void SendSwipeEvent(ESwipeDirection dir) { OnSwipe?.Invoke(this, new OnSwipeEventArgs(dir)); }