Author Topic: How to cancel touch when user drags finger away from button?  (Read 4027 times)

ronronmx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
How to cancel touch when user drags finger away from button?
« 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

PoN

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 4
  • Posts: 111
    • View Profile
Re: How to cancel touch when user drags finger away from button?
« Reply #1 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
Worked on Doc&DogAge Of Fury 3D. Actually working on WarMach.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to cancel touch when user drags finger away from button?
« Reply #2 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.

ronronmx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: How to cancel touch when user drags finger away from button?
« Reply #3 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 :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to cancel touch when user drags finger away from button?
« Reply #4 on: October 12, 2012, 03:51:50 AM »
No, you're not missing anything. :)

ronronmx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: How to cancel touch when user drags finger away from button?
« Reply #5 on: October 12, 2012, 11:28:33 PM »
Cool, thanks for the help guys!

ronronmx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: How to cancel touch when user drags finger away from button?
« Reply #6 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.  

  • First, is it the right way of doing it, or am I doing unnecessary stuff?
  • Second, in the last part of the code within "if( hitCollider == accelBtn.collider )", I have to set currentTouch.pressed = accelBtn manually because if I don't, when I drag onto the accelBtn and release the mouse button/touch, it doesn't send the "OnPress(false)" message, and my flag stays true. Am I doing this wrong, like maybe I'm not using a SendMessage() where/when I should?

Thanks for your time!
Stephane

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to cancel touch when user drags finger away from button?
« Reply #7 on: October 14, 2012, 02:39:21 PM »
That's perfectly fine.

ronronmx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: How to cancel touch when user drags finger away from button?
« Reply #8 on: October 14, 2012, 04:46:16 PM »
Cool, thx