UIScale9Sprite.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. #ifndef __cocos2d_libs__UIScale9Sprite__
  22. #define __cocos2d_libs__UIScale9Sprite__
  23. #include "2d/CCSprite.h"
  24. #include "2d/CCSpriteFrame.h"
  25. #include "2d/CCSpriteBatchNode.h"
  26. #include "platform/CCPlatformMacros.h"
  27. #include "ui/GUIExport.h"
  28. #include "renderer/CCTrianglesCommand.h"
  29. /**
  30. * @addtogroup ui
  31. * @{
  32. */
  33. NS_CC_BEGIN
  34. class DrawNode;
  35. class Texture2D;
  36. namespace ui {
  37. /**
  38. *@brief A 9-slice sprite for cocos2d-x.
  39. *
  40. * 9-slice scaling allows you to specify how scaling is applied
  41. * to specific areas of a sprite. With 9-slice scaling (3x3 grid),
  42. * you can ensure that the sprite does not become distorted when
  43. * scaled.
  44. * Note: When you set _scale9Enabled to false,
  45. * then you could call `scale9Sprite->getSprite()` to return the inner Sprite pointer.
  46. * Then you could call any methods of Sprite class with the return pointers.
  47. *
  48. */
  49. class CC_GUI_DLL Scale9Sprite : public Sprite
  50. {
  51. public:
  52. /**
  53. * Default constructor.
  54. * @js ctor
  55. * @lua new
  56. */
  57. Scale9Sprite();
  58. /**
  59. * Default destructor.
  60. * @js NA
  61. * @lua NA
  62. */
  63. virtual ~Scale9Sprite();
  64. /**
  65. * Builtin shader state.
  66. * Currently support Normal and Gray state.
  67. */
  68. enum class State
  69. {
  70. NORMAL,
  71. GRAY
  72. };
  73. enum class RenderingType
  74. {
  75. SIMPLE,
  76. SLICE
  77. };
  78. public:
  79. /**
  80. * @brief Create an empty Scale9Sprite.
  81. *
  82. * @return A Scale9Sprite instance.
  83. */
  84. static Scale9Sprite* create();
  85. /**
  86. * Creates a 9-slice sprite with a texture file, a delimitation zone and
  87. * with the specified cap insets.
  88. *
  89. * @see initWithFile(const char *file, const Rect& rect, const Rect& capInsets)
  90. * @param file A texture file name.
  91. * @param rect A delimitation zone.
  92. * @param capInsets A specified cap insets.
  93. * @return A Scale9Sprite instance.
  94. */
  95. static Scale9Sprite* create(const std::string& file, const Rect& rect, const Rect& capInsets);
  96. /**
  97. * Creates a 9-slice sprite with a texture file. The whole texture will be
  98. * broken down into a 3×3 grid of equal blocks.
  99. *
  100. * @see initWithFile(const Rect& capInsets, const char *file)
  101. * @param capInsets A specified cap insets.
  102. * @param file A texture file name.
  103. * @return A Scale9Sprite instance.
  104. */
  105. static Scale9Sprite* create(const Rect& capInsets, const std::string& file);
  106. /**
  107. * Creates a 9-slice sprite with a texture file and a delimitation zone. The
  108. * texture will be broken down into a 3×3 grid of equal blocks.
  109. *
  110. * @see initWithFile(const char *file, const Rect& rect)
  111. * @param file A texture file name.
  112. * @param rect A delimitation zone.
  113. * @return A Scale9Sprite instance.
  114. */
  115. static Scale9Sprite* create(const std::string& file, const Rect& rect);
  116. /**
  117. * Creates a 9-slice sprite with a texture file. The whole texture will be
  118. * broken down into a 3×3 grid of equal blocks.
  119. *
  120. * @see initWithFile(const char *file)
  121. * @param file A texture file name.
  122. * @return A Scale9Sprite instance.
  123. */
  124. static Scale9Sprite* create(const std::string& file);
  125. /**
  126. * Creates a 9-slice sprite with an sprite frame.
  127. * Once the sprite is created, you can then call its "setContentSize:" method
  128. * to resize the sprite will all it's 9-slice goodness interact.
  129. * It respects the anchorPoint too.
  130. *
  131. * @see initWithSpriteFrame(SpriteFrame *spriteFrame)
  132. * @param spriteFrame A sprite frame pointer.
  133. * @return A Scale9Sprite instance.
  134. */
  135. static Scale9Sprite* createWithSpriteFrame(SpriteFrame* spriteFrame);
  136. /**
  137. * Creates a 9-slice sprite with an sprite frame and the centre of its zone.
  138. * Once the sprite is created, you can then call its "setContentSize:" method
  139. * to resize the sprite will all it's 9-slice goodness interact.
  140. * It respects the anchorPoint too.
  141. *
  142. * @see initWithSpriteFrame(SpriteFrame *spriteFrame, const Rect& capInsets)
  143. * @param spriteFrame A sprite frame pointer.
  144. * @param capInsets A delimitation zone.
  145. * @return A Scale9Sprite instance.
  146. */
  147. static Scale9Sprite* createWithSpriteFrame(SpriteFrame* spriteFrame, const Rect& capInsets);
  148. /**
  149. * Creates a 9-slice sprite with an sprite frame name.
  150. * Once the sprite is created, you can then call its "setContentSize:" method
  151. * to resize the sprite will all it's 9-slice goodness interact.
  152. * It respects the anchorPoint too.
  153. *
  154. * @see initWithSpriteFrameName(const char *spriteFrameName)
  155. * @param spriteFrameName A sprite frame name.
  156. * @return A Scale9Sprite instance.
  157. */
  158. static Scale9Sprite* createWithSpriteFrameName(const std::string& spriteFrameName);
  159. /**
  160. * Creates a 9-slice sprite with an sprite frame name and the centre of its zone.
  161. * Once the sprite is created, you can then call its "setContentSize:" method
  162. * to resize the sprite will all it's 9-slice goodness interact.
  163. * It respects the anchorPoint too.
  164. *
  165. * @see initWithSpriteFrameName(const char *spriteFrameName, const Rect& capInsets)
  166. * @param spriteFrameName A sprite frame name.
  167. * @param capInsets A delimitation zone.
  168. * @return A Scale9Sprite instance.
  169. */
  170. static Scale9Sprite* createWithSpriteFrameName(const std::string& spriteFrameName, const Rect& capInsets);
  171. // overridden methods that takes different parameters
  172. using Sprite::initWithFile;
  173. using Sprite::initWithSpriteFrame;
  174. using Sprite::initWithSpriteFrameName;
  175. using Sprite::setSpriteFrame;
  176. /**
  177. * Initializes a 9-slice sprite with a texture file, a delimitation zone and
  178. * with the specified cap insets.
  179. * Once the sprite is created, you can then call its "setContentSize:" method
  180. * to resize the sprite will all it's 9-slice goodness interact.
  181. * It respects the anchorPoint too.
  182. *
  183. * @param file The name of the texture file.
  184. * @param rect The rectangle that describes the sub-part of the texture that
  185. * is the whole image. If the shape is the whole texture, set this to the
  186. * texture's full rect.
  187. * @param capInsets The values to use for the cap insets.
  188. * @return True if initialize success, false otherwise.
  189. */
  190. virtual bool initWithFile(const std::string& file, const Rect& rect, const Rect& capInsets);
  191. /**
  192. * Initializes a 9-slice sprite with a texture file and with the specified cap
  193. * insets.
  194. * Once the sprite is created, you can then call its "setContentSize:" method
  195. * to resize the sprite will all it's 9-slice goodness interact.
  196. * It respects the anchorPoint too.
  197. *
  198. * @param file The name of the texture file.
  199. * @param capInsets The values to use for the cap insets.
  200. * @return True if initializes success, false otherwise.
  201. */
  202. virtual bool initWithFile(const Rect& capInsets, const std::string& file);
  203. /**
  204. * Initializes a 9-slice sprite with an sprite frame and with the specified
  205. * cap insets.
  206. * Once the sprite is created, you can then call its "setContentSize:" method
  207. * to resize the sprite will all it's 9-slice goodness interact.
  208. * It respects the anchorPoint too.
  209. *
  210. * @param spriteFrame The sprite frame object.
  211. * @param capInsets The values to use for the cap insets.
  212. * @return True if initializes success, false otherwise.
  213. */
  214. virtual bool initWithSpriteFrame(SpriteFrame* spriteFrame, const Rect& capInsets);
  215. /**
  216. * Initializes a 9-slice sprite with an sprite frame name and with the specified
  217. * cap insets.
  218. * Once the sprite is created, you can then call its "setContentSize:" method
  219. * to resize the sprite will all it's 9-slice goodness interact.
  220. * It respects the anchorPoint too.
  221. *
  222. * @param spriteFrameName The sprite frame name.
  223. * @param capInsets The values to use for the cap insets.
  224. * @return True if initializes success, false otherwise.
  225. */
  226. virtual bool initWithSpriteFrameName(const std::string& spriteFrameName, const Rect& capInsets);
  227. //override function
  228. /**
  229. * Initializes a 9-slice sprite with a texture file and a delimitation zone. The
  230. * texture will be broken down into a 3×3 grid of equal blocks.
  231. * Once the sprite is created, you can then call its "setContentSize:" method
  232. * to resize the sprite will all it's 9-slice goodness interact.
  233. * It respects the anchorPoint too.
  234. *
  235. * @param file The name of the texture file.
  236. * @param rect The rectangle that describes the sub-part of the texture that
  237. * is the whole image. If the shape is the whole texture, set this to the
  238. * texture's full rect.
  239. * @return True if initializes success, false otherwise.
  240. */
  241. virtual bool initWithFile(const std::string& file, const Rect& rect) override;
  242. /**
  243. * Initializes a 9-slice sprite with a texture file. The whole texture will be
  244. * broken down into a 3×3 grid of equal blocks.
  245. * Once the sprite is created, you can then call its "setContentSize:" method
  246. * to resize the sprite will all it's 9-slice goodness interact.
  247. * It respects the anchorPoint too.
  248. *
  249. * @param file The name of the texture file.
  250. * @return True if initializes success, false otherwise.
  251. */
  252. virtual bool initWithFile(const std::string& file) override;
  253. /**
  254. * Initializes a 9-slice sprite with an sprite frame name.
  255. * Once the sprite is created, you can then call its "setContentSize:" method
  256. * to resize the sprite will all it's 9-slice goodness interact.
  257. * It respects the anchorPoint too.
  258. *
  259. * @param spriteFrameName The sprite frame name.
  260. * @return True if initializes success, false otherwise.
  261. */
  262. virtual bool initWithSpriteFrameName(const std::string& spriteFrameName) override;
  263. virtual bool init() override;
  264. /**
  265. * @brief Initializes a 9-slice sprite with an sprite instance.
  266. * Once the sprite is created, you can then call its "setContentSize:" method
  267. * to resize the sprite will all it's 9-slice goodness interact.
  268. * It respects the anchorPoint too.
  269. *
  270. * @param sprite The sprite instance.
  271. * @param rect A delimitation zone.
  272. * @param rotated Whether the sprite is rotated or not.
  273. * @param capInsets The values to use for the cap insets.
  274. * @return True if initializes success, false otherwise.
  275. */
  276. virtual bool init(Sprite* sprite, const Rect& rect, bool rotated, const Rect& capInsets);
  277. /**
  278. * @brief Initializes a 9-slice sprite with an sprite instance.
  279. * Once the sprite is created, you can then call its "setContentSize:" method
  280. * to resize the sprite will all it's 9-slice goodness interact.
  281. * It respects the anchorPoint too.
  282. *
  283. * @param sprite The sprite instance.
  284. * @param rect A delimitation zone.
  285. * @param capInsets The values to use for the cap insets.
  286. * @return True if initializes success, false otherwise.
  287. */
  288. virtual bool init(Sprite* sprite, const Rect& rect, const Rect& capInsets);
  289. /**
  290. * @brief Initializes a 9-slice sprite with an sprite instance.
  291. * Once the sprite is created, you can then call its "setContentSize:" method
  292. * to resize the sprite will all it's 9-slice goodness interact.
  293. * It respects the anchorPoint too.
  294. *
  295. * @param sprite The sprite instance.
  296. * @param rect A delimitation zone.
  297. * @param rotated Whether the sprite is rotated or not.
  298. * @param offset The offset when slice the sprite.
  299. * @param originalSize The original size of sprite.
  300. * @param capInsets The values to use for the cap insets.
  301. * @return True if initializes success, false otherwise.
  302. */
  303. virtual bool init(Sprite* sprite,
  304. const Rect& rect,
  305. bool rotated,
  306. const Vec2 &offset,
  307. const Size &originalSize,
  308. const Rect& capInsets);
  309. /**
  310. * @brief Initializes a 9-slice sprite with a sprite batchnode.
  311. * Once the sprite is created, you can then call its "setContentSize:" method
  312. * to resize the sprite will all it's 9-slice goodness interact.
  313. * It respects the anchorPoint too.
  314. *
  315. * @deprecated Use @see `init` instead.
  316. * @param batchnode A batch node pointer.
  317. * @param rect A delimitation zone.
  318. * @param rotated Whether the sprite in batch node is rotated or not.
  319. * @param capInsets The values to use for the cap insets.
  320. * @return True if initializes success, false otherwise.
  321. */
  322. CC_DEPRECATED(v3) virtual bool initWithBatchNode(SpriteBatchNode* batchnode,
  323. const Rect& rect,
  324. bool rotated,
  325. const Rect& capInsets);
  326. /**
  327. * @brief Initializes a 9-slice sprite with a sprite batch node.
  328. * Once the sprite is created, you can then call its "setContentSize:" method
  329. * to resize the sprite will all it's 9-slice goodness interact.
  330. * It respects the anchorPoint too.
  331. *
  332. * @deprecated Use @see `init` instead.
  333. * @param batchnode A batch node pointer.
  334. * @param rect A delimitation zone.
  335. * @param capInsets The values to use for the cap insets.
  336. * @return True if initializes success, false otherwise.
  337. */
  338. CC_DEPRECATED(v3) virtual bool initWithBatchNode(SpriteBatchNode* batchnode, const Rect& rect, const Rect& capInsets);
  339. /**
  340. * Creates and returns a new sprite object with the specified cap insets.
  341. * You use this method to add cap insets to a sprite or to change the existing
  342. * cap insets of a sprite. In both cases, you get back a new image and the
  343. * original sprite remains untouched.
  344. *
  345. * @param capInsets The values to use for the cap insets.
  346. * @return A Scale9Sprite instance.
  347. */
  348. Scale9Sprite* resizableSpriteWithCapInsets(const Rect& capInsets) const;
  349. /**
  350. * @brief Update Scale9Sprite with a specified sprite.
  351. *
  352. * @param sprite A sprite pointer.
  353. * @param rect A delimitation zone.
  354. * @param rotated Whether the sprite is rotated or not.
  355. * @param capInsets The Values to use for the cap insets.
  356. * @return True if update success, false otherwise.
  357. * @js NA
  358. */
  359. virtual bool updateWithSprite(Sprite* sprite,
  360. const Rect& rect,
  361. bool rotated,
  362. const Rect& capInsets);
  363. /**
  364. * @brief Update Scale9Sprite with a specified sprite.
  365. *
  366. * @param sprite A sprite pointer.
  367. * @param rect A delimitation zone.
  368. * @param rotated Whether the sprite is rotated or not.
  369. * @param offset The offset when slice the sprite.
  370. * @param originalSize The original size of the sprite.
  371. * @param capInsets The Values to use for the cap insets.
  372. * @return True if update success, false otherwise.
  373. * @js NA
  374. */
  375. virtual bool updateWithSprite(Sprite* sprite,
  376. const Rect& rect,
  377. bool rotated,
  378. const Vec2 &offset,
  379. const Size &originalSize,
  380. const Rect& capInsets);
  381. /**
  382. * @brief Update Scale9Sprite with a specified sprite.
  383. *
  384. * @deprecated Use @see `updateWithSprite` instead.
  385. * @param batchnode A sprite batch pointer.
  386. * @param originalRect A delimitation zone.
  387. * @param rotated Whether the sprite is rotated or not.
  388. * @param capInsets The Values to use for the cap insets.
  389. * @return True if update success, false otherwise.
  390. */
  391. CC_DEPRECATED(v3) bool updateWithBatchNode(SpriteBatchNode* batchnode,
  392. const Rect& originalRect,
  393. bool rotated,
  394. const Rect& capInsets);
  395. /**
  396. * @brief Change inner sprite's sprite frame.
  397. *
  398. * @param spriteFrame A sprite frame pointer.
  399. * @param capInsets The values to use for the cap insets.
  400. */
  401. virtual void setSpriteFrame(SpriteFrame * spriteFrame, const Rect& capInsets);
  402. /**
  403. * Change the state of 9-slice sprite.
  404. * @see `State`
  405. * @param state A enum value in State.
  406. * @since v3.4
  407. */
  408. void setState(State state);
  409. /**
  410. * Query the current bright state.
  411. * @return @see `State`
  412. * @since v3.7
  413. */
  414. State getState() const;
  415. /**
  416. * @brief Query the sprite's original size.
  417. *
  418. * @return Sprite size.
  419. */
  420. Size getOriginalSize() const;
  421. /**
  422. * @brief Change the preferred size of Scale9Sprite.
  423. *
  424. * @param size A delimitation zone.
  425. */
  426. void setPreferredSize(const Size& size);
  427. /**
  428. * @brief Query the Scale9Sprite's preferred size.
  429. *
  430. * @return Scale9Sprite's preferred size.
  431. */
  432. Size getPreferredSize() const;
  433. /**
  434. * @brief Change the left sprite's cap inset.
  435. *
  436. * @param leftInset The values to use for the cap inset.
  437. */
  438. void setInsetLeft(float leftInset);
  439. /**
  440. * @brief Query the left sprite's cap inset.
  441. *
  442. * @return The left sprite's cap inset.
  443. */
  444. float getInsetLeft() const;
  445. /**
  446. * @brief Change the top sprite's cap inset.
  447. *
  448. * @param topInset The values to use for the cap inset.
  449. */
  450. void setInsetTop(float topInset);
  451. /**
  452. * @brief Query the top sprite's cap inset.
  453. *
  454. * @return The top sprite's cap inset.
  455. */
  456. float getInsetTop() const;
  457. /**
  458. * @brief Change the right sprite's cap inset.
  459. *
  460. * @param rightInset The values to use for the cap inset.
  461. */
  462. void setInsetRight(float rightInset);
  463. /**
  464. * @brief Query the right sprite's cap inset.
  465. *
  466. * @return The right sprite's cap inset.
  467. */
  468. float getInsetRight() const;
  469. /**
  470. * @brief Change the bottom sprite's cap inset.
  471. *
  472. * @param bottomInset The values to use for the cap inset.
  473. */
  474. void setInsetBottom(float bottomInset);
  475. /**
  476. * @brief Query the bottom sprite's cap inset.
  477. *
  478. * @return The bottom sprite's cap inset.
  479. */
  480. float getInsetBottom() const;
  481. /**
  482. * @brief Toggle 9-slice feature.
  483. * If Scale9Sprite is 9-slice disabled, the Scale9Sprite will rendered as a normal sprite.
  484. * @warning: Don't use setScale9Enabled(false), use setRenderingType(RenderingType::SIMPLE) instead.
  485. * The setScale9Enabled(false) is kept only for back back compatibility.
  486. * @param enabled True to enable 9-slice, false otherwise.
  487. * @js NA
  488. */
  489. void setScale9Enabled(bool enabled);
  490. /**
  491. * @brief Query whether the Scale9Sprite is enable 9-slice or not.
  492. *
  493. * @return True if 9-slice is enabled, false otherwise.
  494. * @js NA
  495. */
  496. bool isScale9Enabled() const;
  497. /// @} end of Children and Parent
  498. /**
  499. * @brief Get the original no 9-sliced sprite
  500. *
  501. * @return A sprite instance.
  502. */
  503. Sprite* getSprite();
  504. /**
  505. * @brief copies self to copy
  506. */
  507. void copyTo(Scale9Sprite* copy) const;
  508. /**
  509. * Set the slice sprite rendering type.
  510. * When setting to SIMPLE, only 4 vertexes is used to rendering.
  511. * otherwise 16 vertexes will be used to rendering.
  512. * @see RenderingType
  513. */
  514. void setRenderingType(RenderingType type);
  515. /**
  516. * Return the slice sprite rendering type.
  517. */
  518. RenderingType getRenderingType() const;
  519. /**
  520. * Set the Cap Insets in Points using the untrimmed size as reference
  521. */
  522. void setCapInsets(const Rect& insets);
  523. /**
  524. * Returns the Cap Insets
  525. */
  526. Rect getCapInsets() const;
  527. void resetRender();
  528. protected:
  529. void updateCapInset();
  530. void setupSlice9(Texture2D* texture, const Rect& capInsets);
  531. bool _isPatch9;
  532. float _insetLeft;
  533. float _insetRight;
  534. float _insetTop;
  535. float _insetBottom;
  536. Scale9Sprite::State _brightState;
  537. Scale9Sprite::RenderingType _renderingType;
  538. };
  539. }} //end of namespace
  540. // end of ui group
  541. /// @}
  542. #endif /* defined(__cocos2d_libs__UIScale9Sprite__) */