Phaser.ScaleManager = function (game, width, height){ this.game = game; this.width = width; this.height = height; this.minWidth = null ; this.maxWidth = null ; this.minHeight = null ; this.maxHeight = null ; this.forceLandscape = false ; this.forcePortrait = false ; this.incorrectOrientation = false ; this.pageAlignHorizontally = false ; this.pageAlignVertically = false ; this.maxIterations = 5; this.orientationSprite = null ; this.enterLandscape = new Phaser.Signal(); this.enterPortrait = new Phaser.Signal(); this.enterIncorrectOrientation = new Phaser.Signal(); this.leaveIncorrectOrientation = new Phaser.Signal(); this.hasResized = new Phaser.Signal(); this.fullScreenTarget = this.game.canvas; this.enterFullScreen = new Phaser.Signal(); this.leaveFullScreen = new Phaser.Signal(); this.orientation = 0; if (window.orientation) { this.orientation = window.orientation; } else { if (window.outerWidth > window.outerHeight) { this.orientation = 90; } } this.scaleFactor = new Phaser.Point(1, 1); this.scaleFactorInversed = new Phaser.Point(1, 1); this.margin = new Phaser.Point(0, 0); this.bounds = new Phaser.Rectangle(0, 0, width, height); this.aspectRatio = 0; this.sourceAspectRatio = width / height; this.event = null ; this.scaleMode = Phaser.ScaleManager.NO_SCALE; this.fullScreenScaleMode = Phaser.ScaleManager.NO_SCALE; this._startHeight = 0; this._width = 0; this._height = 0; this._check = null ; var _this = this; window.addEventListener('orientationchange', function (event){ return _this.checkOrientation(event); } , false ); window.addEventListener('resize', function (event){ return _this.checkResize(event); } , false ); if (!this.game.device.cocoonJS) { 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 ); } } ; Phaser.ScaleManager.EXACT_FIT = 0; Phaser.ScaleManager.NO_SCALE = 1; Phaser.ScaleManager.SHOW_ALL = 2; Phaser.ScaleManager.prototype = { startFullScreen: function (antialias){ if (this.isFullScreen || !this.game.device.fullscreen) { return ; } if (typeof antialias !== 'undefined' && this.game.renderType === Phaser.CANVAS) { this.game.stage.smoothed = antialias; } this._width = this.width; this._height = this.height; if (this.game.device.fullscreenKeyboard) { this.fullScreenTarget[this.game.device.requestFullscreen](Element.ALLOW_KEYBOARD_INPUT); } else { this.fullScreenTarget[this.game.device.requestFullscreen](); } } , stopFullScreen: function (){ document[this.game.device.cancelFullscreen](); } , fullScreenChange: function (event){ this.event = event; if (this.isFullScreen) { if (this.fullScreenScaleMode === Phaser.ScaleManager.EXACT_FIT) { this.fullScreenTarget.style.width = '100%'; this.fullScreenTarget.style.height = '100%'; this.width = window.outerWidth; this.height = window.outerHeight; 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; this.checkResize(); } else if (this.fullScreenScaleMode === Phaser.ScaleManager.SHOW_ALL) { this.setShowAll(); _AN_Call_refresh('refresh', this); } this.enterFullScreen.dispatch(this.width, this.height); } else { this.fullScreenTarget.style.width = this.game.width + 'px'; this.fullScreenTarget.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; this.leaveFullScreen.dispatch(this.width, this.height); } } , 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 Phaser.Image(this.game, this.game.width / 2, this.game.height / 2, orientationImage); this.orientationSprite.anchor.set(0.5); 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.addChild(this.orientationSprite); } } , checkOrientationState: function (){ if (this.incorrectOrientation) { if ((this.forceLandscape && window.innerWidth > window.innerHeight) || (this.forcePortrait && window.innerHeight > window.innerWidth)) { this.incorrectOrientation = false ; this.leaveIncorrectOrientation.dispatch(); if (this.orientationSprite) { this.orientationSprite.visible = false ; this.game.world.visible = true ; } if (this.scaleMode !== Phaser.ScaleManager.NO_SCALE) { _AN_Call_refresh('refresh', this); } } } else { if ((this.forceLandscape && window.innerWidth < window.innerHeight) || (this.forcePortrait && window.innerHeight < window.innerWidth)) { this.incorrectOrientation = true ; this.enterIncorrectOrientation.dispatch(); if (this.orientationSprite && this.orientationSprite.visible === false ) { this.orientationSprite.visible = true ; this.game.world.visible = false ; } if (this.scaleMode !== Phaser.ScaleManager.NO_SCALE) { _AN_Call_refresh('refresh', this); } } } } , 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.scaleMode !== Phaser.ScaleManager.NO_SCALE) { _AN_Call_refresh('refresh', this); } } , 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.scaleMode !== Phaser.ScaleManager.NO_SCALE) { _AN_Call_refresh('refresh', this); } this.checkOrientationState(); } , refresh: function (){ if (!this.game.device.iPad && !this.game.device.webApp && !this.game.device.desktop) { if (this.game.device.android && !this.game.device.chrome) { 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 = _AN_Call_setinterval('setInterval', window, function (){ return _this.setScreenSize(); } , 10); this.setScreenSize(); } } , setScreenSize: function (force){ if (typeof force === 'undefined') { force = false ; } if (!this.game.device.iPad && !this.game.device.webApp && !this.game.device.desktop) { if (this.game.device.android && !this.game.device.chrome) { window.scrollTo(0, 1); } else { window.scrollTo(0, 0); } } this._iterations-- ; if (force || window.innerHeight > this._startHeight || this._iterations < 0) { document.documentElement.style.minHeight = window.innerHeight + 'px'; if (this.incorrectOrientation) { this.setMaximum(); } else if (!this.isFullScreen) { if (this.scaleMode === Phaser.ScaleManager.EXACT_FIT) { this.setExactFit(); } else if (this.scaleMode === Phaser.ScaleManager.SHOW_ALL) { this.setShowAll(); } } else { if (this.fullScreenScaleMode === Phaser.ScaleManager.EXACT_FIT) { this.setExactFit(); } else if (this.fullScreenScaleMode === Phaser.ScaleManager.SHOW_ALL) { this.setShowAll(); } } this.setSize(); clearInterval(this._check); this._check = null ; } } , setSize: function (){ if (!this.incorrectOrientation) { 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) { 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) { 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.bounds.setTo(this.game.stage.offset.x, this.game.stage.offset.y, this.width, this.height); 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(); } , setMaximum: function (){ this.width = window.innerWidth; this.height = window.innerHeight; } , 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); } , setExactFit: function (){ var availableWidth = window.innerWidth; var availableHeight = window.innerHeight; 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.ScaleManager.prototype.constructor = Phaser.ScaleManager; Object.defineProperty(Phaser.ScaleManager.prototype, "isFullScreen", { get: function (){ return (document.fullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement); } } ); Object.defineProperty(Phaser.ScaleManager.prototype, "isPortrait", { get: function (){ return (this.orientation === 0 || this.orientation === 180); } } ); Object.defineProperty(Phaser.ScaleManager.prototype, "isLandscape", { get: function (){ return (this.orientation === 90 || this.orientation === -90); } } );