/** * Stats component for displaying some render statistics (framerate, memory consumption, etc) * @param gui * @param id * @param name * @param domElement * @param objects * @param parentEntity * @constructor */ GameLib.GUI = function( gui, id, name, domElement, objects, 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; if (GameLib.Utils.UndefinedOrNull(objects)) { objects = []; } this.objects = objects; 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 { instance = new dat.GUI( { autoPlace: false } ); } return instance; }; /** * Updates the instance with the current state */ GameLib.GUI.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; 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) ); };