Author Topic: Suggested new event addition (and code for those interested)  (Read 6496 times)

mixd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Suggested new event addition (and code for those interested)
« on: November 29, 2012, 02:29:11 PM »
I found myself needing to know when a click was canceled in order to update the UI.  This is important in some situations so that the user knows what will happen when they lift their finger.
The new events are called "OnClickDisabled" and "OnClickEnabled". It has been tested on both regular as well as draggable buttons.

I purposely made as few changes to the code as possible. If this makes it into a release version there is some consolidation that can be done to keep it as elegant as the existing code.

Anyway here is the addition:

in UICamera.cs

1. add this as a private variable
  1. bool _clickFlagEnabled = true;

2. insert this after the existing code "Notify(currentTouch.pressed, "OnDrag", currentTouch.delta);"
  1. if (_clickFlagEnabled && (isDisabled || currentTouch.pressed != currentTouch.current))
  2. {
  3.         Notify(currentTouch.pressed, "OnClickDisabled", currentTouch.delta);
  4.         _clickFlagEnabled = false;
  5. }
  6. else if(!_clickFlagEnabled && (!isDisabled && currentTouch.pressed == currentTouch.current))
  7. {
  8.         Notify(currentTouch.pressed, "OnClickEnabled", currentTouch.delta);
  9.         _clickFlagEnabled = true;
  10. }

3. insert this after the existing code "currentTouch.pressed = null;"
  1. _clickFlagEnabled = true;
« Last Edit: November 29, 2012, 10:37:19 PM by mixd »

mixd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Suggested new event addition (and code for those interested)
« Reply #1 on: November 29, 2012, 02:34:33 PM »
example for any uiwidget/monobehavior with a collider:

void OnClickDisabled() {
   Debug.Log("At exactly this moment, the click has been disabled! Lift your finger and no click event will be sent. Use this event to update the UI accordingly.");
}
   
void OnClickEnabled() {
   Debug.Log("At exactly this moment, the click has been enabled! Lift your finger and the click will go through. Use this event to update the UI accordingly.");
}

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Suggested new event addition (and code for those interested)
« Reply #2 on: November 29, 2012, 04:31:17 PM »
What's wrong with void OnPressed(bool pressed) ?

Oh oh, wait wait, let me just read a little more. Is this made so that you can "cancel click" because of a button in a scrollview when you've dragged? Because then I'm totally game for that. I've been needing that for a while, but never really tried to fix it. :)

mixd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Suggested new event addition (and code for those interested)
« Reply #3 on: November 29, 2012, 05:26:00 PM »
Not exactly just for that use case, however it does solve that problem... Here is a simple example:

In the existing NGUI:
1. Click down on a button but do not release the click
2. The button updates to show the user that they clicked on it correctly
3. Without releasing your finger, slide your finger/mouse off the button.
4. Nothing changes.

On a touch screen this is particularly bad, because the user can't see the position of their finger. The user should always know what will happen when they lift their finger, because they may want to "cancel" the click by sliding off, or may become frustrated that their click didn't work after accidentally sliding off a button before lifting their finger.

The OnClickDisabled event allows me to make the button appear in a "deselected" state BEFORE the user lifts their finger. It seems like a subtle edge-case, however it makes a world of difference in how a UI feels. You'll see this behavior in almost all professional UIs, such as windows, mac, iOS.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Suggested new event addition (and code for those interested)
« Reply #4 on: November 30, 2012, 04:18:29 AM »
Ah true I remember doing this for bullet time when I had to do GUITextures. I seem to remember there's something you can read from UICamera, if you want to do this. ArenMook posted a response about this some time ago, but I can't find it.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Suggested new event addition (and code for those interested)
« Reply #5 on: November 30, 2012, 07:14:52 AM »
Part of 2.2.7:
- NEW: Added UICamera.stickyPress option that makes it possible for multiple objects to receive OnPress notifications from a single touch.

Note: it's not the same code as what you posted. I came up with a more integrated solution.
« Last Edit: November 30, 2012, 07:18:09 AM by ArenMook »

mixd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Suggested new event addition (and code for those interested)
« Reply #6 on: January 28, 2013, 09:49:05 PM »
Finally updated to the new version. I get what you did, but it does not really work for a number of reasons:

1. OnPress should never be triggered unless the user actually pressed on something (treating OnPress differently breaks existing UI)
2. Breaks all scrolling lists, try scrolling a list, then running into an outside button - the list abruptly stops scrolling, or vice versa if dragging from an outer button onto a scrolling list
3. Does not solve the functionality of simply knowing when a click is canceled without writing lots of special-case code for an event that should be built in.
4. Does not solve the functionality of knowing when the user has "touched" an object on mobile devices (i.e. sweeping around their finger to collect gold coins) currently this only works if they first pressed down on something, not just arbitrarily moving their finger. Again, seems like pretty basic functionality.

Simply speaking, something like "OnHover" should also work on mobile devices and would solve all of these problems. Not sure why you don't allow hover events on mobile. Sure, there is no mouse pointer, but you could simply treat the users finger as the same thing as the mouse pointer.

Overall NGUI is a great library, and the code is good enough that it's easy to splice these things in there, just wish it was more based around UI standards comparable to UIKit or something. I like your approach of trying to make solutions as integrated as possible, however in this case it didn't really work. If I'm somehow not understanding something, it would be great to know how I am supposed to accomplish the above functionality without editing the library or writing lots of outside code.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Suggested new event addition (and code for those interested)
« Reply #7 on: January 28, 2013, 11:15:48 PM »
When you are receiving OnDrag events, simply check what's underneath the touch -- UICamera.currentTouch.current, then do something with it. You can have this OnDrag handler in a script attached to a game object specified as UICamera.genericEventHandler.

mixd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Suggested new event addition (and code for those interested)
« Reply #8 on: January 29, 2013, 01:54:30 AM »
Thanks, that did the trick. Am I understanding correctly that the number of messages being sent by NGUI is now doubled if the event also has a non-generic receiver? Not sure if this really matters though.