Advertisement
infinite_ammo

XInput.cs

Nov 5th, 2012
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.10 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class XInput
  5. {
  6.     public enum DPad
  7.     {
  8.         Left,
  9.         Right,
  10.         Up,
  11.         Down
  12.     }
  13.  
  14.     public enum Button
  15.     {
  16.         None,
  17.         A,
  18.         B,
  19.         X,
  20.         Y,
  21.         Back,
  22.         Start,
  23.         Home,
  24.         BumperLeft,
  25.         BumperRight,
  26.         StickClickLeft,
  27.         StickClickRight,
  28.         Max
  29.     }
  30.  
  31.     public int index = 0;
  32.  
  33.     public Vector2 leftStick;
  34.     public Vector2 rightStick;
  35.  
  36.     public float triggerLeft;
  37.     public float triggerRight;
  38.  
  39.     public Vector2 dPad;
  40.     public Vector3 oldDPad;
  41.  
  42.     private const float DEAD_ZONE = 0.33f;
  43.  
  44.     public bool GetButtonDown(Button button)
  45.     {
  46.         return Input.GetKeyDown(ButtonToKeyCode(button));
  47.     }
  48.  
  49.     public bool GetButtonUp(Button button)
  50.     {
  51.         return Input.GetKeyUp(ButtonToKeyCode(button));
  52.     }
  53.  
  54.     public bool GetButton(Button button)
  55.     {
  56.         return Input.GetKey(ButtonToKeyCode(button));
  57.     }
  58.  
  59.     public KeyCode GetButtonForJoystick(int button)
  60.     {
  61.         if (index == 0)
  62.         {
  63.             return (KeyCode)((int)KeyCode.Joystick1Button0 + button);
  64.         }
  65.         else if (index == 1)
  66.         {
  67.             return (KeyCode)((int)KeyCode.Joystick2Button0 + button);
  68.         }
  69.         else if (index == 2)
  70.         {
  71.             return (KeyCode)((int)KeyCode.Joystick3Button0 + button);
  72.         }
  73.         else if (index == 3)
  74.         {
  75.             return (KeyCode)((int)KeyCode.Joystick4Button0 + button);
  76.         }
  77.         return KeyCode.None;
  78.     }
  79.  
  80.     public KeyCode ButtonToKeyCode(Button button)
  81.     {
  82.         if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXWebPlayer)
  83.         {
  84.             switch (button)
  85.             {
  86.                 case Button.A:
  87.                     return GetButtonForJoystick(16);
  88.                 case Button.B:
  89.                     return GetButtonForJoystick(17);
  90.                 case Button.X:
  91.                     return GetButtonForJoystick(18);
  92.                 case Button.Y:
  93.                     return GetButtonForJoystick(19);
  94.                 case Button.BumperLeft:
  95.                     return GetButtonForJoystick(13);
  96.                 case Button.BumperRight:
  97.                     return GetButtonForJoystick(14);
  98.                 case Button.Back:
  99.                     return GetButtonForJoystick(10);
  100.                 case Button.Start:
  101.                     return GetButtonForJoystick(9);
  102.                 case Button.StickClickLeft:
  103.                     return GetButtonForJoystick(11);
  104.                 case Button.StickClickRight:
  105.                     return GetButtonForJoystick(12);
  106.             }
  107.         }
  108.         else if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsWebPlayer)
  109.         {
  110.             switch (button)
  111.             {
  112.                 case Button.A:
  113.                     return GetButtonForJoystick(0);
  114.                 case Button.B:
  115.                     return GetButtonForJoystick(1);
  116.                 case Button.X:
  117.                     return GetButtonForJoystick(2);
  118.                 case Button.Y:
  119.                     return GetButtonForJoystick(3);
  120.                 case Button.BumperLeft:
  121.                     return GetButtonForJoystick(4);
  122.                 case Button.BumperRight:
  123.                     return GetButtonForJoystick(5);
  124.                 case Button.Back:
  125.                     return GetButtonForJoystick(6);
  126.                 case Button.Start:
  127.                     return GetButtonForJoystick(7);
  128.                 case Button.StickClickLeft:
  129.                     return GetButtonForJoystick(8);
  130.                 case Button.StickClickRight:
  131.                     return GetButtonForJoystick(9);
  132.             }
  133.         }
  134.  
  135.         return KeyCode.None;
  136.     }
  137.  
  138.     public void Update()
  139.     {
  140.         if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor)
  141.         {
  142.             //Mac OSX
  143.             triggerLeft = GetAxisRaw(5);
  144.             triggerRight = GetAxisRaw(6);
  145.  
  146.             leftStick = GetAxisVec(1, 2);
  147.             rightStick = GetAxisVec(3, 4);
  148.            
  149.             oldDPad = dPad;
  150.  
  151.             dPad.x = (Input.GetKeyDown(GetButtonForJoystick(8))?1.0f:0.0f) + (Input.GetKeyDown(GetButtonForJoystick(7))?-1.0f:0.0f);
  152.             dPad.y = (Input.GetKeyDown(GetButtonForJoystick(6))?1.0f:0.0f) + (Input.GetKeyDown(GetButtonForJoystick(5))?-1.0f:0.0f);
  153.         }
  154.         else if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
  155.         {
  156.             //Windows
  157.             float triggers = Input.GetAxis("XPCTriggers" + (index+1));
  158.             if (triggers < -Mathf.Epsilon)
  159.                 triggerRight = -triggers;
  160.             else if (triggers > Mathf.Epsilon)
  161.                 triggerLeft = triggers;
  162.             else
  163.                 triggerLeft = triggerRight = 0.0f;
  164.  
  165.             leftStick = GetAxisVec(1, 2);
  166.             rightStick = GetAxisVec(4, 5);
  167.  
  168.             //Debug.LogWarning("player: " + index + " leftStick: " + leftStick + " rightStick: " + rightStick);
  169.  
  170.             oldDPad = dPad;
  171.             dPad = GetAxisVec(6, 7);
  172.         }
  173.     }
  174.  
  175.     float GetAxisRaw(int axis)
  176.     {
  177.         return Input.GetAxisRaw("Joy" + (index+1) + "Axis" + axis);
  178.     }
  179.  
  180.     Vector2 GetAxisVecRaw(int axis1, int axis2)
  181.     {
  182.         return new Vector2(Input.GetAxisRaw("Joy" + (index+1) + "Axis" + axis1), Input.GetAxisRaw("Joy" + (index+1) + "Axis" + axis2));
  183.     }
  184.  
  185.     Vector2 GetAxisVec(int axis1, int axis2)
  186.     {
  187.         Vector2 vec = GetAxisVecRaw(axis1, axis2);
  188.         if (Mathf.Abs(vec.x) < DEAD_ZONE)
  189.         {
  190.             vec.x = 0.0f;
  191.         }
  192.         if (Mathf.Abs(vec.y) < DEAD_ZONE)
  193.         {
  194.             vec.y = 0.0f;
  195.         }
  196.         return vec;
  197.     }
  198.  
  199.     public bool GetDPadDown(DPad dPadEnum)
  200.     {
  201.         switch (dPadEnum)
  202.         {
  203.             case DPad.Left:
  204.                 return (dPad.x < 0.0f && oldDPad.x == 0.0f);
  205.             case DPad.Right:
  206.                 return (dPad.x > 0.0f && oldDPad.x == 0.0f);
  207.             case DPad.Up:
  208.                 return (dPad.y > 0.0f && oldDPad.y == 0.0f);
  209.             case DPad.Down:
  210.                 return (dPad.y < 0.0f && oldDPad.y == 0.0f);
  211.         }
  212.         return false;
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement