From f78abcc674839a30639f804fad43274889c2e216 Mon Sep 17 00:00:00 2001 From: bordun Date: Thu, 29 Oct 2020 11:23:57 +0100 Subject: [PATCH] - `set()`: add supports for wildcards in the id parameter - `set()`: add a 3rd optional parameter for preventing further script executions and/or osc messages from being sent --- CHANGELOG.md | 6 +++++- resources/docs/docs/widgets/scripting.md | 25 +++++++++++++++--------- src/client/widgets/scripts/script-vm.js | 19 +++++++++++++++--- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52f68a6a..bcf5f81f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,11 @@ - widgets - all: add `html` property to allow inserting custom content in widgets (label, value, etc) and style it with the `css` property. - - scripting: `send()` now ignores the widget's `bypass` property (allows bypassing default messages and define custom ones). + +- scripting: + - `send()`: ignore the widget's `bypass` property (allows bypassing default messages and define custom ones) + - `set()`: add supports for wildcards in the id parameter + - `set()`: add a 3rd optional parameter for preventing further script executions and/or osc messages from being sent - custom module - automatically reload custom module when the file is modified diff --git a/resources/docs/docs/widgets/scripting.md b/resources/docs/docs/widgets/scripting.md index 086e3cbe..e222d805 100644 --- a/resources/docs/docs/widgets/scripting.md +++ b/resources/docs/docs/widgets/scripting.md @@ -62,12 +62,15 @@ Returns the value of the first matching widget. ---- -#### `set(id, value)` -- `id`: widget `id` as a string. Can be `"this"` to target the host widget, or `"parent"` to target the parent widget. +#### `set(id, value, options)` +- `id`: widget `id` as a string. Can be `"this"` to target the host widget, or `"parent"` to target the parent widget. `id` can contains wildcards ('\*'), in which case all matching widgets will be affected **except** the emitting widget. - `value`: new value for the widget. +- `options` (optional): `object`, accepts the following items: + - `sync: false` (prevents widgets from triggering synchronization and scripts) + - `send: false` (prevents widgets from sending osc messages) -Sets the value of the first matching widget. -If the event that triggered the script's execution was initiated by a user interaction, this will make the widget send its value as well. +Sets the value of matching widgets. +If the event that triggered the script's execution was initiated by a user interaction, this will make the widget send its value as well (unless `options` is set to `{send: false}`). ---- @@ -76,6 +79,8 @@ If the event that triggered the script's execution was initiated by a user inter - `address`: osc address, must start with a `/` - `args`: value or `{type: "OSC_TYPE_LETTER", value: VALUE}` `object` +Sends an osc message. + If the event that triggered the script's execution was not initiated by a user interaction, this function will have no effect. This function ignores the widget's `bypass` property. @@ -94,7 +99,7 @@ Returns the property called `"name"` of the first matching widget. - `id`: widget `id` as a string. Can be `"this"` to target the host widget, or `"parent"` to target the parent widget. - `name`: property name. -Forcers a widget to check if one of its properties has changed and update itself if needed. +Forces a widget to check if one of its properties has changed and update itself if needed. ---- @@ -175,9 +180,11 @@ Sets the scroll state of a container. #### `toolbar(i1, i2, ...)` - `iX`: menu entry index -Trigger toolbar action at specified index. Examples: +Trigger toolbar action at specified index. -- `toolbar(0, 0)` -> Open a new session -- `toolbar(3)` -> Toggle full screen +!!! example "Examples" -Actions will only be triggered if initiated with a user interaction. Fullscreen cannot be toggled with a simulated interaction (i.e. using `/SET`) + - `toolbar(0, 0)` -> Open a new session + - `toolbar(3)` -> Toggle full screen + + Actions will only be triggered if initiated with a user interaction. Fullscreen cannot be toggled with a simulated interaction (i.e. using `/SET`) diff --git a/src/client/widgets/scripts/script-vm.js b/src/client/widgets/scripts/script-vm.js index 521eec8d..a4d55f11 100644 --- a/src/client/widgets/scripts/script-vm.js +++ b/src/client/widgets/scripts/script-vm.js @@ -59,7 +59,7 @@ class ScriptVm extends Vm { super.registerGlobals() - this.sandbox.contentWindow.set = (id, value)=>{ + this.sandbox.contentWindow.set = (id, value, extraOptions = {send: true, sync: true})=>{ var options = this.getValueOptions() options.fromScript = true @@ -67,11 +67,24 @@ class ScriptVm extends Vm { // if (id === options.id) options.sync = false // loop stop // if (this.getWidget() === options.widget) options.sync = false // loop stop - var widgets = this.resolveId(id) + if (extraOptions.send === false) options.send = false + if (extraOptions.sync === false) options.sync = false + + var widgets + if (id.includes('*')) { + var widget = this.getWidget() + if (widget.builtIn) widget = widget.parent + widgets = this.resolveId( + Object.keys(widgetManager.idRoute).filter(key => key.match(new RegExp('^' + id.replace(/\*/g, '.*') + '$'))) + ).filter(w => w !== widget) + } else { + widgets = this.resolveId(id) + } + for (var i = widgets.length - 1; i >= 0; i--) { - return widgets[i].setValue(value, options) + widgets[i].setValue(value, options) }