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 _browserPrefix: "", 36 _preElement: null,//the pre element to show in full screen mode. 37 _preOnFullScreenChange: null,//the pre fullscreenchange function 38 _touchEvent: "", 39 init: function () { 40 var browserPres = 'webkit,moz,o,ms,khtml'.split(','); 41 var body = document.body; 42 if (body["requestFullScreen"]) { 43 this._supportsFullScreen = true; 44 } else { 45 for (var i = 0, il = browserPres.length, prefix; i < il; i++) { 46 prefix = browserPres[i]; 47 if (body[prefix + "RequestFullScreen"]) { 48 this._supportsFullScreen = true; 49 this._browserPrefix = prefix; 50 break; 51 } 52 } 53 } 54 55 this._touchEvent = ('ontouchstart' in window) ? 'touchstart' : 'mousedown'; 56 }, 57 58 /** 59 * return true if it's full now. 60 * @returns {Boolean} 61 */ 62 fullScreen: function () { 63 var d = document; 64 if (this._supportsFullScreen) { 65 switch (this._browserPrefix) { 66 case '': 67 return d["fullScreen"]; 68 case 'webkit': 69 return d["webkitIsFullScreen"]; 70 default: 71 return d[this._browserPrefix + 'FullScreen']; 72 } 73 } 74 return false; 75 }, 76 77 /** 78 * change the screen to full mode. 79 * @param {Element} element 80 * @param {Function} onFullScreenChange 81 * @returns {*} 82 */ 83 requestFullScreen: function (element, onFullScreenChange) { 84 if (!this._supportsFullScreen || this.fullScreen()) return; 85 if (onFullScreenChange) { 86 var eventName = this._browserPrefix + "fullscreenchange"; 87 if (this._preElement && this._preOnFullScreenChange) this._preElement.removeEventListener(eventName, this._preOnFullScreenChange); 88 this._preElement = element; 89 this._preOnFullScreenChange = onFullScreenChange; 90 element.addEventListener(eventName, onFullScreenChange, false); 91 } 92 return (this._browserPrefix === '') ? element["requestFullScreen"]() : element[this._browserPrefix + 'RequestFullScreen'](); 93 }, 94 95 /** 96 * exit the full mode. 97 * @returns {*} 98 */ 99 exitFullScreen: function () { 100 if (!this._supportsFullScreen || !this.fullScreen()) return; 101 return (this._browserPrefix === '') ? document.body["cancelFullScreen"]() : document.body[this._browserPrefix + 'CancelFullScreen'](); 102 }, 103 104 /** 105 * Automatically request full screen with a touch/click event 106 */ 107 autoFullScreen: function (element, onFullScreenChange) { 108 var theScreen = this; 109 // Function bind will be too complicated here because we need the callback function's reference to remove the listener 110 function callback() { 111 theScreen.requestFullScreen(element, onFullScreenChange); 112 element.removeEventListener(theScreen._touchEvent, callback); 113 } 114 this.requestFullScreen(element, onFullScreenChange); 115 element.addEventListener(this._touchEvent, callback); 116 } 117 }); 118 119 /** 120 * returns a shared instance of the cc.Screen 121 * @function 122 * @return {cc.Screen} 123 */ 124 cc.Screen.getInstance = function () { 125 if (!this._instance){ 126 var screen = new cc.Screen(); 127 screen.init(); 128 this._instance = screen; 129 } 130 return this._instance; 131 }; 132