r3-legacy/src/game-lib-gui.js

153 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-05-16 11:50:06 +02:00
/**
* Stats component for displaying some render statistics (framerate, memory consumption, etc)
* @param gui
* @param id
* @param name
* @param domElement
2017-05-16 14:51:57 +02:00
* @param objects
2017-05-16 11:50:06 +02:00
* @param parentEntity
* @constructor
*/
GameLib.GUI = function(
gui,
id,
name,
domElement,
2017-05-16 14:51:57 +02:00
objects,
2017-05-16 11:50:06 +02:00
parentEntity
) {
if (GameLib.Utils.UndefinedOrNull(gui)) {
throw new Error('Need Stats object')
}
this.gui = gui;
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_GUI,
{
'domElement': GameLib.DomElement
},
null,
parentEntity
);
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
this.id = id;
if (GameLib.Utils.UndefinedOrNull(name)) {
name = 'GUI (' + id + ')';
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(domElement)) {
console.warn('Need a DOM element for stats');
throw new Error('Need a DOM element for stats');
}
this.domElement = domElement;
2017-05-16 14:51:57 +02:00
if (GameLib.Utils.UndefinedOrNull(objects)) {
objects = [];
}
this.objects = objects;
2017-05-16 11:50:06 +02:00
this.instance = this.createInstance();
};
GameLib.GUI.prototype = Object.create(GameLib.Component.prototype);
GameLib.GUI.prototype.constructor = GameLib.GUI;
/**
* Creates a helper instance
* @param update
*/
GameLib.GUI.prototype.createInstance = function(update) {
var instance = null;
if (update) {
instance = this.instance;
} else {
2017-05-16 14:51:57 +02:00
instance = new dat.GUI( { autoPlace: false } );
}
2017-05-16 11:50:06 +02:00
return instance;
};
/**
* Updates the instance with the current state
*/
GameLib.GUI.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
2017-05-16 14:51:57 +02:00
GameLib.GUI.prototype.addObject = function(object) {
this.objects.push(object);
};
GameLib.GUI.prototype.removeObject = function(object) {
var index = this.objects.indexOf(object);
if (index === -1) {
console.warn('could not find object to remove');
return false;
} else {
this.objects.splice(index, 1);
}
return true;
};
GameLib.GUI.prototype.build = function() {
this.instance.removeAllFolders();
var discoveredObjects = [];
this.objects.map(
function(object) {
if (object.idToObject) {
for (var property in object.idToObject) {
if (object.idToObject.hasOwnProperty(property)) {
discoveredObjects.push(object.idToObject[property]);
}
}
}
}.bind(this)
);
discoveredObjects.map(
function(object) {
var apiObject = object.toApiObject();
var folder = this.instance.addFolder(apiObject.name);
for (var property in apiObject) {
if (
apiObject.hasOwnProperty(property) &&
object.hasOwnProperty(property)
) {
if (typeof (object[property]) === 'object') {
console.log('ignored: ' + property);
} else {
folder.add(object, property);
}
}
}
}.bind(this)
);
};