forked from cocos/cocos-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.js
153 lines (135 loc) · 5.5 KB
/
shell.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
'use strict';
(() => {
function loadPromise(url) {
return new Promise((resolve, reject) => {
const xhr = new window.XMLHttpRequest();
xhr.open('GET', url + '?_=' + new Date().getTime(), true); // force no cache
xhr.onreadystatechange = onreadystatechange;
xhr.send(null);
function onreadystatechange(e) {
if (xhr.readyState !== 4) {
return;
}
// Testing harness file:/// results in 0.
if ([0, 200, 304].indexOf(xhr.status) === -1) {
reject(`While loading from url ${url} server responded with a status of ${xhr.status}`);
} else {
resolve(e.target.response);
}
}
});
}
function _load(url) {
loadPromise(url).then(result => {
if (window.dgui) {
window.dgui.destroy();
window.dgui = null;
}
cc.director.off(cc.Director.EVENT_BEFORE_UPDATE);
// cc.director.off(cc.Director.EVENT_AFTER_UPDATE); // should uncomment these after UI manager move to global
// cc.director.off(cc.Director.EVENT_BEFORE_DRAW);
// cc.director.off(cc.Director.EVENT_AFTER_DRAW);
cc.systemEvent.off(cc.SystemEvent.EventType.MOUSE_WHEEL);
cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN);
cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP);
cc.systemEvent.off(cc.SystemEvent.EventType.TOUCH_START);
cc.systemEvent.off(cc.SystemEvent.EventType.TOUCH_MOVE);
cc.systemEvent.off(cc.SystemEvent.EventType.TOUCH_END);
// init dgui
if (window.dat) {
const dgui = new dat.GUI({ width: 270 });
dgui.domElement.classList.add('dgui');
window.dgui = dgui;
}
const getCurAbsPath = () => {
const scripts = document.getElementsByTagName('script');
const lastScriptPath = scripts[scripts.length -1].src;
const segs = lastScriptPath.split('/');
if (segs.length >= 1) {
segs.pop();
}
const path = segs.join('/') + '/';
return path;
};
eval(`(() => {\n${result}\n})();\n\n//# sourceURL=${getCurAbsPath() + url}`);
window.cc.director.on(cc.Director.EVENT_BEFORE_UPDATE, () => {
window.stats.update();
});
}).catch(err => {
console.error(err);
});
}
document.addEventListener('readystatechange', () => {
if (document.readyState !== 'complete') return;
const view = document.getElementById('view');
const showFPS = document.getElementById('showFPS');
const enableSpector = document.getElementById('spector');
const enableVConsole = document.getElementById('vconsole');
const exampleList = document.getElementById('exampleList');
// update profile
showFPS.checked = localStorage.getItem('engine.showFPS') === 'true';
enableSpector.checked = localStorage.getItem('engine.enableSpector') === 'true';
enableVConsole.checked = localStorage.getItem('engine.enableVConsole') === 'true';
let exampleIndex = parseInt(localStorage.getItem('engine.exampleIndex'));
if (isNaN(exampleIndex) || exampleIndex >= exampleList.childElementCount) exampleIndex = 0;
exampleList.selectedIndex = exampleIndex;
// init stats
if (window.Stats) {
const stats = new window.Stats();
stats.dom.style.cssText = 'position:fixed;top:0;right:0;cursor:pointer;opacity:0.9;z-index:10000';
stats.dom.style.display = showFPS.checked ? 'block' : 'none';
document.body.appendChild(stats.dom);
window.stats = stats;
showFPS.addEventListener('click', event => {
localStorage.setItem('engine.showFPS', event.target.checked);
stats.dom.style.display = event.target.checked ? 'block' : 'none';
});
}
// init spector
const url = '../node_modules/spectorjs/dist/spector.bundle.js';
loadPromise(url).then(result => {
eval(`${result}\n//# sourceURL=${url}`);
if (enableSpector.checked) {
window.spector = new window.SPECTOR.Spector();
window.spector.displayUI();
}
enableSpector.addEventListener('click', event => {
// localStorage.setItem('engine.enableSpector', event.target.checked);
if (enableSpector.checked) {
window.spector = new window.SPECTOR.Spector();
window.spector.displayUI();
}
});
});
// init vconsole
if (window.VConsole) {
if (enableVConsole.checked) window.vconsole = new window.VConsole();
enableVConsole.addEventListener('click', event => {
// localStorage.setItem('engine.enableVConsole', event.target.checked);
if (enableVConsole.checked) window.vconsole = new window.VConsole();
});
}
// create canvas
const bcr = view.getBoundingClientRect();
const canvas = document.createElement('canvas');
canvas.classList.add('fit');
canvas.tabIndex = -1;
canvas.id = 'canvas';
canvas.width = bcr.width;
canvas.height = bcr.height;
view.appendChild(canvas);
// init engine
window.cc.game.init({ id: canvas.id });
window.cc.game.run(() => {
_load(exampleList.value);
window.addEventListener('resize', () => {
const bcr = view.getBoundingClientRect();
cc.director.root.resize(bcr.width, bcr.height);
});
exampleList.addEventListener('change', event => {
localStorage.setItem('engine.exampleIndex', event.target.selectedIndex);
_load(exampleList.value);
});
});
});
})();