-
Notifications
You must be signed in to change notification settings - Fork 3
/
FrmMain.cs
441 lines (395 loc) · 19.1 KB
/
FrmMain.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
using System;
using System.Windows.Forms;
using CarlsMidiTools; // http://www.franklins.net/dotnet.aspx
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Threading;
using System.Management;
namespace _PS360Drum
{
public delegate byte Byte_DrumPadDelegate(GuiDrumPad pad);
public delegate bool Bool_DrumPadDelegate(GuiDrumPad pad);
public delegate void Void_DrumPadByteDelegate(GuiDrumPad pad, byte velocity);
public partial class FrmMain : Form
{
ProgressBar[] referenceVelocity = new ProgressBar[ProDrumController.NUM_PADS];
public GuiLinker GuiLinker { get; private set; }
Int32 _lowerValues;
String defaultFilePath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "PS360Drum.p3s");
Random m_Random = new Random();
public MidiSender MidiSender { get; private set; }
private IDrumController m_DrumController;
private Serialize.Serializer m_Serializer;
public MultiNoteGui MultiNoteGui { get; private set; }
private GuiDrumDPad m_PrevDPadState = GuiDrumDPad.None;
public FrmMain()
{
InitializeComponent();
m_Serializer = new Serialize.Serializer(this);
MidiSender = new MidiSender(this);
// Lists MIDI devices
ddlMIDIDevice.DataSource = MidiSender.MidiDevices;
// Sets default file browser properties
openFileDialog.Filter = "PS360 Setting Files|*.p3s";
openFileDialog.DefaultExt = "*.p3s";
openFileDialog.InitialDirectory = Path.GetDirectoryName(Application.ExecutablePath);
openFileDialog.FileName = "PS360DrumSettings";
saveFileDialog.Filter = openFileDialog.Filter;
saveFileDialog.DefaultExt = openFileDialog.DefaultExt;
saveFileDialog.InitialDirectory = openFileDialog.InitialDirectory;
saveFileDialog.FileName = openFileDialog.FileName;
#region Gui link stuff
GuiLinker = new GuiLinker(this);
GuiLinker.AddDrum(GuiDrumPad.RedTom, nupRedTom, nupAdvNoteRedTom, chkBoostRedTom, chkAdvBoostRedTom, ddlRedTom, ddlAdvBoostRedTom);
GuiLinker.AddDrum(GuiDrumPad.YellowTom, nupYellowTom, nupAdvNoteYellowTom, chkBoostYellowTom, chkAdvBoostYellowTom, ddlYellowTom, ddlAdvBoostYellowTom);
GuiLinker.AddDrum(GuiDrumPad.YellowCymbal, nupYellowCymbal, nupAdvNoteYellowCymbal, chkBoostYellowCymbal, chkAdvBoostYellowCymbal, ddlYellowCymbal, ddlAdvBoostYellowCymbal);
GuiLinker.AddDrum(GuiDrumPad.BlueTom, nupBlueTom, nupAdvNoteBlueTom, chkBoostBlueTom, chkAdvBoostBlueTom, ddlBlueTom, ddlAdvBoostBlueTom);
GuiLinker.AddDrum(GuiDrumPad.BlueCymbal, nupBlueCymbal, nupAdvNoteBlueCymbal, chkBoostBlueCymbal, chkAdvBoostBlueCymbal, ddlBlueCymbal, ddlAdvBoostBlueCymbal);
GuiLinker.AddDrum(GuiDrumPad.GreenTom, nupGreenTom, nupAdvNoteGreenTom, chkBoostGreenTom, chkAdvBoostGreenTom, ddlGreenTom, ddlAdvBoostGreenTom);
GuiLinker.AddDrum(GuiDrumPad.GreenCymbal, nupGreenCymbal, nupAdvNoteGreenCymbal, chkBoostGreenCymbal, chkAdvBoostGreenCymbal, ddlGreenCymbal, ddlAdvBoostGreenCymbal);
GuiLinker.AddDrum(GuiDrumPad.PedalLeft, nupPedalLeft, nupAdvNotePedalLeft, chkBoostPedalLeft, chkAdvBoostPedalLeft, ddlPedalLeft, ddlAdvBoostPedalLeft);
GuiLinker.AddDrum(GuiDrumPad.PedalRight, nupPedalRight, nupAdvNotePedalRight, chkBoostPedalRight, chkAdvBoostPedalRight, ddlPedalRight, ddlAdvBoostPedalRight);
GuiLinker.AddButton(GuiDrumButton.BigButton, nupBigButton, nupAdvNoteBigButton, nupAdvVelBigButton, ddlAdvSwitchBigButton, chkBigButton);
GuiLinker.AddButton(GuiDrumButton.Circle, nupCircle, nupAdvNoteCircle, nupAdvVelCircle, ddlAdvSwitchCircle, chkCircle);
GuiLinker.AddButton(GuiDrumButton.Rectangle, nupRectangle, nupAdvNoteRectangle, nupAdvVelRectangle, ddlAdvSwitchRectangle, chkRectangle);
GuiLinker.AddButton(GuiDrumButton.Select, nupSelect, nupAdvNoteSelect, nupAdvVelSelect, ddlAdvSwitchSelect, chkSelect);
GuiLinker.AddButton(GuiDrumButton.Start, nupStart, nupAdvNoteStart, nupAdvVelStart, ddlAdvSwitchStart, chkStart);
GuiLinker.AddButton(GuiDrumButton.Triangle, nupTriangle, nupAdvNoteTriangle, nupAdvVelTriangle, ddlAdvSwitchTriangle, chkTriangle);
GuiLinker.AddButton(GuiDrumButton.X, nupX, nupAdvNoteX, nupAdvVelX, ddlAdvSwitchX, chkX);
GuiLinker.AddDPad(GuiDrumDPad.Up, nupDPadUp, nupAdvNoteDPadUp, nupAdvVelDPadUp, ddlAdvSwitchDPadUp, chkDPadUp);
GuiLinker.AddDPad(GuiDrumDPad.Down, nupDPadDown, nupAdvNoteDPadDown, nupAdvVelDPadDown, ddlAdvSwitchDPadDown, chkDPadDown);
GuiLinker.AddDPad(GuiDrumDPad.Right, nupDPadRight, nupAdvNoteDPadRight, nupAdvVelDPadRight, ddlAdvSwitchDPadRight, chkDPadRight);
GuiLinker.AddDPad(GuiDrumDPad.Left, nupDPadLeft, nupAdvNoteDPadLeft, nupAdvVelDPadLeft, ddlAdvSwitchDPadLeft, chkDPadLeft);
referenceVelocity[(int)GuiDrumPad.RedTom] = pbRedTom;
referenceVelocity[(int)GuiDrumPad.YellowTom] = pbYellowTom;
referenceVelocity[(int)GuiDrumPad.BlueTom] = pbBlueTom;
referenceVelocity[(int)GuiDrumPad.GreenTom] = pbGreenTom;
referenceVelocity[(int)GuiDrumPad.YellowCymbal] = pbYellowCymbal;
referenceVelocity[(int)GuiDrumPad.BlueCymbal] = pbBlueCymbal;
referenceVelocity[(int)GuiDrumPad.GreenCymbal] = pbGreenCymbal;
referenceVelocity[(int)GuiDrumPad.PedalLeft] = pbPedalLeft;
referenceVelocity[(int)GuiDrumPad.PedalRight] = pbPedalRight;
#endregion
#region MultiNoteGUi
MultiNoteGui = new MultiNoteGui(ddlMNvelCheck, nupMNvelCheck, ddlMNnote, nupMNnoteTo,
nupMNvelMult, nupMNvelAdd, btnMNAdd, btnMNRemove, lbMN);
#endregion
// Tries to load default settings or reverts to
if (File.Exists(defaultFilePath))
{
m_Serializer.Load(defaultFilePath);
}
else
{
m_Serializer.LoadDefaultSettings();
}
// Sets high priority and enables search for drums
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
ddlController.Items.Add(ControllerType.Xbox360_GHWT_GH5_Drum);
ddlController.Items.Add(ControllerType.Ps3_RockBandProDrum);
ddlController.SelectedIndex = 0;
FillUsbList();
}
private void ButtonPress(object sender, EventArgs e)
{
//Button b = sender as Button;
//byte vel = (byte)m_Random.Next(0, 255);
//MidiSender.TriggerNote((DrumPad)b.Tag, vel);
}
private void DrumButtonPressed(GuiDrumButton b)
{
if (InvokeRequired)
{
Invoke(new ButtonDelegate(DrumButtonPressed), new object[] { b });
}
else
{
GuiLinker.CheckboxButton(b, true);
MidiSender.SendNoteOn(GuiLinker.GetMidiNote(b), GuiLinker.GetButtonVelocity(b));
if (GuiLinker.GetButtonSwitchType(b) != SwitchType.KeyboardLike)
MidiSender.SendNoteOff(GuiLinker.GetMidiNote(b));
}
}
private void DrumButtonReleased(GuiDrumButton b)
{
if (InvokeRequired)
{
Invoke(new ButtonDelegate(DrumButtonReleased), new object[] { b });
}
else
{
GuiLinker.CheckboxButton(b, false);
if (GuiLinker.GetButtonSwitchType(b) == SwitchType.KeyboardLike)
MidiSender.SendNoteOff(GuiLinker.GetMidiNote(b));
else if (GuiLinker.GetButtonSwitchType(b) == SwitchType.OnOff)
{
MidiSender.SendNoteOn(GuiLinker.GetMidiNote(b), GuiLinker.GetButtonVelocity(b));
MidiSender.SendNoteOff(GuiLinker.GetMidiNote(b));
}
}
}
private void DPadOn(GuiDrumDPad dp)
{
Debug.Assert((byte)dp % 2 == 0, "only left, up, right, down is allowed here");
if (GuiLinker.GetButtonChecked(dp) == false)
{
GuiLinker.CheckboxButton(dp, true);
MidiSender.SendNoteOn(GuiLinker.GetMidiNote(dp), GuiLinker.GetButtonVelocity(dp));
if (GuiLinker.GetButtonSwitchType(dp) != SwitchType.KeyboardLike)
MidiSender.SendNoteOff(GuiLinker.GetMidiNote(dp));
}
}
private void DPadOn(GuiDrumDPad dp, GuiDrumDPad dp2)
{
Debug.Assert((byte)dp % 2 == 0, "only left, up, right, down is allowed here");
Debug.Assert((byte)dp2 % 2 == 0, "only left, up, right, down is allowed here");
if (GuiLinker.GetButtonChecked(dp) == false)
{
GuiLinker.CheckboxButton(dp, true);
MidiSender.SendNoteOn(GuiLinker.GetMidiNote(dp), GuiLinker.GetButtonVelocity(dp));
if (GuiLinker.GetButtonSwitchType(dp) != SwitchType.KeyboardLike)
MidiSender.SendNoteOff(GuiLinker.GetMidiNote(dp));
}
if (GuiLinker.GetButtonChecked(dp2) == false)
{
GuiLinker.CheckboxButton(dp2, true);
MidiSender.SendNoteOn(GuiLinker.GetMidiNote(dp2), GuiLinker.GetButtonVelocity(dp));
if (GuiLinker.GetButtonSwitchType(dp) != SwitchType.KeyboardLike)
MidiSender.SendNoteOff(GuiLinker.GetMidiNote(dp));
}
}
private void DPadOff(GuiDrumDPad dp)
{
Debug.Assert((byte)dp % 2 == 0, "only left, up, right, down is allowed here");
Debug.Assert((byte)dp % 2 == 0, "only left, up, right, down is allowed here");
if (GuiLinker.GetButtonChecked(dp) == true)
{
GuiLinker.CheckboxButton(dp, false);
if (GuiLinker.GetButtonSwitchType(dp) == SwitchType.KeyboardLike)
MidiSender.SendNoteOff(GuiLinker.GetMidiNote(dp));
else if (GuiLinker.GetButtonSwitchType(dp) == SwitchType.OnOff)
{
MidiSender.SendNoteOn(GuiLinker.GetMidiNote(dp), GuiLinker.GetButtonVelocity(dp));
MidiSender.SendNoteOff(GuiLinker.GetMidiNote(dp));
}
}
}
private void DrumDPadStateChanged(GuiDrumDPad dp)
{
if (m_PrevDPadState == dp)
return;
if (InvokeRequired)
{
Invoke(new DPadDelegate(DrumDPadStateChanged), new object[] { dp });
}
else
{
int new1, new2;
if ((byte)dp % 2 == 0)
{
new1 = (byte)dp;
new2 = (byte)dp;
}
else
{
new1 = (byte)dp - 1;
new2 = (byte)dp + 1;
if (new2 == 8) new2 = 0;
}
if (dp != GuiDrumDPad.None)
{
DPadOn((GuiDrumDPad)new1, (GuiDrumDPad)new2);
}
if (m_PrevDPadState != GuiDrumDPad.None)
{
if ((byte)m_PrevDPadState % 2 == 0)
{
if ((byte)m_PrevDPadState != new1 && (byte)m_PrevDPadState != new2)
DPadOff(m_PrevDPadState);
}
else
{
int prev1 = (byte)m_PrevDPadState - 1;
int prev2 = (byte)m_PrevDPadState + 1;
if (prev2 == 8) prev2 = 0;
if (prev1 != new1 && prev1 != new2)
DPadOff((GuiDrumDPad)prev1);
if (prev2 != new1 && prev2 != new2)
DPadOff((GuiDrumDPad)prev2);
}
}
m_PrevDPadState = dp;
}
}
private void UpdateMidi(object sender, EventArgs e)
{
MidiSender.UpdateMidiSettings();
}
#region Getters/Setters
public string GetMidiOutDeviceName()
{
return ddlMIDIDevice.SelectedValue as string;
}
public byte GetMidiChannel()
{
return (byte)nupMIDIChannel.Value;
}
public void SetMidiOutDeviceName(string deviceName)
{
GuiLinker.SetSelectIndex(ref ddlMIDIDevice, deviceName);
}
public void SetMidiChannel(byte channel)
{
nupMIDIChannel.Value = channel;
}
#endregion
#region Settings Methods
private void SaveSettingsClick(object sender, EventArgs e)
{
DialogResult dlgResult = saveFileDialog.ShowDialog();
if (dlgResult == DialogResult.OK)
{
m_Serializer.Save(saveFileDialog.FileName);
}
}
private void LoadSettingsClick(object sender, EventArgs e)
{
DialogResult dlgResult = openFileDialog.ShowDialog();
if (dlgResult == DialogResult.OK)
{
m_Serializer.Load(openFileDialog.FileName);
}
}
private void SaveDefaultSettings(object sender, FormClosingEventArgs e)
{
m_Serializer.Save(defaultFilePath);
}
private void LoadDefaultSettings(object sender, EventArgs e)
{
m_Serializer.LoadDefaultSettings();
}
#endregion
#region Override WndProc and OnHandleCreate
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if (m_DrumController != null)
m_DrumController.RegisterHandle(Handle);
}
protected override void WndProc(ref Message m)
{
if (m_DrumController != null)
m_DrumController.ParseMessages(ref m);
base.WndProc(ref m); // pass message on to base form
}
#endregion
#region Velocity Meters
public void UpdateVelocityPb(GuiDrumPad pad, byte hitVelocity)
{
if (InvokeRequired)
{
Invoke(new Void_DrumPadByteDelegate(UpdateVelocityPb), new object[] { pad, hitVelocity });
}
else
{
referenceVelocity[(int)pad].Value = hitVelocity;
}
}
private void LowerValues(object sender, EventArgs e)
{
for (int i = 0; i < referenceVelocity.GetLength(0); i++)
{
_lowerValues = referenceVelocity[i].Value - 2;
if (_lowerValues > 0)
{
if (_lowerValues > 110)
{
referenceVelocity[i].ForeColor = Color.Red;
}
if (_lowerValues > 63)
{
referenceVelocity[i].ForeColor = Color.FromArgb((_lowerValues) * 2, 255- (_lowerValues - 64) * 4, 0);
}
else
{
referenceVelocity[i].ForeColor = Color.FromArgb((_lowerValues) * 2, (_lowerValues) * 4, 255 - _lowerValues * 4);
}
referenceVelocity[i].Value = _lowerValues;
}
else
referenceVelocity[i].Value = 0;
}
}
#endregion
private void nupPedalRight_ValueChanged(object sender, EventArgs e)
{
}
private void btnRefreshUsb_Click(object sender, EventArgs e)
{
FillUsbList();
}
private void FillUsbList()
{
List<UsbDeviceInfo> devices = new List<UsbDeviceInfo>();
int defaultIndex = 0;
ManagementObjectCollection devicesCollection;
using (var searcher = new ManagementObjectSearcher("select * from Win32_PnPEntity where DeviceID like 'USB%'"))
{
devicesCollection = searcher.Get();
}
var i = 0;
foreach (var device in devicesCollection)
{
var deviceDescription = (string)device.GetPropertyValue("Description");
devices.Add(new UsbDeviceInfo()
{
Description = deviceDescription,
DeviceID = (string)device.GetPropertyValue("DeviceID")
});
// Automatically select the Xbox receiver if it's connected.
if (deviceDescription == "Xbox 360 Wireless Receiver for Windows") {
defaultIndex = i;
}
// TODO PS3 RockBand version of the above
i += 1;
}
devicesCollection.Dispose();
ddlUsbController.Items.Clear();
ddlUsbController.Items.AddRange(devices.ToArray());
ddlUsbController.SelectedIndex = defaultIndex;
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnConnect_Click(object sender, EventArgs e)
{
UsbDeviceInfo info = (UsbDeviceInfo)ddlUsbController.SelectedItem;
string vid = info.DeviceID.Substring(info.DeviceID.IndexOf("VID_") + 4, 4);
string pid = info.DeviceID.Substring(info.DeviceID.IndexOf("PID_") + 4, 4);
int iVid = int.Parse(vid, System.Globalization.NumberStyles.AllowHexSpecifier);
int iPid = int.Parse(pid, System.Globalization.NumberStyles.AllowHexSpecifier);
if (m_DrumController != null)
{
m_DrumController.Dispose();
m_DrumController.ButtonPressedEvent -= DrumButtonPressed;
m_DrumController.ButtonReleasedEvent -= DrumButtonReleased;
m_DrumController.DPadStateChanged -= DrumDPadStateChanged;
}
switch ((ControllerType)ddlController.SelectedItem)
{
case ControllerType.Xbox360_GHWT_GH5_Drum:
m_DrumController = new GHWTDrumController(this, iPid, iVid); break;
case ControllerType.Ps3_RockBandProDrum:
m_DrumController = new ProDrumController(this, iPid, iVid); break;
}
m_DrumController.ButtonPressedEvent += DrumButtonPressed;
m_DrumController.ButtonReleasedEvent += DrumButtonReleased;
m_DrumController.DPadStateChanged += DrumDPadStateChanged;
}
}
}