diff --git a/src/SMAPI/Framework/Input/GamePadStateBuilder.cs b/src/SMAPI/Framework/Input/GamePadStateBuilder.cs index 0f6e99ae3..25993d6ed 100644 --- a/src/SMAPI/Framework/Input/GamePadStateBuilder.cs +++ b/src/SMAPI/Framework/Input/GamePadStateBuilder.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; @@ -22,7 +21,7 @@ internal class GamePadStateBuilder : IInputStateBuilderThe current button states. - private readonly IDictionary? ButtonStates; + private readonly Dictionary ButtonStates; /// The left trigger value. private float LeftTrigger; @@ -37,14 +36,6 @@ internal class GamePadStateBuilder : IInputStateBuilderWhether the gamepad is currently connected. - [MemberNotNullWhen(true, nameof(GamePadStateBuilder.ButtonStates))] - public bool IsConnected { get; } - - /********* ** Public methods *********/ @@ -53,46 +44,44 @@ internal class GamePadStateBuilder : IInputStateBuilder + if (state.IsConnected) { - [SButton.DPadUp] = pad.Up, - [SButton.DPadDown] = pad.Down, - [SButton.DPadLeft] = pad.Left, - [SButton.DPadRight] = pad.Right, - - [SButton.ControllerA] = buttons.A, - [SButton.ControllerB] = buttons.B, - [SButton.ControllerX] = buttons.X, - [SButton.ControllerY] = buttons.Y, - [SButton.LeftStick] = buttons.LeftStick, - [SButton.RightStick] = buttons.RightStick, - [SButton.LeftShoulder] = buttons.LeftShoulder, - [SButton.RightShoulder] = buttons.RightShoulder, - [SButton.ControllerBack] = buttons.Back, - [SButton.ControllerStart] = buttons.Start, - [SButton.BigButton] = buttons.BigButton - }; - this.LeftTrigger = triggers.Left; - this.RightTrigger = triggers.Right; - this.LeftStickPos = sticks.Left; - this.RightStickPos = sticks.Right; + GamePadDPad pad = state.DPad; + GamePadButtons buttons = state.Buttons; + GamePadTriggers triggers = state.Triggers; + GamePadThumbSticks sticks = state.ThumbSticks; + this.ButtonStates = new Dictionary + { + [SButton.DPadUp] = pad.Up, + [SButton.DPadDown] = pad.Down, + [SButton.DPadLeft] = pad.Left, + [SButton.DPadRight] = pad.Right, + + [SButton.ControllerA] = buttons.A, + [SButton.ControllerB] = buttons.B, + [SButton.ControllerX] = buttons.X, + [SButton.ControllerY] = buttons.Y, + [SButton.LeftStick] = buttons.LeftStick, + [SButton.RightStick] = buttons.RightStick, + [SButton.LeftShoulder] = buttons.LeftShoulder, + [SButton.RightShoulder] = buttons.RightShoulder, + [SButton.ControllerBack] = buttons.Back, + [SButton.ControllerStart] = buttons.Start, + [SButton.BigButton] = buttons.BigButton + }; + this.LeftTrigger = triggers.Left; + this.RightTrigger = triggers.Right; + this.LeftStickPos = sticks.Left; + this.RightStickPos = sticks.Right; + } + else + this.ButtonStates = []; } /// public GamePadStateBuilder OverrideButtons(IDictionary overrides) { - if (!this.IsConnected) - return this; - foreach (var pair in overrides) { bool changed = true; @@ -138,10 +127,7 @@ public GamePadStateBuilder OverrideButtons(IDictionary ov // buttons default: - if (this.ButtonStates.ContainsKey(pair.Key)) - this.ButtonStates[pair.Key] = isDown ? ButtonState.Pressed : ButtonState.Released; - else - changed = false; + this.ButtonStates[pair.Key] = isDown ? ButtonState.Pressed : ButtonState.Released; break; } @@ -155,9 +141,6 @@ public GamePadStateBuilder OverrideButtons(IDictionary ov /// public IEnumerable GetPressedButtons() { - if (!this.IsConnected) - yield break; - // buttons foreach (Buttons button in this.GetPressedGamePadButtons()) yield return button.ToSButton(); @@ -213,9 +196,6 @@ public GamePadState GetState() /// Get the pressed gamepad buttons. private IEnumerable GetPressedGamePadButtons() { - if (!this.IsConnected) - yield break; - foreach (var pair in this.ButtonStates) { if (pair.Value == ButtonState.Pressed && pair.Key.TryGetController(out Buttons button)) diff --git a/src/SMAPI/Framework/Input/SInputState.cs b/src/SMAPI/Framework/Input/SInputState.cs index 44276b7c2..d41279e4c 100644 --- a/src/SMAPI/Framework/Input/SInputState.cs +++ b/src/SMAPI/Framework/Input/SInputState.cs @@ -238,14 +238,14 @@ private bool ApplyOverrides(ISet pressed, ISet released, GameP mouseOverrides[button] = newState; else if (button.TryGetKeyboard(out Keys _)) keyboardOverrides[button] = newState; - else if (controller.IsConnected && button.TryGetController(out Buttons _)) + else if (button.TryGetController(out Buttons _)) controllerOverrides[button] = newState; } // override states if (keyboardOverrides.Any()) keyboard.OverrideButtons(keyboardOverrides); - if (controller.IsConnected && controllerOverrides.Any()) + if (controllerOverrides.Any()) controller.OverrideButtons(controllerOverrides); if (mouseOverrides.Any()) mouse.OverrideButtons(mouseOverrides);