1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace StardewModdingAPI.Framework.Input
{
/// <summary>Manages controller state.</summary>
internal class GamePadStateBuilder : IInputStateBuilder<GamePadStateBuilder, GamePadState>
{
/*********
** Fields
*********/
/// <summary>The maximum direction to ignore for the left thumbstick.</summary>
private const float LeftThumbstickDeadZone = 0.2f;
/// <summary>The maximum direction to ignore for the right thumbstick.</summary>
private const float RightThumbstickDeadZone = 0.9f;
/// <summary>The underlying controller state.</summary>
private GamePadState? State;
/// <summary>The current button states.</summary>
private readonly IDictionary<SButton, ButtonState> ButtonStates;
/// <summary>The left trigger value.</summary>
private float LeftTrigger;
/// <summary>The right trigger value.</summary>
private float RightTrigger;
/// <summary>The left thumbstick position.</summary>
private Vector2 LeftStickPos;
/// <summary>The left thumbstick position.</summary>
private Vector2 RightStickPos;
/*********
** Accessors
*********/
/// <summary>Whether the gamepad is currently connected.</summary>
public bool IsConnected { get; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="state">The initial state.</param>
public GamePadStateBuilder(GamePadState state)
{
this.State = state;
this.IsConnected = state.IsConnected;
if (!this.IsConnected)
return;
GamePadDPad pad = state.DPad;
GamePadButtons buttons = state.Buttons;
GamePadTriggers triggers = state.Triggers;
GamePadThumbSticks sticks = state.ThumbSticks;
this.ButtonStates = new Dictionary<SButton, ButtonState>
{
[SButton.DPadUp] = pad.Up,
[SButton.DPadDown] = pad.Down,
[SButton.DPadLeft] = pad.Left,
[SButton.DPadRight] = pad.Right,
[SButton.ControllerA] = buttons.A,
[SButton.ControllerB] = buttons.B,
[SButton.ControllerX] = buttons.X,
[SButton.ControllerY] = buttons.Y,
[SButton.LeftStick] = buttons.LeftStick,
[SButton.RightStick] = buttons.RightStick,
[SButton.LeftShoulder] = buttons.LeftShoulder,
[SButton.RightShoulder] = buttons.RightShoulder,
[SButton.ControllerBack] = buttons.Back,
[SButton.ControllerStart] = buttons.Start,
[SButton.BigButton] = buttons.BigButton
};
this.LeftTrigger = triggers.Left;
this.RightTrigger = triggers.Right;
this.LeftStickPos = sticks.Left;
this.RightStickPos = sticks.Right;
}
/// <summary>Override the states for a set of buttons.</summary>
/// <param name="overrides">The button state overrides.</param>
public GamePadStateBuilder OverrideButtons(IDictionary<SButton, SButtonState> overrides)
{
if (!this.IsConnected)
return this;
foreach (var pair in overrides)
{
bool changed = true;
bool isDown = pair.Value.IsDown();
switch (pair.Key)
{
// left thumbstick
case SButton.LeftThumbstickUp:
this.LeftStickPos.Y = isDown ? 1 : 0;
break;
case SButton.LeftThumbstickDown:
this.LeftStickPos.Y = isDown ? 1 : 0;
break;
case SButton.LeftThumbstickLeft:
this.LeftStickPos.X = isDown ? 1 : 0;
break;
case SButton.LeftThumbstickRight:
this.LeftStickPos.X = isDown ? 1 : 0;
break;
// right thumbstick
case SButton.RightThumbstickUp:
this.RightStickPos.Y = isDown ? 1 : 0;
break;
case SButton.RightThumbstickDown:
this.RightStickPos.Y = isDown ? 1 : 0;
break;
case SButton.RightThumbstickLeft:
this.RightStickPos.X = isDown ? 1 : 0;
break;
case SButton.RightThumbstickRight:
this.RightStickPos.X = isDown ? 1 : 0;
break;
// triggers
case SButton.LeftTrigger:
this.LeftTrigger = isDown ? 1 : 0;
break;
case SButton.RightTrigger:
this.RightTrigger = isDown ? 1 : 0;
break;
// buttons
default:
if (this.ButtonStates.ContainsKey(pair.Key))
this.ButtonStates[pair.Key] = isDown ? ButtonState.Pressed : ButtonState.Released;
else
changed = false;
break;
}
if (changed)
this.State = null;
}
return this;
}
/// <summary>Get the currently pressed buttons.</summary>
public IEnumerable<SButton> GetPressedButtons()
{
if (!this.IsConnected)
yield break;
// buttons
foreach (var pair in this.ButtonStates)
{
if (pair.Value == ButtonState.Pressed && pair.Key.TryGetController(out Buttons button))
yield return button.ToSButton();
}
// triggers
if (this.LeftTrigger > 0.2f)
yield return SButton.LeftTrigger;
if (this.RightTrigger > 0.2f)
yield return SButton.RightTrigger;
// left thumbstick direction
if (this.LeftStickPos.Y > GamePadStateBuilder.LeftThumbstickDeadZone)
yield return SButton.LeftThumbstickUp;
if (this.LeftStickPos.Y < -GamePadStateBuilder.LeftThumbstickDeadZone)
yield return SButton.LeftThumbstickDown;
if (this.LeftStickPos.X > GamePadStateBuilder.LeftThumbstickDeadZone)
yield return SButton.LeftThumbstickRight;
if (this.LeftStickPos.X < -GamePadStateBuilder.LeftThumbstickDeadZone)
yield return SButton.LeftThumbstickLeft;
// right thumbstick direction
if (this.RightStickPos.Length() > GamePadStateBuilder.RightThumbstickDeadZone)
{
if (this.RightStickPos.Y > 0)
yield return SButton.RightThumbstickUp;
if (this.RightStickPos.Y < 0)
yield return SButton.RightThumbstickDown;
if (this.RightStickPos.X > 0)
yield return SButton.RightThumbstickRight;
if (this.RightStickPos.X < 0)
yield return SButton.RightThumbstickLeft;
}
}
/// <summary>Get the equivalent state.</summary>
public GamePadState GetState()
{
this.State ??= new GamePadState(
leftThumbStick: this.LeftStickPos,
rightThumbStick: this.RightStickPos,
leftTrigger: this.LeftTrigger,
rightTrigger: this.RightTrigger,
buttons: this.GetButtonBitmask() // MonoGame requires one bitmask here; don't specify multiple values
);
return this.State.Value;
}
/*********
** Private methods
*********/
/// <summary>Get a bitmask representing the pressed buttons.</summary>
private Buttons GetButtonBitmask()
{
Buttons flag = 0;
foreach (var pair in this.ButtonStates)
{
if (pair.Value == ButtonState.Pressed && pair.Key.TryGetController(out Buttons button))
flag |= button;
}
return flag;
}
}
}
|