From 4adf8611131a5d86b15f017a42a0366837d14528 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 13 Apr 2022 21:07:43 -0400 Subject: enable nullable annotations in the rest of SMAPI core (#837) --- src/SMAPI/Framework/Input/GamePadStateBuilder.cs | 9 ++++++--- src/SMAPI/Framework/Input/SInputState.cs | 6 ++---- 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'src/SMAPI/Framework/Input') diff --git a/src/SMAPI/Framework/Input/GamePadStateBuilder.cs b/src/SMAPI/Framework/Input/GamePadStateBuilder.cs index 21168b7a..4ac3332c 100644 --- a/src/SMAPI/Framework/Input/GamePadStateBuilder.cs +++ b/src/SMAPI/Framework/Input/GamePadStateBuilder.cs @@ -1,6 +1,5 @@ -#nullable disable - using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; @@ -23,7 +22,7 @@ namespace StardewModdingAPI.Framework.Input private GamePadState? State; /// The current button states. - private readonly IDictionary ButtonStates; + private readonly IDictionary? ButtonStates; /// The left trigger value. private float LeftTrigger; @@ -42,6 +41,7 @@ namespace StardewModdingAPI.Framework.Input ** Accessors *********/ /// Whether the gamepad is currently connected. + [MemberNotNullWhen(true, nameof(GamePadStateBuilder.ButtonStates))] public bool IsConnected { get; } @@ -213,6 +213,9 @@ namespace StardewModdingAPI.Framework.Input /// 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 37b3c8ef..fef83af7 100644 --- a/src/SMAPI/Framework/Input/SInputState.cs +++ b/src/SMAPI/Framework/Input/SInputState.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Linq; @@ -17,7 +15,7 @@ namespace StardewModdingAPI.Framework.Input ** Accessors *********/ /// The cursor position on the screen adjusted for the zoom level. - private CursorPosition CursorPositionImpl; + private CursorPosition CursorPositionImpl = new(Vector2.Zero, Vector2.Zero, Vector2.Zero, Vector2.Zero); /// The player's last known tile position. private Vector2? LastPlayerTile; @@ -106,7 +104,7 @@ namespace StardewModdingAPI.Framework.Input this.KeyboardState = keyboard.GetState(); this.MouseState = mouse.GetState(); this.ButtonStates = activeButtons; - if (cursorAbsolutePos != this.CursorPositionImpl?.AbsolutePixels || playerTilePos != this.LastPlayerTile) + if (cursorAbsolutePos != this.CursorPositionImpl.AbsolutePixels || playerTilePos != this.LastPlayerTile) { this.LastPlayerTile = playerTilePos; this.CursorPositionImpl = this.GetCursorPosition(this.MouseState, cursorAbsolutePos, zoomMultiplier); -- cgit