private void BehavourOnIndexChanged() { if (OnSwipe != null) { OnSwipe.Execute(); } }
protected virtual void Scroll(float offset) { if (OnSwipe != null) { OnSwipe.Execute(); } }
//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(); } }
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); } } }
public static void OnSwipeInvoke(object sender, InputEventArg arg) { if (OnSwipe != null) { OnSwipe.Invoke(null, arg); } }
private void HandleIndexChanged() { if (OnSwipe != null) { OnSwipe.Execute(); } }
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(); } }
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 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(); } } }
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; } }
private void CheckForHorizontalMovement(float movementAmount) { if ((totalSwipeAmount + movementAmount) < maxSwipeAmount && (totalSwipeAmount + movementAmount) > -maxSwipeAmount) { totalSwipeAmount += movementAmount; OnSwipe?.Invoke(movementAmount); } }
public ClassicSwipeDetector() { oldLocalTouches = new Dictionary <int, Touch>(); resetDeltaPos = delegate { deltaPosition = 0; }; resetDeltaTime = delegate { deltaTime = 0; }; }
// Start is called before the first frame update void Start() { detector = new ClassicSwipeDetector(); swipeRight = delegate { Debug.Log("swipe right"); transform.position += Vector3.right / 20; }; swipeLeft = delegate { Debug.Log("swipe left"); transform.position += Vector3.left / 20; }; }
/// <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); }
public override bool DetectSwipe(ref Touch[] touches, SwipeDirection direction, OnSwipe doSwipe, float distance = -1) { Touch oldTouch, newTouch; foreach (Touch touch in touches) { Debug.Log(touch.position); if (touch.phase == TouchPhase.Began) { deltaTime = 0; if (!oldLocalTouches.ContainsKey((int)direction)) { oldLocalTouches.Add((int)direction, touch); oldTouch = touch; } else { oldLocalTouches[(int)direction] = touch; oldTouch = touch; } } else { if (touch.phase == TouchPhase.Stationary) { deltaTime = 0; } if (!oldLocalTouches.ContainsKey((int)direction)) { oldLocalTouches.Add((int)direction, touch); oldTouch = touch; } else { oldTouch = oldLocalTouches[(int)direction]; } } newTouch = touch; if (this.DetectSwipe(ref oldTouch, ref newTouch, direction, doSwipe, distance)) { return(true); } } return(false); }
void HandleDraggingEnded(bool willDecelerate) { if (_view != null) { if (_startX != _view.ContentOffset.X || _startY != _view.ContentOffset.Y) { OnScrollEnded(_startX, _startY); if (OnSwipe != null) { OnSwipe.Execute(); } } } }
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)); }
public override bool DetectSwipe(ref Touch[] touches, SwipeDirection direction, OnSwipe doSwipe, float distance = -1) { foreach (Touch touch in touches) { if (touch.phase == TouchPhase.Began) { oldTouch = touch; } else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Ended) { newTouch = touch; if (this.DetectSwipe(ref oldTouch, ref newTouch, direction, doSwipe, distance)) { return(true); } } } return(false); }
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); } } }
/// <summary>It detect the swipe even if you keep your finger stuck to the screen. /// It doesn't consider the movement speed of the finger ///</summary> public override bool DetectSwipe(ref Touch oldTouch, ref Touch newTouch, SwipeDirection direction, OnSwipe doSwipe, float distance = -1) { if (isTouchGood(oldTouch, newTouch)) { //I need to copy because I can't pass ref to delegates Touch newTouchCopy = newTouch; Touch oldTouchCopy = oldTouch; OnSwipe doUpdateLocalTouch = delegate(SwipeDirection dir) { oldTouchCopy.position = newTouchCopy.position; oldLocalTouches[(int)direction] = oldTouchCopy; }; StupidSwipeDetector stupidDetector = new StupidSwipeDetector(); /*The oldPosition is updated at step of 1f. In this way the swipe can be detected * many time without taking out the finger from the screen * I update only the local copy, in this way it's possible to detect opposite swipes simultaneously */ stupidDetector.DetectSwipe(ref oldTouch, ref newTouch, (SwipeDirection)(-(int)direction), doUpdateLocalTouch, 1f); if (oldTouchCopy.position != newTouch.position) { OnSwipe doUpdateRefTouch = delegate(SwipeDirection dir) { oldTouchCopy.position = newTouchCopy.position; }; //if the swipe is detected, I update the real oldTouch if (stupidDetector.DetectSwipe(ref oldTouch, ref newTouch, direction, doSwipe + doUpdateRefTouch + doUpdateLocalTouch, distance)) { oldTouch = oldTouchCopy; return(true); } } } return(false); }
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); } }
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."); } }
/// <summary>It detect the swipe only when the finger is lifted from the screen. /// It detect swipe if the distance between position is big enough or if the finger moves /// faster then the speed limit ///</summary> public override bool DetectSwipe(ref Touch oldTouch, ref Touch newTouch, SwipeDirection direction, OnSwipe doSwipe, float distance = -1) { deltaTime += newTouch.deltaTime; if (IsHorizontalSwipe(direction)) { deltaPosition += Math.Abs(newTouch.deltaPosition.x); } else if (IsHorizontalSwipe(direction)) { deltaPosition += Math.Abs(newTouch.deltaPosition.y); } if (isTouchGood(oldTouch, newTouch)) { if (oldTouch.position != newTouch.position) { StupidSwipeDetector stupidDetector = new StupidSwipeDetector(); //if the finger go in the opposite direction, i reset the delta time stupidDetector.DetectSwipe(ref oldTouch, ref newTouch, (SwipeDirection)(-(int)direction), resetDeltaTime, 1f); OnSwipe swipeAction = doSwipe + resetDeltaTime + resetDeltaPos; if (deltaTime / deltaPosition >= speedLimit) { Debug.Log("aaa"); return(stupidDetector.DetectSwipe(ref oldTouch, ref newTouch, direction, swipeAction, minDistSpeedDetection)); } else { Debug.Log("bbb"); return(stupidDetector.DetectSwipe(ref oldTouch, ref newTouch, direction, swipeAction, distance)); } } } return(false); }
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; } } }
public void OnSwipeScreen(InputAction.CallbackContext context) { OnSwipe?.Invoke(this, context.ReadValue <float>()); }
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}"); }