-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctionsFolder.js
More file actions
371 lines (272 loc) · 9.98 KB
/
functionsFolder.js
File metadata and controls
371 lines (272 loc) · 9.98 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
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
/**
* @module functionsFolder
* @description Adds the [Functions]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function} folder into [dat.gui]{@link https://github.com/anhr/dat.gui}.
*
* @author [Andrej Hristoliubov]{@link https://github.com/anhr}
*
* @copyright 2011 Data Arts Team, Google Creative Lab
*
* @license under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
import Options from './Options.js'
import three from './three.js'
class functionsFolder {
/**
* Adds the [Functions]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function} folder into [dat.gui]{@link https://github.com/anhr/dat.gui}.
* @param {GUI} fParent parent folder for functions folder.
* @param {Object} scales [AxesHelper]{@link https://raw.githack.com/anhr/AxesHelper/master/jsdoc/module-AxesHelper.html} options.scales for details.
* @param {Function} onFinishChange callback function is called every time, when user have entered new value of the function and the function controller is lost of the focus.
* <pre>
* parameter value is new value of the function.
* </pre>
* @param {Options} options <a href="../../jsdoc/Options/Options.html" target="_blank">Options</a> instance. The following options are available.
* See the <b>options</b> parameter of the <a href="../../myThree/jsdoc/module-MyThree-MyThree.html" target="_blank">MyThree</a> class.
* @param {Function} [options.getLanguageCode=language code of your browser] returns the "primary language" subtag of the version of the browser.
* @param {object} [vector] Vector with initial text of the function
* @param {string} [vector.x] text of the x axis function
* @param {string} [vector.y] text of the y axis function
* @param {string} [vector.z] text of the z axis function
*/
constructor( fParent, onFinishChange, options, vector ) {
if ( !options.boOptions ) {
console.error( 'functionsFolder: call options = new Options( options ) first' );
return;
}
const dat = three.dat,
THREE = three.THREE,
scales = options.scales;
const _this = this;
var boError = false,//true - обнаружена ошибка ввода. Нужно вывести сообщение об ошибке и вернуть фокус на поле управления
boAlert = false;//предотвращает бесконечный вывод сообщения об ошибке
//Localization
const lang = {
functions: 'Functions',
defaultButton: 'Default',
defaultTitle: 'Restore default functions.',
};
const _languageCode = options.getLanguageCode === undefined ? 'en'//Default language is English
: options.getLanguageCode();
switch ( _languageCode ) {
case 'ru'://Russian language
lang.functions = 'Функции';
lang.defaultButton = 'Восстановить';
lang.defaultTitle = 'Восстановить функции.';
break;
default://Custom language
if ( ( options.lang === undefined ) || ( options.lang.languageCode != _languageCode ) )
break;
Object.keys( options.lang ).forEach( function ( key ) {
if ( lang[key] === undefined )
return;
lang[key] = options.lang[key];
} );
}
if ( vector ) {
vector.x = getFuncText( vector.x );
vector.y = getFuncText( vector.y );
vector.z = getFuncText( vector.z );
} else vector = { x: '', y: '', z: '', }
const fFunctions = fParent.addFolder( lang.functions ),
//onFinishChange вызывается даже если vector не изменился. Поэтому такой onFinishChange пропускается
vectorCur = {
x: vector.x,
y: vector.y,
z: vector.z,
w: vector.w,
},
cFunctions = {};
function createControl( axisName ) {
if ( vector[axisName] === undefined )
return;
cFunctions[axisName] = fFunctions.add( vector, axisName ).onFinishChange( function ( value ) {
__onFinishChange( value, axisName, vectorCur );
} );
dat.controllerNameAndTitle( cFunctions[axisName], getAxisName( axisName ) );
}
function getAxisName( axisName ) { return scales[axisName] && scales[axisName].name ? scales[axisName].name : axisName; }
createControl( 'x' );
createControl( 'y' );
createControl( 'z' );
createControl( 'w' );
//Default scale button
const buttonDefault = fFunctions.add( {
defaultF: function ( value ) {
},
}, 'defaultF' );
dat.controllerNameAndTitle( buttonDefault, lang.defaultButton, lang.defaultTitle );
function getFuncText( func ) {
if ( func === undefined )
return;
if ( typeof func === 'object' ) {
if ( func instanceof THREE.Color ) return func.getStyle();
if ( Array.isArray( func ) ) return JSON.stringify( func )
func = func.func ? func.func : func;
}
const typeofFunc = typeof func;
switch ( typeofFunc ) {
case 'number':
func = func.toString();//если это не делать будет создан NumberControllerBox, ктороый не позволяет вводить float
case 'string':
return func;
case 'function':
return func.toString().split( /return (.*)/ )[1];
default: console.error( 'functionsFolder.getFuncText(...): typeof func = ' + typeofFunc );
return;
}
}
function __onFinishChange( value, axisName, vectorCur ) {
if ( ( vectorCur[axisName] === value ) && !boError )
return;
try {
boError = false;
vectorCur[axisName] = value;
var func;
const typeofValue = typeof value;
switch ( typeofValue ) {
case 'string':
var float = parseFloat( value );
if ( float.toString() !== value ) {
const color = value.replace( /\s/g, "" ).split( /rgb\((\d+),(\d+),(\d+)\)/ );
if ( color.length === 5 ) func = new THREE.Color( value );
else {
var array;
try {
array = JSON.parse( value );
} catch ( e ) { }
if ( Array.isArray( array ) ) func = array;
else {
func = new Function( 't', 'a', 'b', 'return ' + value );
}
}
} else func = float;
break;
case 'number':
func = value;
break;
default:
console.error( 'onFinishChange( ' + value + ' ): invalid type = ' + typeofValue );
return;
}
//Новое значение введено правильно
onFinishChange( func, axisName, value );
boAlert = false;
} catch ( e ) {
if ( !boAlert ) {
alert( 'Axis: ' + getAxisName( axisName ) + '. Function: "' + value + '". ' + e );
boAlert = true;
}
_this.setFocus( axisName );
}
}
/**
* set the function text
* @param {object} _vector vector of the axis functions.
* @param {object} _vector.x x axis function.
* @param {object} _vector.y y axis function.
* @param {object} _vector.z z axis function.
*/
this.setFunction = function ( _vector ) {
_vector = _vector || options.vector;
if ( !_vector )
return;
const vector = {
x: _vector ? getFuncText( _vector.x ) : '',
y: _vector ? getFuncText( _vector.y ) : '',
z: _vector ? getFuncText( _vector.z ) : '',
w: _vector ? getFuncText( _vector.w ) : '',
},
//onFinishChange вызывается даже если vector не изменился. Поэтому такой onFinishChange пропускается
vectorCur = {
x: vector.x,
y: vector.y,
z: vector.z,
w: vector.w,
};
if ( !_vector.vectorDefault )
_vector.vectorDefault = {
x: vector.x,
y: vector.y,
z: vector.z,
w: vector.w,
};
function setVectorAxis( axisName ) {
if ( _vector[axisName] === undefined )
return;
cFunctions[axisName].__onFinishChange = function ( value ) {
__onFinishChange( value, axisName, vectorCur );
}
vector[axisName] = getFuncText( _vector[axisName] );
cFunctions[axisName].setValue( vector[axisName] );
vectorCur[axisName] = vector[axisName];
}
setVectorAxis( 'x' );
setVectorAxis( 'y' );
var dislay = false;
if ( _vector.z ) {
setVectorAxis( 'z' );
dislay = true;
}
buttonDefault.object.defaultF = function ( value ) {
function setValue( axisName ) {
if ( !cFunctions[axisName] )
return;
cFunctions[axisName].setValue( _vector.vectorDefault[axisName] );
cFunctions[axisName].__onFinishChange( _vector.vectorDefault[axisName] );
}
setValue( 'x' );
setValue( 'y' );
setValue( 'z' );
setValue( 'w' );
}
function dislayEl( controller, displayController ) {
if ( controller === undefined )
return;
if ( typeof displayController === "boolean" )
displayController = displayController ? 'block' : 'none';
var el = controller.domElement;
while ( el.tagName.toUpperCase() !== "LI" ) el = el.parentElement;
el.style.display = displayController;
}
dislayEl( cFunctions.z, dislay );
setVectorAxis( 'w' );
}
this.setFunction();
/**
* Display functions folder
* @param {string|boolean} display 'block' or true - functions folder is visible.
* <p>'none' or false - functions folder is hide.</p>
*/
this.displayFolder = function ( display ) {
fFunctions.domElement.style.display = typeof display === "boolean" ?
display ? 'block' : 'none' :
display;
}
/**
* set focus to controller
* @param {string} axisName Name of the axis of the controller
*/
this.setFocus = function ( axisName ) {
cFunctions[axisName].domElement.childNodes[0].focus();
boError = true;
}
/**
* update the values of the controllers of the functions
*/
this.update = function ( newVector ) {
function updateAxis( axisName ) {
if ( cFunctions[axisName].getValue() === newVector[axisName] )
return;
cFunctions[axisName].setValue( newVector[axisName] );
}
updateAxis( 'x' );
updateAxis( 'y' );
updateAxis( 'z' );
updateAxis( 'w' );
}
}
}
export default functionsFolder;