// This logic starts coroutine on tap and if // heart hits wall within .2 seconds, amplify speed. /* void TouchTap(ref ArTouch touch){ * beenTapped = true; * StartCoroutine(TapTimer(TAP_TIMER)); * } * * void OnCollisionEnter(Collision collision){ * if (beenTapped) * { * Debug.Log("Nice Timing!"); * _heartRigidBody.velocity *= 1.5f; * } * }*/ // Following part sets tapped bool on collison // with wall. If tap is registered within .2 seconds // of collision, amplify speed. This feels better to me. void TouchTap(ref ArTouch touch) { if (GetComponent <DeathCondition>().chargeDefibrilator) { GetComponent <DeathCondition>().defibrilatorClicks += 1; GetComponent <tk2dAnimatedSprite>().ClipFps += 2; Debug.Log("Get Clicks: " + GetComponent <DeathCondition>().defibrilatorClicks); } else if ((canBeat) && (!munchMUNCH)) { Vector3 point = new Vector3( (Input.mousePosition.x - (Screen.width / 2)) / Screen.width, (Input.mousePosition.y - (Screen.height / 2)) / Screen.height, 0); Beat(point); rigidbody.velocity += beatSpeed; canBeat = false; if (canTap) { Debug.Log("Nice Timing!"); rigidbody.AddForce(beatSpeed.normalized * splatForce); StartCoroutine(BeatTimer(BEAT_TIMER)); } } }
/// <summary> /// Private constructor for singleton /// </summary> private ArTouchInput() { // TODO: (jonagill) Set initial capacity? _touches = new List <ArTouch>(); _mouseTouch = null; _recentTaps = new List <ArTouch>(); // Calculate squared distances for gesture detection float dpi = Screen.dpi; // Unity could not determine dpi if (dpi == 0) { dpi = DEFAULT_DPI; } _startMoveSqdist = (START_MOVE_DIST_IN * START_MOVE_DIST_IN * dpi * dpi); _registerMoveSqdist = (REGISTER_MOVE_DIST_IN * REGISTER_MOVE_DIST_IN * dpi * dpi); _doubleTapMaxSqdist = (DOUBLE_TAP_MAX_DIST_IN * DOUBLE_TAP_MAX_DIST_IN * dpi * dpi); // Add all existing touches as if they'd just been placed down if (Input.GetMouseButton(LEFT_MOUSE)) { ProcessTouchDown(ArTouch.MOUSE_ID, Input.mousePosition); } // NOTE: (jonagill) Input.touchCount appears to be zero here, but I'll // leave the code in for safety's sake foreach (Touch touch in Input.touches) { ProcessTouchDown(touch.fingerId, touch.position); } }
void TouchDown(ref ArTouch touch) { // We're not tracking any touches yet, so may as well track this one if (trackedTouchId == ArTouch.NULL_ID) { trackedTouchId = touch.id; } ArDebug.Log("Touch " + touch.id + " down at " + touch.position + "!"); }
void TouchUp(ref ArTouch touch) { // If this is the tracked touch, release that reference if (trackedTouchId == touch.id) { trackedTouchId = ArTouch.NULL_ID; } ArDebug.Log("Touch " + touch.id + " up at " + touch.position + "!"); }
// Draw some debug stuff /*void OnGUI() * { * GUI.Label(new Rect(50,50, 100, 100),""+ ArTouchInput.GetInstance().GetNumTouches()); * if (trackedTouchId != ArTouch.NULL_ID) * { * // Magic numbers are bad, kids. This is example code. * // Also, the camera's screen space (and thus the touch's) has the origin in the bottom-left, * // but the GUI has it in the top-left, so we've got to do a conversion on the y axis. * GUI.Box(new Rect(drawPosition.x - 25, Screen.height - (drawPosition.y+25), 50, 50), "Butts!"); * } * }*/ #region Touch Callbacks void TouchDown(ref ArTouch touch) { // We're not tracking any touches yet, so may as well track this one if (trackedTouchId == ArTouch.NULL_ID) { trackedTouchId = touch.id; } ArDebug.Log("Touch " + touch.id + " down at " + touch.position + "!"); }
void TouchMove(ref ArTouch touch) { // If this is the tracked touch, update using it if (trackedTouchId == touch.id) { drawPosition = touch.position; } ArDebug.Log("Touch " + touch.id + " moving at " + touch.position + "!"); }
void TouchMove(ref ArTouch touch) { // If this is the tracked touch, update using it if (trackedTouchId == touch.id) { drawPosition = touch.position; } ArDebug.Log("Touch " + touch.id + " moving at " + touch.position + "!"); }
// Following part sets tapped bool on collison // with wall. If tap is registered within .2 seconds // of collision, amplify speed. This feels better to me. void TouchTap(ref ArTouch touch) { if (!hasThrown && Input.touchCount > 0){ hasThrown = true; Vector3 point = new Vector3( (Input.mousePosition.x - (Screen.width/2)) / Screen.width, (Input.mousePosition.y - (Screen.height/2)) / Screen.height, 0); ThrowAt(point); Debug.Log(point); } }
// Following part sets tapped bool on collison // with wall. If tap is registered within .2 seconds // of collision, amplify speed. This feels better to me. void TouchTap(ref ArTouch touch) { if (!hasThrown && Input.touchCount > 0) { hasThrown = true; Vector3 point = new Vector3( (Input.mousePosition.x - (Screen.width / 2)) / Screen.width, (Input.mousePosition.y - (Screen.height / 2)) / Screen.height, 0); ThrowAt(point); Debug.Log(point); } }
/// <summary> /// Process just-placed touches and call triggered /// event delegates as part of Update. /// </summary> /// <param name='id'> /// The id of the relevant touch /// </param> /// <param name='position'> /// The position of the relevant touch /// </param> private void ProcessTouchDown(int id, Vector2 position) { // Update instead of add if this touch has already been added ArTouch checkTouch = GetTouch(id); if (checkTouch != null) { ArDebug.LogWarning("Tried to add touch " + id + " multiple times."); ProcessTouchUpdate(id, position); return; } // If the mouse is already held down, force it out // of touches, because Unity's going to overwrite // its data with this new touch anyway ArTouch mouse = GetTouch(ArTouch.MOUSE_ID); if (mouse != null) { // NOTE: (jonagill) Don't pass through Input.mousePosition, // because the new touch has overwritten the mouse data ProcessTouchUp(ArTouch.MOUSE_ID, mouse.position); } // Create and store new ArTouch from the Unity touch ArTouch newTouch = new ArTouch(id, position); if (id == ArTouch.MOUSE_ID) { _mouseTouch = newTouch; } else { _touches.Add(newTouch); } // Call any assigned functions for TouchDown events if (OnTouchDown != null && !newTouch.isDead) { OnTouchDown(ref newTouch); } }
void TouchDrag(ref ArTouch touch) { ArDebug.Log("Touch " + touch.id + " dragging at " + touch.position + "!"); }
void TouchPress(ref ArTouch touch) { ArDebug.Log("Touch " + touch.id + " pressing at " + touch.position + "!"); }
void TouchDrag(ref ArTouch touch) { ArDebug.Log("Touch " + touch.id + " dragging at " + touch.position + "!"); }
/// <summary> /// Process just-released touches and call triggered /// event delegates as part of Update. /// </summary> /// <param name='id'> /// The id of the relevant touch /// </param> /// <param name='position'> /// The position of the relevant touch /// </param> private void ProcessTouchUp(int id, Vector2 position) { ArTouch touch = GetTouch(id); // If this touch has not been added yet, // add it then remove it. // NOTE: (jonagill) Does adding and removing a touch in the same step // ever cause trouble? if (touch == null) { Debug.LogWarning("Tried to remove touch " + id + " before adding it."); ProcessTouchDown(id, position); touch = GetTouch(id); } // Store age for event processing float touchAge = touch.GetAge(); // If a touch hasn't moved and is young enough, throw any assigned tap events if (!touch.hasMoved && touchAge <= TAP_MAXTIME) { // If the touch is near enough to a recent tap's location, throw any // assigned double tap events // NOTE: (jonagill) To prevent this touch throwing a basic tap // event as well, set touch.isDead in the double callback function bool doubleTap = false; foreach (ArTouch oldTap in _recentTaps) { if ((oldTap.position - touch.position).sqrMagnitude <= _doubleTapMaxSqdist) { if (OnDoubleTap != null && !touch.isDead) { OnDoubleTap(ref touch); _recentTaps.Remove(oldTap); doubleTap = true; // Break because we don't want to trigger OnDoubleTap multiple times // if it's near multiple old taps break; } } } // Throw any assigned tap events if (OnTap != null && !touch.isDead) { OnTap(ref touch); } // Add this touch to the list of recent taps // if not used on a double tap (otherwise we enter // every tap on that spot will count as a double if (!doubleTap) { _recentTaps.Add(touch); } } // If a touch has moved and is young enough, throw any assigned flick events else if (touch.hasMoved && touchAge <= FLICK_MAXTIME) { if (OnFlick != null && !touch.isDead) { OnFlick(ref touch); } } // Call any assigned functions for generic TouchUp events if (OnTouchUp != null && !touch.isDead) { OnTouchUp(ref touch); } // Remove the touch from our references if (id == ArTouch.MOUSE_ID) { _mouseTouch = null; } else { _touches.Remove(touch); } }
/// <summary> /// Process just-placed touches and call triggered /// event delegates as part of Update. /// </summary> /// <param name='id'> /// The id of the relevant touch /// </param> /// <param name='position'> /// The position of the relevant touch /// </param> private void ProcessTouchDown(int id, Vector2 position) { // Update instead of add if this touch has already been added ArTouch checkTouch = GetTouch(id); if (checkTouch != null) { ArDebug.LogWarning("Tried to add touch " + id + " multiple times."); ProcessTouchUpdate(id, position); return; } // If the mouse is already held down, force it out // of touches, because Unity's going to overwrite // its data with this new touch anyway ArTouch mouse = GetTouch(ArTouch.MOUSE_ID); if (mouse != null) { // NOTE: (jonagill) Don't pass through Input.mousePosition, // because the new touch has overwritten the mouse data ProcessTouchUp(ArTouch.MOUSE_ID, mouse.position); } // Create and store new ArTouch from the Unity touch ArTouch newTouch = new ArTouch(id, position); if (id == ArTouch.MOUSE_ID) { _mouseTouch = newTouch; } else { _touches.Add(newTouch); } // Call any assigned functions for TouchDown events if (OnTouchDown != null && !newTouch.isDead) { OnTouchDown(ref newTouch); } }
/// <summary> /// Private constructor for singleton /// </summary> private ArTouchInput() { // TODO: (jonagill) Set initial capacity? _touches = new List<ArTouch>(); _mouseTouch = null; _recentTaps = new List<ArTouch>(); // Calculate squared distances for gesture detection float dpi = Screen.dpi; // Unity could not determine dpi if (dpi == 0) { dpi = DEFAULT_DPI; } _startMoveSqdist = (START_MOVE_DIST_IN * START_MOVE_DIST_IN * dpi * dpi); _registerMoveSqdist = (REGISTER_MOVE_DIST_IN * REGISTER_MOVE_DIST_IN * dpi * dpi); _doubleTapMaxSqdist = (DOUBLE_TAP_MAX_DIST_IN * DOUBLE_TAP_MAX_DIST_IN * dpi * dpi); // Add all existing touches as if they'd just been placed down if (Input.GetMouseButton(LEFT_MOUSE)) { ProcessTouchDown(ArTouch.MOUSE_ID, Input.mousePosition); } // NOTE: (jonagill) Input.touchCount appears to be zero here, but I'll // leave the code in for safety's sake foreach (Touch touch in Input.touches) { ProcessTouchDown(touch.fingerId, touch.position); } }
/// <summary> /// Process existing touches and call triggered /// event delegates as part of Update. /// </summary> /// <param name='id'> /// The id of the relevant touch /// </param> /// <param name='position'> /// The position of the relevant touch /// </param> private void ProcessTouchUpdate(int id, Vector2 position) { ArTouch touch = GetTouch(id); // If this touch has not been added yet, add it instead of updating if (touch == null) { Debug.LogWarning("Tried to update touch " + id + " before adding it."); ProcessTouchDown(id, position); return; } // Only update the touch's position if it has moved a significant distance Vector2 moveDelta = position - touch.position; bool moved = (moveDelta.sqrMagnitude >= _registerMoveSqdist); if (moved) { touch.deltaPosition = moveDelta; touch.position = position; } // If touch hasn't moved far enough to throw move events yet, check if it has now if (!touch.hasMoved) { if ((touch.position - touch.startPosition).sqrMagnitude >= _startMoveSqdist) { touch.hasMoved = true; } } // Store age for event checks float touchAge = touch.GetAge(); // If it still hasn't moved, see if it should throw any assigned press events if (!touch.hasMoved && touchAge > PRESS_MINTIME) { if (OnPress != null && !touch.isDead) { OnPress(ref touch); } } // If it has moved far enough to throw move events, process that else if (touch.hasMoved && moved) { // If we're not still waiting for flicks, throw any assigned drag events if (touchAge > DRAG_MINTIME) { if (OnDrag != null && !touch.isDead) { OnDrag(ref touch); } } // Throw any assigned generic touch move events if (OnTouchMove != null && !touch.isDead) { OnTouchMove(ref touch); } } }
void TouchDoubleTap(ref ArTouch touch) { touch.isDead = true; ArDebug.Log("Touch " + touch.id + " double tapped at " + touch.position + "!"); }
void TouchTap(ref ArTouch touch) { ArDebug.Log("Touch " + touch.id + " tapped at " + touch.position + "!"); }
// This logic starts coroutine on tap and if // heart hits wall within .2 seconds, amplify speed. /* void TouchTap(ref ArTouch touch){ beenTapped = true; StartCoroutine(TapTimer(TAP_TIMER)); } void OnCollisionEnter(Collision collision){ if (beenTapped) { Debug.Log("Nice Timing!"); _heartRigidBody.velocity *= 1.5f; } }*/ // Following part sets tapped bool on collison // with wall. If tap is registered within .2 seconds // of collision, amplify speed. This feels better to me. void TouchTap(ref ArTouch touch) { if (GetComponent<DeathCondition>().chargeDefibrilator) { GetComponent<DeathCondition>().defibrilatorClicks += 1; GetComponent<tk2dAnimatedSprite>().ClipFps += 2; Debug.Log("Get Clicks: " + GetComponent<DeathCondition>().defibrilatorClicks); } else if ((canBeat)&&(!munchMUNCH)) { Vector3 point = new Vector3( (Input.mousePosition.x - (Screen.width/2)) / Screen.width, (Input.mousePosition.y - (Screen.height/2)) / Screen.height, 0); Beat(point); rigidbody.velocity+=beatSpeed; canBeat = false; if (canTap) { Debug.Log("Nice Timing!"); rigidbody.AddForce(beatSpeed.normalized * splatForce); StartCoroutine(BeatTimer(BEAT_TIMER)); } } }
/// <summary> /// Process just-released touches and call triggered /// event delegates as part of Update. /// </summary> /// <param name='id'> /// The id of the relevant touch /// </param> /// <param name='position'> /// The position of the relevant touch /// </param> private void ProcessTouchUp(int id, Vector2 position) { ArTouch touch = GetTouch(id); // If this touch has not been added yet, // add it then remove it. // NOTE: (jonagill) Does adding and removing a touch in the same step // ever cause trouble? if (touch == null) { Debug.LogWarning("Tried to remove touch " + id + " before adding it."); ProcessTouchDown(id, position); touch = GetTouch(id); } // Store age for event processing float touchAge = touch.GetAge(); // If a touch hasn't moved and is young enough, throw any assigned tap events if (!touch.hasMoved && touchAge <= TAP_MAXTIME) { // If the touch is near enough to a recent tap's location, throw any // assigned double tap events // NOTE: (jonagill) To prevent this touch throwing a basic tap // event as well, set touch.isDead in the double callback function bool doubleTap = false; foreach (ArTouch oldTap in _recentTaps) { if ((oldTap.position - touch.position).sqrMagnitude <= _doubleTapMaxSqdist) { if (OnDoubleTap != null && !touch.isDead) { OnDoubleTap(ref touch); _recentTaps.Remove(oldTap); doubleTap = true; // Break because we don't want to trigger OnDoubleTap multiple times // if it's near multiple old taps break; } } } // Throw any assigned tap events if (OnTap != null && !touch.isDead) { OnTap(ref touch); } // Add this touch to the list of recent taps // if not used on a double tap (otherwise we enter // every tap on that spot will count as a double if (!doubleTap) { _recentTaps.Add(touch); } } // If a touch has moved and is young enough, throw any assigned flick events else if (touch.hasMoved && touchAge <= FLICK_MAXTIME) { if (OnFlick != null && !touch.isDead) { OnFlick(ref touch); } } // Call any assigned functions for generic TouchUp events if (OnTouchUp != null && !touch.isDead) { OnTouchUp(ref touch); } // Remove the touch from our references if (id == ArTouch.MOUSE_ID) { _mouseTouch = null; } else { _touches.Remove(touch); } }
void TouchFlick(ref ArTouch touch) { ArDebug.Log("Touch " + touch.id + " flicked at " + touch.position + "!"); }
void TouchTap(ref ArTouch touch) { ArDebug.Log("Touch " + touch.id + " tapped at " + touch.position + "!"); }
void TouchPress(ref ArTouch touch) { ArDebug.Log("Touch " + touch.id + " pressing at " + touch.position + "!"); }
void TouchDoubleTap(ref ArTouch touch) { touch.isDead = true; ArDebug.Log("Touch " + touch.id + " double tapped at " + touch.position + "!"); }
void TouchUp(ref ArTouch touch) { // If this is the tracked touch, release that reference if (trackedTouchId == touch.id) { trackedTouchId = ArTouch.NULL_ID; } ArDebug.Log("Touch " + touch.id + " up at " + touch.position + "!"); }
void TouchFlick(ref ArTouch touch) { ArDebug.Log("Touch " + touch.id + " flicked at " + touch.position + "!"); }