forked from ShinyColorsDB/ShinyColorsDB-EventViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
393 lines (336 loc) · 11.8 KB
/
main.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
'use strict';
function getQueryVariable(name, defRet = null) {
const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
const result = window.location.search.substring(1).match(reg);
if (result != null) {
return decodeURI(result[2]);
} else {
return defRet;
}
}
const EventHelper = {
createApp : function(){
if (document.getElementById("ShinyColors")) {
document.getElementById("ShinyColors").remove();
}
PIXI.utils.skipHello();
const app = new PIXI.Application({
width: 1136,
height: 640,
});
app.view.setAttribute("id", "ShinyColors");
document.body.appendChild(app.view);
let resize = () => {
let height = document.documentElement.clientHeight;
let width = document.documentElement.clientWidth;
let ratio = Math.min(width / 1136, height / 640);
let resizedX = 1136 * ratio;
let resizedY = 640 * ratio;
app.view.style.width = resizedX + 'px';
app.view.style.height = resizedY + 'px';
}
window.onresize = () => resize();
return app;
},
getTrack : async function(source){
return await fetch(`${assetUrl}/json/${source}`)
.then(res => res.json())
.catch(e => console.error(e));
},
getTranslateByLabel : async function(label){
label = label.includes('.json') ? label : `${label}.json`;
let masterlist = await fetch(translate_master_list)
.then(res => res.json())
.catch(e => console.error(e));
let result = masterlist.find(([key, _]) => key === label);
if (!result) {
return void 0;
}
let url = translate_CSV_url.replace('{uid}', result[1]);
return this.getTranslateByUrl(url);
},
getTranslateByUrl : async function(url){
let csvtext = await fetch(url)
.then(res => res.text())
.catch(e => console.error(e));
if(csvtext === ''){
return void 0;
}
return this.getTranslateByCSVText(csvtext);
},
getTranslateByCSVText : async function(text){
const dataJSON = {
translator: '',
info : '',
table: [],
};
const table = text.split(/\r\n/).slice(1);
table.forEach(row => {
let columns = row.split(',');
if (columns[0] === 'info') {
dataJSON['info'] = columns[1];
}
else if (columns[0] === '译者') {
dataJSON['translator'] = columns[1];
}
else if (columns[0] != '') {
dataJSON['table'].push({
id: columns[0],
name: columns[1],
text: columns[2].replace('\\n', '\r\n'),
trans: columns[3].replace('\\n', '\r\n'),
});
}
});
return dataJSON;
}
}
class EventPlayer {
_interestedEvents = ["click", "touchstart"];
_Menu = {
touchToStart: null,
autoBtn: null,
switchLangBtn: null,
};
_autoBtn_texture = {
autoOn: null,
autoOff: null,
};
_switchLangBtn_texture = [];
_isTranslate = false;
_tm = void 0;
_app = void 0;
constructor(pixiapp){
this._app = pixiapp;
this._tm = new TrackManager(this._app);
this._tm.addToStage();
this.helloBanner();
}
set isTranslate(boolean) {
this._isTranslate = boolean;
}
reset() {
this._tm.endOfEvent(false);
}
loadTrackScript(Track){
if (!this._app || !this._tm) {
return;
}
this._tm.setTrack = Track;
}
loadranslateScript(translateJson){
if (!this._app || !this._tm) {
return;
}
this._isTranslate = true;
this._tm.setTranslateJson = translateJson;
}
async LoadFont(FontName) {
const font = new FontFaceObserver(FontName);
return await font.load(null, fontTimeout);
}
async start() {
this._app.loader
.add("touchToStart", "./assets/touchToStart.png")
.add("autoOn", "./assets/autoOn.png")
.add("autoOff", "./assets/autoOff.png")
.add("jpON", "./assets/jpOn.png")
.add("zhJPOn", "./assets/zhJPOn.png")
.add("zhOn", "./assets/zhOn.png")
.load((_, resources) => this._ready(resources));
}
_ready = (resources) => {
this._Menu.touchToStart = new PIXI.Sprite(resources.touchToStart.texture);
this._Menu.autoBtn = new PIXI.Sprite(resources.autoOn.texture);
this._autoBtn_texture.autoOn = resources.autoOn.texture;
this._autoBtn_texture.autoOff = resources.autoOff.texture;
if (this._isTranslate) {
this._Menu.switchLangBtn = new PIXI.Sprite(resources.jpON.texture);
this._switchLangBtn_texture = [
resources.jpON.texture,
resources.zhOn.texture,
resources.zhJPOn.texture
];
}
// this._app.stage.interactive = true;
let touchToStart = this._Menu.touchToStart;
touchToStart.anchor.set(0.5);
touchToStart.position.set(568, 500);
this._app.stage.addChild(touchToStart);
this._interestedEvents.forEach(e => {
this._app.view.addEventListener(e, this._afterTouch);
});
};
fastForward(forwardJson) {
this._tm.fastForward = forwardJson.forward;
if (forwardJson.forward) {
this._tm.stopTrack = forwardJson.target;
}
else {
this._tm.stopTrack = -1;
}
this._removeTouchToStart();
this._tm.loadAssetsByTrack();
}
_removeTouchToStart() {
this._app.stage.interactive = false;
this._app.stage.removeChild(this._Menu.touchToStart);
this._interestedEvents.forEach(e => {
this._app.view.removeEventListener(e, this._afterTouch);
});
this._interestedEvents.forEach(e => {
this._app.stage.on(e, this._nextTrack);
});
}
_afterTouch = async () => {
let { autoBtn, switchLangBtn } = this._Menu;
this._removeTouchToStart();
const progressText = new PIXI.Text('0 %', {
fill: "#FFF",
fontSize : 50,
});
progressText.anchor.set(1);
progressText.position.set(1136-30, 640-30);
this._app.stage.addChild(progressText);
let loadProgressHandler = (loader, resource) => {
progressText.text = `${Math.floor(loader.progress)} %`;
if(loader.progress === 100){
this._app.stage.removeChild(progressText);
}
}
this._tm.loadAssetsByTrack(loadProgressHandler);
//auto Btn
autoBtn.anchor.set(0.5);
autoBtn.position.set(1075, 50);
autoBtn.interactive = true;
this._app.stage.addChild(autoBtn);
this._interestedEvents.forEach(e => { // autoplay is initialized to false
autoBtn.on(e, () => {
this._tm.toggleAutoplay();
this._toggleAutoplay();
});
});
//Trans
if (this._isTranslate) {
switchLangBtn.anchor.set(0.5);
switchLangBtn.position.set(1075, 130);
switchLangBtn.interactive = true;
this._app.stage.addChild(switchLangBtn);
this._interestedEvents.forEach(e => { // autoplay is initialized to false
switchLangBtn.on(e, () => {
this._tm.toggleLangDisplay();
this._toggleLangDisplay();
});
});
}
};
_toggleAutoplay() {
let { autoBtn } = this._Menu;
let { autoOn, autoOff } = this._autoBtn_texture;
if (this._tm.autoplay) { // toggle on
// console.log(this._tm._trackPromise)
this._tm._trackPromise?.then((bool) => {
if (bool) {
this._tm._renderTrack();
}
})
autoBtn.texture = autoOn;
this._app.stage.interactive = false;
}
else { // toggle off
autoBtn.texture = autoOff;
this._app.stage.interactive = true;
}
}
_toggleLangDisplay() {
let { switchLangBtn } = this._Menu;
let next = this._tm._translateLang;
switchLangBtn.texture = this._switchLangBtn_texture[next];
}
_nextTrack = (ev) => {
if (ev.target !== this._app.stage) { return; }
if (this._tm.autoplay) { return; }
// if (this._tm._animationPromise) { return; } //正在動畫中
if (this._tm._textTypingEffect) {
clearInterval(this._tm._textTypingEffect);
}
this._tm._renderTrack();
};
helloBanner() {
const log = [
`\n\n %c %c ShinyColors Event Viewer %c %c https://github.com/ShinyColorsDB/ShinyColorsDB-EventViewer %c \n\n`,
'background: #28de10; padding:5px 0;',
'color: #28de10; background: #030307; padding:5px 0;',
'background: #28de10; padding:5px 0;',
'background: #5eff84; padding:5px 0;',
'background: #28de10; padding:5px 0;',
];
console.log(...log);
}
}
//------------Main-----------------
async function init(){
// get props from url
let eventId = getQueryVariable("eventId");
let eventType = getQueryVariable("eventType", "produce_events");
let isIframeMode = getQueryVariable("iframeMode", null) === "1";
let isTranslate = getQueryVariable("isTranslate", null) === '1';
//
let jsonPath;
//create PIXI app and event player
const app = EventHelper.createApp();
const advPlayer = new EventPlayer(app);
await advPlayer.LoadFont(usedFont); //load JP Font
await advPlayer.LoadFont(zhcnFont); //load ZH_cn Font (for Translate)
// Iframe Handler
async function eventHandler(e) {
if (!e.data.messageType || !e.origin) {
console.log("Invalid message");
return;
}
switch (e.data.messageType) {
case "iframeJson":
console.log("Received iframeJson");
advPlayer.loadTrackScript(e.data.iframeJson);
if (e.data.csvText) {
const translateJson = EventHelper.getTranslateByCSVText(e.data.csvText);
if (translateJson) {
advPlayer.loadranslateScript(translateJson);
}
}
advPlayer.start();
break;
case "fastForward":
console.log("Received fastForward");
advPlayer.reset();
advPlayer.fastForward(e.data.fastForward);
break;
}
}
// Iframe Mode
if (isIframeMode) {
window.addEventListener('message', eventHandler, false);
window.parent.postMessage({
eventViewerIframeLoaded: true
}, "*");
}
else {
//load Event Track Script and [Optional]Translate Script
if (eventId) {
jsonPath = `${eventType}/${eventId}.json`;
}
else {
jsonPath = prompt("input json path: ", "produce_events/202100711.json");
eventId = jsonPath.split("/")[1].split(".")[0];
eventType = jsonPath.split("/")[0];
window.location.search = `eventType=${eventType}&eventId=${eventId}`;
}
const trackScript = await EventHelper.getTrack(jsonPath);
advPlayer.loadTrackScript(trackScript);
if (isTranslate) {
const translateJson = await EventHelper.getTranslateByLabel(jsonPath);
advPlayer.loadranslateScript(translateJson);
}
advPlayer.start();
}
}