- renamed script to onValue, draw to onDraw, touch to onTouch
- added `onCreate` script property to all widgets
This commit is contained in:
parent
e8ce280308
commit
467b2cd4c2
16 changed files with 194 additions and 133 deletions
|
|
@ -58,7 +58,7 @@ Options can contain `@{}` blocks.
|
|||
|
||||
## Custom variables: `VAR{variableName, default}`
|
||||
|
||||
This syntax creates a custom variable in the widget that can be read and modified from any widget's `script` property (see [scripting](./scripting.md#getvarid-name)). Changes made to this variable will be applied automatically.
|
||||
This syntax creates a custom variable in the widget that can be read and modified from any widget's script property (see [scripting](./scripting.md#getvarid-name)). Changes made to this variable will be applied automatically.
|
||||
|
||||
|
||||
## Javascript: `JS{ <code> }`
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ This is a convenience option that clears the canvas context and calls [`beginPat
|
|||
If this property is enabled, the widget will be redrawn at a fixed rate even if not interacted with (see [`draw`](#draw)).
|
||||
|
||||
|
||||
### `touch`
|
||||
### `onTouch`
|
||||
|
||||
This script is executed when the widget is touched, when it is released, and during the movement of the pointer while the widget is touched.
|
||||
|
||||
|
|
@ -48,10 +48,10 @@ This script has access to the same variables and functions as the `script` prope
|
|||
|
||||
|
||||
!!! note "Extra html elements"
|
||||
Elements added using the `html` property can be touched as well, `event.targetName` can be used to determine which element is touched.
|
||||
Elements added using the `html` property can be touched as well, `event.targetName` can be used to determine which element is touched.
|
||||
|
||||
|
||||
### `draw`
|
||||
### `onDraw`
|
||||
|
||||
This script is executed when the widget should be redrawn, which is when it's touched and when it receives a value.
|
||||
|
||||
|
|
@ -70,7 +70,7 @@ Let's create a simple xy pad, with a value made of two numbers between 0 and 1.
|
|||
First, we use the `touch` property to store the touch coordinates in the `locals` object. We also call `set()` to store these in the widget's value (this way, the widget can send messages and sync with other widgets).
|
||||
|
||||
```js
|
||||
// touch property
|
||||
// onTouch
|
||||
|
||||
// store normalized coordinates
|
||||
if (event.type == "start") {
|
||||
|
|
@ -91,7 +91,7 @@ set("this", [locals.x, locals.y])
|
|||
Then, we use the `draw` property to draw a circle at the touch coordinates.
|
||||
|
||||
```js
|
||||
// draw property
|
||||
// onDraw
|
||||
|
||||
// draw circle at touch coordinates
|
||||
ctx.arc(value[0] * width, value[1] * height, 6, 0, Math.PI * 2)
|
||||
|
|
@ -104,7 +104,7 @@ ctx.stroke()
|
|||
Finally, we use the `script` property to apply limits to the values.
|
||||
|
||||
```js
|
||||
// script property
|
||||
// onValue
|
||||
|
||||
// apply limits
|
||||
var x = Math.max(0, Math.min(1, value[0])),
|
||||
|
|
@ -124,7 +124,7 @@ set("this", [x, y], {sync: false, send: false})
|
|||
Let's build a row of 20-sliders with a single widget. We first set `valueLength` to... 20 !
|
||||
|
||||
```js
|
||||
// touch property
|
||||
// onTouch
|
||||
|
||||
// store normalized coordinates
|
||||
if (event.type == "start") {
|
||||
|
|
@ -152,7 +152,7 @@ set("this", value)
|
|||
```
|
||||
|
||||
```js
|
||||
// draw property
|
||||
// onDraw
|
||||
|
||||
ctx.fillStyle = cssVars.colorFill
|
||||
ctx.globalAlpha = cssVars.alphaFillOn
|
||||
|
|
@ -166,7 +166,7 @@ for (var i in value){
|
|||
```
|
||||
|
||||
```js
|
||||
// script property
|
||||
// onValue
|
||||
|
||||
// apply limits
|
||||
for (var i in value) {
|
||||
|
|
|
|||
|
|
@ -1,33 +1,58 @@
|
|||
# Scripting
|
||||
|
||||
The `script` property allows widgets to run [Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) upon specific events.
|
||||
Widgets can run [Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) scripts upon specific events. These scripts are written in special properties under the scripting category.
|
||||
|
||||
!!! info "It's slightly different from the [javascript property syntax ](../advanced-syntaxes/#javascript-js-code)"
|
||||
Scripts should not contain any [advanced syntax](../advanced-syntaxes/) code (`JS{}`, `#{}, @{}` and `OSC{}`).
|
||||
|
||||
- no `JS{{}}` wrapper, just the code
|
||||
- `@{}`, `OSC{}`, `JS{{}}` and `#{}` blocks are replaced with their literal value before the script's compilation, therefore **it is recommended to avoid these syntaxes** and use `getProp()` and `get()` instead.
|
||||
## Events
|
||||
|
||||
## Event types
|
||||
### `onCreate`
|
||||
|
||||
!!! note ""
|
||||
The `value` event is the only one available for all widgets except the `script` widget.
|
||||
This script is called when the widget is created. If the widget has children, it will be executed after the children are created.
|
||||
|
||||
=== "Event: value"
|
||||
### `onValue`
|
||||
|
||||
When set on this mode, the script is called when the widget receives a value.
|
||||
This script is called when the widget receives a value.
|
||||
|
||||
The following variables are available in this context:
|
||||
The following variables are available in this context:
|
||||
|
||||
- `id` (`string`): id of the widget that's reponsible for the value update
|
||||
- `value`: widget's value
|
||||
- `touch`: see [Touch state](#touch-state)
|
||||
- `id` (`string`): id of the widget that's reponsible for the value update
|
||||
- `value`: widget's value
|
||||
- `touch`: see [Touch state](#touch-state)
|
||||
|
||||
??? infos "Keyboard & Matrix"
|
||||
In keyboards and matrices, `id` is the id of the child widget that triggered the event, and `value` is an array containing the children's values.
|
||||
The touched widget's value can be retrieved with:
|
||||
```javascript
|
||||
value[getIndex(id)]
|
||||
```
|
||||
??? infos "Keyboard & Matrix"
|
||||
In keyboards and matrices, `id` is the id of the child widget that triggered the event, and `value` is an array containing the children's values.
|
||||
The touched widget's value can be retrieved with:
|
||||
```javascript
|
||||
value[getIndex(id)]
|
||||
```
|
||||
|
||||
??? infos "Touch state"
|
||||
|
||||
When some widgets are touched or released, a special value event can be caught to trigger custom actions.
|
||||
|
||||
If the variable `touch` is not `undefined`, this means it holds the widget's current touch state:
|
||||
|
||||
- `0/1` for the widget's touch state (`fader`, `knob` and `xy`, `range` and `multixy`)
|
||||
- `[i, 0/1]` if the widget is multi touch (`range` and `multixy`). In this case `i` is the touched handle's index, starting with `0`.
|
||||
|
||||
```js
|
||||
if (touch !== undefined) {
|
||||
// send multi touch state
|
||||
if (touch.length) send('/touch_address', touch[0], touch[1])
|
||||
// send global touch state
|
||||
else send('/touch_address', touch)
|
||||
} else {
|
||||
// do something with the value ?
|
||||
}
|
||||
```
|
||||
|
||||
To avoid unwanted script executions, touch state events will only be caught if the script contains the word `touch`.
|
||||
|
||||
|
||||
### `onEvent`
|
||||
|
||||
This script is executed when the event specified in the `event` property occurs.
|
||||
|
||||
=== "Event: keyboard"
|
||||
|
||||
|
|
@ -45,31 +70,15 @@ The `script` property allows widgets to run [Javascript](https://developer.mozil
|
|||
- `alt` (`boolean`): `true` if alt key is pressed
|
||||
- `meta` (`boolean`): `true` if meta key is pressed
|
||||
|
||||
=== "Event: once"
|
||||
=== "Event: value"
|
||||
|
||||
When set on this mode, the script is called only once, when it is created.
|
||||
See `onValue`.
|
||||
|
||||
## Touch state
|
||||
|
||||
When some widgets are touched or released, a special event can be caught to trigger custom actions.
|
||||
### `onTouch` / `onDraw`
|
||||
|
||||
If the variable `touch` is not `undefined`, this means it holds the widget's current touch state:
|
||||
See [canvas](../canvas/).
|
||||
|
||||
- `0/1` for the widget's touch state (`fader`, `knob` and `xy`, `range` and `multixy`)
|
||||
- `[i, 0/1]` if the widget is multi touch (`range` and `multixy`). In this case `i` is the touched handle's index, starting with `0`.
|
||||
|
||||
```js
|
||||
if (touch !== undefined) {
|
||||
// send multi touch state
|
||||
if (touch.length) send('/touch_address', touch[0], touch[1])
|
||||
// send global touch state
|
||||
else send('/touch_address', touch)
|
||||
} else {
|
||||
// do something with the value ?
|
||||
}
|
||||
```
|
||||
|
||||
To avoid unwanted script executions, touch state events will only be caught if the script contains the word `touch`.
|
||||
|
||||
## Available variables
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue