1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 Copyright (c) 2008-2010 Ricardo Quesada 4 Copyright (c) 2011 Zynga Inc. 5 6 http://www.cocos2d-x.org 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ****************************************************************************/ 26 27 /** 28 * The fullscreen API provides an easy way for web content to be presented using the user's entire screen. 29 * It's invalid on safari,QQbrowser and android browser 30 * @class 31 * @extends cc.Class 32 */ 33 cc.Screen = cc.Class.extend({ 34 _supportsFullScreen: false, 35 // the pre fullscreenchange function 36 _preOnFullScreenChange: null, 37 _touchEvent: "", 38 _fn: null, 39 // Function mapping for cross browser support 40 _fnMap: [ 41 [ 42 'requestFullscreen', 43 'exitFullscreen', 44 'fullscreenchange', 45 'fullscreenEnabled', 46 'fullscreenElement' 47 ], 48 [ 49 'requestFullScreen', 50 'exitFullScreen', 51 'fullScreenchange', 52 'fullScreenEnabled', 53 'fullScreenElement' 54 ], 55 [ 56 'webkitRequestFullScreen', 57 'webkitCancelFullScreen', 58 'webkitfullscreenchange', 59 'webkitIsFullScreen', 60 'webkitCurrentFullScreenElement' 61 ], 62 [ 63 'mozRequestFullScreen', 64 'mozCancelFullScreen', 65 'mozfullscreenchange', 66 'mozFullScreen', 67 'mozFullScreenElement' 68 ], 69 [ 70 'msRequestFullscreen', 71 'msExitFullscreen', 72 'MSFullscreenChange', 73 'msFullscreenEnabled', 74 'msFullscreenElement' 75 ] 76 ], 77 78 init: function () { 79 this._fn = {}; 80 var i, val, map = this._fnMap, valL; 81 for (i = 0, l = map.length; i < l; i++ ) { 82 val = map[ i ]; 83 if ( val && val[1] in document ) { 84 for ( i = 0, valL = val.length; i < valL; i++ ) { 85 this._fn[ map[0][ i ] ] = val[ i ]; 86 } 87 break; 88 } 89 } 90 91 this._supportsFullScreen = (this._fn.requestFullscreen != undefined); 92 this._touchEvent = ('ontouchstart' in window) ? 'touchstart' : 'mousedown'; 93 }, 94 95 /** 96 * return true if it's full now. 97 * @returns {Boolean} 98 */ 99 fullScreen: function() { 100 return this._supportsFullScreen && document[ this._fn.fullscreenEnabled ]; 101 }, 102 103 /** 104 * change the screen to full mode. 105 * @param {Element} element 106 * @param {Function} onFullScreenChange 107 * @returns {*} 108 */ 109 requestFullScreen: function (element, onFullScreenChange) { 110 if (!this._supportsFullScreen) return; 111 112 element = element || document.documentElement; 113 element[ this._fn.requestFullscreen ](); 114 115 if (onFullScreenChange) { 116 var eventName = this._fn.fullscreenchange; 117 if (this._preOnFullScreenChange) 118 document.removeEventListener(eventName, this._preOnFullScreenChange); 119 this._preOnFullScreenChange = onFullScreenChange; 120 document.addEventListener(eventName, onFullScreenChange, false); 121 } 122 123 return element[ this._fn.requestFullscreen ](); 124 }, 125 126 /** 127 * exit the full mode. 128 * @returns {*} 129 */ 130 exitFullScreen: function () { 131 return this._supportsFullScreen ? document[ this._fn.exitFullscreen ]() : true; 132 }, 133 134 /** 135 * Automatically request full screen with a touch/click event 136 * @param {Element} element 137 * @param {Function} onFullScreenChange 138 */ 139 autoFullScreen: function (element, onFullScreenChange) { 140 element = element || document.body; 141 var touchTarget = cc.canvas || element; 142 var theScreen = this; 143 // Function bind will be too complicated here because we need the callback function's reference to remove the listener 144 function callback() { 145 theScreen.requestFullScreen(element, onFullScreenChange); 146 touchTarget.removeEventListener(theScreen._touchEvent, callback); 147 } 148 this.requestFullScreen(element, onFullScreenChange); 149 touchTarget.addEventListener(this._touchEvent, callback); 150 } 151 }); 152 153 /** 154 * returns a shared instance of the cc.Screen 155 * @function 156 * @return {cc.Screen} 157 */ 158 cc.Screen.getInstance = function () { 159 if (!this._instance){ 160 var screen = new cc.Screen(); 161 screen.init(); 162 this._instance = screen; 163 } 164 return this._instance; 165 }; 166