1 /**************************************************************************** 2 Copyright (c) 2013 cocos2d-x.org 3 4 http://www.cocos2d-x.org 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in 14 all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 THE SOFTWARE. 23 ****************************************************************************/ 24 25 /** 26 * The container of cc.Component 27 * @class 28 * @extends cc.Class 29 */ 30 cc.ComponentContainer = cc.Class.extend(/** @lends cc.ComponentContainer# */{ 31 _components:null, 32 _owner:null, 33 34 ctor:function(node){ 35 this._components = null; 36 this._owner = node; 37 }, 38 39 getComponent:function(name){ 40 if(!name) 41 throw "cc.ComponentContainer.getComponent(): name should be non-null"; 42 name = name.trim(); 43 return this._components[name]; 44 }, 45 46 add:function(component){ 47 if(!component) 48 throw "cc.ComponentContainer.add(): component should be non-null"; 49 if(component.getOwner()){ 50 cc.log("cc.ComponentContainer.add(): Component already added. It can't be added again"); 51 return false; 52 } 53 54 if(this._components == null){ 55 this._components = {}; 56 this._owner.scheduleUpdate(); 57 } 58 var oldComponent = this._components[component.getName()]; 59 if(oldComponent){ 60 cc.log("cc.ComponentContainer.add(): Component already added. It can't be added again"); 61 return false; 62 } 63 component.setOwner(this._owner); 64 this._components[component.getName()] = component; 65 component.onEnter(); 66 return true; 67 }, 68 69 /** 70 * 71 * @param {String|cc.Component} name 72 * @returns {boolean} 73 */ 74 remove:function(name){ 75 if(!name) 76 throw "cc.ComponentContainer.remove(): name should be non-null"; 77 if(!this._components) 78 return false; 79 if(name instanceof cc.Component){ 80 return this._removeByComponent(name); 81 }else{ 82 name = name.trim(); 83 return this._removeByComponent(this._components[name]); 84 } 85 }, 86 87 _removeByComponent:function(component){ 88 if(component) 89 return false; 90 component.onExit(); 91 component.setOwner(null); 92 delete this._components[component.getName()]; 93 return true; 94 }, 95 96 removeAll:function(){ 97 if(!this._components) 98 return; 99 100 var locComponents = this._components; 101 for(var selKey in locComponents){ 102 var selComponent = locComponents[selKey]; 103 selComponent.onExit(); 104 selComponent.setOwner(null); 105 delete locComponents[selKey]; 106 } 107 this._owner.unscheduleUpdate(); 108 this._components = null; 109 }, 110 111 _alloc:function(){ 112 this._components = {}; 113 }, 114 115 visit:function(delta){ 116 if(!this._components) 117 return; 118 119 var locComponents = this._components; 120 for(var selKey in locComponents){ 121 locComponents[selKey].update(delta); 122 } 123 }, 124 125 isEmpty:function(){ 126 if(!this._components) 127 return true; 128 129 for(var selkey in this._components){ 130 return false; 131 } 132 return true; 133 } 134 }); 135 136 137