-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathControllerConfig.cs
332 lines (305 loc) · 11 KB
/
ControllerConfig.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
using SharpDX.DirectInput;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace dmc3music
{
public partial class ControllerConfig : Form
{
private DMC3MusicConfig Config { get; set; }
public DirectInput directInput = new DirectInput();
private Timer ControllerTimer { get; set; }
public Joystick joystick { get; set; }
public Guid joystickGuid { get; set; } = Guid.Empty;
public string joystickName { get; set; }
public Dictionary<string, Guid> controllerDict { get; set; } = new Dictionary<string, Guid>();
public Dictionary<string, int> ControllerKeysMap = new Dictionary<string, int>()
{
{ "Start", 7 },
{ "Select", 6 },
{ "Circle", 1 },
{ "Triangle", 3 },
{ "Square", 2 },
{ "Cross", 0 },
{ "L1", 4 },
{ "R1", 5 },
{ "L2", 255 },
{ "R2", 255 },
{ "L3", 8 },
{ "R3", 9 },
{ "L<->R", 0 }
};
public Dictionary<string, int> ButtonsAsNums = new Dictionary<string, int>()
{
{ "Buttons0", 0 },
{ "Buttons1", 1 },
{ "Buttons2", 2 },
{ "Buttons3", 3 },
{ "Buttons4", 4 },
{ "Buttons5", 5 },
{ "Buttons6", 6 },
{ "Buttons7", 7 },
{ "Buttons8", 8 },
{ "Buttons9", 9 },
{ "Buttons10", 10 },
{ "Buttons11", 11 },
{ "Buttons12", 12 },
{ "Buttons13", 13 },
{ "Buttons14", 14 },
{ "Buttons15", 15 },
{ "Buttons16", 16 },
};
public PictureBox picClear { get; set; }
public PictureBox picClicked { get; set; }
public string currentKey { get; set; } = "Start";
public Form InputForm { get; set; }
public bool DisplayTriggers { get; set; } = false;
public ControllerConfig()
{
InitializeComponent();
Config = DMC3MusicConfigWriter.ReadConfig();
if (Config.DMC3Path == string.Empty || Config.DMC3Path == null || !Directory.Exists(Config.DMC3Path))
{
MessageBox.Show("Please make sure the path to DMC3 is correct in the Options tab", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Dispose();
}
IList<DeviceInstance> controllers = directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices);
foreach (DeviceInstance ctrl in controllers)
{
comboBox1.Items.Add(ctrl.InstanceName);
controllerDict.Add(ctrl.InstanceName, ctrl.InstanceGuid);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string contents = $"[{joystickName}]\r\n";
foreach (KeyValuePair<string, int> kp in ControllerKeysMap)
{
contents += $"{kp.Key}={kp.Value}\r\n";
}
string dest = Path.Combine(Config.DMC3Path, "DMC3SE.ini");
File.WriteAllText(dest, contents);
MessageBox.Show($"Successfully written the controller config to {dest}", "Controller Config", MessageBoxButtons.OK, MessageBoxIcon.None);
}
catch { }
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
joystickName = comboBox1.SelectedItem.ToString();
if (!controllerDict.TryGetValue(joystickName, out Guid tmpGuid))
{
return;
}
joystickGuid = tmpGuid;
joystick = new Joystick(directInput, joystickGuid);
/* IList<EffectInfo> allEffects = joystick.GetEffects();
foreach (EffectInfo effectInfo in allEffects)
{
Console.WriteLine("Effect available {0}", effectInfo.Name);
}*/
joystick.Properties.BufferSize = 128;
joystick.Acquire();
}
private void PollController(object sender, EventArgs e)
{
joystick.Poll();
JoystickUpdate[] datas = joystick.GetBufferedData();
foreach (JoystickUpdate state in datas)
{
string button = state.Offset.ToString();
if (button.Contains("Buttons") && !InputForm.IsDisposed)
{
if (ButtonsAsNums.TryGetValue(button, out int b))
{
ControllerKeysMap[currentKey] = b;
}
InputForm.Dispose();
picClear.Visible = true;
picClicked.Visible = false;
}
}
}
private void BeginPoll(string displayKey)
{
if (joystickGuid == Guid.Empty)
{
MessageBox.Show("Please select a controller from the dropdown menu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
refreshBoxes();
picClear.Visible = false;
picClicked.Visible = true;
if (Application.OpenForms.OfType<MessageForm>().Count() > 0)
{
InputForm.Dispose();
}
InputForm = new MessageForm(displayKey);
InputForm.Show(this);
ControllerTimer = new Timer
{
Interval = 10
};
ControllerTimer.Tick -= new EventHandler(PollController);
ControllerTimer.Tick += new EventHandler(PollController);
ControllerTimer.Start();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
picClear = pictureBox1;
picClicked = pictureBox24;
currentKey = "Triangle";
BeginPoll("Triangle/Y");
}
private void pictureBox2_Click(object sender, EventArgs e)
{
picClear = pictureBox2;
picClicked = pictureBox22;
currentKey = "Square";
BeginPoll("Square/X");
}
private void pictureBox3_Click(object sender, EventArgs e)
{
picClear = pictureBox3;
picClicked = pictureBox14;
currentKey = "Cross";
BeginPoll("Cross/A");
}
private void pictureBox4_Click(object sender, EventArgs e)
{
picClear = pictureBox4;
picClicked = pictureBox13;
currentKey = "Circle";
BeginPoll("Circle/B");
}
private void pictureBox5_Click(object sender, EventArgs e)
{
picClear = pictureBox5;
picClicked = pictureBox23;
currentKey = "Start";
BeginPoll("Options/Start");
}
private void pictureBox6_Click(object sender, EventArgs e)
{
picClear = pictureBox6;
picClicked = pictureBox21;
currentKey = "Select";
BeginPoll("Select/Back");
}
private void pictureBox12_Click(object sender, EventArgs e)
{
picClear = pictureBox12;
picClicked = pictureBox17;
currentKey = "L3";
BeginPoll("L3/Left Stick");
}
private void pictureBox11_Click(object sender, EventArgs e)
{
picClear = pictureBox11;
picClicked = pictureBox20;
currentKey = "R3";
BeginPoll("R3/Right Stick");
}
private void pictureBox9_Click(object sender, EventArgs e)
{
picClear = pictureBox9;
picClicked = pictureBox18;
currentKey = "R1";
BeginPoll("R1/RB");
}
private void pictureBox10_Click(object sender, EventArgs e)
{
picClear = pictureBox10;
picClicked = pictureBox19;
currentKey = "R2";
BeginPoll("R2/RT");
}
private void pictureBox7_Click(object sender, EventArgs e)
{
picClear = pictureBox7;
picClicked = pictureBox15;
currentKey = "L1";
BeginPoll("L1/LB");
}
private void pictureBox8_Click(object sender, EventArgs e)
{
picClear = pictureBox8;
picClicked = pictureBox16;
currentKey = "L2";
BeginPoll("L2/LT");
}
private void refreshBoxes()
{
pictureBox1.Visible = true;
pictureBox2.Visible = true;
pictureBox3.Visible = true;
pictureBox4.Visible = true;
pictureBox5.Visible = true;
pictureBox6.Visible = true;
pictureBox7.Visible = true;
pictureBox8.Visible = DisplayTriggers;
pictureBox9.Visible = true;
pictureBox10.Visible = DisplayTriggers;
pictureBox11.Visible = true;
pictureBox12.Visible = true;
pictureBox13.Visible = false;
pictureBox14.Visible = false;
pictureBox15.Visible = false;
pictureBox16.Visible = false;
pictureBox17.Visible = false;
pictureBox18.Visible = false;
pictureBox19.Visible = false;
pictureBox20.Visible = false;
pictureBox21.Visible = false;
pictureBox22.Visible = false;
pictureBox23.Visible = false;
pictureBox24.Visible = false;
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked)
{
ControllerKeysMap["L2"] = 255;
ControllerKeysMap["R2"] = 255;
DisplayTriggers = false;
}
else
{
DisplayTriggers = true;
}
refreshBoxes();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
ControllerKeysMap["L<->R"] = 1;
}
else
{
ControllerKeysMap["L<->R"] = 0;
}
}
private void ControllerConfig_Load(object sender, EventArgs e)
{
DialogResult res = MessageBox.Show("Configure for an Xbox controller? (Click Yes if emulated as well, i.e. DS4Windows/Xinput Plus)", "Controller Config", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (res == DialogResult.Yes)
{
DisplayTriggers = false;
checkBox2.Checked = true;
checkBox1.Checked = true;
ControllerKeysMap["L2"] = 255;
ControllerKeysMap["R2"] = 255;
ControllerKeysMap["L<->R"] = 1;
}
else
{
DisplayTriggers = true;
}
refreshBoxes();
}
}
}