gui updates

beta.r3js.org
-=yb4f310 2017-05-20 09:45:50 +02:00
parent 3fd2d5cdbe
commit 4c34fb2b92
1 changed files with 150 additions and 2 deletions

View File

@ -17,11 +17,45 @@ GameLib.GUI = function(
parentEntity parentEntity
) { ) {
if (GameLib.Utils.UndefinedOrNull(gui)) { if (GameLib.Utils.UndefinedOrNull(gui)) {
throw new Error('Need Stats object') throw new Error('Need GUI object')
} }
this.gui = gui; this.gui = gui;
GameLib.Component.call( /**
* Add some GUI behaviour
*/
this.gui.prototype.removeEmtpyFolders = function() {
for (var property in this.__folders) {
if (this.__folders.hasOwnProperty(property)){
var folder = this.__folders[property];
if (folder.__listening.length == 0) {
folder.close();
this.__ul.removeChild(folder.domElement.parentNode);
delete this.__folders[property];
this.onResize();
}
}
}
};
this.gui.prototype.removeAllFolders = function() {
for (var property in this.__folders) {
if (this.__folders.hasOwnProperty(property)){
var folder = this.__folders[property];
folder.close();
this.__ul.removeChild(folder.domElement.parentNode);
delete this.__folders[property];
this.onResize();
}
}
};
GameLib.Component.call(
this, this,
GameLib.Component.COMPONENT_GUI, GameLib.Component.COMPONENT_GUI,
{ {
@ -685,4 +719,118 @@ GameLib.GUI.prototype.build = function() {
}.bind(this) }.bind(this)
); );
};
//returns params, a stripped version of paramsGUI, without all the GUI fluff
GameLib.GUI.prototype.glue = function(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 = {};
this.renderParameters(paramsGUI, optionsGUI, params);
//return stripped parameter object
return params;
};
GameLib.GUI.prototype.renderParameters = function(paramsGUI, optionsGUI, params){
//initial creation
var gui = this.instance;
//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;
}; };