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