pbt.js 17 KB

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