1
0

CCNinePatchImageParser.cpp 6.0 KB

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