boot.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. (function () {
  2. 'use strict';
  3. // init device resolutions
  4. var devices = [
  5. { name: 'Apple iPhone 5', width: 320, height: 568, ratio: 2 },
  6. { name: 'Apple iPhone 6', width: 375, height: 667, ratio: 2 },
  7. { name: 'Apple iPhone 6 Plus', width: 414, height: 736, ratio: 3 },
  8. { name: 'Apple iPhone 7', width: 375, height: 667, ratio: 2 },
  9. { name: 'Apple iPhone 7 Plus', width: 414, height: 736, ratio: 3 },
  10. { name: 'Apple iPhone X', width: 375, height: 812, ratio: 3 },
  11. { name: 'Apple iPad', width: 1024, height: 768, ratio: 2 },
  12. { name: 'Apple iPad Air 2', width: 768, height: 1024, ratio: 2 },
  13. { name: 'Apple iPad Pro 10.5-inch', width: 834, height: 1112, ratio: 2 },
  14. { name: 'Apple iPad Pro 12.9-inch', width: 1024, height: 1366, ratio: 2 },
  15. { name: 'Huawei P9', width: 540, height: 960, ratio: 2 },
  16. { name: 'Huawei Mate9 Pro', width: 720, height: 1280, ratio: 2 },
  17. { name: 'Google Nexus 5', width: 360, height: 640, ratio: 3 },
  18. { name: 'Google Nexus 5X', width: 411, height: 731, ratio: 2.625 },
  19. { name: 'Google Nexus 6', width: 412, height: 732, ratio: 3.5 },
  20. { name: 'Google Nexus 7', width: 960, height: 600, ratio: 2 },
  21. ];
  22. function setCSSChecked (element, checked) {
  23. if (checked) {
  24. element.classList.add('checked');
  25. }
  26. else {
  27. element.classList.remove('checked');
  28. }
  29. return checked;
  30. }
  31. function refreshPauseBtnState () {
  32. if (cc.game.isPaused()) {
  33. btnPause.style.borderTopRightRadius = '0';
  34. btnPause.style.borderBottomRightRadius = '0';
  35. btnStep.style.borderTopLeftRadius = '0';
  36. btnStep.style.borderBottomLeftRadius = '0';
  37. btnStep.style.display = '';
  38. setCSSChecked(btnPause, true);
  39. }
  40. else {
  41. btnPause.style.borderTopRightRadius = '';
  42. btnPause.style.borderBottomRightRadius = '';
  43. btnStep.style.borderTopLeftRadius = '';
  44. btnStep.style.borderBottomLeftRadius = '';
  45. btnStep.style.display = 'none';
  46. setCSSChecked(btnPause, false);
  47. }
  48. }
  49. var isMobile = function () {
  50. var check = false;
  51. (function(a){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4)))check = true;})(navigator.userAgent||navigator.vendor||window.opera);
  52. return check;
  53. };
  54. // init toolbar
  55. // =======================
  56. var designWidth = _CCSettings.designWidth;
  57. var designHeight = _CCSettings.designHeight;
  58. var rotated = false;
  59. // var paused = false;
  60. var canvas = document.getElementById('GameCanvas');
  61. var btnRotate = document.getElementById('btn-rotate');
  62. var optsDevice = document.getElementById('opts-device');
  63. var btnPause = document.getElementById('btn-pause');
  64. var btnStep = document.getElementById('btn-step');
  65. var optsDebugMode = document.getElementById('opts-debug-mode');
  66. var btnShowFPS = document.getElementById('btn-show-fps');
  67. var inputSetFPS = document.getElementById('input-set-fps');
  68. var btnRecompile = document.getElementById('btn-recompile');
  69. devices.forEach( function ( info, idx ) {
  70. var opt = document.createElement('option');
  71. opt.value = idx+1;
  72. opt.text = info.name;
  73. optsDevice.add( opt, null );
  74. });
  75. // coockie
  76. // =======================
  77. function setCookie (name, value, days) {
  78. days = days || 30; //cookie will be saved for 30 days
  79. var expires = new Date();
  80. expires.setTime(expires.getTime() + days*24*60*60*1000);
  81. document.cookie = name + '='+ encodeURIComponent(value) + ';expires=' + expires.toGMTString();
  82. }
  83. function getCookie (name) {
  84. var arr = document.cookie.match(new RegExp('(^| )' + name + '=([^;]*)(;|$)'));
  85. if(arr !== null) return (arr[2]);
  86. return null;
  87. }
  88. function isFullScreen () {
  89. var toolbar = document.getElementsByClassName('toolbar')[0];
  90. return getComputedStyle(toolbar).display === 'none';
  91. }
  92. function getEmulatedScreenSize () {
  93. var w, h;
  94. var idx = optsDevice.value;
  95. if ( idx === '0' ) {
  96. w = designWidth;
  97. h = designHeight;
  98. }
  99. else {
  100. var info = devices[parseInt(idx) - 1];
  101. w = info.width;
  102. h = info.height;
  103. }
  104. return {
  105. width: rotated ? h : w,
  106. height: rotated ? w : h
  107. };
  108. }
  109. function showSplash () {
  110. var LOGO_IMG_L_W = 416;
  111. var LOGO_IMG_L_H = 87;
  112. var LOGO_SIZE = 0.4;
  113. var size = isFullScreen() ? document.documentElement.getBoundingClientRect() : getEmulatedScreenSize();
  114. var splash = document.getElementById('splash');
  115. var progressBar = splash.querySelector('.progress-bar span');
  116. splash.style.width = size.width + 'px';
  117. splash.style.height = size.height + 'px';
  118. var marginTop;
  119. if (size.width < size.height) {
  120. // portrait
  121. splash.style.backgroundImage = 'url("app/editor/static/img/logo_portrait.png")';
  122. splash.style.backgroundSize = '30%';
  123. marginTop = (size.height - size.width * (1 - LOGO_SIZE)) / 2;
  124. }
  125. else {
  126. var logoDisplayH = size.width * LOGO_SIZE / LOGO_IMG_L_W * LOGO_IMG_L_H;
  127. marginTop = logoDisplayH / 2 * 1.47;
  128. }
  129. progressBar.parentElement.style.marginTop = marginTop + 'px';
  130. splash.style.display = '';
  131. progressBar.style.width = '0%';
  132. var div = document.getElementById('GameDiv');
  133. if (div) {
  134. div.style.visibility = 'visible';
  135. }
  136. if ( !isMobile() ) {
  137. // make the splash screen in center
  138. canvas.width = size.width;
  139. canvas.height = size.height;
  140. }
  141. }
  142. // init options
  143. function initPreviewOptions () {
  144. var defaultDevice = getCookie('device');
  145. var defaultRotate = getCookie('rotate');
  146. var hasDefaultDevice = defaultDevice !== null;
  147. var hasDefaultRotate = defaultRotate !== null;
  148. if (hasDefaultDevice) {
  149. optsDevice.value = parseInt(defaultDevice);
  150. }
  151. if (hasDefaultRotate && defaultRotate === 'true') {
  152. rotated = !rotated;
  153. setCSSChecked(btnRotate, rotated);
  154. }
  155. optsDebugMode.value = getCookie('debugMode') || '1';
  156. setCSSChecked(btnShowFPS, getCookie('showFPS') === 'true');
  157. inputSetFPS.value = '60';
  158. showSplash();
  159. }
  160. initPreviewOptions();
  161. window.onload = function () {
  162. if (window.__quick_compile_engine__) {
  163. window.__quick_compile_engine__.load(onload);
  164. }
  165. else {
  166. onload();
  167. }
  168. };
  169. function onload () {
  170. // socket
  171. // =======================
  172. // jshint camelcase:false
  173. var socket = window.__socket_io__();
  174. socket.on('browser:reload', function () {
  175. window.location.reload();
  176. });
  177. socket.on('browser:confirm-reload', function () {
  178. var r = confirm( 'Reload?' );
  179. if ( r ) {
  180. window.location.reload();
  181. }
  182. });
  183. function updateResolution () {
  184. var size = isFullScreen() ? document.documentElement.getBoundingClientRect() : getEmulatedScreenSize();
  185. var gameDiv = document.getElementById('GameDiv');
  186. gameDiv.style.width = size.width + 'px';
  187. gameDiv.style.height = size.height + 'px';
  188. cc.view.setCanvasSize(size.width, size.height);
  189. }
  190. // init rotate button
  191. btnRotate.addEventListener('click', function () {
  192. rotated = !rotated;
  193. setCSSChecked(btnRotate, rotated);
  194. setCookie('rotate', rotated.toString());
  195. updateResolution();
  196. });
  197. optsDevice.addEventListener( 'change', function () {
  198. var idx = optsDevice.value;
  199. setCookie('device', idx.toString());
  200. updateResolution();
  201. });
  202. // init debug modes
  203. optsDebugMode.addEventListener('change', function (event) {
  204. var value = event.target.value;
  205. setCookie('debugMode', value);
  206. cc.debug._resetDebugSetting(parseInt(value));
  207. });
  208. // init pause button
  209. btnPause.addEventListener('click', function () {
  210. var shouldPause = !cc.game.isPaused();
  211. if (shouldPause) {
  212. cc.game.pause();
  213. }
  214. else {
  215. cc.game.resume();
  216. }
  217. refreshPauseBtnState();
  218. });
  219. // init recompile button
  220. btnRecompile.addEventListener('click', function () {
  221. var url = window.location.href + 'update-db';
  222. var xmlHttp = new XMLHttpRequest();
  223. xmlHttp.onreadystatechange = function () {
  224. if (xmlHttp.readyState === 4 && xmlHttp.status === 200) {
  225. document.getElementById('recompiling').style.display = 'block';
  226. }
  227. };
  228. xmlHttp.open("GET", url, true); // true for asynchronous
  229. xmlHttp.send(null);
  230. });
  231. // init step button
  232. btnStep.addEventListener('click', function () {
  233. cc.game.step();
  234. });
  235. // init show fps, true by default
  236. btnShowFPS.addEventListener('click', function () {
  237. var show = !cc.debug.isDisplayStats();
  238. cc.debug.setDisplayStats(show);
  239. setCSSChecked(btnShowFPS, show);
  240. setCookie('showFPS', show.toString());
  241. });
  242. // init set fps
  243. inputSetFPS.addEventListener('change', function (event) {
  244. var fps = parseInt(inputSetFPS.value);
  245. if (isNaN(fps)) {
  246. fps = 60;
  247. inputSetFPS.value = fps.toString();
  248. }
  249. cc.game.setFrameRate(fps);
  250. });
  251. // init engine
  252. // =======================
  253. var engineInited = false;
  254. if (isFullScreen()) {
  255. window.addEventListener('resize', updateResolution);
  256. }
  257. var AssetOptions = {
  258. libraryPath: 'res/import',
  259. rawAssetsBase: 'res/raw-',
  260. rawAssets: _CCSettings.rawAssets
  261. };
  262. // jsList
  263. var jsList = _CCSettings.jsList || [];
  264. jsList = jsList.map(function (x) { return AssetOptions.rawAssetsBase + x; });
  265. if (_CCSettings.jsBundleForWebPreview) {
  266. jsList.push(_CCSettings.jsBundleForWebPreview);
  267. }
  268. let showFPS = getCookie('showFPS');
  269. // FPS is on by default
  270. showFPS = showFPS === null ? true : showFPS === 'true';
  271. var option = {
  272. id: canvas,
  273. scenes: _CCSettings.scenes,
  274. debugMode: parseInt(optsDebugMode.value),
  275. showFPS: showFPS,
  276. frameRate: parseInt(inputSetFPS.value),
  277. groupList: _CCSettings.groupList,
  278. collisionMatrix: _CCSettings.collisionMatrix,
  279. jsList: jsList
  280. // rawUrl: _CCSettings.rawUrl
  281. };
  282. cc.AssetLibrary.init(AssetOptions);
  283. cc.game.run(option, function () {
  284. // resize canvas
  285. if (!isFullScreen()) {
  286. updateResolution();
  287. }
  288. cc.view.enableRetina(true);
  289. cc.view.resizeWithBrowserSize(true);
  290. // Loading splash scene
  291. var splash = document.getElementById('splash');
  292. var progressBar = splash.querySelector('.progress-bar span');
  293. showSplash();
  294. cc.director.once(cc.Director.EVENT_AFTER_SCENE_LAUNCH, function () {
  295. splash.style.display = 'none';
  296. checkEmptyScene();
  297. });
  298. cc.game.pause();
  299. // init assets
  300. engineInited = true;
  301. // load stashed scene
  302. cc.loader.load('preview-scene.json', function (error, json) {
  303. if (error) {
  304. cc.error(error.stack);
  305. return;
  306. }
  307. cc.loader.onProgress = function (completedCount, totalCount, item) {
  308. var percent = 100 * completedCount / totalCount;
  309. if (progressBar) {
  310. progressBar.style.width = percent.toFixed(2) + '%';
  311. }
  312. };
  313. cc.AssetLibrary.loadJson(json,
  314. function (err, sceneAsset) {
  315. if (err) {
  316. cc.error(err.stack);
  317. return;
  318. }
  319. var scene = sceneAsset.scene;
  320. scene._name = sceneAsset._name;
  321. cc.director.runSceneImmediate(scene, function () {
  322. // play game
  323. cc.game.resume();
  324. });
  325. cc.loader.onProgress = null;
  326. }
  327. );
  328. });
  329. // purge
  330. //noinspection JSUnresolvedVariable
  331. _CCSettings = undefined;
  332. });
  333. }
  334. function checkEmptyScene () {
  335. var scene = cc.director.getScene();
  336. if (scene) {
  337. if (scene.children.length > 1) {
  338. return;
  339. }
  340. if (scene.children.length === 1) {
  341. var node = scene.children[0];
  342. if (node.children.length > 0) {
  343. return;
  344. }
  345. if (node._components.length > 1) {
  346. return;
  347. }
  348. if (node._components.length > 0 && !(node._components[0] instanceof cc.Canvas)) {
  349. return;
  350. }
  351. }
  352. }
  353. document.getElementById('bulletin').style.display = 'block';
  354. document.getElementById('sceneIsEmpty').style.display = 'block';
  355. }
  356. })();