mirror of
https://github.com/google/blockly.git
synced 2026-01-10 10:27:08 +01:00
157 lines
5.4 KiB
JavaScript
157 lines
5.4 KiB
JavaScript
/**
|
|
* Blockly Apps: Maze Blocks
|
|
*
|
|
* Copyright 2012 Google Inc.
|
|
* https://developers.google.com/blockly/
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Blocks for Blockly's Maze application.
|
|
* @author fraser@google.com (Neil Fraser)
|
|
*/
|
|
'use strict';
|
|
|
|
// Extensions to Blockly's language and JavaScript generator.
|
|
|
|
Blockly.Blocks['maze_moveForward'] = {
|
|
// Block for moving forward.
|
|
init: function() {
|
|
this.setColour(290);
|
|
this.appendDummyInput()
|
|
.appendField(BlocklyApps.getMsg('Maze_moveForward'));
|
|
this.setPreviousStatement(true);
|
|
this.setNextStatement(true);
|
|
this.setTooltip(BlocklyApps.getMsg('Maze_moveForwardTooltip'));
|
|
}
|
|
};
|
|
|
|
Blockly.JavaScript['maze_moveForward'] = function(block) {
|
|
// Generate JavaScript for moving forward.
|
|
return 'Maze.moveForward(\'block_id_' + block.id + '\');\n';
|
|
};
|
|
|
|
Blockly.Blocks['maze_turn'] = {
|
|
// Block for turning left or right.
|
|
init: function() {
|
|
var DIRECTIONS =
|
|
[[BlocklyApps.getMsg('Maze_turnLeft'), 'turnLeft'],
|
|
[BlocklyApps.getMsg('Maze_turnRight'), 'turnRight']];
|
|
// Append arrows to direction messages.
|
|
DIRECTIONS[0][0] += ' \u21BA';
|
|
DIRECTIONS[1][0] += ' \u21BB';
|
|
this.setColour(290);
|
|
this.appendDummyInput()
|
|
.appendField(new Blockly.FieldDropdown(DIRECTIONS), 'DIR');
|
|
this.setPreviousStatement(true);
|
|
this.setNextStatement(true);
|
|
this.setTooltip(BlocklyApps.getMsg('Maze_turnTooltip'));
|
|
}
|
|
};
|
|
|
|
Blockly.JavaScript['maze_turn'] = function(block) {
|
|
// Generate JavaScript for turning left or right.
|
|
var dir = block.getFieldValue('DIR');
|
|
return 'Maze.' + dir + '(\'block_id_' + block.id + '\');\n';
|
|
};
|
|
|
|
Blockly.Blocks['maze_if'] = {
|
|
// Block for 'if' conditional if there is a path.
|
|
init: function() {
|
|
var DIRECTIONS =
|
|
[[BlocklyApps.getMsg('Maze_pathAhead'), 'isPathForward'],
|
|
[BlocklyApps.getMsg('Maze_pathLeft'), 'isPathLeft'],
|
|
[BlocklyApps.getMsg('Maze_pathRight'), 'isPathRight']];
|
|
// Append arrows to direction messages.
|
|
DIRECTIONS[1][0] += ' \u21BA';
|
|
DIRECTIONS[2][0] += ' \u21BB';
|
|
this.setColour(210);
|
|
this.appendDummyInput()
|
|
.appendField(new Blockly.FieldDropdown(DIRECTIONS), 'DIR');
|
|
this.appendStatementInput('DO')
|
|
.appendField(BlocklyApps.getMsg('Maze_doCode'));
|
|
this.setTooltip(BlocklyApps.getMsg('Maze_ifTooltip'));
|
|
this.setPreviousStatement(true);
|
|
this.setNextStatement(true);
|
|
}
|
|
};
|
|
|
|
|
|
Blockly.JavaScript['maze_if'] = function(block) {
|
|
// Generate JavaScript for 'if' conditional if there is a path.
|
|
var argument = 'Maze.' + block.getFieldValue('DIR') +
|
|
'(\'block_id_' + block.id + '\')';
|
|
var branch = Blockly.JavaScript.statementToCode(block, 'DO');
|
|
var code = 'if (' + argument + ') {\n' + branch + '}\n';
|
|
return code;
|
|
};
|
|
|
|
Blockly.Blocks['maze_ifElse'] = {
|
|
// Block for 'if/else' conditional if there is a path.
|
|
init: function() {
|
|
var DIRECTIONS =
|
|
[[BlocklyApps.getMsg('Maze_pathAhead'), 'isPathForward'],
|
|
[BlocklyApps.getMsg('Maze_pathLeft'), 'isPathLeft'],
|
|
[BlocklyApps.getMsg('Maze_pathRight'), 'isPathRight']];
|
|
// Append arrows to direction messages.
|
|
DIRECTIONS[1][0] += ' \u21BA';
|
|
DIRECTIONS[2][0] += ' \u21BB';
|
|
this.setColour(210);
|
|
this.appendDummyInput()
|
|
.appendField(new Blockly.FieldDropdown(DIRECTIONS), 'DIR');
|
|
this.appendStatementInput('DO')
|
|
.appendField(BlocklyApps.getMsg('Maze_doCode'));
|
|
this.appendStatementInput('ELSE')
|
|
.appendField(BlocklyApps.getMsg('Maze_elseCode'));
|
|
this.setTooltip(BlocklyApps.getMsg('Maze_ifelseTooltip'));
|
|
this.setPreviousStatement(true);
|
|
this.setNextStatement(true);
|
|
}
|
|
};
|
|
|
|
Blockly.JavaScript['maze_ifElse'] = function(block) {
|
|
// Generate JavaScript for 'if/else' conditional if there is a path.
|
|
var argument = 'Maze.' + block.getFieldValue('DIR') +
|
|
'(\'block_id_' + block.id + '\')';
|
|
var branch0 = Blockly.JavaScript.statementToCode(block, 'DO');
|
|
var branch1 = Blockly.JavaScript.statementToCode(block, 'ELSE');
|
|
var code = 'if (' + argument + ') {\n' + branch0 +
|
|
'} else {\n' + branch1 + '}\n';
|
|
return code;
|
|
};
|
|
|
|
Blockly.Blocks['maze_forever'] = {
|
|
// Block for forever loop.
|
|
init: function() {
|
|
this.setColour(120);
|
|
this.appendDummyInput()
|
|
.appendField(BlocklyApps.getMsg('Maze_repeatUntil'))
|
|
.appendField(new Blockly.FieldImage(Maze.SKIN.marker, 12, 16));
|
|
this.appendStatementInput('DO')
|
|
.appendField(BlocklyApps.getMsg('Maze_doCode'));
|
|
this.setPreviousStatement(true);
|
|
this.setTooltip(BlocklyApps.getMsg('Maze_whileTooltip'));
|
|
}
|
|
};
|
|
|
|
Blockly.JavaScript['maze_forever'] = function(block) {
|
|
// Generate JavaScript for forever loop.
|
|
var branch = Blockly.JavaScript.statementToCode(block, 'DO');
|
|
if (Blockly.JavaScript.INFINITE_LOOP_TRAP) {
|
|
branch = Blockly.JavaScript.INFINITE_LOOP_TRAP.replace(/%1/g,
|
|
'\'block_id_' + block.id + '\'') + branch;
|
|
}
|
|
return 'while (true) {\n' + branch + '}\n';
|
|
};
|