Author Topic: UIButtonKeys backtrace selection  (Read 2377 times)

kjenkins_oculus

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
UIButtonKeys backtrace selection
« on: November 22, 2013, 02:08:50 PM »
I've added a backtrace selection feature to UIButtonKeys that I think should go in the main branch. It makes it so if you press one direction, then the opposite direction, you end up at the same UIButtonKeys element you started at. Currently this does not happen if you have two or more UIButtonKeys that both go to one UIButtonKeys using the same direction. For example, if you have a menu at the top of the screen "Games, Movies, Apps", and you press down, then up, you would normally expect to go back to movies. But as-is, it would always end back up at the same item (probably games).

It would also help if the classes in NGui were virtual so I could derive from them rather than entirely copy/paste.

  1. //----------------------------------------------
  2. //            NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2013 Tasharen Entertainment
  4. //----------------------------------------------
  5.  
  6. using UnityEngine;
  7. using System.Collections.Generic;
  8.  
  9. /// <summary>
  10. /// Attaching this script to a widget makes it react to key events such as tab, up, down, etc.
  11. /// </summary>
  12.  
  13. [RequireComponent(typeof(Collider))]
  14. [AddComponentMenu("NGUI/Interaction/Button Keys 2")]
  15. public class UIButtonKeys2 : MonoBehaviour
  16. {
  17.         public bool backtraceSelection = true;
  18.         public bool startsSelected = false;
  19.         public UIButtonKeys2 selectOnClick;
  20.         public UIButtonKeys2 selectOnUp;
  21.         public UIButtonKeys2 selectOnDown;
  22.         public UIButtonKeys2 selectOnLeft;
  23.         public UIButtonKeys2 selectOnRight;
  24.  
  25.         void OnEnable ()
  26.         {
  27.                 if (startsSelected)
  28.                 {
  29.                         if (UICamera.selectedObject == null || !NGUITools.GetActive(UICamera.selectedObject))
  30.                         {
  31.                                 UICamera.selectedObject = gameObject;
  32.                                 UICamera.Notify(gameObject, "OnHover", true);
  33.                         }
  34.                 }
  35.         }
  36.          
  37.         void OnKey (KeyCode key)
  38.         {
  39.                 if (enabled && NGUITools.GetActive(gameObject))
  40.                 {
  41.                         switch (key)
  42.                         {
  43.                         case KeyCode.LeftArrow:
  44.                                 if (selectOnLeft != null)
  45.                                 {
  46.                                         UICamera.selectedObject = selectOnLeft.gameObject;
  47.                                         if (backtraceSelection)
  48.                                                 selectOnLeft.selectOnRight = this;                                     
  49.                                 }
  50.                                 break;
  51.                         case KeyCode.RightArrow:
  52.                                 if (selectOnRight != null)
  53.                                 {
  54.                                         UICamera.selectedObject = selectOnRight.gameObject;
  55.                                         if (backtraceSelection)
  56.                                                 selectOnRight.selectOnLeft = this;                                     
  57.                                 }
  58.                                 break;
  59.                         case KeyCode.UpArrow:
  60.                                 if (selectOnUp != null)
  61.                                 {
  62.                                         UICamera.selectedObject = selectOnUp.gameObject;
  63.                                         if (backtraceSelection)
  64.                                                 selectOnUp.selectOnDown = this;
  65.                                 }
  66.                                 break;
  67.                         case KeyCode.DownArrow:
  68.                                 if (selectOnDown != null)
  69.                                 {
  70.                                         UICamera.selectedObject = selectOnDown.gameObject;
  71.                                         if (backtraceSelection)
  72.                                                 selectOnDown.selectOnUp = this;
  73.                                 }
  74.                                 break;
  75.                         case KeyCode.Tab:
  76.                                 if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
  77.                                 {
  78.                                         if (selectOnLeft != null)
  79.                                         {
  80.                                                 UICamera.selectedObject = selectOnLeft.gameObject;
  81.                                                 if (backtraceSelection)
  82.                                                         selectOnLeft.selectOnRight = this;                             
  83.                                         }
  84.                                         else if (selectOnUp != null)
  85.                                         {
  86.                                                 UICamera.selectedObject = selectOnUp.gameObject;
  87.                                                 if (backtraceSelection)
  88.                                                         selectOnUp.selectOnDown = this;
  89.                                         }
  90.                                         else if (selectOnDown != null)
  91.                                         {
  92.                                                 UICamera.selectedObject = selectOnDown.gameObject;
  93.                                                 if (backtraceSelection)
  94.                                                         selectOnDown.selectOnUp = this;
  95.                                         }
  96.                                         else if (selectOnRight != null)
  97.                                         {
  98.                                                 UICamera.selectedObject = selectOnRight.gameObject;
  99.                                                 if (backtraceSelection)
  100.                                                         selectOnRight.selectOnLeft = this;     
  101.                                         }
  102.                                 }
  103.                                 else
  104.                                 {
  105.                                         if (selectOnRight != null)
  106.                                         {
  107.                                                 UICamera.selectedObject = selectOnRight.gameObject;
  108.                                                 if (backtraceSelection)
  109.                                                         selectOnRight.selectOnLeft = this;     
  110.                                         }
  111.                                         else if (selectOnDown != null)
  112.                                         {
  113.                                                 UICamera.selectedObject = selectOnDown.gameObject;
  114.                                                 if (backtraceSelection)
  115.                                                         selectOnDown.selectOnUp = this;
  116.                                         }
  117.                                         else if (selectOnUp != null)
  118.                                         {
  119.                                                 UICamera.selectedObject = selectOnUp.gameObject;
  120.                                                 if (backtraceSelection)
  121.                                                         selectOnUp.selectOnDown = this;
  122.                                         }
  123.                                         else if (selectOnLeft != null)
  124.                                         {
  125.                                                 UICamera.selectedObject = selectOnLeft.gameObject;
  126.                                                 if (backtraceSelection)
  127.                                                         selectOnLeft.selectOnRight = this;
  128.                                         }
  129.                                 }
  130.                                 break;
  131.                         }
  132.                 }
  133.         }
  134.  
  135.         void OnClick ()
  136.         {
  137.                 if (enabled && selectOnClick != null)
  138.                 {
  139.                         UICamera.selectedObject = selectOnClick.gameObject;
  140.                 }
  141.         }
  142. }
  143.  
  144.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButtonKeys backtrace selection
« Reply #1 on: November 22, 2013, 04:03:24 PM »
Thanks for sharing, but it's worth noting that the key-based navigation is scheduled to be re-designed in the near future. Possibly even in 3.0.7.