Advertisement
ZachHoefler

OUYA UIButtonKeys for NGUI

Jan 29th, 2013
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.04 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System;
  4. using System.Timers;
  5.  
  6. /// <summary>
  7. /// OUYA variant of NGUI's built-in UIButtonKeys script. Handles OUYA input, which doesn't go through the normal Unity input on the console.
  8. /// </summary>
  9. /// <remarks>
  10. /// NGUI's UICamera.cs needs to have its "Application.platform == RuntimePlatform.Android" check under Awake() commented out to allow
  11. /// buttons to be selected properly.
  12. /// The input checks in here could be done more efficiently by modifying NGUI's UICamera.cs directly for all of the input,
  13. /// but we want to keep this as non-intrusive as humanly possible.
  14. /// </remarks>
  15. [RequireComponent(typeof(Collider))]
  16. [AddComponentMenu("NGUI/Interaction/OUYA Button Keys")]
  17. public class OuyaUIButtonKeys : MonoBehaviour
  18. {
  19.     public bool startsSelected = false;
  20.     public OuyaUIButtonKeys selectOnClick;
  21.     public OuyaUIButtonKeys selectOnUp;
  22.     public OuyaUIButtonKeys selectOnDown;
  23.     public OuyaUIButtonKeys selectOnLeft;
  24.     public OuyaUIButtonKeys selectOnRight;
  25.     public OuyaSDK.KeyEnum activateButton = OuyaSDK.KeyEnum.BUTTON_O;
  26.  
  27.     private const double JoystickUpdateInterval = 200;
  28.     private const float JoystickThreshold = 0.9f;
  29.     private Timer joystickTimer;
  30.     private bool joystickCanTrigger = true;
  31.  
  32.     void Start()
  33.     {
  34.         if (startsSelected && (UICamera.selectedObject == null || !NGUITools.GetActive(UICamera.selectedObject)))
  35.         {
  36.             UICamera.selectedObject = gameObject;
  37.         }
  38.     }
  39.  
  40.     void Awake()
  41.     {
  42.         OuyaInputManager.OuyaButtonEvent.addButtonEventListener(HandleButtonEvents);
  43.         joystickTimer = new Timer(JoystickUpdateInterval);
  44.         joystickTimer.Elapsed += new ElapsedEventHandler(joystickTimer_Elapsed);
  45.     }
  46.  
  47.     void joystickTimer_Elapsed(object sender, ElapsedEventArgs e)
  48.     {
  49.         joystickCanTrigger = true;
  50.         joystickTimer.Stop();
  51.     }
  52.  
  53.     private void StartJoystickResetTimer()
  54.     {
  55.         joystickCanTrigger = false;
  56.         joystickTimer.Start();
  57.     }
  58.  
  59.     void OnDestroy()
  60.     {
  61.         OuyaInputManager.OuyaButtonEvent.removeButtonEventListener(HandleButtonEvents);
  62.     }
  63.  
  64.     private void HandleButtonEvents(OuyaSDK.OuyaPlayer p, OuyaSDK.KeyEnum b, OuyaSDK.InputAction bs)
  65.     {
  66.         if (enabled && NGUITools.GetActive(gameObject))
  67.         {
  68.             // Ignoring P to allow any player to use the menu
  69.             if (bs.Equals(OuyaSDK.InputAction.KeyDown))
  70.             {
  71.                 switch (b)
  72.                 {
  73.                     case OuyaSDK.KeyEnum.BUTTON_DPAD_UP:
  74.                         OnKey(KeyCode.UpArrow);
  75.                         break;
  76.                     case OuyaSDK.KeyEnum.BUTTON_DPAD_DOWN:
  77.                         OnKey(KeyCode.DownArrow);
  78.                         break;
  79.                     case OuyaSDK.KeyEnum.BUTTON_DPAD_LEFT:
  80.                         OnKey(KeyCode.LeftArrow);
  81.                         break;
  82.                     case OuyaSDK.KeyEnum.BUTTON_DPAD_RIGHT:
  83.                         OnKey(KeyCode.RightArrow);
  84.                         break;
  85.                 }
  86.             }
  87.  
  88.             if (b == activateButton && gameObject == UICamera.selectedObject)
  89.             {
  90.                 SendMessage("OnPress", bs.Equals(OuyaSDK.InputAction.KeyDown), SendMessageOptions.DontRequireReceiver);
  91.             }
  92.         }
  93.     }
  94.  
  95.     void Update()
  96.     {
  97.         if (joystickCanTrigger && enabled && NGUITools.GetActive(gameObject))
  98.         {
  99.             float greatestX = 0.0f;
  100.             float greatestY = 0.0f;
  101.             float leastX = 0.0f;
  102.             float leastY = 0.0f;
  103.  
  104.             //foreach (OuyaSDK.OuyaPlayer player in Enum.GetValues(typeof(OuyaSDK.OuyaPlayer))) // TODO: Use once input lag is fixed
  105.             for (int i = 1; i <= 4; ++i) // HACK: check a hard-coded number of players
  106.             {
  107.                 OuyaSDK.OuyaPlayer player = (OuyaSDK.OuyaPlayer)i;
  108.  
  109.                 float joystickX = OuyaInputManager.GetAxis("LX", player);
  110.                 float joystickY = OuyaInputManager.GetAxis("LY", player);
  111.  
  112.                 greatestX = Math.Max(joystickX, greatestX);
  113.                 greatestY = Math.Max(joystickY, greatestY);
  114.                 leastX = Math.Min(joystickX, leastX);
  115.                 leastY = Math.Min(joystickY, leastY);
  116.             }
  117.  
  118.             if (greatestX > JoystickThreshold)
  119.             {
  120.                 StartJoystickResetTimer();
  121.                 OnKey(KeyCode.RightArrow);
  122.             }
  123.             if (greatestY > JoystickThreshold)
  124.             {
  125.                 StartJoystickResetTimer();
  126.                 OnKey(KeyCode.UpArrow);
  127.             }
  128.             if (leastX < -JoystickThreshold)
  129.             {
  130.                 StartJoystickResetTimer();
  131.                 OnKey(KeyCode.LeftArrow);
  132.             }
  133.             if (leastY < -JoystickThreshold)
  134.             {
  135.                 StartJoystickResetTimer();
  136.                 OnKey(KeyCode.DownArrow);
  137.             }
  138.         }
  139.  
  140.  
  141.     }
  142.  
  143.     void OnKey(KeyCode key)
  144.     {
  145.         if (enabled && NGUITools.GetActive(gameObject))
  146.         {
  147.             switch (key)
  148.             {
  149.                 case KeyCode.LeftArrow:
  150.                     if (selectOnLeft != null) UICamera.selectedObject = selectOnLeft.gameObject;
  151.                     break;
  152.                 case KeyCode.RightArrow:
  153.                     if (selectOnRight != null) UICamera.selectedObject = selectOnRight.gameObject;
  154.                     break;
  155.                 case KeyCode.UpArrow:
  156.                     if (selectOnUp != null) UICamera.selectedObject = selectOnUp.gameObject;
  157.                     break;
  158.                 case KeyCode.DownArrow:
  159.                     if (selectOnDown != null) UICamera.selectedObject = selectOnDown.gameObject;
  160.                     break;
  161.                 case KeyCode.Tab:
  162.                     if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
  163.                     {
  164.                         if (selectOnLeft != null) UICamera.selectedObject = selectOnLeft.gameObject;
  165.                         else if (selectOnUp != null) UICamera.selectedObject = selectOnUp.gameObject;
  166.                         else if (selectOnDown != null) UICamera.selectedObject = selectOnDown.gameObject;
  167.                         else if (selectOnRight != null) UICamera.selectedObject = selectOnRight.gameObject;
  168.                     }
  169.                     else
  170.                     {
  171.                         if (selectOnRight != null) UICamera.selectedObject = selectOnRight.gameObject;
  172.                         else if (selectOnDown != null) UICamera.selectedObject = selectOnDown.gameObject;
  173.                         else if (selectOnUp != null) UICamera.selectedObject = selectOnUp.gameObject;
  174.                         else if (selectOnLeft != null) UICamera.selectedObject = selectOnLeft.gameObject;
  175.                     }
  176.                     break;
  177.             }
  178.         }
  179.     }
  180.  
  181.     void OnClick()
  182.     {
  183.         if (enabled && selectOnClick != null)
  184.         {
  185.             UICamera.selectedObject = selectOnClick.gameObject;
  186.         }
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement