diff --git a/src/SMAPI/Framework/Input/GamePadStateBuilder.cs b/src/SMAPI/Framework/Input/GamePadStateBuilder.cs
index e5ca1b1c..f1500ea5 100644
--- a/src/SMAPI/Framework/Input/GamePadStateBuilder.cs
+++ b/src/SMAPI/Framework/Input/GamePadStateBuilder.cs
@@ -145,54 +145,52 @@ internal class GamePadStateBuilder : IInputStateBuilder
- public IEnumerable GetPressedButtons()
+ public void FillPressedButtons(HashSet set)
{
// buttons
foreach (Buttons button in this.GetPressedGamePadButtons())
- yield return button.ToSButton();
+ set.Add(button.ToSButton());
// triggers
if (this.LeftTrigger > 0.2f)
- yield return SButton.LeftTrigger;
+ set.Add(SButton.LeftTrigger);
if (this.RightTrigger > 0.2f)
- yield return SButton.RightTrigger;
+ set.Add(SButton.RightTrigger);
// left thumbstick direction
if (this.LeftStickPos.Y > GamePadStateBuilder.LeftThumbstickDeadZone)
- yield return SButton.LeftThumbstickUp;
+ set.Add(SButton.LeftThumbstickUp);
if (this.LeftStickPos.Y < -GamePadStateBuilder.LeftThumbstickDeadZone)
- yield return SButton.LeftThumbstickDown;
+ set.Add(SButton.LeftThumbstickDown);
if (this.LeftStickPos.X > GamePadStateBuilder.LeftThumbstickDeadZone)
- yield return SButton.LeftThumbstickRight;
+ set.Add(SButton.LeftThumbstickRight);
if (this.LeftStickPos.X < -GamePadStateBuilder.LeftThumbstickDeadZone)
- yield return SButton.LeftThumbstickLeft;
+ set.Add(SButton.LeftThumbstickLeft);
// right thumbstick direction
if (this.RightStickPos.Length() > GamePadStateBuilder.RightThumbstickDeadZone)
{
if (this.RightStickPos.Y > 0)
- yield return SButton.RightThumbstickUp;
+ set.Add(SButton.RightThumbstickUp);
if (this.RightStickPos.Y < 0)
- yield return SButton.RightThumbstickDown;
+ set.Add(SButton.RightThumbstickDown);
if (this.RightStickPos.X > 0)
- yield return SButton.RightThumbstickRight;
+ set.Add(SButton.RightThumbstickRight);
if (this.RightStickPos.X < 0)
- yield return SButton.RightThumbstickLeft;
+ set.Add(SButton.RightThumbstickLeft);
}
}
///
public GamePadState GetState()
{
- this.State ??= new GamePadState(
+ return this.State ??= new GamePadState(
leftThumbStick: this.LeftStickPos,
rightThumbStick: this.RightStickPos,
leftTrigger: this.LeftTrigger,
rightTrigger: this.RightTrigger,
buttons: this.GetPressedGamePadButtons().ToArray()
);
-
- return this.State.Value;
}
diff --git a/src/SMAPI/Framework/Input/IInputStateBuilder.cs b/src/SMAPI/Framework/Input/IInputStateBuilder.cs
index 221bffcc..367a4c64 100644
--- a/src/SMAPI/Framework/Input/IInputStateBuilder.cs
+++ b/src/SMAPI/Framework/Input/IInputStateBuilder.cs
@@ -20,8 +20,9 @@ internal interface IInputStateBuilder
/// The button state overrides.
THandler OverrideButtons(IDictionary overrides);
- /// Get the currently pressed buttons.
- IEnumerable GetPressedButtons();
+ /// Fill a set with the currently pressed buttons.
+ /// The set to populate with the pressed buttons.
+ void FillPressedButtons(HashSet set);
/// Get the equivalent state.
TState GetState();
diff --git a/src/SMAPI/Framework/Input/KeyboardStateBuilder.cs b/src/SMAPI/Framework/Input/KeyboardStateBuilder.cs
index 66e67f74..247e01a6 100644
--- a/src/SMAPI/Framework/Input/KeyboardStateBuilder.cs
+++ b/src/SMAPI/Framework/Input/KeyboardStateBuilder.cs
@@ -50,17 +50,15 @@ internal class KeyboardStateBuilder : IInputStateBuilder
- public IEnumerable GetPressedButtons()
+ public void FillPressedButtons(HashSet set)
{
foreach (Keys key in this.PressedButtons)
- yield return key.ToSButton();
+ set.Add(key.ToSButton());
}
///
public KeyboardState GetState()
{
- return
- this.State
- ?? (this.State = new KeyboardState(this.PressedButtons.ToArray())).Value;
+ return this.State ??= new KeyboardState(this.PressedButtons.ToArray());
}
}
diff --git a/src/SMAPI/Framework/Input/MouseStateBuilder.cs b/src/SMAPI/Framework/Input/MouseStateBuilder.cs
index 17bac8e8..ac27f67a 100644
--- a/src/SMAPI/Framework/Input/MouseStateBuilder.cs
+++ b/src/SMAPI/Framework/Input/MouseStateBuilder.cs
@@ -66,19 +66,19 @@ internal class MouseStateBuilder : IInputStateBuilder
- public IEnumerable GetPressedButtons()
+ public void FillPressedButtons(HashSet set)
{
foreach (var pair in this.ButtonStates)
{
if (pair.Value == ButtonState.Pressed)
- yield return pair.Key;
+ set.Add(pair.Key);
}
}
///
public MouseState GetState()
{
- this.State ??= new MouseState(
+ return this.State ??= new MouseState(
x: this.X,
y: this.Y,
scrollWheel: this.ScrollWheelValue,
@@ -88,7 +88,5 @@ internal class MouseStateBuilder : IInputStateBuilderThe builder which reads the mouse state and applies overrides.
private readonly MouseStateBuilder MouseStateBuilder = new();
+ /// The pooled cache set for in .
+ private readonly HashSet PooledPressedButtons = [];
+
/*********
** Accessors
@@ -82,21 +85,26 @@ internal sealed class SInputState : InputState
KeyboardStateBuilder keyboard = this.KeyboardStateBuilder;
MouseStateBuilder mouse = this.MouseStateBuilder;
+ // get pooled button set
+ HashSet pressedButtons = this.PooledPressedButtons;
+
// get real values
controller.Reset(base.GetGamePadState());
keyboard.Reset(base.GetKeyboardState());
mouse.Reset(base.GetMouseState());
Vector2 cursorAbsolutePos = new((mouse.X * zoomMultiplier) + Game1.viewport.X, (mouse.Y * zoomMultiplier) + Game1.viewport.Y);
Vector2? playerTilePos = Context.IsPlayerFree ? Game1.player.Tile : null;
- HashSet reallyDown = new(this.GetPressedButtons(keyboard, mouse, controller));
+
+ pressedButtons.Clear();
+ this.FillPressedButtons(pressedButtons, keyboard, mouse, controller);
// apply overrides
bool hasOverrides = false;
if (this.CustomPressedKeys.Count > 0 || this.CustomReleasedKeys.Count > 0)
{
// reset overrides that no longer apply
- this.CustomPressedKeys.ExceptWith(reallyDown);
- this.CustomReleasedKeys.IntersectWith(reallyDown);
+ this.CustomPressedKeys.ExceptWith(pressedButtons);
+ this.CustomReleasedKeys.IntersectWith(pressedButtons);
// apply overrides
if (this.ApplyOverrides(this.CustomPressedKeys, this.CustomReleasedKeys, controller, keyboard, mouse))
@@ -107,9 +115,11 @@ internal sealed class SInputState : InputState
}
// get button states
- var pressedButtons = hasOverrides
- ? new(this.GetPressedButtons(keyboard, mouse, controller))
- : reallyDown;
+ if (hasOverrides)
+ {
+ pressedButtons.Clear();
+ this.FillPressedButtons(pressedButtons, keyboard, mouse, controller);
+ }
var activeButtons = this.DeriveStates(this.ButtonStates, pressedButtons);
// update
@@ -315,15 +325,15 @@ internal sealed class SInputState : InputState
}
/// Get the buttons pressed in the given stats.
+ /// The set to populate with pressed buttons.
/// The keyboard state.
/// The mouse state.
/// The controller state.
/// Thumbstick direction logic derived from .
- private IEnumerable GetPressedButtons(KeyboardStateBuilder keyboard, MouseStateBuilder mouse, GamePadStateBuilder controller)
+ private void FillPressedButtons(HashSet set, KeyboardStateBuilder keyboard, MouseStateBuilder mouse, GamePadStateBuilder controller)
{
- return keyboard
- .GetPressedButtons()
- .Concat(mouse.GetPressedButtons())
- .Concat(controller.GetPressedButtons());
+ keyboard.FillPressedButtons(set);
+ mouse.FillPressedButtons(set);
+ controller.FillPressedButtons(set);
}
}