Author Topic: How to handle each touch separately?  (Read 3630 times)

ronronmx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
How to handle each touch separately?
« on: October 14, 2012, 07:39:59 PM »
Hey all,
so I've got my "Update Touch On Drag Past Threshold" working (if finger drags away from button, cancel touch. If finger drags onto another button, update touch...).
But, when testing on the device, I realized that when I place a second finger on the screen, it cancels/updates my first finger touch. What I need to do is handle each touch separately, independent of each other, so that touch 1 doesn't care about touch 2 and vice-versa.

Here's my current code below, and if anyone could point me in the right direction, it would be a huge help :)

  1. public void AccelDrag( GameObject go, Vector2 delta )
  2.         {
  3.                 // Check if user dragged his finger passed the treshold,
  4.                 // and update which UI Element was dragged on
  5.                 HandleTouchTreshold( accelBtn );
  6.                
  7.                 // Turn accel on if we dragged onto it
  8.                 if( UICamera.lastHit.collider != null && !accelOn )
  9.                 {
  10.                         // Send a message instead of "accelOn = true" to re-play button OnPress animations/events
  11.                         accelBtn.SendMessage("OnPress", true, SendMessageOptions.DontRequireReceiver);
  12.                 }
  13.         }
  14.  
  15. public void BrakeDrag( GameObject go, Vector2 delta )
  16.         {
  17.                 // Check if user dragged his finger passed the treshold,
  18.                 // and update which UI Element was dragged on
  19.                 HandleTouchTreshold( brakeBtn, UICamera.currentTouchID );
  20.                
  21.                 // Turn brake on if we dragged onto it
  22.                 if( UICamera.lastHit.collider != null )
  23.                 {
  24.                         // Send a message instead of "brakeOn = true" to re-play button OnPress animations/events
  25.                         brakeBtn.SendMessage("OnPress", true, SendMessageOptions.DontRequireReceiver);
  26.                 }
  27.         }
  28.  
  29. // =================== Function to handle touch treshold ================= //
  30. public void HandleTouchTreshold( GameObject lastGameObject )
  31.         {
  32.                 // Store the last hit gameobject.
  33.                 // If it's null, send OnPress(false) to the lastGameObject, and stop execution.
  34.                 Collider hitCollider = UICamera.lastHit.collider;
  35.                 if( !hitCollider )
  36.                 {
  37. //                      Debug.LogWarning("DRAGGED PAST TRESHOLD");
  38.                         lastGameObject.SendMessage( "OnPress", false, SendMessageOptions.DontRequireReceiver );
  39.                         return;
  40.                 }
  41.                
  42.                 // Detect which UI element we dragged onto
  43.                 GameObject activeGameObject = null;
  44.                 foreach( GameObject go in availableUIElements )
  45.                 {
  46.                         if( hitCollider == go.collider ) activeGameObject = go;
  47.                         else go.SendMessage( "OnPress", false, SendMessageOptions.DontRequireReceiver );
  48.                 }
  49.                
  50.                 // Now that we know the last hit object AND the active object( the one we dragged onto )
  51.                 // we can switch focus to the new object, and activate it.
  52.                 if( hitCollider != lastGameObject.collider )
  53.                 {
  54.                         Debug.LogWarning("DRAGGED ONTO NEW OBJECT: " + UICamera.currentTouch.current);
  55.                        
  56.                         if( hitCollider == activeGameObject.collider )
  57.                         {
  58.                                 UICamera.currentTouch.pressed = activeGameObject;
  59.                                 activeGameObject.SendMessage( "OnPress", true, SendMessageOptions.DontRequireReceiver );
  60.                                 Debug.Log("SENDING ONPRESS TO: " + activeGameObject.name);
  61.                         }
  62.                         else activeGameObject.SendMessage( "OnPress", false, SendMessageOptions.DontRequireReceiver );
  63.                 }
  64.                
  65.         }

Thanks in advance for any help you guys can provide!

Stephane
« Last Edit: October 14, 2012, 07:41:35 PM by ronronmx »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to handle each touch separately?
« Reply #1 on: October 15, 2012, 06:33:33 AM »
UICamera.currentTouchID tells you the ID of the touch so you can differentiate between different touches.

ronronmx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: How to handle each touch separately?
« Reply #2 on: October 15, 2012, 12:53:38 PM »
Hey Aren,
yes I know about .currentTouchID, but it doesn't help me in this case( and that might very well be because I'm not experienced enough to figure out how to use it in my particular case ), my problem was that I was using UICamera.lastHit.collider globally instead of per object touched, so whenever I put a second finger on another object, UICamera.lastHit.collider would change and the first touch was getting canceled.

Now what I'm doing is checking UICamera.lastHit.collider only for the object being dragged on, against the object being dragged on, for each object being dragged on separately. It's working great except I have duplicate code for each object I'm checking, in their Drag() function. So I just need to make 1 function work for every object and get rid of the duplicated code.

Thanks for the quick reply,
Stephane