settingsPG.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * This JS file is for settings :
  3. * - Theme
  4. * - Script language
  5. * - Font size
  6. */
  7. class SettingsPG {
  8. constructor(parent) {
  9. this.parent = parent;
  10. // The elements that will color with languages
  11. this.elementForscriptLanguage = [
  12. '#exampleList #exampleBanner',
  13. '.navbar',
  14. '.navbar .category',
  15. '.navbar .select .toDisplay',
  16. '.navbar .select .toDisplay .subSelect .toDisplaySub',
  17. '#fpsLabel',
  18. '.save-form',
  19. '#switchWrapper',
  20. '.navbarBottom',
  21. '.navbarBottom .links .link a',
  22. '.buttonPG'
  23. ];
  24. // The elements that will color with theme
  25. this.elementToTheme = [
  26. '.wrapper #jsEditor',
  27. '.wrapper .gutter'
  28. ];
  29. // Editor font size
  30. this.safeMode = localStorage.getItem("bjs-playground-safeMode") || false;
  31. this.ctrlS = localStorage.getItem("bjs-playground-ctrlS") || true;
  32. // Editor font size
  33. this.fontSize = localStorage.getItem("bjs-playground-font") || 14;
  34. // Editor theme
  35. this.vsTheme = localStorage.getItem("bjs-playground-theme") || 'light';
  36. // Editor language
  37. this.scriptLanguage = localStorage.getItem("bjs-playground-scriptLanguage") || 'JS';
  38. this.defaultScene = "scripts/basic scene.js";
  39. if (this.scriptLanguage == "JS") {
  40. this.defaultScene = "scripts/basic scene.js";
  41. this.parent.monacoCreator.monacoMode = "javascript";
  42. } else if (this.scriptLanguage == "TS") {
  43. this.defaultScene = "scripts/basic scene.txt";
  44. this.parent.monacoCreator.monacoMode = "typescript";
  45. }
  46. }
  47. get ScriptLanguage() {
  48. return this.scriptLanguage;
  49. };
  50. set ScriptLanguage(value) {
  51. localStorage.setItem("bjs-playground-scriptLanguage", value);
  52. this.scriptLanguage = value;
  53. };
  54. get DefaultScene() {
  55. return this.defaultScene;
  56. };
  57. /**
  58. * Change safe mode
  59. */
  60. setSafeMode(value) {
  61. localStorage.setItem("bjs-playground-safeMode", value);
  62. this.safeMode = value;
  63. if (value) {
  64. this.parent.utils.setToMultipleID("safemodeToggle", "innerHTML", 'Safe mode <i class="fa fa-check-square" aria-hidden="true"></i>');
  65. } else {
  66. this.parent.utils.setToMultipleID("safemodeToggle", "innerHTML", 'Safe mode <i class="fa fa-square" aria-hidden="true"></i>');
  67. }
  68. };
  69. restoreSafeMode() {
  70. this.setSafeMode(this.safeMode);
  71. };
  72. /**
  73. * Change CTRL+S
  74. */
  75. setCTRLS(value) {
  76. localStorage.setItem("bjs-playground-ctrlS", value);
  77. this.ctrlS = value;
  78. if (value) {
  79. this.parent.utils.setToMultipleID("ctrlsToggle", "innerHTML", 'CTRL+S to save <i class="fa fa-check-square" aria-hidden="true"></i>');
  80. } else {
  81. this.parent.utils.setToMultipleID("ctrlsToggle", "innerHTML", 'CTRL+S to save <i class="fa fa-square" aria-hidden="true"></i>');
  82. }
  83. };
  84. restoreCTRLS() {
  85. this.setSafeMode(this.ctrlS);
  86. };
  87. /**
  88. * Change font size
  89. */
  90. setFontSize(size) {
  91. localStorage.setItem("bjs-playground-font", size);
  92. this.fontSize = size;
  93. this.parent.monacoCreator.jsEditor.updateOptions({
  94. fontSize: size
  95. });
  96. var array = document.getElementsByClassName("displayFontSize");
  97. for (var i = 0; i < array.length; i++) {
  98. var subArray = array[i].children;
  99. for (var j = 0; j < subArray.length; j++) {
  100. subArray[j].classList.remove("selected");
  101. if (subArray[j].innerText == size)
  102. subArray[j].classList.add("selected");
  103. }
  104. }
  105. };
  106. restoreFont() {
  107. this.setFontSize(this.fontSize);
  108. };
  109. /**
  110. * Toggle Typescript / Javascript language
  111. */
  112. setScriptLanguage() {
  113. for (var index = 0; index < this.elementForscriptLanguage.length; index++) {
  114. var obj = this.elementForscriptLanguage[index];
  115. var domObjArr = document.querySelectorAll(obj);
  116. for (var domObjIndex = 0; domObjIndex < domObjArr.length; domObjIndex++) {
  117. var domObj = domObjArr[domObjIndex];
  118. domObj.classList.remove('languageJS');
  119. domObj.classList.remove('languageTS');
  120. domObj.classList.add("language" + this.scriptLanguage);
  121. }
  122. }
  123. if (this.scriptLanguage == "JS") {
  124. this.parent.utils.setToMultipleID("toJSbutton", "removeClass", "floatLeft");
  125. this.parent.utils.setToMultipleID("toJSbutton", "addClass", "selectedLanguage");
  126. this.parent.utils.setToMultipleID("toJSbutton", "innerHTML", "Javascript");
  127. this.parent.utils.setToMultipleID("toTSbutton", "title", "Switch to TypeScript");
  128. } else if (this.scriptLanguage == "TS") {
  129. this.parent.utils.setToMultipleID("toJSbutton", "addClass", "floatLeft");
  130. this.parent.utils.setToMultipleID("toTSbutton", "addClass", "selectedLanguage");
  131. this.parent.utils.setToMultipleID("toTSbutton", "innerHTML", "Typescript");
  132. this.parent.utils.setToMultipleID("toJSbutton", "title", "Switch to JavaScript");
  133. }
  134. };
  135. /**
  136. * Set the theme (dark / light)
  137. */
  138. setTheme(theme) {
  139. localStorage.setItem("bjs-playground-theme", theme);
  140. // Get the Monaco theme name.
  141. // Change the selected button style
  142. this.parent.utils.setToMultipleID("darkTheme", "removeClass", "selected");
  143. this.parent.utils.setToMultipleID("lightTheme", "removeClass", "selected");
  144. if (theme == 'dark') {
  145. this.vsTheme = 'vs-dark';
  146. this.parent.utils.setToMultipleID("darkTheme", "addClass", "selected");
  147. } else {
  148. this.vsTheme = 'vs';
  149. this.parent.utils.setToMultipleID("lightTheme", "addClass", "selected");
  150. }
  151. this.parent.monacoCreator.createMonacoEditor();
  152. this.setFontSize(this.fontSize);
  153. // Color the elements to theme
  154. for (var index = 0; index < this.elementToTheme.length; index++) {
  155. var obj = this.elementToTheme[index];
  156. var domObjArr = document.querySelectorAll(obj);
  157. for (var domObjIndex = 0; domObjIndex < domObjArr.length; domObjIndex++) {
  158. var domObj = domObjArr[domObjIndex];
  159. domObj.classList.remove('light');
  160. domObj.classList.remove('dark');
  161. domObj.classList.add(theme);
  162. }
  163. }
  164. };
  165. restoreTheme() {
  166. this.setTheme(this.vsTheme, this.parent.monacoCreator);
  167. };
  168. /**
  169. * Set BJS version on click
  170. */
  171. setBJSversion(evt, code) {
  172. localStorage.setItem("bjs-playground-apiversion", evt.target.value);
  173. localStorage.setItem("bjs-playground-apiversion-tempcode", code);
  174. window.location.reload();
  175. };
  176. /**
  177. * Check if we need to restore a BJS version
  178. */
  179. restoreVersion() {
  180. if (this.mustModifyBJSversion()) {
  181. this.parent.menuPG.displayWaitDiv();
  182. window.def = window.define;
  183. window.define = undefined;
  184. var apiVersion = localStorage.getItem("bjs-playground-apiversion");
  185. BABYLON = null;
  186. var position = 0;
  187. for (var i = 0; i < CONFIG_last_versions.length; i++) {
  188. if (CONFIG_last_versions[i][0] == apiVersion) {
  189. position = i;
  190. break;
  191. }
  192. }
  193. var count = CONFIG_last_versions[position][1].length - 1;
  194. var newBJSscript = document.createElement('script');
  195. newBJSscript.src = CONFIG_last_versions[position][1][0];
  196. newBJSscript.onload = function () {
  197. for (var i = 1; i < CONFIG_last_versions[position][1].length; i++) {
  198. var newBJSscript = document.createElement('script');
  199. newBJSscript.src = CONFIG_last_versions[position][1][i];
  200. newBJSscript.onload = function () {
  201. count--;
  202. if (count == 0) {
  203. if (BABYLON.Engine.Version.search('-') != -1) this.parent.menuPG.displayVersionNumber("Latest");
  204. else this.parent.menuPG.displayVersionNumber(BABYLON.Engine.Version);
  205. this.parent.utils.setToMultipleID("mainTitle", "innerHTML", "v" + BABYLON.Engine.Version);
  206. this.parent.monacoCreator.addOnMonacoLoadedCallback(() => {
  207. this.parent.monacoCreator.setCode(localStorage.getItem("bjs-playground-apiversion-tempcode"));
  208. localStorage.removeItem("bjs-playground-apiversion");
  209. localStorage.removeItem("bjs-playground-apiversion-tempcode");
  210. this.parent.main.compileAndRunFromOutside();
  211. });
  212. if (window.def) {
  213. window.define = window.def;
  214. }
  215. }
  216. }.bind(this);
  217. document.head.appendChild(newBJSscript);
  218. }
  219. }.bind(this);
  220. document.head.appendChild(newBJSscript);
  221. } else return false;
  222. };
  223. mustModifyBJSversion() {
  224. if (localStorage.getItem("bjs-playground-apiversion") && localStorage.getItem("bjs-playground-apiversion") != null && localStorage.getItem("bjs-playground-apiversion") != "Latest") return true;
  225. else return false;
  226. };
  227. };