-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
554 lines (500 loc) · 21.7 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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Threading;
namespace LoadRetimer {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public static double frameRate = 30;
private DispatcherTimer timerVideo;
private DispatcherTimer timerAnalyzer;
int loadType = 0;
List<string> customLoadNames;
readonly string[] anyPercentLoadNames = new string[] {
"1-1 Start",
"1-1 End",
"1-2 Start",
"1-2 Pipe 1",
"1-2 Pipe 2",
"1-2 End",
"1-3 Start",
"1-3 Pipe",
"1-3 End",
"1-C Start",
"1-C End",
"5-1 Start",
"5-1 Pipe",
"5-1 End",
"Piranha Start",
"Piranha End",
"5-3 Start",
"5-3 Pipe",
"5-3 End",
"5-T Start",
"5-T Door",
"5-T End",
"5-4 Start",
"5-4 Pipe 1",
"5-4 Pipe 2",
"5-4 End",
"5-GH Start",
"5-GH Door 1",
"5-GH Door 2",
"5-GH End",
"5-C Start",
"5-C End",
"8-1 Start",
"8-1 Pipe",
"8-1 End",
"8-2 Start",
"8-2 Pipe 1",
"8-2 Pipe 2",
"8-2 Pipe 3",
"8-2 End",
"8-7 Start",
"8-7 Pipe",
"8-7 End",
"8-A Start",
"8-A Pipe 1",
"8-A Pipe 2",
"8-A Pipe 3",
"8-A Fade 1",
"8-A End",
"8-C Start",
"8-C Pipe 1",
"8-C Pipe 2",
"8-C Door"
};
public MainWindow() {
InitializeComponent();
Unosquare.FFME.Library.FFmpegDirectory = @"./ffmpeg";
TotalRunInfo.SetName("Entire Run");
TotalRunInfo.MakeNonInclusive();
TotalRunInfo.MakeUnchangable();
Video.ScrubbingEnabled = false;
timerVideo = new DispatcherTimer {
Interval = TimeSpan.FromMilliseconds(5)
};
timerVideo.Tick += TimerVideo_Tick;
timerVideo.Start();
timerAnalyzer = new DispatcherTimer {
Interval = TimeSpan.FromMilliseconds(20)
};
timerAnalyzer.Tick += Analyzer_Tick;
timerAnalyzer.Start();
if (File.Exists("defaultNames.lns")) {
LoadCustomNames("defaultNames.lns");
} else {
customLoadNames = anyPercentLoadNames.ToList();
}
}
private void LoadCustomNames(string filename) {
customLoadNames = new List<string>();
foreach (string line in File.ReadLines(filename)) {
customLoadNames.Add(line);
}
string categoryName = customLoadNames[0];
((ComboBoxItem)LoadCategory.Items[0]).Content = categoryName;
customLoadNames.RemoveAt(0);
for (int i = 0; i < LoadBox.Items.Count; i++) {
string newName;
if (i < customLoadNames.Count) {
newName = customLoadNames[i];
} else {
newName = String.Format("Load {0}", i + 1);
}
((LoadInfo)((ListBoxItem)LoadBox.Items[i]).Content).Rename(newName);
}
}
private void ButtonPlayPause_Click(object sender, RoutedEventArgs e) {
if (!Video.IsOpen) return;
if (Video.MediaState == Unosquare.FFME.Common.MediaPlaybackState.Play) {
Video.Pause();
PlayPause.Content = "Play";
} else if (Video.MediaState == Unosquare.FFME.Common.MediaPlaybackState.Pause) {
Video.Play();
PlayPause.Content = "Pause";
} else if (Video.MediaState == Unosquare.FFME.Common.MediaPlaybackState.Stop) {
Video.Position = new TimeSpan(0);
Video.Play();
PlayPause.Content = "Pause";
}
}
private void FrameBack_Click(object sender, RoutedEventArgs e) {
if (!Video.IsOpen) return;
Video.StepBackward();
}
private void FrameForward_Click(object sender, RoutedEventArgs e) {
if (!Video.IsOpen) return;
Video.StepForward();
}
private void Info_Click(object sender, RoutedEventArgs e) {
new Info().ShowDialog();
}
private LoadInfo CreateNewLoadInfo() {
ListBoxItem lbi = new ListBoxItem();
LoadInfo newLoad = new LoadInfo(String.Format("Load {0}", LoadBox.Items.Count + 1));
if (loadType == 0) {
if (LoadBox.Items.Count < customLoadNames.Count) {
newLoad.SetName(customLoadNames[LoadBox.Items.Count]);
}
}
lbi.Content = newLoad;
LoadBox.Items.Add(lbi);
LoadBox.ScrollIntoView(lbi);
LoadBox.SelectedItem = lbi;
LoadBox.Focus();
return newLoad;
}
private void BeginLoad_Click(object sender, RoutedEventArgs e) {
if (LoadBox.SelectedItem == null) {
CreateNewLoadInfo();
LoadInfo selLoad = (LoadInfo)((ListBoxItem)LoadBox.SelectedItem).Content;
selLoad.SetBegin(TrueFramePos());
} else {
LoadInfo selLoad = (LoadInfo)((ListBoxItem)LoadBox.SelectedItem).Content;
selLoad.SetBegin(TrueFramePos());
LoadBox.SelectedItem = null;
}
}
private void EndLoad_Click(object sender, RoutedEventArgs e) {
if (LoadBox.SelectedItem == null) {
CreateNewLoadInfo();
LoadInfo selLoad = (LoadInfo)((ListBoxItem)LoadBox.SelectedItem).Content;
selLoad.SetEnd(TrueFramePos());
} else {
LoadInfo selLoad = (LoadInfo)((ListBoxItem)LoadBox.SelectedItem).Content;
selLoad.SetEnd(TrueFramePos());
LoadBox.SelectedItem = null;
}
}
private void Retime_Click(object sender, RoutedEventArgs e) {
if (TotalRunInfo.FrameDuration() == 0) {
System.Windows.MessageBox.Show("You must retime the runs without loads before calculating the final retime.");
return;
}
long loadFrames = 0;
for (int i = 0; i < LoadBox.Items.Count; i++) {
LoadInfo li = (LoadInfo)((ListBoxItem)LoadBox.Items[i]).Content;
loadFrames += li.FrameDuration();
}
var tsLoads = new TimeSpan((long)(loadFrames / frameRate * 10_000_000));
var tsWLoads = new TimeSpan((long)(TotalRunInfo.FrameDuration() / frameRate * 10_000_000));
new FinalRetimeWindow(tsWLoads, tsLoads).ShowDialog();
}
private void OpenFile_Click(object sender, RoutedEventArgs e) {
var ofd = new OpenFileDialog {
Title = "Select a video",
Filter = "Video files (*.mp4, *.mkv, *.flv, *.wmv, *.avi)|*.mp4;*.mkv;*.flv;*.wmv;*.avi|All files (*.*)|*.*"
};
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
if (Video.IsOpen) {
Video.Close();
}
Video.Open(new Uri(ofd.FileName));
}
}
const byte CURR_VERSION = 3;
private void OpenLoadNames_Click(object sender, RoutedEventArgs e) {
var ofd = new OpenFileDialog {
Title = "Select load names",
Filter = "Load Retimer Name files (*.lns)|*.lns|All files (*.*)|*.*"
};
if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
LoadCustomNames(ofd.FileName);
}
private void OpenLoads_Click(object sender, RoutedEventArgs e) {
var ofd = new OpenFileDialog {
Title = "Select loads",
Filter = "Load Retimer files (*.lds)|*.lds|All files (*.*)|*.*"
};
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
var f = ofd.OpenFile();
var binReader = new BinaryReader(f);
int version = binReader.ReadByte();
bool hasTotalRun;
if (version >= 2) { // the version field didn't exist before
hasTotalRun = binReader.ReadByte() == 1;
} else {
hasTotalRun = version == 1;
}
uint loadCount;
if (version >= 3) {
loadCount = binReader.ReadUInt32();
} else {
loadCount = binReader.ReadByte();
}
frameRate = binReader.ReadDouble();
FPSLabel.Content = String.Format("FPS: {0:F2}", frameRate);
if (hasTotalRun) {
long startFrames = binReader.ReadUInt32();
long endFrames = binReader.ReadUInt32();
TotalRunInfo.SetBegin(new TimeSpan((long)(startFrames / frameRate * 10_000_000)));
TotalRunInfo.SetEnd(new TimeSpan((long)(endFrames / frameRate * 10_000_000)));
}
LoadBox.Items.Clear();
for (int i = 0; i < loadCount; i++) {
long startFrames = binReader.ReadUInt32();
long endFrames = binReader.ReadUInt32();
LoadInfo newLoad = CreateNewLoadInfo();
newLoad.SetBegin(new TimeSpan((long)(startFrames / frameRate * 10_000_000)));
newLoad.SetEnd(new TimeSpan((long)(endFrames / frameRate * 10_000_000)));
if (version >= 2) {
string loadName = binReader.ReadString();
newLoad.SetName(loadName);
}
}
f.Close();
}
}
private void SaveLoads_Click(object sender, RoutedEventArgs e) {
var ofd = new SaveFileDialog {
Title = "Save loads",
Filter = "Load Retimer files (*.lds)|*.lds|All files (*.*)|*.*"
};
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
var f = ofd.OpenFile();
var binWriter = new BinaryWriter(f);
binWriter.Write(CURR_VERSION);
if (TotalRunInfo.FrameDuration() > 0) {
binWriter.Write((byte)1);
} else {
binWriter.Write((byte)0);
}
binWriter.Write((UInt32)LoadBox.Items.Count);
binWriter.Write(frameRate);
if (TotalRunInfo.FrameDuration() > 0) {
binWriter.Write((UInt32)TotalRunInfo.frameStart);
binWriter.Write((UInt32)TotalRunInfo.frameEnd);
}
for (int i = 0; i < LoadBox.Items.Count; i++) {
LoadInfo li = (LoadInfo)((ListBoxItem)LoadBox.Items[i]).Content;
binWriter.Write((UInt32)li.frameStart);
binWriter.Write((UInt32)li.frameEnd);
binWriter.Write(li.LoadName.Text);
}
}
}
private void Video_MediaOpened(object sender, Unosquare.FFME.Common.MediaOpenedEventArgs e) {
FPSLabel.Content = String.Format("FPS: {0:F2}", Video.VideoFrameRate);
DurationLabel.Content = String.Format("Duration: {0:hh\\:mm\\:ss\\.fff}", Video.NaturalDuration);
frameRate = Video.VideoFrameRate;
}
private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) {
if (!Video.IsOpen) return;
if (FocusManager.GetFocusedElement(this) is System.Windows.Controls.TextBox) return;
if (e.Key == Key.A) {
Video.StepBackward();
e.Handled = true;
}
if (e.Key == Key.D) {
Video.StepForward();
e.Handled = true;
}
if (e.Key == Key.S) {
BeginLoad_Click(null, null);
e.Handled = true;
}
if (e.Key == Key.F) {
EndLoad_Click(null, null);
e.Handled = true;
}
if (e.Key == Key.Q) {
Video.Seek(Video.Position - new TimeSpan(0, 0, 5));
e.Handled = true;
}
if (e.Key == Key.E) {
Video.Seek(Video.Position + new TimeSpan(0, 0, 5));
e.Handled = true;
}
if (e.Key == Key.Space) {
ButtonPlayPause_Click(null, null);
e.Handled = true;
}
if (e.Key == Key.Delete || e.Key == Key.Back) {
if (LoadBox.SelectedItem != null) {
LoadBox.Items.Remove(LoadBox.SelectedItem);
}
}
}
private void StartRun_Click(object sender, RoutedEventArgs e) {
if (!Video.IsOpen) return;
TotalRunInfo.SetBegin(TrueFramePos());
}
private void EndRun_Click(object sender, RoutedEventArgs e) {
if (!Video.IsOpen) return;
TotalRunInfo.SetEnd(TrueFramePos());
}
private bool userChanging = false;
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) {
if (userChanging) {
if (!Video.IsOpen) return;
var ts = new TimeSpan((long)(Video.NaturalDuration.GetValueOrDefault().TotalSeconds * Slider.Value * 10_000));
Video.Position = ts + Video.PlaybackStartTime.GetValueOrDefault();
}
}
private void Slider_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e) {
userChanging = true;
}
private void Slider_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e) {
userChanging = false;
}
private enum AnalyzeState {
Idle,
SelectTL, SelectBR,
FindStartFast, FindEndFast,
FindStart, FindEnd,
};
private AnalyzeState analyzeState = AnalyzeState.SelectTL;
private void SeekNext_Click(object sender, RoutedEventArgs e) {
if (analyzeState == AnalyzeState.SelectTL) {
System.Windows.MessageBox.Show("Click on the top-left corner, then on the bottom-right corner of the game area.");
} else {
analyzeState = AnalyzeState.FindStart;
}
}
private void SeekPrev_Click(object sender, RoutedEventArgs e) {
if (analyzeState == AnalyzeState.SelectTL) {
System.Windows.MessageBox.Show("Click on the top-left corner, then on the bottom-right corner of the game area.");
} else {
analyzeState = AnalyzeState.FindEnd;
}
}
int rectX1 = 0, rectY1 = 0, rectX2 = 0, rectY2 = 0;
private void Video_MouseDown(object sender, MouseButtonEventArgs e) {
return; // turn this off for now
/*var m = Mouse.GetPosition(Video);
if (m.X < 0 || m.Y < 0 || m.X > Video.ActualWidth || m.Y > Video.ActualHeight) return;
var p = TransformToNaturalVideo(m);
if (analyzeState == AnalyzeState.SelectTL) {
Rect.Margin = new Thickness(m.X, m.Y, 0, 0);
rectX1 = (int) p.X;
rectY1 = (int) p.Y;
analyzeState = AnalyzeState.SelectBR;
} else if (analyzeState == AnalyzeState.SelectBR) {
double w = m.X - Rect.Margin.Left;
double h = m.Y - Rect.Margin.Top;
if (w <= 0 || h <= 0) return;
rectX2 = (int) p.X;
rectY2 = (int) p.Y;
Rect.Width = w;
Rect.Height = h;
}*/
}
private Point TransformToNaturalVideo(Point m) {
double videoRatio = (double)Video.NaturalVideoWidth / Video.NaturalVideoHeight;
double actualRatio = Video.ActualWidth / Video.ActualHeight;
double xOffset = 0;
double yOffset = 0;
if (videoRatio < actualRatio) {
xOffset = (Video.ActualWidth - (Video.ActualHeight / Video.NaturalVideoHeight * Video.NaturalVideoWidth)) / 2;
} else {
yOffset = (Video.ActualHeight - (Video.ActualWidth / Video.NaturalVideoWidth * Video.NaturalVideoHeight)) / 2;
}
double w = Video.ActualWidth - xOffset * 2;
double h = Video.ActualHeight - yOffset * 2;
int x = (int)((m.X - xOffset) * Video.NaturalVideoWidth / w);
int y = (int)((m.Y - yOffset) * Video.NaturalVideoHeight / h);
return new Point(x, y);
}
private Point TransformFromNaturalVideo(Point m) {
double videoRatio = (double)Video.NaturalVideoWidth / Video.NaturalVideoHeight;
double actualRatio = Video.ActualWidth / Video.ActualHeight;
double xOffset = 0;
double yOffset = 0;
if (videoRatio < actualRatio) {
xOffset = (Video.ActualWidth - (Video.ActualHeight / Video.NaturalVideoHeight * Video.NaturalVideoWidth)) / 2;
} else {
yOffset = (Video.ActualHeight - (Video.ActualWidth / Video.NaturalVideoWidth * Video.NaturalVideoHeight)) / 2;
}
double w = Video.ActualWidth - xOffset * 2;
double h = Video.ActualHeight - yOffset * 2;
int x = (int)(m.X * w / Video.NaturalVideoWidth + xOffset);
int y = (int)(m.Y * h / Video.NaturalVideoHeight + yOffset);
return new Point(x, y);
}
private void TimerVideo_Tick(object sender, object e) {
if (!Video.IsOpen) return;
var pos = Mouse.GetPosition(Video);
Magnifier.Viewbox = new Rect(pos.X - 10, pos.Y + 10, 20, 20);
var tmp = new TimeSpan((long)(TrueFramePos().TotalMilliseconds * 10_000));
TimePosition.Content = String.Format("{0:hh\\:mm\\:ss\\.fff}", Helper.RoundTimeSpanMillis(tmp));
if (userChanging) return;
Slider.Value = TrueFramePos().TotalMilliseconds / Video.NaturalDuration.GetValueOrDefault().TotalMilliseconds * 1000;
}
private async void Analyzer_Tick(object sender, object e) {
if (analyzeState == AnalyzeState.Idle || analyzeState == AnalyzeState.SelectBR || analyzeState == AnalyzeState.SelectTL) return;
if (Video.IsSeeking) return;
var bmp = await Video.CaptureBitmapAsync();
bool isBlack = IsBlack(bmp);
if (analyzeState == AnalyzeState.FindStart) {
if (isBlack) {
analyzeState = AnalyzeState.Idle;
} else {
await Video.StepForward();
}
} else if (analyzeState == AnalyzeState.FindEnd) {
if (isBlack) {
analyzeState = AnalyzeState.Idle;
} else {
await Video.StepBackward();
}
}
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
loadType = LoadCategory.SelectedIndex;
}
private System.Drawing.Bitmap tempBmp;
private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
var mouseWasDownOn = e.Source as FrameworkElement;
if (mouseWasDownOn != null) {
if (mouseWasDownOn is not (LoadInfo or System.Windows.Controls.Button)) {
LoadBox.SelectedItem = null;
}
}
}
private bool IsBlack(System.Drawing.Bitmap bitmap) {
tempBmp = bitmap.Clone(new System.Drawing.Rectangle(rectX1, rectY1, rectX2 - rectX1, rectY2 - rectY1), bitmap.PixelFormat);
int w = tempBmp.Width;
int h = tempBmp.Height;
for (double x = 0; x < 1; x += 0.05) {
for (double y = 0; y < 1; y += 0.05) {
if (tempBmp.GetPixel((int)(w * x), (int)(h * y)).GetBrightness() > 0.1) return false;
}
}
return true;
}
private void Window_SizeChanged(object sender, SizeChangedEventArgs e) {
if (rectX1 > 0 && rectY1 > 0 && rectX2 > 0 && rectY2 > 0) {
var p1 = TransformFromNaturalVideo(new Point(rectX1, rectY1));
var p2 = TransformFromNaturalVideo(new Point(rectX2, rectY2));
Rect.Margin = new Thickness(p1.X, p1.Y, 0, 0);
Rect.Width = p2.X - p1.X;
Rect.Height = p2.Y - p1.Y;
}
}
private TimeSpan TrueFramePos() {
if (Video.PlaybackStartTime == null) return new TimeSpan(0);
return (TimeSpan)(Video.FramePosition - Video.PlaybackStartTime);
}
}
class Helper {
public static TimeSpan RoundTimeSpanMillis(TimeSpan ts) {
long ticks = (long) Math.Round(ts.Ticks / 10_000.0) * 10_000;
var res = new TimeSpan(ticks);
return res;
}
public static TimeSpan CeilTimeSpanMillis(TimeSpan ts) {
long ticks = (long)Math.Ceiling(ts.Ticks / 10_000.0) * 10_000;
var res = new TimeSpan(ticks);
return res;
}
}
}