Skip to content

Commit

Permalink
add blockly to javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesoteriou committed Oct 22, 2019
1 parent 0a497a1 commit 4b4bc82
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
76 changes: 76 additions & 0 deletions blocks/xDrone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
Blockly.defineBlocksWithJsonArray([
{
type: "fly",
message0: "Fly %1 Take off %2 %3 Land",
args0: [
{
type: "input_dummy"
},
{
type: "input_dummy"
},
{
type: "input_statement",
name: "commands"
}
],
colour: 230,
tooltip: "",
helpUrl: "fly"
},
{
type: "move",
message0: "Move %1 for %2 seconds",
args0: [
{
type: "field_dropdown",
name: "Direction",
options: [
["up", "UP"],
["down", "DOWN"],
["forward", "FORWARD"],
["backwards", "BACK"],
["right", "RIGHT"],
["left", "LEFT"]
]
},
{
type: "field_number",
name: "duration",
value: 1,
min: 0,
max: 10,
precision: 0.1
}
],
previousStatement: null,
nextStatement: null,
colour: 230,
tooltip: "move Forward",
helpUrl: ""
},
{
type: "rotate",
message0: "Rotate %1 for %2 seconds",
args0: [
{
type: "field_dropdown",
name: "direction",
options: [["right", "ROTATERIGHT"], ["left", "ROTATELEFT"]]
},
{
type: "field_number",
name: "duration",
value: 1,
min: 0,
max: 10,
precision: 0.1
}
],
previousStatement: null,
nextStatement: null,
colour: 230,
tooltip: "move Forward",
helpUrl: ""
}
]);
28 changes: 28 additions & 0 deletions generators/javascript/xDrone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
goog.provide("Blockly.JavaScript.xDrone");
goog.require("Blockly.JavaScript");

Blockly.JavaScript["fly"] = function(block) {
var statements_commands = Blockly.JavaScript.statementToCode(
block,
"commands"
);
var code =
"fly() {\n" + " TAKEOFF()\n" + statements_commands + " LAND()\n" + "}";
return code;
};

Blockly.JavaScript["move"] = function(block) {
var dropdown_direction = block.getFieldValue("Direction");
var number_duration = block.getFieldValue("duration");

var code = dropdown_direction + "(" + number_duration + ")\n";
return code;
};

Blockly.JavaScript["rotate"] = function(block) {
var dropdown_direction = block.getFieldValue("direction");
var number_duration = block.getFieldValue("duration");

var code = dropdown_direction + "(" + number_duration + ")\n";
return code;
};

0 comments on commit 4b4bc82

Please sign in to comment.