CCNinePatchImageParser.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "platform/CCPlatformMacros.h"
  22. #include "math/CCGeometry.h"
  23. NS_CC_BEGIN
  24. class Image;
  25. class SpriteFrame;
  26. /**
  27. * A class for paring Android .9 patch image.
  28. * For more about Android .9 patch image format, please refer to
  29. * http://developer.android.com/tools/help/draw9patch.html
  30. *
  31. * The class could parse a single .9 patch image and produce the capInsets
  32. * as well as a sprite atlas and store all the capInsets infos in a Texture2D.
  33. * Note:
  34. * - Currently only PixelFormat::RGBA8888 is supported.
  35. * - TexturePacker Trim mode is not supported at the moment.
  36. */
  37. class CC_DLL NinePatchImageParser
  38. {
  39. public:
  40. /**
  41. * Determines whether a filename contains ".9.png" suffix.
  42. * @param filename A 9-patch image name.
  43. *
  44. * @return If the filename contains ".9.png", then return true, otherwise false.
  45. */
  46. static bool isNinePatchImage(const std::string& filename);
  47. /**
  48. * Default constructor.
  49. *
  50. */
  51. NinePatchImageParser();
  52. /**
  53. * Instantiate a NinePatchImageParser with a Image object.
  54. *
  55. * @param image A Image object pointer.
  56. *
  57. * @return A NinePatchImageParser instance.
  58. */
  59. explicit NinePatchImageParser(Image* image);
  60. /**
  61. * Instantiate a NinePatchImageParser with a Image object and the spriteFrame info.
  62. * The spriteFrame contains the frame rect in the image atlas and whether it
  63. * is rotated or not.
  64. *
  65. * @param image A Image object pointer.
  66. * @param frameRect The sprite frame rect in the image atlas.
  67. * @param rotated Whether is sprite frame is rotated in the image atlas.
  68. */
  69. NinePatchImageParser(Image* image, const Rect& frameRect, bool rotated);
  70. /**
  71. * Change the sprite frame info.
  72. * It is useful when parsing multiple sprite frame with only on NinePatchImageParser.
  73. *
  74. * @param frameRect The sprite frame rect in the image atlas.
  75. * @param rotated Whether is sprite frame is rotated in the image atlas.
  76. */
  77. void setSpriteFrameInfo(Image* image, const Rect& frameRect, bool rotated);
  78. /**
  79. * Default destructor.
  80. */
  81. virtual ~NinePatchImageParser();
  82. /**
  83. * Parsing the image data and extract the capInsets info.
  84. * @return The capInsets Rect.
  85. */
  86. Rect parseCapInset()const;
  87. private:
  88. enum class Direction
  89. {
  90. HORIZONTAL,
  91. VERTICAL
  92. };
  93. int getPixelOriginOffset(Direction direction)const;
  94. Vec2 parseHorizontalMargin()const;
  95. Vec2 parseVerticalMargin()const;
  96. int getFrameWidth()const;
  97. int getFrameHeight()const;
  98. Image* _image;
  99. Rect _imageFrame;
  100. bool _isRotated;
  101. };
  102. NS_CC_END