Author Topic: Dragging buttons without clicking them  (Read 10727 times)

tranvv

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Dragging buttons without clicking them
« on: April 20, 2012, 05:42:32 PM »
NGUI is amazing.

I've got a draggable menu of buttons using the UIDrag Camera implementation. The problem I am having is when I drag the buttons and the cursor ( or finger on ios ) ends on a button it presses it. How would I prevent this from happening so if a drag ( maybe within a threshold? ) operation is occurring we don't click the button?

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dragging buttons without clicking them
« Reply #1 on: April 20, 2012, 06:18:35 PM »
Are you using iOS remote perchance? UICamera has a drag threshold parameter, and if you exceed it, it cancels the drag -- so you shouldn't be getting clicks. Btw, you are using the latest version of NGUI (2.0.3c), right?

tranvv

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Dragging buttons without clicking them
« Reply #2 on: April 23, 2012, 11:10:28 AM »
Let me get more detailed.

I have a UIGrid of buttons in a horizontal row where only a few buttons are on screen at any time. The UIGrid has a UIDrag Camera on it. When the user drags in the UIGrid it moves the camera across the row of buttons making it look like the user is dragging the buttons left and right letting him scroll the menu. In the editor, if I am dragging the UIGrid and release the drag with the mouse cursor on a button it ends up clicking the button ( the scroll works fine ). I'm trying to prevent that click at the end of a drag operation.

I'm not sure what iOS remote is. We are using at least version 2.0.0, I would have to check to see if its 2.0.3c or not.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dragging buttons without clicking them
« Reply #3 on: April 23, 2012, 01:33:29 PM »
Hmm, no reason for it to send out a click then, unless you set the threshold to be something high on UICamera itself. The way the code works is once you drag something far enough on the screen (past UICamera's mouseClickThreshold or touchClickThreshold, depending on whether you use a mouse or touch), the click event gets cancelled and will not be sent out.

The only place where this breaks is when using the iOS remote, because it sends out both touch and moue events at the same time, but it's fixed by selecting the UICamera and turning off mouse events.

tranvv

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Dragging buttons without clicking them
« Reply #4 on: April 23, 2012, 01:51:31 PM »
it looks like this only happens when I click in the area of a button, drag, and finish the drag still in the area of the button. Mouse click threshold is 10, use mouse is false, I'm in the editor on PC.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dragging buttons without clicking them
« Reply #5 on: April 23, 2012, 01:53:38 PM »
Check line 844 of UICamera, it should read:

  1.                         else if (currentTouch.clickNotification == ClickNotification.BasedOnDelta)
  2.                         {
  3.                                 // If the notification is based on delta and the delta gets exceeded, disable the notification
  4.                                 float threshold = (currentTouch == mMouse[0]) ? mouseClickThreshold : Mathf.Max(touchClickThreshold, Screen.height * 0.1f);
  5.  
  6.                                 if (currentTouch.totalDelta.magnitude > threshold)
  7.                                 {
  8.                                         currentTouch.clickNotification = ClickNotification.None;
  9.                                 }
  10.                         }

If it doesn't, talk to your "merge guy".

GavinF

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Dragging buttons without clicking them
« Reply #6 on: February 20, 2013, 05:53:23 PM »
Sorry to reopen an old thread, but I am having a very similar problem using version 2.2.3.  I have a UIDraggable Panel with a UIGrid filled with GameObjects that have UIDragPanelContents and UIButton components.  I have my UICamera set to use a drag threshold of 40 and a click threshold of 5 for both mouse and touch.  I have tested both in the editor and on an iOS device with either Use Mouse or Use Touch unchecked and am still getting clicks when I drag my panel contents. 

In looking at UICamera, it looks like no matter what you set the thresholds to, they are ignored if you end your click event on the same object that started it, and since I am moving the panel/button that starts the event with my mouse or finger, it is always registering a click.  What do you suggest for working around this?   I try to avoid making custom changes to NGUI source files if I can help it since we do try to stay somewhat current.  Any advice appreciated.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dragging buttons without clicking them
« Reply #7 on: February 20, 2013, 08:22:44 PM »
2.2.3 is quite old. You need to update.

GavinF

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Dragging buttons without clicking them
« Reply #8 on: February 21, 2013, 06:19:06 PM »
I spent the day upgrading to latest NGUI. Same result.  Any useful advice?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dragging buttons without clicking them
« Reply #9 on: February 21, 2013, 10:25:54 PM »
In OnDrag, cancel the click event:
  1. UICamera.currentTouch.clickNotification = UICamera.ClickNotification.None;

GavinF

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Dragging buttons without clicking them
« Reply #10 on: February 22, 2013, 05:33:12 PM »
The clickNotification was already set to None by way of the BasedOnDelta check in UICamera.ProcessTouch(). 

I didn't provide enough information in my original post.  My problem ended up being we've been doing all of our button logic based on onPress events (playing a UI Sound on pressed, and doing button clicked logic on the !pressed). 

After looking over the UICamera code I realized that we had been completely ignoring the onClick events.  Up until now, it hadn't been an issue since we were just using simple buttons that were not draggable.  Since onPress is always sent regardless of drag thresholds (as it should be), our buttons were always behaving as though they'd been clicked on when we released from a drag.  I found two ways around my problem:

1.  Add onClick UIEventListeners to my buttons, and move the 'clicked' logic out of the onPress handlers (which in retrospect looks like the correct way, and is how we should have set up buttons in the first place)

or

2. In my onPress code, check to make sure UICamera.currentTouch.clickNotification != UICamera.ClickNotifcation.None before doing my select logic (which is a bit more hacky, but perhaps more convenient in certain situations)

Anyway, just wanted to share my solution in case anyone else gets confused by this.
« Last Edit: February 22, 2013, 07:31:26 PM by GavinF »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dragging buttons without clicking them
« Reply #11 on: February 23, 2013, 02:16:07 AM »
Yeah, you should really use OnClick.