-
Notifications
You must be signed in to change notification settings - Fork 0
/
p5.gui.js
208 lines (163 loc) · 5.3 KB
/
p5.gui.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
//
(function() {
// list of guis
var guis = [];
// default slider params
var sliderMin = 0;
var sliderMax = 100;
var sliderStep = 1;
// default gui provider
var guiProvider = 'QuickSettings';
// Create a GUI using QuickSettings (or DAT.GUI or ...)
p5.prototype.createGui = function(label, x, y, provider) {
label = label || 'GUI';
x = x || 20;
y = y || 20;
provider = provider || guiProvider;
var gui;
// create a gui using the provider
if(provider === 'QuickSettings') {
if(QuickSettings) {
console.log('Creating p5.gui powered by QuickSettings.');
gui = new QSGui(label, x, y);
} else {
console.log('QuickSettings not found. Is the script included in your HTML?');
gui = new DummyGui(label, x, y);
}
} else {
console.log('Unknown GUI provider ' + provider);
gui = new DummyGui(label, x, y);
}
// add it to the list of guis
guis.push(gui);
// return it
return gui;
};
p5.prototype.removeGui = function(gui) {
// TODO: implement this
};
// update defaults used for creation of sliders
p5.prototype.sliderRange = function(vmin, vmax, vstep) {
sliderMin = vmin;
sliderMax = vmax;
sliderStep = vstep;
};
// extend default behaviour of noLoop()
p5.prototype.noLoop = function() {
this._loop = false;
for(var i = 0; i < guis.length; i++) {
guis[i].noLoop();
}
};
// extend default behaviour of loop()
p5.prototype.loop = function() {
for(var i = 0; i < guis.length; i++) {
guis[i].loop();
}
this._loop = true;
this._draw();
};
// interface for quicksettings
function QSGui(label, x, y) {
var qs = QuickSettings.create(x, y, label);
this.prototype = qs;
// addGlobals(global1, global2, ...) to add the selected globals
this.addGlobals = function() {
qs.bindGlobals(arguments);
};
// addObject(object) to add all params of the object
// addObject(object, param1, param2, ...) to add selected params
this.addObject = function() {
// get object
object = arguments[0];
// convert arguments object to array
var params = [];
if(arguments.length > 1) {
params = Array.prototype.slice.call(arguments)
params = params.slice(1);
}
// if no arguments are provided take all keys of the object
if(params.length === 0) {
params = object.keys();
}
qs.bindParams(object, params);
};
// noLoop() to call draw every time the gui changes when we are not looping
this.noLoop = function() {
qs.setGlobalChangeHandler(draw);
};
this.loop = function() {
qs.setGlobalChangeHandler(null);
};
this.show = function() { qs.show(); };
this.hide = function() { qs.hide(); };
this.toggleVisibility = function() { qs.toggleVisibility(); };
}
// Just a Dummy object that provides the GUI interface
function DummyGui() {
var f = function() {};
this.addGlobals = f;
this.noLoop = f;
this.addObject = f;
this.show = f;
}
// Extend Quicksettings
// so it can magically create a GUI for parameters passed by name
QuickSettings.bindParams = function(object, params) {
// iterate over all the arguments
for(var i = 0; i < params.length; i++) {
var arg = params[i];
var val = object[arg];
var typ = typeof val;
// console.log(typ, arg, val);
switch(typ) {
case 'object':
// color triple ?
if(val instanceof Array && val.length === 3 && typeof val[0] === 'number') {
// create color according to the current color mode
var c = color(val[0], val[1], val[2]);
// get decimal RGB values
var c2 = c.levels.slice(0,3);
// create HTML color code
var vcolor = '#' + c2.map(function(value) {
return ('0' + value.toString(16)).slice(-2);
}).join('');
this.bindColor(arg, vcolor, object);
} else {
// multiple choice drop down list
this.bindDropDown(arg, val, object);
object[arg] = val[0];
}
break;
case 'number':
// values as defined by magic variables or gui.sliderRange()
var vmin = object[arg + 'Min'] || object[arg + 'min'] || sliderMin;
var vmax = object[arg + 'Max'] || object[arg + 'max'] || sliderMax;
var vstep = object[arg + 'Step'] || object[arg + 'step'] || sliderStep;
// the actual values can still overrule the limits set by magic
var vmin = min(val, vmin);
var vmax = max(val, vmax);
// set the range
this.bindRange(arg, vmin, vmax, val, vstep, object);
break;
case 'string':
var HEX6 = /^#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i;
if(HEX6.test(val)) {
// HTML color value (such as #ff0000)
this.bindColor(arg, val, object);
} else {
// String value
this.bindText(arg, val, object);
}
break;
case 'boolean':
this.bindBoolean(arg, object[arg], object);
break;
}
}
};
// bind params that are defined globally
QuickSettings.bindGlobals = function(params) {
this.bindParams(window, params);
};
})();