-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
384 lines (350 loc) · 12.9 KB
/
MainWindow.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
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
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Windows.Media;
namespace bgmPlayer
{
public partial class MainWindow : Window
{
private readonly Timer timer = Timer.Instance;
private readonly DispatcherTimer dispatcherTimer;
private readonly PersistedState? initState;
private bool allowControlBySMTC = false;
private int currentVolume = 100;
public MainWindow()
{
dispatcherTimer = new DispatcherTimer();
initState = PersistedStateManager.LoadState();
InitializeComponent();
AudioPathManager.Init(PersistedStateManager.LoadState() ?? new PersistedState());
AudioPathManager.InitTextBlock(IntroField, LoopField);
SMTCManager.InitSMTC(OnPlayPause);
SMTCManager.UpdateTitle(AudioPathManager.Intro, AudioPathManager.Loop);
SMTCManager.UpdateThumbnail();
InitVolume();
InitCheckbox();
InitTimer();
#if ME
InitBackgroundImage();
InitTitleOption();
#endif
UpdateAudioControlButton(AudioPlayer.CurrentState);
AllowChooseFile(AudioPlayer.IsStopped);
Title = SMTCManager.WindowTitle ?? SMTCManager.Title ?? AppConstants.DEFAULT_MUSIC_TITLE;
AudioPlayer.StateChanged += UpdateAudioControlButton;
}
#region Initialize
private void InitVolume()
{
if (initState == null)
Debug.WriteLine("InitVolume: config data is null, set volume to default value.");
else if (initState.Volume == null)
Debug.WriteLine("InitVolume: configData doesn't have Volume value");
else if (initState.Volume >= 0 && initState.Volume <= AppConstants.VOLUME_SCALE)
currentVolume = (int)initState.Volume;
else
currentVolume = (int)AppConstants.VOLUME_SCALE;
VolSlider.Value = currentVolume;
AudioPlayer.SetVolume(currentVolume / AppConstants.VOLUME_SCALE);
}
private void InitCheckbox()
{
if (initState != null && initState.AutoFill != null)
autoFill.IsChecked = initState.AutoFill;
else
{
autoFill.IsChecked = false;
PersistedStateManager.SaveState(AutoFill: false);
}
AudioPathManager.AutoFill = autoFill.IsChecked ?? false;
}
private void InitTimer()
{
dispatcherTimer.Interval = TimeSpan.FromSeconds(0.5);
dispatcherTimer.Tick += (o, e) =>
{
TimerBlock.Text = "Played: " + timer.GetParsedElapsed();
};
dispatcherTimer.Start();
StateChanged += (o, e) =>
{
if (WindowState == WindowState.Minimized) dispatcherTimer.Stop();
else dispatcherTimer.Start();
};
}
#if ME
private void InitBackgroundImage()
{
System.Windows.Media.ImageBrush background = new(new BitmapImage(new Uri("pack://application:,,,/img/schwarz_blured.png")))
{
Stretch = System.Windows.Media.Stretch.UniformToFill
};
Background = background;
}
/// <summary>
/// Option 1: show all
/// Option 2: Official title only
/// Option 3: Translated title only (if applicable, else show official title)
/// </summary>
private void InitTitleOption()
{
TitleOption.Visibility = Visibility.Visible;
switch (initState?.TitleOption)
{
case 2:
titleOfficialOnly.IsChecked = true;
break;
case 3:
titleTransOnly.IsChecked = true;
break;
default:
titleShowAll.IsChecked = true;
break;
}
}
#endif
#endregion
#region Button handler
private void Intro_Click(object sender, RoutedEventArgs e)
{
allowControlBySMTC = false;
if (AudioPathManager.OpenIntroPathDialog() != null)
{
SMTCManager.UpdateTitle(AudioPathManager.Intro, AudioPathManager.Loop);
}
allowControlBySMTC = true;
}
private void Loop_Click(object sender, RoutedEventArgs e)
{
allowControlBySMTC = false;
if (AudioPathManager.OpenLoopPathDialog() != null)
{
SMTCManager.UpdateTitle(AudioPathManager.Intro, AudioPathManager.Loop);
}
allowControlBySMTC = true;
}
private void PlayPause_Click(object? sender, RoutedEventArgs? e)
{
if (AudioPathManager.Intro == string.Empty && AudioPathManager.Loop == string.Empty)
{
MessageBox.Show(AppConstants.FILE_MISSING, AppConstants.USER_ERROR_TITLE);
return;
}
if (AudioPlayer.IsStopped)
{
AllowChooseFile(false);
UpdateAudioControlButton(AudioState.PLAY);
allowControlBySMTC = true;
TaskbarChangeIconToPause();
if (AudioPlayer.PlayBGM(AudioPathManager.Intro, AudioPathManager.Loop) == AudioPlayerState.FAILED)
{
MessageBox.Show("Unknown error!", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
Stop_Click(null, null);
return;
}
timer.Start(); // Moved this after AudioPlayer.PlayBGM call, because sometimes opening stream from disk takes too much time which makes timer inaccurate
return;
}
if (AudioPlayer.IsPaused)
{
try
{
AudioPlayer.Continue();
UpdateAudioControlButton(AudioState.PLAY);
timer.Start();
}
catch (NAudio.MmException)
{
MessageBox.Show(AppConstants.AUDIO_DEVICE_ERROR_MSG, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
Stop_Click(null, null);
}
return;
}
if (AudioPlayer.IsPlaying)
{
AudioPlayer.Pause();
UpdateAudioControlButton(AudioState.PAUSE);
TaskbarChangeIconToPlay();
timer.Stop();
return;
}
}
private void Stop_Click(object? sender, RoutedEventArgs? e)
{
AudioPlayer.Stop();
AllowChooseFile(true);
UpdateAudioControlButton(AudioState.STOP);
TaskbarChangeIconToPlay();
timer.Stop();
timer.Reset();
}
private void RemoveIntro_Click(object sender, RoutedEventArgs e)
{
if (AudioPathManager.Intro == string.Empty) return;
if (AudioPlayer.IsStopped)
{
AudioPathManager.Intro = string.Empty;
SMTCManager.UpdateTitle(AudioPathManager.Intro, AudioPathManager.Loop);
}
else
{
MessageBox.Show("Stop music before removing music file.");
}
}
private void RemoveLoop_Click(object sender, RoutedEventArgs e)
{
if (AudioPathManager.Loop == string.Empty) return;
if (AudioPlayer.IsStopped)
{
AudioPathManager.Loop = string.Empty;
SMTCManager.UpdateTitle(AudioPathManager.Intro, AudioPathManager.Loop);
}
else
{
MessageBox.Show("Stop music before removing music file.");
}
}
private void UpdateTitleOption(object sender, RoutedEventArgs e)
{
#if ME
RadioButton? opttion = sender as RadioButton;
switch (opttion?.Name)
{
case "titleOfficialOnly":
PersistedStateManager.SaveState(TitleOption: 2);
break;
case "titleTransOnly":
PersistedStateManager.SaveState(TitleOption: 3);
break;
default:
PersistedStateManager.SaveState(TitleOption: 1);
break;
}
SMTCManager.UpdateTitle(AudioPathManager.Intro, AudioPathManager.Loop);
#endif
}
#endregion
#region Taskbar handler
private void TaskbarPlayPause_handler(object? sender, EventArgs? e)
{
if ((AudioPlayer.IsPaused || AudioPlayer.IsStopped) && SMTCManager.IsEnable)
{
TaskbarChangeIconToPause();
PlayPause_Click(sender, null);
}
else if (AudioPlayer.IsPlaying)
{
TaskbarChangeIconToPlay();
PlayPause_Click(sender, null);
}
else return;
}
private void TaskbarStop_handler(object? sender, EventArgs? e)
{
if (!AudioPlayer.IsStopped)
Stop_Click(sender, null);
}
#endregion
#region Event handler
private void OnPlayPause(SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs e)
{
if (!allowControlBySMTC) return;
switch (e.Button)
{
case SystemMediaTransportControlsButton.Play:
Dispatcher.Invoke(() =>
{
TaskbarPlayPause_handler(sender, null);
});
break;
case SystemMediaTransportControlsButton.Pause:
Dispatcher.Invoke(() =>
{
TaskbarPlayPause_handler(sender, null);
});
break;
case SystemMediaTransportControlsButton.Stop:
Dispatcher.Invoke(() =>
{
TaskbarStop_handler(sender, null);
});
break;
default:
break;
}
}
private void OnChecked(object sender, RoutedEventArgs e)
{
PersistedStateManager.SaveState(AutoFill: true);
AudioPathManager.AutoFill = true;
}
private void OnUnchecked(object sender, RoutedEventArgs e)
{
PersistedStateManager.SaveState(AutoFill: false);
AudioPathManager.AutoFill = false;
}
private void VolSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
SetVolume((int)VolSlider.Value);
}
private void Window_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
SetVolume(Math.Clamp(currentVolume + (e.Delta / AppConstants.MOUSE_WHEEL_SCALE), 0, 100));
}
#if DEBUG
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
GC.Collect();
}
#endif
#endregion
#region Private helper methods
private void AllowChooseFile(bool isAllow)
{
intro_button.IsEnabled = isAllow;
loop_button.IsEnabled = isAllow;
}
private void TaskbarChangeIconToPlay()
{
play_pause_taskbar.ImageSource = new BitmapImage(new Uri("pack://application:,,,/img/play.png"));
}
private void TaskbarChangeIconToPause()
{
play_pause_taskbar.ImageSource = new BitmapImage(new Uri("pack://application:,,,/img/pause.png"));
}
private void SetVolume(int Volume)
{
currentVolume = Volume;
VolSlider.Value = Volume;
VolValue.Text = Volume.ToString();
AudioPlayer.SetVolume(Volume / AppConstants.VOLUME_SCALE);
PersistedStateManager.SaveState(Volume: Volume);
}
private void UpdateAudioControlButton(AudioState audioState)
{
switch (audioState)
{
case AudioState.PLAY:
play_pause_button.Content = "Pause";
TaskbarChangeIconToPause();
stop_button.IsEnabled = true;
break;
case AudioState.PAUSE:
play_pause_button.Content = "Play";
TaskbarChangeIconToPlay();
stop_button.IsEnabled = true;
break;
case AudioState.STOP:
play_pause_button.Content = "Play";
TaskbarChangeIconToPlay();
stop_button.IsEnabled = false;
break;
}
}
#endregion
}
}