// SET ACTION private void SetAction() { if (hasMoved) { // SWIPE if (speed > TouchHandler._swipeThreshold) { action = TouchHandler.actions.Swipe; swipeDirection = GetSwipeDirection(); } else { swipeStartPos = currentPos; } // DRAG if (action != TouchHandler.actions.Swipe) { action = TouchHandler.actions.Drag; } } else { // TAP if (_touch.phase.IsDone() && currentPressTime < TouchHandler._longPressTime) { action = TouchHandler.actions.Tap; } // LONG-PRESS if (currentPressTime > TouchHandler._longPressTime) { action = TouchHandler.actions.LongPress; } } }
public TouchInstance(Touch t) { _touch = t; fingerId = t.fingerId; startPos = _touch.position; swipeStartPos = _touch.position; startTime = Time.time; action = TouchHandler.actions.Down; overrideAction = TouchHandler.actions.None; }
// JUST (RE)TAPPED public void AddTap() { tapCount++; tapTimer = 0f; currentPressTime = 0f; distanceTraveled = 0f; startPos = _touch.position; swipeStartPos = _touch.position; overrideAction = TouchHandler.actions.None; hasMoved = false; }
// UPDATE CALLED BY TouchHandler public void Update() { // refresh _touch _touch = GetTouchById(fingerId); phase = _touch.phase; // add a new tap if (phase == TouchPhase.Began) { AddTap(); } // update properties currentPos = _touch.position; magnitude = _touch.deltaPosition.magnitude; speed = magnitude.PixelsToTouchUnits() / _touch.deltaTime; velocity = _touch.deltaPosition; // Moved > dragThreshold if (Vector2.Distance(startPos, currentPos) > TouchHandler._dragThreshold) { hasMoved = true; distanceTraveled += magnitude / TouchHandler._dragThreshold; } // start checking for timeout // if touch is done if (_touch.phase.IsDone()) { tapTimer += Time.deltaTime; if (tapTimer > TouchHandler._tapTimeout) { TouchHandler._touchCache.Remove(this); } } else { totalPressTime += Time.deltaTime; currentPressTime += Time.deltaTime; } // ACTIONS if (overrideAction != TouchHandler.actions.None) { action = overrideAction; } else { SetAction(); } }