'use strict'; goog.provide('Blockly.Blocks.touch'); goog.require('Blockly.Blocks'); goog.require('Blockly.Types'); Blockly.Blocks.servo.HUE = 225; Blockly.Blocks['touch_setup'] = { init: function() { this.appendDummyInput() // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_touchSensor.png", 140, 40, "15")); .appendField(new Blockly.FieldImage("./../blockly/media/main-touch.png", 50, 40, "15")); this.appendDummyInput() .appendField(Blockly.Msg.TOUCH_SETUP) this.setColour(Blockly.Blocks.servo.HUE); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Blocks['touch_keyboardset'] = { init: function() { this.appendDummyInput('CASE0') .appendField(Blockly.Msg.TOUCH_SWITCH); // this.appendValueInput('CASE0') // .setAlign(Blockly.ALIGN_RIGHT) // .setCheck(['Text', 'Number']) // .appendField(Blockly.Msg.TOUCH_CASE); this.appendDummyInput() .setAlign(Blockly.ALIGN_RIGHT) .appendField(Blockly.Msg.TOUCH_CASE) .appendField(new Blockly.FieldDropdown([ ["0", "0"], ["1", "1"], ["2", "2"], ["3", "3"], ["4", "4"], ["5", "5"], ["6", "6"], ["7", "7"], ["8", "8"], ["9", "9"], ["10", "10"], ["11", "11"], ]), "keyboard0"); this.appendStatementInput("DO0") .setCheck(null) .appendField(Blockly.Msg.TOUCH_DO); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(Blockly.Blocks.servo.HUE); this.setTooltip(''); this.setHelpUrl(''); this.setMutator(new Blockly.Mutator(['mod_touch_case'])); this.caseCount_ = 0; this.defaultCount_ = 0; this.updateShape_(); }, /** @return {!boolean} True if the block instance is in the workspace. */ getTouchMatchInstance: function() { return true; }, /** * Create XML to represent the number of else-if and else inputs. * @return {Element} XML storage element. * @this Blockly.Block */ mutationToDom: function() { if (!this.caseCount_) { return null; } var container = document.createElement('mutation'); if (this.caseCount_) { container.setAttribute('case', this.caseCount_); } return container; }, /** * Parse XML to restore the else-if and else inputs. * @param {!Element} xmlElement XML storage element. * @this Blockly.Block */ domToMutation: function(xmlElement) { this.caseCount_ = parseInt(xmlElement.getAttribute('case'), 10) || 0; this.updateShape_(); }, /** * Populate the mutator's dialog with this block's components. * @param {!Blockly.Workspace} workspace Mutator's workspace. * @return {!Blockly.Block} Root block in mutator. * @this Blockly.Block */ decompose: function(workspace) { var containerBlock = workspace.newBlock('mod_touch_statement'); containerBlock.initSvg(); var connection = containerBlock.nextConnection; for (var i = 1; i <= this.caseCount_; i++) { var caseBlock = workspace.newBlock('mod_touch_case'); caseBlock.initSvg(); connection.connect(caseBlock.previousConnection); connection = caseBlock.nextConnection; } return containerBlock; }, /** * Reconfigure this block based on the mutator dialog's components. * @param {!Blockly.Block} containerBlock Root block in mutator. * @this Blockly.Block */ compose: function(containerBlock) { var clauseBlock = containerBlock.nextConnection.targetBlock(); // Count number of inputs. this.caseCount_ = 0; var valueConnections = [null]; var statementConnections = [null]; while (clauseBlock) { switch (clauseBlock.type) { case 'mod_touch_case': this.caseCount_++; valueConnections.push(clauseBlock.valueConnection_); statementConnections.push(clauseBlock.statementConnection_); break; default: throw 'Unknown block type.'; } clauseBlock = clauseBlock.nextConnection && clauseBlock.nextConnection.targetBlock(); } this.updateShape_(); // Reconnect any child blocks. for (var i = 1; i <= this.caseCount_; i++) { // Blockly.Mutator.reconnect(valueConnections[i], this, 'keyboard' + i); Blockly.Mutator.reconnect(statementConnections[i], this, 'DO' + i); } }, /** * Store pointers to any connected child blocks. * @param {!Blockly.Block} containerBlock Root block in mutator. * @this Blockly.Block */ saveConnections: function(containerBlock) { var clauseBlock = containerBlock.nextConnection.targetBlock(); var i = 1; while (clauseBlock) { switch (clauseBlock.type) { case 'mod_touch_case': var inputIf = this.getInput('CASE' + i); var value = this.getFieldValue('keyboard' + i); var inputDo = this.getInput('DO' + i); // clauseBlock.valueConnection_ = // inputIf && inputIf.connection.targetConnection; clauseBlock.statementConnection_ = inputDo && inputDo.connection.targetConnection; i++; break; default: throw 'Unknown block type.'; } clauseBlock = clauseBlock.nextConnection && clauseBlock.nextConnection.targetBlock(); } }, /** * Modify this block to have the correct number of inputs. * @private * @this Blockly.Block */ updateShape_: function() { // Delete everything. // var i = 1; // while (this.getInput('CASE' + i)) { // this.removeInput('CASE' + i); // this.removeInput('DO' + i); // i++; // } // Rebuild block. for (var i = 1; i <= this.caseCount_; i++) { // this.appendValueInput('CASE' + i) // .setAlign(Blockly.ALIGN_RIGHT) // .appendField(Blockly.Msg.TOUCH_CASE) // .setCheck(['Text', 'Number']); if (!this.getInput("CASE" + i)) { this.appendDummyInput("CASE" + i) .setAlign(Blockly.ALIGN_RIGHT) .appendField(Blockly.Msg.TOUCH_CASE) .appendField(new Blockly.FieldDropdown([ ["0", "0"], ["1", "1"], ["2", "2"], ["3", "3"], ["4", "4"], ["5", "5"], ["6", "6"], ["7", "7"], ["8", "8"], ["9", "9"], ["10", "10"], ["11", "11"], ]), "keyboard" + i); this.appendStatementInput('DO' + i) .appendField(Blockly.Msg.TOUCH_DO) .setCheck(null); } } while (this.getInput('CASE' + i)) { this.removeInput('CASE' + i); this.removeInput('DO' + i); i++; } } }; Blockly.Blocks['touch_keyboardset_loose'] = { init: function() { this.appendDummyInput() .appendField(Blockly.Msg.TOUCH_THEN_LOOSE_SWITCH); // this.appendValueInput('CASE0') // .setAlign(Blockly.ALIGN_RIGHT) // .setCheck(['Text', 'Number']) // .appendField(Blockly.Msg.TOUCH_CASE); this.appendDummyInput("CASE0") .setAlign(Blockly.ALIGN_RIGHT) .appendField(Blockly.Msg.TOUCH_CASE) .appendField(new Blockly.FieldDropdown([ ["0", "0"], ["1", "1"], ["2", "2"], ["3", "3"], ["4", "4"], ["5", "5"], ["6", "6"], ["7", "7"], ["8", "8"], ["9", "9"], ["10", "10"], ["11", "11"], ]), "keyboard0"); this.appendStatementInput("DO0") .setCheck(null) .appendField(Blockly.Msg.TOUCH_DO); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(Blockly.Blocks.servo.HUE); this.setTooltip(''); this.setHelpUrl(''); this.setMutator(new Blockly.Mutator(['mod_touch_case'])); this.caseCount_ = 0; this.defaultCount_ = 0; this.updateShape_(); }, /** @return {!boolean} True if the block instance is in the workspace. */ getLooseMatchInstance: function() { return true; }, /** * Create XML to represent the number of else-if and else inputs. * @return {Element} XML storage element. * @this Blockly.Block */ mutationToDom: function() { if (!this.caseCount_) { return null; } var container = document.createElement('mutation'); if (this.caseCount_) { container.setAttribute('case', this.caseCount_); } return container; }, /** * Parse XML to restore the else-if and else inputs. * @param {!Element} xmlElement XML storage element. * @this Blockly.Block */ domToMutation: function(xmlElement) { this.caseCount_ = parseInt(xmlElement.getAttribute('case'), 10) || 0; this.updateShape_(); }, /** * Populate the mutator's dialog with this block's components. * @param {!Blockly.Workspace} workspace Mutator's workspace. * @return {!Blockly.Block} Root block in mutator. * @this Blockly.Block */ decompose: function(workspace) { var containerBlock = workspace.newBlock('mod_touch_statement'); containerBlock.initSvg(); var connection = containerBlock.nextConnection; for (var i = 1; i <= this.caseCount_; i++) { var caseBlock = workspace.newBlock('mod_touch_case'); caseBlock.initSvg(); connection.connect(caseBlock.previousConnection); connection = caseBlock.nextConnection; } return containerBlock; }, /** * Reconfigure this block based on the mutator dialog's components. * @param {!Blockly.Block} containerBlock Root block in mutator. * @this Blockly.Block */ compose: function(containerBlock) { var clauseBlock = containerBlock.nextConnection.targetBlock(); // Count number of inputs. this.caseCount_ = 0; var valueConnections = [null]; var statementConnections = [null]; while (clauseBlock) { switch (clauseBlock.type) { case 'mod_touch_case': this.caseCount_++; // valueConnections.push(clauseBlock.valueConnection_); statementConnections.push(clauseBlock.statementConnection_); break; default: throw 'Unknown block type.'; } clauseBlock = clauseBlock.nextConnection && clauseBlock.nextConnection.targetBlock(); } this.updateShape_(); // Reconnect any child blocks. for (var i = 1; i <= this.caseCount_; i++) { // Blockly.Mutator.reconnect(valueConnections[i], this, 'CASE' + i); Blockly.Mutator.reconnect(statementConnections[i], this, 'DO' + i); } }, /** * Store pointers to any connected child blocks. * @param {!Blockly.Block} containerBlock Root block in mutator. * @this Blockly.Block */ saveConnections: function(containerBlock) { var clauseBlock = containerBlock.nextConnection.targetBlock(); var i = 1; while (clauseBlock) { switch (clauseBlock.type) { case 'mod_touch_case': var inputIf = this.getInput('CASE' + i); var value = this.getFieldValue('keyboard' + i); var inputDo = this.getInput('DO' + i); // clauseBlock.valueConnection_ = // inputIf && inputIf.connection.targetConnection; clauseBlock.statementConnection_ = inputDo && inputDo.connection.targetConnection; i++; break; default: throw 'Unknown block type.'; } clauseBlock = clauseBlock.nextConnection && clauseBlock.nextConnection.targetBlock(); } }, /** * Modify this block to have the correct number of inputs. * @private * @this Blockly.Block */ updateShape_: function() { // // Delete everything. // var i = 1; // while (this.getInput('CASE' + i)) { // this.removeInput('CASE' + i); // this.removeInput('DO' + i); // i++; // } // Rebuild block. for (var i = 1; i <= this.caseCount_; i++) { // this.appendValueInput('CASE' + i) // .setAlign(Blockly.ALIGN_RIGHT) // .appendField(Blockly.Msg.TOUCH_CASE) // .setCheck(['Text', 'Number']); if (!this.getInput('CASE' + i)) { this.appendDummyInput("CASE" + i) .setAlign(Blockly.ALIGN_RIGHT) .appendField(Blockly.Msg.TOUCH_CASE) .appendField(new Blockly.FieldDropdown([ ["0", "0"], ["1", "1"], ["2", "2"], ["3", "3"], ["4", "4"], ["5", "5"], ["6", "6"], ["7", "7"], ["8", "8"], ["9", "9"], ["10", "10"], ["11", "11"], ]), "keyboard" + i); this.appendStatementInput('DO' + i) .appendField(Blockly.Msg.TOUCH_DO) .setCheck(null); } } while (this.getInput('CASE' + i)) { this.removeInput('CASE' + i); this.removeInput('DO' + i); i++; } } }; Blockly.Blocks['mod_touch_statement'] = { /** * Mutator block for if container. * @this Blockly.Block */ init: function() { this.setColour(Blockly.Blocks.servo.HUE) this.appendDummyInput() .setAlign(Blockly.ALIGN_RIGHT) .appendField(Blockly.Msg.TOUCH_SWITCH); this.setNextStatement(true); this.contextMenu = false; } }; Blockly.Blocks['mod_touch_case'] = { /** * Mutator bolck for else-if condition. * @this Blockly.Block */ init: function() { this.setColour(Blockly.Blocks.servo.HUE); this.appendDummyInput() .setAlign(Blockly.ALIGN_RIGHT) .appendField(Blockly.Msg.TOUCH_CASE); this.setPreviousStatement(true); this.setNextStatement(true); this.contextMenu = false; } }; /***********************************************************************************************************************/ /* new version touch */ Blockly.Blocks['touch_setup_1'] = { init: function() { this.appendDummyInput() // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_touchSensor.png", 140, 40, "15")); .appendField(new Blockly.FieldImage("./../blockly/media/main-touch.png", 50, 40, "15")); this.appendDummyInput() .appendField(Blockly.Msg.TOUCH_SETUP) this.setColour(Blockly.Blocks.servo.HUE); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Blocks['touch_each'] = { init: function() { this.appendDummyInput() .appendField(Blockly.Msg.TOUCH_AT) .appendField(new Blockly.FieldDropdown([ ["0", "0"], ["1", "1"], ["2", "2"], ["3", "3"], ["4", "4"], ["5", "5"], ["6", "6"], ["7", "7"], ["8", "8"], ["9", "9"], ["10", "10"], ["11", "11"], ]), "keyboard"); this.appendStatementInput('DO0') .appendField(Blockly.Msg.TOUCH_DO); this.appendDummyInput() .appendField(Blockly.Msg.TOUCH_LOOSE); this.appendStatementInput('DO1') .appendField(Blockly.Msg.TOUCH_DO); this.setPreviousStatement(true); this.setNextStatement(true); this.setColour(Blockly.Blocks.servo.HUE); } };