public static void RemoveEndedTouchContacts() { for (int i = 0; i < touchContacts.Count; ++i) { TouchContact tc = touchContacts[i]; if (tc.phase == TouchPhase.Ended) { touchContacts.Remove(tc); --i; continue; } } }
public static TouchContact GetTouchContactByFingerId(int fingerId, TouchContact[] touches) { TouchContact found = null; for (int i = 0; i < touches.Length; ++i) { if (fingerId == touches[i].fingerId) { found = touches[i]; break; } } return(found); }
} // end of Touched() public bool Touched(TouchContact touch, Vector2 adjustedTouchPos, bool confirm) { bool result = false; if (Contains(adjustedTouchPos)) { if (touch.phase == TouchPhase.Began) { // touch down - store object touched touch.TouchedObject = this; } else if (touch.phase == TouchPhase.Ended) { if (confirm) { AABB2D storedHitBox = touch.TouchedObject as AABB2D; if (storedHitBox != null) { if (storedHitBox == touch.TouchedObject) { // touch up/released on the same object result = true; touch.TouchedObject = null; } } } else { result = true; touch.TouchedObject = null; } } } return(result); } // end of Touched()
//static private bool skipShow = false; public static void Update() { //if (BokuGame.bokuGame.IsActive) { #if NETFX_CORE Touch[] touchesThisFrame = AltGetTouchContacts(); #else //---------------------------------------------------------------------------------- // This is a stub until Unity support is added. Unity doesn't need its update called //---------------------------------------------------------------------------------- Input.Update(); //---------------------------------------------------------------------------------- // Please remove the above hack when Unity is integrated //---------------------------------------------------------------------------------- Touch[] touchesThisFrame = Input.touches; //From touchesThisFrame list in UnityTouchEmulation. #endif // Sort the array of Touch objects on the fingerId Array.Sort(touchesThisFrame, delegate(Touch touchZero, Touch touchOne) { return(touchZero.fingerId.CompareTo(touchOne.fingerId)); }); // Initially set it to nothing is touching the screen isTouched = false; wasMoved = false; wasTouched = false; if (touchesThisFrame.Length <= 0) { wasMultiTouch = false; } else if (touchesThisFrame.Length > 1) { //a single frame of multi touch means multi touch was detected, don't reset until no data wasMultiTouch = true; } // Assume we just released until proven false below wasReleased = true; // If there are a different number of touches than touch contacts, we either added // or deleted some contacts. int touchContactsLength = touchContacts.Count; int touchesThisFrameLength = touchesThisFrame.Length; if (touchContacts.Count != touchesThisFrame.Length) { //RemoveEndedTouchContacts(); // A touch was added, or multiple touches if (touchesThisFrame.Length > touchContacts.Count) { wasTouched = true; for (int i = 0; i < touchesThisFrame.Length; ++i) { Touch t = touchesThisFrame[i]; TouchContact contact = GetTouchContactByFingerId(t.fingerId); if (contact == null) { contact = new TouchContact(); contact.fingerId = t.fingerId; touchContacts.Add(contact); } } } else // A touch was deleted, or multiple touches { List <TouchContact> temp = new List <TouchContact>(); for (int i = 0; i < touchesThisFrame.Length; ++i) { Touch t = touchesThisFrame[i]; TouchContact contact = GetTouchContactByFingerId(t.fingerId); if (contact != null) { //search for existing touch with this id TouchContact existT = temp.Find(delegate(TouchContact p) { return(p.fingerId == contact.fingerId); }); if (existT == null) { temp.Add(contact); } } } touchContacts = temp; } } // Now that the number of TouchContact's is correct, lets sort them on the fingerId touchContacts.Sort(delegate(TouchContact contactZero, TouchContact contactOne) { return(contactZero.fingerId.CompareTo(contactOne.fingerId)); }); //string message = ""; if (touchContacts.Count != touchesThisFrame.Length) { /* * Debug.Assert((touchContacts.Count == touchesThisFrame.Length), message + * "old:\n" + oldTouchesThisFrameString + oldTouchContactsString + * "new:\n" + newTouchesThisFrameString + newTouchContactsString); */ #if !NETFX_CORE Input.ClearEvents(); #endif touchContacts.Clear(); wasReleased = false; return; } // Perform a deep copy of each touch object for (int i = 0; i < touchesThisFrame.Length; ++i) { Touch touch = touchesThisFrame[i]; TouchContact touchContact = touchContacts[i]; switch (touch.phase) { case TouchPhase.Began: //Console.WriteLine("New TouchContact"); touchContact.phase = TouchPhase.Began; touchContact.startPosition = touch.position; touchContact.previousPosition = touch.position; touchContact.startTime = Time.WallClockTotalSeconds; touchContact.elapsedTime = Time.WallClockFrameSeconds; break; case TouchPhase.Moved: case TouchPhase.Stationary: // JW - NOTE: Even when the Unity layer reports a touch as stationary once the // delta position is unchanged for one frame, that is not a very accurate measure. // We won't set TouchContact objects to be stationary until we've detected no // delta position change for STATIONARY_TIME seconds. if (touch.deltaPosition == Vector2.Zero) { if ((Time.WallClockTotalSeconds - touchContact.lastMoveTime) > STATIONARY_TIME) { touchContact.phase = TouchPhase.Stationary; } } else { touchContact.lastMoveTime = Time.WallClockTotalSeconds; touchContact.phase = TouchPhase.Moved; } touchContact.previousPosition = touchContact.position; touchContact.elapsedTime = Time.WallClockFrameSeconds; wasMoved = true; break; case TouchPhase.Ended: //Console.WriteLine("TouchContact has ended"); touchContact.previousPosition = touchContact.position; touchContact.phase = TouchPhase.Ended; touchContact.elapsedTime = Time.WallClockFrameSeconds; break; default: break; } if (touchContact.phase != TouchPhase.Ended) { isTouched = true; wasReleased = false; } touchContact.position = touch.position; touchContact.deltaPosition = touch.deltaPosition; touchContact.fingerId = touch.fingerId; //touchContact.tapCount = touch.tapCount; touchContact.deltaTime = touch.deltaTime; if (touchContact.elapsedTime > 0.0f) { touchContact.Velocity = (touchContact.position - touchContact.previousPosition) / touchContact.elapsedTime; } else { touchContact.Velocity = Vector2.Zero; } } // If we have no touch objects, then we are at least one frame after the last touch ended // so we are not released. if (touchContacts.Count == 0) { wasReleased = false; } } TouchGestureManager.Get().Update(); }
} // end of AABB2D Intersect() /// <summary> /// Encapsulates a commonly used pattern when comparing a touch /// to a bounding box. On press, the box is set as the touchedObject. /// On release, if this box is stil the touchedObject then this function /// returns true. /// </summary> /// <param name="touch">the touch potentially contacting this object</param> /// <param name="adjustedTouchPos">camera relative adjusted position of the touch</param> /// <returns></returns> public bool Touched(TouchContact touch, Vector2 adjustedTouchPos) { return(Touched(touch, adjustedTouchPos, true)); } // end of Touched()