forked from changbowen/Ken-Burns-Slideshow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.vb
1718 lines (1599 loc) · 91.1 KB
/
MainWindow.xaml.vb
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Imports System.ComponentModel
Imports System.Threading
Class MainWindow
Public Shared ListOfPic As New System.Data.DataTable("ImageList")
Public Shared PicFormats() As String = {".jpg", ".jpeg", ".bmp", ".png", ".tif", ".tiff"}
Private BGMFormats() As String = {".mp3", ".wma", ".m4a", ".aac", ".wav", "asf"}
Public Shared config_path As String = "config.xml"
Public Shared config_instant_path As String = "config_instant.xml"
Private Shared ListOfMusic As New List(Of String)
Private ran As New Random
Private Shared player As New MediaPlayer With {.Volume = 0.5}
Private currentaudio As Integer = 0
Private playing As Boolean = False
Private audiofading As Boolean = False
Public Shared w, h As Double
Private position As Integer = 0
Private m As Integer = 0, mm As Integer = 0
Private pic As BitmapImage
Private pics As New Dictionary(Of String, BitmapImage)
Private picmove_sec As UInteger = 7
Private moveon As Boolean = True
Private aborting As Boolean = False
Private worker_pic As Thread
Private ctrlwindow As ControlWindow
Public Shared framerate As UInteger = 60
Public Shared duration As UInteger = 7 'only serves as a store. program will read duration value from picmove_sec.
Public Shared folders_image As New List(Of String)
Public Shared folders_music As New List(Of String)
Public Shared verticalLock As Boolean = True
Private verticalLockR As Double = 1.5
Public Shared resolutionLock As Boolean = True
Public Shared verticalOptimize As Boolean = True
Public Shared horizontalOptimize As Boolean = True
Public Shared fadeout As Boolean = True
Public Shared verticalOptimizeR As Double = 0.6
Public Shared horizontalOptimizeR As Double = 0.6
Public Shared transit As Integer = 0
Public Shared loadquality As Double = 1.2
Public Shared loadmode_next As Integer = 0 'only serves as a store. program will read duration value from loadmode.
Private loadmode As Integer = 0
Public Shared ScaleMode_Dic As New Dictionary(Of Integer, String)
Public Shared scalemode As Integer = 2
Public Shared blurmode As Integer = 0
Private Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esFlags As EXECUTION_STATE) As EXECUTION_STATE
Private ExecState_Set As Boolean
Public Shared nextcloseaction As CloseAction = CloseAction.FadeToBlack
Public Shared randomizeV As Boolean = False
Public Shared randomizeA As Boolean = False
Public Shared recursive_folder As Boolean = True
Public Shared recursive_music As Boolean = True
Public Shared showcontrol As Boolean = True
Private minCfgVer As Version = New Version(1, 5, 7, 0)
Private Enum EXECUTION_STATE As Integer
''' <summary>
''' Informs the system that the state being set should remain in effect until the next call that uses ES_CONTINUOUS and one of the other state flags is cleared.
''' </summary>
ES_CONTINUOUS = &H80000000
''' <summary>
''' Forces the display to be on by resetting the display idle timer.
''' </summary>
ES_DISPLAY_REQUIRED = &H2
''' <summary>
''' Forces the system to be in the working state by resetting the system idle timer.
''' </summary>
ES_SYSTEM_REQUIRED = &H1
End Enum
Public Enum CloseAction
DirectQuit
FadeToBlack
FadeToDesktop
End Enum
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
#If DEBUG Then
Topmost = False
#End If
w = Me.Width
h = Me.Height
Process.GetCurrentProcess.PriorityClass = ProcessPriorityClass.High
'initialize datatable
ListOfPic.Columns.Add("Path", GetType(String))
ListOfPic.Columns.Add("Date", GetType(String))
ListOfPic.Columns.Add("TitleSlide", GetType(Boolean)).DefaultValue = False
ListOfPic.Columns.Add("Text", GetType(String)).DefaultValue = ""
ListOfPic.Columns.Add("FontFamily", GetType(String)).DefaultValue = "Georgia"
ListOfPic.Columns.Add("FontSize", GetType(Double)).DefaultValue = CDbl(12)
ListOfPic.Columns.Add("FontColor", GetType(String)).DefaultValue = "White"
ListOfPic.Columns.Add("FontOffsetH", GetType(Double)).DefaultValue = CDbl(0)
ListOfPic.Columns.Add("FontOffsetV", GetType(Double)).DefaultValue = CDbl(0)
ListOfPic.PrimaryKey = {ListOfPic.Columns("Path")}
'loading other settings
ScaleMode_Dic.Add(2, Application.Current.Resources("(default) high"))
ScaleMode_Dic.Add(3, Application.Current.Resources("medium"))
ScaleMode_Dic.Add(1, Application.Current.Resources("low"))
'set working dir
IO.Directory.SetCurrentDirectory(IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly.Location))
'check cmd args and load config
Dim cmdargs = Environment.GetCommandLineArgs
'Dim cmdargs() As String = {"asdasd", "F:\Pictures\Giant"}
Dim config As XElement
If cmdargs.Length > 1 Then 'parametered start
If My.Computer.FileSystem.FileExists(config_instant_path) Then
config = XElement.Load(config_instant_path)
config.Element("PicDir").RemoveAll()
config.Element("Music").RemoveAll()
config_path = config_instant_path
ElseIf My.Computer.FileSystem.FileExists(config_path) Then
config = XElement.Load(config_path)
config.Element("PicDir").RemoveAll()
config.Element("Music").RemoveAll()
config_path = config_instant_path
Else 'generate necessary settings
config = New XElement("CfgRoot")
config.Add(New XElement("Version", FileVersionInfo.GetVersionInfo(Reflection.Assembly.GetExecutingAssembly().Location).FileVersion))
config.Add(New XElement("PicDir"))
config.Add(New XElement("Music"))
End If
For i = 1 To cmdargs.Length - 1
config.Element("PicDir").Add(New XElement("dir", New XCData(cmdargs(i))))
config.Element("Music").Add(New XElement("dir", New XCData(cmdargs(i))))
Next
Else 'normal start. loading config.xml
Do
Try
config = XElement.Load(config_path)
Exit Do
Catch
If MsgBox(Application.Current.Resources("msg_loadcfgerr"), MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
If New OptWindow().ShowDialog = False Then
Close()
Exit Sub
End If
Else
Close()
Exit Sub
End If
End Try
Loop
End If
'config.xml version check
If config.Elements("Version").Any Then
Dim xmlver As New Version(config.Element("Version").Value)
If xmlver < minCfgVer Then
MsgBox(Application.Current.Resources("msg_versionerr"), MsgBoxStyle.Exclamation)
Me.Close()
Exit Sub
End If
Else
MsgBox(Application.Current.Resources("msg_versionerr"), MsgBoxStyle.Exclamation)
Me.Close()
Exit Sub
End If
'loading settings
Do
If config.Elements("Framerate").Any Then framerate = config.Element("Framerate").Value
If config.Elements("Duration").Any Then
Dim tmp = Convert.ToUInt32(config.Element("Duration").Value)
duration = tmp
If tmp >= 5 Then picmove_sec = tmp
End If
If config.Elements("VerticalLock").Any AndAlso config.Element("VerticalLock").Value.ToLower = "false" Then verticalLock = False
If config.Elements("ResolutionLock").Any AndAlso config.Element("ResolutionLock").Value.ToLower = "false" Then resolutionLock = False
If config.Elements("VerticalOptimize").Any AndAlso config.Element("VerticalOptimize").Value.ToLower = "false" Then verticalOptimize = False
If config.Elements("HorizontalOptimize").Any AndAlso config.Element("HorizontalOptimize").Value.ToLower = "false" Then horizontalOptimize = False
If config.Elements("Fadeout").Any AndAlso config.Element("Fadeout").Value.ToLower = "false" Then fadeout = False
If config.Elements("VerticalOptimizeRatio").Any Then verticalOptimizeR = config.Element("VerticalOptimizeRatio").Value
If config.Elements("HorizontalOptimizeRatio").Any Then horizontalOptimizeR = config.Element("HorizontalOptimizeRatio").Value
If config.Elements("Transit").Any Then transit = config.Element("Transit").Value
If config.Elements("LoadQuality").Any Then loadquality = config.Element("LoadQuality").Value
If config.Elements("ScaleMode").Any Then scalemode = config.Element("ScaleMode").Value
If config.Elements("BlurMode").Any Then blurmode = config.Elements("BlurMode").Value
If config.Elements("LoadMode").Any Then
loadmode_next = config.Elements("LoadMode").Value
loadmode = loadmode_next
End If
If config.Elements("RandomizeV").Any AndAlso config.Element("RandomizeV").Value.ToLower = "true" Then randomizeV = True
If config.Elements("RandomizeA").Any AndAlso config.Element("RandomizeA").Value.ToLower = "true" Then randomizeA = True
If config.Elements("RecursiveFolder").Any AndAlso config.Element("RecursiveFolder").Value.ToLower = "false" Then recursive_folder = False
If config.Elements("RecursiveMusic").Any AndAlso config.Element("RecursiveMusic").Value.ToLower = "false" Then recursive_music = False
If config.Elements("ShowControl").Any AndAlso config.Element("ShowControl").Value.ToLower = "false" Then showcontrol = False
'loading music list
folders_music.Clear()
FillMusic(config.Element("Music"))
'loading ListOfPic
ListOfPic.Clear()
If config.Elements("DocumentElement").Any Then
ListOfPic.ReadXml(New IO.StringReader(config.Element("DocumentElement").ToString))
End If
folders_image.Clear()
FillPic(config.Element("PicDir"))
'remove old paths from ListOfPic
If folders_image.Count > 0 Then
Dim tmplst As New List(Of Data.DataRow)
For Each row As Data.DataRow In ListOfPic.Rows
If Not My.Computer.FileSystem.FileExists(row("Path")) Then
'remove when file doesnt exist
tmplst.Add(row)
Else
Dim score As Integer = 0
For i = 0 To folders_image.Count - 1
If recursive_folder Then
'dont remove when recursive is true and image is inside folder
If IO.Path.GetDirectoryName(row("Path")).Contains(folders_image(i)) Then
score += 1
Exit For
End If
Else
'dont remove when recursive is false and image is directly inside folder
If IO.Path.GetDirectoryName(row("Path")) = folders_image(i) Then
score += 1
Exit For
End If
End If
Next
If score = 0 Then tmplst.Add(row)
End If
Next
For Each row In tmplst
ListOfPic.Rows.Remove(row)
Next
Else
ListOfPic.Clear()
End If
'check if no image
If ListOfPic.Rows.Count = 0 Then
If MsgBox(Application.Current.Resources("msg_noimgerr"), MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
If New OptWindow().ShowDialog() = True Then config = XElement.Load(config_path)
Else
Close()
Exit Sub
End If
Else
Exit Do
End If
Loop
'save changes
Using lop_str = New IO.StringWriter()
ListOfPic.WriteXml(lop_str)
Dim lop = XElement.Parse(lop_str.ToString)
config.Elements("DocumentElement").Remove()
config.Add(lop)
config.Save(config_path)
End Using
If randomizeV Then Shuffle(ListOfPic)
If randomizeA Then Shuffle(ListOfMusic)
AddHandler player.MediaEnded, Sub()
playing = False
NextSong()
End Sub
tb_date0.FontSize = h / 12
tb_date1.FontSize = h / 12
Me.Background = Brushes.Black
Select Case transit
Case 0 'Ken Burns
worker_pic = New Thread(AddressOf mainThrd_KBE)
Case 1 'Breath
worker_pic = New Thread(AddressOf mainThrd_Breath)
Case 2 'Throw
worker_pic = New Thread(AddressOf mainThrd_Throw)
Case 3 'Random
worker_pic = New Thread(AddressOf mainThrd_Mix)
Case Else
MsgBox(Application.Current.Resources("msg_transerr"), MsgBoxStyle.Critical)
Me.Close()
Exit Sub
End Select
worker_pic.IsBackground = True
worker_pic.Priority = ThreadPriority.Lowest 'this is not the UI thread
If loadmode = 1 Then
Task.Run(
Sub()
Dim count = ListOfPic.Rows.Count
Dim tb As TextBlock = Nothing
Dim tbtext As String = Application.Current.Resources("loading images")
Dispatcher.Invoke(Sub()
tb = New TextBlock With {.Text = tbtext & "..."}
tb.FontFamily = New FontFamily("Segoe UI")
tb.FontSize = 16
tb.Foreground = Brushes.WhiteSmoke
tb.Margin = New Thickness(w / 2 - 60, h / 2 - 8, 0, 0)
mainGrid.Children.Add(tb)
Panel.SetZIndex(tb, 10)
End Sub)
For i = 0 To count - 1
If count > 1 Then
Dim ii = i
Dispatcher.Invoke(Sub() tb.Text = tbtext & "... " & ii * 100 \ (count - 1) & "%")
End If
Dim img As New BitmapImage
Dim imgpath As String = ListOfPic.Rows(i)("Path")
Try
Using ms = New IO.FileStream(imgpath, IO.FileMode.Open, IO.FileAccess.Read)
Dim frame = BitmapFrame.Create(ms, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None)
Dim s As Size = New Size(frame.PixelWidth, frame.PixelHeight)
ms.Position = 0
img.BeginInit()
Try
'getting orientation value from exif and rotate image. Rotate before setting DecodePixelHeight/Width?
Dim ori As UShort = DirectCast(frame.Metadata, BitmapMetadata).GetQuery("/app1/ifd/{ushort=274}")
Select Case ori
Case 6
img.Rotation = Rotation.Rotate90
Case 3
img.Rotation = Rotation.Rotate180
Case 8
img.Rotation = Rotation.Rotate270
End Select
ms.Position = 0
Catch
End Try
If resolutionLock Then
If s.Width > s.Height Then
If s.Height > h * loadquality Then
img.DecodePixelHeight = h * loadquality
End If
Else
If s.Width > w * loadquality Then
img.DecodePixelWidth = w * loadquality
End If
End If
End If
img.CacheOption = BitmapCacheOption.OnLoad
img.StreamSource = ms
img.EndInit()
End Using
Catch
img = New BitmapImage
Dim encoder As New BmpBitmapEncoder
Dim bmpsource = BitmapSource.Create(64, 64, 96, 96, PixelFormats.Indexed1, BitmapPalettes.BlackAndWhite, New Byte(64 * 8) {}, 8)
encoder.Frames.Add(BitmapFrame.Create(bmpsource))
Using ms As New IO.MemoryStream
encoder.Save(ms)
ms.Position = 0
img.BeginInit()
img.CacheOption = BitmapCacheOption.OnLoad
img.StreamSource = ms
img.EndInit()
End Using
Finally
img.Freeze()
End Try
pics.Add(imgpath, img)
Dim imgctrl As Image = Nothing
Dispatcher.Invoke(Sub()
imgctrl = New Image
imgctrl.Source = img
RenderOptions.SetBitmapScalingMode(imgctrl, scalemode)
If img.PixelWidth / img.PixelHeight > w / h Then
imgctrl.Height = h
imgctrl.Width = h * img.PixelWidth / img.PixelHeight
Else
imgctrl.Width = w
imgctrl.Height = w / img.PixelWidth * img.PixelHeight
End If
mainGrid.Children.Add(imgctrl)
imgctrl.Opacity = 0.01
End Sub)
Dim scales() As Double
'This is like a hack to achieve smooth transit animation. By setting opacity to 0.01 the image is basically
'invisible but it is still drawn to the screen. The below select case block is to force WPF to draw once the
'images at the scales that will be used by each transit animation. Numerous tests indicates that WPF seems
'to cache different ScaleTransform results for further use. Sort of like a background mipmapping.
Select Case transit
Case 0
scales = {1, 1.2}
Case 1
scales = {1, 1.3}
Case 2
scales = {0.7, 1}
Case 3
scales = {0.7, 1, 1.3}
Case Else
scales = {}
End Select
For Each d In scales
Dispatcher.Invoke(Sub() imgctrl.RenderTransform = New ScaleTransform(d, d))
Thread.Sleep(250)
Next
Dispatcher.Invoke(Sub() mainGrid.Children.Remove(imgctrl))
Next
Dispatcher.Invoke(Sub() mainGrid.Children.Remove(tb))
End Sub).ContinueWith(Sub()
LoadNextImg()
worker_pic.Start()
Dispatcher.Invoke(Sub()
If showcontrol Then
If ctrlwindow Is Nothing Then ctrlwindow = New ControlWindow
ctrlwindow.Show()
End If
End Sub)
End Sub)
ElseIf loadmode = 0 Then
LoadNextImg()
worker_pic.Start()
If showcontrol Then
If ctrlwindow Is Nothing Then ctrlwindow = New ControlWindow
ctrlwindow.Show()
End If
Else
Close()
End If
'disabling sleep / screensaver
'this seems to be unnecessary on the dev PC as the sleep timers are ignored anyway without the following two lines.
SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED Or EXECUTION_STATE.ES_DISPLAY_REQUIRED Or EXECUTION_STATE.ES_CONTINUOUS)
ExecState_Set = True
End Sub
Private Sub Shuffle(ByRef dt As System.Data.DataTable)
Using dtclone = dt.Clone
Using dtcopy = dt.Copy
Dim r As New Random
Do While dtcopy.Rows.Count > 0
Dim i = r.Next(dtcopy.Rows.Count) 'selecting a random row index
dtclone.ImportRow(dtcopy.Rows(i))
dtcopy.Rows.RemoveAt(i)
Loop
End Using
dt = dtclone.Copy
End Using
End Sub
Private Sub Shuffle(ByRef lst As List(Of String))
Dim tmplst = New List(Of String)
Dim lstcopy As New List(Of String)(lst)
Dim r As New Random
Do While lstcopy.Count > 0
Dim i = r.Next(lstcopy.Count)
tmplst.Add(lstcopy(i))
lstcopy.RemoveAt(i)
Loop
lst = New List(Of String)(tmplst)
End Sub
Private Sub FillPic(PicDir_ele As XElement)
Dim searchopt = If(recursive_folder, FileIO.SearchOption.SearchAllSubDirectories, FileIO.SearchOption.SearchTopLevelOnly)
For Each ele In PicDir_ele.Elements
If My.Computer.FileSystem.DirectoryExists(ele.Value) Then
folders_image.Add(ele.Value)
For Each f In My.Computer.FileSystem.GetFiles(ele.Value, searchopt)
Dim filefullname = My.Computer.FileSystem.GetName(f)
Dim filename = IO.Path.GetFileNameWithoutExtension(filefullname)
Dim ext = IO.Path.GetExtension(filefullname)
If PicFormats.Contains(ext.ToLower) Then
Dim row = ListOfPic.Rows.Find(f)
If row IsNot Nothing Then
Dim tmpdate As Date
If Date.TryParse(filename, tmpdate) Then
row("Date") = Date.Parse(filename).ToString
End If
Else
Dim tmprow = ListOfPic.NewRow
tmprow("Path") = f
Dim tmpdate As Date
If Date.TryParse(filename, tmpdate) Then
tmprow("Date") = Date.Parse(filename).ToString
End If
ListOfPic.Rows.Add(tmprow)
End If
End If
Next
End If
Next
End Sub
Private Sub FillMusic(Music_ele As XElement)
Dim searchopt = If(recursive_music, FileIO.SearchOption.SearchAllSubDirectories, FileIO.SearchOption.SearchTopLevelOnly)
For Each ele In Music_ele.Elements
If My.Computer.FileSystem.DirectoryExists(ele.Value) Then
folders_music.Add(ele.Value)
For Each f In My.Computer.FileSystem.GetFiles(ele.Value, searchopt)
Dim ext = IO.Path.GetExtension(f)
If BGMFormats.Contains(ext.ToLower) Then
ListOfMusic.Add(f)
End If
Next
End If
Next
End Sub
Private Function RandomNum(min As UInteger, max As UInteger, neg As Boolean)
If neg Then
If ran.Next(2) = 0 Then
Return -ran.Next(min, max)
Else
Return ran.Next(min, max)
End If
Else
Return ran.Next(min, max)
End If
End Function
Private Sub Anim_TitleSlide(tgt As Image)
'title slide animation
Thread.Sleep(2500) 'waiting for the former img to fade out
Dispatcher.Invoke(
Sub()
tgt = New Image
tgt.Name = "tgt"
tgt.Source = pic
tgt.Width = w
tgt.Height = h
mainGrid.Children.Add(tgt)
tgt.BeginAnimation(Image.OpacityProperty, New Animation.DoubleAnimation(0, 1, New Duration(New TimeSpan(0, 0, 2))))
End Sub)
Thread.Sleep(picmove_sec * 2000)
Dispatcher.Invoke(Sub() tgt.BeginAnimation(Image.OpacityProperty, New Animation.DoubleAnimation(0, New Duration(New TimeSpan(0, 0, 1)))))
Thread.Sleep(1500)
Dispatcher.Invoke(Sub() mainGrid.Children.Remove(tgt))
End Sub
Private Sub textThrd(pos As Integer, ByRef output As Integer)
'determine future same texts
Dim tmpstr As String = ListOfPic.Rows(pos - 1)("Text").ToString.Trim
Dim tbmove_sec = picmove_sec
If output = ListOfPic.Rows.Count Then
output = 1
Else
For n = 1 To ListOfPic.Rows.Count - pos 'not running if it is the last pic as ListOfPic.Count-pos=0
If ListOfPic.Rows(pos - 1 + n)("Text").ToString.Trim = tmpstr Then
output = pos + n
If output = ListOfPic.Rows.Count Then output = 1
tbmove_sec += (picmove_sec - 1)
Else
If ListOfPic.Rows(pos - 1 + n)("Text").ToString.Trim = "" Then
output = pos + n
If output = ListOfPic.Rows.Count Then output = 1 'if next (also the last) is empty, skip to the beginning directly
Else
output = pos + n
Exit For
End If
End If
Next
End If
'displaying custom text
If Not tmpstr = "" Then
Dim txt_tb As TextBlock = Nothing
Dispatcher.Invoke(
Sub()
txt_tb = New TextBlock
With txt_tb
.Text = tmpstr
.FontFamily = New FontFamily(ListOfPic.Rows(pos - 1)("FontFamily"))
.FontSize = h / DirectCast(ListOfPic.Rows(pos - 1)("FontSize"), Double)
.Foreground = New BrushConverter().ConvertFromString(ListOfPic.Rows(pos - 1)("FontColor"))
.CacheMode = New BitmapCache
.Effect = New Effects.DropShadowEffect With {.ShadowDepth = 2, .Opacity = 0.8}
End With
mainGrid.Children.Add(txt_tb)
Panel.SetZIndex(txt_tb, 8)
Dim startw = (w * 0.7) + (w * DirectCast(ListOfPic.Rows(pos - 1)("FontOffsetH"), Double) / 100)
Dim starth = (h * 0.15) + (h * DirectCast(ListOfPic.Rows(pos - 1)("FontOffsetV"), Double) / 100)
Dim anim_tbfadein As New Animation.DoubleAnimation(0, 0.7, New Duration(New TimeSpan(0, 0, 2)))
Dim anim_tbmovex As New Animation.DoubleAnimation(startw, startw + RandomNum(h / 32, h / 25, True), New Duration(New TimeSpan(0, 0, tbmove_sec)))
Dim anim_tbmovey As New Animation.DoubleAnimation(starth, starth + RandomNum(h / 32, h / 25, True), New Duration(New TimeSpan(0, 0, tbmove_sec)))
Animation.Timeline.SetDesiredFrameRate(anim_tbfadein, framerate)
Animation.Timeline.SetDesiredFrameRate(anim_tbmovex, framerate)
Animation.Timeline.SetDesiredFrameRate(anim_tbmovey, framerate)
Dim trans_trans As New TranslateTransform
txt_tb.RenderTransform = trans_trans
txt_tb.BeginAnimation(TextBlock.OpacityProperty, anim_tbfadein)
trans_trans.BeginAnimation(TranslateTransform.XProperty, anim_tbmovex)
trans_trans.BeginAnimation(TranslateTransform.YProperty, anim_tbmovey)
End Sub)
Thread.Sleep((tbmove_sec - 1) * 1000)
Dispatcher.Invoke(Sub()
Dim anim_tbfadeout As New Animation.DoubleAnimation(0, New Duration(New TimeSpan(0, 0, 1)))
AddHandler anim_tbfadeout.Completed, Sub(s As Animation.AnimationClock, e As EventArgs)
mainGrid.Children.Remove(Animation.Storyboard.GetTarget(s.Timeline))
End Sub
Animation.Storyboard.SetTarget(anim_tbfadeout, txt_tb)
Animation.Timeline.SetDesiredFrameRate(anim_tbfadeout, framerate)
txt_tb.BeginAnimation(TextBlock.OpacityProperty, anim_tbfadeout)
End Sub)
End If
End Sub
Private Sub dateThrd(pos As Integer, ByRef output As Integer)
'determining future pics with same year and month
Dim crntdate = ListOfPic.Rows(pos - 1)("Date")
If IsDBNull(crntdate) Then
If output = ListOfPic.Rows.Count Then
output = 1 'without this line when the last date is non-null and of different month.
Else
For n = 1 To ListOfPic.Rows.Count - pos 'this is not run when pos=count
If IsDBNull(ListOfPic.Rows(pos - 1 + n)("Date")) Then
output = pos + n
If output = ListOfPic.Rows.Count Then output = 1
Else
output = pos + n
Exit For
End If
Next
End If
Else 'current date is date
Dim tmpdate = Date.Parse(crntdate).ToString("yyyy.M")
Dim tbmove_sec = picmove_sec
If output = ListOfPic.Rows.Count Then
output = 1
Else
For n = 1 To ListOfPic.Rows.Count - pos
Dim nextdate = ListOfPic.Rows(pos - 1 + n)("Date")
If IsDBNull(nextdate) Then 'next date is null
output = pos + n
If output = ListOfPic.Rows.Count Then output = 1 'output is changed. need to recheck. output=count equals n=count-pos, so no need to exit for.
tmpdate = "" 'to set output to the next real date seperated by non-dates, even when it has same month & year with crntdate
Else
If Date.Parse(nextdate).ToString("yyyy.M") = tmpdate Then 'next date is not null and of same month with the current
output = pos + n
If output = ListOfPic.Rows.Count Then output = 1
tbmove_sec += (picmove_sec - 1)
Else 'next date is not null and of different month with the current
output = pos + n 'no output=1. need to ensure the last different date is processed
Exit For
End If
End If
Next
End If
tmpdate = Date.Parse(crntdate).ToString("yyyy.M")
Dim tgt_tb As TextBlock = Nothing
If mm = 0 Then
mm = 1
Else
mm = 0
End If
Dispatcher.Invoke(Sub()
tgt_tb = CType(mainGrid.FindName("tb_date" & mm), TextBlock)
tgt_tb.Text = tmpdate
Dim anim_tbfadein As New Animation.DoubleAnimation(0, 0.7, New Duration(New TimeSpan(0, 0, 2)))
Dim anim_tbmovex As New Animation.DoubleAnimation(w / 5, w / 5 + RandomNum(h / 32, h / 25, True), New Duration(New TimeSpan(0, 0, tbmove_sec)))
Dim anim_tbmovey As New Animation.DoubleAnimation(h * 0.75, h * 0.75 + RandomNum(h / 32, h / 25, True), New Duration(New TimeSpan(0, 0, tbmove_sec)))
Animation.Timeline.SetDesiredFrameRate(anim_tbfadein, framerate)
Animation.Timeline.SetDesiredFrameRate(anim_tbmovex, framerate)
Animation.Timeline.SetDesiredFrameRate(anim_tbmovey, framerate)
Dim trans_trans As New TranslateTransform(w / 5, h * 0.75)
tgt_tb.RenderTransform = trans_trans
tgt_tb.BeginAnimation(TextBlock.OpacityProperty, anim_tbfadein)
trans_trans.BeginAnimation(TranslateTransform.XProperty, anim_tbmovex)
trans_trans.BeginAnimation(TranslateTransform.YProperty, anim_tbmovey)
End Sub)
Thread.Sleep((tbmove_sec - 1) * 1000)
Dispatcher.Invoke(Sub()
Dim anim_tbfadeout As New Animation.DoubleAnimation(0, New Duration(New TimeSpan(0, 0, 1)))
Animation.Timeline.SetDesiredFrameRate(anim_tbfadeout, framerate)
tgt_tb.BeginAnimation(TextBlock.OpacityProperty, anim_tbfadeout)
End Sub)
End If
''Dim tmpdate = Date.Parse(ListOfPic.Rows(pos - 1)("Date")).ToString("yyyy.M")
''Dim tbmove_sec = picmove_sec
'For n = 1 To ListOfPic.Rows.Count - pos 'not running if it is the last pic as ListOfPic.Count-pos=0
' If Not IsDBNull(ListOfPic.Rows(pos - 1 + n)("Date")) Then
' If Date.Parse(ListOfPic.Rows(pos - 1 + n)("Date")).ToString("yyyy.M") = tmpdate Then
' tbmove_sec += (picmove_sec - 1)
' output = pos + n
' If output = ListOfPic.Rows.Count Then output = 1
' Else
' output = pos + n
' Exit For
' End If
' Else 'date is null
' output = pos + n
' If output = ListOfPic.Rows.Count Then output = 1
' End If
'Next
''displaying date
'If Not IsDBNull(ListOfPic.Rows(pos - 1)("Date")) Then
'Else
' output = pos + 1
'End If
End Sub
Private Sub mainThrd_KBE()
Do While audiofading
Thread.Sleep(500)
Loop
Dim ease_in, ease_out, ease_inout As Animation.CubicEase
Dim anim_fadein, anim_fadeout As Animation.DoubleAnimation
Dispatcher.Invoke(Sub()
If ListOfMusic.Count > 0 Then
player.Open(New Uri(ListOfMusic(0)))
player.Play()
playing = True
End If
ease_in = New Animation.CubicEase With {.EasingMode = Animation.EasingMode.EaseIn}
ease_out = New Animation.CubicEase With {.EasingMode = Animation.EasingMode.EaseOut}
ease_inout = New Animation.CubicEase With {.EasingMode = Animation.EasingMode.EaseInOut}
anim_fadein = New Animation.DoubleAnimation(0, 1, New Duration(New TimeSpan(0, 0, 1)))
anim_fadeout = New Animation.DoubleAnimation(0, New Duration(New TimeSpan(0, 0, 1)))
Panel.SetZIndex(tb_date0, 2)
Panel.SetZIndex(tb_date1, 3)
End Sub)
Dim tgt_img As Image
Dim date_chkpoint As Integer = 1
Dim text_chkpoint As Integer = 1
Do
If position = date_chkpoint Then Task.Run(Sub() dateThrd(position, date_chkpoint))
If position = text_chkpoint Then Task.Run(Sub() textThrd(position, text_chkpoint))
If ListOfPic.Rows(position - 1)("TitleSlide") Then
Anim_TitleSlide(tgt_img)
Else
Dispatcher.Invoke(Sub()
SwitchTarget(tgt_img)
Anim_KBE(tgt_img, ease_in, ease_out, ease_inout, anim_fadein)
End Sub)
Thread.Sleep(1000)
End If
Dim tmpposition = position
Dim loadtask = Task.Run(Sub()
Thread.CurrentThread.Priority = ThreadPriority.Lowest
If position = ListOfPic.Rows.Count Then
position = 0
If randomizeV Then Shuffle(ListOfPic)
End If
LoadNextImg()
End Sub)
If Not ListOfPic.Rows(tmpposition - 1)("TitleSlide") Then
Thread.Sleep((picmove_sec - 2) * 1000)
Dispatcher.Invoke(Sub()
Animation.Timeline.SetDesiredFrameRate(anim_fadeout, framerate)
tgt_img.BeginAnimation(Image.OpacityProperty, anim_fadeout)
End Sub)
End If
loadtask.Wait()
If aborting Then
Exit Sub
End If
Do Until moveon
Thread.Sleep(1000)
Loop
Loop
End Sub
Private Sub mainThrd_Breath()
Do While audiofading
Thread.Sleep(500)
Loop
Dim ease_in, ease_out, ease_inout As Animation.CubicEase
Dim anim_fadein, anim_fadeout As Animation.DoubleAnimation
Dispatcher.Invoke(Sub()
If ListOfMusic.Count > 0 Then
player.Open(New Uri(ListOfMusic(0)))
player.Play()
playing = True
End If
ease_in = New Animation.CubicEase With {.EasingMode = Animation.EasingMode.EaseIn}
ease_out = New Animation.CubicEase With {.EasingMode = Animation.EasingMode.EaseOut}
ease_inout = New Animation.CubicEase With {.EasingMode = Animation.EasingMode.EaseInOut}
anim_fadein = New Animation.DoubleAnimation(0, 1, New Duration(New TimeSpan(0, 0, 1)))
anim_fadeout = New Animation.DoubleAnimation(0, New Duration(New TimeSpan(0, 0, 1)))
Panel.SetZIndex(tb_date0, 2)
Panel.SetZIndex(tb_date1, 3)
End Sub)
Dim tgt_img As Image
Dim date_chkpoint As Integer = 1
Dim text_chkpoint As Integer = 1
Dim last_zoom = False
Do
If position = date_chkpoint Then Task.Run(Sub() dateThrd(position, date_chkpoint))
If position = text_chkpoint Then Task.Run(Sub() textThrd(position, text_chkpoint))
If ListOfPic.Rows(position - 1)("TitleSlide") Then
Anim_TitleSlide(tgt_img)
Else
Dispatcher.Invoke(Sub()
SwitchTarget(tgt_img)
Anim_Breath(tgt_img, ease_in, ease_out, ease_inout, anim_fadein, last_zoom)
End Sub)
Thread.Sleep(1000)
End If
Dim tmpposition = position
Dim loadtask = Task.Run(Sub()
Thread.CurrentThread.Priority = ThreadPriority.Lowest
If position = ListOfPic.Rows.Count Then
position = 0
If randomizeV Then Shuffle(ListOfPic)
End If
LoadNextImg()
End Sub)
If Not ListOfPic.Rows(tmpposition - 1)("TitleSlide") Then
Thread.Sleep((picmove_sec - 2.5) * 1000)
Dispatcher.Invoke(Sub()
Animation.Timeline.SetDesiredFrameRate(anim_fadeout, framerate)
tgt_img.BeginAnimation(Image.OpacityProperty, anim_fadeout)
End Sub)
End If
Thread.Sleep(500)
loadtask.Wait()
If aborting Then
Exit Sub
End If
Do Until moveon
Thread.Sleep(1000)
Loop
Loop
End Sub
Private Sub mainThrd_Throw()
Do While audiofading
Thread.Sleep(500)
Loop
Dim ease_in, ease_out, ease_inout As Animation.CubicEase
Dim anim_fadein, anim_fadeout As Animation.DoubleAnimation
Dim direction As Boolean = ran.Next(2)
Dispatcher.Invoke(Sub()
If ListOfMusic.Count > 0 Then
player.Open(New Uri(ListOfMusic(0)))
player.Play()
playing = True
End If
ease_in = New Animation.CubicEase With {.EasingMode = Animation.EasingMode.EaseIn}
ease_out = New Animation.CubicEase With {.EasingMode = Animation.EasingMode.EaseOut}
ease_inout = New Animation.CubicEase With {.EasingMode = Animation.EasingMode.EaseInOut}
anim_fadein = New Animation.DoubleAnimation(0, 1, New Duration(New TimeSpan(0, 0, 2)))
anim_fadeout = New Animation.DoubleAnimation(0, New Duration(New TimeSpan(0, 0, 1)))
Panel.SetZIndex(tb_date0, 2)
Panel.SetZIndex(tb_date1, 3)
End Sub)
Dim tgt_img As Image
Dim date_chkpoint As Integer = 1
Dim text_chkpoint As Integer = 1
Do
If position = date_chkpoint Then Task.Run(Sub() dateThrd(position, date_chkpoint))
If position = text_chkpoint Then Task.Run(Sub() textThrd(position, text_chkpoint))
If ListOfPic.Rows(position - 1)("TitleSlide") Then
Anim_TitleSlide(tgt_img)
Else
Dispatcher.Invoke(Sub()
SwitchTarget(tgt_img)
Anim_Throw(tgt_img, ease_in, ease_out, ease_inout, anim_fadein, direction)
End Sub)
End If
Dim tmpposition = position
Dim loadtask = Task.Run(Sub()
Thread.CurrentThread.Priority = ThreadPriority.Lowest
If position = ListOfPic.Rows.Count Then
position = 0
If randomizeV Then Shuffle(ListOfPic)
End If
LoadNextImg()
End Sub)
If Not ListOfPic.Rows(tmpposition - 1)("TitleSlide") Then
Thread.Sleep((picmove_sec - 1) * 1000)
Dispatcher.Invoke(Sub()
Animation.Timeline.SetDesiredFrameRate(anim_fadeout, framerate)
tgt_img.BeginAnimation(Image.OpacityProperty, anim_fadeout)
End Sub)
End If
loadtask.Wait()
If aborting Then
Exit Sub
End If
Do Until moveon
Thread.Sleep(1000)
Loop
Loop
End Sub
Private Sub mainThrd_Mix()
Do While audiofading
Thread.Sleep(500)
Loop
Dim ease_in, ease_out, ease_inout As Animation.CubicEase
Dim anim_fadein1, anim_fadein2, anim_fadeout As Animation.DoubleAnimation
Dim tgt_img As Image
Dim date_chkpoint As Integer = 1
Dim text_chkpoint As Integer = 1
Dim last_zoom As Boolean = False
Dim direction As Boolean = ran.Next(2)
Dispatcher.Invoke(Sub()
If ListOfMusic.Count > 0 Then
player.Open(New Uri(ListOfMusic(0)))
player.Play()
playing = True
End If
ease_in = New Animation.CubicEase With {.EasingMode = Animation.EasingMode.EaseIn}
ease_out = New Animation.CubicEase With {.EasingMode = Animation.EasingMode.EaseOut}
ease_inout = New Animation.CubicEase With {.EasingMode = Animation.EasingMode.EaseInOut}
anim_fadein1 = New Animation.DoubleAnimation(0, 1, New Duration(New TimeSpan(0, 0, 1)))
anim_fadein2 = New Animation.DoubleAnimation(0, 1, New Duration(New TimeSpan(0, 0, 2)))
anim_fadeout = New Animation.DoubleAnimation(0, New Duration(New TimeSpan(0, 0, 1)))
Panel.SetZIndex(tb_date0, 2)
Panel.SetZIndex(tb_date1, 3)
End Sub)
Do
If position = date_chkpoint Then Task.Run(Sub() dateThrd(position, date_chkpoint))
If position = text_chkpoint Then Task.Run(Sub() textThrd(position, text_chkpoint))
If ListOfPic.Rows(position - 1)("TitleSlide") Then
Anim_TitleSlide(tgt_img)
Else
Dispatcher.Invoke(Sub()
tgt_img = New Image
tgt_img.Name = "tgt"
RenderOptions.SetBitmapScalingMode(tgt_img, scalemode)
mainGrid.Children.Add(tgt_img)
End Sub)
Select Case ran.Next(3)
Case 0
Dispatcher.Invoke(Sub() Anim_Breath(tgt_img, ease_in, ease_out, ease_inout, anim_fadein1, last_zoom))
Thread.Sleep((picmove_sec - 1.5) * 1000)
Dispatcher.Invoke(Sub()
Dim fadeout As New Animation.DoubleAnimation(0, New Duration(New TimeSpan(0, 0, 1)))
AddHandler fadeout.Completed, Sub(s As Animation.AnimationClock, e As EventArgs)
mainGrid.Children.Remove(Animation.Storyboard.GetTarget(s.Timeline))
End Sub
Animation.Storyboard.SetTarget(fadeout, tgt_img)
tgt_img.BeginAnimation(Image.OpacityProperty, fadeout)
End Sub)
Thread.Sleep(500) 'dont miss this
Case 1
Dispatcher.Invoke(Sub() Anim_Throw(tgt_img, ease_in, ease_out, ease_inout, anim_fadein2, direction))
Thread.Sleep((picmove_sec - 1) * 1000)
Dispatcher.Invoke(Sub()
Dim fadeout As New Animation.DoubleAnimation(0, New Duration(New TimeSpan(0, 0, 1)))
AddHandler fadeout.Completed, Sub(s As Animation.AnimationClock, e As EventArgs)
mainGrid.Children.Remove(Animation.Storyboard.GetTarget(s.Timeline))
End Sub
Animation.Storyboard.SetTarget(fadeout, tgt_img)
tgt_img.BeginAnimation(Image.OpacityProperty, fadeout)
End Sub)
Case Else
Dispatcher.Invoke(Sub() Anim_KBE(tgt_img, ease_in, ease_out, ease_inout, anim_fadein1))
Thread.Sleep((picmove_sec - 1) * 1000)
Dispatcher.Invoke(Sub()
Dim fadeout As New Animation.DoubleAnimation(0, New Duration(New TimeSpan(0, 0, 1)))
AddHandler fadeout.Completed, Sub(s As Animation.AnimationClock, e As EventArgs)
mainGrid.Children.Remove(Animation.Storyboard.GetTarget(s.Timeline))
End Sub
Animation.Storyboard.SetTarget(fadeout, tgt_img)
tgt_img.BeginAnimation(Image.OpacityProperty, fadeout)
End Sub)
End Select
End If
Dim loadtask = Task.Run(Sub()
Thread.CurrentThread.Priority = ThreadPriority.Lowest
If position = ListOfPic.Rows.Count Then
position = 0
If randomizeV Then Shuffle(ListOfPic)
End If
LoadNextImg()
End Sub)
loadtask.Wait()