Skip to content

Commit d9c62b9

Browse files
committed
optimize ButtonsChanged event args
This commit... - removes the Lazy<T>, which was unnecessary since mods will virtually always check which buttons were changed; - avoids a dictionary lookup on each property access; - and avoids allocating LINQ enumerators, temporary array, and dictionary on init.
1 parent bc6d7e9 commit d9c62b9

1 file changed

Lines changed: 25 additions & 31 deletions

File tree

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using StardewModdingAPI.Framework.Input;
54

65
namespace StardewModdingAPI.Events;
76

87
/// <summary>Event arguments when any buttons were pressed or released.</summary>
98
public class ButtonsChangedEventArgs : EventArgs
109
{
11-
/*********
12-
** Fields
13-
*********/
14-
/// <summary>The buttons that were pressed, held, or released since the previous tick.</summary>
15-
private readonly Lazy<Dictionary<SButtonState, SButton[]>> ButtonsByState;
16-
17-
1810
/*********
1911
** Accessors
2012
*********/
2113
/// <summary>The current cursor position.</summary>
2214
public ICursorPosition Cursor { get; }
2315

2416
/// <summary>The buttons which were pressed since the previous tick.</summary>
25-
public IEnumerable<SButton> Pressed => this.ButtonsByState.Value[SButtonState.Pressed];
17+
public IEnumerable<SButton> Pressed { get; }
2618

2719
/// <summary>The buttons which were held since the previous tick.</summary>
28-
public IEnumerable<SButton> Held => this.ButtonsByState.Value[SButtonState.Held];
20+
public IEnumerable<SButton> Held { get; }
2921

3022
/// <summary>The buttons which were released since the previous tick.</summary>
31-
public IEnumerable<SButton> Released => this.ButtonsByState.Value[SButtonState.Released];
23+
public IEnumerable<SButton> Released { get; }
3224

3325

3426
/*********
@@ -39,29 +31,31 @@ public class ButtonsChangedEventArgs : EventArgs
3931
/// <param name="inputState">The game's current input state.</param>
4032
internal ButtonsChangedEventArgs(ICursorPosition cursor, SInputState inputState)
4133
{
42-
this.Cursor = cursor;
43-
this.ButtonsByState = new Lazy<Dictionary<SButtonState, SButton[]>>(() => this.GetButtonsByState(inputState));
44-
}
45-
34+
var pressed = new List<SButton>();
35+
var held = new List<SButton>();
36+
var released = new List<SButton>();
4637

47-
/*********
48-
** Private methods
49-
*********/
50-
/// <summary>Get the buttons that were pressed, held, or released since the previous tick.</summary>
51-
/// <param name="inputState">The game's current input state.</param>
52-
private Dictionary<SButtonState, SButton[]> GetButtonsByState(SInputState inputState)
53-
{
54-
Dictionary<SButtonState, SButton[]> lookup = inputState
55-
.GetActiveButtonStates()
56-
.GroupBy(p => p.Value)
57-
.ToDictionary(p => p.Key, p => p.Select(p => p.Key).ToArray());
58-
59-
foreach (SButtonState state in new[] { SButtonState.Pressed, SButtonState.Held, SButtonState.Released })
38+
foreach ((SButton button, SButtonState state) in inputState.GetActiveButtonStates())
6039
{
61-
if (!lookup.ContainsKey(state))
62-
lookup[state] = [];
40+
switch (state)
41+
{
42+
case SButtonState.Pressed:
43+
pressed.Add(button);
44+
break;
45+
46+
case SButtonState.Held:
47+
held.Add(button);
48+
break;
49+
50+
case SButtonState.Released:
51+
released.Add(button);
52+
break;
53+
}
6354
}
6455

65-
return lookup;
56+
this.Cursor = cursor;
57+
this.Pressed = pressed;
58+
this.Held = held;
59+
this.Released = released;
6660
}
6761
}

0 commit comments

Comments
 (0)