index.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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. }
  353. var name = url.substr(url.lastIndexOf("/") + 1);
  354. addContentToZip(folder,
  355. name,
  356. url,
  357. null,
  358. true,
  359. function () {
  360. addTexturesToZip(zip, index + 1, textures, folder, then);
  361. });
  362. }
  363. var addImportedFilesToZip = function (zip, index, importedFiles, folder, then) {
  364. if (index === importedFiles.length) {
  365. then();
  366. return;
  367. }
  368. if (!folder) {
  369. folder = zip.folder("scenes");
  370. }
  371. var url = importedFiles[index];
  372. var name = url.substr(url.lastIndexOf("/") + 1);
  373. addContentToZip(folder,
  374. name,
  375. url,
  376. null,
  377. true,
  378. function () {
  379. addImportedFilesToZip(zip, index + 1, importedFiles, folder, then);
  380. });
  381. }
  382. var getZip = function () {
  383. if (engine.scenes.length === 0) {
  384. return;
  385. }
  386. var zip = new JSZip();
  387. var scene = engine.scenes[0];
  388. var textures = scene.textures;
  389. var importedFiles = scene.importedMeshesFiles;
  390. document.getElementById("statusBar").innerHTML = "Creating archive...Please wait";
  391. if (zipCode.indexOf("textures/worldHeightMap.jpg") !== -1) {
  392. textures.push({ name: "textures/worldHeightMap.jpg" });
  393. }
  394. addContentToZip(zip,
  395. "index.html",
  396. "zipContent/index.html",
  397. zipCode,
  398. false,
  399. function () {
  400. addTexturesToZip(zip,
  401. 0,
  402. textures,
  403. null,
  404. function () {
  405. addImportedFilesToZip(zip,
  406. 0,
  407. importedFiles,
  408. null,
  409. function () {
  410. var blob = zip.generate({ type: "blob" });
  411. saveAs(blob, "sample.zip");
  412. document.getElementById("statusBar").innerHTML = "";
  413. });
  414. });
  415. });
  416. }
  417. // Versions
  418. setVersion = function (version) {
  419. switch (version) {
  420. case "2.5":
  421. location.href = "index2_5.html" + location.hash;
  422. break;
  423. default:
  424. location.href = "index.html" + location.hash;
  425. break;
  426. }
  427. }
  428. // Fonts
  429. setFontSize = function (size) {
  430. fontSize = size;
  431. document.querySelector(".view-lines").style.fontSize = size + "px";
  432. document.getElementById("currentFontSize").innerHTML = "Font: " + size;
  433. };
  434. // Fullscreen
  435. var goFullscreen = function () {
  436. if (engine) {
  437. engine.switchFullscreen(true);
  438. }
  439. }
  440. var toggleEditor = function () {
  441. var editorButton = document.getElementById("editorButton");
  442. var scene = engine.scenes[0];
  443. // If the editor is present
  444. if (editorButton.classList.contains('checked')) {
  445. editorButton.classList.remove('checked');
  446. splitInstance.collapse(0);
  447. editorButton.innerHTML = 'Editor <i class="fa fa-square-o" aria-hidden="true"></i>';
  448. } else {
  449. editorButton.classList.add('checked');
  450. splitInstance.setSizes([50, 50]); // Reset
  451. editorButton.innerHTML = 'Editor <i class="fa fa-check-square" aria-hidden="true"></i>';
  452. }
  453. engine.resize();
  454. if (scene.debugLayer.isVisible()) {
  455. scene.debugLayer.hide();
  456. scene.debugLayer.show();
  457. }
  458. }
  459. /**
  460. * Toggle the dark theme
  461. */
  462. var toggleTheme = function (theme) {
  463. // Monaco
  464. var vsTheme;
  465. if (theme == 'dark') {
  466. vsTheme = 'vs-dark'
  467. } else {
  468. vsTheme = 'vs'
  469. }
  470. let oldCode = jsEditor.getValue();
  471. jsEditor.dispose();
  472. jsEditor = monaco.editor.create(document.getElementById('jsEditor'), {
  473. value: "",
  474. language: "javascript",
  475. lineNumbers: true,
  476. tabSize: "auto",
  477. insertSpaces: "auto",
  478. roundedSelection: true,
  479. scrollBeyondLastLine: false,
  480. automaticLayout: true,
  481. readOnly: false,
  482. theme: vsTheme,
  483. contextmenu: false
  484. });
  485. jsEditor.setValue(oldCode);
  486. setFontSize(fontSize);
  487. for (var index = 0; index < elementToTheme.length; index++) {
  488. var obj = elementToTheme[index];
  489. let domObjArr = document.querySelectorAll(obj);
  490. for (var domObjIndex = 0; domObjIndex < domObjArr.length; domObjIndex++) {
  491. var domObj = domObjArr[domObjIndex];
  492. domObj.classList.remove('light');
  493. domObj.classList.remove('dark');
  494. domObj.classList.add(theme);
  495. }
  496. }
  497. localStorage.setItem("bjs-playground-theme", theme);
  498. }
  499. var toggleDebug = function () {
  500. var debugButton = document.getElementById("debugButton");
  501. var scene = engine.scenes[0];
  502. if (debugButton.classList.contains('uncheck')) {
  503. debugButton.classList.remove('uncheck');
  504. scene.debugLayer.show();
  505. } else {
  506. debugButton.classList.add('uncheck');
  507. scene.debugLayer.hide();
  508. }
  509. }
  510. var toggleMetadata = function () {
  511. // var metadataButton = document.getElementById("metadataButton");
  512. var scene = engine.scenes[0];
  513. // metadataButton.classList.add('checked');
  514. document.getElementById("saveLayer").style.display = "block";
  515. }
  516. var formatCode = function () {
  517. jsEditor.getAction('editor.action.format').run();
  518. }
  519. // UI
  520. document.getElementById("runButton").addEventListener("click", compileAndRun);
  521. document.getElementById("zipButton").addEventListener("click", getZip);
  522. document.getElementById("fullscreenButton").addEventListener("click", goFullscreen);
  523. document.getElementById("newButton").addEventListener("click", createNewScript);
  524. document.getElementById("clearButton").addEventListener("click", clear);
  525. document.getElementById("editorButton").addEventListener("click", toggleEditor);
  526. document.getElementById("debugButton").addEventListener("click", toggleDebug);
  527. document.getElementById("metadataButton").addEventListener("click", toggleMetadata);
  528. document.getElementById("darkTheme").addEventListener("click", toggleTheme.bind(this, 'dark'));
  529. document.getElementById("lightTheme").addEventListener("click", toggleTheme.bind(this, 'light'));
  530. document.getElementById("formatButton").addEventListener("click", formatCode);
  531. // Restore theme
  532. var theme = localStorage.getItem("bjs-playground-theme") || 'light';
  533. toggleTheme(theme);
  534. //Navigation Overwrites
  535. var exitPrompt = function (e) {
  536. var safeToggle = document.getElementById("safemodeToggle");
  537. if (safeToggle.classList.contains('checked')) {
  538. e = e || window.event;
  539. var message =
  540. 'This page is asking you to confirm that you want to leave - data you have entered may not be saved.';
  541. if (e) {
  542. e.returnValue = message;
  543. }
  544. return message;
  545. }
  546. };
  547. window.onbeforeunload = exitPrompt;
  548. // Snippet
  549. var save = function () {
  550. // Retrieve title if necessary
  551. if (document.getElementById("saveLayer")) {
  552. currentSnippetTitle = document.getElementById("saveFormTitle").value;
  553. currentSnippetDescription = document.getElementById("saveFormDescription").value;
  554. currentSnippetTags = document.getElementById("saveFormTags").value;
  555. }
  556. var xmlHttp = new XMLHttpRequest();
  557. xmlHttp.onreadystatechange = function () {
  558. if (xmlHttp.readyState === 4) {
  559. if (xmlHttp.status === 201) {
  560. var baseUrl = location.href.replace(location.hash, "").replace(location.search, "");
  561. var snippet = JSON.parse(xmlHttp.responseText);
  562. var newUrl = baseUrl + "#" + snippet.id;
  563. currentSnippetToken = snippet.id;
  564. if (snippet.version && snippet.version !== "0") {
  565. newUrl += "#" + snippet.version;
  566. }
  567. location.href = newUrl;
  568. // Hide the complete title & co message
  569. hideNoMetadata();
  570. compileAndRun();
  571. } else {
  572. showError("Unable to save your code. It may be too long.", null);
  573. }
  574. }
  575. }
  576. xmlHttp.open("POST", snippetUrl + (currentSnippetToken ? "/" + currentSnippetToken : ""), true);
  577. xmlHttp.setRequestHeader("Content-Type", "application/json");
  578. var dataToSend = {
  579. payload: {
  580. code: jsEditor.getValue()
  581. },
  582. name: currentSnippetTitle,
  583. description: currentSnippetDescription,
  584. tags: currentSnippetTags
  585. };
  586. xmlHttp.send(JSON.stringify(dataToSend));
  587. }
  588. document.getElementById("saveButton").addEventListener("click", function () {
  589. if (currentSnippetTitle == null
  590. || currentSnippetDescription == null
  591. || currentSnippetTags == null) {
  592. document.getElementById("saveLayer").style.display = "block";
  593. }
  594. else {
  595. save();
  596. }
  597. });
  598. document.getElementById("saveFormButtonOk").addEventListener("click", function () {
  599. document.getElementById("saveLayer").style.display = "none";
  600. save();
  601. });
  602. document.getElementById("saveFormButtonCancel").addEventListener("click", function () {
  603. document.getElementById("saveLayer").style.display = "none";
  604. });
  605. document.getElementById("saveMessage").addEventListener("click", function () {
  606. document.getElementById("saveMessage").style.display = "none";
  607. });
  608. document.getElementById("mainTitle").innerHTML = "v" + BABYLON.Engine.Version;
  609. var previousHash = "";
  610. var cleanHash = function () {
  611. var splits = decodeURIComponent(location.hash.substr(1)).split("#");
  612. if (splits.length > 2) {
  613. splits.splice(2, splits.length - 2);
  614. }
  615. location.hash = splits.join("#");
  616. }
  617. var checkHash = function (firstTime) {
  618. if (location.hash) {
  619. if (previousHash !== location.hash) {
  620. cleanHash();
  621. previousHash = location.hash;
  622. try {
  623. var xmlHttp = new XMLHttpRequest();
  624. xmlHttp.onreadystatechange = function () {
  625. if (xmlHttp.readyState === 4) {
  626. if (xmlHttp.status === 200) {
  627. var snippet = JSON.parse(xmlHttp.responseText)[0];
  628. blockEditorChange = true;
  629. jsEditor.setValue(JSON.parse(snippet.jsonPayload).code.toString());
  630. // Check if title / descr / tags are already set
  631. if (snippet.name != null && snippet.name != "") {
  632. currentSnippetTitle = snippet.name;
  633. }
  634. else currentSnippetTitle = null;
  635. if (snippet.description != null && snippet.description != "") {
  636. currentSnippetDescription = snippet.description;
  637. }
  638. else currentSnippetDescription = null;
  639. if (snippet.tags != null && snippet.tags != "") {
  640. currentSnippetTags = snippet.tags;
  641. }
  642. else currentSnippetTags = null;
  643. if (currentSnippetTitle != null && currentSnippetTags != null && currentSnippetDescription) {
  644. if (document.getElementById("saveLayer")) {
  645. document.getElementById("saveFormTitle").value = currentSnippetTitle;
  646. document.getElementById("saveFormDescription").value = currentSnippetDescription;
  647. document.getElementById("saveFormTags").value = currentSnippetTags;
  648. hideNoMetadata();
  649. }
  650. }
  651. else {
  652. showNoMetadata();
  653. }
  654. jsEditor.setPosition({ lineNumber: 0, column: 0 });
  655. blockEditorChange = false;
  656. compileAndRun();
  657. document.getElementById("currentScript").innerHTML = "Custom";
  658. } else if (firstTime) {
  659. location.href = location.href.replace(location.hash, "");
  660. if (scripts) {
  661. loadScriptFromIndex(0);
  662. }
  663. }
  664. }
  665. };
  666. var hash = location.hash.substr(1);
  667. currentSnippetToken = hash.split("#")[0];
  668. if (!hash.split("#")[1]) hash += "#0";
  669. xmlHttp.open("GET", snippetUrl + "/" + hash.replace("#", "/"));
  670. xmlHttp.send();
  671. } catch (e) {
  672. }
  673. }
  674. }
  675. setTimeout(checkHash, 200);
  676. }
  677. checkHash(true);
  678. }
  679. // Monaco
  680. var xhr = new XMLHttpRequest();
  681. xhr.open('GET', "babylon.d.txt", true);
  682. xhr.onreadystatechange = function () {
  683. if (xhr.readyState === 4) {
  684. if (xhr.status === 200) {
  685. require.config({ paths: { 'vs': 'node_modules/monaco-editor/min/vs' } });
  686. require(['vs/editor/editor.main'], function () {
  687. monaco.languages.typescript.javascriptDefaults.addExtraLib(xhr.responseText, 'babylon.d.ts');
  688. jsEditor = monaco.editor.create(document.getElementById('jsEditor'), {
  689. value: "",
  690. language: "javascript",
  691. lineNumbers: true,
  692. tabSize: "auto",
  693. insertSpaces: "auto",
  694. roundedSelection: true,
  695. scrollBeyondLastLine: false,
  696. automaticLayout: true,
  697. readOnly: false,
  698. theme: "vs",
  699. contextmenu: false
  700. });
  701. run();
  702. });
  703. }
  704. }
  705. };
  706. xhr.send(null);
  707. })();