touch.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. 'use strict';
  2. goog.provide('Blockly.Blocks.touch');
  3. goog.require('Blockly.Blocks');
  4. goog.require('Blockly.Types');
  5. Blockly.Blocks.servo.HUE = 225;
  6. Blockly.Blocks['touch_setup'] = {
  7. init: function() {
  8. this.appendDummyInput()
  9. // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_touchSensor.png", 140, 40, "15"));
  10. .appendField(new Blockly.FieldImage("./../blockly/media/main-touch.png", 50, 40, "15"));
  11. this.appendDummyInput()
  12. .appendField(Blockly.Msg.TOUCH_SETUP)
  13. this.setColour(Blockly.Blocks.servo.HUE);
  14. this.setTooltip("");
  15. this.setHelpUrl("");
  16. }
  17. };
  18. Blockly.Blocks['touch_keyboardset'] = {
  19. init: function() {
  20. this.appendDummyInput('CASE0')
  21. .appendField(Blockly.Msg.TOUCH_SWITCH);
  22. // this.appendValueInput('CASE0')
  23. // .setAlign(Blockly.ALIGN_RIGHT)
  24. // .setCheck(['Text', 'Number'])
  25. // .appendField(Blockly.Msg.TOUCH_CASE);
  26. this.appendDummyInput()
  27. .setAlign(Blockly.ALIGN_RIGHT)
  28. .appendField(Blockly.Msg.TOUCH_CASE)
  29. .appendField(new Blockly.FieldDropdown([
  30. ["0", "0"],
  31. ["1", "1"],
  32. ["2", "2"],
  33. ["3", "3"],
  34. ["4", "4"],
  35. ["5", "5"],
  36. ["6", "6"],
  37. ["7", "7"],
  38. ["8", "8"],
  39. ["9", "9"],
  40. ["10", "10"],
  41. ["11", "11"],
  42. ]), "keyboard0");
  43. this.appendStatementInput("DO0")
  44. .setCheck(null)
  45. .appendField(Blockly.Msg.TOUCH_DO);
  46. this.setPreviousStatement(true, null);
  47. this.setNextStatement(true, null);
  48. this.setColour(Blockly.Blocks.servo.HUE);
  49. this.setTooltip('');
  50. this.setHelpUrl('');
  51. this.setMutator(new Blockly.Mutator(['mod_touch_case']));
  52. this.caseCount_ = 0;
  53. this.defaultCount_ = 0;
  54. this.updateShape_();
  55. },
  56. /** @return {!boolean} True if the block instance is in the workspace. */
  57. getTouchMatchInstance: function() {
  58. return true;
  59. },
  60. /**
  61. * Create XML to represent the number of else-if and else inputs.
  62. * @return {Element} XML storage element.
  63. * @this Blockly.Block
  64. */
  65. mutationToDom: function() {
  66. if (!this.caseCount_) {
  67. return null;
  68. }
  69. var container = document.createElement('mutation');
  70. if (this.caseCount_) {
  71. container.setAttribute('case', this.caseCount_);
  72. }
  73. return container;
  74. },
  75. /**
  76. * Parse XML to restore the else-if and else inputs.
  77. * @param {!Element} xmlElement XML storage element.
  78. * @this Blockly.Block
  79. */
  80. domToMutation: function(xmlElement) {
  81. this.caseCount_ = parseInt(xmlElement.getAttribute('case'), 10) || 0;
  82. this.updateShape_();
  83. },
  84. /**
  85. * Populate the mutator's dialog with this block's components.
  86. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  87. * @return {!Blockly.Block} Root block in mutator.
  88. * @this Blockly.Block
  89. */
  90. decompose: function(workspace) {
  91. var containerBlock = workspace.newBlock('mod_touch_statement');
  92. containerBlock.initSvg();
  93. var connection = containerBlock.nextConnection;
  94. for (var i = 1; i <= this.caseCount_; i++) {
  95. var caseBlock = workspace.newBlock('mod_touch_case');
  96. caseBlock.initSvg();
  97. connection.connect(caseBlock.previousConnection);
  98. connection = caseBlock.nextConnection;
  99. }
  100. return containerBlock;
  101. },
  102. /**
  103. * Reconfigure this block based on the mutator dialog's components.
  104. * @param {!Blockly.Block} containerBlock Root block in mutator.
  105. * @this Blockly.Block
  106. */
  107. compose: function(containerBlock) {
  108. var clauseBlock = containerBlock.nextConnection.targetBlock();
  109. // Count number of inputs.
  110. this.caseCount_ = 0;
  111. var valueConnections = [null];
  112. var statementConnections = [null];
  113. while (clauseBlock) {
  114. switch (clauseBlock.type) {
  115. case 'mod_touch_case':
  116. this.caseCount_++;
  117. valueConnections.push(clauseBlock.valueConnection_);
  118. statementConnections.push(clauseBlock.statementConnection_);
  119. break;
  120. default:
  121. throw 'Unknown block type.';
  122. }
  123. clauseBlock = clauseBlock.nextConnection &&
  124. clauseBlock.nextConnection.targetBlock();
  125. }
  126. this.updateShape_();
  127. // Reconnect any child blocks.
  128. for (var i = 1; i <= this.caseCount_; i++) {
  129. // Blockly.Mutator.reconnect(valueConnections[i], this, 'keyboard' + i);
  130. Blockly.Mutator.reconnect(statementConnections[i], this, 'DO' + i);
  131. }
  132. },
  133. /**
  134. * Store pointers to any connected child blocks.
  135. * @param {!Blockly.Block} containerBlock Root block in mutator.
  136. * @this Blockly.Block
  137. */
  138. saveConnections: function(containerBlock) {
  139. var clauseBlock = containerBlock.nextConnection.targetBlock();
  140. var i = 1;
  141. while (clauseBlock) {
  142. switch (clauseBlock.type) {
  143. case 'mod_touch_case':
  144. var inputIf = this.getInput('CASE' + i);
  145. var value = this.getFieldValue('keyboard' + i);
  146. var inputDo = this.getInput('DO' + i);
  147. // clauseBlock.valueConnection_ =
  148. // inputIf && inputIf.connection.targetConnection;
  149. clauseBlock.statementConnection_ =
  150. inputDo && inputDo.connection.targetConnection;
  151. i++;
  152. break;
  153. default:
  154. throw 'Unknown block type.';
  155. }
  156. clauseBlock = clauseBlock.nextConnection &&
  157. clauseBlock.nextConnection.targetBlock();
  158. }
  159. },
  160. /**
  161. * Modify this block to have the correct number of inputs.
  162. * @private
  163. * @this Blockly.Block
  164. */
  165. updateShape_: function() {
  166. // Delete everything.
  167. // var i = 1;
  168. // while (this.getInput('CASE' + i)) {
  169. // this.removeInput('CASE' + i);
  170. // this.removeInput('DO' + i);
  171. // i++;
  172. // }
  173. // Rebuild block.
  174. for (var i = 1; i <= this.caseCount_; i++) {
  175. // this.appendValueInput('CASE' + i)
  176. // .setAlign(Blockly.ALIGN_RIGHT)
  177. // .appendField(Blockly.Msg.TOUCH_CASE)
  178. // .setCheck(['Text', 'Number']);
  179. if (!this.getInput("CASE" + i)) {
  180. this.appendDummyInput("CASE" + i)
  181. .setAlign(Blockly.ALIGN_RIGHT)
  182. .appendField(Blockly.Msg.TOUCH_CASE)
  183. .appendField(new Blockly.FieldDropdown([
  184. ["0", "0"],
  185. ["1", "1"],
  186. ["2", "2"],
  187. ["3", "3"],
  188. ["4", "4"],
  189. ["5", "5"],
  190. ["6", "6"],
  191. ["7", "7"],
  192. ["8", "8"],
  193. ["9", "9"],
  194. ["10", "10"],
  195. ["11", "11"],
  196. ]), "keyboard" + i);
  197. this.appendStatementInput('DO' + i)
  198. .appendField(Blockly.Msg.TOUCH_DO)
  199. .setCheck(null);
  200. }
  201. }
  202. while (this.getInput('CASE' + i)) {
  203. this.removeInput('CASE' + i);
  204. this.removeInput('DO' + i);
  205. i++;
  206. }
  207. }
  208. };
  209. Blockly.Blocks['touch_keyboardset_loose'] = {
  210. init: function() {
  211. this.appendDummyInput()
  212. .appendField(Blockly.Msg.TOUCH_THEN_LOOSE_SWITCH);
  213. // this.appendValueInput('CASE0')
  214. // .setAlign(Blockly.ALIGN_RIGHT)
  215. // .setCheck(['Text', 'Number'])
  216. // .appendField(Blockly.Msg.TOUCH_CASE);
  217. this.appendDummyInput("CASE0")
  218. .setAlign(Blockly.ALIGN_RIGHT)
  219. .appendField(Blockly.Msg.TOUCH_CASE)
  220. .appendField(new Blockly.FieldDropdown([
  221. ["0", "0"],
  222. ["1", "1"],
  223. ["2", "2"],
  224. ["3", "3"],
  225. ["4", "4"],
  226. ["5", "5"],
  227. ["6", "6"],
  228. ["7", "7"],
  229. ["8", "8"],
  230. ["9", "9"],
  231. ["10", "10"],
  232. ["11", "11"],
  233. ]), "keyboard0");
  234. this.appendStatementInput("DO0")
  235. .setCheck(null)
  236. .appendField(Blockly.Msg.TOUCH_DO);
  237. this.setPreviousStatement(true, null);
  238. this.setNextStatement(true, null);
  239. this.setColour(Blockly.Blocks.servo.HUE);
  240. this.setTooltip('');
  241. this.setHelpUrl('');
  242. this.setMutator(new Blockly.Mutator(['mod_touch_case']));
  243. this.caseCount_ = 0;
  244. this.defaultCount_ = 0;
  245. this.updateShape_();
  246. },
  247. /** @return {!boolean} True if the block instance is in the workspace. */
  248. getLooseMatchInstance: function() {
  249. return true;
  250. },
  251. /**
  252. * Create XML to represent the number of else-if and else inputs.
  253. * @return {Element} XML storage element.
  254. * @this Blockly.Block
  255. */
  256. mutationToDom: function() {
  257. if (!this.caseCount_) {
  258. return null;
  259. }
  260. var container = document.createElement('mutation');
  261. if (this.caseCount_) {
  262. container.setAttribute('case', this.caseCount_);
  263. }
  264. return container;
  265. },
  266. /**
  267. * Parse XML to restore the else-if and else inputs.
  268. * @param {!Element} xmlElement XML storage element.
  269. * @this Blockly.Block
  270. */
  271. domToMutation: function(xmlElement) {
  272. this.caseCount_ = parseInt(xmlElement.getAttribute('case'), 10) || 0;
  273. this.updateShape_();
  274. },
  275. /**
  276. * Populate the mutator's dialog with this block's components.
  277. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  278. * @return {!Blockly.Block} Root block in mutator.
  279. * @this Blockly.Block
  280. */
  281. decompose: function(workspace) {
  282. var containerBlock = workspace.newBlock('mod_touch_statement');
  283. containerBlock.initSvg();
  284. var connection = containerBlock.nextConnection;
  285. for (var i = 1; i <= this.caseCount_; i++) {
  286. var caseBlock = workspace.newBlock('mod_touch_case');
  287. caseBlock.initSvg();
  288. connection.connect(caseBlock.previousConnection);
  289. connection = caseBlock.nextConnection;
  290. }
  291. return containerBlock;
  292. },
  293. /**
  294. * Reconfigure this block based on the mutator dialog's components.
  295. * @param {!Blockly.Block} containerBlock Root block in mutator.
  296. * @this Blockly.Block
  297. */
  298. compose: function(containerBlock) {
  299. var clauseBlock = containerBlock.nextConnection.targetBlock();
  300. // Count number of inputs.
  301. this.caseCount_ = 0;
  302. var valueConnections = [null];
  303. var statementConnections = [null];
  304. while (clauseBlock) {
  305. switch (clauseBlock.type) {
  306. case 'mod_touch_case':
  307. this.caseCount_++;
  308. // valueConnections.push(clauseBlock.valueConnection_);
  309. statementConnections.push(clauseBlock.statementConnection_);
  310. break;
  311. default:
  312. throw 'Unknown block type.';
  313. }
  314. clauseBlock = clauseBlock.nextConnection &&
  315. clauseBlock.nextConnection.targetBlock();
  316. }
  317. this.updateShape_();
  318. // Reconnect any child blocks.
  319. for (var i = 1; i <= this.caseCount_; i++) {
  320. // Blockly.Mutator.reconnect(valueConnections[i], this, 'CASE' + i);
  321. Blockly.Mutator.reconnect(statementConnections[i], this, 'DO' + i);
  322. }
  323. },
  324. /**
  325. * Store pointers to any connected child blocks.
  326. * @param {!Blockly.Block} containerBlock Root block in mutator.
  327. * @this Blockly.Block
  328. */
  329. saveConnections: function(containerBlock) {
  330. var clauseBlock = containerBlock.nextConnection.targetBlock();
  331. var i = 1;
  332. while (clauseBlock) {
  333. switch (clauseBlock.type) {
  334. case 'mod_touch_case':
  335. var inputIf = this.getInput('CASE' + i);
  336. var value = this.getFieldValue('keyboard' + i);
  337. var inputDo = this.getInput('DO' + i);
  338. // clauseBlock.valueConnection_ =
  339. // inputIf && inputIf.connection.targetConnection;
  340. clauseBlock.statementConnection_ =
  341. inputDo && inputDo.connection.targetConnection;
  342. i++;
  343. break;
  344. default:
  345. throw 'Unknown block type.';
  346. }
  347. clauseBlock = clauseBlock.nextConnection &&
  348. clauseBlock.nextConnection.targetBlock();
  349. }
  350. },
  351. /**
  352. * Modify this block to have the correct number of inputs.
  353. * @private
  354. * @this Blockly.Block
  355. */
  356. updateShape_: function() {
  357. // // Delete everything.
  358. // var i = 1;
  359. // while (this.getInput('CASE' + i)) {
  360. // this.removeInput('CASE' + i);
  361. // this.removeInput('DO' + i);
  362. // i++;
  363. // }
  364. // Rebuild block.
  365. for (var i = 1; i <= this.caseCount_; i++) {
  366. // this.appendValueInput('CASE' + i)
  367. // .setAlign(Blockly.ALIGN_RIGHT)
  368. // .appendField(Blockly.Msg.TOUCH_CASE)
  369. // .setCheck(['Text', 'Number']);
  370. if (!this.getInput('CASE' + i)) {
  371. this.appendDummyInput("CASE" + i)
  372. .setAlign(Blockly.ALIGN_RIGHT)
  373. .appendField(Blockly.Msg.TOUCH_CASE)
  374. .appendField(new Blockly.FieldDropdown([
  375. ["0", "0"],
  376. ["1", "1"],
  377. ["2", "2"],
  378. ["3", "3"],
  379. ["4", "4"],
  380. ["5", "5"],
  381. ["6", "6"],
  382. ["7", "7"],
  383. ["8", "8"],
  384. ["9", "9"],
  385. ["10", "10"],
  386. ["11", "11"],
  387. ]), "keyboard" + i);
  388. this.appendStatementInput('DO' + i)
  389. .appendField(Blockly.Msg.TOUCH_DO)
  390. .setCheck(null);
  391. }
  392. }
  393. while (this.getInput('CASE' + i)) {
  394. this.removeInput('CASE' + i);
  395. this.removeInput('DO' + i);
  396. i++;
  397. }
  398. }
  399. };
  400. Blockly.Blocks['mod_touch_statement'] = {
  401. /**
  402. * Mutator block for if container.
  403. * @this Blockly.Block
  404. */
  405. init: function() {
  406. this.setColour(Blockly.Blocks.servo.HUE)
  407. this.appendDummyInput()
  408. .setAlign(Blockly.ALIGN_RIGHT)
  409. .appendField(Blockly.Msg.TOUCH_SWITCH);
  410. this.setNextStatement(true);
  411. this.contextMenu = false;
  412. }
  413. };
  414. Blockly.Blocks['mod_touch_case'] = {
  415. /**
  416. * Mutator bolck for else-if condition.
  417. * @this Blockly.Block
  418. */
  419. init: function() {
  420. this.setColour(Blockly.Blocks.servo.HUE);
  421. this.appendDummyInput()
  422. .setAlign(Blockly.ALIGN_RIGHT)
  423. .appendField(Blockly.Msg.TOUCH_CASE);
  424. this.setPreviousStatement(true);
  425. this.setNextStatement(true);
  426. this.contextMenu = false;
  427. }
  428. };
  429. /***********************************************************************************************************************/
  430. /* new version touch */
  431. Blockly.Blocks['touch_setup_1'] = {
  432. init: function() {
  433. this.appendDummyInput()
  434. // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_touchSensor.png", 140, 40, "15"));
  435. .appendField(new Blockly.FieldImage("./../blockly/media/main-touch.png", 50, 40, "15"));
  436. this.appendDummyInput()
  437. .appendField(Blockly.Msg.TOUCH_SETUP)
  438. this.setColour(Blockly.Blocks.servo.HUE);
  439. this.setTooltip("");
  440. this.setHelpUrl("");
  441. }
  442. };
  443. Blockly.Blocks['touch_each'] = {
  444. init: function() {
  445. this.appendDummyInput()
  446. .appendField(Blockly.Msg.TOUCH_AT)
  447. .appendField(new Blockly.FieldDropdown([
  448. ["0", "0"],
  449. ["1", "1"],
  450. ["2", "2"],
  451. ["3", "3"],
  452. ["4", "4"],
  453. ["5", "5"],
  454. ["6", "6"],
  455. ["7", "7"],
  456. ["8", "8"],
  457. ["9", "9"],
  458. ["10", "10"],
  459. ["11", "11"],
  460. ]), "keyboard");
  461. this.appendStatementInput('DO0')
  462. .appendField(Blockly.Msg.TOUCH_DO);
  463. this.appendDummyInput()
  464. .appendField(Blockly.Msg.TOUCH_LOOSE);
  465. this.appendStatementInput('DO1')
  466. .appendField(Blockly.Msg.TOUCH_DO);
  467. this.setPreviousStatement(true);
  468. this.setNextStatement(true);
  469. this.setColour(Blockly.Blocks.servo.HUE);
  470. }
  471. };