CCSpriteFrame.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /****************************************************************************
  2. Copyright (c) 2008-2011 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  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. #ifndef __SPRITE_CCSPRITE_FRAME_H__
  25. #define __SPRITE_CCSPRITE_FRAME_H__
  26. #include "2d/CCNode.h"
  27. #include "2d/CCAutoPolygon.h"
  28. #include "base/CCRef.h"
  29. #include "math/CCGeometry.h"
  30. NS_CC_BEGIN
  31. class Texture2D;
  32. /**
  33. * @addtogroup _2d
  34. * @{
  35. */
  36. /** @class SpriteFrame
  37. * @brief A SpriteFrame has:
  38. - texture: A Texture2D that will be used by the Sprite
  39. - rectangle: A rectangle of the texture
  40. You can modify the frame of a Sprite by doing:
  41. @code
  42. SpriteFrame* frame = SpriteFrame::createWithTexture(texture, rect);
  43. sprite->setSpriteFrame(frame);
  44. @endcode
  45. */
  46. class CC_DLL SpriteFrame : public Ref, public Clonable
  47. {
  48. public:
  49. /** Create a SpriteFrame with a texture filename, rect in points.
  50. It is assumed that the frame was not trimmed.
  51. *
  52. * @param filename Texture file name.
  53. * @param rect A specified rect.
  54. * @return An autoreleased SpriteFrame object.
  55. */
  56. static SpriteFrame* create(const std::string& filename, const Rect& rect);
  57. /** Create a SpriteFrame with a texture filename, rect, rotated, offset and originalSize in pixels.
  58. The originalSize is the size in pixels of the frame before being trimmed.
  59. *
  60. * @param filename Texture filename
  61. * @param rect A specified rect.
  62. * @param rotated Is rotated if true.
  63. * @param offset A specified offset.
  64. * @param originalSize A specified original size.
  65. * @return An autoreleased SpriteFrame object.
  66. */
  67. static SpriteFrame* create(const std::string& filename, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize);
  68. /** Create a SpriteFrame with a texture, rect in points.
  69. It is assumed that the frame was not trimmed.
  70. * @param pobTexture The texture pointer.
  71. * @param rect A specified rect.
  72. * @return An autoreleased SpriteFrame object.
  73. */
  74. static SpriteFrame* createWithTexture(Texture2D* pobTexture, const Rect& rect);
  75. /** Create a SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels.
  76. The originalSize is the size in points of the frame before being trimmed.
  77. * @param pobTexture The texture pointer.
  78. * @param rect A specified rect.
  79. * @param rotated Is rotated if true.
  80. * @param offset A specified offset.
  81. * @param originalSize A specified original size.
  82. * @return An autoreleased SpriteFrame object.
  83. */
  84. static SpriteFrame* createWithTexture(Texture2D* pobTexture, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize);
  85. // attributes
  86. /** Get rect of the sprite frame.
  87. *
  88. * @return The rect of the sprite frame, in pixels.
  89. */
  90. const Rect& getRectInPixels() const { return _rectInPixels; }
  91. /** Set rect of the sprite frame.
  92. *
  93. * @param rectInPixels The rect of the sprite frame, in pixels.
  94. */
  95. void setRectInPixels(const Rect& rectInPixels);
  96. /**Is the sprite frame rotated or not.
  97. *
  98. * @return Is rotated if true.
  99. */
  100. bool isRotated() const { return _rotated; }
  101. /** Set rotated of the sprite frame.
  102. *
  103. * @param rotated Rotated the sprite frame if true.
  104. */
  105. void setRotated(bool rotated) { _rotated = rotated; }
  106. /** Get rect of the frame.
  107. *
  108. * @return The rect of the sprite frame.
  109. */
  110. const Rect& getRect() const { return _rect; }
  111. /** Set rect of the frame.
  112. *
  113. * @param rect The rect of the sprite.
  114. */
  115. void setRect(const Rect& rect);
  116. /** Get center rect of the frame.
  117. *
  118. * Useful to create 9-slice sprites
  119. *
  120. * @return The center rect of the sprite frame in points
  121. */
  122. const Rect& getCenterRect() const { return _centerRect; }
  123. /**
  124. * setCenterRect
  125. *
  126. * Useful to implement "9 sliced" sprites.
  127. * The sprite will be sliced into a 3 x 3 grid. The four corners of this grid are applied without
  128. * performing any scaling. The upper- and lower-middle parts are scaled horizontally, and the left- and right-middle parts are scaled vertically.
  129. * The center is scaled in both directions.
  130. * Important: The scaling is based the Sprite's trimmed size.
  131. *
  132. * Limitations: Does not work when the sprite is part of `SpriteBatchNode`.
  133. * @param centerRect the Rect in points
  134. */
  135. void setCenterRectInPixels(const Rect& centerRect);
  136. /** hasCenterRect
  137. @return Whether or not it has a centerRect
  138. */
  139. bool hasCenterRect() const;
  140. /** Get offset of the frame.
  141. *
  142. * @return The offset of the sprite frame, in pixels.
  143. */
  144. const Vec2& getOffsetInPixels() const;
  145. /** Set offset of the frame.
  146. *
  147. * @param offsetInPixels The offset of the sprite frame, in pixels.
  148. */
  149. void setOffsetInPixels(const Vec2& offsetInPixels);
  150. /** Get original size of the trimmed image.
  151. *
  152. * @return The original size of the trimmed image, in pixels.
  153. */
  154. const Size& getOriginalSizeInPixels() const { return _originalSizeInPixels; }
  155. /** Set original size of the trimmed image.
  156. *
  157. * @param sizeInPixels The original size of the trimmed image, in pixels.
  158. */
  159. void setOriginalSizeInPixels(const Size& sizeInPixels) { _originalSizeInPixels = sizeInPixels; }
  160. /** Get original size of the trimmed image.
  161. *
  162. * @return The original size of the trimmed image.
  163. */
  164. const Size& getOriginalSize() const { return _originalSize; }
  165. /** Set original size of the trimmed image.
  166. *
  167. * @param sizeInPixels The original size of the trimmed image.
  168. */
  169. void setOriginalSize(const Size& sizeInPixels) { _originalSize = sizeInPixels; }
  170. /** Get texture of the frame.
  171. *
  172. * @return The texture of the sprite frame.
  173. */
  174. Texture2D* getTexture();
  175. /** Set texture of the frame, the texture is retained.
  176. *
  177. * @param pobTexture The texture of the sprite frame.
  178. */
  179. void setTexture(Texture2D* pobTexture);
  180. /** Get offset of the frame.
  181. *
  182. * @return The offset of the sprite frame.
  183. */
  184. const Vec2& getOffset() const;
  185. /** Set offset of the frame.
  186. *
  187. * @param offsets The offset of the sprite frame.
  188. */
  189. void setOffset(const Vec2& offsets);
  190. /** Get anchor point of the frame.
  191. *
  192. * @return The anchor point of the sprite frame.
  193. */
  194. const Vec2& getAnchorPoint() const;
  195. /** Set anchor point of the frame.
  196. *
  197. * @param anchorPoint The anchor point of the sprite frame.
  198. */
  199. void setAnchorPoint(const Vec2& anchorPoint);
  200. /** Check if anchor point is defined for the frame.
  201. *
  202. * @return true if anchor point is available.
  203. */
  204. bool hasAnchorPoint() const;
  205. // Overrides
  206. virtual SpriteFrame *clone() const override;
  207. /** Set the polygon info for polygon mesh sprites
  208. *
  209. * @param polygonInfo triangle mesh of the sprite
  210. */
  211. void setPolygonInfo(const PolygonInfo &polygonInfo);
  212. /** Get the polygonInfo for this sprite
  213. *
  214. * @return a reference to the polygonInfo structure
  215. */
  216. const PolygonInfo& getPolygonInfo() const;
  217. /** Check if sprite frame is a polygon sprite
  218. *
  219. * @return true if polygonInfo is available
  220. */
  221. bool hasPolygonInfo() const;
  222. CC_CONSTRUCTOR_ACCESS:
  223. /**
  224. * @lua NA
  225. */
  226. SpriteFrame();
  227. /**
  228. * @lua NA
  229. */
  230. virtual ~SpriteFrame();
  231. /** Initializes a SpriteFrame with a texture, rect in points.
  232. It is assumed that the frame was not trimmed.
  233. */
  234. bool initWithTexture(Texture2D* pobTexture, const Rect& rect);
  235. /** Initializes a SpriteFrame with a texture filename, rect in points;
  236. It is assumed that the frame was not trimmed.
  237. */
  238. bool initWithTextureFilename(const std::string& filename, const Rect& rect);
  239. /** Initializes a SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels.
  240. The originalSize is the size in points of the frame before being trimmed.
  241. */
  242. bool initWithTexture(Texture2D* pobTexture, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize);
  243. /** Initializes a SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels.
  244. The originalSize is the size in pixels of the frame before being trimmed.
  245. @since v1.1
  246. */
  247. bool initWithTextureFilename(const std::string& filename, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize);
  248. protected:
  249. Vec2 _offset;
  250. Vec2 _anchorPoint;
  251. Size _originalSize;
  252. Rect _rectInPixels;
  253. bool _rotated;
  254. Rect _rect;
  255. Rect _centerRect;
  256. Vec2 _offsetInPixels;
  257. Size _originalSizeInPixels;
  258. Texture2D *_texture;
  259. std::string _textureFilename;
  260. PolygonInfo _polygonInfo;
  261. };
  262. // end of _2d group
  263. /// @}
  264. NS_CC_END
  265. #endif //__SPRITE_CCSPRITE_FRAME_H__