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