Advertisement
Guest User

Untitled

a guest
May 28th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Input;
  7. using Microsoft.DirectX.DirectInput;
  8.  
  9. namespace Maelstrom_Engine.Input
  10. {
  11.     /// <summary>
  12.     /// Represents a standard DirectInput controller.
  13.     /// </summary>
  14.     public class NormalGamepad : GamepadDevice
  15.     {
  16.         #region - Variables Fields
  17.  
  18.         /// <summary>
  19.         /// Maps the first 12 pc gamepad buttons to the matching
  20.         /// bit positions of Microsoft.Xna.Framework.Input.Buttons
  21.         /// </summary>
  22.         private int[] GenericButtonMappings = new int[] { 14, 12, 13, 15, 8, 9, 23, 22, 4, 5, 7, 8 };
  23.  
  24.         private GamePadState _CurrentState;
  25.         private GamePadState _PreviousState;
  26.  
  27.         //Device information:
  28.         protected Device _Device;
  29.         protected DeviceCaps _Caps;
  30.  
  31.  
  32.         //The current joystick state!
  33.         private JoystickState _JoyState;
  34.  
  35.         //Used to calculate thumbstick data.
  36.         private const float CENTER = 32767.5f;
  37.  
  38.  
  39.         //Is the controller connected?
  40.         private bool _IsConnected = true;
  41.  
  42.         #endregion
  43.  
  44.         #region - Initilize NormalGamepad
  45.  
  46.         public NormalGamepad(Guid gamepadInstanceGuid)
  47.         {
  48.             //Aquire the device and capabilties.
  49.             _Device = new Device(gamepadInstanceGuid);
  50.             _Device.SetDataFormat(DeviceDataFormat.Joystick);
  51.             _Device.Acquire();
  52.             _Caps = _Device.Caps;
  53.         }
  54.  
  55.         #endregion
  56.  
  57.         #region - Override LThumbstick Method from GamepadDevice
  58.  
  59.         public override Vector2 LThumbstick
  60.         {
  61.             get { return _CurrentState.ThumbSticks.Left; }
  62.         }
  63.  
  64.         #endregion
  65.  
  66.         #region - Override RThumbstick Method from GamepadDevice
  67.  
  68.         public override Vector2 RThumbstick
  69.         {
  70.             get { return _CurrentState.ThumbSticks.Right; }
  71.         }
  72.  
  73.         #endregion
  74.  
  75.         #region - Override Connected Method from GamepadDevice
  76.  
  77.         public override bool Connected
  78.         {
  79.             get { return _IsConnected; }
  80.         }
  81.  
  82.         #endregion
  83.  
  84.         #region - Override IsButtonDown Method from GamepadDevice
  85.  
  86.         public override bool IsButtonDown(Buttons button)
  87.         {
  88.             if (_CurrentState.IsButtonDown(button))
  89.                 return true;
  90.             else
  91.                 return false;
  92.         }
  93.  
  94.         #endregion
  95.  
  96.         #region - Override IsButtonUp Method from GamepadDevice
  97.  
  98.         public override bool IsButtonUp(Buttons button)
  99.         {
  100.             if (_CurrentState.IsButtonUp(button))
  101.                 return true;
  102.             else
  103.                 return false;
  104.         }
  105.  
  106.         #endregion
  107.  
  108.         #region - Override WasButtonHeld Method from GamepadDevice
  109.  
  110.         public override bool WasButtonHeld(Buttons button)
  111.         {
  112.             if (_CurrentState.IsButtonDown(button) && _PreviousState.IsButtonDown(button))
  113.                 return true;
  114.             else
  115.                 return false;
  116.         }
  117.  
  118.         #endregion
  119.  
  120.         #region - Override WasButtonPressed Method from GamepadDevice
  121.  
  122.         public override bool WasButtonPressed(Buttons button)
  123.         {
  124.             if (_CurrentState.IsButtonDown(button) && _PreviousState.IsButtonUp(button))
  125.                 return true;
  126.             else
  127.                 return false;
  128.         }
  129.  
  130.         #endregion
  131.  
  132.         #region - Override WasButtonReleased Method from GamepadDevice
  133.  
  134.         public override bool WasButtonReleased(Buttons button)
  135.         {
  136.             if (_PreviousState.IsButtonDown(button) && _CurrentState.IsButtonUp(button))
  137.                 return true;
  138.             else
  139.                 return false;
  140.         }
  141.  
  142.         #endregion
  143.  
  144.         #region - Override Update Method from GamepadDevice
  145.  
  146.         public override void Update()
  147.         {
  148.  
  149.             //Now, if the controller is connected update the states.
  150.             if (Connected)
  151.             {
  152.  
  153.                 #region Is controller connected? Needs a faster implementation eventuallly.
  154.                 //Set the device to connected
  155.                 _IsConnected = true;
  156.                 try
  157.                 {
  158.                     _JoyState = _Device.CurrentJoystickState;
  159.                 }
  160.  
  161.                 catch (Exception)
  162.                 {
  163.                     //If the device errors out, it is no longer connected... error.
  164.                     _IsConnected = false;
  165.                 }
  166.                 #endregion
  167.  
  168.                 _PreviousState = _CurrentState;
  169.                 _CurrentState = GetState();
  170.             }
  171.         }
  172.  
  173.         #endregion
  174.  
  175.         #region - Internal Deadzone Set
  176.  
  177.         private float ApplyDeadzone(float value)
  178.         {
  179.             if (value < 0.01f && value > -0.1f)
  180.                 value = 0f;
  181.  
  182.             return value;
  183.         }
  184.  
  185.         #endregion
  186.  
  187.         #region - Take all the info from NormalGamepad and set a knew gamepadstate for generic gamepads
  188.  
  189.         private GamePadState GetState()
  190.         {
  191.             byte[] btns = _JoyState.GetButtons();
  192.  
  193.             // Point Of View = DPad
  194.             int pov = _JoyState.GetPointOfView()[0];
  195.  
  196.             // Map the DirectInput buttons to XNA Buttons enum values
  197.             Buttons padBtns = (Buttons)0;
  198.             int btnCount = Math.Min(_Caps.NumberButtons, 12);
  199.             for (int i = 0; i < btnCount; i++)
  200.             {
  201.                 if (btns[i] != 0)
  202.                     padBtns |= (Buttons)(1 << GenericButtonMappings[i]);
  203.             }
  204.  
  205.             float LeftX = ApplyDeadzone((_JoyState.X - CENTER) / CENTER);
  206.             float LeftY = ApplyDeadzone((_JoyState.Y - CENTER) / CENTER);
  207.             float RightX = ApplyDeadzone((_JoyState.Z - CENTER) / CENTER);
  208.             float RightY = ApplyDeadzone((_JoyState.Rz - CENTER) / CENTER);
  209.  
  210.             GamePadState _GamepadState =
  211.                   new GamePadState(
  212.  
  213.                       new GamePadThumbSticks(
  214.                   _Caps.NumberAxes <= 0 ? Vector2.Zero :
  215.                       new Vector2(LeftX, LeftY),
  216.                   _Caps.NumberAxes <= 2 ? Vector2.Zero :
  217.                       new Vector2(RightX, RightY)),
  218.  
  219.                       new GamePadTriggers(
  220.                   (0 != (int)(Buttons.LeftTrigger & padBtns)) ? 1.0f : 0.0f,
  221.                   (0 != (int)(Buttons.RightTrigger & padBtns)) ? 1.0f : 0.0f),
  222.  
  223.                   new GamePadButtons(padBtns),
  224.               (pov < 0) ? new GamePadDPad() : new GamePadDPad(
  225.                   (pov > 27000 || pov < 9000) ? ButtonState.Pressed : ButtonState.Released,
  226.                   (9000 < pov && pov < 27000) ? ButtonState.Pressed : ButtonState.Released,
  227.                   (18000 < pov) ? ButtonState.Pressed : ButtonState.Released,
  228.                   (0 < pov && pov < 18000) ? ButtonState.Pressed : ButtonState.Released));
  229.  
  230.             return _GamepadState;
  231.         }
  232.  
  233.         #endregion
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement