From 945649841714de0b40b6edbcdf22657cfb134f2a Mon Sep 17 00:00:00 2001 From: sussy layers dev <72522395+jwklong@users.noreply.github.com> Date: Thu, 9 Nov 2023 21:10:21 +0000 Subject: [PATCH] add sound category --- src/lib/Toolbox/Toolbox.xml | 3 ++ src/resources/blocks/sound.js | 34 +++++++++++++++++++++ src/resources/compiler/compileVarSection.js | 2 +- src/routes/+page.svelte | 2 ++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/resources/blocks/sound.js diff --git a/src/lib/Toolbox/Toolbox.xml b/src/lib/Toolbox/Toolbox.xml index 9abbe98..f6747f2 100644 --- a/src/lib/Toolbox/Toolbox.xml +++ b/src/lib/Toolbox/Toolbox.xml @@ -8,6 +8,9 @@ + + + diff --git a/src/resources/blocks/sound.js b/src/resources/blocks/sound.js new file mode 100644 index 0000000..a93a4ab --- /dev/null +++ b/src/resources/blocks/sound.js @@ -0,0 +1,34 @@ +import javascriptGenerator from '../javascriptGenerator'; +import registerBlock from '../register'; +import { compileVars } from '../compiler/compileVarSection.js' + +const categoryPrefix = 'sound_'; +const categoryColor = '#CF63CF'; + +function register() { + // start playing a sound (and also it needs to load lol!!) + registerBlock(`${categoryPrefix}startsound`, { + message0: 'start sound %1', + args0: [ + { + "type": "field_input", + "name": "SOUND", + "value": "https://www.myinstants.com/media/sounds/extra-reverb-fard-noise.mp3", + "spellcheck": false + }, + ], + previousStatement: null, + nextStatement: null, + inputsInline: true, + colour: categoryColor, + }, (block) => { + const SOUND = block.getFieldValue('SOUND') + const code = `var ${compileVars.new()} = new Audio(\`${encodeURI(SOUND)}\`); + ${compileVars.new()}.addEventListener("canplaythrough", (event) => { + ${compileVars.new()}.play(); + });`; + return `${code}\n`; + }) +} + +export default register; diff --git a/src/resources/compiler/compileVarSection.js b/src/resources/compiler/compileVarSection.js index d669595..d357cd8 100644 --- a/src/resources/compiler/compileVarSection.js +++ b/src/resources/compiler/compileVarSection.js @@ -1,5 +1,5 @@ const throwAwayVars = {}; // used for repeat loops -const compileVars = {}; +export const compileVars = {}; compileVars._idx = 0; compileVars.new = () => { const _listLow = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 3556fbf..35aa7b0 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -53,6 +53,7 @@ import registerCore from "../resources/blocks/core.js"; import registerControl from "../resources/blocks/control.js"; import registerSensing from "../resources/blocks/sensing.js"; + import registerSound from "../resources/blocks/sound.js"; import registerLiterals from "../resources/blocks/literals.js"; import registerOperators from "../resources/blocks/operators.js"; import registerVariables from "../resources/blocks/variables.js"; @@ -61,6 +62,7 @@ registerCore(); registerControl(); + registerSound(); registerSensing(); registerLiterals(); registerOperators();