geom_set_ops.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // geom_set_ops.js
  2. // contains the definitions of the Geometric Set operations
  3. // union, difference, intersection, and hull
  4. // These blocks are made with mutators that allow them to
  5. // be expanded by the user to take more than the default two
  6. // object arguments.
  7. //
  8. goog.require('Blockly.Blocks');
  9. //goog.require('Blockly.MutatorPlus');
  10. Blockly.Blocks['union'] = {
  11. init: function() {
  12. this.category = 'SET_OP';
  13. this.setHelpUrl('http://www.example.com/');
  14. this.setColour(Blockscad.Toolbox.HEX_SETOP);
  15. this.appendDummyInput()
  16. .appendField(Blockscad.Msg.UNION);
  17. this.appendStatementInput("A")
  18. .setCheck(["CSG","CAG"]);
  19. this.appendStatementInput("PLUS0")
  20. .appendField(Blockscad.Msg.PLUS)
  21. .setCheck(["CSG","CAG"]);
  22. this.setInputsInline(true);
  23. this.setPreviousStatement(true, ["CSG","CAG"]);
  24. this.setTooltip(Blockscad.Msg.UNION_TOOLTIP);
  25. // try to set up a mutator - Jennie
  26. this.setMutatorPlus(new Blockly.MutatorPlus(this));
  27. this.plusCount_ = 0;
  28. },
  29. mutationToDom: function() {
  30. if (!this.plusCount_) {
  31. return null;
  32. }
  33. var container = document.createElement('mutation');
  34. if (this.plusCount_) {
  35. container.setAttribute('plus',this.plusCount_);
  36. }
  37. return container;
  38. },
  39. domToMutation: function(xmlElement) {
  40. this.plusCount_ = parseInt(xmlElement.getAttribute('plus'), 10);
  41. var mytype = this.getInput('A').connection.check_;
  42. for (var x = 1; x <= this.plusCount_; x++) {
  43. this.appendStatementInput('PLUS' + x)
  44. .appendField(Blockscad.Msg.PLUS)
  45. .setCheck(mytype);
  46. }
  47. if (this.plusCount_ >= 1) {
  48. this.setMutatorMinus(new Blockly.MutatorMinus(this));
  49. }
  50. },
  51. updateShape_ : function(num) {
  52. if (num == 1) {
  53. this.plusCount_++;
  54. var mytype = this.getInput('A').connection.check_;
  55. var plusInput = this.appendStatementInput('PLUS' + this.plusCount_)
  56. .appendField(Blockscad.Msg.PLUS)
  57. .setCheck(mytype);
  58. } else if (num == -1) {
  59. this.removeInput('PLUS' + this.plusCount_);
  60. this.plusCount_--;
  61. }
  62. if (this.plusCount_ >= 1) {
  63. if (this.plusCount_ == 1) {
  64. this.setMutatorMinus(new Blockly.MutatorMinus(this));
  65. this.render();
  66. }
  67. } else {
  68. this.mutatorMinus.dispose();
  69. this.mutatorMinus = null;
  70. this.render();
  71. }
  72. },
  73. setType: function(type) {
  74. if (!this.workspace) {
  75. // Block has been deleted.
  76. return;
  77. }
  78. //console.log("setting union type to",type);
  79. this.previousConnection.setCheck(type);
  80. this.getInput('A').connection.setCheck(type);
  81. this.getInput('PLUS0').connection.setCheck(type);
  82. for (var i = 1; i <= this.plusCount_; i++) {
  83. this.getInput('PLUS' + i).connection.setCheck(type);
  84. }
  85. }
  86. };
  87. Blockly.Blocks['difference'] = {
  88. init: function() {
  89. this.category = 'SET_OP';
  90. this.setHelpUrl('http://www.example.com/');
  91. this.setColour(Blockscad.Toolbox.HEX_SETOP);
  92. this.appendDummyInput()
  93. .appendField(Blockscad.Msg.DIFFERENCE);
  94. this.appendStatementInput("A")
  95. .setCheck(["CSG",'CAG']);
  96. this.appendStatementInput("MINUS0")
  97. .appendField(Blockscad.Msg.MINUS)
  98. .setCheck(['CSG','CAG']);
  99. this.setInputsInline(true);
  100. this.setPreviousStatement(true, ['CSG','CAG']);
  101. this.setTooltip(Blockscad.Msg.DIFFERENCE_TOOLTIP);
  102. // try to set up a mutator - Jennie
  103. this.setMutatorPlus(new Blockly.MutatorPlus(this));
  104. this.minusCount_ = 0;
  105. },
  106. mutationToDom: function() {
  107. if (!this.minusCount_) {
  108. return null;
  109. }
  110. var container = document.createElement('mutation');
  111. if (this.minusCount_) {
  112. container.setAttribute('minus',this.minusCount_);
  113. }
  114. return container;
  115. },
  116. domToMutation: function(xmlElement) {
  117. this.minusCount_ = parseInt(xmlElement.getAttribute('minus'), 10);
  118. var mytype = this.getInput('A').connection.check_;
  119. for (var x = 1; x <= this.minusCount_; x++) {
  120. this.appendStatementInput('MINUS' + x)
  121. .appendField(Blockscad.Msg.MINUS)
  122. .setCheck(mytype);
  123. }
  124. if (this.minusCount_ >= 1) {
  125. this.setMutatorMinus(new Blockly.MutatorMinus(this));
  126. }
  127. },
  128. updateShape_ : function(num) {
  129. if (num == 1) {
  130. this.minusCount_++;
  131. var mytype = this.getInput('A').connection.check_;
  132. var minusInput = this.appendStatementInput('MINUS' + this.minusCount_)
  133. .appendField(Blockscad.Msg.MINUS)
  134. .setCheck(mytype);
  135. } else if (num == -1) {
  136. this.removeInput('MINUS' + this.minusCount_);
  137. this.minusCount_--;
  138. }
  139. if (this.minusCount_ >= 1) {
  140. if (this.minusCount_ == 1) {
  141. this.setMutatorMinus(new Blockly.MutatorMinus(this));
  142. this.render();
  143. }
  144. } else {
  145. this.mutatorMinus.dispose();
  146. this.mutatorMinus = null;
  147. this.render();
  148. }
  149. },
  150. setType: function(type) {
  151. if (!this.workspace) {
  152. // Block has been deleted.
  153. return;
  154. }
  155. //console.log("setting diff type to",type);
  156. this.previousConnection.setCheck(type);
  157. this.getInput('A').connection.setCheck(type);
  158. this.getInput('MINUS0').connection.setCheck(type);
  159. for (var i = 1; i
  160. <= this.minusCount_; i++) {
  161. this.getInput('MINUS' + i).connection.setCheck(type);
  162. }
  163. }
  164. };
  165. Blockly.Blocks['intersection'] = {
  166. init: function() {
  167. this.category = 'SET_OP';
  168. this.setHelpUrl('http://www.example.com/');
  169. this.setColour(Blockscad.Toolbox.HEX_SETOP);
  170. this.appendDummyInput()
  171. .appendField(Blockscad.Msg.INTERSECTION);
  172. this.appendStatementInput("A")
  173. .setCheck(["CSG","CAG"]);
  174. this.appendStatementInput("WITH0")
  175. .appendField(Blockscad.Msg.WITH)
  176. .setCheck(["CSG","CAG"]);
  177. this.setInputsInline(true);
  178. this.setPreviousStatement(true, ["CSG",'CAG']);
  179. this.setTooltip(Blockscad.Msg.INTERSECTION_TOOLTIP);
  180. this.setMutatorPlus(new Blockly.MutatorPlus(this));
  181. this.withCount_ = 0;
  182. },
  183. mutationToDom: function() {
  184. if (!this.withCount_) {
  185. return null;
  186. }
  187. var container = document.createElement('mutation');
  188. if (this.withCount_) {
  189. container.setAttribute('with',this.withCount_);
  190. }
  191. return container;
  192. },
  193. domToMutation: function(xmlElement) {
  194. this.withCount_ = parseInt(xmlElement.getAttribute('with'), 10);
  195. var mytype = this.getInput('A').connection.check_;
  196. for (var x = 1; x <= this.withCount_; x++) {
  197. this.appendStatementInput('WITH' + x)
  198. .appendField(Blockscad.Msg.WITH)
  199. .setCheck(mytype);
  200. }
  201. if (this.withCount_ >= 1) {
  202. this.setMutatorMinus(new Blockly.MutatorMinus(this));
  203. }
  204. },
  205. updateShape_ : function(num) {
  206. if (num == 1) {
  207. this.withCount_++;
  208. var mytype = this.getInput('A').connection.check_;
  209. var withInput = this.appendStatementInput('WITH' + this.withCount_)
  210. .appendField(Blockscad.Msg.WITH)
  211. .setCheck(mytype);
  212. } else if (num == -1) {
  213. this.removeInput('WITH' + this.withCount_);
  214. this.withCount_--;
  215. }
  216. if (this.withCount_ >= 1) {
  217. if (this.withCount_ == 1) {
  218. this.setMutatorMinus(new Blockly.MutatorMinus(this));
  219. this.render();
  220. }
  221. } else {
  222. this.mutatorMinus.dispose();
  223. this.mutatorMinus = null;
  224. this.render();
  225. }
  226. },
  227. setType: function(type) {
  228. if (!this.workspace) {
  229. // Block has been deleted.
  230. return;
  231. }
  232. //console.log("setting intersect type to",type);
  233. this.previousConnection.setCheck(type);
  234. this.getInput('A').connection.setCheck(type);
  235. this.getInput('WITH0').connection.setCheck(type);
  236. for (var i = 1; i
  237. <= this.withCount_; i++) {
  238. this.getInput('WITH' + i).connection.setCheck(type);
  239. }
  240. }
  241. };
  242. Blockly.Blocks['hull'] = {
  243. init: function() {
  244. this.category = 'SET_OP';
  245. this.setHelpUrl('http://www.example.com/');
  246. this.setColour(Blockscad.Toolbox.HEX_SETOP);
  247. this.appendDummyInput()
  248. .appendField(Blockscad.Msg.CONVEX_HULL);
  249. this.appendStatementInput("A")
  250. .setCheck(["CSG","CAG"]);
  251. this.appendStatementInput("WITH0")
  252. .appendField(Blockscad.Msg.WITH)
  253. .setCheck(["CSG","CAG"]);
  254. this.setInputsInline(true);
  255. this.setPreviousStatement(true, ["CSG","CAG"]);
  256. this.setTooltip(Blockscad.Msg.HULL_TOOLTIP);
  257. // try to set up a mutator - Jennie
  258. this.setMutatorPlus(new Blockly.MutatorPlus(this));
  259. this.withCount_ = 0;
  260. },
  261. mutationToDom: function() {
  262. if (!this.withCount_) {
  263. return null;
  264. }
  265. var container = document.createElement('mutation');
  266. if (this.withCount_) {
  267. container.setAttribute('with',this.withCount_);
  268. }
  269. return container;
  270. },
  271. domToMutation: function(xmlElement) {
  272. this.withCount_ = parseInt(xmlElement.getAttribute('with'), 10);
  273. var mytype = this.getInput('A').connection.check_;
  274. for (var x = 1; x <= this.withCount_; x++) {
  275. this.appendStatementInput('WITH' + x)
  276. .appendField(Blockscad.Msg.WITH)
  277. .setCheck(mytype);
  278. }
  279. if (this.withCount_ >= 1) {
  280. this.setMutatorMinus(new Blockly.MutatorMinus(this));
  281. }
  282. },
  283. updateShape_ : function(num) {
  284. if (num == 1) {
  285. this.withCount_++;
  286. var mytype = this.getInput('A').connection.check_;
  287. var withInput = this.appendStatementInput('WITH' + this.withCount_)
  288. .appendField(Blockscad.Msg.WITH)
  289. .setCheck(mytype);
  290. } else if (num == -1) {
  291. this.removeInput('WITH' + this.withCount_);
  292. this.withCount_--;
  293. }
  294. if (this.withCount_ >= 1) {
  295. if (this.withCount_ == 1) {
  296. this.setMutatorMinus(new Blockly.MutatorMinus(this));
  297. this.render();
  298. }
  299. } else {
  300. this.mutatorMinus.dispose();
  301. this.mutatorMinus = null;
  302. this.render();
  303. }
  304. },
  305. setType: function(type) {
  306. if (!this.workspace) {
  307. // Block has been deleted.
  308. return;
  309. }
  310. //console.log("setting union type to",type);
  311. this.previousConnection.setCheck(type);
  312. this.getInput('A').connection.setCheck(type);
  313. this.getInput('WITH0').connection.setCheck(type);
  314. for (var i = 1; i <= this.withCount_; i++) {
  315. this.getInput('WITH' + i).connection.setCheck(type);
  316. }
  317. }
  318. };