forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStageScaleMode.js
More file actions
778 lines (632 loc) · 23.2 KB
/
StageScaleMode.js
File metadata and controls
778 lines (632 loc) · 23.2 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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
/**
* @author Richard Davey <[email protected]>
* @copyright 2013 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The StageScaleMode object is responsible for helping you manage the scaling, resizing and alignment of your game within the browser.
*
* @class Phaser.StageScaleMode
* @constructor
* @param {Phaser.Game} game - A reference to the currently running game.
* @param {number} width - The native width of the game.
* @param {number} height - The native height of the game.
*/
Phaser.StageScaleMode = function (game, width, height) {
/**
* @property {Phaser.Game} game - A reference to the currently running game.
*/
this.game = game;
/**
* @property {number} width - Width of the stage after calculation.
*/
this.width = width;
/**
* @property {number} height - Height of the stage after calculation.
*/
this.height = height;
/**
* @property {number} minWidth - Minimum width the canvas should be scaled to (in pixels).
* @default
*/
this.minWidth = null;
/**
* @property {number} maxWidth - Maximum width the canvas should be scaled to (in pixels).
* If null it will scale to whatever width the browser can handle.
* @default
*/
this.maxWidth = null;
/**
* @property {number} minHeight - Minimum height the canvas should be scaled to (in pixels).
* @default
*/
this.minHeight = null;
/**
* @property {number} maxHeight - Maximum height the canvas should be scaled to (in pixels).
* If null it will scale to whatever height the browser can handle.
* @default
*/
this.maxHeight = null;
/**
* @property {number} _startHeight - Stage height when starting the game.
* @default
* @private
*/
this._startHeight = 0;
/**
* @property {boolean} forceLandscape - If the game should be forced to use Landscape mode, this is set to true by Game.Stage
* @default
*/
this.forceLandscape = false;
/**
* @property {boolean} forcePortrait - If the game should be forced to use Portrait mode, this is set to true by Game.Stage
* @default
*/
this.forcePortrait = false;
/**
* @property {boolean} incorrectOrientation - If the game should be forced to use a specific orientation and the device currently isn't in that orientation this is set to true.
* @default
*/
this.incorrectOrientation = false;
/**
* @property {boolean} pageAlignHorizontally - If you wish to align your game in the middle of the page then you can set this value to true.
* It will place a re-calculated margin-left pixel value onto the canvas element which is updated on orientation/resizing.
* It doesn't care about any other DOM element that may be on the page, it literally just sets the margin.
* @default
*/
this.pageAlignHorizontally = false;
/**
* @property {boolean} pageAlignVertically - If you wish to align your game in the middle of the page then you can set this value to true.
* It will place a re-calculated margin-left pixel value onto the canvas element which is updated on orientation/resizing.
* It doesn't care about any other DOM element that may be on the page, it literally just sets the margin.
* @default
*/
this.pageAlignVertically = false;
/**
* @property {number} _width - Cached stage width for full screen mode.
* @default
* @private
*/
this._width = 0;
/**
* @property {number} _height - Cached stage height for full screen mode.
* @default
* @private
*/
this._height = 0;
/**
* @property {number} maxIterations - The maximum number of times it will try to resize the canvas to fill the browser.
* @default
*/
this.maxIterations = 5;
/**
* @property {PIXI.Sprite} orientationSprite - The Sprite that is optionally displayed if the browser enters an unsupported orientation.
* @default
*/
this.orientationSprite = null;
/**
* @property {Phaser.Signal} enterLandscape - The event that is dispatched when the browser enters landscape orientation.
*/
this.enterLandscape = new Phaser.Signal();
/**
* @property {Phaser.Signal} enterPortrait - The event that is dispatched when the browser enters horizontal orientation.
*/
this.enterPortrait = new Phaser.Signal();
/**
* @property {Phaser.Signal} enterIncorrectOrientation - The event that is dispatched when the browser enters an incorrect orientation, as defined by forceOrientation.
*/
this.enterIncorrectOrientation = new Phaser.Signal();
/**
* @property {Phaser.Signal} leaveIncorrectOrientation - The event that is dispatched when the browser leaves an incorrect orientation, as defined by forceOrientation.
*/
this.leaveIncorrectOrientation = new Phaser.Signal();
/**
* @property {Phaser.Signal} hasResized - The event that is dispatched when the game scale changes.
*/
this.hasResized = new Phaser.Signal();
if (window['orientation'])
{
this.orientation = window['orientation'];
}
else
{
if (window.outerWidth > window.outerHeight)
{
this.orientation = 90;
}
else
{
this.orientation = 0;
}
}
/**
* @property {Phaser.Point} scaleFactor - The scale factor based on the game dimensions vs. the scaled dimensions.
* @readonly
*/
this.scaleFactor = new Phaser.Point(1, 1);
/**
* @property {Phaser.Point} scaleFactorInversed - The inversed scale factor. The displayed dimensions divided by the game dimensions.
* @readonly
*/
this.scaleFactorInversed = new Phaser.Point(1, 1);
/**
* @property {Phaser.Point} margin - If the game canvas is seto to align by adjusting the margin, the margin calculation values are stored in this Point.
* @readonly
*/
this.margin = new Phaser.Point(0, 0);
/**
* @property {number} aspectRatio - Aspect ratio.
* @default
*/
this.aspectRatio = 0;
/**
* @property {any} event- The native browser events from full screen API changes.
*/
this.event = null;
var _this = this;
window.addEventListener('orientationchange', function (event) {
return _this.checkOrientation(event);
}, false);
window.addEventListener('resize', function (event) {
return _this.checkResize(event);
}, false);
document.addEventListener('webkitfullscreenchange', function (event) {
return _this.fullScreenChange(event);
}, false);
document.addEventListener('mozfullscreenchange', function (event) {
return _this.fullScreenChange(event);
}, false);
document.addEventListener('fullscreenchange', function (event) {
return _this.fullScreenChange(event);
}, false);
};
/**
* @constant
* @type {number}
*/
Phaser.StageScaleMode.EXACT_FIT = 0;
/**
* @constant
* @type {number}
*/
Phaser.StageScaleMode.NO_SCALE = 1;
/**
* @constant
* @type {number}
*/
Phaser.StageScaleMode.SHOW_ALL = 2;
Phaser.StageScaleMode.prototype = {
/**
* Tries to enter the browser into full screen mode.
* Please note that this needs to be supported by the web browser and isn't the same thing as setting your game to fill the browser.
* @method Phaser.StageScaleMode#startFullScreen
* @param {boolean} antialias - You can toggle the anti-alias feature of the canvas before jumping in to full screen (false = retain pixel art, true = smooth art)
*/
startFullScreen: function (antialias) {
if (this.isFullScreen)
{
return;
}
if (typeof antialias !== 'undefined')
{
Phaser.Canvas.setSmoothingEnabled(this.game.context, antialias);
}
var element = this.game.canvas;
this._width = this.width;
this._height = this.height;
// This needs updating to match the final spec:
// http://generatedcontent.org/post/70347573294/is-your-fullscreen-api-code-up-to-date-find-out-how-to
if (element['requestFullScreen'])
{
element['requestFullScreen']();
}
else if (element['mozRequestFullScreen'])
{
element.parentNode['mozRequestFullScreen']();
}
else if (element['webkitRequestFullScreen'])
{
element['webkitRequestFullScreen'](Element.ALLOW_KEYBOARD_INPUT);
}
},
/**
* Stops full screen mode if the browser is in it.
* @method Phaser.StageScaleMode#stopFullScreen
*/
stopFullScreen: function () {
if (document['cancelFullScreen'])
{
document['cancelFullScreen']();
}
else if (document['mozCancelFullScreen'])
{
document['mozCancelFullScreen']();
}
else if (document['webkitCancelFullScreen'])
{
document['webkitCancelFullScreen']();
}
},
/**
* Called automatically when the browser enters of leaves full screen mode.
* @method Phaser.StageScaleMode#fullScreenChange
* @param {Event} event - The fullscreenchange event
* @protected
*/
fullScreenChange: function (event) {
this.event = event;
if (this.isFullScreen)
{
if (this.game.stage.fullScreenScaleMode === Phaser.StageScaleMode.EXACT_FIT)
{
this.game.stage.canvas.style['width'] = '100%';
this.game.stage.canvas.style['height'] = '100%';
this.setMaximum();
this.game.input.scale.setTo(this.game.width / this.width, this.game.height / this.height);
this.aspectRatio = this.width / this.height;
this.scaleFactor.x = this.game.width / this.width;
this.scaleFactor.y = this.game.height / this.height;
}
else if (this.game.stage.fullScreenScaleMode === Phaser.StageScaleMode.SHOW_ALL)
{
this.game.stage.scale.setShowAll();
this.game.stage.scale.refresh();
}
}
else
{
this.game.stage.canvas.style['width'] = this.game.width + 'px';
this.game.stage.canvas.style['height'] = this.game.height + 'px';
this.width = this._width;
this.height = this._height;
this.game.input.scale.setTo(this.game.width / this.width, this.game.height / this.height);
this.aspectRatio = this.width / this.height;
this.scaleFactor.x = this.game.width / this.width;
this.scaleFactor.y = this.game.height / this.height;
}
},
/**
* If you need your game to run in only one orientation you can force that to happen.
* The optional orientationImage is displayed when the game is in the incorrect orientation.
* @method Phaser.StageScaleMode#forceOrientation
* @param {boolean} forceLandscape - true if the game should run in landscape mode only.
* @param {boolean} [forcePortrait=false] - true if the game should run in portrait mode only.
* @param {string} [orientationImage=''] - The string of an image in the Phaser.Cache to display when this game is in the incorrect orientation.
*/
forceOrientation: function (forceLandscape, forcePortrait, orientationImage) {
if (typeof forcePortrait === 'undefined') { forcePortrait = false; }
this.forceLandscape = forceLandscape;
this.forcePortrait = forcePortrait;
if (typeof orientationImage !== 'undefined')
{
if (orientationImage == null || this.game.cache.checkImageKey(orientationImage) === false)
{
orientationImage = '__default';
}
this.orientationSprite = new PIXI.Sprite(PIXI.TextureCache[orientationImage]);
this.orientationSprite.anchor.x = 0.5;
this.orientationSprite.anchor.y = 0.5;
this.orientationSprite.position.x = this.game.width / 2;
this.orientationSprite.position.y = this.game.height / 2;
this.checkOrientationState();
if (this.incorrectOrientation)
{
this.orientationSprite.visible = true;
this.game.world.visible = false;
}
else
{
this.orientationSprite.visible = false;
this.game.world.visible = true;
}
this.game.stage._stage.addChild(this.orientationSprite);
}
},
/**
* Checks if the browser is in the correct orientation for your game (if forceLandscape or forcePortrait have been set)
* @method Phaser.StageScaleMode#checkOrientationState
*/
checkOrientationState: function () {
// They are in the wrong orientation
if (this.incorrectOrientation)
{
if ((this.forceLandscape && window.innerWidth > window.innerHeight) || (this.forcePortrait && window.innerHeight > window.innerWidth))
{
// Back to normal
this.game.paused = false;
this.incorrectOrientation = false;
this.leaveIncorrectOrientation.dispatch();
if (this.orientationSprite)
{
this.orientationSprite.visible = false;
this.game.world.visible = true;
}
this.refresh();
}
}
else
{
if ((this.forceLandscape && window.innerWidth < window.innerHeight) || (this.forcePortrait && window.innerHeight < window.innerWidth))
{
// Show orientation screen
this.game.paused = true;
this.incorrectOrientation = true;
this.enterIncorrectOrientation.dispatch();
if (this.orientationSprite && this.orientationSprite.visible === false)
{
this.orientationSprite.visible = true;
this.game.world.visible = false;
}
this.refresh();
}
}
},
/**
* Handle window.orientationchange events
* @method Phaser.StageScaleMode#checkOrientation
* @param {Event} event - The orientationchange event data.
*/
checkOrientation: function (event) {
this.event = event;
this.orientation = window['orientation'];
if (this.isLandscape)
{
this.enterLandscape.dispatch(this.orientation, true, false);
}
else
{
this.enterPortrait.dispatch(this.orientation, false, true);
}
if (this.game.stage.scaleMode !== Phaser.StageScaleMode.NO_SCALE)
{
this.refresh();
}
},
/**
* Handle window.resize events
* @method Phaser.StageScaleMode#checkResize
* @param {Event} event - The resize event data.
*/
checkResize: function (event) {
this.event = event;
if (window.outerWidth > window.outerHeight)
{
this.orientation = 90;
}
else
{
this.orientation = 0;
}
if (this.isLandscape)
{
this.enterLandscape.dispatch(this.orientation, true, false);
}
else
{
this.enterPortrait.dispatch(this.orientation, false, true);
}
if (this.game.stage.scaleMode !== Phaser.StageScaleMode.NO_SCALE)
{
this.refresh();
}
this.checkOrientationState();
},
/**
* Re-calculate scale mode and update screen size.
* @method Phaser.StageScaleMode#refresh
*/
refresh: function () {
// We can't do anything about the status bars in iPads, web apps or desktops
if (this.game.device.iPad === false && this.game.device.webApp === false && this.game.device.desktop === false)
{
if (this.game.device.android && this.game.device.chrome === false)
{
window.scrollTo(0, 1);
}
else
{
window.scrollTo(0, 0);
}
}
if (this._check == null && this.maxIterations > 0)
{
this._iterations = this.maxIterations;
var _this = this;
this._check = window.setInterval(function () {
return _this.setScreenSize();
}, 10);
this.setScreenSize();
}
},
/**
* Set screen size automatically based on the scaleMode.
* @param {boolean} force - If force is true it will try to resize the game regardless of the document dimensions.
*/
setScreenSize: function (force) {
if (typeof force == 'undefined')
{
force = false;
}
if (this.game.device.iPad === false && this.game.device.webApp === false && this.game.device.desktop === false)
{
if (this.game.device.android && this.game.device.chrome === false)
{
window.scrollTo(0, 1);
}
else
{
window.scrollTo(0, 0);
}
}
this._iterations--;
if (force || window.innerHeight > this._startHeight || this._iterations < 0)
{
// Set minimum height of content to new window height
document.documentElement['style'].minHeight = window.innerHeight + 'px';
if (this.incorrectOrientation === true)
{
this.setMaximum();
}
else if (!this.isFullScreen)
{
if (this.game.stage.scaleMode == Phaser.StageScaleMode.EXACT_FIT)
{
this.setExactFit();
}
else if (this.game.stage.scaleMode == Phaser.StageScaleMode.SHOW_ALL)
{
this.setShowAll();
}
}
else
{
if (this.game.stage.fullScreenScaleMode == Phaser.StageScaleMode.EXACT_FIT)
{
this.setExactFit();
}
else if (this.game.stage.fullScreenScaleMode == Phaser.StageScaleMode.SHOW_ALL)
{
this.setShowAll();
}
}
this.setSize();
clearInterval(this._check);
this._check = null;
}
},
/**
* Sets the canvas style width and height values based on minWidth/Height and maxWidth/Height.
* @method Phaser.StageScaleMode#setSize
*/
setSize: function () {
if (this.incorrectOrientation === false)
{
if (this.maxWidth && this.width > this.maxWidth)
{
this.width = this.maxWidth;
}
if (this.maxHeight && this.height > this.maxHeight)
{
this.height = this.maxHeight;
}
if (this.minWidth && this.width < this.minWidth)
{
this.width = this.minWidth;
}
if (this.minHeight && this.height < this.minHeight)
{
this.height = this.minHeight;
}
}
this.game.canvas.style.width = this.width + 'px';
this.game.canvas.style.height = this.height + 'px';
this.game.input.scale.setTo(this.game.width / this.width, this.game.height / this.height);
if (this.pageAlignHorizontally)
{
if (this.width < window.innerWidth && this.incorrectOrientation === false)
{
this.margin.x = Math.round((window.innerWidth - this.width) / 2);
this.game.canvas.style.marginLeft = this.margin.x + 'px';
}
else
{
this.margin.x = 0;
this.game.canvas.style.marginLeft = '0px';
}
}
if (this.pageAlignVertically)
{
if (this.height < window.innerHeight && this.incorrectOrientation === false)
{
this.margin.y = Math.round((window.innerHeight - this.height) / 2);
this.game.canvas.style.marginTop = this.margin.y + 'px';
}
else
{
this.margin.y = 0;
this.game.canvas.style.marginTop = '0px';
}
}
Phaser.Canvas.getOffset(this.game.canvas, this.game.stage.offset);
this.aspectRatio = this.width / this.height;
this.scaleFactor.x = this.game.width / this.width;
this.scaleFactor.y = this.game.height / this.height;
this.scaleFactorInversed.x = this.width / this.game.width;
this.scaleFactorInversed.y = this.height / this.game.height;
this.hasResized.dispatch(this.width, this.height);
this.checkOrientationState();
},
/**
* Sets this.width equal to window.innerWidth and this.height equal to window.innerHeight
* @method Phaser.StageScaleMode#setMaximum
*/
setMaximum: function () {
this.width = window.innerWidth;
this.height = window.innerHeight;
},
/**
* Calculates the multiplier needed to scale the game proportionally.
* @method Phaser.StageScaleMode#setShowAll
*/
setShowAll: function () {
var multiplier = Math.min((window.innerHeight / this.game.height), (window.innerWidth / this.game.width));
this.width = Math.round(this.game.width * multiplier);
this.height = Math.round(this.game.height * multiplier);
},
/**
* Sets the width and height values of the canvas, no larger than the maxWidth/Height.
* @method Phaser.StageScaleMode#setExactFit
*/
setExactFit: function () {
var availableWidth = window.innerWidth;
var availableHeight = window.innerHeight;
// console.log('available', availableWidth, availableHeight);
if (this.maxWidth && availableWidth > this.maxWidth)
{
this.width = this.maxWidth;
}
else
{
this.width = availableWidth;
}
if (this.maxHeight && availableHeight > this.maxHeight)
{
this.height = this.maxHeight;
}
else
{
this.height = availableHeight;
}
}
};
Phaser.StageScaleMode.prototype.constructor = Phaser.StageScaleMode;
/**
* @name Phaser.StageScaleMode#isFullScreen
* @property {boolean} isFullScreen - Returns true if the browser is in full screen mode, otherwise false.
* @readonly
*/
Object.defineProperty(Phaser.StageScaleMode.prototype, "isFullScreen", {
get: function () {
return (document['fullscreenElement'] || document['mozFullScreenElement'] || document['webkitFullscreenElement'])
}
});
/**
* @name Phaser.StageScaleMode#isPortrait
* @property {boolean} isPortrait - Returns true if the browser dimensions match a portrait display.
* @readonly
*/
Object.defineProperty(Phaser.StageScaleMode.prototype, "isPortrait", {
get: function () {
return this.orientation === 0 || this.orientation == 180;
}
});
/**
* @name Phaser.StageScaleMode#isLandscape
* @property {boolean} isLandscape - Returns true if the browser dimensions match a landscape display.
* @readonly
*/
Object.defineProperty(Phaser.StageScaleMode.prototype, "isLandscape", {
get: function () {
return this.orientation === 90 || this.orientation === -90;
}
});