-
Notifications
You must be signed in to change notification settings - Fork 8
/
guiGlue.js
113 lines (82 loc) · 3.29 KB
/
guiGlue.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
//returns params, a stripped version of paramsGUI, without all the GUI fluff
function guiGlue(paramsGUI, optionsGUI){
//pass options to GUI e.g., { autoPlace: false }
optionsGUI = optionsGUI || {};
//extra parameter whether you want folders open or closed
optionsGUI.folded = optionsGUI.folded || false;
// params is a stripped version of paramsGUI, where everything
// but its default attributes have been removed
var params = {};
var gui = renderParameters(paramsGUI, optionsGUI, params);
//return stripped parameter object
return params;
function renderParameters(paramsGUI, optionsGUI, params){
//initial creation
var gui = new dat.GUI(optionsGUI);
//walk the parameter tree
unfurl(paramsGUI, gui, params);
function unfurl(obj, folder, params){
for (var key in obj){
var subObj = obj[key];
var leaf = isLeaf(subObj);
if (leaf){
addToFolder(key, obj, subObj, folder, params);
}
else{
//is folder
var subfolder = folder.addFolder(key);
if (!optionsGUI.folded)
subfolder.open();
params[key] = {};
unfurl(obj[key], subfolder, params[key]);
}
}
//a leaf object is one that contains no other objects
//it is critical that none of the tracked parameters is itself an object
function isLeaf(obj){
var Leaf = true;
for (var key in obj){
if (key === 'choices' && obj.display === 'selector') continue;
if (Leaf){
var isObj = (Object.prototype.toString.call( obj[key] ) != '[object Object]');
Leaf = Leaf && isObj;
}
else
continue;
}
return Leaf;
}
}
function addToFolder(key, obj, options, folder, params){
var handle;
params[key] = options.value;
var display = options.display || '';
switch (display){
case 'range':
if (options.step)
handle = folder.add(params, key, options.min, options.max).step(options.step);
else
handle = folder.add(params, key, options.min, options.max);
break;
case 'selector':
handle = folder.add(params, key, options.choices);
break;
case 'color':
handle = folder.addColor(params, key);
break;
case 'none':
break;
default:
handle = folder.add(params, key);
break;
}
if (handle && options.onChange)
handle.onChange(options.onChange);
if (handle && options.onFinishChange)
handle.onChange(options.onFinishChange);
if (handle && options.listen)
handle.listen();
}
return gui;
}
}