Phaser.ScaleManager = function (game, width, height){ this.game = game; this.grid = null ; this.width = 0; this.height = 0; this.minWidth = null ; this.maxWidth = null ; this.minHeight = null ; this.maxHeight = null ; this.offset = new Phaser.Point(); this.forceLandscape = false ; this.forcePortrait = false ; this.incorrectOrientation = false ; this.pageAlignHorizontally = false ; this.pageAlignVertically = false ; this.maxIterations = 5; this.enterLandscape = new Phaser.Signal(); this.enterPortrait = new Phaser.Signal(); this.enterIncorrectOrientation = new Phaser.Signal(); this.leaveIncorrectOrientation = new Phaser.Signal(); this.fullScreenTarget = null ; 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(); this.aspectRatio = 0; this.sourceAspectRatio = 0; this.event = null ; this.fullScreenScaleMode = Phaser.ScaleManager.NO_SCALE; this.parentIsWindow = false ; this.parentNode = null ; this.parentScaleFactor = new Phaser.Point(1, 1); this.trackParentInterval = 2000; this.onResize = null ; this.onResizeContext = null ; this._scaleMode = Phaser.ScaleManager.NO_SCALE; this._width = 0; this._height = 0; this._check = null ; this._nextParentCheck = 0; this._parentBounds = null ; if (game.config) { this.parseConfig(game.config); } this.setupScale(width, height); } ; Phaser.ScaleManager.EXACT_FIT = 0; Phaser.ScaleManager.NO_SCALE = 1; Phaser.ScaleManager.SHOW_ALL = 2; Phaser.ScaleManager.RESIZE = 3; Phaser.ScaleManager.prototype = { parseConfig: function (config){ if (config.scaleMode) { this.scaleMode = config.scaleMode; } if (config.fullScreenScaleMode) { this.fullScreenScaleMode = config.fullScreenScaleMode; } if (config.fullScreenTarget) { this.fullScreenTarget = config.fullScreenTarget; } } , setupScale: function (width, height){ var target; var rect = new Phaser.Rectangle(); if (this.game.parent !== '') { if (typeof this.game.parent === 'string') { target = document.getElementById(this.game.parent); } else if (typeof this.game.parent === 'object' && this.game.parent.nodeType === 1) { target = this.game.parent; } } if (!target) { this.parentNode = null ; this.parentIsWindow = true ; rect.width = window.innerWidth; rect.height = window.innerHeight; } else { this.parentNode = target; this.parentIsWindow = false ; this._parentBounds = this.parentNode.getBoundingClientRect(); rect.width = this._parentBounds.width; rect.height = this._parentBounds.height; this.offset.set(this._parentBounds.left, this._parentBounds.top); } var newWidth = 0; var newHeight = 0; if (typeof width === 'number') { newWidth = width; } else { this.parentScaleFactor.x = parseInt(width, 10) / 100; newWidth = rect.width * this.parentScaleFactor.x; } if (typeof height === 'number') { newHeight = height; } else { this.parentScaleFactor.y = parseInt(height, 10) / 100; newHeight = rect.height * this.parentScaleFactor.y; } this.grid = new Phaser.FlexGrid(this, newWidth, newHeight); this.updateDimensions(newWidth, newHeight, false ); } , boot: function (){ this.fullScreenTarget = this.game.canvas; var _this = this; this._checkOrientation = function (event){ return _this.checkOrientation(event); } ; this._checkResize = function (event){ return _this.checkResize(event); } ; this._fullScreenChange = function (event){ return _this.fullScreenChange(event); } ; window.addEventListener('orientationchange', this._checkOrientation, false ); window.addEventListener('resize', this._checkResize, false ); if (!this.game.device.cocoonJS) { document.addEventListener('webkitfullscreenchange', this._fullScreenChange, false ); document.addEventListener('mozfullscreenchange', this._fullScreenChange, false ); document.addEventListener('fullscreenchange', this._fullScreenChange, false ); } this.updateDimensions(this.width, this.height, true ); Phaser.Canvas.getOffset(this.game.canvas, this.offset); this.bounds.setTo(this.offset.x, this.offset.y, this.width, this.height); } , setResizeCallback: function (callback, context){ this.onResize = callback; this.onResizeContext = context; } , 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; } } , preUpdate: function (){ if (this.game.time.now < this._nextParentCheck) { return ; } if (!this.parentIsWindow) { Phaser.Canvas.getOffset(this.game.canvas, this.offset); if (this._scaleMode === Phaser.ScaleManager.RESIZE) { this._parentBounds = this.parentNode.getBoundingClientRect(); if (this._parentBounds.width !== this.width || this._parentBounds.height !== this.height) { this.updateDimensions(this._parentBounds.width, this._parentBounds.height, true ); } } } this._nextParentCheck = this.game.time.now + this.trackParentInterval; } , 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.bounds.width = this.width; this.bounds.height = this.height; if (resize) { this.game.renderer.resize(this.width, this.height); this.game.camera.setSize(this.width, this.height); this.game.world.resize(this.width, this.height); } this.grid.onResize(width, height); if (this.onResize) { this.onResize.call(this.onResizeContext, this.width, this.height); } this.game.state.resize(width, height); } , forceOrientation: function (forceLandscape, forcePortrait){ if (typeof forcePortrait === 'undefined') { forcePortrait = false ; } this.forceLandscape = forceLandscape; this.forcePortrait = forcePortrait; } , 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.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.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; var wasLandscape = this.isLandscape; if (window.outerWidth > window.outerHeight) { this.orientation = 90; } else { this.orientation = 0; } if (wasLandscape && this.isPortrait) { this.enterPortrait.dispatch(this.orientation, false , true ); if (this.forceLandscape) { this.enterIncorrectOrientation.dispatch(); } else if (this.forcePortrait) { this.leaveIncorrectOrientation.dispatch(); } } else if (!wasLandscape && this.isLandscape) { this.enterLandscape.dispatch(this.orientation, true , false ); if (this.forceLandscape) { this.leaveIncorrectOrientation.dispatch(); } else if (this.forcePortrait) { this.enterIncorrectOrientation.dispatch(); } } if (this._scaleMode === Phaser.ScaleManager.RESIZE && this.parentIsWindow) { this.updateDimensions(window.innerWidth, window.innerHeight, true ); } else if (this._scaleMode === Phaser.ScaleManager.EXACT_FIT || this._scaleMode === Phaser.ScaleManager.SHOW_ALL) { _AN_Call_refresh('refresh', this); this.checkOrientationState(); if (this.onResize) { this.onResize.call(this.onResizeContext, this.width, this.height); } } } , refresh: function (){ if (this.scaleMode === Phaser.ScaleManager.RESIZE) { return ; } 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 (this.scaleMode === Phaser.ScaleManager.RESIZE) { return ; } 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 || 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.offset); this.bounds.setTo(this.offset.x, this.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.checkOrientationState(); } , reset: function (clearWorld){ if (clearWorld) { this.grid.reset(); } } , 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; } } , 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); } } , destroy: function (){ window.removeEventListener('orientationchange', this._checkOrientation, false ); window.removeEventListener('resize', this._checkResize, false ); if (!this.game.device.cocoonJS) { document.removeEventListener('webkitfullscreenchange', this._fullScreenChange, false ); document.removeEventListener('mozfullscreenchange', this._fullScreenChange, false ); document.removeEventListener('fullscreenchange', this._fullScreenChange, false ); } } } ; Phaser.ScaleManager.prototype.constructor = Phaser.ScaleManager; Object.defineProperty(Phaser.ScaleManager.prototype, "scaleMode", { get: function (){ return this._scaleMode; } , set: function (value){ if (value !== this._scaleMode) { this._scaleMode = value; } } } ); 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); } } );