-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathopc.js
367 lines (338 loc) · 10.5 KB
/
opc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
class OPC {
static options = {};
constructor() {
this.options = {};
this.collapsed = false;
this.osc = null;
}
static setOSC(server, port = null) {
// you can set the OSC server and port here
// this will be used to send OSC messages when a variable changes
// This will also be used to receive OSC messages to update the variables
//load the OSC library
if (typeof window.OSC === 'undefined') {
let script = document.createElement('script');
script.onload = function () {
console.log('OSC library loaded');
//initialize OSC
OPC.loadOSC(server, port);
};
script.onerror = function () {
console.log('OSC library failed to load');
};
script.src = 'https://cdn.jsdelivr.net/npm/[email protected]/dist/osc-browser.min.js';
document.head.appendChild(script);
}
}
static loadOSC(server, port = null) {
//initialize OSC
OPC.osc = new osc.WebSocketPort({
url: `${server}` + (port ? `:${port}` : ''), // URL to your Web Socket server.
metadata: true
});
OPC.osc.on('open', function () {
console.log('OSC connection opened');
});
OPC.osc.on('close', function () {
console.log('OSC connection closed. Trying again in 5 seconds...');
//try again in 5 seconds
setTimeout(function () {
OPC.osc.open();
}, 5000);
});
OPC.osc.on('error', function (error) {
console.log('OSC connection error. Trying again in 5 seconds...');
//try again in 5 seconds
setTimeout(function () {
OPC.osc.open();
}, 5000);
});
OPC.osc.on('message', function (message) {
//set variables based on message
// console.log('OSC message:', message);
if (message.address) {
let variableName = message.address.slice(1); //remove the first slash
if (OPC.options[variableName]) {
if (message.args.length === 1) {
let value = message.args[0];
if (typeof message.args[0] === 'object') {
value = message.args[0].value;
if (message.args[0].type === 'f') {
value = parseFloat(value);
} else if (message.args[0].type === 'i') {
value = parseInt(value);
} else if (message.args[0].type === 's') {
value = value.split(',');
}
}
OPC._set(variableName, value);
}
}
}
});
OPC.osc.on('ready', function () {
console.log('OSC ready');
});
OPC.osc.open();
}
static oscSendMessage(varName, value) {
let type;
if (Array.isArray(value)) {
type = 's';
value = value.join(',');
} else if (typeof value === 'string') {
type = 's';
} else if (typeof value === 'number') {
type = 'f';
} else if (typeof value === 'boolean') {
type = 'i';
value = value ? 1 : 0;
}
OPC.osc.send({
address: `/${varName}`,
args: [
{ type, value }
]
});
}
static slider(variableNameOrConfig, value, min = 0, max = null, step = null) {
let variableName, label, description;
if (typeof variableNameOrConfig === 'object') {
variableName = variableNameOrConfig.name;
value = variableNameOrConfig.value;
min = variableNameOrConfig.min ?? min;
max = variableNameOrConfig.max ?? max;
step = variableNameOrConfig.step ?? step;
label = variableNameOrConfig.label;
description = variableNameOrConfig.description;
} else {
variableName = variableNameOrConfig;
}
//check existing params
let url = new URL(document.location.href);
if (url && url.searchParams.has(variableName)) {
//if found, ignore requested value, replace with URL param
value = +url.searchParams.get(variableName);
}
max = max == null ? value * 2 : max;
step = step == null ? value / 10 : step;
this.options[variableName] = {
name: variableName,
type: 'slider',
min, max, value, step, label, description
}
return this.initVariable(this.options[variableName]);
}
static toggle(variableNameOrConfig, value = true) {
let variableName, label, description;
if (typeof variableNameOrConfig === 'object') {
variableName = variableNameOrConfig.name;
value = variableNameOrConfig.value ?? value;
label = variableNameOrConfig.label;
description = variableNameOrConfig.description;
} else {
variableName = variableNameOrConfig;
}
//check existing params
let url = new URL(document.location.href);
if (url && url.searchParams.has(variableName)) {
//if found, ignore requested value, replace with URL param
value = +url.searchParams.get(variableName) == 1 ? true : false;
}
this.options[variableName] = {
name: variableName,
type: 'toggle',
value, label, description
}
return this.initVariable(this.options[variableName]);
}
static palette(variableNameOrConfig, options, value = null) {
let variableName, label, description;
if (typeof variableNameOrConfig === 'object') {
variableName = variableNameOrConfig.name;
options = variableNameOrConfig.options;
value = variableNameOrConfig.value ?? value;
label = variableNameOrConfig.label;
description = variableNameOrConfig.description;
} else {
variableName = variableNameOrConfig;
}
//check existing params
let url = new URL(document.location.href);
if (url && url.searchParams.has(variableName)) {
//Note: query params are all strings, so turn it to array
value = url.searchParams.get(variableName).split(',');
}
this.options[variableName] = {
name: variableName,
type: 'palette',
value: value ?? options[0],
options, label, description
}
return this.initVariable(this.options[variableName]);
}
static color(variableNameOrConfig, value = '#333333') {
let variableName, label, description;
if (typeof variableNameOrConfig === 'object') {
variableName = variableNameOrConfig.name;
value = variableNameOrConfig.value ?? value;
label = variableNameOrConfig.label;
description = variableNameOrConfig.description;
} else {
variableName = variableNameOrConfig;
}
//check existing params
let url = new URL(document.location.href);
if (url && url.searchParams.has(variableName)) {
//if found, ignore requested value, replace with URL param
value = url.searchParams.get(variableName);
}
this.options[variableName] = {
name: variableName,
type: 'color',
value, label, description
}
return this.initVariable(this.options[variableName]);
}
static text(variableNameOrConfig, value, placeholder = null, maxChars = 1000) {
let variableName, label, description;
if (typeof variableNameOrConfig === 'object') {
variableName = variableNameOrConfig.name;
value = variableNameOrConfig.value;
placeholder = variableNameOrConfig.placeholder ?? placeholder;
maxChars = variableNameOrConfig.maxChars ?? maxChars;
label = variableNameOrConfig.label;
description = variableNameOrConfig.description;
} else {
variableName = variableNameOrConfig;
}
//check existing params
let url = new URL(document.location.href);
if (url && url.searchParams.has(variableName)) {
//if found, ignore requested value, replace with URL param
value = url.searchParams.get(variableName);
}
this.options[variableName] = {
name: variableName,
type: 'text',
max: maxChars,
value, placeholder, label, description
}
return this.initVariable(this.options[variableName]);
}
static button(variableNameOrConfig, value = 'Click Me!') {
let variableName, label, description;
if (typeof variableNameOrConfig === 'object') {
variableName = variableNameOrConfig.name;
value = variableNameOrConfig.value ?? value;
label = variableNameOrConfig.label ?? label;
description = variableNameOrConfig.description;
} else {
variableName = variableNameOrConfig;
}
//check existing params
let url = new URL(document.location.href);
if (url && url.searchParams.has(variableName)) {
//if found, ignore requested value, replace with URL param
value = url.searchParams.get(variableName);
}
this.options[variableName] = {
name: variableName,
type: 'button',
value, label, description
}
return this.initVariable(this.options[variableName]);
}
//create the same for "select"
static select(variableNameOrConfig, options, value = null) {
let variableName, label, description;
if (typeof variableNameOrConfig === 'object') {
variableName = variableNameOrConfig.name;
options = variableNameOrConfig.options;
value = variableNameOrConfig.value ?? value ?? options[Object.keys(options)[0]];
label = variableNameOrConfig.label;
description = variableNameOrConfig.description;
} else {
variableName = variableNameOrConfig;
value = value ?? options[Object.keys(options)[0]];
}
//check existing params
let url = new URL(document.location.href);
if (url && url.searchParams.has(variableName)) {
//Note: query params are all strings, so turn it to array
value = url.searchParams.get(variableName).split(',');
}
this.options[variableName] = {
name: variableName,
type: 'select',
value: value ?? options[0],
options, label, description
}
return this.initVariable(this.options[variableName]);
}
static initVariable = function (option) {
Object.defineProperty(window, option.name, {
configurable: true,
get: function () {
return OPC.options[option.name].value;
},
set: function (value) {
OPC._set(option.name, value);
if (OPC.osc) {
OPC.oscSendMessage(option.name, value);
}
}
});
this.callParentFunction('OPC', option);
return true;
}
static _set(variableName, value) {
OPC.options[variableName].value = value;
OPC.callParentFunction('OPC', OPC.options[variableName]);
if (typeof window.parameterChanged == 'function') {
window.parameterChanged(variableName, value);
}
}
static set(variableName, value) {
window[variableName] = value;
}
static buttonPressed(variableName, value) {
OPC.options[variableName].value = value;
if (typeof window.buttonPressed == 'function') {
window.buttonPressed(variableName, value);
}
}
static buttonReleased(variableName, value) {
OPC.options[variableName].value = value;
if (typeof window.buttonReleased == 'function') {
window.buttonReleased(variableName, value);
}
}
static collapse() {
OPC.collapsed = true;
OPC.callParentFunction('OPC_collapsed', OPC.collapsed);
}
static expand() {
OPC.collapsed = false;
OPC.callParentFunction('OPC_collapsed', OPC.collapsed);
}
static delete(variableName) {
if (OPC.options[variableName]) {
delete OPC.options[variableName];
delete window.variableName;
}
OPC.callParentFunction('OPC_delete', variableName);
}
static callParentFunction(functionName, arg = {}) {
// console.log(arg);
try {
//try sending as is
window.parent.postMessage({
'messageType': functionName,
'message': arg
}, '*');
} catch (error) {
console.log('postMessage', error);
}
}
}