hanson_blockly_test/behaviourEditor.js

142 lines
4.4 KiB
JavaScript

// behaviourEditor.js
// Define custom blocks
Blockly.defineBlocksWithJsonArray([
{
"type": "onStartup",
"message0": "On Startup %1",
"args0": [
{
"type": "input_statement",
"name": "DO"
}
],
"colour": 230,
"tooltip": "Triggers when the application starts.",
"helpUrl": ""
},
{
"type": "onFaceDetect",
"message0": "On Face Detect %1",
"args0": [
{
"type": "input_statement",
"name": "DO"
}
],
"colour": 230,
"tooltip": "Triggers when a face is detected.",
"helpUrl": ""
},
{
"type": "playAnimation",
"message0": "Play %1 with arg0 %2 and arg1 %3",
"args0": [
{
"type": "field_dropdown",
"name": "ANIMATION",
"options": [
["asdf.anim", "ANIMATION_1"],
["idle.anim", "ANIMATION_2"],
["walk.anim", "ANIMATION_3"]
]
},
{
"type": "input_value",
"name": "arg0", // Input for speed
"check": "Number" // Optional: Check for number type
},
{
"type": "input_value",
"name": "arg1", // Input for duration
"check": "Number" // Optional: Check for number type
}
],
"previousStatement": null, // Allow connection to previous blocks
"nextStatement": null, // Allow connection to next blocks
"colour": 160,
"tooltip": "Plays the selected animation with specified speed and duration.",
"helpUrl": ""
},
{
"type": "print",
"message0": "Print %1",
"args0": [
{
"type": "field_input",
"name": "TEXT",
"text": "Hello, World!" // Default text
}
],
"colour": 210,
"tooltip": "Prints the specified text.",
"helpUrl": ""
},
{
"type": "constant_variable",
"message0": "Builtin Output %1",
"args0": [
{
"type": "field_dropdown",
"name": "CONSTANT",
"options": [
["faceDetectX", "faceDetectX"],
["faceDetectY", "faceDetectY"],
["analogRead()", "analogRead()"],
["sine", "sine"]
]
}
],
"output": null, // This block will output a value
"colour": 230,
"tooltip": "Select one of the predefined constants.",
"helpUrl": ""
}
]);
// Generate JavaScript code for custom blocks
Blockly.JavaScript['onStartup'] = function (block) {
const code = Blockly.JavaScript.statementToCode(block, 'DO');
return `function onStartup() {\n${code}}\nonStartup();\n`;
};
Blockly.JavaScript['onFaceDetect'] = function (block) {
const code = Blockly.JavaScript.statementToCode(block, 'DO');
return `function onFaceDetect() {\n${code}}\nonFaceDetect();\n`;
};
Blockly.JavaScript['playAnimation'] = function(block) {
const animation = block.getFieldValue('ANIMATION');
const speed = Blockly.JavaScript.valueToCode(block, 'SPEED', Blockly.JavaScript.ORDER_ATOMIC) || '1'; // Default speed
const duration = Blockly.JavaScript.valueToCode(block, 'DURATION', Blockly.JavaScript.ORDER_ATOMIC) || '1000'; // Default duration
return `playAnimation("${animation}", ${speed}, ${duration});\n`;
};
// Generate code for the print block
Blockly.JavaScript['print'] = function (block) {
const text = block.getFieldValue('TEXT');
return `console.log("${text}");\n`; // Prints the text to the console
};
// Generate JavaScript code for the constant variable block
Blockly.JavaScript['constant_variable'] = function(block) {
const constant = block.getFieldValue('CONSTANT');
return constant; // Returns the selected constant
};
// Define the function that plays the animation
function playAnimation(animation) {
console.log(`Playing: ${animation}`);
}
// Example of how to log all variables (if needed)
function logAllVariables(workspace) {
const variableMap = workspace.getVariableMap();
const variables = variableMap.getAllVariables();
variables.forEach(variable => {
console.log(`Variable: ${variable.name}`);
});
}