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