/
SlideShow.cs
635 lines (552 loc) · 17.3 KB
/
SlideShow.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Input;
using System.Diagnostics;
using Microsoft.Win32;
namespace SlideDiscWPF
{
class SlideShow : Grid
{
/// <summary>
/// Slideshow Settings
/// </summary>
public class Settings
{
List<string> m_paths = new List<string>();
public string RootPath { get; set; }
public IList<string> Paths { get { return m_paths; } }
public int? AdvanceTime { get; set; } // In milliseconds
public int? FadeTime { get; set; } // In milliseconds
public bool? ShowMetadata { get; set; }
}
const string cAtBeginning = "Beginning of collection.";
const string cNoImagesFound = "No Photos or Videos found!";
static string[] sIncludeExtensions = new string[] { ".jpg", ".jpeg", ".avi", ".wmv", ".mpg", ".mpeg", ".mp4", ".mov" };
static readonly TimeSpan cMinFadeTime = TimeSpan.FromSeconds(0.25);
const int cMaxWaitForLoad = 30000; // In milliseconds
const int cMessageDisplayPeriod = 1000; // In milliseconds
const int cHideCursorAfter = 3000; // In milliseconds;
static readonly Duration sLongVideoThreshold = new Duration(new TimeSpan(0, 0, 40)); // 40 Seconds
const int cLongVideoLimit = 30000; // In milliseconds
private delegate void TickHandler();
System.Threading.Timer fTimer;
TickHandler fTickHandler;
int fTimeOfLastTick;
bool fInTick;
int fAutoAdvanceAt = 0;
int fAutoAdvanceNextPeriod = 0;
int fCursorDisplayedAt = 0;
Point fLastMousePosition; // = 0,0
bool fDelayed = false; // AutoAdvanceIn set to a large value but not paused.
bool fDirectionIsForward = true;
int fClearMessageAt = 0;
private SlidePanel fFormerPanel;
private SlidePanel fActivePanel;
private SlidePanel fNextPanel;
private TextBlock fMessage;
string fRootPath = string.Empty;
private FolderTreeEnumerator fEnumerator = new FolderTreeEnumerator();
#region Construction
public SlideShow()
{
fEnumerator.IncludeExtensions = sIncludeExtensions;
BeginInit();
fFormerPanel = new TextPanel();
fActivePanel = new TextPanel();
fNextPanel = new TextPanel();
fNextPanel.Opacity = 0;
Children.Add(fFormerPanel);
Children.Add(fActivePanel);
Children.Add(fNextPanel);
fTickHandler = new TickHandler(Tick);
fTimer = new System.Threading.Timer(RawTick, null, 500, 500);
EndInit();
}
#endregion // Construction
#region Properties
public string RootPath
{
get { return fRootPath; }
set { fRootPath = value; }
}
int fAdvanceTime = 3000;
public int AdvanceTime
{
get { return fAdvanceTime; }
set { fAdvanceTime = value; }
}
int fDelayAdvanceTime = 15000;
public int DelayAdvanceTime
{
get { return fDelayAdvanceTime; }
set { fDelayAdvanceTime = value; }
}
// fFadeTime is a TimeSpan while FadeTime is in milliseconds
TimeSpan fFadeTime = TimeSpan.FromSeconds(0.75);
public int FadeTime
{
get { return (int)fFadeTime.TotalMilliseconds; }
set { fFadeTime = TimeSpan.FromMilliseconds(value); }
}
bool fMuted = true;
public bool IsMuted
{
get { return fMuted; }
set
{
if (value != fMuted)
{
fMuted = value;
fActivePanel.IsMuted = value;
}
}
}
public bool ToggleMuted()
{
IsMuted = !fMuted;
Message(fMuted ? "Mute" : "Unmute");
return fMuted;
}
bool fTruncateVideo = true;
public bool TruncateVideo
{
get { return fTruncateVideo; }
set
{
if (value != fTruncateVideo)
{
fTruncateVideo = value;
if (fActivePanel.PanelState == PanelState.Playing)
{
if (fTruncateVideo)
{
AutoAdvanceIn(cLongVideoLimit);
Debug.WriteLine("Truncating video to {0} seconds.", cLongVideoLimit / 1000);
}
else
{
AutoAdvanceIn(-1); // Allow to play to the end.
Debug.WriteLine("Allowing {0} second video to play to end.", fActivePanel.Duration.TimeSpan.TotalSeconds);
}
}
}
}
}
public bool ToggleTruncateVideo()
{
TruncateVideo = !fTruncateVideo;
Message(fTruncateVideo ? "Truncate On" : "Truncate Off");
return fTruncateVideo;
}
bool fShowMetadata = true;
public bool ShowMetadata
{
get { return fShowMetadata; }
set
{
if (fShowMetadata != value)
{
fShowMetadata = value;
fFormerPanel.ShowMetadata = value;
fActivePanel.ShowMetadata = value;
fNextPanel.ShowMetadata = value;
}
}
}
public bool ToggleShowMetadata()
{
ShowMetadata = !fShowMetadata;
Message(fShowMetadata ? "Metadata On" : "Metadata Off");
return fShowMetadata;
}
public void ToggleTag(string tag)
{
if (fActivePanel != null)
{
fActivePanel.ToggleTag(tag);
}
}
public string[] SelectedDirectories
{
get { return fEnumerator.RootDirectories; }
set { fEnumerator.RootDirectories = value; }
}
#endregion // Properties
#region // Public Methods
public void PanelStateChanged(SlidePanel panel, PanelState oldState, PanelState newState)
{
Debug.WriteLine(string.Format("State changed from {0} to {1}", oldState, newState));
// Must be the active panel to matter
if (!Object.ReferenceEquals(panel, fActivePanel))
{
return;
}
switch(newState)
{
case PanelState.Still:
if (oldState == PanelState.Paused)
{
Next();
}
else
{
AutoAdvanceIn(fAutoAdvanceNextPeriod == 0 ? fAdvanceTime : fAutoAdvanceNextPeriod);
}
break;
case PanelState.Playing:
if (fTruncateVideo && Duration.Compare(panel.Duration, sLongVideoThreshold) >= 0)
{
AutoAdvanceIn(cLongVideoLimit);
Debug.WriteLine("Truncating {0} second video.", panel.Duration.TimeSpan.TotalSeconds);
}
else
{
AutoAdvanceIn(-1); // Clear auto-advance
}
break;
case PanelState.Paused:
AutoAdvanceIn(-1); // Clear auto-advance
break;
case PanelState.Completed:
AutoAdvanceIn(fAdvanceTime);
break;
}
}
public void Next(bool delayAdvance)
{
if (fNextPanel == null || fNextPanel.PanelState == PanelState.Init)
{
AutoAdvanceIn(1, fDelayAdvanceTime);
}
else
{
Next();
if (delayAdvance && fAutoAdvanceAt != 0)
{
AutoAdvanceIn(fDelayAdvanceTime);
}
}
}
public void Prev(bool delayAdvance)
{
Prev();
if (delayAdvance && fAutoAdvanceAt != 0)
{
AutoAdvanceIn(fDelayAdvanceTime);
}
}
public void Pause()
{
fActivePanel.Pause();
}
public void UnPause()
{
fActivePanel.UnPause();
}
public void TogglePause()
{
if (fActivePanel.PanelState == PanelState.Paused)
{
fActivePanel.UnPause();
Message("Play");
}
else if (fDelayed)
{
Next();
Message("Play");
}
else
{
fActivePanel.Pause();
Message("Pause");
}
}
public void Start(int advanceCount)
{
if (advanceCount > 0)
{
EmptyDelegate nextDelegate = new EmptyDelegate(Next);
while (advanceCount-- > 0)
{
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, nextDelegate);
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, nextDelegate);
}
}
else if (fActivePanel.PanelState == PanelState.Paused)
{
fActivePanel.UnPause();
}
}
public void Start()
{
Start(2);
}
public string CurrentSlidePath
{
get
{
return fActivePanel.Uri?.OriginalString ?? string.Empty;
}
set
{
if (!string.IsNullOrEmpty(value))
{
fEnumerator.SetCurrent(value);
fEnumerator.MovePrev();
}
}
}
public Settings GetSettings()
{
Settings value = new Settings();
value.RootPath = fRootPath;
foreach(var path in fEnumerator.RootDirectories)
{
value.Paths.Add(path);
}
// For now, we don't persist FadeTime, AdvanceTime, or ShowMetadata
return value;
}
public void LoadSettings(Settings value)
{
fRootPath = value.RootPath;
if (string.IsNullOrEmpty(fRootPath))
{
fRootPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
}
if (value.Paths.Count > 0)
{
fEnumerator.RootDirectories = value.Paths.ToArray();
}
else
{
fEnumerator.RootDirectories = new string[] { fRootPath };
}
if (value.FadeTime.HasValue) FadeTime = value.FadeTime.Value;
if (value.AdvanceTime.HasValue) AdvanceTime = value.AdvanceTime.Value;
if (value.ShowMetadata.HasValue) ShowMetadata = value.ShowMetadata.Value;
}
public void Message(string msg)
{
if (fMessage != null)
{
Children.Remove(fMessage);
fMessage = null;
}
if (!string.IsNullOrEmpty(msg))
{
TextBlock txtblk = new TextBlock();
txtblk.BeginInit();
txtblk.Text = msg;
txtblk.FontSize = 28;
txtblk.Foreground = Brushes.Crimson;
txtblk.HorizontalAlignment = HorizontalAlignment.Left;
txtblk.VerticalAlignment = VerticalAlignment.Bottom;
txtblk.EndInit();
Children.Add(txtblk);
fMessage = txtblk;
fClearMessageAt = Environment.TickCount + cMessageDisplayPeriod;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
Point fMousePos = e.GetPosition(this);
double distance = Math.Abs(fMousePos.X - fLastMousePosition.X) + Math.Abs(fMousePos.Y - fLastMousePosition.Y);
if (distance >= 2)
{
fLastMousePosition = fMousePos;
if (fCursorDisplayedAt == 0)
{
Cursor = null;
}
fCursorDisplayedAt = fTimeOfLastTick;
}
base.OnMouseMove(e);
}
#endregion
#region Private Operations
public void Next()
{
Children.Remove(fFormerPanel);
fFormerPanel.BeginAnimation(Panel.OpacityProperty, null); // Remove animation (not required but this assists the garbage collector)
fFormerPanel.Done(); // Notify former panel that it is no longer in use.
// Memory leak checking
/*
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect(5, GCCollectionMode.Forced);
*/
// Rotate the panels down
fFormerPanel = fActivePanel;
fActivePanel = fNextPanel;
fNextPanel = null;
// Stop the former panel
fFormerPanel.Stop();
// Animate the next panel in and play it
if (fFadeTime >= cMinFadeTime)
{
DoubleAnimation animation = new DoubleAnimation(0.0, 1.0, fFadeTime);
fActivePanel.BeginAnimation(Panel.OpacityProperty, animation, HandoffBehavior.SnapshotAndReplace);
}
else
{
fActivePanel.Opacity = 1.0;
}
fActivePanel.IsMuted = fMuted;
fActivePanel.Play();
if (!fDirectionIsForward)
{
fEnumerator.MoveNext();
fEnumerator.MoveNext();
fDirectionIsForward = true;
}
// Prep the next panel
fEnumerator.MoveNext(AdvanceFlags.Wrap);
string filename = fEnumerator.Current;
if (string.IsNullOrEmpty(filename))
{
fNextPanel = new TextPanel(cNoImagesFound);
}
else
{
fNextPanel = SlidePanel.Load(new Uri(filename));
fNextPanel.ShowMetadata = fShowMetadata;
}
fNextPanel.Opacity = 0.0;
Children.Add(fNextPanel);
}
public void Prev()
{
if (fFormerPanel == null || fFormerPanel is TextPanel)
{
Message("At beginning.");
return;
}
// Make sure we're ready for a reversal
if (fFormerPanel.PanelState == PanelState.Init)
{
Message("Waiting for previous media to load...");
return;
}
// Remove the prepped next panel
Children.Remove(fNextPanel);
fNextPanel.Done(); // Notify next panel that it is no longer in use.
// Reverse-rotate the panels
fNextPanel = fActivePanel;
fActivePanel = fFormerPanel;
fFormerPanel = null;
// Stop the formerly active panel
fNextPanel.Stop();
// Make it transparent again (remove existing animation)
fNextPanel.BeginAnimation(Panel.OpacityProperty, null, HandoffBehavior.SnapshotAndReplace);
fNextPanel.Opacity = 0.0;
Debug.Assert(fActivePanel.Opacity == 1.0); // Ensure that the new active panel is visible
Debug.WriteLine("New Active Panel: " + fActivePanel.Uri);
// Play the new active panel
fActivePanel.IsMuted = fMuted;
fActivePanel.Play();
if (fDirectionIsForward)
{
fEnumerator.MovePrev();
fEnumerator.MovePrev();
fDirectionIsForward = false;
}
// Load up a replacement former panel
if (!fEnumerator.MovePrev() || string.IsNullOrEmpty(fEnumerator.Current))
{
fFormerPanel = new TextPanel("At Beginning");
}
else
{
fFormerPanel = SlidePanel.Load(new Uri(fEnumerator.Current));
fFormerPanel.ShowMetadata = fShowMetadata;
}
Debug.Assert(fFormerPanel.Opacity == 1.0);
Children.Insert(0, fFormerPanel);
}
public void AutoAdvanceIn(int milliSeconds)
{
AutoAdvanceIn(milliSeconds, fAdvanceTime);
}
public void AutoAdvanceIn(int milliSeconds, int nextAdvancePeriod)
{
Debug.WriteLine(string.Format("AutoAdvanceIn: {0}", milliSeconds));
fDelayed = (milliSeconds > fAdvanceTime);
if (milliSeconds < 0)
{
fAutoAdvanceAt = 0; // Clear auto-advance
return;
}
unchecked
{
if (fInTick)
{
fAutoAdvanceAt = fTimeOfLastTick + milliSeconds;
}
else
{
fAutoAdvanceAt = Environment.TickCount + milliSeconds;
}
if (fAutoAdvanceAt == 0) fAutoAdvanceAt = 1;
}
fAutoAdvanceNextPeriod = nextAdvancePeriod;
}
private void RawTick(object state)
{
int tickCount = Environment.TickCount;
fTimeOfLastTick = (tickCount != 0) ? tickCount : 1;
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Input, fTickHandler);
}
private void Tick()
{
fInTick = true;
unchecked
{
// Hide the mouse cursor if it's been idle for a while
if (fCursorDisplayedAt != 0 && fTimeOfLastTick - fCursorDisplayedAt > cHideCursorAfter)
{
Cursor = Cursors.None;
fCursorDisplayedAt = 0;
}
if (fAutoAdvanceAt != 0)
{
int timeSinceAutoAdvance = fTimeOfLastTick - fAutoAdvanceAt;
// Subtract and compare to zero rather than just compare because of wrapping issues
if (timeSinceAutoAdvance > 0)
{
if (fNextPanel.PanelState != PanelState.Init || timeSinceAutoAdvance > cMaxWaitForLoad)
{
fAutoAdvanceAt = 0;
if (fActivePanel.PanelState == PanelState.Paused)
{
Debug.WriteLine("AutoPlay");
fActivePanel.Play();
}
else
{
Debug.WriteLine("AutoAdvance");
Next();
}
}
else
{
Message("Waiting for media to load...");
Debug.WriteLine(string.Format("Waiting for next image to load: name={0}, tick={1}", fNextPanel.Uri, fTimeOfLastTick - fAutoAdvanceAt));
}
}
}
if (fMessage != null && fTimeOfLastTick - fClearMessageAt > 0)
{
Message(null);
}
}
fInTick = false;
}
#endregion // Private operations
}
}