/
SettingsDialog.cs
428 lines (393 loc) · 16.9 KB
/
SettingsDialog.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
using LiveSplit.Model.Input;
using LiveSplit.Options;
using LiveSplit.UI;
using LiveSplit.UI.Components;
using LiveSplit.Utils;
using LiveSplit.Web;
using LiveSplit.Web.Share;
using LiveSplit.Web.SRL;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
namespace LiveSplit.View
{
public partial class SettingsDialog : Form
{
public ISettings Settings { get; set; }
public CompositeHook Hook { get; set; }
public string SelectedHotkeyProfile { get; set; }
public string SplitKey => FormatKey(Settings.HotkeyProfiles[SelectedHotkeyProfile].SplitKey);
public string ResetKey => FormatKey(Settings.HotkeyProfiles[SelectedHotkeyProfile].ResetKey);
public string SkipKey => FormatKey(Settings.HotkeyProfiles[SelectedHotkeyProfile].SkipKey);
public string UndoKey => FormatKey(Settings.HotkeyProfiles[SelectedHotkeyProfile].UndoKey);
public string PauseKey => FormatKey(Settings.HotkeyProfiles[SelectedHotkeyProfile].PauseKey);
public string SwitchComparisonPrevious => FormatKey(Settings.HotkeyProfiles[SelectedHotkeyProfile].SwitchComparisonPrevious);
public string SwitchComparisonNext => FormatKey(Settings.HotkeyProfiles[SelectedHotkeyProfile].SwitchComparisonNext);
public string ToggleGlobalHotkeys => FormatKey(Settings.HotkeyProfiles[SelectedHotkeyProfile].ToggleGlobalHotkeys);
public float HotkeyDelay
{
get { return Settings.HotkeyProfiles[SelectedHotkeyProfile].HotkeyDelay; }
set { Settings.HotkeyProfiles[SelectedHotkeyProfile].HotkeyDelay = Math.Max(value, 0); }
}
public bool GlobalHotkeysEnabled
{
get { return Settings.HotkeyProfiles[SelectedHotkeyProfile].GlobalHotkeysEnabled; }
set { Settings.HotkeyProfiles[SelectedHotkeyProfile].GlobalHotkeysEnabled = value; }
}
public bool DoubleTapPrevention
{
get { return Settings.HotkeyProfiles[SelectedHotkeyProfile].DoubleTapPrevention; }
set { Settings.HotkeyProfiles[SelectedHotkeyProfile].DoubleTapPrevention = value; }
}
public bool DeactivateHotkeysForOtherPrograms
{
get { return Settings.HotkeyProfiles[SelectedHotkeyProfile].DeactivateHotkeysForOtherPrograms; }
set { Settings.HotkeyProfiles[SelectedHotkeyProfile].DeactivateHotkeysForOtherPrograms = value; }
}
public bool AllowGamepadsAsHotkeys
{
get { return Settings.HotkeyProfiles[SelectedHotkeyProfile].AllowGamepadsAsHotkeys; }
set { Settings.HotkeyProfiles[SelectedHotkeyProfile].AllowGamepadsAsHotkeys = value; }
}
public int RefreshRate
{
get { return Settings.RefreshRate; }
set { Settings.RefreshRate = Math.Min(Math.Max(value, 20), 300); }
}
public int ServerPort
{
get { return Settings.ServerPort; }
set { Settings.ServerPort = value; }
}
public event EventHandler SumOfBestModeChanged;
public string RaceViewer { get { return Settings.RaceViewer.Name; } set { Settings.RaceViewer = Web.SRL.RaceViewer.FromName(value); } }
public SettingsDialog(CompositeHook hook, ISettings settings, string hotkeyProfile)
{
InitializeComponent();
Settings = settings;
Hook = hook;
InitializeHotkeyProfiles(hotkeyProfile);
SetClickEvents(this);
chkGlobalHotkeys.DataBindings.Add("Checked", this, "GlobalHotkeysEnabled");
chkDoubleTap.DataBindings.Add("Checked", this, "DoubleTapPrevention");
txtDelay.DataBindings.Add("Text", this, "HotkeyDelay");
chkWarnOnReset.DataBindings.Add("Checked", Settings, "WarnOnReset");
cbxRaceViewer.DataBindings.Add("SelectedItem", this, "RaceViewer");
chkAllowGamepads.DataBindings.Add("Checked", this, "AllowGamepadsAsHotkeys");
txtRefreshRate.DataBindings.Add("Text", this, "RefreshRate");
txtServerPort.DataBindings.Add("Text", this, "ServerPort");
UpdateDisplayedHotkeyValues();
RefreshRemoveButton();
RefreshLogOutButton();
}
private void InitializeHotkeyProfiles(string hotkeyProfile)
{
cmbHotkeyProfiles.Items.AddRange(Settings.HotkeyProfiles.Keys.ToArray());
cmbHotkeyProfiles.SelectedItem = hotkeyProfile;
}
private void UpdateDisplayedHotkeyValues()
{
txtStartSplit.Text = SplitKey;
txtReset.Text = ResetKey;
txtSkip.Text = SkipKey;
txtUndo.Text = UndoKey;
txtPause.Text = PauseKey;
txtToggle.Text = ToggleGlobalHotkeys;
txtSwitchPrevious.Text = SwitchComparisonPrevious;
txtSwitchNext.Text = SwitchComparisonNext;
chkGlobalHotkeys.Checked = GlobalHotkeysEnabled;
chkDoubleTap.Checked = DoubleTapPrevention;
txtDelay.Text = HotkeyDelay.ToString();
chkDeactivateForOtherPrograms.Checked = DeactivateHotkeysForOtherPrograms;
chkAllowGamepads.Checked = AllowGamepadsAsHotkeys;
chkGlobalHotkeys_CheckedChanged(null, null);
}
private void RefreshRemoveButton()
{
btnRemoveProfile.Enabled = Settings.HotkeyProfiles.Count > 1;
}
private void RefreshLogOutButton()
{
btnLogOut.Enabled = WebCredentials.AnyCredentialsExist();
}
void chkSimpleSOB_CheckedChanged(object sender, EventArgs e)
{
Settings.SimpleSumOfBest = chkSimpleSOB.Checked;
SumOfBestModeChanged?.Invoke(this, null);
}
void SettingsDialog_Load(object sender, EventArgs e)
{
chkSimpleSOB.Checked = Settings.SimpleSumOfBest;
chkGlobalHotkeys_CheckedChanged(null, null);
}
void chkGlobalHotkeys_CheckedChanged(object sender, EventArgs e)
{
if (chkGlobalHotkeys.Checked)
{
chkDeactivateForOtherPrograms.Enabled = true;
chkDeactivateForOtherPrograms.DataBindings.Clear();
chkDeactivateForOtherPrograms.DataBindings.Add("Checked", this, "DeactivateHotkeysForOtherPrograms");
}
else
{
chkDeactivateForOtherPrograms.Enabled = false;
chkDeactivateForOtherPrograms.DataBindings.Clear();
chkDeactivateForOtherPrograms.Checked = false;
}
}
private string FormatKey(KeyOrButton key)
{
if (key != null)
{
var keyString = key.ToString();
if (key.IsButton)
{
var lastSpaceIndex = keyString.LastIndexOf(' ');
if (lastSpaceIndex != -1)
{
keyString = keyString.Substring(0, lastSpaceIndex);
}
}
return keyString;
}
return "None";
}
private void SetHotkeyHandlers(TextBox txtBox, Action<KeyOrButton> keySetCallback)
{
var oldText = txtBox.Text;
txtBox.Text = "Set Hotkey...";
txtBox.Select(0, 0);
KeyEventHandler handlerDown = null;
KeyEventHandler handlerUp = null;
EventHandler leaveHandler = null;
EventHandlerT<GamepadButton> gamepadButtonPressed = null;
Action unregisterEvents = () =>
{
txtBox.KeyDown -= handlerDown;
txtBox.KeyUp -= handlerUp;
txtBox.Leave -= leaveHandler;
Hook.AnyGamepadButtonPressed -= gamepadButtonPressed;
};
handlerDown = (s, x) =>
{
var key = x.KeyCode == Keys.Escape ? null : new KeyOrButton(x.KeyCode | x.Modifiers);
if (x.KeyCode == Keys.ControlKey || x.KeyCode == Keys.ShiftKey || x.KeyCode == Keys.Menu)
return;
keySetCallback(key);
unregisterEvents();
txtBox.Select(0, 0);
chkGlobalHotkeys.Select();
txtBox.Text = FormatKey(key);
};
handlerUp = (s, x) =>
{
var key = x.KeyCode == Keys.Escape ? null : new KeyOrButton(x.KeyCode | x.Modifiers);
if (x.KeyCode == Keys.ControlKey || x.KeyCode == Keys.ShiftKey || x.KeyCode == Keys.Menu)
{
keySetCallback(key);
unregisterEvents();
txtBox.Select(0, 0);
chkGlobalHotkeys.Select();
txtBox.Text = FormatKey(key);
}
};
leaveHandler = (s, x) =>
{
unregisterEvents();
txtBox.Text = oldText;
};
gamepadButtonPressed = (s, x) =>
{
var key = new KeyOrButton(x);
keySetCallback(key);
unregisterEvents();
this.InvokeIfRequired(() =>
{
txtBox.Select(0, 0);
chkGlobalHotkeys.Select();
txtBox.Text = FormatKey(key);
});
};
txtBox.KeyDown += handlerDown;
txtBox.KeyUp += handlerUp;
txtBox.Leave += leaveHandler;
Hook.AnyGamepadButtonPressed += gamepadButtonPressed;
}
private void Split_Set_Enter(object sender, EventArgs e)
{
SetHotkeyHandlers((TextBox)sender, x =>
{
Settings.HotkeyProfiles[SelectedHotkeyProfile].SplitKey = x;
});
}
private void Reset_Set_Enter(object sender, EventArgs e)
{
SetHotkeyHandlers((TextBox)sender, x =>
{
Settings.HotkeyProfiles[SelectedHotkeyProfile].ResetKey = x;
});
}
private void Skip_Set_Enter(object sender, EventArgs e)
{
SetHotkeyHandlers((TextBox)sender, x =>
{
Settings.HotkeyProfiles[SelectedHotkeyProfile].SkipKey = x;
});
}
private void Undo_Set_Enter(object sender, EventArgs e)
{
SetHotkeyHandlers((TextBox)sender, x =>
{
Settings.HotkeyProfiles[SelectedHotkeyProfile].UndoKey = x;
});
}
private void Pause_Set_Enter(object sender, EventArgs e)
{
SetHotkeyHandlers((TextBox)sender, x =>
{
Settings.HotkeyProfiles[SelectedHotkeyProfile].PauseKey = x;
});
}
private void Toggle_Set_Enter(object sender, EventArgs e)
{
SetHotkeyHandlers((TextBox)sender, x =>
{
Settings.HotkeyProfiles[SelectedHotkeyProfile].ToggleGlobalHotkeys = x;
});
}
private void Switch_Previous_Set_Enter(object sender, EventArgs e)
{
SetHotkeyHandlers((TextBox)sender, x =>
{
Settings.HotkeyProfiles[SelectedHotkeyProfile].SwitchComparisonPrevious = x;
});
}
private void Switch_Next_Set_Enter(object sender, EventArgs e)
{
SetHotkeyHandlers((TextBox)sender, x =>
{
Settings.HotkeyProfiles[SelectedHotkeyProfile].SwitchComparisonNext = x;
});
}
private void ClickControl(object sender, EventArgs e)
{
chkGlobalHotkeys.Select();
}
private void SetClickEvents(Control control)
{
foreach (Control childControl in control.Controls)
{
if (childControl is TableLayoutPanel || childControl is Label || childControl is GroupBox)
{
SetClickEvents(childControl);
}
}
control.Click += ClickControl;
}
private void btnOK_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void btnChooseComparisons_Click(object sender, EventArgs e)
{
var generatorStates = new Dictionary<string, bool>(Settings.ComparisonGeneratorStates);
var result = (new ChooseComparisonsDialog() { ComparisonGeneratorStates = generatorStates }).ShowDialog(this);
if (result == DialogResult.OK)
Settings.ComparisonGeneratorStates = generatorStates;
}
private void cmbHotkeyProfiles_SelectedIndexChanged(object sender, EventArgs e)
{
SelectedHotkeyProfile = cmbHotkeyProfiles.SelectedItem.ToString();
UpdateDisplayedHotkeyValues();
}
private void btnRemoveProfile_Click(object sender, EventArgs e)
{
if (Settings.HotkeyProfiles.Count > 1)
{
Settings.HotkeyProfiles.Remove(SelectedHotkeyProfile);
cmbHotkeyProfiles.Items.Remove(SelectedHotkeyProfile);
cmbHotkeyProfiles.SelectedItem = Settings.HotkeyProfiles.Keys.Last();
RefreshRemoveButton();
}
}
private void btnRenameProfile_Click(object sender, EventArgs e)
{
var newName = SelectedHotkeyProfile;
var result = InputBox.Show("Rename Hotkey Profile", "Hotkey Profile Name:", ref newName);
if (result == DialogResult.OK)
{
if (!Settings.HotkeyProfiles.ContainsKey(newName))
{
for (var i = 0; i < Settings.RecentSplits.Count; i++)
{
var file = Settings.RecentSplits[i];
if (file.LastHotkeyProfile == SelectedHotkeyProfile)
{
var newFile = new RecentSplitsFile(file.Path, file.LastTimingMethod, newName, file.GameName, file.CategoryName);
Settings.RecentSplits.RemoveAt(i);
Settings.RecentSplits.Insert(i, newFile);
}
}
var curProfile = Settings.HotkeyProfiles[SelectedHotkeyProfile];
Settings.HotkeyProfiles.Remove(SelectedHotkeyProfile);
Settings.HotkeyProfiles[newName] = curProfile;
cmbHotkeyProfiles.Items.Remove(SelectedHotkeyProfile);
cmbHotkeyProfiles.Items.Add(newName);
cmbHotkeyProfiles.SelectedItem = newName;
}
else if (newName != SelectedHotkeyProfile)
{
result = MessageBox.Show(this, "A Hotkey Profile with this name already exists.", "Hotkey Profile Already Exists", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (result == DialogResult.Retry)
btnRenameProfile_Click(sender, e);
}
}
}
private void btnNewProfile_Click(object sender, EventArgs e)
{
var name = "";
var result = InputBox.Show("New Hotkey Profile", "Hotkey Profile Name:", ref name);
if (result == DialogResult.OK)
{
if (!Settings.HotkeyProfiles.ContainsKey(name))
{
var newProfile = (HotkeyProfile)Settings.HotkeyProfiles[SelectedHotkeyProfile].Clone();
Settings.HotkeyProfiles.Add(name, newProfile);
cmbHotkeyProfiles.Items.Add(name);
cmbHotkeyProfiles.SelectedItem = name;
RefreshRemoveButton();
}
else
{
result = MessageBox.Show(this, "A Hotkey Profile with this name already exists.", "Hotkey Profile Already Exists", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (result == DialogResult.Retry)
btnNewProfile_Click(sender, e);
}
}
}
private void btnLogOut_Click(object sender, EventArgs e)
{
SpeedrunCom.ClearAccessToken();
Twitch.Instance.ClearAccessToken();
WebCredentials.DeleteAllCredentials();
RefreshLogOutButton();
}
private void btnChooseRaceProvider_Click(object sender, EventArgs e)
{
var newSettings = Settings.RaceProvider.Select(x => (RaceProviderSettings)x.Clone()).ToList();
var dialog = new RaceProviderManagingDialog(newSettings);
if (dialog.ShowDialog(this) == DialogResult.OK)
{
Settings.RaceProvider = newSettings;
}
}
}
}