This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.lua
578 lines (482 loc) · 15.3 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
gl.setup(NATIVE_WIDTH, NATIVE_HEIGHT)
node.alias("walkin")
local json = require "json"
local loader = require "loader"
local walkin_state = "walkin"
local show_event_slide = false
local switch_time = sys.now()
local event_slide
local event_slide_alpha = 0
local progress_indicator = "no"
local settings = {
IMAGE_PRELOAD = 2;
VIDEO_PRELOAD = 2;
CHILD_PRELOAD = 2;
PRELOAD_TIME = 4;
FALLBACK_PLAYLIST = {
{
offset = 0;
total_duration = 1;
duration = 1;
asset_name = "empty.png";
type = "image";
}
}
}
local white = resource.create_colored_texture(1, 1, 1, 1)
local black = resource.create_colored_texture(0, 0, 0, 1)
local font = resource.load_font "roboto.ttf"
local function ramp(t_s, t_e, t_c, ramp_time)
if ramp_time == 0 then return 1 end
local delta_s = t_c - t_s
local delta_e = t_e - t_c
return math.min(1, delta_s * 1 / ramp_time, delta_e * 1 / ramp_time)
end
local function cycled(items, offset)
offset = offset % #items + 1
return items[offset], offset
end
local function strtobool(str)
if str == "true" then
return true
end
return false
end
local function printWalkinState()
print("CURRENTLY SHOWING: " .. walkin_state)
end
local Loading = (function()
local loading = "Loading..."
local size = 80
local w = font:width(loading, size)
local alpha = 0
local function draw()
if alpha == 0 then
return
end
font:write((WIDTH - w) / 2, (HEIGHT - size) / 2, loading, size, 1, 1, 1, alpha)
end
local function fade_in()
alpha = math.min(1, alpha + 0.01)
end
local function fade_out()
alpha = math.max(0, alpha - 0.01)
end
return {
fade_in = fade_in;
fade_out = fade_out;
draw = draw;
}
end)()
local Config = (function()
local playlist = {}
local switch_time = 1
local config_file = "config.json"
-- You can put a static-config.json file into the package directory.
-- That way the config.json provided by info-beamer hosted will be
-- ignored and static-config.json is used instead.
--
-- This allows you to import this package bundled with images/
-- videos and a custom generated configuration without changing
-- any of the source code.
if CONTENTS["static-config.json"] then
config_file = "static-config.json"
print "[WARNING]: will use static-config.json, so config.json is ignored"
end
util.file_watch(config_file, function(raw)
print("updated " .. config_file)
local config = json.decode(raw)
if config.auto_resolution then
gl.setup(NATIVE_WIDTH, NATIVE_HEIGHT)
else
gl.setup(config.width, config.height)
end
print("screen size is " .. WIDTH .. "x" .. HEIGHT)
if #config.playlist == 0 then
playlist = settings.FALLBACK_PLAYLIST
switch_time = 0
else
playlist = {}
local total_duration = 0
for idx = 1, #config.playlist do
local item = config.playlist[idx]
total_duration = total_duration + item.duration
end
local offset = 0
for idx = 1, #config.playlist do
local item = config.playlist[idx]
if item.duration > 0 then
playlist[#playlist + 1] = {
offset = offset,
total_duration = total_duration,
duration = item.duration,
asset_name = item.file.asset_name,
type = item.file.type,
}
offset = offset + item.duration
end
end
switch_time = config.switch_time
end
end)
return {
get_playlist = function() return playlist end;
get_switch_time = function() return switch_time end;
}
end)()
local Modules = (function()
local function is_module(module_name)
for name, module in pairs(loader.modules) do
if name == module_name then
return true, module
end
end
return false
end
return {
is_module = is_module;
}
end)()
local Scheduler = (function()
local playlist_offset = 0
local function get_next()
local playlist = Config.get_playlist()
local item
local got_content = false
while not got_content
do
item, playlist_offset = cycled(playlist, playlist_offset)
if item.type == "child" or item.type == "module" then
local is_mod, module = Modules.is_module(item.asset_name)
if is_mod then
if module.has_content() then
item.type = "module"
else
print(item.asset_name .. " module has no content")
end
got_content = module.has_content()
else
got_content = true --childs have content
end
else
got_content = true --images and videos have content
end
end
print(string.format("next scheduled item is %s [%f]", item.asset_name, item.duration))
return item
end
local function restart_schedule()
playlist_offset = 0
end
return {
get_next = get_next;
restart_schedule = restart_schedule;
}
end)()
local function draw_progress(starts, ends, now)
local mode = progress_indicator
if mode == "no" then
return
end
if ends - starts < 2 then
return
end
local progress = 1.0 / (ends - starts) * (now - starts)
if mode == "bar_thin_white" then
white:draw(0, HEIGHT - 10, WIDTH * progress, HEIGHT, 0.5)
elseif mode == "bar_thin_black" then
black:draw(0, HEIGHT - 10, WIDTH * progress, HEIGHT, 0.5)
elseif mode == "countdown" then
local remaining = math.ceil(ends - now)
local text
if remaining >= 60 then
text = string.format("%d:%02d", remaining / 60, remaining % 60)
else
text = remaining
end
local size = 32
local w = font:width(text, size)
black:draw(WIDTH - w - 4, HEIGHT - size - 4, WIDTH, HEIGHT, 0.6)
font:write(WIDTH - w - 2, HEIGHT - size - 2, text, size, 1, 1, 1, 0.8)
end
end
local ImageJob = function(item, ctx, fn)
fn.wait_t(ctx.starts - settings.IMAGE_PRELOAD)
local res = resource.load_image(ctx.asset)
for now in fn.wait_next_frame do
local state, err = res:state()
if state == "loaded" then
break
elseif state == "error" then
error("preloading failed: " .. err)
end
end
print "waiting for start"
local starts = fn.wait_t(ctx.starts)
local duration = ctx.ends - starts
print(">>> IMAGE", res, ctx.starts, ctx.ends)
while true do
local now = sys.now()
if walkin_state ~= "eventslide" then
util.draw_correct(res, 0, 0, WIDTH, HEIGHT, ramp(ctx.starts, ctx.ends, now, Config.get_switch_time()))
draw_progress(ctx.starts, ctx.ends, now)
end
if now > ctx.ends then
break
end
fn.wait_next_frame()
end
print("<<< IMAGE", res, ctx.starts, ctx.ends)
res:dispose()
return true
end
local VideoJob = function(item, ctx, fn)
fn.wait_t(ctx.starts - settings.VIDEO_PRELOAD)
local raw = sys.get_ext "raw_video"
local res = raw.load_video {
file = ctx.asset,
audio = false,
looped = false,
paused = true,
}
for now in fn.wait_next_frame do
local state, err = res:state()
if state == "paused" then
break
elseif state == "error" then
error("preloading failed: " .. err)
end
end
print "waiting for start"
fn.wait_t(ctx.starts)
print(">>> VIDEO", res, ctx.starts, ctx.ends)
res:start()
while true do
local now = sys.now()
if walkin_state ~= "eventslide" then
local state, width, height = res:state()
if state ~= "finished" then
local layer = -2
if now > ctx.starts + 0.1 then
-- after the video started, put it on a more
-- foregroundy layer. that way two videos
-- played after one another are sorted in a
-- predictable way and no flickering occurs.
layer = -1
end
local x1, y1, x2, y2 = util.scale_into(NATIVE_WIDTH, NATIVE_HEIGHT, width, height)
res:layer(layer):target(x1, y1, x2, y2, ramp(ctx.starts, ctx.ends, now, Config.get_switch_time()))
end
draw_progress(ctx.starts, ctx.ends, now)
end
if now > ctx.ends then
break
end
fn.wait_next_frame()
end
print("<<< VIDEO", res, ctx.starts, ctx.ends)
res:dispose()
return true
end
local ChildJob = function(item, ctx, fn)
fn.wait_t(ctx.starts - settings.CHILD_PRELOAD)
local res = resource.render_child(item.asset_name)
for now in fn.wait_next_frame do
local state, err = res:state()
if state == "loaded" then
break
elseif state == "error" then
error("preloading failed: " .. err)
end
end
print "waiting for start"
local starts = fn.wait_t(ctx.starts)
local duration = ctx.ends - starts
print(">>> CHILD", res, ctx.starts, ctx.ends)
while true do
local now = sys.now()
if walkin_state ~= "eventslide" then
util.draw_correct(res, 0, 0, WIDTH, HEIGHT, ramp(ctx.starts, ctx.ends, now, Config.get_switch_time()))
draw_progress(ctx.starts, ctx.ends, now)
end
if now > ctx.ends then
break
end
fn.wait_next_frame()
end
print("<<< CHILD", res, ctx.starts, ctx.ends)
res:dispose()
return true
end
local ModuleJob = function(item, ctx, fn)
fn.wait_t(ctx.starts - settings.CHILD_PRELOAD)
local is_mod, mod = Modules.is_module(item.asset_name)
local res = mod.get_surface()
for now in fn.wait_next_frame do
local state, err = res:state()
if state == "loaded" then
break
elseif state == "error" then
error("preloading failed: " .. err)
end
end
print "waiting for start"
local starts = fn.wait_t(ctx.starts)
local duration = ctx.ends - starts
print(">>> MODULE", item.asset_name, ctx.starts, ctx.ends)
while true do
local now = sys.now()
if walkin_state ~= "eventslide" then
util.draw_correct(res, 0, 0, WIDTH, HEIGHT, ramp(ctx.starts, ctx.ends, now, Config.get_switch_time()))
draw_progress(ctx.starts, ctx.ends, now)
end
if now > ctx.ends then
break
end
fn.wait_next_frame()
end
print("<<< MODULE", item.asset_name, ctx.starts, ctx.ends)
res:dispose()
return true
end
local Queue = (function()
local jobs = {}
local scheduled_until = sys.now()
local function clear_jobs()
jobs = {}
scheduled_until = sys.now()
end
local function enqueue(starts, ends, item)
local co = coroutine.create(({
image = ImageJob,
video = VideoJob,
child = ChildJob,
module = ModuleJob,
})[item.type])
local asset = null
if (item.type == "child" or item.type == "module") then
asset = item
else
local success, openfile = pcall(resource.open_file, item.asset_name)
if not success then
print("CANNOT GRAB ASSET: ", asset)
return
end
asset = openfile
end
-- start all content at begining of transition
if #jobs > 0 then
starts = starts - Config.get_switch_time()
end
local ctx = {
starts = starts,
ends = ends,
asset = asset;
}
local success, err = coroutine.resume(co, item, ctx, {
wait_next_frame = function()
return coroutine.yield(false)
end;
wait_t = function(t)
while true do
local now = coroutine.yield(false)
if now > t then
return now
end
end
end;
})
if not success then
print("CANNOT START JOB: ", err)
return
end
jobs[#jobs + 1] = {
co = co;
ctx = ctx;
type = item.type;
}
scheduled_until = ends
print("added job. scheduled program until ", scheduled_until)
end
local function tick()
gl.clear(0, 0, 0, 0)
for try = 1, 3 do
if sys.now() + settings.PRELOAD_TIME < scheduled_until then
break
end
local item = Scheduler.get_next()
enqueue(scheduled_until, scheduled_until + item.duration, item)
end
if #jobs == 0 then
Loading.fade_in()
else
Loading.fade_out()
end
local now = sys.now()
for idx = #jobs, 1, -1 do -- iterate backwards so we can remove finished jobs
local job = jobs[idx]
local success, is_finished = coroutine.resume(job.co, now)
if not success then
print("CANNOT RESUME JOB: ", is_finished)
table.remove(jobs, idx)
elseif is_finished then
table.remove(jobs, idx)
end
end
Loading.draw()
end
return {
tick = tick;
clear_jobs = clear_jobs;
}
end)()
util.data_mapper {
["show_event_slide"] = function(v)
local status = strtobool(v)
if status ~= show_event_slide then
show_event_slide = status
switch_time = sys.now()
if status then
walkin_state = "fadein"
else
walkin_state = "fadeout"
end
print("SHOW EVENT SLIDE " .. v)
else
print("EVENT SLIDE IS CURRENTLY " .. v)
end
end;
["eventid"] = function(eventid)
Queue.clear_jobs()
Scheduler.restart_schedule()
end;
["show_progress"] = function(v)
progress_indicator = v;
end;
}
util.set_interval(1, node.gc)
util.set_interval(5, printWalkinState)
function node.render()
gl.clear(0, 0, 0, 1)
Queue.tick()
local now = sys.now()
if walkin_state == "fadein" then
event_slide_alpha = ramp(switch_time, now + 1, now, Config.get_switch_time())
elseif walkin_state == "fadeout" then
event_slide_alpha = ramp(switch_time - 1, switch_time + 0.5, now, Config.get_switch_time())
end
if walkin_state ~= "walkin" then
event_slide = resource.render_child("event-slide")
event_slide:draw(0, 0, WIDTH, HEIGHT, event_slide_alpha)
end
if event_slide_alpha < 0 then
walkin_state = "walkin"
end
if event_slide_alpha >= 1 then
walkin_state = "eventslide"
end
if event_slide then
event_slide:dispose()
event_slide = nil
end
end