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 cc.ComponentContainer = cc.Class.extend({ 26 _components:null, 27 _owner:null, 28 29 ctor:function(node){ 30 this._components = null; 31 this._owner = node; 32 }, 33 34 getComponent:function(name){ 35 if(!name) 36 throw "cc.ComponentContainer.getComponent(): name should be non-null"; 37 name = name.trim(); 38 return this._components[name]; 39 }, 40 41 add:function(component){ 42 if(!component) 43 throw "cc.ComponentContainer.add(): component should be non-null"; 44 if(component.getOwner()){ 45 cc.log("cc.ComponentContainer.add(): Component already added. It can't be added again"); 46 return false; 47 } 48 49 if(this._components == null){ 50 this._components = {}; 51 this._owner.scheduleUpdate(); 52 } 53 var oldComponent = this._components[component.getName()]; 54 if(oldComponent){ 55 cc.log("cc.ComponentContainer.add(): Component already added. It can't be added again"); 56 return false; 57 } 58 component.setOwner(this._owner); 59 this._components[component.getName()] = component; 60 component.onEnter(); 61 return true; 62 }, 63 64 remove:function(name){ 65 if(!name) 66 throw "cc.ComponentContainer.remove(): name should be non-null"; 67 if(!this._components) 68 return false; 69 var locComponents = this._components; 70 name = name.trim(); 71 var component = locComponents[name]; 72 if(component) 73 return false; 74 component.onExit(); 75 component.setOwner(null); 76 delete locComponents[name]; 77 return true; 78 }, 79 80 removeAll:function(){ 81 if(!this._components) 82 return; 83 84 var locComponents = this._components; 85 for(var selKey in locComponents){ 86 var selComponent = locComponents[selKey]; 87 selComponent.onExit(); 88 selComponent.setOwner(null); 89 delete locComponents[selKey]; 90 } 91 this._owner.unscheduleUpdate(); 92 this._components = null; 93 }, 94 95 _alloc:function(){ 96 this._components = {}; 97 }, 98 99 visit:function(delta){ 100 if(!this._components) 101 return; 102 103 var locComponents = this._components; 104 for(var selKey in locComponents){ 105 locComponents[selKey].update(delta); 106 } 107 }, 108 109 isEmpty:function(){ 110 if(!this._components) 111 return true; 112 113 for(var selkey in this._components){ 114 return false; 115 } 116 return true; 117 } 118 }); 119 120 121