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  * Base class for ccs.ComAttribute
 28  * @class
 29  * @extends ccs.Component
 30  */
 31 ccs.ComAttribute = ccs.Component.extend(/** @lends ccs.ComAttribute# */{
 32     _jsonDict: null,
 33     _filePath: "",
 34     ctor: function () {
 35         cc.Component.prototype.ctor.call(this);
 36         this._jsonDict = {};
 37         this._filePath = "";
 38         this._name = "CCComAttribute";
 39     },
 40     init: function () {
 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._jsonDict[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._jsonDict[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._jsonDict[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._jsonDict[key] = value;
 95     },
 96 
 97     /**
 98      * Set string attribute
 99      * @param {String} key
100      * @param {Boolean} value
101      */
102     setString: function (key, value) {
103         if (!key) {
104             cc.log("Argument must be non-nil");
105             return;
106         }
107         this._jsonDict[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._jsonDict[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._jsonDict[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._jsonDict[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._jsonDict[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._jsonDict[key];
160         return Boolean(ret || false);
161     },
162 
163     /**
164      * Get string value from attribute
165      * @param {String} key
166      * @returns {String}
167      */
168     getString: function (key) {
169         var ret = this._jsonDict[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._jsonDict[key];
180     },
181 
182     /**
183      *
184      * @param path
185      */
186     parse:function(path){
187         this._jsonDict = cc.loader.getRes(path);
188     }
189 });
190 /**
191  * allocates and initializes a ComAttribute.
192  * @constructs
193  * @return {ccs.ComAttribute}
194  * @example
195  * // example
196  * var com = ccs.ComAttribute.create();
197  */
198 ccs.ComAttribute.create = function () {
199     var com = new ccs.ComAttribute();
200     if (com && com.init()) {
201         return com;
202     }
203     return null;
204 };