Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: ronronmx on October 11, 2012, 01:33:33 AM

Title: How to cancel touch when user drags finger away from button?
Post by: ronronmx on October 11, 2012, 01:33:33 AM
Hi there, quick question:

Is there a built-in way to cancel a touch when the user drags his finger past a certain threshold, away from the button which was pressed? For example, in my HUD controls, I have 2 buttons next to each other, 1 for going left <-- and 1 for going right -->. Let's say I start the touch on the left arrow button, when I drag my finger onto the right arrow button, I want to cancel the left arrow button touch and activate the right arrow button touch, without having to lift my finger.

Before I started using NGUI, I was achieving that by doing:

  1. if( leftArrowRect.Contains( myTouch ) ) DoSomething();
  2. else if( rightArrowRect.Contains( myTouch ) DoSomethingElse();
  3.  

which would automatically cancel 1 button press for the other.

Thanks for your time!
Stephane
Title: Re: How to cancel touch when user drags finger away from button?
Post by: PoN on October 11, 2012, 03:51:01 AM
Create collider on touch position and drag his when you are dragging finger , and then if this collider will collide with leftArrowRect or RightArrowRect DoSomething
Title: Re: How to cancel touch when user drags finger away from button?
Post by: ArenMook on October 11, 2012, 04:20:15 AM
UICamera.currentTouch gives you everything you need here. If you want to cancel a press event, set UICamera.currentTouch.pressed to null, for example. You may also want to send out OnPress(false) via SendMessage first though.
Title: Re: How to cancel touch when user drags finger away from button?
Post by: ronronmx on October 12, 2012, 01:29:33 AM
PoN: Interesting approach, never thought of that before!

Aren: So no, there isn't a built-in way to do this, I have to write my own logic, eg; if( currentTouch.pos > myButton.pos + threshold ) SendMessage("OnPress", false); I just want to make sure I'm not missing something that's already implemented :)
Title: Re: How to cancel touch when user drags finger away from button?
Post by: ArenMook on October 12, 2012, 03:51:50 AM
No, you're not missing anything. :)
Title: Re: How to cancel touch when user drags finger away from button?
Post by: ronronmx on October 12, 2012, 11:28:33 PM
Cool, thanks for the help guys!
Title: Re: How to cancel touch when user drags finger away from button?
Post by: ronronmx on October 14, 2012, 12:22:33 PM
Aren, I'm gonna bug you one more time if you don't mind.
Here's what I'm doing to cancel a touch on a button and activate another one, while dragging( without releasing the mouse button/screen):

  1. Collider hitCollider = UICamera.lastHit.collider;
  2. if( hitCollider != preloadBtn.collider )
  3. {
  4.         UICamera.currentTouch.pressed = null;
  5.         Debug.LogWarning("DRAGGED PAST TRESHOLD\n"+UICamera.currentTouch.current);
  6.        
  7.         if( hitCollider == accelBtn.collider )
  8.         {
  9.                 UICamera.currentTouch.pressed = accelBtn;
  10.                 accelBtn.SendMessage( "OnPress", true, SendMessageOptions.DontRequireReceiver );
  11.         }
  12.                        
  13. }
  14.  


Thanks for your time!
Stephane
Title: Re: How to cancel touch when user drags finger away from button?
Post by: ArenMook on October 14, 2012, 02:39:21 PM
That's perfectly fine.
Title: Re: How to cancel touch when user drags finger away from button?
Post by: ronronmx on October 14, 2012, 04:46:16 PM
Cool, thx