pbt.js 15 KB

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