-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathXbox360Controller.cs
350 lines (266 loc) · 9.31 KB
/
Xbox360Controller.cs
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#region usings
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Globalization;
using VVVV.PluginInterfaces.V2;
using SlimDX;
using SlimDX.XInput;
using VVVV.Core.Logging;
#endregion usings
[PluginInfo(Name = "Xbox360Controller", Category = "Devices", Version = "1.0.0", Help = "Allows you to use XBox360 Controller", Tags = "joystick xbox")]
public class Xbox360Controller : IPluginEvaluate, IPartImportsSatisfiedNotification
{
#region fields & pins
private const string ControllersEnumName = "XBox360Controllers";
[Input("Device", EnumName = ControllersEnumName)]
IDiffSpread<EnumEntry> FGamePadsEnumInput;
[Input("Refresh", IsBang = true, IsSingle = true)]
IDiffSpread<bool> FRefreshInput;
[Output("Left Thumbstick")]
ISpread<Vector2> FLeftThumbPin;
[Output("Left Thumbstick Pressed")]
ISpread<bool> FLeftThumbPressPin;
[Output("Right Thumbstick")]
ISpread<Vector2> FRightThumbPin;
[Output("Right Thumbstick Pressed")]
ISpread<bool> FRightThumbPressPin;
[Output("Left Trigger")]
ISpread<double> FLeftTriggerPin;
[Output("Left Shoulder Button")]
ISpread<bool> FLeftShoulderPin;
[Output("Right Trigger")]
ISpread<double> FRightTriggerPin;
[Output("Right Shoulder Button")]
ISpread<bool> FRightShoulderPin;
[Output("A Button")]
ISpread<bool> FAButtonPin;
[Output("B Button")]
ISpread<bool> FBButtonPin;
[Output("X Button")]
ISpread<bool> FXButtonPin;
[Output("Y Button")]
ISpread<bool> FYButtonPin;
[Output("D-Pad Up")]
ISpread<bool> FDpadUpPin;
[Output("D-Pad Down")]
ISpread<bool> FDpadDownPin;
[Output("D-Pad Left")]
ISpread<bool> FDpadLeftPin;
[Output("D-Pad Right")]
ISpread<bool> FDpadRightPin;
[Output("Start")]
ISpread<bool> FStartPin;
[Output("Back")]
ISpread<bool> FBackPin;
[Import]
ILogger Flogger;
private readonly List<GamepadState> FAllGamepads = new List<GamepadState>(4);
private readonly List<GamepadState> FConnectedGamepads = new List<GamepadState>(4);
private readonly List<GamepadState> FSelectedGamepads = new List<GamepadState>(4);
private int FGamepadsCount;
#endregion fields & pins
public void OnImportsSatisfied()
{
for (int i = 0; i < 4; i++)
{
var gamepad = new GamepadState((UserIndex)i);
FAllGamepads.Add(gamepad);
}
CheckConnectedDevices();
}
private void CheckConnectedDevices()
{
FConnectedGamepads.Clear();
for (int i = 0; i < 4; i++)
{
if(FAllGamepads[i].Connected)
{
FConnectedGamepads.Add(FAllGamepads[i]);
}
}
string[] names;
if(FConnectedGamepads.Count == 0)
{
names = new string[1];
names[0] = "(nil)";
EnumManager.UpdateEnum(ControllersEnumName, names[0], names);
return;
}
names = new string[FConnectedGamepads.Count];
for (int j = 0; j < FConnectedGamepads.Count; j++)
{
names[j] = "XBOX 360 Controller Player " + FConnectedGamepads[j].UserIndex + " #" + j.ToString(CultureInfo.InvariantCulture);
}
EnumManager.UpdateEnum(ControllersEnumName, names[0], names);
InitControllers();
}
private void InitControllers()
{
FSelectedGamepads.Clear();
for (int i = 0; i < FGamePadsEnumInput.SliceCount; i++)
{
EnumEntry entry = FGamePadsEnumInput[i];
if(entry == "(nil)" || entry.Name == null) continue;
int sliceIndex = int.Parse(entry.Name.Split('#')[1]);
if(sliceIndex >= FConnectedGamepads.Count) continue;
FSelectedGamepads.Add(FConnectedGamepads[sliceIndex]);
}
FGamepadsCount = FSelectedGamepads.Count;
}
//called each frame by vvvv
public void Evaluate(int spreadMax)
{
if (FRefreshInput[0]) CheckConnectedDevices();
if (FGamePadsEnumInput.IsChanged) InitControllers();
FLeftThumbPin.SliceCount = FGamepadsCount;
FLeftThumbPressPin.SliceCount = FGamepadsCount;
FRightThumbPin.SliceCount = FGamepadsCount;
FRightThumbPressPin.SliceCount = FGamepadsCount;
FLeftTriggerPin.SliceCount = FGamepadsCount;
FLeftShoulderPin.SliceCount = FGamepadsCount;
FRightTriggerPin.SliceCount = FGamepadsCount;
FRightShoulderPin.SliceCount = FGamepadsCount;
FAButtonPin.SliceCount = FGamepadsCount;
FBButtonPin.SliceCount = FGamepadsCount;
FXButtonPin.SliceCount = FGamepadsCount;
FYButtonPin.SliceCount = FGamepadsCount;
FDpadUpPin.SliceCount = FGamepadsCount;
FDpadDownPin.SliceCount = FGamepadsCount;
FDpadLeftPin.SliceCount = FGamepadsCount;
FDpadRightPin.SliceCount = FGamepadsCount;
FStartPin.SliceCount = FGamepadsCount;
FBackPin.SliceCount = FGamepadsCount;
for (int i = 0; i < FGamepadsCount; i++)
{
FSelectedGamepads[i].Update();
FLeftThumbPin[i] = FSelectedGamepads[i].LeftStick.Position;
FLeftThumbPressPin[i] = FSelectedGamepads[i].LeftStick.Clicked;
FRightThumbPin[i] = FSelectedGamepads[i].RightStick.Position;
FRightThumbPressPin[i] = FSelectedGamepads[i].RightStick.Clicked;
FLeftTriggerPin[i] = FSelectedGamepads[i].LeftTrigger;
FLeftShoulderPin[i] = FSelectedGamepads[i].LeftShoulder;
FRightTriggerPin[i] = FSelectedGamepads[i].RightTrigger;
FRightShoulderPin[i] = FSelectedGamepads[i].RightShoulder;
FAButtonPin[i] = FSelectedGamepads[i].A;
FBButtonPin[i] = FSelectedGamepads[i].B;
FXButtonPin[i] = FSelectedGamepads[i].X;
FYButtonPin[i] = FSelectedGamepads[i].Y;
FDpadUpPin[i] = FSelectedGamepads[i].DPad.Up;
FDpadDownPin[i] = FSelectedGamepads[i].DPad.Down;
FDpadLeftPin[i] = FSelectedGamepads[i].DPad.Left;
FDpadRightPin[i] = FSelectedGamepads[i].DPad.Right;
FStartPin[i] = FSelectedGamepads[i].Start;
FBackPin[i] = FSelectedGamepads[i].Back;
}
}
}
// SlimDX Wrapper developed by
// Zaknafein/Renaud Bédard of The Instruction Limit
// http://theinstructionlimit.com
public class GamepadState
{
uint FLastPacket;
public GamepadState(UserIndex userIndex)
{
UserIndex = userIndex;
Controller = new Controller(userIndex);
}
public readonly UserIndex UserIndex;
public readonly Controller Controller;
public DPadState DPad { get; private set; }
public ThumbstickState LeftStick { get; private set; }
public ThumbstickState RightStick { get; private set; }
public bool A { get; private set; }
public bool B { get; private set; }
public bool X { get; private set; }
public bool Y { get; private set; }
public bool RightShoulder { get; private set; }
public bool LeftShoulder { get; private set; }
public bool Start { get; private set; }
public bool Back { get; private set; }
public float RightTrigger { get; private set; }
public float LeftTrigger { get; private set; }
public bool Connected
{
get { return Controller.IsConnected; }
}
public void Vibrate(float leftMotor, float rightMotor)
{
Controller.SetVibration(new Vibration
{
LeftMotorSpeed = (ushort)(MathHelper.Saturate(leftMotor) * ushort.MaxValue),
RightMotorSpeed = (ushort)(MathHelper.Saturate(rightMotor) * ushort.MaxValue)
});
}
public void Update()
{
// If not connected, nothing to update
if (!Connected) return;
// If same packet, nothing to update
State state = Controller.GetState();
if (FLastPacket == state.PacketNumber) return;
FLastPacket = state.PacketNumber;
var gamepadState = state.Gamepad;
// Shoulders
LeftShoulder = (gamepadState.Buttons & GamepadButtonFlags.LeftShoulder) != 0;
RightShoulder = (gamepadState.Buttons & GamepadButtonFlags.RightShoulder) != 0;
// Triggers
LeftTrigger = gamepadState.LeftTrigger / (float)byte.MaxValue;
RightTrigger = gamepadState.RightTrigger / (float)byte.MaxValue;
// Buttons
Start = (gamepadState.Buttons & GamepadButtonFlags.Start) != 0;
Back = (gamepadState.Buttons & GamepadButtonFlags.Back) != 0;
A = (gamepadState.Buttons & GamepadButtonFlags.A) != 0;
B = (gamepadState.Buttons & GamepadButtonFlags.B) != 0;
X = (gamepadState.Buttons & GamepadButtonFlags.X) != 0;
Y = (gamepadState.Buttons & GamepadButtonFlags.Y) != 0;
// D-Pad
DPad = new DPadState((gamepadState.Buttons & GamepadButtonFlags.DPadUp) != 0,
(gamepadState.Buttons & GamepadButtonFlags.DPadDown) != 0,
(gamepadState.Buttons & GamepadButtonFlags.DPadLeft) != 0,
(gamepadState.Buttons & GamepadButtonFlags.DPadRight) != 0);
// Thumbsticks
LeftStick = new ThumbstickState(
Normalize(gamepadState.LeftThumbX, gamepadState.LeftThumbY, Gamepad.GamepadLeftThumbDeadZone),
(gamepadState.Buttons & GamepadButtonFlags.LeftThumb) != 0);
RightStick = new ThumbstickState(
Normalize(gamepadState.RightThumbX, gamepadState.RightThumbY, Gamepad.GamepadRightThumbDeadZone),
(gamepadState.Buttons & GamepadButtonFlags.RightThumb) != 0);
}
static Vector2 Normalize(short rawX, short rawY, short threshold)
{
var value = new Vector2(rawX, rawY);
var magnitude = value.Length();
var direction = value / (magnitude == 0 ? 1 : magnitude);
var normalizedMagnitude = 0.0f;
if (magnitude - threshold > 0)
normalizedMagnitude = Math.Min((magnitude - threshold) / (short.MaxValue - threshold), 1);
return direction * normalizedMagnitude;
}
public struct DPadState
{
public readonly bool Up, Down, Left, Right;
public DPadState(bool up, bool down, bool left, bool right)
{
Up = up; Down = down; Left = left; Right = right;
}
}
public struct ThumbstickState
{
public readonly Vector2 Position;
public readonly bool Clicked;
public ThumbstickState(Vector2 position, bool clicked)
{
Clicked = clicked;
Position = position;
}
}
}
public static class MathHelper
{
public static float Saturate(float value)
{
return value < 0 ? 0 : value > 1 ? 1 : value;
}
}