Ejemplo n.º 1
0
        private static GamePadState PlatformGetState(int index, GamePadDeadZone leftDeadZoneMode, GamePadDeadZone rightDeadZoneMode)
        {
            var state = GamePadState.Default;
            var jcap  = Joystick.GetCapabilities(index);

            if (jcap.IsConnected)
            {
                state.IsConnected = true;

                var jstate = Joystick.GetState(index);

                if (!GamePadCache.ContainsKey(jcap.Identifier))
                {
                    GamePadCache.Add(jcap.Identifier, Configurations.ContainsKey(jcap.Identifier) ? new GamepadTranslator(Configurations[jcap.Identifier]) : new GamepadTranslator(""));
                }

                var gpc = GamePadCache[jcap.Identifier];

                Buttons buttons =
                    (gpc.ButtonPressed("a", jstate) ? Buttons.A : 0) |
                    (gpc.ButtonPressed("b", jstate) ? Buttons.B : 0) |
                    (gpc.ButtonPressed("back", jstate) ? Buttons.Back : 0) |
                    (gpc.ButtonPressed("guide", jstate) ? Buttons.BigButton : 0) |
                    (gpc.ButtonPressed("leftshoulder", jstate) ? Buttons.LeftShoulder : 0) |
                    (gpc.ButtonPressed("leftstick", jstate) ? Buttons.LeftStick : 0) |
                    (gpc.ButtonPressed("rightshoulder", jstate) ? Buttons.RightShoulder : 0) |
                    (gpc.ButtonPressed("rightstick", jstate) ? Buttons.RightStick : 0) |
                    (gpc.ButtonPressed("start", jstate) ? Buttons.Start : 0) |
                    (gpc.ButtonPressed("x", jstate) ? Buttons.X : 0) |
                    (gpc.ButtonPressed("y", jstate) ? Buttons.Y : 0) |
                    0;

                var sticks =
                    new GamePadThumbSticks(
                        new Vector2(gpc.AxisPressed("leftx", jstate), gpc.AxisPressed("lefty", jstate)),
                        new Vector2(gpc.AxisPressed("rightx", jstate), gpc.AxisPressed("righty", jstate)),
                        leftDeadZoneMode,
                        rightDeadZoneMode
                        );

                var dpad =
                    new GamePadDPad(
                        gpc.DpadPressed("dpup", jstate) ? ButtonState.Pressed : ButtonState.Released,
                        gpc.DpadPressed("dpdown", jstate) ? ButtonState.Pressed : ButtonState.Released,
                        gpc.DpadPressed("dpleft", jstate) ? ButtonState.Pressed : ButtonState.Released,
                        gpc.DpadPressed("dpright", jstate) ? ButtonState.Pressed : ButtonState.Released
                        );

                var triggers =
                    new GamePadTriggers(
                        gpc.TriggerPressed("lefttrigger", jstate),
                        gpc.TriggerPressed("righttrigger", jstate)
                        );


                state = new GamePadState(sticks, triggers, new GamePadButtons(buttons), dpad);
            }

            return(state);
        }
Ejemplo n.º 2
0
        private static GamePadState PlatformGetState(int index, GamePadDeadZone leftDeadZoneMode, GamePadDeadZone rightDeadZoneMode)
        {
            if (!_gamepads.ContainsKey(index))
            {
                return(index == 0 ? GetDefaultState() : GamePadState.Default);
            }

            var state = _gamepads[index].GetCurrentReading();

            var sticks = new GamePadThumbSticks(
                new Vector2((float)state.LeftThumbstickX, (float)state.LeftThumbstickY),
                new Vector2((float)state.RightThumbstickX, (float)state.RightThumbstickY),
                leftDeadZoneMode,
                rightDeadZoneMode
                );

            var triggers = new GamePadTriggers(
                (float)state.LeftTrigger,
                (float)state.RightTrigger
                );

            Buttons buttonStates =
                (state.Buttons.HasFlag(WGI.GamepadButtons.A) ? Buttons.A : 0) |
                (state.Buttons.HasFlag(WGI.GamepadButtons.B) ? Buttons.B : 0) |
                ((state.Buttons.HasFlag(WGI.GamepadButtons.View) || Back) ? Buttons.Back : 0) |
                0 | //BigButton is unavailable by Windows.Gaming.Input.Gamepad
                (state.Buttons.HasFlag(WGI.GamepadButtons.LeftShoulder) ? Buttons.LeftShoulder : 0) |
                (state.Buttons.HasFlag(WGI.GamepadButtons.LeftThumbstick) ? Buttons.LeftStick : 0) |
                (state.Buttons.HasFlag(WGI.GamepadButtons.RightShoulder) ? Buttons.RightShoulder : 0) |
                (state.Buttons.HasFlag(WGI.GamepadButtons.RightThumbstick) ? Buttons.RightStick : 0) |
                (state.Buttons.HasFlag(WGI.GamepadButtons.Menu) ? Buttons.Start : 0) |
                (state.Buttons.HasFlag(WGI.GamepadButtons.X) ? Buttons.X : 0) |
                (state.Buttons.HasFlag(WGI.GamepadButtons.Y) ? Buttons.Y : 0) |
                0;

            // Check triggers
            if (triggers.Left > TriggerThreshold)
            {
                buttonStates |= Buttons.LeftTrigger;
            }
            if (triggers.Right > TriggerThreshold)
            {
                buttonStates |= Buttons.RightTrigger;
            }

            var buttons = new GamePadButtons(buttonStates);

            var dpad = new GamePadDPad(
                state.Buttons.HasFlag(WGI.GamepadButtons.DPadUp) ? ButtonState.Pressed : ButtonState.Released,
                state.Buttons.HasFlag(WGI.GamepadButtons.DPadDown) ? ButtonState.Pressed : ButtonState.Released,
                state.Buttons.HasFlag(WGI.GamepadButtons.DPadLeft) ? ButtonState.Pressed : ButtonState.Released,
                state.Buttons.HasFlag(WGI.GamepadButtons.DPadRight) ? ButtonState.Pressed : ButtonState.Released
                );

            var result = new GamePadState(sticks, triggers, buttons, dpad);

            result.PacketNumber = (int)state.Timestamp;
            return(result);
        }