1
- using System ;
2
- using System . Collections . Generic ;
3
- using System . Linq ;
4
- using Farmhand . Events ;
5
- using Farmhand . Events . Arguments . ControlEvents ;
6
- using Microsoft . Xna . Framework . Input ;
1
+ namespace Farmhand . UI
2
+ {
3
+ using System ;
4
+ using System . Collections . Generic ;
5
+ using System . Linq ;
7
6
8
- using StardewValley ;
9
- using StardewValley . Menus ;
7
+ using Farmhand . Events ;
8
+ using Farmhand . Events . Arguments . ControlEvents ;
9
+
10
+ using Microsoft . Xna . Framework . Input ;
11
+
12
+ using StardewValley ;
13
+ using StardewValley . Menus ;
10
14
11
- namespace Farmhand . UI
12
- {
13
15
/// <summary>
14
- /// Enables overlaying any <see cref="IClickableMenu"/> over the Gui , both in and outside of menus
16
+ /// Enables overlaying any <see cref="IClickableMenu" /> over the GUI , both in and outside of menus
15
17
/// </summary>
16
18
public static class OverlayManager
17
19
{
20
+ private static readonly List < IClickableMenu > Overlays = new List < IClickableMenu > ( ) ;
21
+
18
22
/// <summary>
19
- /// Adds the given overlay to the list of <see cref="IClickableMenu"/>'s that should receive emulated events
23
+ /// Count of how many <see cref="IClickableMenu" />'s are currently in the list
20
24
/// </summary>
21
- /// <param name="overlay">The <see cref="IClickableMenu"/> to add</param>
25
+ public static int Count => Overlays . Count ;
26
+
27
+ /// <summary>
28
+ /// Adds the given overlay to the list of <see cref="IClickableMenu" />'s that should receive emulated events
29
+ /// </summary>
30
+ /// <param name="overlay">The <see cref="IClickableMenu" /> to add</param>
22
31
public static void Add ( IClickableMenu overlay )
23
32
{
24
33
Overlays . Add ( overlay ) ;
25
34
if ( Overlays . Count == 1 )
35
+ {
26
36
Init ( ) ;
37
+ }
27
38
}
39
+
28
40
/// <summary>
29
- /// Removes the given overlay from the list of <see cref="IClickableMenu"/>'s that should receive emulated events
41
+ /// Removes the given overlay from the list of <see cref="IClickableMenu" />'s that should receive emulated events
30
42
/// </summary>
31
- /// <param name="overlay">The <see cref="IClickableMenu"/> to remove</param>
43
+ /// <param name="overlay">The <see cref="IClickableMenu" /> to remove</param>
32
44
public static void Remove ( IClickableMenu overlay )
33
45
{
34
46
Overlays . Remove ( overlay ) ;
35
47
if ( Overlays . Count == 0 )
48
+ {
36
49
Uninit ( ) ;
50
+ }
37
51
}
52
+
38
53
/// <summary>
39
- /// Empties the list of <see cref="IClickableMenu"/>'s that should receive emulated events
54
+ /// Empties the list of <see cref="IClickableMenu" />'s that should receive emulated events
40
55
/// </summary>
41
56
public static void Clear ( )
42
57
{
43
58
Overlays . Clear ( ) ;
44
59
Uninit ( ) ;
45
60
}
61
+
46
62
/// <summary>
47
- /// Checks if a given <see cref="IClickableMenu"/> is in the list of emulated <see cref="IClickableMenu"/>'s
63
+ /// Checks if a given <see cref="IClickableMenu" /> is in the list of emulated <see cref="IClickableMenu" />'s
48
64
/// </summary>
49
- /// <param name="overlay">The <see cref="IClickableMenu"/> to check for</param>
50
- /// <returns></returns>
65
+ /// <param name="overlay">The <see cref="IClickableMenu" /> to check for</param>
66
+ /// <returns>
67
+ /// Whether the manager contains the specified overlay
68
+ /// </returns>
51
69
public static bool Contains ( IClickableMenu overlay )
52
70
{
53
71
return Overlays . Contains ( overlay ) ;
54
72
}
55
- /// <summary>
56
- /// Count of how many <see cref="IClickableMenu"/>'s are currently in the list
57
- /// </summary>
58
- public static int Count => Overlays . Count ;
59
73
60
74
/// <summary>
61
- /// Tries to remove the given <see cref="IClickableMenu"/> from the list and returns a <see cref="bool"/> with the result of this attempt
75
+ /// Tries to remove the given <see cref="IClickableMenu" /> from the list and returns a <see cref="bool" /> with the
76
+ /// result of this attempt
62
77
/// </summary>
63
- /// <param name="overlay">The <see cref="IClickableMenu"/> to remove</param>
78
+ /// <param name="overlay">The <see cref="IClickableMenu" /> to remove</param>
64
79
/// <returns>Returns true if the given overlay was removed successfully, or false otherwise</returns>
65
80
public static bool TryRemove ( IClickableMenu overlay )
66
81
{
67
- bool a = Contains ( overlay ) ;
82
+ var a = Contains ( overlay ) ;
68
83
if ( a )
84
+ {
69
85
Remove ( overlay ) ;
86
+ }
87
+
70
88
return a ;
71
89
}
72
- // Internal functional code, not related to API level code.
73
- private static readonly List < IClickableMenu > Overlays = new List < IClickableMenu > ( ) ;
90
+
74
91
private static void Init ( )
75
92
{
76
93
GameEvents . OnAfterUpdateTick += GameEvents_UpdateTick ;
77
94
GraphicsEvents . OnPostRenderGuiEvent += GraphicsEvents_OnPostRenderGuiEvent ;
78
95
ControlEvents . OnMouseChanged += ControlEvents_MouseChanged ;
79
96
ControlEvents . OnKeyboardChanged += ControlEvents_KeyboardChanged ;
80
97
}
98
+
81
99
private static void Uninit ( )
82
100
{
83
101
GameEvents . OnAfterUpdateTick -= GameEvents_UpdateTick ;
84
102
GraphicsEvents . OnPostRenderGuiEvent -= GraphicsEvents_OnPostRenderGuiEvent ;
85
103
ControlEvents . OnMouseChanged -= ControlEvents_MouseChanged ;
86
104
ControlEvents . OnKeyboardChanged -= ControlEvents_KeyboardChanged ;
87
105
}
106
+
88
107
internal static void GameEvents_UpdateTick ( object s , EventArgs e )
89
108
{
90
- foreach ( IClickableMenu overlay in Overlays )
109
+ foreach ( var overlay in Overlays )
91
110
{
92
111
overlay . update ( Game1 . currentGameTime ) ;
93
112
overlay . performHoverAction ( Game1 . getMouseX ( ) , Game1 . getMouseY ( ) ) ;
94
113
}
95
114
}
115
+
96
116
internal static void GraphicsEvents_OnPostRenderGuiEvent ( object s , EventArgs e )
97
117
{
98
- foreach ( IClickableMenu overlay in Overlays )
118
+ foreach ( var overlay in Overlays )
119
+ {
99
120
overlay . draw ( Game1 . spriteBatch ) ;
121
+ }
100
122
}
123
+
101
124
internal static void ControlEvents_MouseChanged ( object s , EventArgsMouseStateChanged e )
102
125
{
103
126
if ( e . NewState . LeftButton == ButtonState . Pressed && e . PriorState . LeftButton == ButtonState . Released )
104
- foreach ( IClickableMenu overlay in Overlays )
127
+ {
128
+ foreach ( var overlay in Overlays )
129
+ {
105
130
overlay . receiveLeftClick ( Game1 . getMouseX ( ) , Game1 . getMouseY ( ) ) ;
131
+ }
132
+ }
133
+
106
134
if ( e . NewState . RightButton == ButtonState . Pressed && e . PriorState . RightButton == ButtonState . Released )
107
- foreach ( IClickableMenu overlay in Overlays )
135
+ {
136
+ foreach ( var overlay in Overlays )
137
+ {
108
138
overlay . receiveRightClick ( Game1 . getMouseX ( ) , Game1 . getMouseY ( ) ) ;
139
+ }
140
+ }
141
+
109
142
if ( e . NewState . ScrollWheelValue != e . PriorState . ScrollWheelValue )
110
- foreach ( IClickableMenu overlay in Overlays )
143
+ {
144
+ foreach ( var overlay in Overlays )
145
+ {
111
146
overlay . receiveScrollWheelAction ( e . NewState . ScrollWheelValue - e . PriorState . ScrollWheelValue ) ;
147
+ }
148
+ }
149
+
112
150
if ( e . NewState . RightButton == ButtonState . Released && e . PriorState . RightButton == ButtonState . Pressed )
113
- foreach ( IClickableMenu overlay in Overlays )
151
+ {
152
+ foreach ( var overlay in Overlays )
153
+ {
114
154
overlay . releaseLeftClick ( Game1 . getMouseX ( ) , Game1 . getMouseY ( ) ) ;
155
+ }
156
+ }
157
+
115
158
if ( e . NewState . RightButton == ButtonState . Pressed && e . PriorState . RightButton == ButtonState . Pressed )
116
- foreach ( IClickableMenu overlay in Overlays )
159
+ {
160
+ foreach ( var overlay in Overlays )
161
+ {
117
162
overlay . leftClickHeld ( Game1 . getMouseX ( ) , Game1 . getMouseY ( ) ) ;
163
+ }
164
+ }
118
165
}
166
+
119
167
internal static void ControlEvents_KeyboardChanged ( object s , EventArgsKeyboardStateChanged e )
120
168
{
121
169
IEnumerable < Keys > old = e . PriorState . GetPressedKeys ( ) ;
122
- foreach ( Keys key in e . NewState . GetPressedKeys ( ) )
170
+ foreach ( var key in e . NewState . GetPressedKeys ( ) )
171
+ {
123
172
if ( ! old . Contains ( key ) )
124
- foreach ( IClickableMenu overlay in Overlays )
173
+ {
174
+ foreach ( var overlay in Overlays )
175
+ {
125
176
overlay . receiveKeyPress ( key ) ;
177
+ }
178
+ }
179
+ }
126
180
}
127
181
}
128
- }
182
+ }
0 commit comments