-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdocument.ts
More file actions
328 lines (283 loc) · 7.9 KB
/
Copy pathdocument.ts
File metadata and controls
328 lines (283 loc) · 7.9 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
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
import { getLogger } from '@node-3d/addon-tools';
import { emptyFunction, ESC_KEY, F_KEY } from './constants.ts';
import { FakeImage } from './fake-image.ts';
import { glfw } from './core.ts';
import { Window } from './legacy-window.ts';
import type { TCbVoid, TSize, TWebgl } from './types.ts';
const logger = getLogger('glfw');
export type TDocumentOpts = ConstructorParameters<typeof Window>[0] &
Readonly<
Partial<{
/**
* Whether the window should ignore default quit signals.
*
* Examples: process `SIGINT`, document `quit`, and ESC press if `autoEsc` is enabled.
*/
ignoreQuit: boolean;
/**
* Whether the window has default fullscreen key handlers.
*
* * CTRL+F - borderless fullscreen window.
* * CTRL+ALT+F - real, exclusive fullscreen mode.
* * CTRL+SHIFT+F - back to windowed.
*/
autoFullscreen: boolean;
/**
* Handle ESC key to close the window automatically.
*
* Does nothing if `ignoreQuit` is enabled.
*/
autoEsc: boolean;
}>
>;
type TImageConstructor = new () => {
src?: string;
complete?: boolean;
on?: (...args: unknown[]) => void;
onload?: (...args: unknown[]) => void;
onerror?: (...args: unknown[]) => void;
};
type TMutableWebgl = TWebgl & {
canvas?: Document;
init?: TCbVoid;
data?: unknown;
};
type TCanvasStub = Readonly<{
width: number;
height: number;
getContext: (kind: string) => TMutableWebgl | InstanceType<typeof Document.Image> | null;
readonly data: unknown;
onkeydown: TCbVoid;
onkeyup: TCbVoid;
onmousedown: TCbVoid;
onmouseup: TCbVoid;
onwheel: TCbVoid;
onmousewheel: TCbVoid;
onresize: TCbVoid;
dispatchEvent: TCbVoid;
addEventListener: TCbVoid;
removeEventListener: TCbVoid;
}>;
/**
* Web-like Document.
*
* Document extends Window to provide an additional web-style compatibility
* layer. It mimics the behavior and properties of a browser `window.document`,
* while still being a Window. It is intentionally incomplete: callers still
* provide the Image class and WebGL implementation they want to use.
*/
export class Document extends Window {
public static Image: TImageConstructor;
public static webgl: TMutableWebgl | null;
public static isWebglInited: boolean;
private _isCanvasRequested: boolean;
/**
* Set Image implementation.
*
* Also sets `global.HTMLImageElement`.
*/
public static setImage(Image: TImageConstructor): void {
this.Image = Image;
global.HTMLImageElement = Image as typeof global.HTMLImageElement;
}
/**
* Set WebGL implementation.
*/
public static setWebgl(webgl: TMutableWebgl | null): void {
this.webgl = webgl;
this.isWebglInited = false;
}
public constructor(opts: TDocumentOpts = {}) {
super(opts);
this._isCanvasRequested = false;
if (Document.webgl && !Document.isWebglInited) {
try {
if (typeof Document.webgl.init === 'function') {
Document.webgl.init();
}
} catch {
logger.warn('WebGL `init()` call failed, but it may still work.');
}
Document.isWebglInited = true;
}
if (Document.webgl) {
Document.webgl.canvas = this;
}
this.on('mousedown', (e) => {
this.emit('pointerdown', e);
});
this.on('mouseup', (e) => {
this.emit('pointerup', e);
});
this.on('mousemove', (e) => {
this.emit('pointermove', e);
});
if (!opts.ignoreQuit) {
const isUnix = process.platform !== 'win32';
if (isUnix && !process.listeners('SIGINT').includes(Document.exit)) {
process.on('SIGINT', Document.exit);
}
this.on('quit', () => Window.exit());
if (opts.autoEsc) {
this.on('keydown', (e) => e.keyCode === ESC_KEY && Window.exit());
}
}
if (opts.autoFullscreen) {
this.on('keydown', (e) => {
if (e.keyCode === F_KEY && e.ctrlKey && e.shiftKey) {
this.mode = 'windowed';
} else if (e.keyCode === F_KEY && e.ctrlKey && e.altKey) {
this.mode = 'fullscreen';
} else if (e.keyCode === F_KEY && e.ctrlKey) {
this.mode = 'borderless';
}
});
}
}
/** Set `glfw.CURSOR` mode to `glfw.CURSOR_DISABLED`. */
public setPointerCapture = (): void => {
this.setInputMode(glfw.CURSOR, glfw.CURSOR_DISABLED);
};
/** Set `glfw.CURSOR` mode to `glfw.CURSOR_NORMAL`. */
public releasePointerCapture = (): void => {
this.setInputMode(glfw.CURSOR, glfw.CURSOR_NORMAL);
};
public makeCurrent(): void {
if (Document.webgl) {
Document.webgl.canvas = this;
}
super.makeCurrent();
}
/** Returns `this`. */
public get body(): Document {
return this;
}
/**
* Mimics the web element `style` property.
*
* Only `width` and `height` matter.
*/
public get style(): TSize {
const getWidth = (): number => this.innerWidth;
const setWidth = (value: string): void => {
this.width = Number.parseInt(value, 10) * this.devicePixelRatio;
};
const getHeight = (): number => this.innerHeight;
const setHeight = (value: string): void => {
this.height = Number.parseInt(value, 10) * this.devicePixelRatio;
};
return {
get width(): number {
return getWidth();
},
set width(value: string) {
setWidth(value);
},
get height(): number {
return getHeight();
},
set height(value: string) {
setHeight(value);
},
};
}
/** Returns `Document.webgl`, set through `Document.setWebgl`. */
public get context(): TWebgl | null {
return Document.webgl;
}
/** Returns `Document.webgl`, set through `Document.setWebgl`. */
public getContext(kind: string): TMutableWebgl | InstanceType<typeof Document.Image> | null {
return kind === '2d' ? new Document.Image() : Document.webgl;
}
/** Returns `this`. */
public getRootNode(): Document {
return this;
}
/** Returns `this`. */
public getElementById(): Document {
return this;
}
/** Returns `this`. */
public querySelector(): Document {
return this;
}
/** Returns an array containing `this`. */
public querySelectorAll(): readonly Document[] {
return [this];
}
/** Returns an array containing `this`. */
public getElementsByTagName(): readonly Document[] {
return [this];
}
/** Returns whether `node` is this document. */
public contains(node: unknown): boolean {
return node === this;
}
/** Does nothing. */
public appendChild(): void {
/* nop */
}
/** Does nothing. */
public append(): void {
/* nop */
}
/** Returns the result of `createElement(name)`. */
public createElementNS(
_0: unknown,
name: string,
): Document | InstanceType<typeof Document.Image> | TCanvasStub | null {
return this.createElement(name);
}
/**
* Fake `createElement`.
*
* For `canvas`, returns `this` on the first call, then returns canvas-like
* objects capable of using 2D or 3D context. This supports web APIs like
* three.js, which create additional canvases.
*
* For `img`, returns `new Document.Image()`.
*/
public createElement(
nameRaw: string,
): Document | InstanceType<typeof Document.Image> | TCanvasStub | null {
const name = nameRaw.toLowerCase();
if (name.includes('img')) {
return new Document.Image();
}
if (name.includes('canvas')) {
if (!this._isCanvasRequested) {
this._isCanvasRequested = true;
return this;
}
const getContext = (
kind: string,
): TMutableWebgl | InstanceType<typeof Document.Image> | null => this.getContext(kind);
let ctx: TMutableWebgl | InstanceType<typeof Document.Image> | null = null;
return {
width: this.width,
height: this.height,
getContext(kind) {
ctx = getContext(kind);
return ctx;
},
get data() {
return ctx && 'data' in ctx ? ctx.data : undefined;
},
onkeydown: emptyFunction,
onkeyup: emptyFunction,
onmousedown: emptyFunction,
onmouseup: emptyFunction,
onwheel: emptyFunction,
onmousewheel: emptyFunction,
onresize: emptyFunction,
dispatchEvent: emptyFunction,
addEventListener: emptyFunction,
removeEventListener: emptyFunction,
};
}
return null;
}
}
global.HTMLCanvasElement = Document as unknown as typeof global.HTMLCanvasElement;
Document.setImage(FakeImage);
Document.setWebgl(null);