/** * System takes care of updating all the entities (based on their component data) * @param apiSystem GameLib.API.System * @constructor */ GameLib.System.GUI = function( apiSystem ) { GameLib.System.call( this, apiSystem ); this.guis = []; this.buildGUISubscription = null; this.meshDeletedSubscription = null; this.meshSelectedSubscription = null; this.meshDeselectedSubscription = null; this.newEntitySubscription = null; this.meshSelectionObjects = {}; }; GameLib.System.GUI.prototype = Object.create(GameLib.System.prototype); GameLib.System.GUI.prototype.constructor = GameLib.System.GUI; GameLib.System.GUI.prototype.start = function() { this.guis = GameLib.EntityManager.Instance.queryComponents(GameLib.GUI); this.guis.map(function(gui){ gui.domElement.instance.parentElement.appendChild(gui.instance.domElement); }); this.buildGUISubscription = this.subscribe( GameLib.Event.BUILD_GUI, this.buildGUI ); this.meshDeletedSubscription = this.subscribe( GameLib.Event.MESH_DELETED, this.meshDeleted ); this.meshSelectedSubscription = this.subscribe( GameLib.Event.MESH_SELECTED, this.meshSelected ); this.meshDeselectedSubscription = this.subscribe( GameLib.Event.MESH_DESELECTED, this.meshDeslected ); this.newEntitySubscription = this.subscribe( GameLib.Event.NEW_ENTITY, this.newEntity ) }; GameLib.System.GUI.prototype.buildGUI = function(data) { this.guis.map(function(gui){ gui.objects = []; if ( data.components && data.components.length > 0 ) { gui.objects = data.components; } gui.build(GameLib.EntityManager.Instance); }); }; GameLib.System.GUI.prototype.meshDeleted = function(data) { }; GameLib.System.GUI.prototype.meshSelected = function(data) { var mesh = data.mesh; var objectsToAdd = []; this.guis.map( function(gui) { if (mesh.parentEntity) { GameLib.Utils.PushUnique(objectsToAdd, mesh.parentEntity); } objectsToAdd.push(mesh); mesh.materials.map( function(material){ GameLib.Utils.PushUnique(objectsToAdd, material); } ); mesh.materials.map( function(material){ for (var property in material.linkedObjects) { if ( material.linkedObjects.hasOwnProperty(property) && material.hasOwnProperty(property) && material[property] && property !== 'parentEntity' ) { GameLib.Utils.PushUnique(objectsToAdd, material[property]); for (var tProperty in material[property].linkedObjects) { if ( material[property].linkedObjects.hasOwnProperty(tProperty) && material[property].hasOwnProperty(tProperty) && material[property][tProperty] && tProperty !== 'parentEntity' ) { GameLib.Utils.PushUnique(objectsToAdd, material[property][tProperty]); } } } } } ) } ); this.guis.map(function(gui){ objectsToAdd.map(function(object){ gui.addObject(object); }.bind(this)); gui.build(GameLib.EntityManager.Instance); }); this.meshSelectionObjects[mesh.id] = objectsToAdd; }; GameLib.System.GUI.prototype.meshDeslected = function(data) { if (GameLib.Utils.UndefinedOrNull(this.meshSelectionObjects[data.mesh.id])) { console.warn('selected mesh data not available'); return; } this.guis.map( function(gui) { this.meshSelectionObjects[data.mesh.id].map(function(object){ gui.removeObject(object); }.bind(this)); gui.build(GameLib.EntityManager.Instance); }.bind(this) ) }; GameLib.System.GUI.prototype.newEntity = function(data) { }; GameLib.System.GUI.prototype.stop = function() { this.guis.map(function(gui){ gui.domElement.instance.parentElement.removeChild(gui.instance.domElement); }); this.buildGUISubscription.remove(); this.meshDeletedSubscription.remove(); this.meshSelectedSubscription.remove(); this.meshDeselectedSubscription.remove(); this.newEntitySubscription.remove(); this.guis = []; };