forked from info-beamer/package-scheduled-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.lua
2199 lines (1904 loc) · 63 KB
/
node.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
gl.setup(NATIVE_WIDTH, NATIVE_HEIGHT)
node.alias "*" -- catch all communication
util.no_globals()
math.randomseed(os.time())
local json = require "json"
local loader = require "loader"
local helper = require "helper"
local placement = require "placement"
local easing = require "easing"
local min, max, abs, floor, ceil = math.min, math.max, math.abs, math.floor, math.ceil
local font_regl = resource.load_font "default-font.ttf"
local font_bold = resource.load_font "default-font-bold.ttf"
local font_7seg = resource.load_font "7segment.ttf"
local colored = resource.create_shader[[
uniform sampler2D Texture;
varying vec2 TexCoord;
uniform vec4 color;
void main() {
gl_FragColor = texture2D(Texture, TexCoord) * color;
}
]]
local white_pixel = resource.create_colored_texture(1,1,1,1)
local function log(system, format, ...)
return print(string.format("[%s] " .. format, system, ...))
end
local function permute(tab)
for i = 1, #tab do
local j = math.random(i, #tab)
tab[i], tab[j] = tab[j], tab[i]
end
end
local function json_nullify(val)
if val == json.null then
return
else
return val
end
end
local function startswith(str, prefix)
return str:sub(1, #prefix) == prefix
end
local function Music()
local asset_name = nil
local music = nil
node.event("config_updated", function(config)
log("music", "updating music config settings")
if config.music.asset_id == "mute.mp4" then
if music then
music:dispose()
end
asset_name = nil
music = nil
elseif config.music.asset_name ~= asset_name then
if music then
music:dispose()
end
asset_name = config.music.asset_name
music = resource.load_video{
file = config.music.asset_name,
looped = true,
audio = true,
raw = true,
}
end
end)
end
local music = Music()
local function TCPClients()
local clients = {}
local handlers = {}
node.event("connect", function(client, path)
log("tcpclient", "new tcp client for %s", path)
clients[client] = {
path = path;
send = function(...)
node.client_write(client, ...)
end
}
end)
node.event("input", function(line, client)
local cinfo = clients[client]
if handlers[cinfo.path] then
handlers[cinfo.path](line)
end
end)
node.event("disconnect", function(client)
clients[client] = nil
end)
local function send(path, ...)
for client, cinfo in pairs(clients) do
if cinfo.path == path then
cinfo.send(...)
end
end
end
local function add_handler(path, handler)
handlers[path] = handler
end
return {
send = send;
add_handler = add_handler;
}
end
local tcp_clients = TCPClients()
local function Screen()
local placer
local rotation
local frame_time = 1/60
pcall(function()
local fps, swap_interval = sys.get_ext("screen").get_display_info()
frame_time = 1 / fps * swap_interval
log("screen", "detected frame delay is %f", frame_time)
end)
node.event("config_updated", function(config)
rotation = config.rotation
local is_portrait = rotation == 90 or rotation == 270
local width, height = config.resolution[1], config.resolution[2]
log("screen", "configured content resolution is %dx%d", width, height)
local surface = {
width = width,
height = height,
}
local target = {
x = 0,
y = 0,
width = NATIVE_WIDTH,
height = NATIVE_HEIGHT,
rotation = rotation,
}
if is_portrait then
surface.width, surface.height = surface.height, surface.width
end
placer = placement.Screen(target, surface)
end)
local function setup()
return placer.setup()
end
local function place_video(...)
return placer.place(...)
end
local function set_scissor(...)
return placer.scissor(...)
end
local function get_rotation()
return rotation
end
return {
setup = setup;
place_video = place_video;
set_scissor = set_scissor;
frame_time = frame_time;
get_rotation = get_rotation;
}
end
local screen = Screen()
local function FontCache()
local fonts = {}
local function get(filename)
local font = fonts[filename]
if not font then
font = {
obj = resource.load_font(filename),
}
fonts[filename] = font
end
font.lru = sys.now()
return font.obj
end
local function tick()
for filename, font in pairs(fonts) do
if sys.now() - font.lru > 300 then
log("fontcache", "purging font %s", filename)
fonts[filename] = nil
end
end
end
return {
get = get;
tick = tick;
}
end
local FontCache = FontCache()
local error_img = resource.create_colored_texture(1,0,0,1)
local function ImageCache()
local images = {}
local get, register
function get(asset_name, keep)
if not images[asset_name] then
register(asset_name, keep)
end
local image = images[asset_name]
if not image then
return error_img
end
if not image.obj then
image.obj = resource.load_image{
file = image.file,
fastload = true,
}
end
image.lru = max(image.lru, sys.now() + keep)
return image.obj
end
function register(asset_name, keep)
log("imagecache", "register %s %d", asset_name, keep)
if not images[asset_name] then
images[asset_name] = {
file = resource.open_file(asset_name),
lru = sys.now() + keep
}
end
return function(keep)
return get(asset_name, keep or 0)
end
end
local function tick()
for asset_name, image in pairs(images) do
local max_age = 0.5
if not image.obj then
max_age = 10
end
if sys.now() - image.lru > max_age then
log("imagecache", "purging image %s", asset_name)
image.file:dispose()
if image.obj then
image.obj:dispose()
end
images[asset_name] = nil
end
end
end
return {
register = register;
get = get;
tick = tick;
}
end
local ImageCache = ImageCache()
local function Clock()
local has_time = false
local time = {diff=0}
local updated = sys.now()
local function since_midnight()
local delta = sys.now() - updated
local seconds = (time.since_midnight + delta) % 86400
return seconds
end
return {
update = function(new_time)
time = new_time
has_time = true
updated = sys.now()
end;
has_time = function()
return has_time
end;
human = function()
local t = since_midnight()
return string.format("%02d:%02d", math.floor(t / 3600), math.floor(t % 3600 / 60))
end;
unix = function()
return os.time() + time.diff
end;
week_hour = function()
return time.week_hour
end;
day_of_week = function()
return time.dow
end;
since_midnight = since_midnight,
today = function()
return {
day = time.day;
month = time.month;
year = time.year;
}
end;
}
end
local function Clocks()
local tz_clocks = {}
local function create_and_get(tz)
if not tz_clocks[tz] then
tz_clocks[tz] = Clock()
end
return tz_clocks[tz]
end
util.data_mapper{
["clock/(.*)"] = function(tz, data)
local time = json.decode(data)
local clock = create_and_get(tz)
clock.update(time)
end;
}
return {
get = create_and_get;
}
end
local Clocks = Clocks()
local function Countdowns()
local countdowns = {}
util.data_mapper{
["countdown"] = function(update)
local update = json.decode(update)
countdowns[update.target] = update.unix
end;
}
local function delta(target)
local unix = countdowns[target]
if not unix then
return 0
end
return unix - os.time()
end
return {
delta = delta;
}
end
local Countdowns = Countdowns()
-- clock object pointing to the configured schedule timezone
local schedule_clock = setmetatable({
tz = "UTC",
}, {
__index = function(config, key)
return Clocks.get(config.tz)[key]
end,
})
node.event("config_updated", function(config)
if config.timezone == "device" then
schedule_clock.tz = config.__metadata.timezone
else
schedule_clock.tz = config.timezone
end
end)
local function clock_for_tz_or_default(tz)
if tz then
return Clocks.get(tz)
else
return schedule_clock
end
end
local SharedData = function()
-- {
-- scope: { key: data }
-- }
local data = {}
-- {
-- key: { scope: listener }
-- }
local listeners = {}
local function call_listener(scope, listener, key, value)
local ok, err = xpcall(listener, debug.traceback, scope, value)
if not ok then
log("shareddata", "while calling listener for key %s: %s", key, err)
end
end
local function call_listeners(scope, key, value)
local key_listeners = listeners[key]
if not key_listeners then
return
end
for _, listener in pairs(key_listeners) do
call_listener(scope, listener, key, value)
end
end
local function update(scope, key, value)
if not data[scope] then
data[scope] = {}
end
data[scope][key] = value
if value == nil and not next(data[scope]) then
data[scope] = nil
end
return call_listeners(scope, key, value)
end
local function delete(scope, key)
return update(scope, key, nil)
end
local function add_listener(scope, key, listener)
local key_listeners = listeners[key]
if not key_listeners then
listeners[key] = {}
key_listeners = listeners[key]
end
if key_listeners[scope] then
error "right now only a single listener is supported per scope"
end
key_listeners[scope] = listener
for scope, scoped_data in pairs(data) do
for key, value in pairs(scoped_data) do
call_listener(scope, listener, key, value)
end
end
end
local function del_scope(scope)
for key, key_listeners in pairs(listeners) do
key_listeners[scope] = nil
if not next(key_listeners) then
listeners[key] = nil
end
end
local scoped_data = data[scope]
if scoped_data then
for key, value in pairs(scoped_data) do
delete(scope, key)
end
end
data[scope] = nil
end
return {
update = update;
delete = delete;
add_listener = add_listener;
del_scope = del_scope;
}
end
local data = SharedData()
local tile_loader = loader.setup "tile.lua"
tile_loader.before_load = function(tile, exports)
exports.tcp_clients = tcp_clients
exports.wait_frame = helper.wait_frame
exports.wait_t = helper.wait_t
exports.frame_between = helper.frame_between
exports.screen = {
place_video = screen.place_video;
set_scissor = screen.set_scissor;
get_rotation = screen.get_rotation;
}
exports.clock = schedule_clock;
exports.update_data = function(key, value)
data.delete(tile, key)
data.update(tile, key, value)
end
exports.add_listener = function(key, listener)
data.add_listener(tile, key, listener)
end
end
tile_loader.unload = function(tile)
data.del_scope(tile)
end
local function dispatch_to_all_tiles(event, ...)
for module_name, module in pairs(tile_loader.modules) do
local fn = module[event]
if fn then
local ok, err = xpcall(fn, debug.traceback, ...)
if not ok then
log(
"dispatch_to_all_tiles",
"cannot dispatch '%s' into '%s': %s",
event, module_name, err
)
end
end
end
end
local kenburns_shader = resource.create_shader[[
uniform sampler2D Texture;
varying vec2 TexCoord;
uniform vec4 Color;
uniform float x, y, s;
void main() {
gl_FragColor = texture2D(Texture, TexCoord * vec2(s, s) + vec2(x, y)) * Color;
}
]]
local gl_effects = {
none = function(x1, y1, x2, y2)
local w, h = x2-x1, y2-y1
return function(draw, t)
gl.pushMatrix()
gl.translate(x1, y1)
draw(t)
gl.popMatrix()
end, w, h
end,
rotation = function(x1, y1, x2, y2, config)
local w, h = x2-x1, y2-y1
return function(draw, t, starts, ends)
local effect_easing = config.effect_easing or 'inQuad'
local effect_rotation = config.effect_rotation or 'y-axis'
local effect_pivot = config.effect_pivot or 'center'
local pivot_x, pivot_y = unpack(({
center = { .5, .5},
top = { .5, 0},
bottom = { .5, 1},
left = { 0, .5},
right = { 1, .5},
})[effect_pivot])
local enter_t = min(t-starts, 1)
local exit_t = 1-max(0, 1-(ends-t))
local effect_value = (
-easing[effect_easing](1-enter_t, 0, 1, 1)
+easing[effect_easing](1-exit_t, 0, 1, 1)
)
gl.pushMatrix()
gl.translate(x1+w*pivot_x, y1+h*pivot_y)
if effect_rotation == 'y-axis' then
gl.rotate(effect_value*90, 0, 1, 0)
elseif effect_rotation == 'x-axis' then
gl.rotate(effect_value*90, 1, 0, 0)
end
gl.translate(-w*pivot_x, -h*pivot_y)
draw(t)
gl.popMatrix()
end, w, h
end,
enter_exit_move = function(x1, y1, x2, y2, config)
local w, h = x2-x1, y2-y1
return function(draw, t, starts, ends)
local effect_duration = config.effect_duration or 1
local effect_easing = config.effect_easing or 'inQuad'
local effect_direction = config.effect_direction or 'from_left'
local progress = easing[effect_easing](
1 - helper.ramp(starts, ends, t, effect_duration),
0, 1, 1
)
local move_x, move_y = unpack(({
from_left = {-w, 0},
from_right = { w, 0},
from_bottom = { 0, h},
from_top = { 0, -h},
})[effect_direction])
gl.pushMatrix()
gl.translate(x1+move_x*progress, y1+move_y*progress)
draw(t)
gl.popMatrix()
end, w, h
end,
}
local function ChildTile(asset, config, x1, y1, x2, y2)
return function(starts, ends)
local impl = tile_loader.modules[asset.asset_name]
return impl.task(starts, ends, config, x1, y1, x2, y2)
end
end
local function ImageTile(asset, config, x1, y1, x2, y2)
-- config:
-- kenburns: true/false
-- fade_time: 0-1
-- fit: true/false
local img = ImageCache.register(asset.asset_name, 10)
local fade_time = config.fade_time or 0
return function(starts, ends)
helper.wait_t(starts - 2)
-- force loading and keep around for 3 seconds minimum
img(3)
local effect, width, height = gl_effects[
config.effect or 'none'
](x1, y1, x2, y2, config)
local function draw(now)
if config.fit then
util.draw_correct(img(), 0, 0, width, height, helper.ramp(
starts, ends, now, fade_time
))
else
img():draw(0, 0, width, height, helper.ramp(
starts, ends, now, fade_time
))
end
end
if config.kenburns then
local function lerp(s, e, t)
return s + t * (e-s)
end
local paths = {
{from = {x=0.0, y=0.0, s=1.0 }, to = {x=0.08, y=0.08, s=0.9 }},
{from = {x=0.05, y=0.0, s=0.93}, to = {x=0.03, y=0.03, s=0.97}},
{from = {x=0.02, y=0.05, s=0.91}, to = {x=0.01, y=0.05, s=0.95}},
{from = {x=0.07, y=0.05, s=0.91}, to = {x=0.04, y=0.03, s=0.95}},
}
local path = paths[math.random(1, #paths)]
local to, from = path.to, path.from
if math.random() >= 0.5 then
to, from = from, to
end
local w, h = img():size()
local duration = ends - starts
local function lerp(s, e, t)
return s + t * (e-s)
end
for now in helper.frame_between(starts, ends) do
local t = (now - starts) / duration
kenburns_shader:use{
x = lerp(from.x, to.x, t);
y = lerp(from.y, to.y, t);
s = lerp(from.s, to.s, t);
}
effect(draw, now, starts, ends)
kenburns_shader:deactivate()
end
else
for now in helper.frame_between(starts, ends) do
effect(draw, now, starts, ends)
end
end
end
end
local transparent_shader = resource.create_shader[[
uniform sampler2D Texture;
varying vec2 TexCoord;
uniform vec3 Transparent;
uniform vec4 Color;
// These values seem to work reasonably well.
const float thresholdSensitivity = 0.15;
const float smoothing = 0.3;
void main() {
vec3 col = texture2D(Texture, TexCoord).rgb;
float maskY = 0.2989 * Transparent.r + 0.5866 * Transparent.g + 0.1145 * Transparent.b;
float maskCr = 0.7132 * (Transparent.r - maskY);
float maskCb = 0.5647 * (Transparent.b - maskY);
float Y = 0.2989 * col.r + 0.5866 * col.g + 0.1145 * col.b;
float Cr = 0.7132 * (col.r - Y);
float Cb = 0.5647 * (col.b - Y);
float blendValue = smoothstep(thresholdSensitivity, thresholdSensitivity + smoothing, distance(vec2(Cr, Cb), vec2(maskCr, maskCb)));
gl_FragColor = vec4(col * blendValue, 1.0 * blendValue);
}
]]
local function VideoTile(asset, config, x1, y1, x2, y2)
-- config:
-- fade_time: 0-1
-- looped
-- transparency: bool
-- transparent_color: #ffffff
local file = resource.open_file(asset.asset_name)
local fade_time = config.fade_time or 0.5
local looped = config.looped
local audio = config.audio
local transparency = config.transparency
local r, g, b = helper.parse_rgb(config.transparent_color or "#ffffff")
local shader_args = {
Transparent = {r, g, b}
}
return function(starts, ends)
helper.wait_t(starts - 2)
local vid = resource.load_video{
file = file,
paused = true,
looped = looped,
audio = audio,
}
for now in helper.frame_between(starts, ends) do
if transparency then
transparent_shader:use(shader_args)
end
vid:draw(x1, y1, x2, y2, helper.ramp(
starts, ends, now, fade_time
)):start()
if transparency then
transparent_shader:deactivate()
end
end
vid:dispose()
end
end
local function RawVideoTile(asset, config, x1, y1, x2, y2)
-- config:
-- asset_name: 'foo.mp4'
-- fit: aspect fit or scale?
-- fade_time: 0-1
-- looped
-- layer: video layer for raw videos
local file = resource.open_file(asset.asset_name)
local fade_time = config.fade_time or 0.5
local looped = config.looped
local audio = config.audio
local layer = config.layer or 5
return function(starts, ends)
helper.wait_t(starts - 2)
local vid = resource.load_video{
file = file,
paused = true,
looped = looped,
audio = audio,
raw = true,
}
vid:layer(-10)
for now in helper.frame_between(starts, ends) do
screen.place_video(vid, layer, helper.ramp(
starts, ends, now, fade_time
), x1, y1, x2, y2):start()
end
vid:dispose()
end
end
local function Streams()
local frame = 0
local streams = {}
local MIN_LOAD_INTERVAL = 5
local LOADING_TIMEOUT = 10
local function stream_key(url, audio)
return string.format("%s|%s", url, audio)
end
local function get_stream(url, audio, immediate_reload)
local now = sys.now()
local key = stream_key(url, audio)
if not streams[key] then
streams[key] = {
last_used = now,
next_load = now,
last_load = nil,
url = url,
vid = nil,
}
end
local stream = streams[key]
local state = stream.vid and stream.vid:state()
local need_reload = (
not stream.vid
or state == "finished" or state == "error"
or (state == "loading" and now - stream.last_load > LOADING_TIMEOUT)
)
if need_reload then
print("[stream] needs reload", url)
if stream.vid then
print "[stream] disposing current stream instance"
stream.vid:dispose()
stream.vid = nil
end
if now >= stream.next_load or immediate_reload then
stream.next_load = now + MIN_LOAD_INTERVAL
stream.last_load = now
stream.vid = resource.load_video{
file = url,
audio = audio,
raw = true,
}
stream.vid:layer(-10):place(0, 0, 0, 0):alpha(0)
end
end
stream.last_used = frame
return stream.vid
end
local function tick()
frame = frame + 1
if frame % 300 == 0 then
print "[stream] active streams"
pp(streams)
end
for key, stream in pairs(streams) do
local frame_delta = frame - stream.last_used
if frame_delta > 1 then
print("[stream] disposing stream", stream.url)
if stream.vid then
stream.vid:dispose()
end
streams[key] = nil
end
end
end
return {
get_stream = get_stream;
tick = tick;
}
end
local streams = Streams()
local function StreamTile(asset, config, x1, y1, x2, y2)
local layer = config.layer or 5
local url = config.url or ""
local audio = config.audio
return function(starts, ends)
helper.wait_t(starts - 1)
-- force load
streams.get_stream(url, audio, true)
-- keepalive before start
for now in helper.frame_between(0, starts) do
streams.get_stream(url, audio)
end
streams.get_stream(url, audio)
-- player
for now in helper.frame_between(starts, ends) do
local vid = streams.get_stream(url, audio)
if vid then
screen.place_video(vid, layer, 1, x1, y1, x2, y2)
end
end
end
end
local function FlatTile(asset, config, x1, y1, x2, y2)
-- config:
-- color: "#rrggbb"
-- fade_time: 0-1
local r, g, b = helper.parse_rgb(config.color or "#ffffff")
local a = (config.alpha or 255)/255
local flat = resource.create_colored_texture(r, g, b, a)
local fade_time = config.fade_time or 0.5
return function(starts, ends)
for now in helper.frame_between(starts, ends) do
flat:draw(x1, y1, x2, y2, helper.ramp(
starts, ends, now, fade_time
))
end
flat:dispose()
end
end
local function CountdownTile(asset, config, x1, y1, x2, y2)
local target = config.target
local type = config.type or "hms"
local r, g, b = helper.parse_rgb(config.color or "#333333")
local align = config.align or "center"
local mode = config.mode or "countdown"
local size = y2 - y1
local font = ({
default = font_regl,
digital = font_7seg,
})[config.font or "default"]
local fmt = ({
german = {
dh = "%d Tag(e), %d Std.",
hms = "%d Std %d Min %d Sek",
ms = "%d Min %d Sek",
hm = "%d Std %d Min",
},
english = {
dh = "%d day(s), %dh",
hms = "%dh %dm %ds",
ms = "%dm %ds",
hm = "%dh %dm",
},
none = {
hms = "%d:%02d:%02d",
ms = "%d:%02d",
hm = "%d:%02d",
},
})[config.locale or "english"]
return function(starts, ends)
for now in helper.frame_between(starts, ends) do
local delta = Countdowns.delta(target)
delta = ceil(delta)
if mode == "countdown" then
delta = max(0, delta)
elseif mode == "countup" then
delta = min(0, delta)
end
delta = abs(delta)
local text
if type == "hms" then
text = string.format(fmt.hms,
math.floor(delta / 3600),
math.floor(delta % 3600 / 60),
math.floor(delta % 60)
)
elseif type == "hm" then
text = string.format(fmt.hm,
math.floor(delta / 3600),
math.floor(delta % 3600 / 60)
)
elseif type == "adaptive_dhm" then
if abs(delta) > 86400 then
text = string.format(fmt.dh,
math.floor(delta / 86400),
math.floor(delta % 86400 / 3600)
)
elseif abs(delta) > 120 * 60 then