avoid querying controller state if it's not connected

This commit is contained in:
Jesse Plamondon-Willard
2026-02-01 16:59:08 -05:00
parent 850e99cb45
commit 5fc8e806c2
@@ -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 : IInputStateBuilder<GamePadStateBuilder, Gam
private GamePadState? State;
/// <summary>The current button states.</summary>
private readonly IDictionary<SButton, ButtonState>? ButtonStates;
private readonly Dictionary<SButton, ButtonState> ButtonStates;
/// <summary>The left trigger value.</summary>
private float LeftTrigger;
@@ -41,7 +40,6 @@ internal class GamePadStateBuilder : IInputStateBuilder<GamePadStateBuilder, Gam
** Accessors
*********/
/// <summary>Whether the gamepad is currently connected.</summary>
[MemberNotNullWhen(true, nameof(GamePadStateBuilder.ButtonStates))]
public bool IsConnected { get; }
@@ -55,33 +53,38 @@ internal class GamePadStateBuilder : IInputStateBuilder<GamePadStateBuilder, Gam
this.State = state;
this.IsConnected = state.IsConnected;
GamePadDPad pad = state.DPad;
GamePadButtons buttons = state.Buttons;
GamePadTriggers triggers = state.Triggers;
GamePadThumbSticks sticks = state.ThumbSticks;
this.ButtonStates = new Dictionary<SButton, ButtonState>
if (this.IsConnected)
{
[SButton.DPadUp] = pad.Up,
[SButton.DPadDown] = pad.Down,
[SButton.DPadLeft] = pad.Left,
[SButton.DPadRight] = pad.Right,
GamePadDPad pad = state.DPad;
GamePadButtons buttons = state.Buttons;
GamePadTriggers triggers = state.Triggers;
GamePadThumbSticks sticks = state.ThumbSticks;
this.ButtonStates = new Dictionary<SButton, ButtonState>
{
[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;
[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 = [];
}
/// <inheritdoc />
@@ -132,10 +135,7 @@ internal class GamePadStateBuilder : IInputStateBuilder<GamePadStateBuilder, Gam
// 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;
}