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 component container for Cocostudio, it has some components. 28 * @class 29 * @extends cc.Class 30 */ 31 cc.ComponentContainer = cc.Class.extend(/** @lends cc.ComponentContainer# */{ 32 _components:null, 33 _owner:null, 34 35 /** 36 * Construction of cc.ComponentContainer 37 * @param node 38 */ 39 ctor:function(node){ 40 this._components = null; 41 this._owner = node; 42 }, 43 44 /** 45 * Gets component by name. 46 * @param name 47 * @returns {*} 48 */ 49 getComponent:function(name){ 50 if(!name) 51 throw new Error("cc.ComponentContainer.getComponent(): name should be non-null"); 52 name = name.trim(); 53 if(!this._components){ 54 this._components = {}; 55 } 56 return this._components[name]; 57 }, 58 59 /** 60 * Adds a component to container 61 * @param {cc.Component} component 62 * @returns {boolean} 63 */ 64 add:function(component){ 65 if(!component) 66 throw new Error("cc.ComponentContainer.add(): component should be non-null"); 67 if(component.getOwner()){ 68 cc.log("cc.ComponentContainer.add(): Component already added. It can't be added again"); 69 return false; 70 } 71 72 if(this._components == null){ 73 this._components = {}; 74 this._owner.scheduleUpdate(); 75 } 76 var oldComponent = this._components[component.getName()]; 77 if(oldComponent){ 78 cc.log("cc.ComponentContainer.add(): Component already added. It can't be added again"); 79 return false; 80 } 81 component.setOwner(this._owner); 82 this._components[component.getName()] = component; 83 component.onEnter(); 84 return true; 85 }, 86 87 /** 88 * Removes component from container by name or component object. 89 * @param {String|cc.Component} name component name or component object. 90 * @returns {boolean} 91 */ 92 remove:function(name){ 93 if(!name) 94 throw new Error("cc.ComponentContainer.remove(): name should be non-null"); 95 if(!this._components) 96 return false; 97 if(name instanceof cc.Component) 98 return this._removeByComponent(name); 99 else { 100 name = name.trim(); 101 return this._removeByComponent(this._components[name]); 102 } 103 }, 104 105 _removeByComponent:function(component){ 106 if(!component) 107 return false; 108 component.onExit(); 109 component.setOwner(null); 110 delete this._components[component.getName()]; 111 return true; 112 }, 113 114 /** 115 * Removes all components of container. 116 */ 117 removeAll:function(){ 118 if(!this._components) 119 return; 120 var locComponents = this._components; 121 for(var selKey in locComponents){ 122 var selComponent = locComponents[selKey]; 123 selComponent.onExit(); 124 selComponent.setOwner(null); 125 delete locComponents[selKey]; 126 } 127 this._owner.unscheduleUpdate(); 128 this._components = null; 129 }, 130 131 _alloc:function(){ 132 this._components = {}; 133 }, 134 135 /** 136 * Visit callback by director. it calls every frame. 137 * @param {Number} delta 138 */ 139 visit:function(delta){ 140 if(!this._components) 141 return; 142 143 var locComponents = this._components; 144 for(var selKey in locComponents) 145 locComponents[selKey].update(delta); 146 }, 147 148 /** 149 * Returns the container whether is empty. 150 * @returns {boolean} 151 */ 152 isEmpty: function () { 153 if (!this._components) 154 return true; 155 return this._components.length === 0; 156 } 157 }); 158 159 160