Author Topic: UIButton Keys not respecting disabled buttons?  (Read 2609 times)

brisck1

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
UIButton Keys not respecting disabled buttons?
« on: January 03, 2014, 01:43:05 PM »
So I've been working on setting up my UI so that it can be navigated via keyboard and a controller, but a problem I seem to be having is that the UIButton Keys doesn't seem to respect if a button is disabled or not. Firstly it will continue to cycle through the disabled items (although it is not visible), but worst of all it still allows the user to select the invisibly selected item when pressing enter or a controller button.

Everything works fine for the mouse, so I'm not sure what's going on with the keyboard controls. At the moment i'm using the input "OnClick" to activate a button's function, perhaps this is not the best way for a mouse + Keyboard + controller combo?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton Keys not respecting disabled buttons?
« Reply #1 on: January 04, 2014, 08:48:41 AM »
You're right. I've made the changes locally. Here's the modified script:
  1. //----------------------------------------------
  2. //            NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2014 Tasharen Entertainment
  4. //----------------------------------------------
  5.  
  6. using UnityEngine;
  7.  
  8. /// <summary>
  9. /// Attaching this script to a widget makes it react to key events such as tab, up, down, etc.
  10. /// </summary>
  11.  
  12. [RequireComponent(typeof(Collider))]
  13. [AddComponentMenu("NGUI/Interaction/Button Keys")]
  14. public class UIButtonKeys : MonoBehaviour
  15. {
  16.         public bool startsSelected = false;
  17.         public UIButtonKeys selectOnClick;
  18.         public UIButtonKeys selectOnUp;
  19.         public UIButtonKeys selectOnDown;
  20.         public UIButtonKeys selectOnLeft;
  21.         public UIButtonKeys selectOnRight;
  22.  
  23.         void OnEnable ()
  24.         {
  25.                 if (startsSelected)
  26.                 {
  27.                         if (UICamera.selectedObject == null || !NGUITools.GetActive(UICamera.selectedObject))
  28.                         {
  29.                                 UICamera.currentScheme = UICamera.ControlScheme.Controller;
  30.                                 UICamera.selectedObject = gameObject;
  31.                         }
  32.                 }
  33.         }
  34.  
  35.         void OnKey (KeyCode key)
  36.         {
  37.                 if (NGUITools.GetActive(this))
  38.                 {
  39.                         switch (key)
  40.                         {
  41.                         case KeyCode.LeftArrow:
  42.                                 if (NGUITools.GetActive(selectOnLeft)) UICamera.selectedObject = selectOnLeft.gameObject;
  43.                                 break;
  44.                         case KeyCode.RightArrow:
  45.                                 if (NGUITools.GetActive(selectOnRight)) UICamera.selectedObject = selectOnRight.gameObject;
  46.                                 break;
  47.                         case KeyCode.UpArrow:
  48.                                 if (NGUITools.GetActive(selectOnUp)) UICamera.selectedObject = selectOnUp.gameObject;
  49.                                 break;
  50.                         case KeyCode.DownArrow:
  51.                                 if (NGUITools.GetActive(selectOnDown)) UICamera.selectedObject = selectOnDown.gameObject;
  52.                                 break;
  53.                         case KeyCode.Tab:
  54.                                 if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
  55.                                 {
  56.                                         if (NGUITools.GetActive(selectOnLeft)) UICamera.selectedObject = selectOnLeft.gameObject;
  57.                                         else if (NGUITools.GetActive(selectOnUp)) UICamera.selectedObject = selectOnUp.gameObject;
  58.                                         else if (NGUITools.GetActive(selectOnDown)) UICamera.selectedObject = selectOnDown.gameObject;
  59.                                         else if (NGUITools.GetActive(selectOnRight)) UICamera.selectedObject = selectOnRight.gameObject;
  60.                                 }
  61.                                 else
  62.                                 {
  63.                                         if (NGUITools.GetActive(selectOnRight)) UICamera.selectedObject = selectOnRight.gameObject;
  64.                                         else if (NGUITools.GetActive(selectOnDown)) UICamera.selectedObject = selectOnDown.gameObject;
  65.                                         else if (NGUITools.GetActive(selectOnUp)) UICamera.selectedObject = selectOnUp.gameObject;
  66.                                         else if (NGUITools.GetActive(selectOnLeft)) UICamera.selectedObject = selectOnLeft.gameObject;
  67.                                 }
  68.                                 break;
  69.                         }
  70.                 }
  71.         }
  72.  
  73.         void OnClick ()
  74.         {
  75.                 if (NGUITools.GetActive(this) && NGUITools.GetActive(selectOnClick))
  76.                         UICamera.selectedObject = selectOnClick.gameObject;
  77.         }
  78. }
  79.  

brisck1

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: UIButton Keys not respecting disabled buttons?
« Reply #2 on: January 04, 2014, 11:56:43 AM »
Awesome, thanks! I will give that a go!

brisck1

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: UIButton Keys not respecting disabled buttons?
« Reply #3 on: January 05, 2014, 05:36:45 AM »
Ok so I tried the code revision this morning and it doesn't seem to make any difference - I'm still able to select disabled items. Also, it seems the 'Select On Click' option no longer works at all...  :(

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton Keys not respecting disabled buttons?
« Reply #4 on: January 05, 2014, 08:42:04 AM »
You mentioned that the script wasn't checking to see if the script was disabled or not. NGUITools.GetActive checks inside every function do just that -- they prevent execution if the script is disabled. Additional checks now prevent selectedObject changes if the target object is disabled. This means you must first enable the object before the selection takes place.

Memige

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: UIButton Keys not respecting disabled buttons?
« Reply #5 on: April 22, 2014, 10:36:56 AM »
This is for people tracking down related issues:

With the update to the new UIKeyNavigation system (which is awesome btw) the system is not respecting buttons in a disabled state.  A quick fix for this is by altering one line in UIKeyNavigation.cs, specifically line #131

change from:
  1. if (nav == this) continue;

to:
  1. if (nav == this || !nav.GetComponent<UIButton>().isEnabled) continue;

UIKeyNavigation will now ignore buttons that are set to disabled states when looking for non-explicit connections. 
[Important notes:
- This is in NGUI Version 3.5.5  NOT tested in or intended for later versions.
- Explicitly setting a Left, Right, Up, or Down target will still navigate to that object even if it is disabled
- This has not been thoroughly tested and may cause issues if UIKeyNavigation components are used on non-UIButton objects
- This is not an official fix, this is just a hack workaround.  Aren will need to implement a stable and robust alteration before this will be supported natively.
]

[Clarification: the disabled buttons correctly ignore clicks regardless]


-Cheers, Memige

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton Keys not respecting disabled buttons?
« Reply #6 on: April 23, 2014, 07:01:33 AM »
Thanks, I will add a similar check. You need to check for null though. Your script assumes there will always be a button script, which is not the case.