From 371b7e2fdba04e39cc7ea39750217643c9268786 Mon Sep 17 00:00:00 2001 From: sussy layers dev <72522395+jwklong@users.noreply.github.com> Date: Thu, 16 Nov 2023 21:46:45 +0000 Subject: [PATCH] functions --- src/lib/Toolbox/Toolbox.xml | 7 +++ src/resources/blocks/functions.js | 95 +++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/resources/blocks/functions.js diff --git a/src/lib/Toolbox/Toolbox.xml b/src/lib/Toolbox/Toolbox.xml index 65e2b44..3bbc3db 100644 --- a/src/lib/Toolbox/Toolbox.xml +++ b/src/lib/Toolbox/Toolbox.xml @@ -78,6 +78,13 @@ + + + + + + + diff --git a/src/resources/blocks/functions.js b/src/resources/blocks/functions.js new file mode 100644 index 0000000..f4dbf94 --- /dev/null +++ b/src/resources/blocks/functions.js @@ -0,0 +1,95 @@ +import javascriptGenerator from '../javascriptGenerator'; +import registerBlock from '../register'; + +const categoryPrefix = 'functions_'; +const categoryColor = '#5531D6'; + +function register() { + // function + registerBlock(`${categoryPrefix}create`, { + message0: 'function %1 %2 %3', + args0: [ + { + "type": "field_input", + "name": "ID", + "text": "id", + "spellcheck": false + }, + { + "type": "input_dummy" + }, + { + "type": "input_statement", + "name": "FUNC" + } + ], + nextStatement: null, + inputsInline: true, + colour: categoryColor, + }, (block) => { + const ID = block.getFieldValue('ID') + const FUNC = javascriptGenerator.statementToCode(block, 'FUNC'); + + const code = `function ${ID}() { ${FUNC} }`; + return `${code}\n`; + }) + + // return + registerBlock(`${categoryPrefix}return`, { + message0: 'return %1', + args0: [ + { + "type": "input_value", + "name": "VALUE", + } + ], + previousStatement: null, + inputsInline: true, + colour: categoryColor, + }, (block) => { + const VALUE = javascriptGenerator.valueToCode(block, 'VALUE', javascriptGenerator.ORDER_ATOMIC); + const code = `return ${VALUE || ''}`; + return `${code}\n`; + }) + + // call + registerBlock(`${categoryPrefix}call`, { + message0: 'call %1', + args0: [ + { + "type": "field_input", + "name": "ID", + "text": "id", + "spellcheck": false + }, + ], + previousStatement: null, + nextStatement: null, + inputsInline: true, + colour: categoryColor, + }, (block) => { + const ID = block.getFieldValue('ID') + const code = `${ID}()`; + return `${code}\n`; + }) + + // call + registerBlock(`${categoryPrefix}callreporter`, { + message0: 'call %1', + args0: [ + { + "type": "field_input", + "name": "ID", + "text": "id", + "spellcheck": false + }, + ], + output: null, + inputsInline: true, + colour: categoryColor, + }, (block) => { + const ID = block.getFieldValue('ID') + return [`${ID}()\n`, javascriptGenerator.ORDER_ATOMIC]; + }) +} +export default register;