pbt.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // jsEditor Manipulation
  2. var PBT = function() {
  3. this.decorationStyles = new Array();
  4. this.decorations = new Array();
  5. this.lineRanges = new Array();
  6. var advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
  7. var background = "pbt-back-highlight";
  8. var margin = "pbt-margin-decor-on";
  9. if(localStorage.getItem("bjs-playground-theme") =="dark") {
  10. background += "-dark";
  11. margin +="-dark";
  12. }
  13. this.clearDecorLines = function() {
  14. this.decorations = jsEditor.deltaDecorations(this.decorations, []);
  15. }
  16. this.setDecorLines = function (lineRanges) {
  17. this.decorationStyles = [];
  18. var endLineNm = jsEditor.getModel()._lines.length;
  19. this.decorationStyles.push({ range: new monaco.Range(1,1,endLineNm,1), options: { isWholeLine: true, inlineClassName: 'pbt-fade' }});
  20. for(var i = 0; i < lineRanges.length; i +=2) {
  21. this.decorationStyles.push({ range: new monaco.Range(lineRanges[i],1,lineRanges[i + 1],1), options: { isWholeLine: true, linesDecorationsClassName: margin }});
  22. this.decorationStyles.push({ range: new monaco.Range(lineRanges[i],1,lineRanges[i + 1],1), options: { isWholeLine: true, className: background }});
  23. this.decorationStyles.push({ range: new monaco.Range(lineRanges[i],1,lineRanges[i + 1],1), options: { isWholeLine: true, inlineClassName: 'pbt-darken' }});
  24. }
  25. this.decorations = jsEditor.deltaDecorations([this.decorations], this.decorationStyles);
  26. }
  27. this.replaceLines = function(lineRange, text) {
  28. jsEditor.executeEdits("", [
  29. { range: new monaco.Range(lineRange[0], 1, lineRange[1], 100000), text: text}
  30. ]);
  31. }
  32. this.replaceText = function(line, start, end, text) {
  33. jsEditor.executeEdits("", [
  34. { range: new monaco.Range(line, start, line, end), text: text}
  35. ]);
  36. }
  37. this.getLineText = function(lineNm) {
  38. return jsEditor.getModel().getLineContent(lineNm);
  39. }
  40. this.hideLines = function(lineRanges) {
  41. var ranges = [];
  42. this.lineRanges = lineRanges;
  43. for(var i = 0; i < lineRanges.length; i +=2) {
  44. ranges.push(new monaco.Range(lineRanges[i], 1, lineRanges[i + 1], 100000));
  45. }
  46. jsEditor.setHiddenAreas(ranges);
  47. }
  48. this.hideRange = function(lineRanges) {
  49. var ranges = [];
  50. lineRanges = this.lineRanges.concat(lineRanges);
  51. this.lineRanges = lineRanges;
  52. for(var i = 0; i < lineRanges.length; i +=2) {
  53. ranges.push(new monaco.Range(lineRanges[i], 1, lineRanges[i + 1], 100000));
  54. }
  55. jsEditor.setHiddenAreas(ranges);
  56. }
  57. this.showRange = function(lineRanges) {
  58. var rangePairs = [];
  59. var linePairs = [];
  60. for(var i = 0; i < this.lineRanges.length; i +=2) {
  61. rangePairs.push(this.lineRanges[i] + "=" + this.lineRanges[i + 1]);
  62. }
  63. for(var i = 0; i < lineRanges.length; i +=2) {
  64. linePairs.push(lineRanges[i] + "=" + lineRanges[i + 1]);
  65. }
  66. var rangeString = rangePairs.join("-");
  67. for(var i = 0; i < linePairs.length; i++) {
  68. rangeString = rangeString.replace(linePairs[i]+"-", "");
  69. rangeString = rangeString.replace("-" + linePairs[i], ""); //when last element
  70. }
  71. rangeString = rangeString.replace(/-/g, ",");
  72. rangeString = rangeString.replace(/=/g, ",");
  73. lineRanges = rangeString.split(",");
  74. lineRanges = lineRanges.map(function(n){
  75. return parseInt(n);
  76. });
  77. var ranges = [];
  78. for(var i = 0; i < lineRanges.length; i +=2) {
  79. ranges.push(new monaco.Range(lineRanges[i], 1, lineRanges[i + 1], 100000));
  80. }
  81. this.lineRanges = lineRanges;
  82. jsEditor.setHiddenAreas(ranges);
  83. }
  84. this.editOn = function() {
  85. jsEditor.updateOptions({readOnly: false});
  86. }
  87. this.editOff = function() {
  88. jsEditor.updateOptions({readOnly: true});
  89. }
  90. //hide menu items
  91. this.hideMenu = function() {
  92. var headings = document.getElementsByClassName('category');
  93. for (var i = 0; i < headings.length; i ++) {
  94. headings[i].style.visibility = 'hidden';
  95. }
  96. headings = document.getElementsByClassName('category right');
  97. for (var i = 0; i < headings.length; i ++) {
  98. headings[i].style.visibility = 'visible';
  99. }
  100. }
  101. //Standard GUI Dialogues
  102. this.StandardDialog = function(options) {
  103. options = options||{};
  104. var width = options.width||0.5;
  105. var height = options.height||0.25;
  106. var top = options.top||0;
  107. var left = options.left||0;
  108. var verticalAlignment = options.verticalAlignment||BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
  109. var horizontalAlignment = options.horizontalAlignment||BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  110. var text = options.text||"Playground Based Tutorial";
  111. if(options.useImage === undefined) {
  112. var useImage = true;
  113. }
  114. else {
  115. var useImage = false;
  116. }
  117. var imageURL = options.imageURL||"LogoPBT.png";
  118. var textBlockWidth = 0.95;
  119. var textBlockLeft = "2%";
  120. this.container = new BABYLON.GUI.Rectangle();
  121. this.container.verticalAlignment = verticalAlignment;
  122. this.container.horizontalAlignment = horizontalAlignment;
  123. this.container.width = width;
  124. this.container.height = height;
  125. this.container.cornerRadius = 10;
  126. this.container.color = "#364249";
  127. this.container.thickness = 4;
  128. this.container.background = "#CDC8F9";
  129. this.container.top = top;
  130. this.container.left = left;
  131. advancedTexture.addControl(this.container);
  132. if(useImage) {
  133. this.logoPBT = BABYLON.GUI.Button.CreateImageOnlyButton("but", imageURL);
  134. this.logoPBT.width = "100px";
  135. this.logoPBT.height = "100px";
  136. this.logoPBT.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  137. this.logoPBT.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
  138. this.logoPBT.top = 2;
  139. this.logoPBT.left=2;
  140. this.logoPBT.color = "#CDC8F9";
  141. this.container.addControl(this.logoPBT);
  142. textBlockWidth = 0.6;
  143. textBlockLeft = "35%";
  144. }
  145. this.textBlock = new BABYLON.GUI.TextBlock("text", text);
  146. this.textBlock.width = textBlockWidth;
  147. this.textBlock.height = 0.7
  148. this.textBlock.textWrapping = true;
  149. this.textBlock.textHorizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  150. this.textBlock.color = "#364249";
  151. this.textBlock.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
  152. this.textBlock.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  153. this.textBlock.left = textBlockLeft;
  154. this.textBlock.top = 2;
  155. this.container.addControl(this.textBlock);
  156. this.nextButton = BABYLON.GUI.Button.CreateSimpleButton("nextbut", "Next >");
  157. this.nextButton.width = 0.2
  158. this.nextButton.height = 0.15;
  159. this.nextButton.color = "white";
  160. this.nextButton.cornerRadius = 5;
  161. this.nextButton.background = "#364249";
  162. this.nextButton.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
  163. this.nextButton.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  164. this.nextButton.left = "78%";
  165. this.nextButton.top = "80%";
  166. this.container.addControl(this.nextButton);
  167. this.prevButton = BABYLON.GUI.Button.CreateSimpleButton("prevbut", "< Prev");
  168. this.prevButton.width = 0.2
  169. this.prevButton.height = 0.15;
  170. this.prevButton.color = "white";
  171. this.prevButton.cornerRadius = 5;
  172. this.prevButton.background = "#364249";
  173. this.prevButton.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
  174. this.prevButton.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  175. this.prevButton.left = "2%";
  176. this.prevButton.top = "80%";
  177. this.container.addControl(this.prevButton);
  178. this.showNext = function() {
  179. this.nextButton.isVisible = true;
  180. }
  181. this.hideNext = function() {
  182. this.nextButton.isVisible = false;
  183. }
  184. this.getNextButton = function() {
  185. return this.nextButton;
  186. }
  187. this.getPrevButton = function() {
  188. return this.prevButton;
  189. }
  190. this.showPrev = function() {
  191. this.prevButton.isVisible = true;
  192. }
  193. this.hidePrev = function() {
  194. this.prevButton.isVisible = false;
  195. }
  196. this.setWidth = function(width) {
  197. this.container.width = width;
  198. }
  199. this.setHeight = function(height) {
  200. this.container.height = height;
  201. }
  202. this.setTop = function(top) {
  203. this.container.top = top;
  204. }
  205. this.setLeft = function(left) {
  206. this.container.left = left;
  207. }
  208. this.getWidth = function() {
  209. return this.container.width;
  210. }
  211. this.getHeight = function() {
  212. return this.container.height;
  213. }
  214. this.getTop = function() {
  215. return this.container.top;
  216. }
  217. this.getLeft = function() {
  218. return this.container.left;
  219. }
  220. this.setHorizontalAlignment = function(hrzAlgn) {
  221. this.container.horizontalAlignment = hrzAlgn;
  222. }
  223. this.setVerticalAlignment = function(vrtAlign) {
  224. this.container.VerticalAlignmenv = vrtAlign;
  225. }
  226. this.setText = function(text) {
  227. this.textBlock.text = text;
  228. }
  229. this.show = function() {
  230. this.container.isVisible = true;
  231. }
  232. this.hide = function() {
  233. this.container.isVisible = false;
  234. }
  235. return this;
  236. }
  237. //Radio and Checkbox Button GUI
  238. this.ButtonGroup = function(name, type) {
  239. this.name = name;
  240. var type = type||"C";
  241. type = type.substr(0,1).toUpperCase();
  242. if(type !="R") {
  243. if(type != "S") {
  244. if(type != "C") {
  245. type = "C";
  246. }
  247. }
  248. }
  249. this.type = type;
  250. this.buttons = new Array();
  251. this.addButton = function(text, func, checked) {
  252. this.buttons.push({
  253. text: text||"",
  254. func: func||function(){},
  255. checked: checked||false
  256. });
  257. }
  258. this.addSlider = function(text, func, unit, onVal, min, max, value) {
  259. this.buttons.push({
  260. text: text||"",
  261. func: func||function(){},
  262. unit: unit||"",
  263. onVal: onVal||function(){},
  264. min: min||0,
  265. max: max||10,
  266. value: value||0
  267. });
  268. }
  269. return this;
  270. }
  271. this.SelectionDialog = function(options) {
  272. options = options||{};
  273. var justStarted = true;
  274. var width = options.width||0.3;
  275. var top = options.top||0;
  276. var left = options.left||0;
  277. var verticalAlignment = options.verticalAlignment||BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;
  278. var horizontalAlignment = options.horizontalAlignment||BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  279. var groups = options.groups;
  280. this.container = new BABYLON.GUI.Rectangle();
  281. this.container.verticalAlignment = verticalAlignment;
  282. this.container.horizontalAlignment = horizontalAlignment;
  283. var height = 36 * groups.length;
  284. for(var i = 0; i < groups.length; i++) {
  285. height += 32 * groups[i].buttons.length;
  286. if(groups[i].type == "S") {
  287. height += 31 * groups[i].buttons.length;
  288. }
  289. }
  290. this.container.height = height + "px";
  291. this.container.cornerRadius = 10;
  292. this.container.color = "#364249";
  293. this.container.thickness = 4;
  294. this.container.background = "#CDC8F9";
  295. this.container.top = top;
  296. this.container.left = left;
  297. this.container.width = width;
  298. advancedTexture.addControl(this.container);
  299. var panel = new BABYLON.GUI.StackPanel();
  300. panel.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
  301. panel.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  302. panel.top = 5;
  303. panel.left = 5;
  304. this.container.addControl(panel);
  305. var addRadio = function(text, parent, group, func, checked) {
  306. checked = checked || false;
  307. var button = new BABYLON.GUI.RadioButton();
  308. button.width = "20px";
  309. button.height = "20px";
  310. button.color = "#364249";
  311. button.background = "white";
  312. button.group = group;
  313. button.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  314. button.justStarted = true;
  315. button.func = func;
  316. button.onIsCheckedChangedObservable.add(function(state) {
  317. if (state && !justStarted) {
  318. func();
  319. }
  320. });
  321. var header = BABYLON.GUI.Control.AddHeader(button, text, "200px", { isHorizontal: true, controlFirst: true });
  322. header.height = "30px";
  323. header.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  324. header.left = "4px";
  325. parent.addControl(header);
  326. button.isChecked = checked;
  327. }
  328. var addCheckbox = function(text, parent, func, checked) {
  329. checked = checked || false;
  330. var button = new BABYLON.GUI.Checkbox();
  331. button.width = "20px";
  332. button.height = "20px";
  333. button.color = "#364249";
  334. button.background = "white";
  335. button.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  336. button.onIsCheckedChangedObservable.add(function(state) {
  337. func();
  338. });
  339. var header = BABYLON.GUI.Control.AddHeader(button, text, "200px", { isHorizontal: true, controlFirst: true });
  340. header.height = "30px";
  341. header.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  342. header.left = "4px";
  343. parent.addControl(header);
  344. button.isChecked = checked;
  345. }
  346. var addSldr = function(text, parent, func, unit, onValueChange, min, max, value) {
  347. var button = new BABYLON.GUI.Slider();
  348. button.value = value;
  349. button.minimum = min;
  350. button.maximum = max;
  351. button.width = "200px";
  352. button.height = "20px";
  353. button.color = "#364249";
  354. button.background = "white";
  355. button.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  356. button.left = "4px";
  357. var header = new BABYLON.GUI.TextBlock();
  358. header.text = text+": " + value + " " + unit;
  359. header.height = "30px";
  360. header.color = "#364249";
  361. header.textHorizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  362. header.left = "4px";
  363. parent.addControl(header);
  364. button.onValueChangedObservable.add(function(value) {
  365. header.text = text + onValueChange(value) + " " + unit;
  366. func(value);
  367. });
  368. parent.addControl(button);
  369. }
  370. var groupHeader = function(name) {
  371. var groupHeading = new BABYLON.GUI.TextBlock("groupHead", name);
  372. groupHeading.width = 0.9;
  373. groupHeading.height = "30px";
  374. groupHeading.textWrapping = true;
  375. groupHeading.color = "black";
  376. groupHeading.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  377. groupHeading.textHorizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  378. groupHeading.left = "2px";
  379. panel.addControl(groupHeading);
  380. }
  381. var addSpacer = function(name) {
  382. var separator = new BABYLON.GUI.Rectangle();
  383. separator.width = 1;
  384. separator.height = "2px";
  385. separator.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  386. separator.background = "#364249";
  387. separator.color = "#364249";
  388. panel.addControl(separator);
  389. groupHeader(name);
  390. }
  391. this.addGroup = function(group) {
  392. if(group.type == "R") {
  393. for(var i = 0; i < group.buttons.length; i++) {
  394. addRadio(group.buttons[i].text, panel, group.name, group.buttons[i].func, group.buttons[i].checked);
  395. }
  396. }
  397. else if(group.type == "S") {
  398. for(var i = 0; i < group.buttons.length; i++) {
  399. addSldr(group.buttons[i].text, panel, group.buttons[i].func, group.buttons[i].unit, group.buttons[i].onVal, group.buttons[i].min, group.buttons[i].max, group.buttons[i].value);
  400. }
  401. }
  402. else {
  403. for(var i = 0; i < group.buttons.length; i++) {
  404. addCheckbox(group.buttons[i].text, panel, group.buttons[i].func, group.buttons[i].checked);
  405. }
  406. }
  407. }
  408. groupHeader(groups[0].name);
  409. this.addGroup(groups[0]);
  410. for(var i = 1; i < groups.length; i++) {
  411. addSpacer(groups[i].name);
  412. this.addGroup(groups[i]);
  413. }
  414. justStarted = false;
  415. this.setWidth = function(width) {
  416. this.container.width = width;
  417. }
  418. this.setTop = function(top) {
  419. this.container.top = top;
  420. }
  421. this.setLeft = function(left) {
  422. this.container.left = left;
  423. }
  424. this.getWidth = function() {
  425. return this.container.width;
  426. }
  427. this.getTop = function() {
  428. return this.container.top;
  429. }
  430. this.getLeft = function() {
  431. return this.container.left;
  432. }
  433. this.setHorizontalAlignment = function(hrzAlgn) {
  434. this.container.horizontalAlignment = hrzAlgn;
  435. }
  436. this.setVerticalAlignment = function(vrtAlign) {
  437. this.container.VerticalAlignmenv = vrtAlign;
  438. }
  439. this.show = function() {
  440. this.container.isVisible = true;
  441. }
  442. this.hide = function() {
  443. this.container.isVisible = false;
  444. }
  445. return this;
  446. }
  447. }