|
| 1 | +/** |
| 2 | + * Blockly Apps: CoderBot Blocks |
| 3 | + * |
| 4 | + * Copyright 2012 Google Inc. |
| 5 | + * https://blockly.googlecode.com/ |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/** |
| 21 | + * @fileoverview Blocks for Blockly's CoderBot application. |
| 22 | + * @author fraser@google.com (Neil Fraser) |
| 23 | + */ |
| 24 | +'use strict'; |
| 25 | + |
| 26 | +// Extensions to Blockly's language and JavaScript generator. |
| 27 | + |
| 28 | +Blockly.Blocks['coderbot_moveForward'] = { |
| 29 | + // Block for moving forward. |
| 30 | + init: function() { |
| 31 | + this.setHelpUrl('http://code.google.com/p/blockly/wiki/Move'); |
| 32 | + this.setColour(290); |
| 33 | + this.appendDummyInput() |
| 34 | + .appendField('moveForward'); |
| 35 | + this.setPreviousStatement(true); |
| 36 | + this.setNextStatement(true); |
| 37 | + this.setTooltip('CoderBot_moveForwardTooltip'); |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +Blockly.JavaScript['coderbot_moveForward'] = function(block) { |
| 42 | + // Generate JavaScript for moving forward. |
| 43 | + return 'CoderBot.forward(2);\n'; |
| 44 | +}; |
| 45 | + |
| 46 | +Blockly.Blocks['coderbot_moveBackward'] = { |
| 47 | + // Block for moving forward. |
| 48 | + init: function() { |
| 49 | + this.setHelpUrl('http://code.google.com/p/blockly/wiki/Move'); |
| 50 | + this.setColour(290); |
| 51 | + this.appendDummyInput() |
| 52 | + .appendField('moveBackward'); |
| 53 | + this.setPreviousStatement(true); |
| 54 | + this.setNextStatement(true); |
| 55 | + this.setTooltip('CoderBot_moveBackwardTooltip'); |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +Blockly.JavaScript['coderbot_moveBackward'] = function(block) { |
| 60 | + // Generate JavaScript for moving forward. |
| 61 | + return 'CoderBot.backward(2);\n'; |
| 62 | +}; |
| 63 | + |
| 64 | +Blockly.Blocks['coderbot_turnLeft'] = { |
| 65 | + // Block for turning left or right. |
| 66 | + init: function() { |
| 67 | + this.setHelpUrl('http://code.google.com/p/blockly/wiki/Turn'); |
| 68 | + this.setColour(290); |
| 69 | + this.appendDummyInput() |
| 70 | + .appendField("turnLeft"); |
| 71 | + this.setPreviousStatement(true); |
| 72 | + this.setNextStatement(true); |
| 73 | + this.setTooltip(('CoderBot_turnTooltip')); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +Blockly.JavaScript['coderbot_turnRight'] = function(block) { |
| 78 | + // Generate JavaScript for turning left or right. |
| 79 | + var dir = block.getFieldValue('DIR'); |
| 80 | + return 'CoderBot.right(2);\n'; |
| 81 | +}; |
| 82 | + |
| 83 | +Blockly.Blocks['coderbot_turnRight'] = { |
| 84 | + // Block for turning left or right. |
| 85 | + init: function() { |
| 86 | + this.setHelpUrl('http://code.google.com/p/blockly/wiki/Turn'); |
| 87 | + this.setColour(290); |
| 88 | + this.appendDummyInput() |
| 89 | + .appendField("turnRight"); |
| 90 | + this.setPreviousStatement(true); |
| 91 | + this.setNextStatement(true); |
| 92 | + this.setTooltip(('CoderBot_turnTooltip')); |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +Blockly.JavaScript['coderbot_turnRight'] = function(block) { |
| 97 | + // Generate JavaScript for turning left or right. |
| 98 | + var dir = block.getFieldValue('DIR'); |
| 99 | + return 'CoderBot.right(2);\n'; |
| 100 | +}; |
| 101 | + |
| 102 | + |
| 103 | +Blockly.Blocks['coderbot_if'] = { |
| 104 | + // Block for 'if' conditional if there is a path. |
| 105 | + init: function() { |
| 106 | + var DIRECTIONS = |
| 107 | + [['CoderBot_pathAhead', 'isPathForward'], |
| 108 | + ['CoderBot_pathLeft', 'isPathLeft'], |
| 109 | + ['CoderBot_pathRight', 'isPathRight']]; |
| 110 | + this.setColour(210); |
| 111 | + this.appendDummyInput() |
| 112 | + .appendField(new Blockly.FieldDropdown(DIRECTIONS), 'DIR'); |
| 113 | + this.appendStatementInput('DO') |
| 114 | + .appendField('CoderBot_doCode'); |
| 115 | + this.setTooltip('CoderBot_ifTooltip'); |
| 116 | + this.setPreviousStatement(true); |
| 117 | + this.setNextStatement(true); |
| 118 | + } |
| 119 | +}; |
| 120 | + |
| 121 | + |
| 122 | +Blockly.JavaScript['coderbot_if'] = function(block) { |
| 123 | + // Generate JavaScript for 'if' conditional if there is a path. |
| 124 | + var argument = 'CoderBot.' + block.getFieldValue('DIR') + |
| 125 | + '(\'block_id_' + block.id + '\')'; |
| 126 | + var branch = Blockly.JavaScript.statementToCode(block, 'DO'); |
| 127 | + var code = 'if (' + argument + ') {\n' + branch + '}\n'; |
| 128 | + return code; |
| 129 | +}; |
| 130 | + |
| 131 | +Blockly.Blocks['coderbot_ifElse'] = { |
| 132 | + // Block for 'if/else' conditional if there is a path. |
| 133 | + init: function() { |
| 134 | + var DIRECTIONS = |
| 135 | + [[('CoderBot_pathAhead'), 'isPathForward'], |
| 136 | + [('CoderBot_pathLeft'), 'isPathLeft'], |
| 137 | + [('CoderBot_pathRight'), 'isPathRight']]; |
| 138 | + this.setColour(210); |
| 139 | + this.appendDummyInput() |
| 140 | + .appendField(new Blockly.FieldDropdown(DIRECTIONS), 'DIR'); |
| 141 | + this.appendStatementInput('DO') |
| 142 | + .appendField(('CoderBot_doCode')); |
| 143 | + this.appendStatementInput('ELSE') |
| 144 | + .appendField(('CoderBot_elseCode')); |
| 145 | + this.setTooltip(('CoderBot_ifelseTooltip')); |
| 146 | + this.setPreviousStatement(true); |
| 147 | + this.setNextStatement(true); |
| 148 | + } |
| 149 | +}; |
| 150 | + |
| 151 | +Blockly.JavaScript['coderbot_ifElse'] = function(block) { |
| 152 | + // Generate JavaScript for 'if/else' conditional if there is a path. |
| 153 | + var argument = 'CoderBot.' + block.getFieldValue('DIR') + |
| 154 | + '(\'block_id_' + block.id + '\')'; |
| 155 | + var branch0 = Blockly.JavaScript.statementToCode(block, 'DO'); |
| 156 | + var branch1 = Blockly.JavaScript.statementToCode(block, 'ELSE'); |
| 157 | + var code = 'if (' + argument + ') {\n' + branch0 + |
| 158 | + '} else {\n' + branch1 + '}\n'; |
| 159 | + return code; |
| 160 | +}; |
| 161 | + |
| 162 | +Blockly.Blocks['coderbot_forever'] = { |
| 163 | + // Do forever loop. |
| 164 | + init: function() { |
| 165 | + this.setHelpUrl('http://code.google.com/p/blockly/wiki/Repeat'); |
| 166 | + this.setColour(120); |
| 167 | + this.appendDummyInput() |
| 168 | + .appendField(('CoderBot_repeatUntil')) |
| 169 | + .appendField(new Blockly.FieldImage(CoderBot.SKIN.marker, 12, 16)); |
| 170 | + this.appendStatementInput('DO') |
| 171 | + .appendField(('CoderBot_doCode')); |
| 172 | + this.setPreviousStatement(true); |
| 173 | + this.setTooltip(('CoderBot_whileTooltip')); |
| 174 | + } |
| 175 | +}; |
| 176 | + |
| 177 | +Blockly.JavaScript['coderbot_forever'] = function(block) { |
| 178 | + // Generate JavaScript for do forever loop. |
| 179 | + var branch = Blockly.JavaScript.statementToCode(block, 'DO'); |
| 180 | + if (Blockly.JavaScript.INFINITE_LOOP_TRAP) { |
| 181 | + branch = Blockly.JavaScript.INFINITE_LOOP_TRAP.replace(/%1/g, |
| 182 | + '\'block_id_' + block.id + '\'') + branch; |
| 183 | + } |
| 184 | + return 'while (true) {\n' + branch + '}\n'; |
| 185 | +}; |
0 commit comments