Skip to content

Commit 38e66a6

Browse files
author
natural-law
committed
issue cocos2d#391,implement functions:CCRectFromString, CCPointFromString and CCSizeFromString.
1 parent 2dbb6dc commit 38e66a6

File tree

18 files changed

+300
-288
lines changed

18 files changed

+300
-288
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
d5a987afa7d4b73559fafa94ca068affc445ddab
1+
546c6952da19344ddc1ff5444e70f5bcd153542c

cocos2dx/Android.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ cocoa/CCAffineTransform.cpp \
2525
cocoa/CCGeometry.cpp \
2626
cocoa/CCAutoreleasePool.cpp \
2727
cocoa/CCData.cpp \
28+
cocoa/CCNS.cpp \
2829
cocoa/CCObject.cpp \
2930
cocoa/CCSet.cpp \
3031
cocoa/CCZone.cpp \
@@ -62,7 +63,6 @@ platform/CCCommon.cpp \
6263
platform/CCParticleSystemPoint_mobile.cpp \
6364
platform/CCTransition_mobile.cpp \
6465
platform/platform.cpp \
65-
platform/android/CCNS_android.cpp \
6666
platform/android/CCEGLView_android.cpp \
6767
platform/android/CCFileUtils_android.cpp \
6868
platform/android/CCAccelerometer_android.cpp \

