Esempio n. 1
0
        private static Vector2 ConvertThumbStick(
            short x, short y, short deadZone, Microsoft.Xna.Framework.Input.GamePadDeadZone deadZoneMode)
        {
            if (deadZoneMode == Microsoft.Xna.Framework.Input.GamePadDeadZone.IndependentAxes)
            {
                // using int to prevent overrun
                int fx  = x;
                int fy  = y;
                int fdz = deadZone;
                if (fx * fx < fdz * fdz)
                {
                    x = 0;
                }
                if (fy * fy < fdz * fdz)
                {
                    y = 0;
                }
            }
            else if (deadZoneMode == Microsoft.Xna.Framework.Input.GamePadDeadZone.Circular)
            {
                // using int to prevent overrun
                int fx  = x;
                int fy  = y;
                int fdz = deadZone;
                if ((fx * fx) + (fy * fy) < fdz * fdz)
                {
                    x = 0;
                    y = 0;
                }
            }

            return(new Vector2(x < 0 ? -((float)x / (float)short.MinValue) : (float)x / (float)short.MaxValue,
                               y < 0 ? -((float)y / (float)short.MinValue) : (float)y / (float)short.MaxValue));
        }
Esempio n. 2
0
        public static Microsoft.Xna.Framework.Input.GamePadState GetState(PlayerIndex playerIndex,
                                                                          Microsoft.Xna.Framework.Input.GamePadDeadZone deadZoneMode)
        {
            var controller = GetController(playerIndex);

            if (!controller.IsConnected)
            {
                return(new GamePadState()); // GamePadState.IsConnected = false by default
            }
            var gamepad = controller.GetState().Gamepad;

            Microsoft.Xna.Framework.Input.GamePadThumbSticks thumbSticks = new Microsoft.Xna.Framework.Input.GamePadThumbSticks(
                leftPosition: ConvertThumbStick(gamepad.LeftThumbX, gamepad.LeftThumbY,
                                                SharpDX.XInput.Gamepad.LeftThumbDeadZone, deadZoneMode),
                rightPosition: ConvertThumbStick(gamepad.RightThumbX, gamepad.RightThumbY,
                                                 SharpDX.XInput.Gamepad.RightThumbDeadZone, deadZoneMode));

            Microsoft.Xna.Framework.Input.GamePadTriggers triggers = new Microsoft.Xna.Framework.Input.GamePadTriggers(
                leftTrigger: gamepad.LeftTrigger / (float)byte.MaxValue,
                rightTrigger: gamepad.RightTrigger / (float)byte.MaxValue);

            Microsoft.Xna.Framework.Input.GamePadState state = new Microsoft.Xna.Framework.Input.GamePadState(
                thumbSticks: thumbSticks,
                triggers: triggers,
                buttons: ConvertToButtons(
                    buttonFlags: gamepad.Buttons,
                    leftThumbX: gamepad.LeftThumbX,
                    leftThumbY: gamepad.LeftThumbY,
                    rightThumbX: gamepad.RightThumbX,
                    rightThumbY: gamepad.RightThumbY,
                    leftTrigger: gamepad.LeftTrigger,
                    rightTrigger: gamepad.RightTrigger),
                dPad: ConvertToGamePadDPad(gamepad.Buttons));

            return(state);
        }