- Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathGUI.js
More file actions
Latest commit
155 lines (131 loc) · 4.23 KB
/
Copy pathGUI.js
File metadata and controls
155 lines (131 loc) · 4.23 KB
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
import*asutilfrom'/src/utils.js'
importdatfrom'/vendor/dat.gui.js'
constguiTypes=[
'color',
'button',
'input',
'slider',
'chooser',
'monitor',
'switch',
]
/** @class */
classGUI{
/**
* @param {Object} template A set of name/object pairs, one per UI element
*
* @example
* const gui = new GUI ({
* opacity: {
* slider: [canvas.opacity, [0, 1, 0.1]],
* cmd: val => canvas.setOpacity(val),
* },
* download: {
* button: () => util.downloadBlob(data, 'data.json', false),
* },
* ...
* })
*/
// the default width is 245. You can change it via width below
constructor(template,width=null){
this.template=template
this.controllers={}
this.values={}// the key/val's from each template
this.gui=newdat.GUI()
if(width)this.gui.width=width
constguis=[this.gui]
constparseGuis=obj=>{
util.forLoop(obj,(obj,key)=>{
if(this.isFolder(obj)){
console.log('new folder',obj,key)
this.gui=this.gui.addFolder(key)
guis.push(this.gui)
parseGuis(obj)
guis.pop()
this.gui=guis.at(-1)
}else{
// console.log('new ui', obj, key)
// obj.type = this.objType(obj)
this.controllers[key]=this.addUI(obj,key)
}
})
}
parseGuis(template)
console.log('controllers, values',this.controllers,this.values,guis)
}
close(){
this.gui.close()
}
open(){
this.gui.open()
}
objType(obj){
letkeys=Object.keys(obj)
keys=keys.filter(elem=>elem!=='cmd')
if(keys.length!=1)returnfalse
constkey=keys[0]
returnguiTypes.includes(key) ? key : false
}
isFolder(obj){
returnthis.objType(obj)===false
}
// /**
// *
// * @param {Object} obj A gui object with two optional objects: 'val' and 'cmd'
// * @param {string} key The name of the gui
// * @returns A dat.gui control object
// */
addUI(obj,key){
consttype=this.objType(obj)
if(type===false)throwError('GUI type error:'+obj)
letval=obj[type]
letcmd=obj.cmd
letcontrol,extent
if(type==='monitor')cmd=()=>val[0][val[1]]
// if (type === 'button') cmd = val
if(['slider','chooser'].includes(type))[val,extent]=val
this.values[key]=val
console.log('\n--- addUI obj',obj,'type',type)
console.log('key:',key,'val:',val,'cmd:',cmd)
switch(type){
case'slider':
const[min,max,step=1]=extent
control=this.gui.add(this.values,key,min,max).step(step)
break
case'chooser':
control=this.gui.add(this.values,key,extent)
break
case'color':
control=this.gui.addColor(this.values,key)
break
// case 'button':
// control = this.gui.add(this.values, val)
// break
case'switch':
case'button':
case'input':
control=this.gui.add(this.values,key)
break
case'monitor':
control=this.gui.add(val[0],val[1])
break
default:
throwError(`Controller.addUI: bad type: ${type}`)
}
// initialize: set model etc initial values to this value
if(!['monitor','button','switch'].includes(type))cmd(val)
if(cmd){
// if (val === 'listen') control.listen().onChange(cmd)
if(type==='monitor'){
control.listen()//.onChange(cmd)}
}else{
control.onChange(cmd)
}
}
returncontrol
}
// updateGui(name, value) {
// console.log('updateGui name, value', name, value)
// }
}
exportdefaultGUI