CCNinePatchImageParser.cpp
6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "base/CCNinePatchImageParser.h"
#include "platform/CCImage.h"
#include "base/CCDirector.h"
NS_CC_BEGIN
NinePatchImageParser::~NinePatchImageParser()
{
}
NinePatchImageParser::NinePatchImageParser()
:_image(nullptr)
,_imageFrame(Rect::ZERO)
,_isRotated(false)
{
}
NinePatchImageParser::NinePatchImageParser(Image* image)
:_image(image)
,_imageFrame(Rect::ZERO)
,_isRotated(false)
{
this->_imageFrame = Rect(0,0,image->getWidth(), image->getHeight());
CCASSERT(image->getPixelFormat()==backend::PixelFormat::RGBA8888,
"unsupported format, currently only supports rgba8888");
}
NinePatchImageParser::NinePatchImageParser(Image* image, const Rect& frame, bool rotated)
:_image(image)
,_imageFrame(frame)
,_isRotated(rotated)
{
CCASSERT(image->getPixelFormat()==backend::PixelFormat::RGBA8888,
"unsupported format, currently only supports rgba8888");
}
int NinePatchImageParser::getFrameHeight()const
{
if (_isRotated)
{
return _imageFrame.size.width;
}
return _imageFrame.size.height;
}
int NinePatchImageParser::getFrameWidth()const
{
if (_isRotated)
{
return _imageFrame.size.height;
}
return _imageFrame.size.width;
}
int NinePatchImageParser::getPixelOriginOffset(Direction direction)const
{
int imageWidth = _image->getWidth();
int frameWidth = this->getFrameWidth();
int topLineLeftOffset = (int)_imageFrame.origin.y * imageWidth * 4 + (int)_imageFrame.origin.x * 4;
if(direction == Direction::HORIZONTAL)
{
return topLineLeftOffset;
}
else
{
if(_isRotated)
{
return topLineLeftOffset + (frameWidth - 1) * 4;
}
else
{
return topLineLeftOffset;
}
}
}
Vec2 NinePatchImageParser::parseHorizontalMargin()const
{
unsigned char* data = _image->getData();
data = data + this->getPixelOriginOffset(Direction::HORIZONTAL);
unsigned char lastPixel = *(data + 3);
int x1 = 0;
int x2 = 0;
int length = _imageFrame.origin.x + this->getFrameWidth();
for(int i = (int)_imageFrame.origin.x; i <= length ; i++)
{
unsigned char pixel = *(data + (i - (int)_imageFrame.origin.x) * 4 +3);
if(pixel != lastPixel)
{
if (pixel > 0)
{
x1 = (i - (int)_imageFrame.origin.x);
}
else
{
x2 = (i - (int)_imageFrame.origin.x);
break;
}
}
lastPixel = pixel;
}
return Vec2(x1,x2);
}
Vec2 NinePatchImageParser::parseVerticalMargin()const
{
unsigned char* data = _image->getData();
int imageWidth = _image->getWidth();
int y1 = 0;
int y2 = 0;
data = data + this->getPixelOriginOffset(Direction::VERTICAL);
unsigned char lastPixel = *(data + 3);
int length = (int)(_imageFrame.origin.y + this->getFrameHeight());
for(int i = _imageFrame.origin.y; i <= length; i++)
{
unsigned char pixel = *(data + (i - (int)_imageFrame.origin.y) * imageWidth * 4 + 3);
if(pixel != lastPixel)
{
if(pixel > 0)
{
y1 = (i - (int)_imageFrame.origin.y);
}
else
{
y2 = (i - (int)_imageFrame.origin.y);
break;
}
}
lastPixel = pixel;
}
return Vec2(y1,y2);
}
Rect NinePatchImageParser::parseCapInset() const
{
Rect capInsets;
Vec2 horizontalLine = this->parseHorizontalMargin();
Vec2 verticalLine = this->parseVerticalMargin();
if(_isRotated)
{
capInsets = Rect(verticalLine.y,
_imageFrame.size.height - horizontalLine.y,
verticalLine.y - verticalLine.x,
horizontalLine.y - horizontalLine.x);
}
else
{
capInsets = Rect(horizontalLine.x,
verticalLine.x,
horizontalLine.y - horizontalLine.x,
verticalLine.y - verticalLine.x);
}
capInsets = CC_RECT_PIXELS_TO_POINTS(capInsets);
return capInsets;
}
void NinePatchImageParser::setSpriteFrameInfo(Image* image, const cocos2d::Rect& frameRect, bool rotated )
{
this->_image = image;
CCASSERT(image->getPixelFormat()==backend::PixelFormat::RGBA8888,
"unsupported format, currently only supports rgba8888");
this->_imageFrame = frameRect;
this->_isRotated = rotated;
}
bool NinePatchImageParser::isNinePatchImage(const std::string& filepath)
{
size_t length = filepath.length();
if(length <7 )
{
return false;
}
if(filepath.compare(length-6, 6, ".9.png") == 0)
{
return true;
}
else
{
return false;
}
}
NS_CC_END