forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScaleManager.js
More file actions
2169 lines (1776 loc) · 65.3 KB
/
Copy pathScaleManager.js
File metadata and controls
2169 lines (1776 loc) · 65.3 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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2014 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The ScaleManager object is responsible managing the scaling, resizing and alignment of the game and display canvas.
*
* The `width` and `height` constructor parameters can either be a number which represents pixels or a string that represents a percentage: e.g. `800` (for 800 pixels) or `"80%"` for 80%.
*
* Some parts of ScaleManager were inspired by the research of Ryan Van Etten, released under MIT License 2013.
*
* @class Phaser.ScaleManager
* @constructor
* @param {Phaser.Game} game - A reference to the currently running game.
* @param {number|string} width - The width of the game. See above.
* @param {number|string} height - The height of the game. See above.
*/
Phaser.ScaleManager = function (game, width, height) {
/**
* A reference to the currently running game.
* @property {Phaser.Game} game
* @protected
* @readonly
*/
this.game = game;
/**
* _EXPERIMENTAL:_ A responsive grid on which you can align game objects.
* @property {Phaser.FlexGrid} grid
* @public
*/
this.grid = null;
/**
* Target width (in pixels) of the Game canvas.
* @property {number} width
* @readonly
*/
this.width = 0;
/**
* Target height (in pixels) of the Game canvas.
* @property {number} height
* @readonly
*/
this.height = 0;
/**
* Minimum width the canvas should be scaled to (in pixels).
* Change with `setMinMax`.
* @property {number} minWidth
* @readonly
*/
this.minWidth = null;
/**
* Maximum width the canvas should be scaled to (in pixels).
* If null it will scale to whatever width the browser can handle.
* Change with `setMinMax`.
* @property {number} maxWidth
* @readonly
*/
this.maxWidth = null;
/**
* Minimum height the canvas should be scaled to (in pixels).
* Change with `setMinMax`.
* @property {number} minHeight
* @readonly
*/
this.minHeight = null;
/**
* Maximum height the canvas should be scaled to (in pixels).
* If null it will scale to whatever height the browser can handle.
* Change with `setMinMax`.
* @property {number} maxHeight
* @readonly
*/
this.maxHeight = null;
/**
* The offset coordinates of the Game canvas from the top-left of the browser window.
* The is used internally by Phaser.Pointer (for Input) and possibly other types.
* @property {Phaser.Point} offset
* @readonly
* @protected
*/
this.offset = new Phaser.Point();
/**
* If true, the game should only run in a landscape orientation.
* Change with `forceOrientation`.
* @property {boolean} forceLandscape
* @readonly
* @default
* @protected
*/
this.forceLandscape = false;
/**
* If true, the game should only run in a portrait
* Change with `forceOrientation`.
* @property {boolean} forcePortrait
* @readonly
* @default
* @protected
*/
this.forcePortrait = false;
/**
* True if the `forceLandscape` or `forcePortrait` are set and do not agree with the browser orientation.
* @property {boolean} incorrectOrientation
* @readonly
* @protected
*/
this.incorrectOrientation = false;
/**
* See `pageAlignHorizontally`.
* @property {boolean} _pageAlignHorizontally
* @private
*/
this._pageAlignHorizontally = false;
/**
* See `pageAlignVertically`.
* @property {boolean} _pageAlignVertically
* @private
*/
this._pageAlignVertically = false;
/**
* The maximum number of times a canvas will be resized (in a row) in order to fill the browser.
* @property {number} maxIterations
* @default
* @deprecated 2.1.4 - This is not used anymore as reflow iterations are "automatic".
* @protected
*/
this.maxIterations = 5;
/**
* This signal is dispatched when the browser enters landscape orientation, having been in portrait.
*
* This is signaled from `preUpdate` (or `pauseUpdate`) _even when_ the game is paused.
*
* @property {Phaser.Signal} enterLandscape
* @public
*/
this.enterLandscape = new Phaser.Signal();
/**
* This signal is dispatched when the browser enters portrait orientation, having been in landscape.
*
* This is signaled from `preUpdate` (or `pauseUpdate`) _even when_ the game is paused.
*
* @property {Phaser.Signal} enterPortrait
* @public
*/
this.enterPortrait = new Phaser.Signal();
/**
* This signal is dispatched when the browser enters an incorrect orientation, as defined by `forceOrientation`.
*
* This is signaled from `preUpdate` (or `pauseUpdate`) _even when_ the game is paused.
*
* @property {Phaser.Signal} enterIncorrectOrientation
* @public
*/
this.enterIncorrectOrientation = new Phaser.Signal();
/**
* This signal is dispatched when the browser leaves an incorrect orientation, as defined by `forceOrientation`.
*
* This is signaled from `preUpdate` (or `pauseUpdate`) _even when_ the game is paused.
*
* @property {Phaser.Signal} leaveIncorrectOrientation -
* @public
*/
this.leaveIncorrectOrientation = new Phaser.Signal();
/**
* This is the DOM element on which the Full Screen API will be invoked.
* It can be any valid DOM element - the target element must have the correct CSS styling and should contain the game canvas.
*
* This element's style will be modified (ie. the width and height might be set to 100%)
* but it will not be added to, removed from, or repositioned within the DOM.
* An attempt is made to restore relevant style changes when fullscreen mode is left.
*
* For pre 2.1.4 behavior set `game.scale.fullScreenTarget = game.canvas`.
*
* @property {DOMElement|null} fullScreenTarget
* @default
* @deprecated 2.1.4 - See `createFullScreenTarget` as an alternative.
*/
this.fullScreenTarget = null;
/**
* The `createFullScreenTarget` function creates a fullscreen target when `fullScreenTarget` is not set.
*
* The Game canvas is moved onto the created element for the duration of the fullscreen mode
* and restored to it's original DOM location when fullscreen is exited.
*
* The returned element (which should probably be newly created) is moved/reparanted within
* the DOM and may have its CSS styles updated. Assign an element to `fullScreenTarget` to avoid
* this DOM manipulation and revert to earlier behavior.
*
* The default implementation is to create a new element with a black background.
*
* @property {function} createFullScreenTarget
* @protected
*/
this.createFullScreenTarget = function () {
var fsTarget = document.createElement('div');
fsTarget.style.margin = '0';
fsTarget.style.padding = '0';
fsTarget.style.background = '#000';
return fsTarget;
};
/**
* The fullscreen target, as created by `createFullScreenTarget`.
* This is not set if `fullScreenTarget` is used and is cleared when fullscreen mode ends.
* @property {DOMElement|null} _createdFullScreenTarget
* @private
*/
this._createdFullScreenTarget = null;
/**
* This signal is dispatched when the browser enters fullscreen mode, if supported.
* @property {Phaser.Signal} enterFullScreen
* @public
*/
this.enterFullScreen = new Phaser.Signal();
/**
* This signal is dispatched when the browser leaves fullscreen mode.
* @property {Phaser.Signal} leaveFullScreen
* @public
*/
this.leaveFullScreen = new Phaser.Signal();
/**
* This signal is dispatched when the browser fails to enter fullscreen mode;
* or if the device does not support fullscreen mode and `startFullScreen` is invoked.
* @property {Phaser.Signal} leaveFullScreen
* @public
*/
this.fullScreenFailed = new Phaser.Signal();
/**
* The _last known_ orientation of the screen, as defined in the Window Screen Web API.
* See {@link Phaser.DOM.getScreenOrientation} for possible values.
*
* @property {string} screenOrientation
* @readonly
* @public
*/
this.screenOrientation = Phaser.DOM.getScreenOrientation();
/**
* The _current_ scale factor based on the game dimensions vs. the scaled dimensions.
* @property {Phaser.Point} scaleFactor
* @readonly
*/
this.scaleFactor = new Phaser.Point(1, 1);
/**
* The _current_ inversed scale factor. The displayed dimensions divided by the game dimensions.
* @property {Phaser.Point} scaleFactorInversed
* @readonly
* @protected
*/
this.scaleFactorInversed = new Phaser.Point(1, 1);
/**
* The game canvas is aligned by adjusting the margins; the last margins are stored here.
* @property {Bounds-like} margin
* @readonly
*/
this.margin = {left: 0, top: 0, right: 0, bottom: 0, x: 0, y: 0};
/**
* The bounds of the scaled game. The x/y will match the offset of the canvas element and the width/height the scaled width and height.
* @property {Phaser.Rectangle} bounds
* @readonly
*/
this.bounds = new Phaser.Rectangle();
/**
* The aspect ratio of the scaled game canvas.
* @property {number} aspectRatio
* @readonly
*/
this.aspectRatio = 0;
/**
* The aspect ratio of the original game dimensions.
* @property {number} sourceAspectRatio
* @readonly
*/
this.sourceAspectRatio = 0;
/**
* The native browser events from Fullscreen API changes.
* @property {any} event
* @readonly
* @private
*/
this.event = null;
/**
* The edges on which to constrain the Canvas _to_ the Window viewport in _addition_ to any restrictions of the parent container.
* @property {boolean} windowConstraints
* @default
*/
this.windowConstraints = {
bottom: true,
right: true
};
/**
* Various compatibility settings. The `(auto)` settings are automatically configured on boot based on device and other runtime information.
* @protected
*
* @property {boolean} [supportsFullscreen=(auto)] - True only if fullscreen support will be used. (Changing to fullscreen still might not work.)
*
* @property {boolean} [orientationFallback=(auto)] - See {@link Phaser.DOM.getScreenOrientation}.
*
* @property {boolean} [noMargins=false] - If true then the Game canvas's margins will not be updated anymore: existing margins must be manually cleared. Disabling margins prevents automatic canvas alignment/centering, possibly in fullscreen.
*
* @property {Phaser.Point|null} [scrollTo=(auto)] - If specified the window will be scrolled to this position on every refresh.
*
* @property {boolean} [forceMinimumDocumentHeight=true] - If enabled the document element's minimum height is explicity set on updates.
*
* @property {boolean} [showAllCanExpand=true] - If enabled then SHOW_ALL is allowed to try and expand it's non-window parent. It may be necessary for the parent element to impose CSS width/height restrictions.
*/
this.compatibility = {
supportsFullScreen: false,
orientationFallback: null,
noMargins: false,
scrollTo: null,
forceMinimumDocumentHeight: true,
showAllCanExpand: true
};
/**
* Scale mode to be used when not in fullscreen.
* @property {number} _scaleMode
* @private
*/
this._scaleMode = Phaser.ScaleManager.NO_SCALE;
/*
* Scale mode to be used in fullscreen.
* @property {number} _fullScreenScaleMode
* @private
*/
this._fullScreenScaleMode = Phaser.ScaleManager.NO_SCALE;
/**
* If the parent container of the game is the browser window (ie. document.body), rather than a div, this should set to `true`.
* @property {boolean} parentIsWindow
* @readonly
*/
this.parentIsWindow = false;
/**
* The _original_ DOM element for the parent of the game canvas.
* This may be different in fullscreen - see `createFullScreenTarget`.
*
* If the `parentIsWindow` is true then this should likely be `null`.
*
* @property {DOMElement|null} parentNode
* @readonly
*/
this.parentNode = null;
/**
* The scale of the game in relation to its parent container.
* @property {Phaser.Point} parentScaleFactor
* @readonly
*/
this.parentScaleFactor = new Phaser.Point(1, 1);
/**
* The maximum time (in ms) between dimension update checks for the Canvas's parent element (or window).
* Update checks normally happen quicker in response to other events.
* @property {integer} trackParentInterval
* @default
* @protected
*/
this.trackParentInterval = 2000;
/*
* This signal is dispatched when the size of the Game canvas changes _or_ the size of the Game changes.
* When invoked this is done _after_ the Canvas size/position have been updated.
*
* This signal is _only_ called when a change occurs and a reflow may be required.
* For example, if the canvas does not change sizes because of CSS settings (such as min-width)
* then this signal will _not_ be triggered.
*
* Use this to handle responsive game layout options.
*
* This is signaled from `preUpdate` (or `pauseUpdate`) _even when_ the game is paused.
*
* @property {Phaser.Signal} onSizeChange
* @todo Formalize the arguments, if any, supplied to this signal.
*/
this.onSizeChange = new Phaser.Signal();
/**
* The callback that will be called each the parent container resizes.
* @property {function} onResize
* @private
*/
this.onResize = null;
/**
* The context in which the `onResize` callback will be called.
* @property {object} onResizeContext
* @private
*/
this.onResizeContext = null;
/**
* Information saved when fullscreen mode is started.
* @property {object|null} _fullScreenRestore
* @private
*/
this._fullScreenRestore = null;
/**
* The _actual_ game dimensions, as initially set or set by `setGameSize`.
* @property {Phaser.Rectangle} _gameSize
* @private
*/
this._gameSize = new Phaser.Rectangle();
/**
* The user-supplied scale factor, used with the USER_SCALE scaling mode.
* @property {Phaser.Point} _userScaleFactor
* @private
*/
this._userScaleFactor = new Phaser.Point(1, 1);
/**
* The last time the bounds were checked in `preUpdate`.
* @property {number} _lastUpdate
* @private
*/
this._lastUpdate = 0;
/**
* Size checks updates are delayed according to the throttle.
* The throttle increases to `trackParentInterval` over time and is used to more
* rapidly detect changes in certain browsers (eg. IE) while providing back-off safety.
* @property {integer} _updateThrottle
* @private
*/
this._updateThrottle = 0;
/**
* The minimum throttle allowed until it has slowed down sufficiently.
* @property {integer} _updateThrottleReset
* @private
*/
this._updateThrottleReset = 100;
/**
* The cached result of the parent (possibly window) bounds; used to invalidate sizing.
* @property {Phaser.Rectangle} _parentBounds
* @private
*/
this._parentBounds = new Phaser.Rectangle();
/**
* The Canvas size at which the last onSizeChange signal was triggered.
* @property {Phaser.Rectangle} _lastReportedCanvasSize
* @private
*/
this._lastReportedCanvasSize = new Phaser.Rectangle();
/**
* The Game size at which the last onSizeChange signal was triggered.
* @property {Phaser.Rectangle} _lastReportedGameSize
* @private
*/
this._lastReportedGameSize = new Phaser.Rectangle();
if (game.config)
{
this.parseConfig(game.config);
}
this.setupScale(width, height);
};
/**
* The Game display area will be _stretched_ to fill the entire size of the canvas's parent element and/or screen.
* Proportions are not mainted.
*
* @constant
* @type {integer}
*/
Phaser.ScaleManager.EXACT_FIT = 0;
/**
* The Game display area will not be scaled - even if it is too large for the canvas/screen.
*
* This mode _ignores_ any applied scaling factor and displays the canvas at the Game size.
*
* @constant
* @type {integer}
*/
Phaser.ScaleManager.NO_SCALE = 1;
/**
* Show the entire game display area while _maintaining_ the original aspect ratio.
*
* @constant
* @type {integer}
*/
Phaser.ScaleManager.SHOW_ALL = 2;
/**
* The dimensions of the game display area are changed to match the size of the parent container.
* That is, this mode _changes the Game size_ to match the display size.
*
* Any manually set Game size (see `setGameSize`) is ignored while in effect.
*
* @constant
* @type {integer}
*/
Phaser.ScaleManager.RESIZE = 3;
/**
* _Experimental_: The Game display area is scaled according to a user-speficied scale.
* Use `setUserScale` to change the scale factor.
*
* @constant
* @protected
* @type {integer}
*/
Phaser.ScaleManager.USER_SCALE = 4;
Phaser.ScaleManager.prototype = {
/**
* Start the ScaleManager.
*
* @method Phaser.ScaleManager#boot
* @protected
*/
boot: function () {
// Configure device-dependent compatibility
var compat = this.compatibility;
compat.supportsFullScreen = this.game.device.fullscreen && !this.game.device.cocoonJS;
// We can't do anything about the status bars in iPads, web apps or desktops
if (!this.game.device.iPad && !this.game.device.webApp && !this.game.device.desktop)
{
if (this.game.device.android && !this.game.device.chrome)
{
compat.scrollTo = new Phaser.Point(0, 1);
}
else
{
compat.scrollTo = new Phaser.Point(0, 0);
}
}
if (this.game.device.desktop)
{
compat.orientationFallback = 'screen';
}
else
{
compat.orientationFallback = '';
}
// Configure event listeners
var _this = this;
this._orientationChange = function(event) {
return _this.orientationChange(event);
};
this._windowResize = function(event) {
return _this.windowResize(event);
};
// This does not appear to be on the standards track
window.addEventListener('orientationchange', this._orientationChange, false);
window.addEventListener('resize', this._windowResize, false);
if (this.compatibility.supportsFullScreen)
{
this._fullScreenChange = function(event) {
return _this.fullScreenChange(event);
};
this._fullScreenError = function(event) {
return _this.fullScreenError(event);
};
document.addEventListener('webkitfullscreenchange', this._fullScreenChange, false);
document.addEventListener('mozfullscreenchange', this._fullScreenChange, false);
document.addEventListener('MSFullscreenChange', this._fullScreenChange, false);
document.addEventListener('fullscreenchange', this._fullScreenChange, false);
document.addEventListener('webkitfullscreenerror', this._fullScreenError, false);
document.addEventListener('mozfullscreenerror', this._fullScreenError, false);
document.addEventListener('MSFullscreenError', this._fullScreenError, false);
document.addEventListener('fullscreenerror', this._fullScreenError, false);
}
this.game.onResume.add(this._gameResumed, this);
// Initialize core bounds
Phaser.DOM.getOffset(this.game.canvas, this.offset);
this.bounds.setTo(this.offset.x, this.offset.y, this.width, this.height);
this.setGameSize(this.game.width, this.game.height);
// Don't use updateOrientationState so events are not fired
this.screenOrientation = Phaser.DOM.getScreenOrientation(this.compatibility.orientationFallback);
},
/**
* Load configuration settings.
*
* @method Phaser.ScaleManager#parseConfig
* @protected
* @param {object} config - The game configuration object.
*/
parseConfig: function (config) {
if (config['scaleMode'])
{
this.scaleMode = config['scaleMode'];
}
if (config['fullScreenScaleMode'])
{
this.fullScreenScaleMode = config['fullScreenScaleMode'];
}
if (config['fullScreenTarget'])
{
this.fullScreenTarget = config['fullScreenTarget'];
}
},
/**
* Calculates and sets the game dimensions based on the given width and height.
*
* This should _not_ be called when in fullscreen mode.
*
* @method Phaser.ScaleManager#setupScale
* @protected
* @param {number|string} width - The width of the game.
* @param {number|string} height - The height of the game.
*/
setupScale: function (width, height) {
var target;
var rect = new Phaser.Rectangle();
if (this.game.parent !== '')
{
if (typeof this.game.parent === 'string')
{
// hopefully an element ID
target = document.getElementById(this.game.parent);
}
else if (this.game.parent && this.game.parent.nodeType === 1)
{
// quick test for a HTMLelement
target = this.game.parent;
}
}
// Fallback, covers an invalid ID and a non HTMLelement object
if (!target)
{
// Use the full window
this.parentNode = null;
this.parentIsWindow = true;
rect.width = window.innerWidth;
rect.height = window.innerHeight;
this.offset.set(0, 0);
}
else
{
this.parentNode = target;
this.parentIsWindow = false;
this.getParentBounds(this._parentBounds);
rect.width = this._parentBounds.width;
rect.height = this._parentBounds.height;
this.offset.set(this._parentBounds.x, this._parentBounds.y);
}
var newWidth = 0;
var newHeight = 0;
if (typeof width === 'number')
{
newWidth = width;
}
else
{
// Percentage based
this.parentScaleFactor.x = parseInt(width, 10) / 100;
newWidth = rect.width * this.parentScaleFactor.x;
}
if (typeof height === 'number')
{
newHeight = height;
}
else
{
// Percentage based
this.parentScaleFactor.y = parseInt(height, 10) / 100;
newHeight = rect.height * this.parentScaleFactor.y;
}
this._gameSize.setTo(0, 0, newWidth, newHeight);
this.grid = new Phaser.FlexGrid(this, newWidth, newHeight);
this.updateDimensions(newWidth, newHeight, false);
},
/**
* Invoked when the game is resumed.
* @method Phaser.ScaleManager#gameResumed
* @private
*/
_gameResumed: function ()
{
this.queueUpdate(true);
},
/**
* Set the actual Game size.
* Use this instead of directly changing `game.width` or `game.height`.
*
* The actual physical display (Canvas element size) depends on various settings including
* - Scale mode
* - Scaling factor
* - Size of Canvas's parent element or CSS rules such as min-height/max-height;
* - The size of the Window
*
* @method Phaser.ScaleManager#setGameSize
* @public
* @param {integer} width - _Game width_, in pixels.
* @param {integer} height - _Game height_, in pixels.
*/
setGameSize: function (width, height) {
this._gameSize.setTo(0, 0, width, height);
if (this.currentScaleMode !== Phaser.ScaleManager.RESIZE)
{
this.updateDimensions(width, height, true);
}
this.queueUpdate(true);
},
/**
* _Experimental_: Set a User scaling factor. This is only used in the USER_SCALE scaling mode.
*
* @method Phaser.ScaleManager#setGameSize
* @protected
* @param {number} width - Width scaling factor.
* @param {numer} height - Height scaling factor.
*/
setUserScale: function (width, height) {
this._userScaleFactor.setTo(width, height);
this.queueUpdate(true);
},
/**
* Sets the callback that will be called when the bounds of the Canvas's parent container may have changed.
*
* This callback
* - May be invoked even though the parent container or canvas sizes have not changed
* - Unlike `onSizeChange`, it runs _before_ the canvas is guaranteed to be updated
* - Will be invoked from `preUpdate`, _even when_ the game is paused
*
* See `onSizeChange` for a better way of reacting to layout updates.
*
* @method Phaser.ScaleManager#setResizeCallback
* @public
* @param {function} callback - The callback that will be called each time a window.resize event happens or if set, the parent container resizes.
* @param {object} context - The context in which the callback will be called.
*/
setResizeCallback: function (callback, context) {
this.onResize = callback;
this.onResizeContext = context;
},
/**
* Signals a resize - IF the canvas or game size differs from the last signal.
*
* This also triggers updates on `grid` (FlexGrid) and, if in a RESIZE mode, `game.state` (StateManager).
*
* @method Phaser.ScaleMager#signalSizeChange
* @private
*/
signalSizeChange: function () {
if (!Phaser.Rectangle.sameDimensions(this, this._lastReportedCanvasSize) ||
!Phaser.Rectangle.sameDimensions(this.game, this._lastReportedGameSize))
{
var width = this.width;
var height = this.height;
this._lastReportedCanvasSize.setTo(0, 0, width, height);
this._lastReportedGameSize.setTo(0, 0, this.game.width, this.game.height);
this.grid.onResize(width, height);
this.onSizeChange.dispatch(this, width, height);
// Per StateManager#onResizeCallback, it only occurs when in RESIZE mode.
if (this.currentScaleMode === Phaser.ScaleManager.RESIZE)
{
this.game.state.resize(width, height);
}
}
},
/**
* Set the min and max dimensions for the game object.
*
* @method Phaser.ScaleManager#setMinMax
* @public
* @param {number} minWidth - The minimum width the game is allowed to scale down to.
* @param {number} minHeight - The minimum height the game is allowed to scale down to.
* @param {number} [maxWidth] - The maximum width the game is allowed to scale up to; only changed if specified.
* @param {number} [maxHeight] - The maximum height the game is allowed to scale up to; only changed if specified.
* @todo These values are only sometimes honored.
*/
setMinMax: function (minWidth, minHeight, maxWidth, maxHeight) {
this.minWidth = minWidth;
this.minHeight = minHeight;
if (typeof maxWidth !== 'undefined')
{
this.maxWidth = maxWidth;
}
if (typeof maxHeight !== 'undefined')
{
this.maxHeight = maxHeight;
}
},
/**
* The ScaleManager.preUpdate is called automatically by the core Game loop.
*
* @method Phaser.ScaleManager#preUpdate
* @protected
*/
preUpdate: function () {
if (this.game.time.time < (this._lastUpdate + this._updateThrottle))
{
return;
}
var prevThrottle = this._updateThrottle;
this._updateThrottleReset = prevThrottle >= 400 ? 0 : 100;
Phaser.DOM.getOffset(this.game.canvas, this.offset);
var prevWidth = this._parentBounds.width;
var prevHeight = this._parentBounds.height;
var bounds = this.getParentBounds(this._parentBounds);
var boundsChanged = bounds.width !== prevWidth || bounds.height !== prevHeight;
// Always invalidate on a newly detected orientation change
var orientationChanged = this.updateOrientationState(false);
if (boundsChanged || orientationChanged)
{
if (this.onResize)
{
this.onResize.call(this.onResizeContext, bounds.width, bounds.height);
}
this.setScreenSize();
this.signalSizeChange();
}
// Next throttle, eg. 25, 50, 100, 200..
var throttle = this._updateThrottle * 2;
// Don't let an update be too eager about resetting the throttle.
if (this._updateThrottle < prevThrottle)
{
throttle = Math.min(prevThrottle, this._updateThrottleReset);
}
this._updateThrottle = Phaser.Math.clamp(throttle, 25, this.trackParentInterval);
this._lastUpdate = this.game.time.time;
},
pauseUpdate: function () {
this.preUpdate();
// Updates at slowest.
this._updateThrottle = this.trackParentInterval;
},
/**
* Update the dimensions taking the parent scaling factor into account.
*
* @method Phaser.ScaleManager#updateDimensions
* @private
* @param {number} width - The new width of the parent container.
* @param {number} height - The new height of the parent container.
* @param {boolean} resize - True if the renderer should be resized, otherwise false to just update the internal vars.
*/
updateDimensions: function (width, height, resize) {
this.width = width * this.parentScaleFactor.x;
this.height = height * this.parentScaleFactor.y;
this.game.width = this.width;
this.game.height = this.height;
this.sourceAspectRatio = this.width / this.height;
this.updateScalingAndBounds();
if (resize)
{
// Resize the renderer (which in turn resizes the Game canvas!)
this.game.renderer.resize(this.width, this.height);
// The Camera can never be smaller than the game size
this.game.camera.setSize(this.width, this.height);
// This should only happen if the world is smaller than the new canvas size
this.game.world.resize(this.width, this.height);
}
},
/**
* Update relevant scaling values based on the ScaleManager dimension and game dimensions,
* which should already be set. This does not change `sourceAspectRatio`.
* @private
*/
updateScalingAndBounds: function () {
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;