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