CCNinePatchImageParser.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "platform/CCPlatformMacros.h"
  21. #include "math/CCGeometry.h"
  22. NS_CC_BEGIN
  23. class Image;
  24. class SpriteFrame;
  25. /**
  26. * A class for paring Android .9 patch image.
  27. * For more about Android .9 patch image format, please refer to
  28. * http://developer.android.com/tools/help/draw9patch.html
  29. *
  30. * The class could parse a single .9 patch image and produce the capInsets
  31. * as well as a sprite atlas and store all the capInsets infos in a Texture2D.
  32. * Note:
  33. * - Currently only PixelFormat::RGBA8888 is supported.
  34. * - TexturePacker Trim mode is not supported at the moment.
  35. */
  36. class CC_DLL NinePatchImageParser
  37. {
  38. public:
  39. /**
  40. * Determines whether a filename contains ".9.png" suffix.
  41. * @param filename A 9-patch image name.
  42. *
  43. * @return If the filename contains ".9.png", then return true, otherwise false.
  44. */
  45. static bool isNinePatchImage(const std::string& filename);
  46. /**
  47. * Default constructor.
  48. *
  49. */
  50. NinePatchImageParser();
  51. /**
  52. * Instantiate a NinePatchImageParser with a Image object.
  53. *
  54. * @param image A Image object pointer.
  55. *
  56. * @return A NinePatchImageParser instance.
  57. */
  58. explicit NinePatchImageParser(Image* image);
  59. /**
  60. * Instantiate a NinePatchImageParser with a Image object and the spriteFrame info.
  61. * The spriteFrame contains the frame rect in the image atlas and whether it
  62. * is rotated or not.
  63. *
  64. * @param image A Image object pointer.
  65. * @param frameRect The sprite frame rect in the image atlas.
  66. * @param rotated Whether is sprite frame is rotated in the image atlas.
  67. */
  68. NinePatchImageParser(Image* image, const Rect& frameRect, bool rotated);
  69. /**
  70. * Change the sprite frame info.
  71. * It is useful when parsing multiple sprite frame with only on NinePatchImageParser.
  72. *
  73. * @param frameRect The sprite frame rect in the image atlas.
  74. * @param rotated Whether is sprite frame is rotated in the image atlas.
  75. */
  76. void setSpriteFrameInfo(Image* image, const Rect& frameRect, bool rotated);
  77. /**
  78. * Default destructor.
  79. */
  80. virtual ~NinePatchImageParser();
  81. /**
  82. * Parsing the image data and extract the capInsets info.
  83. * @return The capInsets Rect.
  84. */
  85. Rect parseCapInset()const;
  86. private:
  87. enum class Direction
  88. {
  89. HORIZONTAL,
  90. VERTICAL
  91. };
  92. int getPixelOriginOffset(Direction direction)const;
  93. Vec2 parseHorizontalMargin()const;
  94. Vec2 parseVerticalMargin()const;
  95. int getFrameWidth()const;
  96. int getFrameHeight()const;
  97. Image* _image;
  98. Rect _imageFrame;
  99. bool _isRotated;
  100. };
  101. NS_CC_END