forked from M3IY0U/StalkbotGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigWindow.xaml.cs
309 lines (286 loc) · 12.6 KB
/
ConfigWindow.xaml.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using AForge.Video.DirectShow;
using Microsoft.WindowsAPICodePack.Dialogs;
using NAudio.Wave;
using StalkbotGUI.Stalkbot.Discord.Commands;
using StalkbotGUI.Stalkbot.Utilities;
namespace StalkbotGUI
{
/// <summary>
/// Interaction logic for ConfigWindow.xaml
/// </summary>
public partial class ConfigWindow
{
/// <summary>
/// Keeps track of the currently selected webcam
/// </summary>
private VideoCaptureDevice _selectedCam;
/// <summary>
/// Constructor
/// </summary>
public ConfigWindow()
{
InitializeComponent();
FolderLabel.Content = string.IsNullOrEmpty(Config.Instance.FolderPath)
? "No folder selected."
: Config.Instance.FolderPath;
DurationInput.Text = $"{Config.Instance.Timeout}";
BlurInput.Text = $"{Config.Instance.BlurAmount}";
CamDelayInput.Text = $"{Config.Instance.CamTimer}";
PrefixInput.Text = Config.Instance.Prefix;
TokenInput.Text = Config.Instance.Token;
AutoStartCheckBox.IsChecked = Config.Instance.AutoStartDiscord;
MinimizeCheckBox.IsChecked = Config.Instance.MinimizeToTray;
GifLengthInput.Text = $"{Config.Instance.GifLength}";
var webcams = new List<string>();
for (var i = 0; i < Constants.Cameras.Count; i++)
webcams.Add(Constants.Cameras[i].Name);
CamSelector.ItemsSource = webcams;
CamSelector.SelectedIndex = Config.Instance.DefaultCam;
_selectedCam = new VideoCaptureDevice(Constants.Cameras[Config.Instance.DefaultCam].MonikerString);
GifFps.IsChecked = Config.Instance.GifFps;
CustomGifFpsInput.Text = $"{Config.Instance.CustomGifFps}";
CustomGifFpsInput.IsEnabled = !Config.Instance.GifFps;
UpdateResBox(true);
UpdateResBox(false);
var mics = new List<string>();
for (var i = 0; i < WaveIn.DeviceCount; i++)
mics.Add(WaveIn.GetCapabilities(i).ProductName);
MicSelector.ItemsSource = mics;
MicSelector.SelectedIndex = Config.Instance.MicIndex;
MicDelayTxtBx.Text = Config.Instance.MicTimer.ToString();
MicLengthTxtBx.Text = Config.Instance.MicLength.ToString();
}
/// <summary>
/// Handles clicking the folder picker
/// </summary>
/// <param name="sender">Button object</param>
/// <param name="e">Event args</param>
private void FolderSelect_Click(object sender, RoutedEventArgs e)
{
using (var dialog = new CommonOpenFileDialog("Folder Resources") {IsFolderPicker = true})
{
if (dialog.ShowDialog() != CommonFileDialogResult.Ok) return;
Config.Instance.FolderPath = dialog.FileName;
FolderLabel.Content = $"{Config.Instance.FolderPath}";
}
}
/// <summary>
/// Handles clicking the folder reset button
/// </summary>
/// <param name="sender">Button object</param>
/// <param name="e">Event args</param>
private void FolderClear_Click(object sender, RoutedEventArgs e)
{
Config.Instance.FolderPath = "";
FolderLabel.Content = "No folder selected.";
}
/// <summary>
/// Handles changing the camera selection
/// </summary>
/// <param name="sender">Combobox object</param>
/// <param name="e">Event args</param>
private void CamSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Config.Instance.DefaultCam = CamSelector.SelectedIndex;
_selectedCam = new VideoCaptureDevice(Constants.Cameras[Config.Instance.DefaultCam].MonikerString);
UpdateResBox(true);
UpdateResBox(false);
}
/// <summary>
/// Handles changing the microphone selection
/// </summary>
/// <param name="sender">Combobox object</param>
/// <param name="e">Event args</param>
private void MicSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Config.Instance.MicIndex = MicSelector.SelectedIndex;
WaveIn.GetCapabilities(Config.Instance.MicIndex);
}
/// <summary>
/// Updates the content of the corresponding resolution combobox
/// </summary>
/// <param name="isGif">Whether the gif or normal resolution should be updated</param>
private void UpdateResBox(bool isGif)
{
var cb = isGif ? GifResolutionSelector : ResolutionSelector;
var items =
_selectedCam.VideoCapabilities.Select(x => $"{x.FrameSize.Width}x{x.FrameSize.Height}").ToList();
cb.ItemsSource = items;
cb.SelectedIndex =
items.FindIndex(s
=> s.Contains(isGif
? Config.Instance.GifCamWidth.ToString()
: Config.Instance.CamWidth.ToString())
&& s.Contains(isGif
? Config.Instance.GifCamHeight.ToString()
: Config.Instance.CamHeight.ToString()));
}
/// <summary>
/// Handles changing the resolution selection
/// </summary>
/// <param name="sender">Combobox object</param>
/// <param name="e">Event args</param>
private void ResolutionSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
Config.Instance.CamWidth = Convert.ToInt32(((string)ResolutionSelector.SelectedItem).Split('x').First());
Config.Instance.CamHeight = Convert.ToInt32(((string)ResolutionSelector.SelectedItem).Split('x').Last());
}
catch (Exception)
{
Logger.Log("Unable to determine camera resolution of the selected camera", LogLevel.Warning);
}
}
/// <summary>
/// Handles the window being closed via the X button
/// </summary>
/// <param name="sender">Window object</param>
/// <param name="e">Event args</param>
private void Window_Closed(object sender, EventArgs e)
{
Config.Instance.SaveConfig();
Logger.Log("Config saved", LogLevel.Info);
}
/// <summary>
/// Handles text changing in the cam delay input
/// </summary>
/// <param name="sender">Textbox object</param>
/// <param name="e">Event args</param>
private void CamDelayInput_TextChanged(object sender, TextChangedEventArgs e)
{
if (!string.IsNullOrEmpty(CamDelayInput.Text))
Config.Instance.CamTimer = Convert.ToInt32(CamDelayInput.Text);
}
/// <summary>
/// Handles text changing in the blur input
/// </summary>
/// <param name="sender">Textbox object</param>
/// <param name="e">Event args</param>
private void BlurInput_TextChanged(object sender, TextChangedEventArgs e)
{
if (!string.IsNullOrEmpty(BlurInput.Text))
Config.Instance.BlurAmount = Convert.ToDouble(BlurInput.Text);
}
/// <summary>
/// Handles text changing in the duration/timeout input
/// </summary>
/// <param name="sender">Textbox object</param>
/// <param name="e">Event args</param>
private void DurationInput_TextChanged(object sender, TextChangedEventArgs e)
{
if (!string.IsNullOrEmpty(DurationInput.Text))
Config.Instance.Timeout = Convert.ToDouble(DurationInput.Text);
}
/// <summary>
/// Handles checkbox changes for minimizing
/// </summary>
/// <param name="sender">Checkbox object</param>
/// <param name="e">Event args</param>
private void MinimizeCheckBox_Click(object sender, RoutedEventArgs e)
{
if (MinimizeCheckBox.IsChecked.HasValue)
Config.Instance.MinimizeToTray = MinimizeCheckBox.IsChecked.Value;
}
/// <summary>
/// Handles checkbox changes for auto starting
/// </summary>
/// <param name="sender">Checkbox object</param>
/// <param name="e">Event args</param>
private void AutoStartCheckBox_Click(object sender, RoutedEventArgs e)
{
if (AutoStartCheckBox.IsChecked.HasValue)
Config.Instance.AutoStartDiscord = AutoStartCheckBox.IsChecked.Value;
}
/// <summary>
/// Handles text changing in the token input
/// </summary>
/// <param name="sender">Textbox object</param>
/// <param name="e">Event args</param>
private void TokenInput_TextChanged(object sender, TextChangedEventArgs e)
=> Config.Instance.Token = TokenInput.Text;
/// <summary>
/// Handles text changing in the prefix input
/// </summary>
/// <param name="sender">Textbox input</param>
/// <param name="e">Event args</param>
private void PrefixInput_TextChanged(object sender, TextChangedEventArgs e)
=> Config.Instance.Prefix = PrefixInput.Text;
/// <summary>
/// Handles text changing in the gif length input
/// </summary>
/// <param name="sender">Textbox object</param>
/// <param name="e">Event args</param>
private void GifLengthInput_TextChanged(object sender, TextChangedEventArgs e)
{
if (!string.IsNullOrEmpty(GifLengthInput.Text))
Config.Instance.GifLength = int.Parse(GifLengthInput.Text);
}
/// <summary>
/// Handles checkbox changes for gif fps
/// </summary>
/// <param name="sender">Checkbox object</param>
/// <param name="e">Event args</param>
private void GifFps_Click(object sender, RoutedEventArgs e)
{
if (GifFps.IsChecked.HasValue)
{
Config.Instance.GifFps = GifFps.IsChecked.Value;
CustomGifFpsInput.IsEnabled = !GifFps.IsChecked.Value;
}
}
/// <summary>
/// Handles text changing in the custom gif fps input
/// </summary>
/// <param name="sender">Textbox input</param>
/// <param name="e">Event args</param>
private void CustomGifFpsInput_TextChanged(object sender, TextChangedEventArgs e)
=> Config.Instance.CustomGifFps = int.Parse(CustomGifFpsInput.Text);
/// <summary>
/// Handles changing the gif resolution selection
/// </summary>
/// <param name="sender">Combobox object</param>
/// <param name="e">Event args</param>
private void GifResolutionSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
Config.Instance.GifCamWidth = Convert.ToInt32(((string)GifResolutionSelector.SelectedItem).Split('x').First());
Config.Instance.GifCamHeight = Convert.ToInt32(((string)GifResolutionSelector.SelectedItem).Split('x').Last());
}
catch (Exception) { /* ignored */ }
}
/// <summary>
/// Handles text changing in the MicDelay input
/// </summary>
/// <param name="sender">Textbox object</param>
/// <param name="e">Event args</param>
private void MicDelayTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (!string.IsNullOrEmpty(MicDelayTxtBx.Text))
Config.Instance.MicTimer = Convert.ToInt32(MicDelayTxtBx.Text);
}
/// <summary>
/// Handles text changing in the MicLength input
/// </summary>
/// <param name="sender">Textbox object</param>
/// <param name="e">Event args</param>
private void MicLengthTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (!string.IsNullOrEmpty(MicLengthTxtBx.Text))
Config.Instance.MicLength = Convert.ToInt32(MicLengthTxtBx.Text);
}
/// <summary>
/// Handles clicking the Screenshot test button
/// </summary>
/// <param name="sender">Button object</param>
/// <param name="e">Event args</param>
private async void TestScreenshotButton_Click(object sender, RoutedEventArgs e)
=> await Screenshot.TestScreenshotAsync(float.Parse(BlurInput.Text));
}
}