1 /**************************************************************************** 2 Copyright (c) 2010-2012 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 * Base class for ccs.ComAttribute 27 * @class 28 * @extends ccs.Component 29 */ 30 ccs.ComAttribute = ccs.Component.extend(/** @lends ccs.ComAttribute# */{ 31 _attributes: null, 32 _jsonDict: null, 33 ctor: function () { 34 cc.Component.prototype.ctor.call(this); 35 this._attributes = {}; 36 this._jsonDict = {}; 37 this._name = "ComAttribute"; 38 }, 39 init: function () { 40 this._attributes = {}; 41 this._jsonDict = {}; 42 return true; 43 }, 44 45 /** 46 * Set int attribute 47 * @param {String} key 48 * @param {number} value 49 */ 50 setInt: function (key, value) { 51 if (!key) { 52 cc.log("Argument must be non-nil"); 53 return; 54 } 55 this._attributes[key] = value; 56 }, 57 58 /** 59 * Set double attribute 60 * @param {String} key 61 * @param {number} value 62 */ 63 setDouble: function (key, value) { 64 if (!key) { 65 cc.log("Argument must be non-nil"); 66 return; 67 } 68 this._attributes[key] = value; 69 }, 70 71 /** 72 * Set float attribute 73 * @param {String} key 74 * @param {number} value 75 */ 76 setFloat: function (key, value) { 77 if (!key) { 78 cc.log("Argument must be non-nil"); 79 return; 80 } 81 this._attributes[key] = value; 82 }, 83 84 /** 85 * Set boolean attribute 86 * @param {String} key 87 * @param {Boolean} value 88 */ 89 setBool: function (key, value) { 90 if (!key) { 91 cc.log("Argument must be non-nil"); 92 return; 93 } 94 this._attributes[key] = value; 95 }, 96 97 /** 98 * Set string attribute 99 * @param {String} key 100 * @param {Boolean} value 101 */ 102 setCString: function (key, value) { 103 if (!key) { 104 cc.log("Argument must be non-nil"); 105 return; 106 } 107 this._attributes[key] = value; 108 }, 109 110 /** 111 * Set object attribute 112 * @param {String} key 113 * @param {Object} value 114 */ 115 setObject: function (key, value) { 116 if (!key) { 117 cc.log("Argument must be non-nil"); 118 return; 119 } 120 this._attributes[key] = value; 121 }, 122 123 /** 124 * Get int value from attribute 125 * @param {String} key 126 * @returns {Number} 127 */ 128 getInt: function (key) { 129 var ret = this._attributes[key]; 130 return parseInt(ret || 0); 131 }, 132 133 /** 134 * Get double value from attribute 135 * @param {String} key 136 * @returns {Number} 137 */ 138 getDouble: function (key) { 139 var ret = this._attributes[key]; 140 return parseFloat(ret || 0.0); 141 }, 142 143 /** 144 * Get float value from attribute 145 * @param {String} key 146 * @returns {Number} 147 */ 148 getFloat: function (key) { 149 var ret = this._attributes[key]; 150 return parseFloat(ret || 0.0); 151 }, 152 153 /** 154 * Get boolean value from attribute 155 * @param {String} key 156 * @returns {Boolean} 157 */ 158 getBool: function (key) { 159 var ret = this._attributes[key]; 160 return Boolean(ret || false); 161 }, 162 163 /** 164 * Get string value from attribute 165 * @param {String} key 166 * @returns {String} 167 */ 168 getCString: function (key) { 169 var ret = this._attributes[key]; 170 return ret || ""; 171 }, 172 173 /** 174 * Get object value from attribute 175 * @param {String} key 176 * @returns {Object} 177 */ 178 getObject: function (key) { 179 return this._attributes[key]; 180 }, 181 182 /** 183 * Getter of jsonDict 184 * @returns {Object} 185 */ 186 getDict: function () { 187 return this._jsonDict; 188 }, 189 190 /** 191 * setter of jsonDict 192 * @returns {Object} 193 */ 194 setDict: function (dict) { 195 this._jsonDict = dict; 196 } 197 }); 198 /** 199 * allocates and initializes a ComAttribute. 200 * @constructs 201 * @return {ccs.ComAttribute} 202 * @example 203 * // example 204 * var com = ccs.ComAttribute.create(); 205 */ 206 ccs.ComAttribute.create = function () { 207 var com = new ccs.ComAttribute(); 208 if (com && com.init()) { 209 return com; 210 } 211 return null; 212 };