From 9b80609172abbfc9fd550201ca920f5c69f14b6c Mon Sep 17 00:00:00 2001 From: Jake Date: Tue, 21 Oct 2025 09:44:06 +0800 Subject: [PATCH] first --- behaviourEditor.js | 141 +++++++++++++++++++++++++++++++++++++++++++++ index.html | 83 ++++++++++++++++++++++++++ style.css | 10 ++++ 3 files changed, 234 insertions(+) create mode 100644 behaviourEditor.js create mode 100644 index.html create mode 100644 style.css diff --git a/behaviourEditor.js b/behaviourEditor.js new file mode 100644 index 0000000..4b0a754 --- /dev/null +++ b/behaviourEditor.js @@ -0,0 +1,141 @@ +// 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}`); + }); +} + diff --git a/index.html b/index.html new file mode 100644 index 0000000..679c4fd --- /dev/null +++ b/index.html @@ -0,0 +1,83 @@ + + + + + + + Behaviour Editor + + + + + + +

Behaviour Editor: Blockly

+
+ + + + + + + \ No newline at end of file diff --git a/style.css b/style.css new file mode 100644 index 0000000..77a8976 --- /dev/null +++ b/style.css @@ -0,0 +1,10 @@ +html, body { + height: 100%; + margin: 0; + padding: 0; +} + +#blocklyDiv { + height: 480px; + width: 100%; +}