-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine.js
996 lines (762 loc) · 27.6 KB
/
engine.js
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
// SETTINGS ////////////////////////////////////////////////////////
var FILE = "game.tw";
var VIDEO_PATH = "videos/";
//once you reach the end of the clip select the first choice (true)
//if false disables the bar
var AUTOCHOICE = true;
//allows players to pause the video
var VIDEO_PAUSE = true;
//allows pausing when the choices are on
var PAUSE_ON_CHOICE = false;
//allows play pause by clicking on the video
var PAUSE_ON_CLICK = false;
//if true choices are rendered in a table, otherwise it's divs
var TABLE = true;
//choices panels appears instantly or slides up
var ANIMATED = true;
//make all choices instantaneous by default
//can be overridden case by case eg {movie.mp4|passageTitle|delayed}
var INSTANT_CHOICES = false;
//preload all the videos or just the next choices?
//if true you are 99% the playback will be smooth but it may be a lot to load
var PRELOAD_ALL = false;
//activates the browser base video control, not generally recommended
//as it would visually interfere with the choices overlay
var DEFAULT_VIDEO_CONTROLS = false;
//activates a custom control (and related keyboard shortcuts with the buttons activated belo)
var VIDEO_CONTROLS = true;
//allow fast forward to the next choice, or if choice is already displayed, next clip
var FAST_FORWARD = true;
//allow rewind to the previous passage ie UNDO
var REWIND = true;
//allow skipping 10 seconds
var FFD10 = true;
//allow rewind 10 seconds
var REWIND10 = true;
//time the controls stay visible after mouse moved
var FADE_TIME = 2000;
///////////////////////////////////////////////////////////////////
var GAME = {};
var videoObjects = {};
var preloadInterval;
var updateInterval;
var mouseMoveInterval;
var mouseHideInterval;
var currentPassage;
var currentVideo;
var currentChoices = [];
var choicesTime;
var choicesOn;
//choice is made
var currentChoice;
//is the current choice instantaneous
var instantChoice;
//before autoplaying videos the user needs to click on the document
var interacted = false;
//ids of passages since the start
var gameHistory = [];
function initGame() {
$.get(FILE, function (data) {
GAME = parseTwee(data);
//find the videos
for (id in GAME) {
//parse the video macro
var videoTag = GAME[id].text.match(/{([\s\S]*?)}/);
//found link
if (videoTag != null) {
//create invisible muted videos
var tag = videoTag[1].split("|");
var fileName = tag[0];
//remove whitespaces
fileName = fileName.replace(/^\s+|\s+$/g, "");
var video = document.createElement('video');
document.getElementById('videos').appendChild(video);
video.id = id;
video.classList.add("passageVideo");
//print("Loading "+VIDEO_PATH + fileName+"...");
video.src = VIDEO_PATH + fileName;
if (PRELOAD_ALL) {
video.load();
video.preload = 'auto';
}
else {
video.preload = 'none';
}
video.playsinline = true;
video.muted = false;
video.controls = DEFAULT_VIDEO_CONTROLS;
video.pause();
video.style.display = 'none';
//add control on the video element
if(PAUSE_ON_CLICK)
$(video).on('click touch', playPause);
else if(VIDEO_CONTROLS)
$(video).on('click touch', revealMenu);
//save the reference
videoObjects[id] = video;
}
else {
print("WARNING: passage " + id + " doesn't have a clip associated");
}
}//go through all videos
///add keyboard controls
$(window).keydown(function (e) {
//allow play pause with space
if (VIDEO_CONTROLS && VIDEO_PAUSE) {
if (e.key === ' ' || e.key === 'Spacebar') {
// ' ' is standard, 'Spacebar' was used by IE9 and Firefox < 37
e.preventDefault()
playPause();
}
}
//skip to the choice or next clip
if (FAST_FORWARD) {
if (e.which == 39) {
fastForward();
e.preventDefault();
}
}
//skip to the choice or next clip
if (REWIND) {
if (e.which == 37) {
rewind();
e.preventDefault();
}
}
});
//add controls
if (VIDEO_CONTROLS) {
if (VIDEO_PAUSE) {
$("#play, #pause").on('click touch', function (e) {
e.preventDefault();
if (currentVideo != null) {
playPause();
}
});
$("#play").css("display", "none");
}
else {
//if not active disable
$("#pause, #play").css("display", "none");
}
if (FAST_FORWARD) {
$("#next").on('click touch', function (e) {
e.preventDefault();
if (currentVideo != null) {
fastForward();
}
});
}
else {
$("#next").css("display", "none");
}
if (REWIND) {
$("#previous").on('click touch', function (e) {
e.preventDefault();
if (currentVideo != null) {
rewind();
}
});
}
else {
$("#previous").css("display", "none");
}
if (FFD10) {
$("#ffd10").on('click touch', function (e) {
e.preventDefault();
if (currentVideo != null) {
ffd10();
}
});
}
else {
$("#ffd10").css("display", "none");
}
if (REWIND10) {
$("#rewind10").on('click touch', function (e) {
e.preventDefault();
if (currentVideo != null) {
rewind10();
}
});
}
else {
$("#rewind10").css("display", "none");
}
/*
if (FULLSCREEN) {
$("#fullscreen").on('click touch', function (e) {
e.preventDefault();
if (currentVideo != null) {
openFullscreen(currentVideo);
}
});
}
else {
$("#fullscreen").css("display", "none");
}
*/
}
if (PRELOAD_ALL) {
preloadInterval = setInterval(function () {
var loaded = true;
for (id in videoObjects) {
if (videoObjects[id].readyState !== 4) {
loaded = false;
}
}
if (loaded) {
print("all videos loaded");
clearInterval(preloadInterval);
if (GAME.Start != null) {
startGame("Start");
}
else {
print("Error: no passage titled 'Start'")
}
}
}, 500); //end preload loop
}//end preload all
//preload the first one and then load the next choices
if (!PRELOAD_ALL) {
//is there a start
if (GAME.Start != null) {
//there is a video associated? display
if (videoObjects["Start"] != null) {
var v = videoObjects["Start"];
v.load();
//preload
//fire this event only once
$(v).one('canplay', function (event) {
startGame("Start");
});
}
else {
//no video on the first passage, just start
startGame("Start");
}
}
else {
print("Error: no passage titled 'Start'")
}
}
}, 'text');
$("#choicesContainer").css("display", "none");
$("#choicesContainer").css("animation-play-state", "paused");
if(!AUTOCHOICE)
$("#bar").css("visibility", "hidden");
}
function startGame(pId) {
gameHistory = [];
clearInterval(updateInterval);
displayPassage(pId);
//main loop constantly checking on the video
updateInterval = setInterval(update, 1000 / 60);
resizeContainer();
revealMenu();
}
function update() {
if (currentVideo != null) {
//print("current time "+currentVideo.currentTime*1000);
//display the choices
if (!choicesOn && currentVideo.currentTime * 1000 > choicesTime) {
currentChoices = getChoices(GAME[currentPassage].text);
//only one choice: don't display, change at the end of the video, unless it's instant
if (currentChoices.length == 1 && (instantChoice || (!instantChoice && choicesTime == 0))) {
currentChoice = currentChoices[0].passage;
}
else {
//allows layouts based on the number of choices eg: .choices.layout1 {}
var html = "";
choicesOn = true;
//showMouse();
if (VIDEO_CONTROLS) $("#menuContainer").css("display", "none");
if (TABLE) {
html = '<table border="0" cellpadding="0" cellspacing="0" class="tableChoices tableLayut' + currentChoices.length + '"><tr class="rowChoice">';
for (var i = 0; i < currentChoices.length; i++) {
print(currentChoices[i].passage);
html += "<th class='tableChoice layout" + currentChoices.length + "'><a class='passageLink' href='#' onclick='makeChoice(\"" + currentChoices[i].passage + "\", this); return false; '>" + currentChoices[i].linkText + "</a></th>";
}
html += '</tr></table>';
}
else {
html = '<div class="choices layout' + currentChoices.length + '">';
for (var i = 0; i < currentChoices.length; i++) {
//print(currentChoices[i].passage);
html += "<div class='choice layout" + currentChoices.length + "'><a class='passageLink' href='#' onclick='makeChoice(\"" + currentChoices[i].passage + "\", this); return false; '>" + currentChoices[i].linkText + "</a></div>";
}
html += '</div>';
}
$("#choices").html(html);
$("#choicesContainer").css("display", "block");
if (ANIMATED)
$("#choicesContainer").css("animation-play-state", "running");
else {
$("#choicesContainer").css("bottom", "0px");
$("#choicesContainer").css("animation-play-state", "paused");
}
}//multi choice
}
//display countdown
if (choicesOn && AUTOCHOICE) {
var w = map(currentVideo.currentTime * 1000, choicesTime, currentVideo.duration * 1000, 100, 0);
$("#bar").css("width", w + "%");
}
}//end there is a video
}
function fastForward() {
//only works with video
if (currentVideo != null) {
var nextChoices = getChoices(GAME[currentPassage].text);
if (nextChoices.length == 1) {
//skip to next
currentVideo.currentTime = currentVideo.duration;
}
else if (nextChoices.length > 1) {
if (!choicesOn) {
//get the control delay in milliseconds
var nextChoicesTime = getChoicesTime(GAME[currentPassage].text);
if (nextChoicesTime != 0)
currentVideo.currentTime = nextChoicesTime / 1000;
}
else {
//choices are on
currentVideo.currentTime = currentVideo.duration;
}
}
}
}
function rewind() {
if (gameHistory.length > 1) {
//pop the current
gameHistory.pop();
//display
displayPassage(gameHistory[gameHistory.length - 1]);
//avoid duplicate
gameHistory.pop();
}
else if (gameHistory.length == 1) {
//
displayPassage(gameHistory[0]);
gameHistory.pop();
}
}
//10 sec rewind
function rewind10() {
if (currentVideo != null) {
currentVideo.currentTime = currentVideo.currentTime - 10;
}
}
function ffd10() {
if (currentVideo != null) {
currentVideo.currentTime = currentVideo.currentTime + 10;
}
}
/*
//not working reliable with overlays at the moment
function openFullscreen(elem) {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.mozRequestFullScreen) {
//Firefox
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen) {
//Chrome, Safari and Opera
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen) {
//IE/Edge
elem.msRequestFullscreen();
}
resizeContainer();
}
*/
function playPause() {
if (!choicesOn || (choicesOn && PAUSE_ON_CHOICE))
if (currentPassage != null)
if (currentVideo != null) {
if (currentVideo.paused) {
currentVideo.play();
if (VIDEO_CONTROLS) {
$("#play").css("display", "none");
$("#pause").css("display", "inline-block");
}
}
else {
currentVideo.pause();
if (VIDEO_CONTROLS) {
if (!choicesOn)
$("#menuContainer").css("display", "block");
$("#pause").css("display", "none");
$("#play").css("display", "inline-block");
}
}
}
}
function videoEnded() {
//auto select the first choice at the end of the clip OR the only choice
if ((AUTOCHOICE && currentChoice == null) || currentChoices.length == 1) {
displayPassage(currentChoices[0].passage);
}
else if (currentChoice == null) {
//no choice and no autochoice
//no choice
if (!AUTOCHOICE && currentVideo != null)
currentVideo.pause();
}
else {
displayPassage(currentChoice);
}
}
function displayPassage(pId) {
currentChoice = null;
//hide the current video if any
if (currentPassage != null) {
if (currentVideo != null) {
currentVideo.pause();
currentVideo.style.display = 'none';
}
}
//get rid of the choices
$("#choices").html("");
$("#passage").html("");
$("#choicesContainer").css("display", "none");
$("#choicesContainer").css("animation-play-state", "paused");
currentVideo = null;
currentPassage = null;
//there is a video associated? display
if (videoObjects[pId] != null) {
currentVideo = videoObjects[pId];
currentPassage = pId;
gameHistory.push(pId);
currentVideo.style.display = 'block';
if (interacted) {
currentVideo.play();
}
//$("#container").css("cursor", "none");
//clearTimeout(mouseHideInterval);
//get the control delay in milliseconds
choicesTime = getChoicesTime(GAME[pId].text);
//is it instant or delayed
instantChoice = getChoicesType(GAME[pId].text);
choicesOn = false;
//add a timeout parsetext display
currentVideo.currentTime = 0;
currentVideo.onended = videoEnded;
}//no video? display the placeholder text
else {
currentPassage = pId;
gameHistory.push(pId);
var html = parseText(GAME[pId].text);
$("#passage").html(html);
}
if (!PRELOAD_ALL) {
//start loading the next possible videos
var nextChoices = getChoices(GAME[currentPassage].text);
for (var i = 0; i < nextChoices.length; i++) {
var psg = nextChoices[i].passage;
if (videoObjects[psg] != null) {
videoObjects[psg].load();
}
}
}
resizeContainer();
}
//creates a PASSAGE object starting from a twee file, only passages, titles, links, and tags are supported
function parseTwee(data) {
var OBJ = {};
var parsingErrors = "";
//split passages
var arr = data.split(":: ");
for (var i = 0; i < arr.length; i++) {
//header is first line
var header = arr[i].match(/(.+)/);
if (header != null) {
var id = header[1];
print(">>> " + id);
//everything outside of the header is body
var txt = arr[i].replace(header[0], "");
var room = "";
//room is in the tag field
var roomArr = header[1].match(/\[(.*?)\]/);
if (roomArr != null) {
//remove leading and trailing spaces
room = roomArr[1].replace(/^\s+|\s+$/g, "");
//strip tags from header if any
id = id.replace(roomArr[0], "");
if (room.match(/\W/g) != null)
parsingErrors += "Room id error: room id must not contain special characters or spaces - triggered by " + room + "\n";
}
//remove leading and trailing whitespaces
id = id.replace(/^\s+|\s+$/g, "");
if (id.match(/\W/g) != null)
parsingErrors += "Twee file error: passage title must not contain special characters or spaces - triggered by " + id + "\n";
else {
//header non null and valid as id
//read the properties of the passage
OBJ[id] = {};
//if there is a room associated to a passage
if (room != null) {
OBJ[id].room = room;
}
OBJ[id].text = txt.trim();
OBJ[id].id = id;
//print("Text: " + txt);
}
}
}//passage loop
if (parsingErrors != "") {
//print(parsingErrors);
return null
}
else {
print("Twee file parsed successfully: " + Object.keys(OBJ).length + " passages");
return OBJ;
}
}
function getChoicesTime(txt) {
//parse the video macro
var macro = txt.match(/{([\s\S]*?)}/);
var time = 0;
//found link
if (macro != null) {
var m = macro[1].split("|");
var t = m[1];
if (t != null) {
//remove whitespaces and spaces
t = t.replace(/^\s+|\s+$/g, "");
t = t.replace(" ", "");
var a = t.split(':'); // split it at the colons
//seconds and milliseconds
if (a[a.length - 1] != null)
time += parseFloat(a[a.length - 1]);
//minutes
if (a[a.length - 2] != null)
time += parseFloat(a[a.length - 2]) * 60;
//HOURS???
if (a[a.length - 3] != null)
time += parseFloat(a[a.length - 3]) * 60 * 60;
time *= 1000;
}
}
return time;
}
//instant or not
function getChoicesType(txt) {
//set the default
var type = INSTANT_CHOICES;
//parse the video macro
var macro = txt.match(/{([\s\S]*?)}/);
//found link
if (macro != null) {
var m = macro[1].split("|");
//third optional member
if (m.length >= 3) {
t = m[2].toLowerCase();
//remove whitespaces and spaces
t = t.replace(/^\s+|\s+$/g, "");
t = t.replace(" ", "");
if (t == "instant") {
type = true;
}
if (t == "delayed") {
type = false;
}
}
}
return type;
}
function makeChoice(passage, elt) {
if (instantChoice) {
displayPassage(passage);
}
else {
if (currentChoice == null) {
currentChoice = passage;
//remove rollover states
$(elt).addClass('active');
//deactivate the other links
$(".passageLink").each(function (index) {
//compare jquery elements ayayayay
if ($(this)[0] != $(elt)[0]) {
$(this).removeClass("passageLink");
$(this).addClass("inactiveLink");
}
});
//special case, the video is over and choice is not automatic, jump
if (currentVideo != null)
if (!AUTOCHOICE && currentVideo.currentTime == currentVideo.duration) {
displayPassage(currentChoice);
}
}
}
}
//creates an array with the destination passages and link text
function getChoices(txt) {
//parse the "links" between passages
var link = txt.match(/\[\[([\s\S]*?)\]\]/);
var failSafe = 0;
var choices = [];
//{linkText:x, passage:t}
while (link != null && failSafe < 1000) {
//found link
if (link != null) {
var choice = {};
var l = link[1].split("|");
var linkText = l[0];
if (l.length == 1) {
//passage and link text are the same
var passage = linkText.replace(/^\s+|\s+$/g, "");
choices.push({ linkText: linkText, passage: passage });
txt = txt.replace(link[0], "");
}
else if (l.length == 2) {
//passage and link text are not the same
//whitespaces
var passage = l[1].replace(/^\s+|\s+$/g, "");
choices.push({ linkText: linkText, passage: passage });
txt = txt.replace(link[0], "");
}
else {
print("Error: Link malformed " + link[0]);
failSafe = 1000;
}
}
failSafe++;
if (failSafe > 100) {
print("Error: infinite parsing loop at " + link);
}
//keep replacing until there are no matches
link = txt.match(/\[\[([\s\S]*?)\]\]/);
}
return choices;
}
function parseText(txt) {
//1-
//parse the "links" between rooms, it's a twine-like syntax that gets converted into a link that calls a changePassage function
var link = txt.match(/\[\[([\s\S]*?)\]\]/);
var failSafe = 0;
while (link != null && failSafe < 1000) {
//found link
if (link != null) {
var l = link[1].split("|");
var linkText = l[0];
if (l.length == 1) {
//passage and link text are the same
var passage = linkText.replace(/^\s+|\s+$/g, "");
var htmlLink = "<a class='passageLink' href='#' onclick='displayPassage(\"" + passage + "\"); return false; '>" + linkText + "</a>";
txt = txt.replace(link[0], htmlLink);
}
else if (l.length == 2) {
//passage and link text are not the same
//whitespaces
var passage = l[1].replace(/^\s+|\s+$/g, "");
var htmlLink = "<a class='passageLink' href='#' onclick='displayPassage(\"" + passage + "\"); return false; '>" + linkText + "</a>";
txt = txt.replace(link[0], htmlLink);
}
else {
print("Error: Link malformed " + link[0]);
failSafe = 1000;
}
}
failSafe++;
if (failSafe > 100) {
print("Error: infinite parsing loop at " + link);
}
//keep replacing until there are no matches
link = txt.match(/\[\[([\s\S]*?)\]\]/);
}
//get rid of video macro
var macro = txt.match(/{([\s\S]*?)}/);
if (macro != null) {
//for legibility macros may be typed with a linebreaks so get rid of the ones immediately preceding and following a macro
var ind = txt.indexOf(macro[0]);
if (ind > 0) {
var c = txt.charAt(ind - 1);
if (c == "\n" || c == "\r") {
txt = txt.slice(0, ind - 1) + txt.slice(ind);
}
}
txt = txt.replace(macro[0], "");
}
//restore the innerlink variable markers
txt = txt.replace(/@@/g, "$");
//print(txt);
//fix the punctuation
txt = txt.replace(/(\.{3})/g, "…");
txt = txt.replace(/,(?=[^\s])/g, ", ");
txt = txt.replace(/\.(?=[^\s])/g, ". ");
txt = txt.replace(/\s\./g, ".");
txt = txt.replace(/\s,/g, ",");
txt = txt.replace(/;(?=[^\s])/g, "; ");
txt = txt.replace(/:(?=[^\s])/g, ": ");
//add line breaks
//txt = txt.replace(/\n\n/g, "");
txt = txt.replace(/\n/g, "<br/>");
return "<div class='passageBody'>" + txt + "</div>";
}
function print(m) {
console.log(m);
}
function map(value, low1, high1, low2, high2) {
return low2 + (high2 - low2) * (value - low1) / (high1 - low1);
}
function resizeContainer() {
if (currentVideo != null) {
var scale = 1;
//landscape
if (window.innerWidth > window.innerHeight) {
scale = window.innerHeight / currentVideo.videoHeight;
//except when width is too much
if ((currentVideo.videoWidth * scale) > window.innerWidth) {
scale = window.innerWidth / currentVideo.videoWidth;
}
container.setAttribute("style", "width:" + currentVideo.videoWidth * scale + "px; height: " + currentVideo.videoHeight * scale + "px");
}
else {
scale = window.innerWidth / currentVideo.videoWidth;
//except when height is too much
if ((currentVideo.videoHeight * scale) > window.innerHeight) {
scale = window.innerHeight / currentVideo.videoHeight;
}
container.setAttribute("style", "width:" + currentVideo.videoWidth * scale + "px; height: " + currentVideo.videoHeight * scale + "px");
}
}
}
window.addEventListener('resize', resizeContainer);
//check the fist click (browser restriction on autoplay)
$(document).on('click touch', function () {
if (!interacted) {
interacted = true;
if (currentPassage != null)
if (currentVideo != null)
currentVideo.play();
}
});
function revealMenu() {
if (VIDEO_CONTROLS && currentVideo != null) {
if (!choicesOn)
$("#menuContainer").css("display", "block");
if (mouseMoveInterval != null)
clearTimeout(mouseMoveInterval);
mouseMoveInterval = setTimeout(function () {
//disappear only if video is playing
if (currentVideo != null)
if (!currentVideo.paused) {
$("#menuContainer").fadeOut();
}
}, FADE_TIME);
}
}
//mouse hide not active, too tricky
function showMouse() {
$("#container").css("cursor", "auto");
clearTimeout(mouseHideInterval);
mouseHideInterval = setTimeout(function () {
$("#container").css("cursor", "none");
}, FADE_TIME);
}
//reveal the menu if applicable
$(document).mousemove(function() {
//showMouse();
revealMenu();
});