123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- '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);
- }
- };
|