1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3  Copyright (c) 2008-2010 Ricardo Quesada
  4  Copyright (c) 2011      Zynga Inc.
  5 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 /**
 28  * first, judge whether the form of the string like this: {x,y}  <br/>
 29  * if the form is right,the string will be splited into the parameter strs;<br/>
 30  * or the parameter strs will be empty. <br/>
 31  * if the form is right return true,else return false.<br/>
 32  * @function
 33  * @param {String} content
 34  * @param {String} strs
 35  * @return {String}
 36  */
 37 cc.splitWithForm = function (content, strs) {
 38     do {
 39         if (!content) break;
 40 
 41         // string is empty
 42         if (content.length == 0) break;
 43 
 44         var posLeft = content.indexOf('{');
 45         var posRight = content.indexOf('}');
 46 
 47         // don't have '{' and '}'
 48         if (posLeft == -1 || posRight == -1) break;
 49         // '}' is before '{'
 50         if (posLeft > posRight) break;
 51 
 52         var pointStr = content.substr(posLeft + 1, posRight - posLeft - 1);
 53         // nothing between '{' and '}'
 54         if (pointStr.length == 0) break;
 55 
 56         var nPos1 = pointStr.indexOf('{');
 57         var nPos2 = pointStr.indexOf('}');
 58         // contain '{' or '}'
 59         if (nPos1 != -1 || nPos2 != -1) break;
 60         strs = pointStr.split(",");
 61         if (strs.length != 2 || strs[0] != null || strs[1] != null) {
 62             break;
 63         }
 64     } while (0);
 65 
 66     return strs;
 67 };
 68 
 69 /**
 70  * Returns a Core Graphics rectangle structure corresponding to the data in a given string. <br/>
 71  * The string is not localized, so items are always separated with a comma. <br/>
 72  * If the string is not well-formed, the function returns cc.RectZero.
 73  * @function
 74  * @param {String} content content A string object whose contents are of the form "{{x,y},{w, h}}",<br/>
 75  * where x is the x coordinate, y is the y coordinate, w is the width, and h is the height. <br/>
 76  * These components can represent integer or float values.
 77  * @return {cc.Rect} A Core Graphics structure that represents a rectangle.
 78  * Constructor
 79  * @example
 80  * // example
 81  * var rect = cc.RectFromString("{{3,2},{4,5}}");
 82  */
 83 cc.RectFromString = function (content) {
 84     var result = cc.RectZero();
 85     do {
 86         if (!content) break;
 87 
 88         // find the first '{' and the third '}'
 89         var posLeft = content.indexOf('{') + 1;
 90         var posRight = content.lastIndexOf('}', content.length);
 91         if (posLeft == -1 || posRight == -1) break;
 92 
 93         content = content.substring(posLeft, posRight);
 94         var nPointEnd = content.indexOf('}');
 95         if (nPointEnd == -1) break;
 96         nPointEnd = content.indexOf(',', nPointEnd);
 97         if (nPointEnd == -1) break;
 98         // get the point string and size string
 99         var pointStr = content.substr(0, nPointEnd);
100         var sizeStr = content.substr(nPointEnd + 1, content.length - nPointEnd);
101 
102         // split the string with ','
103         var pointInfo = cc.splitWithForm(pointStr);
104         var sizeInfo = cc.splitWithForm(sizeStr);
105 
106         var x = parseFloat(pointInfo[0]);
107         var y = parseFloat(pointInfo[1]);
108         var width = parseFloat(sizeInfo[0]);
109         var height = parseFloat(sizeInfo[1]);
110 
111         result = cc.rect(x, y, width, height);
112     } while (0);
113     return result;
114 };
115 
116 /**
117  * Returns a Core Graphics point structure corresponding to the data in a given string.
118  * @function
119  * @param {String} content   A string object whose contents are of the form "{x,y}",
120  * where x is the x coordinate and y is the y coordinate.<br/>
121  * The x and y values can represent integer or float values. <br/>
122  * The string is not localized, so items are always separated with a comma.<br/>
123  * @return {cc.Point} A Core Graphics structure that represents a point.<br/>
124  * If the string is not well-formed, the function returns cc.PointZero.
125  * Constructor
126  * @example
127  * //example
128  * var point = cc.PointFromString("{3.0,2.5}");
129  */
130 cc.PointFromString = function (content) {
131     var ret = cc.PointZero();
132     try {
133         if (content == "")
134             return ret;
135 
136         var strs = cc.splitWithForm(content);
137         var x = parseFloat(strs[0]);
138         var y = parseFloat(strs[1]);
139         ret = cc.p(x, y);
140     } catch (e) {
141     }
142     return ret;
143 };
144 
145 /**
146  * Returns a Core Graphics size structure corresponding to the data in a given string.
147  * @function
148  * @param {String} content   A string object whose contents are of the form "{w, h}",<br/>
149  * where w is the width and h is the height.<br/>
150  * The w and h values can be integer or float values. <br/>
151  * The string is not localized, so items are always separated with a comma.<br/>
152  * @return {cc.Size} A Core Graphics structure that represents a size.<br/>
153  * If the string is not well-formed, the function returns cc.SizeZero.
154  * @example
155  * // example
156  * var size = cc.SizeFromString("{3.0,2.5}");
157  */
158 cc.SizeFromString = function (content) {
159     var ret = cc.SizeZero();
160     try {
161         if (content == "")
162             return ret;
163 
164         var strs = cc.splitWithForm(content);
165         var width = parseFloat(strs[0]);
166         var height = parseFloat(strs[1]);
167         ret = cc.size(width, height);
168     } catch (e) {
169     }
170     return ret;
171 };
172