-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcutscene.js
More file actions
255 lines (199 loc) · 6.64 KB
/
Copy pathcutscene.js
File metadata and controls
255 lines (199 loc) · 6.64 KB
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
(function () {
"use strict";
// section for the cutscene replay
var VIDEO_URL = "https://user-cdn.hackclub-assets.com/01a08b30-7fec-707c-941c-4fae1a0282e2/entityevent.mp4";
var FADE_MS = 400;
var RETRY_MS = 4000;
var BAIL_MS = 20000;
var scrim = null;
var video = null;
var closeBtn = null;
var waiting = null;
var retryTimer = 0;
var active = false;
var inertSnapshot = null;
var scrollLock = "";
var returnFocus = null;
var bailTimer = 0;
var paused = [];
function build() {
if (scrim) return;
scrim = document.createElement("div");
scrim.className = "replayScrim";
scrim.id = "replayScrim";
scrim.setAttribute("role", "dialog");
scrim.setAttribute("aria-modal", "true");
scrim.setAttribute("aria-label", "Cutscene");
scrim.hidden = true;
video = document.createElement("video");
video.className = "replayVideo";
video.playsInline = true;
video.preload = "none";
video.setAttribute("disableremoteplayback", "");
var source = document.createElement("source");
source.src = VIDEO_URL;
source.type = "video/mp4";
video.appendChild(source);
waiting = document.createElement("div");
waiting.className = "replayWaiting";
waiting.textContent = "LOADING";
waiting.hidden = true;
closeBtn = document.createElement("button");
closeBtn.className = "replayClose";
closeBtn.type = "button";
closeBtn.setAttribute("aria-label", "Close the cutscene");
closeBtn.textContent = "SKIP";
scrim.appendChild(video);
scrim.appendChild(waiting);
scrim.appendChild(closeBtn);
document.body.appendChild(scrim);
closeBtn.addEventListener("click", function () { close("skip"); });
video.addEventListener("ended", function () { close("ended"); });
video.addEventListener("error", function () { close("error"); });
video.addEventListener("progress", armBail);
video.addEventListener("loadedmetadata", armBail);
video.addEventListener("playing", function () {
clearTimeout(bailTimer);
clearTimeout(retryTimer);
waiting.hidden = true;
});
}
// the clip is fetched cold, so the bail only fires once the download has genuinely stalled
function armBail() {
if (!isOpen()) return;
clearTimeout(bailTimer);
bailTimer = setTimeout(function () {
if (video.readyState < 2) close("error");
}, BAIL_MS);
}
function kick() {
if (!isOpen() || video.readyState >= 2) return;
if (video.networkState === HTMLMediaElement.NETWORK_EMPTY) video.load();
var again = video.play();
if (again && again.catch) again.catch(function () {});
retryTimer = setTimeout(kick, RETRY_MS);
}
function isOpen() {
return active;
}
// nothing else decodes while the cutscene is on screen
function hushClips() {
paused = [];
var clips = document.querySelectorAll("video");
for (var i = 0; i < clips.length; i++) {
if (clips[i] === video || clips[i].paused) continue;
clips[i].pause();
paused.push(clips[i]);
}
}
function wakeClips() {
for (var i = 0; i < paused.length; i++) {
var playing = paused[i].play();
if (playing && playing.catch) playing.catch(function () {});
}
paused = [];
}
function setInertBehind(on) {
var kids = Array.prototype.slice.call(document.body.children);
var i;
if (on) {
inertSnapshot = kids.map(function (child) { return child.inert; });
for (i = 0; i < kids.length; i++) {
if (kids[i] !== scrim && kids[i].tagName !== "SCRIPT") kids[i].inert = true;
}
return;
}
if (!inertSnapshot) return;
for (i = 0; i < kids.length; i++) {
if (kids[i] !== scrim && kids[i].tagName !== "SCRIPT" && i < inertSnapshot.length) {
kids[i].inert = inertSnapshot[i];
}
}
inertSnapshot = null;
}
function play() {
build();
if (active) return;
active = true;
returnFocus = document.activeElement;
scrim.hidden = false;
void scrim.offsetHeight;
var raise = function () { scrim.classList.add("show"); };
requestAnimationFrame(raise);
setTimeout(raise, 60);
scrollLock = document.body.style.overflow;
document.body.style.overflow = "hidden";
setInertBehind(true);
hushClips();
video.preload = "auto";
video.muted = false;
video.volume = 1;
if (video.readyState === 0) video.load();
if (video.currentTime > 0.05) video.currentTime = 0;
waiting.hidden = video.readyState >= 2;
var started = video.play();
if (started && started.catch) started.catch(function () {});
closeBtn.focus({ preventScroll: true });
armBail();
retryTimer = setTimeout(kick, RETRY_MS);
}
function close(reason) {
if (!active) return;
active = false;
clearTimeout(bailTimer);
clearTimeout(retryTimer);
waiting.hidden = true;
video.pause();
scrim.classList.remove("show");
setInertBehind(false);
document.body.style.overflow = scrollLock;
wakeClips();
setTimeout(function () {
if (!active) scrim.hidden = true;
}, FADE_MS);
if (returnFocus && document.contains(returnFocus)) returnFocus.focus();
returnFocus = null;
if (reason !== "error") {
document.dispatchEvent(new CustomEvent("zoneout:cutsceneend", { detail: { reason: reason || "skip" } }));
}
}
// the cutscene owns the keyboard while it is up
document.addEventListener("keydown", function (e) {
if (!isOpen()) return;
if (e.key === "Escape" || e.key === "Enter" || e.key === " ") {
e.preventDefault();
close("skip");
}
e.stopPropagation();
}, true);
document.addEventListener("visibilitychange", function () {
if (!isOpen()) return;
if (document.visibilityState === "visible") {
var playing = video.play();
if (playing && playing.catch) playing.catch(function () {});
} else {
video.pause();
}
});
// section for the settings row
function mountRow() {
var api = window.zoneoutSettings;
if (!api || typeof api.addRow !== "function") return;
var button = document.createElement("button");
button.className = "camNode settingsAction";
button.type = "button";
button.id = "replayCutsceneBtn";
button.textContent = "PLAY CUTSCENE";
button.addEventListener("click", function () {
api.close();
setTimeout(play, 260);
});
var wrap = document.createElement("div");
wrap.className = "settingsSlot";
wrap.appendChild(button);
api.addRow("Cutscene", wrap);
}
if (window.zoneoutSettings && window.zoneoutSettings.ready) mountRow();
else document.addEventListener("zoneout:settingsready", mountRow, { once: true });
window.zoneoutCutscene = { play: play, close: close, isOpen: isOpen };
})();