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