CCScreen.js 5.0 KB

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