forked from maoiscat/mpv-osc-modern
-
Notifications
You must be signed in to change notification settings - Fork 1
/
youtube-ui.lua
2624 lines (2251 loc) · 90.8 KB
/
youtube-ui.lua
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
local assdraw = require 'mp.assdraw'
local msg = require 'mp.msg'
local opt = require 'mp.options'
local utils = require 'mp.utils'
--
-- Parameters
--
-- default user option values
-- may change them in osc.conf
local user_opts = {
showwindowed = true, -- show OSC when windowed?
showfullscreen = true, -- show OSC when fullscreen?
idlescreen = true, -- show mpv logo on idle
scalewindowed = 1, -- scaling of the controller when windowed
scalefullscreen = 1.5, -- scaling of the controller when fullscreen
scaleforcedwindow = 1, -- scaling when rendered on a forced window
vidscale = false, -- scale the controller with the video?
hidetimeout = 2000, -- duration in ms until the OSC hides if no
-- mouse movement. enforced non-negative for the
-- user, but internally negative is "always-on".
fadeduration = 150, -- duration of fade out in ms, 0 = no fade
deadzonesize = 0, -- size of deadzone
minmousemove = 0, -- minimum amount of pixels the mouse has to
-- move between ticks to make the OSC show up
iamaprogrammer = false, -- use native mpv values and disable OSC
-- internal track list management (and some
-- functions that depend on it)
seekrange = true, -- show seekrange overlay
seekrangealpha = 192, -- transparency of seekranges
seekbarkeyframes = true, -- use keyframes when dragging the seekbar
title = "${media-title}", -- string compatible with property-expansion
-- to be shown as OSC title
timetotal = true, -- display total time instead of remaining time?
remaining_playtime = true, -- display the remaining time in playtime or video-time mode
-- playtime takes speed into account, whereas video-time doesn't
timems = false, -- display timecodes with milliseconds?
visibility = "auto", -- only used at init to set visibility_mode(...)
boxvideo = false, -- apply osc_param.video_margins to video
windowcontrols = "auto", -- whether to show window controls
windowcontrols_alignment = "right", -- which side to show window controls on
livemarkers = true, -- update seekbar chapter markers on duration change
chapters_osd = false, -- whether to show chapters OSD on seekbar
playlist_osd = false, -- whether to show playlist OSD on next/prev
unicodeminus = false, -- whether to use the Unicode minus sign character
language = "eng", -- eng=English, chs=Chinese
thumbpad = 4, -- thumbnail border size
}
-- read options from config and command-line
opt.read_options(user_opts, "osc", function(list) update_options(list) end)
-- Localization
local language = {
["eng"] = {
welcome = "{\\fs24\\1c&H0&\\3c&HFFFFFF&}Drop files or URLs to play here.", -- this text appears when mpv starts
off = "Off",
unknown = "unknown",
none = "none",
video_track = "Video track",
video_tracks = "Video tracks",
audio_track = "Audio track",
audio_tracks = "Audio tracks",
subtitle = "Subtitle",
subtitles = "Subtitles",
playlist = "Playlist",
chapter = "Chapter",
chapters = "Chapters",
},
["chs"] = {
welcome = "{\\1c&H00\\bord0\\fs30\\fn微软雅黑 light\\fscx125}MPV{\\fscx100} 播放器", -- this text appears when mpv starts
off = "关闭",
na = "n/a",
none = "无",
video = "视频",
audio = "音频",
subtitle = "字幕",
available = "可选",
track = ":",
playlist = "播放列表",
nolist = "无列表信息",
chapter = "章节",
nochapter = "无章节信息",
}
}
-- apply lang opts
local texts = language[user_opts.language]
local osc_param = { -- calculated by osc_init()
playresy = 0, -- canvas size Y
playresx = 0, -- canvas size X
display_aspect = 1,
unscaled_y = 0,
areas = {},
video_margins = {
l = 0, r = 0, t = 0, b = 0, -- left/right/top/bottom
},
}
local osc_styles = {
box = "{\\blur100\\bord0\\1c&H000000\\3c&H000000}",
seekbar_bg = "{\\blur0\\bord0\\1c&HFFFFFF}",
seekbar_fg = "{\\blur0\\bord0\\1c&HE39C42}",
volumebar_bg = "{\\blur0\\bord0\\1c&HFFFFFF}",
volumebar_fg = "{\\blur0\\bord0\\1c&HFFFFFF}",
button = "{\\blur0\\bord0\\1c&HFFFFFF\\3c&HFFFFFF}",
timecode = "{\\blur0\\bord0\\1c&HFFFFFF\\3c&H000000\\fs18}",
tooltip = "{\\blur1.5\\bord0.01\\1c&HFFFFFF\\3c&H000000\\fs18}",
title = "{\\1c&HFFFFFF\\fs24}",
}
-- internal states, do not touch
local state = {
showtime, -- time of last invocation (last mouse move)
osc_visible = false,
anistart, -- time when the animation started
anitype, -- current type of animation
animation, -- current animation alpha
mouse_down_counter = 0, -- used for softrepeat
active_element = nil, -- nil = none, 0 = background, 1+ = see elements[]
active_event_source = nil, -- the "button" that issued the current event
rightTC_trem = not user_opts.timetotal, -- if the right timecode should display total or remaining time
tc_ms = user_opts.timems, -- Should the timecodes display their time with milliseconds
mp_screen_sizeX, mp_screen_sizeY, -- last screen-resolution, to detect resolution changes to issue reINITs
initREQ = false, -- is a re-init request pending?
marginsREQ = false, -- is a margins update pending?
last_mouseX, last_mouseY, -- last mouse position, to detect significant mouse movement
mouse_in_window = false,
message_text,
message_hide_timer,
fullscreen = false,
tick_timer = nil,
tick_last_time = 0, -- when the last tick() was run
hide_timer = nil,
cache_state = nil,
idle = false,
enabled = true,
input_enabled = true,
showhide_enabled = false,
windowcontrols_buttons = false,
dmx_cache = 0,
using_video_margins = false,
border = true,
maximized = false,
osd = mp.create_osd_overlay("ass-events"),
chapter_list = {}, -- sorted by time
last_visibility = user_opts.visibility, -- last visibility on pause
mute = false,
}
local icons = {
close = "{\\p1}m 0 0 m 24 24 m 19 6.41 l 17.59 5 l 12 10.59 l 6.41 5 l 5 6.41 l 10.59 12 l 5 17.59 l 6.41 19 l 12 13.41 l 17.59 19 l 19 17.59 l 13.41 12{\\p0}",
cy_audio = "{\\p1}m 0 0 m 24 24 m 20 4 l 4 4 b 2.9 4 2 4.9 2 6 l 2 18 b 2 19.1 2.9 20 4 20 l 20 20 b 21.1 20 22 19.1 22 18 l 22 6 b 22 4.9 21.1 4 20 4 m 7.76 16.24 l 6.35 17.65 b 4.78 16.1 4 14.05 4 12 b 4 9.95 4.78 7.9 6.34 6.34 l 7.75 7.75 b 6.59 8.93 6 10.46 6 12 b 6 13.54 6.59 15.07 7.76 16.24 m 12 16 b 9.79 16 8 14.21 8 12 b 8 9.79 9.79 8 12 8 b 14.21 8 16 9.79 16 12 b 16 14.21 14.21 16 12 16 m 17.66 17.66 l 16.25 16.25 b 17.41 15.07 18 13.54 18 12 b 18 10.46 17.41 8.93 16.24 7.76 l 17.65 6.35 b 19.22 7.9 20 9.95 20 12 b 20 14.05 19.22 16.1 17.66 17.66 m 12 10 b 10.9 10 10 10.9 10 12 b 10 13.1 10.9 14 12 14 b 13.1 14 14 13.1 14 12 b 14 10.9 13.1 10 12 10{\\p0}",
cy_sub = "{\\p1}m 0 0 m 24 24 m 20 4 l 4 4 b 2.9 4 2 4.9 2 6 l 2 18 b 2 19.1 2.9 20 4 20 l 20 20 b 21.1 20 22 19.1 22 18 l 22 6 b 22 4.9 21.1 4 20 4 m 4 12 l 8 12 l 8 14 l 4 14 l 4 12 m 14 18 l 4 18 l 4 16 l 14 16 l 14 18 m 20 18 l 16 18 l 16 16 l 20 16 l 20 18 m 20 14 l 10 14 l 10 12 l 20 12 l 20 14{\\p0}",
fs_enter = "{\\p1}m 0 0 m 24 24 m 7 14 l 5 14 l 5 19 l 10 19 l 10 17 l 7 17 l 7 14 m 5 10 l 7 10 l 7 7 l 10 7 l 10 5 l 5 5 l 5 10 m 17 17 l 14 17 l 14 19 l 19 19 l 19 14 l 17 14 l 17 17 m 14 5 l 14 7 l 17 7 l 17 10 l 19 10 l 19 5 l 14 5{\\p0}",
fs_exit = "{\\p1}m 0 0 m 24 24 m 5 16 l 8 16 l 8 19 l 10 19 l 10 14 l 5 14 l 5 16 m 8 8 l 5 8 l 5 10 l 10 10 l 10 5 l 8 5 l 8 8 m 14 19 l 16 19 l 16 16 l 19 16 l 19 14 l 14 14 l 14 19 m 16 8 l 16 5 l 14 5 l 14 10 l 19 10 l 19 8 l 16 8{\\p0}",
info = "{\\p1}m 0 0 m 24 24 m 11 7 l 13 7 l 13 9 l 11 9 m 11 11 l 13 11 l 13 17 l 11 17 m 12 2 b 6.48 2 2 6.48 2 12 b 2 17.52 6.48 22 12 22 b 17.52 22 22 17.52 22 12 b 22 6.48 17.52 2 12 2 m 12 20 b 7.59 20 4 16.41 4 12 b 4 7.59 7.59 4 12 4 b 16.41 4 20 7.59 20 12 b 20 16.41 16.41 20 12 20{\\p0}",
maximize = "{\\p1}m 0 0 m 24 24 m 18 4 l 6 4 b 4.9 4 4 4.9 4 6 l 4 18 b 4 19.1 4.9 20 6 20 l 18 20 b 19.1 20 20 19.1 20 18 l 20 6 b 20 4.9 19.1 4 18 4 m 18 18 l 6 18 l 6 6 l 18 6 l 18 18{\\p0}",
maximize_exit = "{\\p1}m 0 0 m 24 24 m 6 8 l 4 8 l 4 18 b 4 19.1 4.9 20 6 20 l 16 20 l 16 18 l 6 18 m 18 4 l 10 4 b 8.9 4 8 4.9 8 6 l 8 14 b 8 15.1 8.9 16 10 16 l 18 16 b 19.1 16 20 15.1 20 14 l 20 6 b 20 4.9 19.1 4 18 4 m 18 14 l 10 14 l 10 6 l 18 6{\\p0}",
minimize = "{\\p1}m 0 0 m 24 24 m 4 18 l 20 18 l 20 20 l 4 20{\\p0}",
pause = "{\\p1}m 0 0 m 24 24 m 6 19 l 10 19 l 10 5 l 6 5 l 6 19 m 14 5 l 14 19 l 18 19 l 18 5 l 14 5{\\p0}",
pl_next = "{\\p1}m 0 0 m 24 24 m 6 18 l 14.5 12 l 6 6 l 6 18 m 16 6 l 16 18 l 18 18 l 18 6 l 16 6{\\p0}",
pl_prev = "{\\p1}m 0 0 m 24 24 m 6 6 l 8 6 l 8 18 l 6 18 m 9.5 12 l 18 18 l 18 6{\\p0}",
play = "{\\p1}m 0 0 m 24 24 m 8 5 l 8 19 l 19 12{\\p0}",
skipback = "{\\p1}m 0 0 m 24 24 m 11 18 l 11 6 l 2.5 12 l 11 18 m 11.5 12 l 20 18 l 20 6 l 11.5 12{\\p0}",
skipfrwd = "{\\p1}m 0 0 m 24 24 m 4 18 l 12.5 12 l 4 6 l 4 18 m 13 6 l 13 18 l 21.5 12 l 13 6{\\p0}",
volume_high = "{\\p1}m 0 0 m 24 24 m 3 9 l 3 15 l 7 15 l 12 20 l 12 4 l 7 9 l 3 9 m 16.5 12 b 16.5 10.23 15.48 8.71 14 7.97 l 14 16.02 b 15.48 15.29 16.5 13.77 16.5 12 m 14 3.23 l 14 5.29 b 16.89 6.15 19 8.83 19 12 b 19 15.17 16.89 17.85 14 18.71 l 14 20.77 b 18.01 19.86 21 16.28 21 12 b 21 7.72 18.01 4.14 14 3.23{\\p0}",
volume_low = "{\\p1}m 0 0 m 24 24 m 3 9 l 3 15 l 7 15 l 12 20 l 12 4 l 7 9 l 3 9{\\p0}",
volume_medium = "{\\p1}m 0 0 m 24 24 m 14 7.97 l 14 16.02 b 15.48 15.29 16.5 13.77 16.5 12 b 16.5 10.23 15.48 8.71 14 7.97 m 3 9 l 3 15 l 7 15 l 12 20 l 12 4 l 7 9 l 3 9{\\p0}",
volume_mute = "{\\p1}m 0 0 m 24 24 m 16.5 12 b 16.5 10.23 15.48 8.71 14 7.97 l 14 10.18 l 16.45 12.63 b 16.48 12.43 16.5 12.22 16.5 12 m 19 12 b 19 12.94 18.8 13.82 18.46 14.64 l 19.97 16.15 b 20.63 14.91 21 13.5 21 12 b 21 7.72 18.01 4.14 14 3.23 l 14 5.29 b 16.89 6.15 19 8.83 19 12 m 4.27 3 l 3 4.27 l 7.73 9 l 3 9 l 3 15 l 7 15 l 12 20 l 12 13.27 l 16.25 17.52 b 15.58 18.04 14.83 18.45 14 18.7 l 14 20.76 b 15.38 20.45 16.63 19.81 17.69 18.95 l 19.73 21 l 21 19.73 l 12 10.73 l 4.27 3 m 12 4 l 9.91 6.09 l 12 8.18 l 12 4{\\p0}",
volume_over = "{\\p1}m 0 0 m 24 24 m 3 9 l 3 15 l 7 15 l 12 20 l 12 4 l 7 9 l 3 9 m 16 6 l 18 6 l 18 14 l 16 14 m 16 16 l 18 16 l 18 18 l 16 18{\\p0}",
}
local thumbfast = {
width = 0,
height = 0,
disabled = true,
available = false
}
local window_control_box_width = 138
local tick_delay = 0.03
--
-- Helperfunctions
--
function kill_animation()
state.anistart = nil
state.animation = nil
state.anitype = nil
end
function set_osd(res_x, res_y, text, z)
if state.osd.res_x == res_x and
state.osd.res_y == res_y and
state.osd.data == text then
return
end
state.osd.res_x = res_x
state.osd.res_y = res_y
state.osd.data = text
state.osd.z = z
state.osd:update()
end
local margins_opts = {
{"l", "video-margin-ratio-left"},
{"r", "video-margin-ratio-right"},
{"t", "video-margin-ratio-top"},
{"b", "video-margin-ratio-bottom"},
}
-- scale factor for translating between real and virtual ASS coordinates
function get_virt_scale_factor()
local w, h = mp.get_osd_size()
if w <= 0 or h <= 0 then
return 0, 0
end
return osc_param.playresx / w, osc_param.playresy / h
end
-- return mouse position in virtual ASS coordinates (playresx/y)
function get_virt_mouse_pos()
if state.mouse_in_window then
local sx, sy = get_virt_scale_factor()
local x, y = mp.get_mouse_pos()
return x * sx, y * sy
else
return -1, -1
end
end
function set_virt_mouse_area(x0, y0, x1, y1, name)
local sx, sy = get_virt_scale_factor()
mp.set_mouse_area(x0 / sx, y0 / sy, x1 / sx, y1 / sy, name)
end
function scale_value(x0, x1, y0, y1, val)
local m = (y1 - y0) / (x1 - x0)
local b = y0 - (m * x0)
return (m * val) + b
end
-- returns hitbox spanning coordinates (top left, bottom right corner)
-- according to alignment
function get_hitbox_coords(x, y, an, w, h)
local alignments = {
[1] = function () return x, y-h, x+w, y end,
[2] = function () return x-(w/2), y-h, x+(w/2), y end,
[3] = function () return x-w, y-h, x, y end,
[4] = function () return x, y-(h/2), x+w, y+(h/2) end,
[5] = function () return x-(w/2), y-(h/2), x+(w/2), y+(h/2) end,
[6] = function () return x-w, y-(h/2), x, y+(h/2) end,
[7] = function () return x, y, x+w, y+h end,
[8] = function () return x-(w/2), y, x+(w/2), y+h end,
[9] = function () return x-w, y, x, y+h end,
}
return alignments[an]()
end
function get_hitbox_coords_geo(geometry)
return get_hitbox_coords(geometry.x, geometry.y, geometry.an,
geometry.w, geometry.h)
end
function get_element_hitbox(element)
return element.hitbox.x1, element.hitbox.y1,
element.hitbox.x2, element.hitbox.y2
end
function mouse_hit(element)
return mouse_hit_coords(get_element_hitbox(element))
end
function mouse_hit_coords(bX1, bY1, bX2, bY2)
local mX, mY = get_virt_mouse_pos()
return (mX >= bX1 and mX <= bX2 and mY >= bY1 and mY <= bY2)
end
function limit_range(min, max, val)
if val > max then
val = max
elseif val < min then
val = min
end
return val
end
-- translate value into element coordinates
function get_slider_ele_pos_for(element, val)
local ele_pos = scale_value(
element.slider.min.value, element.slider.max.value,
element.slider.min.ele_pos, element.slider.max.ele_pos,
val)
return limit_range(
element.slider.min.ele_pos, element.slider.max.ele_pos,
ele_pos)
end
-- translates global (mouse) coordinates to value
function get_slider_value_at(element, glob_pos)
local val = scale_value(
element.slider.min.glob_pos, element.slider.max.glob_pos,
element.slider.min.value, element.slider.max.value,
glob_pos)
return limit_range(
element.slider.min.value, element.slider.max.value,
val)
end
-- get value at current mouse position
function get_slider_value(element)
return get_slider_value_at(element, get_virt_mouse_pos())
end
function countone(val)
if not (user_opts.iamaprogrammer) then
val = val + 1
end
return val
end
-- align: -1 .. +1
-- frame: size of the containing area
-- obj: size of the object that should be positioned inside the area
-- margin: min. distance from object to frame (as long as -1 <= align <= +1)
function get_align(align, frame, obj, margin)
return (frame / 2) + (((frame / 2) - margin - (obj / 2)) * align)
end
-- multiplies two alpha values, formular can probably be improved
function mult_alpha(alphaA, alphaB)
return 255 - (((1-(alphaA/255)) * (1-(alphaB/255))) * 255)
end
function add_area(name, x1, y1, x2, y2)
-- create area if needed
if (osc_param.areas[name] == nil) then
osc_param.areas[name] = {}
end
table.insert(osc_param.areas[name], {x1=x1, y1=y1, x2=x2, y2=y2})
end
function ass_append_alpha(ass, alpha, modifier)
local ar = {}
for ai, av in pairs(alpha) do
av = mult_alpha(av, modifier)
if state.animation then
av = mult_alpha(av, state.animation)
end
ar[ai] = av
end
ass:append(string.format("{\\1a&H%X&\\2a&H%X&\\3a&H%X&\\4a&H%X&}",
ar[1], ar[2], ar[3], ar[4]))
end
function ass_draw_cir_cw(ass, x, y, r)
ass:round_rect_cw(x-r, y-r, x+r, y+r, r)
end
function ass_draw_rr_h_cw(ass, x0, y0, x1, y1, r1, hexagon, r2)
if hexagon then
ass:hexagon_cw(x0, y0, x1, y1, r1, r2)
else
ass:round_rect_cw(x0, y0, x1, y1, r1, r2)
end
end
function ass_draw_rr_h_ccw(ass, x0, y0, x1, y1, r1, hexagon, r2)
if hexagon then
ass:hexagon_ccw(x0, y0, x1, y1, r1, r2)
else
ass:round_rect_ccw(x0, y0, x1, y1, r1, r2)
end
end
--
-- Tracklist Management
--
local nicetypes = {video = texts.video_track, audio = texts.audio_track, sub = texts.subtitle}
local nicetypes_pl = {video = texts.video_tracks, audio = texts.audio_tracks, sub = texts.subtitles}
-- updates the OSC internal playlists, should be run each time the track-layout changes
function update_tracklist()
local tracktable = mp.get_property_native("track-list", {})
-- by osc_id
tracks_osc = {}
tracks_osc.video, tracks_osc.audio, tracks_osc.sub = {}, {}, {}
-- by mpv_id
tracks_mpv = {}
tracks_mpv.video, tracks_mpv.audio, tracks_mpv.sub = {}, {}, {}
for n = 1, #tracktable do
if not (tracktable[n].type == "unknown") then
local type = tracktable[n].type
local mpv_id = tonumber(tracktable[n].id)
-- by osc_id
table.insert(tracks_osc[type], tracktable[n])
-- by mpv_id
tracks_mpv[type][mpv_id] = tracktable[n]
tracks_mpv[type][mpv_id].osc_id = #tracks_osc[type]
end
end
end
-- return a nice list of tracks of the given type (video, audio, sub)
function get_tracklist(type)
local msg = nicetypes_pl[type] .. ": "
if not tracks_osc or #tracks_osc[type] == 0 then
msg = msg .. texts.none
else
for n = 1, #tracks_osc[type] do
local track = tracks_osc[type][n]
local lang, title, selected = texts.unknown, "", "○"
if not(track.lang == nil) then lang = track.lang end
if not(track.title == nil) then title = track.title end
if (track.id == tonumber(mp.get_property(type))) then
selected = "●"
end
msg = msg.."\n"..selected.." "..n..". ["..lang.."] "..title
end
end
return msg
end
-- relatively change the track of given <type> by <next> tracks
--(+1 -> next, -1 -> previous)
function set_track(type, next)
local current_track_mpv, current_track_osc
if (mp.get_property(type) == "no") then
current_track_osc = 0
else
current_track_mpv = tonumber(mp.get_property(type))
current_track_osc = tracks_mpv[type][current_track_mpv].osc_id
end
local new_track_osc = (current_track_osc + next) % (#tracks_osc[type] + 1)
-- never set 0 for audio/video
if new_track_osc == 0 and type ~= "sub" then
new_track_osc = (current_track_osc + next - 1) % #tracks_osc[type] + 1
end
local new_track_mpv
if new_track_osc == 0 then
new_track_mpv = "no"
else
new_track_mpv = tracks_osc[type][new_track_osc].id
end
mp.commandv("set", type, new_track_mpv)
if (new_track_osc == 0) then
show_message(nicetypes[type] .. ": " .. texts.off)
else
show_message(nicetypes[type] .. ": "
.. new_track_osc .. "/" .. #tracks_osc[type]
.. " [" .. (tracks_osc[type][new_track_osc].lang or texts.unknown) .. "] "
.. (tracks_osc[type][new_track_osc].title or ""))
end
end
-- get the currently selected track of <type>, OSC-style counted
function get_track(type)
local track = mp.get_property(type)
if track ~= "no" and track ~= nil then
local tr = tracks_mpv[type][tonumber(track)]
if tr then
return tr.osc_id
end
end
return 0
end
-- WindowControl helpers
function window_controls_enabled()
val = user_opts.windowcontrols
if val == "auto" then
return not state.border
else
return val ~= "no"
end
end
function window_controls_alignment()
return user_opts.windowcontrols_alignment
end
--
-- Element Management
--
local elements = {}
function prepare_elements()
-- remove elements without layout or invisible
local elements2 = {}
for n, element in pairs(elements) do
if not (element.layout == nil) and (element.visible) then
table.insert(elements2, element)
end
end
elements = elements2
function elem_compare (a, b)
return a.layout.layer < b.layout.layer
end
table.sort(elements, elem_compare)
for _,element in pairs(elements) do
local elem_geo = element.layout.geometry
-- Calculate the hitbox
local bX1, bY1, bX2, bY2 = get_hitbox_coords_geo(elem_geo)
element.hitbox = {x1 = bX1, y1 = bY1, x2 = bX2, y2 = bY2}
local style_ass = assdraw.ass_new()
-- prepare static elements
style_ass:append("{}") -- hack to troll new_event into inserting a \n
style_ass:new_event()
style_ass:pos(elem_geo.x, elem_geo.y)
style_ass:an(elem_geo.an)
style_ass:append(element.layout.style)
element.style_ass = style_ass
local static_ass = assdraw.ass_new()
if (element.type == "box") then
--draw box
static_ass:draw_start()
ass_draw_rr_h_cw(static_ass, 0, 0, elem_geo.w, elem_geo.h,
element.layout.box.radius, element.layout.box.hexagon)
static_ass:draw_stop()
elseif (element.type == "slider") then
--draw static slider parts
local slider_lo = element.layout.slider
-- calculate positions of min and max points
element.slider.min.ele_pos = slider_lo.pad + slider_lo.handle_size / 2
element.slider.max.ele_pos = elem_geo.w - element.slider.min.ele_pos
element.slider.min.glob_pos = element.hitbox.x1 + element.slider.min.ele_pos
element.slider.max.glob_pos = element.hitbox.x1 + element.slider.max.ele_pos
-- -- --
static_ass:draw_start()
-- a hack which prepares the whole slider area to allow center placements such like an=5
static_ass:rect_cw(0, 0, elem_geo.w, elem_geo.h)
static_ass:rect_ccw(0, 0, elem_geo.w, elem_geo.h)
-- marker nibbles
if not (element.slider.markerF == nil) and (slider_lo.gap > 0) then
local markers = element.slider.markerF()
for _,marker in pairs(markers) do
if (marker >= element.slider.min.value) and
(marker <= element.slider.max.value) then
local s = get_slider_ele_pos_for(element, marker)
if (slider_lo.gap > 5) then -- draw triangles
--top
if (slider_lo.nibbles_top) then
static_ass:move_to(s - 3, slider_lo.gap - 5)
static_ass:line_to(s + 3, slider_lo.gap - 5)
static_ass:line_to(s, slider_lo.gap - 1)
end
--bottom
if (slider_lo.nibbles_bottom) then
static_ass:move_to(s - 3, elem_geo.h - slider_lo.gap + 5)
static_ass:line_to(s, elem_geo.h - slider_lo.gap + 1)
static_ass:line_to(s + 3, elem_geo.h - slider_lo.gap + 5)
end
else -- draw 2x1px nibbles
--top
if (slider_lo.nibbles_top) then
static_ass:rect_cw(s - 1, 0, s + 1, slider_lo.gap);
end
--bottom
if (slider_lo.nibbles_bottom) then
static_ass:rect_cw(s - 1, elem_geo.h - slider_lo.gap, s + 1, elem_geo.h);
end
end
end
end
end
end
element.static_ass = static_ass
-- if the element is supposed to be disabled,
-- style it accordingly and kill the eventresponders
if not (element.enabled) then
element.layout.alpha[1] = 153
element.eventresponder = nil
end
end
end
--
-- Element Rendering
--
-- returns nil or a chapter element from the native property chapter-list
function get_chapter(possec)
local cl = state.chapter_list -- sorted, get latest before possec, if any
for n=#cl,1,-1 do
if possec >= cl[n].time then
return cl[n]
end
end
end
function render_elements(master_ass)
for n=1, #elements do
local element = elements[n]
-- false if other element is active
local activatable = (state.active_element == nil) or (state.active_element == n)
local style_ass = assdraw.ass_new()
style_ass:merge(element.style_ass)
ass_append_alpha(style_ass, element.layout.alpha, 0)
if element.eventresponder and (state.active_element == n) then
-- run render event functions
if not (element.eventresponder.render == nil) then
element.eventresponder.render(element)
end
if mouse_hit(element) then
-- mouse down styling
if (element.styledown) then
ass_append_alpha(style_ass, element.layout.alpha, 102)
end
if (element.softrepeat) and (state.mouse_down_counter >= 15
and state.mouse_down_counter % 5 == 0) then
element.eventresponder[state.active_event_source.."_down"](element)
end
state.mouse_down_counter = state.mouse_down_counter + 1
end
end
local elem_ass = assdraw.ass_new()
elem_ass:merge(style_ass)
if not (element.type == "button") then
elem_ass:merge(element.static_ass)
end
if (element.type == "slider") then
local slider_lo = element.layout.slider
local elem_geo = element.layout.geometry
local s_min = element.slider.min.value
local s_max = element.slider.max.value
-- draw pos marker
local pos = element.slider.posF()
local foH = elem_geo.h / 2
local r = slider_lo.handle_size / 2
local bar_r = slider_lo.bar_height / 2
local range_r = bar_r + 2
local pad = slider_lo.pad
if element.slider.seekRangesF ~= nil then
local seekRanges = element.slider.seekRangesF()
end
if seekRanges then
elem_ass:merge(element.style_ass)
elem_ass:append(slider_lo.bg_style)
ass_append_alpha(elem_ass, element.layout.alpha, user_opts.seekrangealpha)
elem_ass:merge(element.static_ass)
for _,range in pairs(seekRanges) do
local pstart = get_slider_ele_pos_for(element, range["start"])
local pend = get_slider_ele_pos_for(element, range["end"])
ass_draw_rr_h_cw(elem_ass, pstart - range_r, foH - range_r,
pend + range_r, foH + range_r,
range_r, false)
end
end
if pos then
local xp = get_slider_ele_pos_for(element, pos)
elem_ass:merge(element.style_ass)
elem_ass:append(slider_lo.bg_style)
ass_append_alpha(elem_ass, slider_lo.alpha, slider_lo.bg_alpha)
elem_ass:merge(element.static_ass)
-- inactive bar
ass_draw_rr_h_cw(elem_ass, xp, foH - bar_r,
elem_geo.w - (pad + r - bar_r), foH + bar_r,
0, false, bar_r)
elem_ass:merge(element.style_ass)
ass_append_alpha(elem_ass, element.layout.alpha, 0)
elem_ass:merge(element.static_ass)
-- active bar
ass_draw_rr_h_cw(elem_ass, pad + r - bar_r, foH - bar_r,
xp, foH + bar_r,
bar_r, false, 0)
-- handle
ass_draw_rr_h_cw(elem_ass, xp - r, foH - r,
xp + r, foH + r,
r, false)
elem_ass:merge(element.style_ass)
ass_append_alpha(elem_ass, element.layout.alpha, 0)
elem_ass:merge(element.static_ass)
end
if element.enabled and not (slider_lo.adjust_tooltip) then
if mouse_hit(element) and activatable then
element.layout.alpha[1] = 0
else
element.layout.alpha[1] = 51
end
end
elem_ass:draw_stop()
-- add tooltip
if not (element.slider.tooltipF == nil) and element.enabled then
if mouse_hit(element) and activatable then
local sliderpos = get_slider_value(element)
local tooltiplabel = element.slider.tooltipF(sliderpos)
local an = slider_lo.tooltip_an
local ty
if (an == 2) then
ty = element.hitbox.y1 - 4
else
ty = element.hitbox.y1 + elem_geo.h/2
end
local tx = get_virt_mouse_pos()
if (slider_lo.adjust_tooltip) then
if (an == 2) then
if (sliderpos < (s_min + 3)) then
an = an - 1
elseif (sliderpos > (s_max - 3)) then
an = an + 1
end
elseif (sliderpos > (s_max+s_min)/2) then
an = an + 1
tx = tx - 5
else
an = an - 1
tx = tx + 10
end
else
-- for volumebar
tx = element.hitbox.x1 + elem_geo.w / 2
ty = element.hitbox.y1 - 20
end
-- tooltip label
elem_ass:new_event()
elem_ass:pos(tx, ty)
elem_ass:an(an)
elem_ass:append(slider_lo.tooltip_style)
ass_append_alpha(elem_ass, slider_lo.alpha, 0)
elem_ass:append(tooltiplabel)
-- thumbnail
if element.thumbnail and not thumbfast.disabled then
local osd_w = mp.get_property_number("osd-width")
if osd_w then
local r_w, r_h = get_virt_scale_factor()
local thumb_pad = user_opts.thumbpad
local thumb_margin_x = 16 / r_w + thumb_pad
local thumb_margin_y = 24 / r_h + thumb_pad
local thumb_x = math.min(osd_w - thumbfast.width - thumb_margin_x, math.max(thumb_margin_x, tx / r_w - thumbfast.width / 2))
local thumb_y = ty / r_h - thumbfast.height - thumb_margin_y
thumb_x = math.floor(thumb_x + 0.5)
thumb_y = math.floor(thumb_y + 0.5)
elem_ass:new_event()
elem_ass:pos(thumb_x * r_w, thumb_y * r_h)
elem_ass:append(slider_lo.tooltip_style)
elem_ass:draw_start()
elem_ass:rect_cw(-thumb_pad * r_w, -thumb_pad * r_h, (thumbfast.width + thumb_pad) * r_w, (thumbfast.height + thumb_pad) * r_h)
elem_ass:draw_stop()
mp.commandv("script-message-to", "thumbfast", "thumb",
mp.get_property_number("duration", 0) * (sliderpos / 100),
thumb_x,
thumb_y
)
end
end
else
if element.thumbnail and thumbfast.available then
mp.commandv("script-message-to", "thumbfast", "clear")
end
end
end
elseif (element.type == "button") then
local buttontext
if type(element.content) == "function" then
buttontext = element.content() -- function objects
elseif not (element.content == nil) then
buttontext = element.content -- text objects
end
buttontext = buttontext:gsub(':%((.?.?.?)%) unknown ', ':%(%1%)') --gsub('%) unknown %(\'', '')
local maxchars = element.layout.button.maxchars
-- 认为1个中文字符约等于1.5个英文字符
local charcount = (buttontext:len() + select(2, buttontext:gsub('[^\128-\193]', ''))*2) / 3
if not (maxchars == nil) and (charcount > maxchars) then
local limit = math.max(0, maxchars - 3)
if (charcount > limit) then
while (charcount > limit) do
buttontext = buttontext:gsub('.[\128-\191]*$', '')
charcount = (buttontext:len() + select(2, buttontext:gsub('[^\128-\193]', ''))*2) / 3
end
buttontext = buttontext .. "..."
end
end
if element.enabled then
if mouse_hit(element) and activatable then
element.layout.alpha[1] = 0
else
element.layout.alpha[1] = 51
end
end
elem_ass:append(buttontext)
-- add tooltip
if not (element.tooltipF == nil) and element.enabled then
if mouse_hit(element) and activatable then
local tooltiplabel = element.tooltipF
local an = 2
local ty = element.hitbox.y1 - 20
local tx = (element.hitbox.x1 + element.hitbox.x2) / 2
if ty < osc_param.playresy / 2 then
ty = element.hitbox.y2
an = 7
end
-- tooltip label
if type(element.tooltipF) == "function" then
tooltiplabel = element.tooltipF()
else
tooltiplabel = element.tooltipF
end
elem_ass:new_event()
elem_ass:pos(tx, ty)
elem_ass:an(an)
elem_ass:append(element.tooltip_style)
elem_ass:append(tooltiplabel)
end
end
end
master_ass:merge(elem_ass)
end
end
--
-- Message display
--
-- pos is 1 based
function limited_list(prop, pos)
local proplist = mp.get_property_native(prop, {})
local count = #proplist
if count == 0 then
return count, proplist
end
local fs = tonumber(mp.get_property('options/osd-font-size'))
local max = math.ceil(osc_param.unscaled_y*0.75 / fs)
if max % 2 == 0 then
max = max - 1
end
local delta = math.ceil(max / 2) - 1
local begi = math.max(math.min(pos - delta, count - max + 1), 1)
local endi = math.min(begi + max - 1, count)
local reslist = {}
for i=begi, endi do
local item = proplist[i]
item.current = (i == pos) and true or nil
table.insert(reslist, item)
end
return count, reslist
end
function get_playlist()
local pos = mp.get_property_number('playlist-pos', 0) + 1
local count, limlist = limited_list('playlist', pos)
if count == 0 then
return texts.playlist .. ': ' .. texts.none
end
local message = string.format(texts.playlist .. ' [%d/%d]:\n', pos, count)
for i, v in ipairs(limlist) do
local title = v.title
local _, filename = utils.split_path(v.filename)
if title == nil then
title = filename
end
message = string.format('%s %s %s. %s\n', message,
(v.current and '●' or '○'), i, title)
end
return message
end
function get_chapterlist()
local pos = mp.get_property_number('chapter', 0) + 1
local count, limlist = limited_list('chapter-list', pos)
if count == 0 then
return texts.chapters .. ': ' .. texts.none
end
local message = string.format(texts.chapters .. ' [%d/%d]:\n', pos, count)
for i, v in ipairs(limlist) do
local time = mp.format_time(v.time)
local title = v.title
if title == nil then
title = string.format(texts.chapter .. ' %02d', i)
end
message = string.format('%s %s [%s] %s\n', message,
(v.current and '●' or '○'), time, title)
end
return message
end
function show_message(text, duration)
--print("text: "..text.." duration: " .. duration)
if duration == nil then
duration = tonumber(mp.get_property("options/osd-duration")) / 1000
elseif not type(duration) == "number" then
print("duration: " .. duration)
end
-- cut the text short, otherwise the following functions
-- may slow down massively on huge input
text = string.sub(text, 0, 4000)
-- replace actual linebreaks with ASS linebreaks
text = string.gsub(text, "\n", "\\N")
state.message_text = text
if not state.message_hide_timer then