11using System ;
22using System . Collections . Generic ;
3- using System . Linq ;
43using StardewModdingAPI . Framework . Input ;
54
65namespace StardewModdingAPI . Events ;
76
87/// <summary>Event arguments when any buttons were pressed or released.</summary>
98public 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