-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecordSettingsForm.cs
More file actions
374 lines (334 loc) · 16.1 KB
/
RecordSettingsForm.cs
File metadata and controls
374 lines (334 loc) · 16.1 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SharpDX.DirectInput;
namespace VirtualController
{
public partial class RecordSettingsForm : Form
{
private DirectInput directInput;
private List<DeviceInstance> uniqueDevices;
private Joystick currentJoystick;
private bool isListening = false;
private Button listeningButton = null;
private Label listeningLabel = null;
private int? assignedZValue = null;
// ボタンごとの割り当て値保存用プロパティ
private Dictionary<Button, int?> assignedButtonIndices = new Dictionary<Button, int?>();
private Dictionary<Button, int?> assignedZValues = new Dictionary<Button, int?>();
private const string ConfigFilePath = "RecordSettingsConfig.json";
// 変更後: private class RecordSettingsConfig
[DataContract]
public class RecordSettingsConfig
{
[DataMember]
public Guid ControllerGuid;
[DataMember]
public Dictionary<string, int?> ButtonIndices = new Dictionary<string, int?>();
[DataMember]
public Dictionary<string, int?> ZValues = new Dictionary<string, int?>();
}
public RecordSettingsForm()
{
InitializeComponent();
SetActionButtonsEnabled(false);
OKButton.Enabled = false;
LoadPhysicalControllers();
LoadConfig(); // 追加: 設定ファイルから復元
}
private void SetActionButtonsEnabled(bool enabled)
{
LPButton.Enabled = enabled;
MPButton.Enabled = enabled;
HPButton.Enabled = enabled;
LKButton.Enabled = enabled;
MKButton.Enabled = enabled;
HKButton.Enabled = enabled;
}
private void LoadPhysicalControllers()
{
directInput = new DirectInput();
var devices = directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices)
.Concat(directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices))
.Concat(directInput.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AllDevices))
.ToList();
// InstanceGuidで重複排除
uniqueDevices = devices
.GroupBy(d => d.InstanceGuid)
.Select(g => g.First())
.ToList();
ControllerComboBox.Items.Clear();
foreach (var device in uniqueDevices)
{
ControllerComboBox.Items.Add($"{device.InstanceName} ({device.InstanceGuid})");
}
}
private void ControllerComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (ControllerComboBox.SelectedIndex < 0)
{
SetActionButtonsEnabled(false);
return;
}
SetActionButtonsEnabled(true);
var device = uniqueDevices[ControllerComboBox.SelectedIndex];
// 既存のジョイスティックを解放
currentJoystick?.Unacquire();
currentJoystick?.Dispose();
// 新しいジョイスティックを接続
currentJoystick = new Joystick(directInput, device.InstanceGuid);
currentJoystick.Acquire();
LogTextBox.AppendText($"[{DateTime.Now:HH:mm:ss}] コントローラー「{device.InstanceName}」に接続しました。\r\n");
var defaultState = currentJoystick.GetCurrentState();
var defaultZValue = defaultState.Z;
var timer = new Timer();
timer.Interval = 1000 / 60 * 2; // 2フレームに1回
timer.Tick += (s, ev) =>
{
try
{
var state = currentJoystick.GetCurrentState();
if (isListening)
{
// ボタン判定
int pressedIndex = Array.FindIndex(state.Buttons, b => b);
if (pressedIndex >= 0)
{
EndListening($"{pressedIndex}ボタン", pressedIndex, null);
LogTextBox.AppendText($"[{DateTime.Now:HH:mm:ss}] {pressedIndex}ボタン を割り当てました\r\n");
return;
}
// Z軸判定
int zValueListening = state.Z;
if (assignedZValue == null || assignedZValue != zValueListening)
{
if (zValueListening == 128)
{
EndListening("Z1ボタン", null, zValueListening);
LogTextBox.AppendText($"[{DateTime.Now:HH:mm:ss}] Z1ボタン を割り当てました\r\n");
}
else if (zValueListening == 65408)
{
EndListening("Z2ボタン", null, zValueListening);
LogTextBox.AppendText($"[{DateTime.Now:HH:mm:ss}] Z2ボタン を割り当てました\r\n");
}
return;
}
}
// Z軸の値を取得し、変化があればログ出力
int zValue = state.Z;
if (zValue != defaultZValue)
{
string zLog = $"[{DateTime.Now:HH:mm:ss}] Z軸: {zValue}\r\n";
if (zValue == 128)
zLog = $"[{DateTime.Now:HH:mm:ss}] Z1ボタン\r\n";
else if (zValue == 65408)
zLog = $"[{DateTime.Now:HH:mm:ss}] Z2ボタン\r\n";
LogTextBox.AppendText(zLog);
}
// ボタン・方向キーのログ
var pressed = state.Buttons.Select((b, i) => b ? $"{i}ボタン" : null).Where(x => x != null).ToList();
var povs = state.PointOfViewControllers;
var directions = new List<string>();
foreach (var pov in povs)
{
if (pov >= 0)
{
switch (pov)
{
case 0: directions.Add("上"); break;
case 4500: directions.Add("右上"); break;
case 9000: directions.Add("右"); break;
case 13500: directions.Add("右下"); break;
case 18000: directions.Add("下"); break;
case 22500: directions.Add("左下"); break;
case 27000: directions.Add("左"); break;
case 31500: directions.Add("左上"); break;
default: directions.Add($"POV:{pov}"); break;
}
}
}
if (pressed.Count > 0 || directions.Count > 0)
{
LogTextBox.AppendText(
$"[{DateTime.Now:HH:mm:ss}] {string.Join(", ", pressed.Concat(directions))}\r\n"
);
}
}
catch { /* デバイス切断時など */ }
};
timer.Start();
}
// リスニングモード開始
private void StartListening(Button button, Label label)
{
if (isListening) return;
isListening = true;
listeningButton = button;
listeningLabel = label;
button.BackColor = Color.Orange;
label.Text = "リスニング中...";
LogTextBox.AppendText($"[{DateTime.Now:HH:mm:ss}] {button.Text} リスニングモード開始\r\n");
}
// リスニングモード終了
private void EndListening(string displayText, int? buttonIndex, int? zValue)
{
// 既存の割り当て解除処理
if (buttonIndex != null)
{
// 他のボタンに同じbuttonIndexが割り当てられていたら解除
foreach (var kvp in assignedButtonIndices.ToList())
{
if (kvp.Value == buttonIndex && kvp.Key != listeningButton)
{
assignedButtonIndices[kvp.Key] = null;
assignedZValues[kvp.Key] = null;
Control labelParent;
if (kvp.Key == F1Button)
labelParent = OptionGroupBox;
else
labelParent = this;
var label = labelParent.Controls.OfType<Label>().FirstOrDefault(l => l.Name == kvp.Key.Name.Replace("Button", "Label"));
if (label != null) label.Text = "未割り当て";
}
}
}
if (zValue != null)
{
// 他のボタンに同じzValueが割り当てられていたら解除
foreach (var kvp in assignedZValues.ToList())
{
if (kvp.Value == zValue && kvp.Key != listeningButton)
{
assignedButtonIndices[kvp.Key] = null;
assignedZValues[kvp.Key] = null;
Control labelParent;
if (kvp.Key == F1Button)
labelParent = OptionGroupBox;
else
labelParent = this;
var label = labelParent.Controls.OfType<Label>().FirstOrDefault(l => l.Name == kvp.Key.Name.Replace("Button", "Label"));
if (label != null) label.Text = "未割り当て";
}
}
}
if (listeningButton != null)
{
listeningButton.BackColor = SystemColors.Control;
Control labelParent;
if (listeningButton == F1Button)
labelParent = OptionGroupBox;
else
labelParent = this;
var label = labelParent.Controls.OfType<Label>().FirstOrDefault(l => l.Name == listeningButton.Name.Replace("Button", "Label"));
if (label != null)
label.Text = displayText;
assignedButtonIndices[listeningButton] = buttonIndex;
assignedZValues[listeningButton] = zValue;
}
isListening = false;
listeningButton = null;
listeningLabel = null;
// 6つのボタンがすべて割り当て済みならOKボタンを有効化
OKButton.Enabled = IsAllActionButtonsAssigned();
}
private bool IsAllActionButtonsAssigned()
{
var buttons = new[] { LPButton, MPButton, HPButton, LKButton, MKButton, HKButton };
return buttons.All(btn =>
(assignedButtonIndices.ContainsKey(btn) && assignedButtonIndices[btn] != null) ||
(assignedZValues.ContainsKey(btn) && assignedZValues[btn] != null)
);
}
// 各ボタンのクリックイベント
private void LPButton_Click(object sender, EventArgs e) => StartListening(LPButton, LPLabel);
private void MPButton_Click(object sender, EventArgs e) => StartListening(MPButton, MPLabel);
private void HPButton_Click(object sender, EventArgs e) => StartListening(HPButton, HPLabel);
private void LKButton_Click(object sender, EventArgs e) => StartListening(LKButton, LKLabel);
private void MKButton_Click(object sender, EventArgs e) => StartListening(MKButton, MKLabel);
private void HKButton_Click(object sender, EventArgs e) => StartListening(HKButton, HKLabel);
private void F1Button_Click(object sender, EventArgs e) => StartListening(F1Button, F1Label); // 追加
private void OKButton_Click(object sender, EventArgs e)
{
SaveConfig();
this.DialogResult = DialogResult.OK;
this.Close();
}
private void SaveConfig()
{
var config = new RecordSettingsConfig();
if (ControllerComboBox.SelectedIndex >= 0)
config.ControllerGuid = uniqueDevices[ControllerComboBox.SelectedIndex].InstanceGuid;
var buttons = new[] { LPButton, MPButton, HPButton, LKButton, MKButton, HKButton, F1Button }; // 追加
foreach (var btn in buttons)
{
config.ButtonIndices[btn.Name] = (assignedButtonIndices.ContainsKey(btn) && assignedButtonIndices[btn] != null)
? assignedButtonIndices[btn]
: null;
config.ZValues[btn.Name] = (assignedZValues.ContainsKey(btn) && assignedZValues[btn] != null)
? assignedZValues[btn]
: null;
}
using (var fs = new FileStream(ConfigFilePath, FileMode.Create))
{
var serializer = new DataContractJsonSerializer(
typeof(RecordSettingsConfig),
new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true }
);
serializer.WriteObject(fs, config);
}
}
private void LoadConfig()
{
if (!File.Exists(ConfigFilePath)) return;
RecordSettingsConfig config;
using (var fs = new FileStream(ConfigFilePath, FileMode.Open))
{
var serializer = new DataContractJsonSerializer(
typeof(RecordSettingsConfig),
new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true }
);
config = (RecordSettingsConfig)serializer.ReadObject(fs);
}
// コントローラー自動選択
int idx = uniqueDevices.FindIndex(d => d.InstanceGuid == config.ControllerGuid);
if (idx >= 0)
{
ControllerComboBox.SelectedIndex = idx;
SetActionButtonsEnabled(true);
}
// 割り当て復元
var buttons = new[] { LPButton, MPButton, HPButton, LKButton, MKButton, HKButton, F1Button };
foreach (var btn in buttons)
{
assignedButtonIndices[btn] = config.ButtonIndices.ContainsKey(btn.Name) ? config.ButtonIndices[btn.Name] : null;
assignedZValues[btn] = config.ZValues.ContainsKey(btn.Name) ? config.ZValues[btn.Name] : null;
System.Diagnostics.Debug.WriteLine(
$"ボタン: {btn.Name}, ButtonIndex: {assignedButtonIndices[btn]}, ZValue: {assignedZValues[btn]}"
);
Control labelParent = btn == F1Button ? (Control)OptionGroupBox : (Control)this;
var labelName = btn.Name.Replace("Button", "Label");
var label = labelParent.Controls.OfType<Label>().FirstOrDefault(l => l.Name == labelName);
if (label != null)
{
if (assignedButtonIndices[btn] != null)
label.Text = $"{assignedButtonIndices[btn]}ボタン";
else if (assignedZValues[btn] != null)
label.Text = assignedZValues[btn] == 128 ? "Z1ボタン" : assignedZValues[btn] == 65408 ? "Z2ボタン" : $"Z軸:{assignedZValues[btn]}";
else
label.Text = "未割り当て";
}
}
OKButton.Enabled = IsAllActionButtonsAssigned();
}
}
}