-
Notifications
You must be signed in to change notification settings - Fork 3
/
Stage.ts
391 lines (355 loc) · 11.8 KB
/
Stage.ts
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
import {Layer} from './Layer';
import {Ticker} from '../system/Ticker';
import {Vector} from '../geom/Vector';
import {Rectangle} from '../geom/Rectangle';
import {Event} from '../event/Event';
import {TouchEvent} from '../event/TouchEvent';
import {ResourceManager, ResourceInfo, ResourceManagerOption} from '../net/ResourceManager';
export class Stage extends Layer {
public static readonly SHOW_ALL: string = 'showAll';
public static readonly EXACT_FIT: string = 'exactFit';
public static readonly NO_SCALE: string = 'noScale';
public static readonly NO_BORDER: string = 'noBorder';
public static readonly FIXED_WIDE: string = 'fixedWide';
public static readonly FIXED_NARROW: string = 'fixedNarrow';
public static readonly FIXED_WIDTH: string = 'fixedWidth';
public static readonly FIXED_HEIGHT: string = 'fixedHeight';
protected $drawCalls: number = 0;
protected $activated: boolean = false;
protected $scaleMode: string = Stage.SHOW_ALL;
protected $viewportWidth: number = 0;
protected $viewportHeight: number = 0;
protected $viewportBackground: string = null;
protected readonly $ticker: Ticker;
protected readonly $elementEvents: ElementEvent[];
protected readonly $viewportCanvas: HTMLCanvasElement;
protected readonly $viewportContext: CanvasRenderingContext2D;
protected readonly $renderBounds: Rectangle = Rectangle.create();
protected readonly $boundResizeViewportCanvas: () => void;
public constructor(canvas?: HTMLCanvasElement) {
super();
this.$ticker = new Ticker(this);
this.$elementEvents = [];
this.$viewportCanvas = canvas || document.createElement('canvas');
this.$viewportContext = this.$viewportCanvas.getContext('2d');
this.$boundResizeViewportCanvas = this.$resizeViewportCanvas.bind(this);
this.$resizeViewportCanvas();
this.$initEvents();
this.width = 320;
this.height = 568;
if (!canvas) {
this.$viewportCanvas.style.top = '0';
this.$viewportCanvas.style.left = '0';
this.$viewportCanvas.style.position = 'fixed';
document.body.appendChild(this.$viewportCanvas);
}
}
public get x(): number {
return 0;
}
public set x(x: number) {
this.$x = 0;
}
public get y(): number {
return 0;
}
public set y(y: number) {
this.$y = 0;
}
public get ticker(): Ticker {
return this.$ticker;
}
public get fps(): number {
return this.$ticker.fps;
}
public get drawCalls(): number {
return this.$drawCalls;
}
public get activated(): boolean {
return this.$activated;
}
public get scaleMode(): string {
return this.$scaleMode;
}
public set scaleMode(scaleMode: string) {
if (this.scaleMode !== scaleMode) {
this.$scaleMode = scaleMode;
this.$resizeCanvas();
}
}
public get viewportCanvas(): HTMLCanvasElement {
return this.$viewportCanvas;
}
public get viewportWidth(): number {
return this.$viewportWidth ? this.$viewportWidth : this.$viewportCanvas.width / Layer.pixelRatio;
}
public set viewportWidth(width: number) {
if (this.$viewportWidth !== width) {
this.$viewportWidth = width;
this.$resizeViewportCanvas();
}
}
public get viewportHeight(): number {
return this.$viewportHeight ? this.$viewportHeight : this.$viewportCanvas.height / Layer.pixelRatio;
}
public set viewportHeight(height: number) {
if (this.$viewportHeight !== height) {
this.$viewportHeight = height;
this.$resizeViewportCanvas();
}
}
public get viewportBackground(): string {
return this.$viewportBackground;
}
public set viewportBackground(viewportBackground: string) {
this.$viewportBackground = viewportBackground;
this.$viewportCanvas.style.background = viewportBackground;
}
public createResourceManager(list: Array<ResourceInfo>, options?: ResourceManagerOption): ResourceManager {
return new ResourceManager(this, list, options);
}
public removeSelf(): this {
let canvas = this.$viewportCanvas;
if (canvas.parentElement) {
canvas.parentElement.removeChild(canvas);
}
if (this.$activated) {
this.$activated = false;
this.emit(Event.DEACTIVATE);
}
if (this.$stage) {
this.$emitRemovedFromStage();
}
this.$removeElementEvents();
return this;
}
protected $initEvents(): void {
let prefix;
let hiddenKey;
let resizeTimer;
let ticker = this.$ticker;
let prefixes = ['', 'o', 'ms', 'moz', 'webkit'];
for (prefix of prefixes) {
hiddenKey = prefix ? prefix + 'Hidden' : 'hidden';
if (document[hiddenKey] !== undefined) {
break;
}
}
this.$addTouchEventListeners();
this.$addElementEvent(window, 'orientationchange', () => {
ticker.clearTimeout(resizeTimer);
resizeTimer = ticker.setTimeout(this.$boundResizeViewportCanvas, 100);
});
this.$addElementEvent(window, prefix + 'visibilitychange', () => {
let hidden = document[hiddenKey];
this.$activated = !hidden;
this.emit(hidden ? Event.DEACTIVATE : Event.ACTIVATE);
});
this.on(Event.ENTER_FRAME, this.$render);
setTimeout(() => {
if (!document[hiddenKey]) {
this.$activated = true;
this.emit(Event.ACTIVATE);
}
this.$emitAddedToStage(this);
});
}
protected $addElementEvent(target: EventTarget, type: string, listener: (event) => void, options?: boolean | AddEventListenerOptions): void {
target.addEventListener(type, listener, options);
this.$elementEvents.push({target, type, listener});
}
protected $removeElementEvents(): void {
let listeners = this.$elementEvents;
for (let {target, type, listener} of listeners) {
target.removeEventListener(type, listener);
}
listeners.length = 0;
}
protected $addTouchEventListeners(): void {
if (document.ontouchstart !== undefined) {
this.$addElementEvent(document, 'touchstart', event => {
this.$dispatchTouches(TouchEvent.TOUCH_START, event);
});
this.$addElementEvent(document, 'touchmove', event => {
this.$dispatchTouches(TouchEvent.TOUCH_MOVE, event);
event.preventDefault();
}, {passive: false});
this.$addElementEvent(document, 'touchend', event => {
this.$dispatchTouches(TouchEvent.TOUCH_END, event);
this.$dispatchTouches(TouchEvent.TOUCH_TAP, event);
});
this.$addElementEvent(document, 'touchcancel', event => {
this.$dispatchTouches(TouchEvent.TOUCH_CANCEL, event);
});
} else {
let touching = false;
this.$addElementEvent(window, 'mousedown', event => {
this.$dispatchTouchEvent(TouchEvent.TOUCH_START, event.pageX, event.pageY, 0);
touching = true;
});
this.$addElementEvent(window, 'mousemove', event => {
if (touching) {
this.$dispatchTouchEvent(TouchEvent.TOUCH_MOVE, event.pageX, event.pageY, 0);
}
});
this.$addElementEvent(window, 'mouseup', event => {
this.$dispatchTouchEvent(TouchEvent.TOUCH_END, event.pageX, event.pageY, 0);
touching = false;
});
this.$addElementEvent(window, 'click', event => {
this.$dispatchTouchEvent(TouchEvent.TOUCH_TAP, event.pageX, event.pageY, 0);
});
this.$addElementEvent(window, 'blur', () => {
this.$dispatchTouchEvent(TouchEvent.TOUCH_CANCEL, 0, 0, 0);
touching = false;
});
}
}
protected $dispatchTouches(type: string, event: any): void {
let touches = event['changedTouches'];
for (let i = 0, l = touches.length; i < l; ++i) {
let touch = touches[i];
this.$dispatchTouchEvent(type, touch.pageX, touch.pageY, touch.identifier);
}
}
protected $dispatchTouchEvent(type: string, pageX: number, pageY: number, identifier: number): void {
if (this.$ticker.paused || !this.$visible || !this.touchable) {
return;
}
let event = TouchEvent.create(type);
let scrollX = window.scrollX || 0;
let scrollY = window.scrollY || 0;
let width = this.$canvas.width;
let height = this.$canvas.height;
let bounds = this.$renderBounds;
let pixelRatio = Layer.pixelRatio;
let viewportBounds = this.$viewportCanvas.getBoundingClientRect();
let x = (pageX - scrollX - viewportBounds.left - bounds.x / pixelRatio) * width / bounds.width - this.$anchorX;
let y = (pageY - scrollY - viewportBounds.top - bounds.y / pixelRatio) * height / bounds.height - this.$anchorY;
let matrix = this.$getTransform();
let localPos = Vector.create(x, y).transform(matrix.invert()).subtract(this.$anchorX, this.$anchorY);
let inside = this.$localHitTest(localPos);
if (inside || type !== TouchEvent.TOUCH_START) {
event.localX = localPos.x;
event.localY = localPos.y;
event.targetX = event.stageX = x;
event.targetY = event.stageY = y;
event.identifier = identifier;
this.$emitTouchEvent(event, inside);
}
event.release();
matrix.release();
localPos.release();
}
protected $calculateRenderBounds(): void {
let x = 0;
let y = 0;
let canvas = this.$canvas;
let width = canvas.width;
let height = canvas.height;
let scaleMode = this.$scaleMode;
let bounds = this.$renderBounds;
let aspectRatio = width / height;
let viewportCanvas = this.$viewportCanvas;
let viewportWidth = viewportCanvas.width;
let viewportHeight = viewportCanvas.height;
let viewportAspectRatio = viewportWidth / viewportHeight;
if (scaleMode === Stage.SHOW_ALL) {
if (aspectRatio > viewportAspectRatio) {
width = viewportWidth;
height = width / aspectRatio;
} else {
height = viewportHeight;
width = height * aspectRatio;
}
} else if (scaleMode === Stage.EXACT_FIT) {
width = viewportWidth;
height = viewportHeight;
} else if (scaleMode === Stage.NO_BORDER) {
if (aspectRatio < viewportAspectRatio) {
width = viewportWidth;
height = width / aspectRatio;
} else {
height = viewportHeight;
width = height * aspectRatio;
}
x = (viewportWidth - width) / 2;
y = (viewportHeight - height) / 2;
} else if (scaleMode === Stage.FIXED_WIDTH) {
width = viewportWidth;
height = width / aspectRatio;
} else if (scaleMode === Stage.FIXED_HEIGHT) {
height = viewportHeight;
width = height * aspectRatio;
} else if (scaleMode === Stage.FIXED_WIDE) {
if (viewportWidth > viewportHeight) {
width = viewportWidth;
height = width / aspectRatio;
} else {
height = viewportHeight;
width = height * aspectRatio;
}
} else if (scaleMode === Stage.FIXED_NARROW) {
if (viewportWidth < viewportHeight) {
width = viewportWidth;
height = width / aspectRatio;
} else {
height = viewportHeight;
width = height * aspectRatio;
}
}
if (width < viewportWidth) {
x = (viewportWidth - width) / 2;
}
if (height < viewportHeight) {
y = (viewportHeight - height) / 2;
}
bounds.x = x;
bounds.y = y;
bounds.width = width;
bounds.height = height;
}
protected $resizeCanvas(): void {
super.$resizeCanvas();
this.$calculateRenderBounds();
}
protected $resizeViewportCanvas(): void {
let canvas = this.$viewportCanvas;
let pixelRatio = Layer.pixelRatio;
let viewportWidth = this.$viewportWidth || window.innerWidth;
let viewportHeight = this.$viewportHeight || window.innerHeight;
let canvasWidth = viewportWidth * pixelRatio;
let canvasHeight = viewportHeight * pixelRatio;
if (canvas.width !== canvasWidth || canvas.height !== canvasHeight) {
canvas.width = canvasWidth;
canvas.height = canvasHeight;
canvas.style.width = viewportWidth + 'px';
canvas.style.height = viewportHeight + 'px';
this.$calculateRenderBounds();
this.$markDirty();
this.emit(Event.VIEWPORT_RESIZE);
}
}
protected $render(): number {
if (!this.$dirty) {
this.$drawCalls = 0;
return 0;
}
let drawCalls = super.$render();
let canvas = this.$canvas;
let ctx = this.$viewportContext;
let bounds = this.$renderBounds;
let viewportCanvas = this.$viewportCanvas;
let viewportWidth = viewportCanvas.width;
let viewportHeight = viewportCanvas.height;
ctx.clearRect(0, 0, viewportWidth, viewportHeight);
ctx.drawImage(canvas, (bounds.x + 0.5) | 0, (bounds.y + 0.5) | 0, (bounds.width + 0.5) | 0, (bounds.height + 0.5) | 0);
this.$drawCalls = ++drawCalls;
return drawCalls;
}
}
interface ElementEvent {
target: EventTarget,
type: string,
listener: (event) => void
}