cocos2dx/cocoa/CCNS.cpp

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/****************************************************************************
2+
Copyright (c) 2010 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+
#include "CCNS.h"
25+
#include <string>
26+
#include <vector>
27+
#include <stdlib.h>
28+
29+
typedef std::vector<std::string> strArray;
30+
31+
// string toolkit
32+
void split(std::string src, const char* token, strArray& vect)
33+
{
34+
int nend=0;
35+
int nbegin=0;
36+
while(nend != -1)
37+
{
38+
nend = src.find(token, nbegin);
39+
if(nend == -1)
40+
vect.push_back(src.substr(nbegin, src.length()-nbegin));
41+
else
42+
vect.push_back(src.substr(nbegin, nend-nbegin));
43+
nbegin = nend + strlen(token);
44+
}
45+
}
46+
47+
// first, judge whether the form of the string like this: {x,y}
48+
// if the form is right,the string will be splited into the parameter strs;
49+
// or the parameter strs will be empty.
50+
// if the form is right return true,else return false.
51+
bool splitWithForm(const char* pStr, strArray& strs)
52+
{
53+
bool bRet = false;
54+
55+
do
56+
{
57+
CC_BREAK_IF(!pStr);
58+
59+
// string is empty
60+
std::string content = pStr;
61+
CC_BREAK_IF(content.length() == 0);
62+
63+
int nPosLeft = content.find('{');
64+
int nPosRight = content.find('}');
65+
66+
// don't have '{' and '}'
67+
CC_BREAK_IF(nPosLeft == std::string::npos || nPosRight == std::string::npos);
68+
// '}' is before '{'
69+
CC_BREAK_IF(nPosLeft > nPosRight);
70+
71+
std::string pointStr = content.substr(nPosLeft + 1, nPosRight - nPosLeft - 1);
72+
// nothing between '{' and '}'
73+
CC_BREAK_IF(pointStr.length() == 0);
74+
75+
int nPos1 = pointStr.find('{');
76+
int nPos2 = pointStr.find('}');
77+
// contain '{' or '}'
78+
CC_BREAK_IF(nPos1 != std::string::npos || nPos2 != std::string::npos);
79+
80+
split(pointStr, ",", strs);
81+
if (strs.size() != 2 || strs[0].length() == 0 || strs[1].length() == 0)
82+
{
83+
strs.clear();
84+
break;
85+
}
86+
87+
bRet = true;
88+
} while (0);
89+
90+
return bRet;
91+
}
92+
93+
// implement the functions
94+
namespace cocos2d
95+
{
96+
CCRect CCRectFromString(const char* pszContent)
97+
{
98+
CCRect result = CCRectZero;
99+
100+
do
101+
{
102+
CC_BREAK_IF(!pszContent);
103+
std::string content = pszContent;
104+
105+
// find the first '{' and the third '}'
106+
int nPosLeft = content.find('{');
107+
int nPosRight = content.find('}');
108+
for (int i = 1; i < 3; ++i)
109+
{
110+
if (nPosRight == std::string::npos)
111+
{
112+
break;
113+
}
114+
nPosRight = content.find('}', nPosRight + 1);
115+
}
116+
CC_BREAK_IF(nPosLeft == std::string::npos || nPosRight == std::string::npos);
117+
118+
content = content.substr(nPosLeft + 1, nPosRight - nPosLeft - 1);
119+
int nPointEnd = content.find('}');
120+
CC_BREAK_IF(nPointEnd == std::string::npos);
121+
nPointEnd = content.find(',', nPointEnd);
122+
CC_BREAK_IF(nPointEnd == std::string::npos);
123+
124+
// get the point string and size string
125+
std::string pointStr = content.substr(0, nPointEnd);
126+
std::string sizeStr = content.substr(nPointEnd + 1, content.length() - nPointEnd);
127+
128+
// split the string with ','
129+
strArray pointInfo;
130+
CC_BREAK_IF(!splitWithForm(pointStr.c_str(), pointInfo));
131+
strArray sizeInfo;
132+
CC_BREAK_IF(!splitWithForm(sizeStr.c_str(), sizeInfo));
133+
134+
float x = (float) atof(pointInfo[0].c_str());
135+
float y = (float) atof(pointInfo[1].c_str());
136+
float width = (float) atof(sizeInfo[0].c_str());
137+
float height = (float) atof(sizeInfo[1].c_str());
138+
139+
result = CCRectMake(x, y, width, height);
140+
} while (0);
141+
142+
return result;
143+
}
144+
145+
CCPoint CCPointFromString(const char* pszContent)
146+
{
147+
CCPoint ret = CCPointZero;
148+
149+
do
150+
{
151+
strArray strs;
152+
CC_BREAK_IF(!splitWithForm(pszContent, strs));
153+
154+
float x = (float) atof(strs[0].c_str());
155+
float y = (float) atof(strs[1].c_str());
156+
157+
ret = CCPointMake(x, y);
158+
} while (0);
159+
160+
return ret;
161+
}
162+
163+
CCSize CCSizeFromString(const char* pszContent)
164+
{
165+
CCSize ret = CCSizeZero;
166+
167+
do
168+
{
169+
strArray strs;
170+
CC_BREAK_IF(!splitWithForm(pszContent, strs));
171+
172+
float width = (float) atof(strs[0].c_str());
173+
float height = (float) atof(strs[1].c_str());
174+
175+
ret = CCSizeMake(width, height);
176+
} while (0);
177+
178+
return ret;
179+
}
180+
}

cocos2dx/cocoa/CCNS.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/****************************************************************************
2+
Copyright (c) 2010 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+
#ifndef __PLATFOMR_CCNS_H__
26+
#define __PLATFOMR_CCNS_H__
27+
28+
#include "CCGeometry.h"
29+
30+
namespace cocos2d
31+
{
32+
/**
33+
@brief Returns a Core Graphics rectangle structure corresponding to the data in a given string.
34+
@param pszContent A string object whose contents are of the form "{{x,y},{w, h}}",
35+
where x is the x coordinate, y is the y coordinate, w is the width, and h is the height.
36+
These components can represent integer or float values.
37+
An example of a valid string is "{{3,2},{4,5}}".
38+
The string is not localized, so items are always separated with a comma.
39+
@return A Core Graphics structure that represents a rectangle.
40+
If the string is not well-formed, the function returns CCRectZero.
41+
*/
42+
CCRect CCRectFromString(const char* pszContent);
43+
44+
/**
45+
@brief Returns a Core Graphics point structure corresponding to the data in a given string.
46+
@param pszContent A string object whose contents are of the form "{x,y}",
47+
where x is the x coordinate and y is the y coordinate.
48+
The x and y values can represent integer or float values.
49+
An example of a valid string is "{3.0,2.5}".
50+
The string is not localized, so items are always separated with a comma.
51+
@return A Core Graphics structure that represents a point.
52+
If the string is not well-formed, the function returns CCPointZero.
53+
*/
54+
CCPoint CCPointFromString(const char* pszContent);
55+
56+
/**
57+
@brief Returns a Core Graphics size structure corresponding to the data in a given string.
58+
@param pszContent A string object whose contents are of the form "{w, h}",
59+
where w is the width and h is the height.
60+
The w and h values can be integer or float values.
61+
An example of a valid string is "{3.0,2.5}".
62+
The string is not localized, so items are always separated with a comma.
63+
@return A Core Graphics structure that represents a size.
64+
If the string is not well-formed, the function returns CCSizeZero.
65+
*/
66+
CCSize CCSizeFromString(const char* pszContent);
67+
}
68+
69+
#endif // __PLATFOMR_CCNS_H__
70+
71+

cocos2dx/platform/CCDirector_mobile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
THE SOFTWARE.
2424
****************************************************************************/
2525

26-
#include "platform/CCNS.h"
26+
#include "cocoa/CCNS.h"
2727
#include "CCDirector.h"
2828
#include "CCScene.h"
2929
#include "CCMutableArray.h"

cocos2dx/platform/CCNS.h

Lines changed: 0 additions & 39 deletions
This file was deleted.

cocos2dx/platform/android/CCNS_android.cpp

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)