Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.DirectX.DirectInput;
- namespace Maelstrom_Engine.Input
- {
- /// <summary>
- /// Represents a standard DirectInput controller.
- /// </summary>
- public class NormalGamepad : GamepadDevice
- {
- #region - Variables Fields
- /// <summary>
- /// Maps the first 12 pc gamepad buttons to the matching
- /// bit positions of Microsoft.Xna.Framework.Input.Buttons
- /// </summary>
- private int[] GenericButtonMappings = new int[] { 14, 12, 13, 15, 8, 9, 23, 22, 4, 5, 7, 8 };
- private GamePadState _CurrentState;
- private GamePadState _PreviousState;
- //Device information:
- protected Device _Device;
- protected DeviceCaps _Caps;
- //The current joystick state!
- private JoystickState _JoyState;
- //Used to calculate thumbstick data.
- private const float CENTER = 32767.5f;
- //Is the controller connected?
- private bool _IsConnected = true;
- #endregion
- #region - Initilize NormalGamepad
- public NormalGamepad(Guid gamepadInstanceGuid)
- {
- //Aquire the device and capabilties.
- _Device = new Device(gamepadInstanceGuid);
- _Device.SetDataFormat(DeviceDataFormat.Joystick);
- _Device.Acquire();
- _Caps = _Device.Caps;
- }
- #endregion
- #region - Override LThumbstick Method from GamepadDevice
- public override Vector2 LThumbstick
- {
- get { return _CurrentState.ThumbSticks.Left; }
- }
- #endregion
- #region - Override RThumbstick Method from GamepadDevice
- public override Vector2 RThumbstick
- {
- get { return _CurrentState.ThumbSticks.Right; }
- }
- #endregion
- #region - Override Connected Method from GamepadDevice
- public override bool Connected
- {
- get { return _IsConnected; }
- }
- #endregion
- #region - Override IsButtonDown Method from GamepadDevice
- public override bool IsButtonDown(Buttons button)
- {
- if (_CurrentState.IsButtonDown(button))
- return true;
- else
- return false;
- }
- #endregion
- #region - Override IsButtonUp Method from GamepadDevice
- public override bool IsButtonUp(Buttons button)
- {
- if (_CurrentState.IsButtonUp(button))
- return true;
- else
- return false;
- }
- #endregion
- #region - Override WasButtonHeld Method from GamepadDevice
- public override bool WasButtonHeld(Buttons button)
- {
- if (_CurrentState.IsButtonDown(button) && _PreviousState.IsButtonDown(button))
- return true;
- else
- return false;
- }
- #endregion
- #region - Override WasButtonPressed Method from GamepadDevice
- public override bool WasButtonPressed(Buttons button)
- {
- if (_CurrentState.IsButtonDown(button) && _PreviousState.IsButtonUp(button))
- return true;
- else
- return false;
- }
- #endregion
- #region - Override WasButtonReleased Method from GamepadDevice
- public override bool WasButtonReleased(Buttons button)
- {
- if (_PreviousState.IsButtonDown(button) && _CurrentState.IsButtonUp(button))
- return true;
- else
- return false;
- }
- #endregion
- #region - Override Update Method from GamepadDevice
- public override void Update()
- {
- //Now, if the controller is connected update the states.
- if (Connected)
- {
- #region Is controller connected? Needs a faster implementation eventuallly.
- //Set the device to connected
- _IsConnected = true;
- try
- {
- _JoyState = _Device.CurrentJoystickState;
- }
- catch (Exception)
- {
- //If the device errors out, it is no longer connected... error.
- _IsConnected = false;
- }
- #endregion
- _PreviousState = _CurrentState;
- _CurrentState = GetState();
- }
- }
- #endregion
- #region - Internal Deadzone Set
- private float ApplyDeadzone(float value)
- {
- if (value < 0.01f && value > -0.1f)
- value = 0f;
- return value;
- }
- #endregion
- #region - Take all the info from NormalGamepad and set a knew gamepadstate for generic gamepads
- private GamePadState GetState()
- {
- byte[] btns = _JoyState.GetButtons();
- // Point Of View = DPad
- int pov = _JoyState.GetPointOfView()[0];
- // Map the DirectInput buttons to XNA Buttons enum values
- Buttons padBtns = (Buttons)0;
- int btnCount = Math.Min(_Caps.NumberButtons, 12);
- for (int i = 0; i < btnCount; i++)
- {
- if (btns[i] != 0)
- padBtns |= (Buttons)(1 << GenericButtonMappings[i]);
- }
- float LeftX = ApplyDeadzone((_JoyState.X - CENTER) / CENTER);
- float LeftY = ApplyDeadzone((_JoyState.Y - CENTER) / CENTER);
- float RightX = ApplyDeadzone((_JoyState.Z - CENTER) / CENTER);
- float RightY = ApplyDeadzone((_JoyState.Rz - CENTER) / CENTER);
- GamePadState _GamepadState =
- new GamePadState(
- new GamePadThumbSticks(
- _Caps.NumberAxes <= 0 ? Vector2.Zero :
- new Vector2(LeftX, LeftY),
- _Caps.NumberAxes <= 2 ? Vector2.Zero :
- new Vector2(RightX, RightY)),
- new GamePadTriggers(
- (0 != (int)(Buttons.LeftTrigger & padBtns)) ? 1.0f : 0.0f,
- (0 != (int)(Buttons.RightTrigger & padBtns)) ? 1.0f : 0.0f),
- new GamePadButtons(padBtns),
- (pov < 0) ? new GamePadDPad() : new GamePadDPad(
- (pov > 27000 || pov < 9000) ? ButtonState.Pressed : ButtonState.Released,
- (9000 < pov && pov < 27000) ? ButtonState.Pressed : ButtonState.Released,
- (18000 < pov) ? ButtonState.Pressed : ButtonState.Released,
- (0 < pov && pov < 18000) ? ButtonState.Pressed : ButtonState.Released));
- return _GamepadState;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement