index.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. var jsEditor;
  2. (function () {
  3. var fontSize = 14;
  4. var splitInstance = Split(['#jsEditor', '#canvasZone']);
  5. var elementToTheme = [
  6. '.wrapper .gutter',
  7. '.wrapper #jsEditor',
  8. '.navbar',
  9. '.navbar .select .toDisplay .option',
  10. '.navbar .select .toDisplayBig',
  11. '.navbar .select .toDisplayBig a',
  12. '.navbar .select .toDisplayBig ul li',
  13. '.navbarBottom',
  14. '.navbarBottom .links .link',
  15. '.save-message'];
  16. var run = function () {
  17. var blockEditorChange = false;
  18. jsEditor.onKeyDown(function (evt) {
  19. });
  20. jsEditor.onKeyUp(function (evt) {
  21. if (blockEditorChange) {
  22. return;
  23. }
  24. document.getElementById("currentScript").innerHTML = "Custom";
  25. document.getElementById('safemodeToggle').classList.add('checked');
  26. });
  27. var snippetUrl = "https://babylonjs-api2.azurewebsites.net/snippets";
  28. var currentSnippetToken;
  29. var currentSnippetTitle = null;
  30. var currentSnippetDescription = null;
  31. var currentSnippetTags = null;
  32. var engine;
  33. var fpsLabel = document.getElementById("fpsLabel");
  34. var scripts;
  35. var zipCode;
  36. BABYLON.Engine.ShadersRepository = "/src/Shaders/";
  37. var currentVersionElement = document.getElementById("currentVersion");
  38. if (currentVersionElement) {
  39. switch (BABYLON.Engine.Version) {
  40. case "2.5":
  41. currentVersionElement.innerHTML = "Version: " + BABYLON.Engine.Version;
  42. break;
  43. default:
  44. currentVersionElement.innerHTML = "Version: Latest";
  45. break;
  46. }
  47. }
  48. var loadScript = function (scriptURL, title) {
  49. var xhr = new XMLHttpRequest();
  50. xhr.open('GET', scriptURL, true);
  51. xhr.onreadystatechange = function () {
  52. if (xhr.readyState === 4) {
  53. if (xhr.status === 200) {
  54. blockEditorChange = true;
  55. jsEditor.setValue(xhr.responseText);
  56. jsEditor.setPosition({ lineNumber: 0, column: 0 });
  57. blockEditorChange = false;
  58. compileAndRun();
  59. document.getElementById("currentScript").innerHTML = title;
  60. currentSnippetToken = null;
  61. }
  62. }
  63. };
  64. xhr.send(null);
  65. };
  66. var loadScriptFromIndex = function (index) {
  67. if (index === 0) {
  68. index = 1;
  69. }
  70. var script = scripts[index - 1].trim();
  71. loadScript("scripts/" + script + ".js", script);
  72. }
  73. var onScriptClick = function (evt) {
  74. loadScriptFromIndex(evt.target.scriptLinkIndex);
  75. };
  76. var loadScriptsList = function () {
  77. var xhr = new XMLHttpRequest();
  78. xhr.open('GET', 'scripts/scripts.txt', true);
  79. xhr.onreadystatechange = function () {
  80. if (xhr.readyState === 4) {
  81. if (xhr.status === 200) {
  82. scripts = xhr.responseText.split("\n");
  83. var ul = document.getElementById("scriptsList");
  84. var index;
  85. for (index = 0; index < scripts.length; index++) {
  86. var option = document.createElement("li");
  87. var a = document.createElement("a");
  88. a.href = "#";
  89. a.innerHTML = (index + 1) + " - " + scripts[index];
  90. a.scriptLinkIndex = index + 1;
  91. a.onclick = onScriptClick;
  92. option.scriptLinkIndex = index + 1;
  93. option.onclick = onScriptClick;
  94. option.appendChild(a);
  95. ul.appendChild(option);
  96. }
  97. if (!location.hash) {
  98. // Query string
  99. var queryString = window.location.search;
  100. if (queryString) {
  101. var query = queryString.replace("?", "");
  102. index = parseInt(query);
  103. if (!isNaN(index)) {
  104. loadScriptFromIndex(index);
  105. } else {
  106. loadScript("scripts/" + query + ".js", query);
  107. }
  108. } else {
  109. loadScript("scripts/basic scene.js", "Basic scene");
  110. }
  111. }
  112. // Restore theme
  113. var theme = localStorage.getItem("bjs-playground-theme") || 'light';
  114. toggleTheme(theme);
  115. // Remove editor if window size is less than 850px
  116. var removeEditorForSmallScreen = function () {
  117. if (mq.matches) {
  118. splitInstance.collapse(0);
  119. } else {
  120. splitInstance.setSizes([50, 50]);
  121. }
  122. }
  123. var mq = window.matchMedia("(max-width: 850px)");
  124. mq.addListener(removeEditorForSmallScreen);
  125. }
  126. }
  127. };
  128. xhr.send(null);
  129. }
  130. var createNewScript = function () {
  131. location.hash = "";
  132. currentSnippetToken = null;
  133. currentSnippetTitle = null;
  134. currentSnippetDescription = null;
  135. currentSnippetTags = null;
  136. showNoMetadata();
  137. jsEditor.setValue('// You have to create a function called createScene. This function must return a BABYLON.Scene object\r\n// You can reference the following variables: scene, canvas\r\n// You must at least define a camera\r\n// More info here: https://doc.babylonjs.com/generals/The_Playground_Tutorial\r\n\r\nvar createScene = function() {\r\n\tvar scene = new BABYLON.Scene(engine);\r\n\tvar camera = new BABYLON.ArcRotateCamera("Camera", 0, Math.PI / 2, 12, BABYLON.Vector3.Zero(), scene);\r\n\tcamera.attachControl(canvas, true);\r\n\r\n\r\n\r\n\treturn scene;\r\n};');
  138. jsEditor.setPosition({ lineNumber: 11, column: 0 });
  139. jsEditor.focus();
  140. compileAndRun();
  141. }
  142. var clear = function () {
  143. location.hash = "";
  144. currentSnippetToken = null;
  145. jsEditor.setValue('');
  146. jsEditor.setPosition({ lineNumber: 0, column: 0 });
  147. jsEditor.focus();
  148. }
  149. var showError = function (errorMessage, errorEvent) {
  150. var errorContent =
  151. '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button>';
  152. if (errorEvent) {
  153. var regEx = /\(.+:(\d+):(\d+)\)\n/g;
  154. var match = regEx.exec(errorEvent.stack);
  155. if (match) {
  156. errorContent += "Line ";
  157. var lineNumber = match[1];
  158. var columnNumber = match[2];
  159. errorContent += lineNumber + ':' + columnNumber + ' - ';
  160. }
  161. }
  162. errorContent += errorMessage + '</div>';
  163. document.getElementById("errorZone").style.display = 'block';
  164. document.getElementById("errorZone").innerHTML = errorContent;
  165. // Close button error
  166. document.getElementById("errorZone").querySelector('.close').addEventListener('click', function () {
  167. document.getElementById("errorZone").style.display = 'none';
  168. });
  169. }
  170. var showNoMetadata = function () {
  171. if (currentSnippetTitle) {
  172. document.getElementById("saveFormTitle").value = currentSnippetTitle;
  173. document.getElementById("saveFormTitle").readOnly = true;
  174. }
  175. else {
  176. document.getElementById("saveFormTitle").value = '';
  177. document.getElementById("saveFormTitle").readOnly = false;
  178. }
  179. if (currentSnippetDescription) {
  180. document.getElementById("saveFormDescription").value = currentSnippetDescription;
  181. document.getElementById("saveFormDescription").readOnly = true;
  182. }
  183. else {
  184. document.getElementById("saveFormDescription").value = '';
  185. document.getElementById("saveFormDescription").readOnly = false;
  186. }
  187. if (currentSnippetTags) {
  188. document.getElementById("saveFormTags").value = currentSnippetTags;
  189. document.getElementById("saveFormTags").readOnly = true;
  190. }
  191. else {
  192. document.getElementById("saveFormTags").value = '';
  193. document.getElementById("saveFormTags").readOnly = false;
  194. }
  195. document.getElementById("saveFormButtons").style.display = "block";
  196. document.getElementById("saveFormButtonOk").style.display = "inline-block";
  197. document.getElementById("saveMessage").style.display = "block";
  198. // document.getElementById("metadataButton").style.display = "none";
  199. };
  200. showNoMetadata();
  201. document.getElementById("saveMessage").style.display = "none";
  202. var hideNoMetadata = function () {
  203. document.getElementById("saveFormTitle").readOnly = true;
  204. document.getElementById("saveFormDescription").readOnly = true;
  205. document.getElementById("saveFormTags").readOnly = true;
  206. document.getElementById("saveFormButtonOk").style.display = "none";
  207. document.getElementById("saveMessage").style.display = "none";
  208. document.getElementById("metadataButton").style.display = "inline-block";
  209. };
  210. compileAndRun = function () {
  211. try {
  212. if (!BABYLON.Engine.isSupported()) {
  213. showError("Your browser does not support WebGL", null);
  214. return;
  215. }
  216. if (engine) {
  217. engine.dispose();
  218. engine = null;
  219. }
  220. var canvas = document.getElementById("renderCanvas");
  221. engine = new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true });
  222. document.getElementById("errorZone").style.display = 'none';
  223. document.getElementById("errorZone").innerHTML = "";
  224. document.getElementById("statusBar").innerHTML = "Loading assets...Please wait";
  225. engine.runRenderLoop(function () {
  226. if (engine.scenes.length === 0) {
  227. return;
  228. }
  229. if (canvas.width !== canvas.clientWidth) {
  230. engine.resize();
  231. }
  232. var scene = engine.scenes[0];
  233. if (scene.activeCamera || scene.activeCameras.length > 0) {
  234. scene.render();
  235. }
  236. fpsLabel.innerHTML = engine.getFps().toFixed() + " fps";
  237. });
  238. var code = jsEditor.getValue();
  239. var scene;
  240. if (code.indexOf("createScene") !== -1) { // createScene
  241. eval(code);
  242. scene = createScene();
  243. if (!scene) {
  244. showError("createScene function must return a scene.", null);
  245. return;
  246. }
  247. zipCode = code + "\r\n\r\nvar scene = createScene();";
  248. } else if (code.indexOf("CreateScene") !== -1) { // CreateScene
  249. eval(code);
  250. scene = CreateScene();
  251. if (!scene) {
  252. showError("CreateScene function must return a scene.", null);
  253. return;
  254. }
  255. zipCode = code + "\r\n\r\nvar scene = CreateScene();";
  256. } else if (code.indexOf("createscene") !== -1) { // createscene
  257. eval(code);
  258. scene = createscene();
  259. if (!scene) {
  260. showError("createscene function must return a scene.", null);
  261. return;
  262. }
  263. zipCode = code + "\r\n\r\nvar scene = createscene();";
  264. } else { // Direct code
  265. scene = new BABYLON.Scene(engine);
  266. eval("runScript = function(scene, canvas) {" + code + "}");
  267. runScript(scene, canvas);
  268. zipCode = "var scene = new BABYLON.Scene(engine);\r\n\r\n" + code;
  269. }
  270. if (engine.scenes.length === 0) {
  271. showError("You must at least create a scene.", null);
  272. return;
  273. }
  274. if (engine.scenes[0].activeCamera == null) {
  275. showError("You must at least create a camera.", null);
  276. return;
  277. }
  278. engine.scenes[0].executeWhenReady(function () {
  279. document.getElementById("statusBar").innerHTML = "";
  280. });
  281. } catch (e) {
  282. showError(e.message, e);
  283. }
  284. };
  285. window.addEventListener("resize",
  286. function () {
  287. if (engine) {
  288. engine.resize();
  289. }
  290. });
  291. // Load scripts list
  292. loadScriptsList();
  293. // Zip
  294. var addContentToZip = function (zip, name, url, replace, buffer, then) {
  295. var xhr = new XMLHttpRequest();
  296. xhr.open('GET', url, true);
  297. if (buffer) {
  298. xhr.responseType = "arraybuffer";
  299. }
  300. xhr.onreadystatechange = function () {
  301. if (xhr.readyState === 4) {
  302. if (xhr.status === 200) {
  303. var text;
  304. if (!buffer) {
  305. if (replace) {
  306. var splits = replace.split("\r\n");
  307. for (var index = 0; index < splits.length; index++) {
  308. splits[index] = " " + splits[index];
  309. }
  310. replace = splits.join("\r\n");
  311. text = xhr.responseText.replace("####INJECT####", replace);
  312. } else {
  313. text = xhr.responseText;
  314. }
  315. }
  316. zip.file(name, buffer ? xhr.response : text);
  317. then();
  318. }
  319. }
  320. };
  321. xhr.send(null);
  322. }
  323. var addTexturesToZip = function (zip, index, textures, folder, then) {
  324. if (index === textures.length) {
  325. then();
  326. return;
  327. }
  328. if (textures[index].isRenderTarget || textures[index] instanceof BABYLON.DynamicTexture) {
  329. addTexturesToZip(zip, index + 1, textures, folder, then);
  330. return;
  331. }
  332. if (textures[index].isCube) {
  333. if (textures[index]._extensions) {
  334. for (var i = 0; i < 6; i++) {
  335. textures.push({ name: textures[index].name + textures[index]._extensions[i] });
  336. }
  337. }
  338. else {
  339. textures.push({ name: textures[index].name });
  340. }
  341. addTexturesToZip(zip, index + 1, textures, folder, then);
  342. return;
  343. }
  344. if (folder == null) {
  345. folder = zip.folder("textures");
  346. }
  347. var url;
  348. if (textures[index].video) {
  349. url = textures[index].video.currentSrc;
  350. } else {
  351. // url = textures[index].name;
  352. url = textures[index].url;
  353. }
  354. var name = textures[index].name;
  355. // var name = url.substr(url.lastIndexOf("/") + 1);
  356. if(url != null) {
  357. addContentToZip(folder,
  358. name,
  359. url,
  360. null,
  361. true,
  362. function () {
  363. addTexturesToZip(zip, index + 1, textures, folder, then);
  364. });
  365. }
  366. else {
  367. addTexturesToZip(zip, index + 1, textures, folder, then);
  368. }
  369. }
  370. var addImportedFilesToZip = function (zip, index, importedFiles, folder, then) {
  371. if (index === importedFiles.length) {
  372. then();
  373. return;
  374. }
  375. if (!folder) {
  376. folder = zip.folder("scenes");
  377. }
  378. var url = importedFiles[index];
  379. var name = url.substr(url.lastIndexOf("/") + 1);
  380. addContentToZip(folder,
  381. name,
  382. url,
  383. null,
  384. true,
  385. function () {
  386. addImportedFilesToZip(zip, index + 1, importedFiles, folder, then);
  387. });
  388. }
  389. var getZip = function () {
  390. if (engine.scenes.length === 0) {
  391. return;
  392. }
  393. var zip = new JSZip();
  394. var scene = engine.scenes[0];
  395. var textures = scene.textures;
  396. var importedFiles = scene.importedMeshesFiles;
  397. document.getElementById("statusBar").innerHTML = "Creating archive...Please wait";
  398. if (zipCode.indexOf("textures/worldHeightMap.jpg") !== -1) {
  399. textures.push({ name: "textures/worldHeightMap.jpg" });
  400. }
  401. addContentToZip(zip,
  402. "index.html",
  403. "zipContent/index.html",
  404. zipCode,
  405. false,
  406. function () {
  407. addTexturesToZip(zip,
  408. 0,
  409. textures,
  410. null,
  411. function () {
  412. addImportedFilesToZip(zip,
  413. 0,
  414. importedFiles,
  415. null,
  416. function () {
  417. var blob = zip.generate({ type: "blob" });
  418. saveAs(blob, "sample.zip");
  419. document.getElementById("statusBar").innerHTML = "";
  420. });
  421. });
  422. });
  423. }
  424. // Versions
  425. setVersion = function (version) {
  426. switch (version) {
  427. case "2.5":
  428. location.href = "index2_5.html" + location.hash;
  429. break;
  430. default:
  431. location.href = "index.html" + location.hash;
  432. break;
  433. }
  434. }
  435. // Fonts
  436. setFontSize = function (size) {
  437. fontSize = size;
  438. document.querySelector(".view-lines").style.fontSize = size + "px";
  439. document.getElementById("currentFontSize").innerHTML = "Font: " + size;
  440. };
  441. // Fullscreen
  442. var goFullscreen = function () {
  443. if (engine) {
  444. engine.switchFullscreen(true);
  445. }
  446. }
  447. var toggleEditor = function () {
  448. var editorButton = document.getElementById("editorButton");
  449. var scene = engine.scenes[0];
  450. // If the editor is present
  451. if (editorButton.classList.contains('checked')) {
  452. editorButton.classList.remove('checked');
  453. splitInstance.collapse(0);
  454. editorButton.innerHTML = 'Editor <i class="fa fa-square-o" aria-hidden="true"></i>';
  455. } else {
  456. editorButton.classList.add('checked');
  457. splitInstance.setSizes([50, 50]); // Reset
  458. editorButton.innerHTML = 'Editor <i class="fa fa-check-square" aria-hidden="true"></i>';
  459. }
  460. engine.resize();
  461. if (scene.debugLayer.isVisible()) {
  462. scene.debugLayer.hide();
  463. scene.debugLayer.show();
  464. }
  465. }
  466. /**
  467. * Toggle the dark theme
  468. */
  469. var toggleTheme = function (theme) {
  470. // Monaco
  471. var vsTheme;
  472. if (theme == 'dark') {
  473. vsTheme = 'vs-dark'
  474. } else {
  475. vsTheme = 'vs'
  476. }
  477. let oldCode = jsEditor.getValue();
  478. jsEditor.dispose();
  479. jsEditor = monaco.editor.create(document.getElementById('jsEditor'), {
  480. value: "",
  481. language: "javascript",
  482. lineNumbers: true,
  483. tabSize: "auto",
  484. insertSpaces: "auto",
  485. roundedSelection: true,
  486. scrollBeyondLastLine: false,
  487. automaticLayout: true,
  488. readOnly: false,
  489. theme: vsTheme,
  490. contextmenu: false
  491. });
  492. jsEditor.setValue(oldCode);
  493. setFontSize(fontSize);
  494. for (var index = 0; index < elementToTheme.length; index++) {
  495. var obj = elementToTheme[index];
  496. let domObjArr = document.querySelectorAll(obj);
  497. for (var domObjIndex = 0; domObjIndex < domObjArr.length; domObjIndex++) {
  498. var domObj = domObjArr[domObjIndex];
  499. domObj.classList.remove('light');
  500. domObj.classList.remove('dark');
  501. domObj.classList.add(theme);
  502. }
  503. }
  504. localStorage.setItem("bjs-playground-theme", theme);
  505. }
  506. var toggleDebug = function () {
  507. var debugButton = document.getElementById("debugButton");
  508. var scene = engine.scenes[0];
  509. if (debugButton.classList.contains('uncheck')) {
  510. debugButton.classList.remove('uncheck');
  511. scene.debugLayer.show();
  512. } else {
  513. debugButton.classList.add('uncheck');
  514. scene.debugLayer.hide();
  515. }
  516. }
  517. var toggleMetadata = function () {
  518. // var metadataButton = document.getElementById("metadataButton");
  519. var scene = engine.scenes[0];
  520. // metadataButton.classList.add('checked');
  521. document.getElementById("saveLayer").style.display = "block";
  522. }
  523. var formatCode = function () {
  524. jsEditor.getAction('editor.action.format').run();
  525. }
  526. // UI
  527. document.getElementById("runButton").addEventListener("click", compileAndRun);
  528. document.getElementById("zipButton").addEventListener("click", getZip);
  529. document.getElementById("fullscreenButton").addEventListener("click", goFullscreen);
  530. document.getElementById("newButton").addEventListener("click", createNewScript);
  531. document.getElementById("clearButton").addEventListener("click", clear);
  532. document.getElementById("editorButton").addEventListener("click", toggleEditor);
  533. document.getElementById("debugButton").addEventListener("click", toggleDebug);
  534. document.getElementById("metadataButton").addEventListener("click", toggleMetadata);
  535. document.getElementById("darkTheme").addEventListener("click", toggleTheme.bind(this, 'dark'));
  536. document.getElementById("lightTheme").addEventListener("click", toggleTheme.bind(this, 'light'));
  537. document.getElementById("formatButton").addEventListener("click", formatCode);
  538. // Restore theme
  539. var theme = localStorage.getItem("bjs-playground-theme") || 'light';
  540. toggleTheme(theme);
  541. //Navigation Overwrites
  542. var exitPrompt = function (e) {
  543. var safeToggle = document.getElementById("safemodeToggle");
  544. if (safeToggle.classList.contains('checked')) {
  545. e = e || window.event;
  546. var message =
  547. 'This page is asking you to confirm that you want to leave - data you have entered may not be saved.';
  548. if (e) {
  549. e.returnValue = message;
  550. }
  551. return message;
  552. }
  553. };
  554. window.onbeforeunload = exitPrompt;
  555. // Snippet
  556. var save = function () {
  557. // Retrieve title if necessary
  558. if (document.getElementById("saveLayer")) {
  559. currentSnippetTitle = document.getElementById("saveFormTitle").value;
  560. currentSnippetDescription = document.getElementById("saveFormDescription").value;
  561. currentSnippetTags = document.getElementById("saveFormTags").value;
  562. }
  563. var xmlHttp = new XMLHttpRequest();
  564. xmlHttp.onreadystatechange = function () {
  565. if (xmlHttp.readyState === 4) {
  566. if (xmlHttp.status === 201) {
  567. var baseUrl = location.href.replace(location.hash, "").replace(location.search, "");
  568. var snippet = JSON.parse(xmlHttp.responseText);
  569. var newUrl = baseUrl + "#" + snippet.id;
  570. currentSnippetToken = snippet.id;
  571. if (snippet.version && snippet.version !== "0") {
  572. newUrl += "#" + snippet.version;
  573. }
  574. location.href = newUrl;
  575. // Hide the complete title & co message
  576. hideNoMetadata();
  577. compileAndRun();
  578. } else {
  579. showError("Unable to save your code. It may be too long.", null);
  580. }
  581. }
  582. }
  583. xmlHttp.open("POST", snippetUrl + (currentSnippetToken ? "/" + currentSnippetToken : ""), true);
  584. xmlHttp.setRequestHeader("Content-Type", "application/json");
  585. var dataToSend = {
  586. payload: {
  587. code: jsEditor.getValue()
  588. },
  589. name: currentSnippetTitle,
  590. description: currentSnippetDescription,
  591. tags: currentSnippetTags
  592. };
  593. xmlHttp.send(JSON.stringify(dataToSend));
  594. }
  595. document.getElementById("saveButton").addEventListener("click", function () {
  596. if (currentSnippetTitle == null
  597. || currentSnippetDescription == null
  598. || currentSnippetTags == null) {
  599. document.getElementById("saveLayer").style.display = "block";
  600. }
  601. else {
  602. save();
  603. }
  604. });
  605. document.getElementById("saveFormButtonOk").addEventListener("click", function () {
  606. document.getElementById("saveLayer").style.display = "none";
  607. save();
  608. });
  609. document.getElementById("saveFormButtonCancel").addEventListener("click", function () {
  610. document.getElementById("saveLayer").style.display = "none";
  611. });
  612. document.getElementById("saveMessage").addEventListener("click", function () {
  613. document.getElementById("saveMessage").style.display = "none";
  614. });
  615. document.getElementById("mainTitle").innerHTML = "v" + BABYLON.Engine.Version;
  616. var previousHash = "";
  617. var cleanHash = function () {
  618. var splits = decodeURIComponent(location.hash.substr(1)).split("#");
  619. if (splits.length > 2) {
  620. splits.splice(2, splits.length - 2);
  621. }
  622. location.hash = splits.join("#");
  623. }
  624. var checkHash = function (firstTime) {
  625. if (location.hash) {
  626. if (previousHash !== location.hash) {
  627. cleanHash();
  628. previousHash = location.hash;
  629. try {
  630. var xmlHttp = new XMLHttpRequest();
  631. xmlHttp.onreadystatechange = function () {
  632. if (xmlHttp.readyState === 4) {
  633. if (xmlHttp.status === 200) {
  634. var snippet = JSON.parse(xmlHttp.responseText)[0];
  635. blockEditorChange = true;
  636. jsEditor.setValue(JSON.parse(snippet.jsonPayload).code.toString());
  637. // Check if title / descr / tags are already set
  638. if (snippet.name != null && snippet.name != "") {
  639. currentSnippetTitle = snippet.name;
  640. }
  641. else currentSnippetTitle = null;
  642. if (snippet.description != null && snippet.description != "") {
  643. currentSnippetDescription = snippet.description;
  644. }
  645. else currentSnippetDescription = null;
  646. if (snippet.tags != null && snippet.tags != "") {
  647. currentSnippetTags = snippet.tags;
  648. }
  649. else currentSnippetTags = null;
  650. if (currentSnippetTitle != null && currentSnippetTags != null && currentSnippetDescription) {
  651. if (document.getElementById("saveLayer")) {
  652. document.getElementById("saveFormTitle").value = currentSnippetTitle;
  653. document.getElementById("saveFormDescription").value = currentSnippetDescription;
  654. document.getElementById("saveFormTags").value = currentSnippetTags;
  655. hideNoMetadata();
  656. }
  657. }
  658. else {
  659. showNoMetadata();
  660. }
  661. jsEditor.setPosition({ lineNumber: 0, column: 0 });
  662. blockEditorChange = false;
  663. compileAndRun();
  664. document.getElementById("currentScript").innerHTML = "Custom";
  665. } else if (firstTime) {
  666. location.href = location.href.replace(location.hash, "");
  667. if (scripts) {
  668. loadScriptFromIndex(0);
  669. }
  670. }
  671. }
  672. };
  673. var hash = location.hash.substr(1);
  674. currentSnippetToken = hash.split("#")[0];
  675. if (!hash.split("#")[1]) hash += "#0";
  676. xmlHttp.open("GET", snippetUrl + "/" + hash.replace("#", "/"));
  677. xmlHttp.send();
  678. } catch (e) {
  679. }
  680. }
  681. }
  682. setTimeout(checkHash, 200);
  683. }
  684. checkHash(true);
  685. }
  686. // Monaco
  687. var xhr = new XMLHttpRequest();
  688. xhr.open('GET', "babylon.d.txt", true);
  689. xhr.onreadystatechange = function () {
  690. if (xhr.readyState === 4) {
  691. if (xhr.status === 200) {
  692. require.config({ paths: { 'vs': 'node_modules/monaco-editor/min/vs' } });
  693. require(['vs/editor/editor.main'], function () {
  694. monaco.languages.typescript.javascriptDefaults.addExtraLib(xhr.responseText, 'babylon.d.ts');
  695. jsEditor = monaco.editor.create(document.getElementById('jsEditor'), {
  696. value: "",
  697. language: "javascript",
  698. lineNumbers: true,
  699. tabSize: "auto",
  700. insertSpaces: "auto",
  701. roundedSelection: true,
  702. scrollBeyondLastLine: false,
  703. automaticLayout: true,
  704. readOnly: false,
  705. theme: "vs",
  706. contextmenu: false
  707. });
  708. run();
  709. });
  710. }
  711. }
  712. };
  713. xhr.send(null);
  714. })();