UIScale9Sprite.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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 "ui/UIScale9Sprite.h"
  21. #include "2d/CCSprite.h"
  22. #include "2d/CCSpriteFrameCache.h"
  23. #include "base/CCVector.h"
  24. #include "base/CCDirector.h"
  25. #include "base/ccUTF8.h"
  26. #include "renderer/CCGLProgram.h"
  27. #include "renderer/ccShaders.h"
  28. #include "platform/CCImage.h"
  29. #include "base/CCNinePatchImageParser.h"
  30. #include "2d/CCDrawNode.h"
  31. #include "2d/CCCamera.h"
  32. #include "renderer/CCRenderer.h"
  33. using namespace cocos2d;
  34. using namespace cocos2d::ui;
  35. Scale9Sprite* Scale9Sprite::create()
  36. {
  37. Scale9Sprite *ret = new (std::nothrow) Scale9Sprite();
  38. if (ret && ret->init())
  39. {
  40. ret->autorelease();
  41. return ret;
  42. }
  43. CC_SAFE_DELETE(ret);
  44. return nullptr;
  45. }
  46. Scale9Sprite* Scale9Sprite::create(const std::string& filename, const Rect& rect, const Rect& capInsets)
  47. {
  48. Scale9Sprite* ret = new (std::nothrow) Scale9Sprite();
  49. if (ret && ret->initWithFile(filename, rect, capInsets))
  50. {
  51. ret->autorelease();
  52. return ret;
  53. }
  54. CC_SAFE_DELETE(ret);
  55. return nullptr;
  56. }
  57. Scale9Sprite* Scale9Sprite::create(const std::string& filename, const Rect& rect)
  58. {
  59. return create(filename, rect, Rect::ZERO);
  60. }
  61. Scale9Sprite* Scale9Sprite::create(const Rect& capInsets, const std::string& file)
  62. {
  63. Scale9Sprite* ret = new (std::nothrow) Scale9Sprite();
  64. if (ret && ret->initWithFile(capInsets, file))
  65. {
  66. ret->autorelease();
  67. return ret;
  68. }
  69. CC_SAFE_DELETE(ret);
  70. return nullptr;
  71. }
  72. Scale9Sprite* Scale9Sprite::create(const std::string& fileaname)
  73. {
  74. return create(Rect::ZERO, fileaname);
  75. }
  76. Scale9Sprite* Scale9Sprite::createWithSpriteFrame(SpriteFrame* spriteFrame, const Rect& capInsets)
  77. {
  78. Scale9Sprite* ret = new (std::nothrow) Scale9Sprite();
  79. if (ret && ret->initWithSpriteFrame(spriteFrame, capInsets))
  80. {
  81. ret->autorelease();
  82. return ret;
  83. }
  84. CC_SAFE_DELETE(ret);
  85. return nullptr;
  86. }
  87. Scale9Sprite* Scale9Sprite::createWithSpriteFrame(SpriteFrame* spriteFrame)
  88. {
  89. return createWithSpriteFrame(spriteFrame, Rect::ZERO);
  90. }
  91. Scale9Sprite* Scale9Sprite::createWithSpriteFrameName(const std::string& spriteFrameName, const Rect& capInsets)
  92. {
  93. Scale9Sprite* ret = new (std::nothrow) Scale9Sprite();
  94. if (ret && ret->initWithSpriteFrameName(spriteFrameName, capInsets))
  95. {
  96. ret->autorelease();
  97. return ret;
  98. }
  99. CC_SAFE_DELETE(ret);
  100. return nullptr;
  101. }
  102. Scale9Sprite* Scale9Sprite::createWithSpriteFrameName(const std::string& spriteFrameName)
  103. {
  104. Scale9Sprite* ret = new (std::nothrow) Scale9Sprite();
  105. if (ret && ret->initWithSpriteFrameName(spriteFrameName, Rect::ZERO))
  106. {
  107. ret->autorelease();
  108. return ret;
  109. }
  110. CC_SAFE_DELETE(ret);
  111. log("Could not allocate Scale9Sprite()");
  112. return nullptr;
  113. }
  114. Scale9Sprite::Scale9Sprite()
  115. : _brightState(State::NORMAL)
  116. , _renderingType(RenderingType::SLICE)
  117. , _insetLeft(0)
  118. , _insetTop(0)
  119. , _insetRight(0)
  120. , _insetBottom(0)
  121. , _isPatch9(false)
  122. {
  123. }
  124. Scale9Sprite::~Scale9Sprite()
  125. {
  126. }
  127. bool Scale9Sprite::initWithFile(const Rect& capInsets, const std::string& file)
  128. {
  129. // calls super
  130. bool ret = Sprite::initWithFile(file);
  131. setupSlice9(getTexture(), capInsets);
  132. return ret;
  133. }
  134. bool Scale9Sprite::initWithFile(const std::string& filename)
  135. {
  136. // calls super
  137. auto originalCapInsets = this->getCapInsets();
  138. bool ret = Sprite::initWithFile(filename);
  139. setupSlice9(getTexture(), originalCapInsets);
  140. return ret;
  141. }
  142. bool Scale9Sprite::initWithFile(const std::string& filename, const Rect& rect)
  143. {
  144. // calls super
  145. auto originalCapInsets = this->getCapInsets();
  146. bool ret = Sprite::initWithFile(filename, rect);
  147. setupSlice9(getTexture(), originalCapInsets);
  148. return ret;
  149. }
  150. bool Scale9Sprite::initWithSpriteFrame(SpriteFrame* spriteFrame, const Rect& capInsets)
  151. {
  152. // calls super
  153. bool ret = Sprite::initWithSpriteFrame(spriteFrame);
  154. setupSlice9(getTexture(), capInsets);
  155. return ret;
  156. }
  157. bool Scale9Sprite::initWithSpriteFrameName(const std::string& spriteFrameName, const Rect& capInsets)
  158. {
  159. // calls super
  160. bool ret = Sprite::initWithSpriteFrameName(spriteFrameName);
  161. setupSlice9(getTexture(), capInsets);
  162. return ret;
  163. }
  164. bool Scale9Sprite::initWithSpriteFrameName(const std::string& spriteFrameName)
  165. {
  166. // calls super
  167. auto originalCapInsets = this->getCapInsets();
  168. bool ret = Sprite::initWithSpriteFrameName(spriteFrameName);
  169. setupSlice9(getTexture(), originalCapInsets);
  170. return ret;
  171. }
  172. bool Scale9Sprite::init()
  173. {
  174. // calls super
  175. bool ret = Sprite::init();
  176. setupSlice9(getTexture(), Rect::ZERO);
  177. return ret;
  178. }
  179. bool Scale9Sprite::init(Sprite* sprite, const Rect& rect, const Rect& capInsets)
  180. {
  181. return init(sprite, rect, false, capInsets);
  182. }
  183. bool Scale9Sprite::init(Sprite* sprite, const Rect& rect, bool rotated, const Rect& capInsets)
  184. {
  185. return init(sprite, rect, rotated, Vec2::ZERO, rect.size, capInsets);
  186. }
  187. bool Scale9Sprite::init(Sprite* sprite, const Rect& origRect, bool rotated, const Vec2 &offset, const Size &originalSize, const Rect& capInsets)
  188. {
  189. bool ret = false;
  190. Rect rect(origRect);
  191. if (sprite) {
  192. auto texture = sprite->getTexture();
  193. if (origRect.equals(Rect::ZERO))
  194. rect.size = texture->getContentSize();
  195. auto spriteFrame = SpriteFrame::createWithTexture(texture, rect, rotated, offset, originalSize);
  196. ret = initWithSpriteFrame(spriteFrame);
  197. setupSlice9(texture, capInsets);
  198. } else {
  199. ret = initWithTexture(nullptr, rect, rotated);
  200. setupSlice9(nullptr, capInsets);
  201. }
  202. return ret;
  203. }
  204. bool Scale9Sprite::initWithBatchNode(SpriteBatchNode *batchnode, const Rect &rect, bool rotated, const Rect &capInsets)
  205. {
  206. auto sprite = Sprite::createWithTexture(batchnode->getTexture());
  207. return init(sprite, rect, rotated, capInsets);
  208. }
  209. bool Scale9Sprite::initWithFile(const std::string& filename, const Rect& rect, const Rect& capInsets)
  210. {
  211. // calls super
  212. bool ret = false;
  213. if (!rect.equals(Rect::ZERO))
  214. {
  215. ret = Sprite::initWithFile(filename, rect);
  216. }
  217. else // if rect is zero, use the whole texture size.
  218. {
  219. ret = Sprite::initWithFile(filename);
  220. }
  221. setupSlice9(getTexture(), capInsets);
  222. return ret;
  223. }
  224. bool Scale9Sprite::initWithBatchNode(SpriteBatchNode *batchnode, const Rect &rect, const Rect &capInsets)
  225. {
  226. auto sprite = Sprite::createWithTexture(batchnode->getTexture());
  227. return init(sprite, rect, false, capInsets);
  228. }
  229. bool Scale9Sprite::updateWithBatchNode(SpriteBatchNode *batchnode, const Rect &originalRect, bool rotated, const Rect &capInsets)
  230. {
  231. Sprite *sprite = Sprite::createWithTexture(batchnode->getTexture());
  232. return updateWithSprite(sprite,
  233. originalRect,
  234. rotated,
  235. Vec2::ZERO,
  236. originalRect.size,
  237. capInsets);
  238. }
  239. bool Scale9Sprite::updateWithSprite(Sprite* sprite,
  240. const Rect& rect,
  241. bool rotated,
  242. const Rect& capInsets)
  243. {
  244. return updateWithSprite(sprite, rect, rotated, Vec2::ZERO, rect.size, capInsets);
  245. }
  246. bool Scale9Sprite::updateWithSprite(Sprite* sprite,
  247. const Rect& textureRect,
  248. bool rotated,
  249. const Vec2 &offset,
  250. const Size &originalSize,
  251. const Rect& capInsets)
  252. {
  253. SpriteFrame *spriteframe = SpriteFrame::createWithTexture(sprite->getTexture(),
  254. CC_RECT_POINTS_TO_PIXELS(textureRect),
  255. rotated,
  256. CC_POINT_POINTS_TO_PIXELS(offset),
  257. CC_SIZE_POINTS_TO_PIXELS(originalSize));
  258. setSpriteFrame(spriteframe);
  259. setCapInsets(capInsets);
  260. return true;
  261. }
  262. Scale9Sprite* Scale9Sprite::resizableSpriteWithCapInsets(const Rect& capInsets) const
  263. {
  264. // FIXME: there are no test cases for this method
  265. Scale9Sprite* ret = new (std::nothrow) Scale9Sprite();
  266. if (ret && ret->init(const_cast<Scale9Sprite*>(this),
  267. _rect,
  268. _rectRotated,
  269. Vec2::ZERO,
  270. _originalContentSize,
  271. capInsets) )
  272. {
  273. ret->autorelease();
  274. return ret;
  275. }
  276. CC_SAFE_DELETE(ret);
  277. return nullptr;
  278. }
  279. Scale9Sprite::State Scale9Sprite::getState() const
  280. {
  281. return _brightState;
  282. }
  283. void Scale9Sprite::setState(Scale9Sprite::State state)
  284. {
  285. if (_brightState != state) {
  286. _brightState = state;
  287. GLProgramState *glState = nullptr;
  288. switch (state)
  289. {
  290. case State::NORMAL:
  291. glState = GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP, getTexture());
  292. break;
  293. case State::GRAY:
  294. glState = GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_GRAYSCALE, getTexture());
  295. default:
  296. break;
  297. }
  298. setGLProgramState(glState);
  299. _brightState = state;
  300. }
  301. }
  302. void Scale9Sprite::setSpriteFrame(SpriteFrame* newFrame)
  303. {
  304. Size oldSize = getPreferredSize();
  305. Sprite::setSpriteFrame(newFrame);
  306. if (oldSize.width !=0 && oldSize.height != 0) {
  307. setPreferredSize(oldSize);
  308. }
  309. }
  310. void Scale9Sprite::setSpriteFrame(SpriteFrame * spriteFrame, const Rect& capInsets)
  311. {
  312. setSpriteFrame(spriteFrame);
  313. setCapInsets(capInsets);
  314. }
  315. void Scale9Sprite::setPreferredSize(const Size& preferredSize)
  316. {
  317. setContentSize(preferredSize);
  318. }
  319. void Scale9Sprite::setInsetLeft(float insetLeft)
  320. {
  321. _insetLeft = insetLeft;
  322. updateCapInset();
  323. }
  324. void Scale9Sprite::setInsetTop(float insetTop)
  325. {
  326. _insetTop = insetTop;
  327. updateCapInset();
  328. }
  329. void Scale9Sprite::setInsetRight(float insetRight)
  330. {
  331. _insetRight = insetRight;
  332. updateCapInset();
  333. }
  334. void Scale9Sprite::setInsetBottom(float insetBottom)
  335. {
  336. _insetBottom = insetBottom;
  337. updateCapInset();
  338. }
  339. void Scale9Sprite::updateCapInset()
  340. {
  341. if (_renderingType == RenderingType::SLICE)
  342. {
  343. Rect capInsets(_insetLeft,
  344. _insetTop,
  345. _originalContentSize.width - _insetRight - _insetLeft,
  346. _originalContentSize.height - _insetBottom -_insetTop);
  347. setCapInsets(capInsets);
  348. }
  349. }
  350. void Scale9Sprite::setCapInsets(int left,int right,int bottom,int top){
  351. _insetLeft = left ;
  352. _insetRight = right;
  353. _insetBottom = bottom;
  354. _insetTop = top;
  355. updateCapInset();
  356. }
  357. Size Scale9Sprite::getOriginalSize() const
  358. {
  359. return _originalContentSize;
  360. }
  361. Size Scale9Sprite::getPreferredSize() const
  362. {
  363. return getContentSize();
  364. }
  365. float Scale9Sprite::getInsetLeft() const
  366. {
  367. return _insetLeft;
  368. }
  369. float Scale9Sprite::getInsetTop() const
  370. {
  371. return _insetTop;
  372. }
  373. float Scale9Sprite::getInsetRight() const
  374. {
  375. return _insetRight;
  376. }
  377. float Scale9Sprite::getInsetBottom() const
  378. {
  379. return _insetBottom;
  380. }
  381. void Scale9Sprite::setScale9Enabled(bool enabled)
  382. {
  383. if (_renderMode == RenderMode::POLYGON) {
  384. CCLOGWARN("Scale9Sprite::setScale9Enabled() can't be called when using POLYGON render modes");
  385. return;
  386. }
  387. RenderingType type = enabled ? RenderingType::SLICE : RenderingType::SIMPLE;
  388. setRenderingType(type);
  389. // only enable stretch when scale9 is enabled
  390. // for backward compatibility, since Sprite stretches the texture no matter the rendering type
  391. setStretchEnabled(enabled);
  392. }
  393. bool Scale9Sprite::isScale9Enabled() const
  394. {
  395. return (_renderingType == RenderingType::SLICE);
  396. }
  397. Sprite* Scale9Sprite::getSprite()
  398. {
  399. return this;
  400. }
  401. /**
  402. * @brief Returns a copy of the Scale9Sprite
  403. */
  404. void Scale9Sprite::copyTo(Scale9Sprite* copy) const
  405. {
  406. copy->initWithSpriteFrame(getSpriteFrame(), getCapInsets());
  407. copy->setRenderingType(_renderingType);
  408. copy->setScale9Enabled(isScale9Enabled());
  409. copy->_isPatch9 = _isPatch9;
  410. copy->_brightState = _brightState;
  411. // these properties should be part of Sprite::clone() (or Node::clone())
  412. // but cloning is not supported on those nodes
  413. copy->setContentSize(getContentSize());
  414. copy->setPosition(getPosition());
  415. copy->setScale(getScaleX(), getScaleY());
  416. copy->setRotation(getRotation());
  417. copy->setRotationSkewX(getRotationSkewX());
  418. copy->setRotationSkewY(getRotationSkewY());
  419. copy->setColor(getColor());
  420. copy->setOpacity(getOpacity());
  421. copy->_originalContentSize = _originalContentSize;
  422. }
  423. // (0,0) O = capInsets.origin
  424. // v0----------------------
  425. // | | | |
  426. // | | | |
  427. // v1-------O------+------|
  428. // | | | |
  429. // | | | |
  430. // v2-------+------+------|
  431. // | | | |
  432. // | | | |
  433. // v3-------------------- (1,1) (texture coordinate is flipped)
  434. // u0 u1 u2 u3
  435. //
  436. // y3----------------------(preferedSize.width, preferedSize.height)
  437. // | | | |
  438. // | | | |
  439. // y2-------O------+------|
  440. // | | | |
  441. // | | | |
  442. // y1-------+------+------|
  443. // | | | |
  444. // | | | |
  445. //x0,y0--------------------
  446. // x1 x2 x3
  447. void Scale9Sprite::setRenderingType(Scale9Sprite::RenderingType type)
  448. {
  449. if (_renderMode == RenderMode::POLYGON) {
  450. CCLOGWARN("Scale9Sprite::setRenderingType() can't be called when using POLYGON render modes");
  451. return;
  452. }
  453. if (_renderingType != type) {
  454. _renderingType = type;
  455. if (_renderingType == RenderingType::SIMPLE) {
  456. setCenterRectNormalized(Rect(0,0,1,1));
  457. } else {
  458. updateCapInset();
  459. }
  460. }
  461. }
  462. Scale9Sprite::RenderingType Scale9Sprite::getRenderingType() const
  463. {
  464. return _renderingType;
  465. }
  466. void Scale9Sprite::resetRender()
  467. {
  468. // nothing. keeping it to be backwards compatible
  469. }
  470. void Scale9Sprite::setupSlice9(Texture2D* texture, const Rect& capInsets)
  471. {
  472. if (texture && texture->isContain9PatchInfo()) {
  473. auto& parsedCapInset = texture->getSpriteFrameCapInset(getSpriteFrame());
  474. if(!parsedCapInset.equals(Rect::ZERO))
  475. {
  476. // adjust texture rect. 1.3f seems to be the magic number
  477. // to avoid artifacts
  478. auto rect = getTextureRect();
  479. rect.origin.x += 1.3f;
  480. rect.origin.y += 1.3f;
  481. rect.size.width -= 2.0f;
  482. rect.size.height -= 2.0f;
  483. setTextureRect(rect);
  484. // and after adjusting the texture, set the new cap insets
  485. _isPatch9 = true;
  486. setCapInsets(parsedCapInset);
  487. }
  488. }
  489. if (!_isPatch9)
  490. {
  491. setCapInsets(capInsets);
  492. }
  493. }
  494. void Scale9Sprite::setCapInsets(const cocos2d::Rect &insetsCopy)
  495. {
  496. Rect insets = insetsCopy;
  497. // When Insets == Zero --> we should use a 1/3 of its untrimmed size
  498. if (insets.equals(Rect::ZERO)) {
  499. insets = Rect( _originalContentSize.width / 3.0f,
  500. _originalContentSize.height / 3.0f,
  501. _originalContentSize.width / 3.0f,
  502. _originalContentSize.height / 3.0f);
  503. }
  504. // emulate invalid insets. shouldn't be supported, but the original code supported it.
  505. if (insets.origin.x > _originalContentSize.width)
  506. insets.origin.x = 0;
  507. if (insets.origin.y > _originalContentSize.height)
  508. insets.origin.y = 0;
  509. if (insets.size.width > _originalContentSize.width)
  510. insets.size.width = 1;
  511. if (insets.size.height > _originalContentSize.height)
  512. insets.size.height = 1;
  513. _insetLeft = insets.origin.x;
  514. _insetTop = insets.origin.y;
  515. _insetRight = _originalContentSize.width - _insetLeft - insets.size.width;
  516. _insetBottom = _originalContentSize.height - _insetTop - insets.size.height;
  517. // we have to convert from untrimmed to trimmed
  518. // Sprite::setCenterRect is using trimmed values (to be compatible with Cocos Creator)
  519. // Scale9Sprite::setCapInsects uses untrimmed values (which makes more sense)
  520. // use _rect coordinates. recenter origin to calculate the
  521. // intersecting rectangle
  522. // can't use _offsetPosition since it is calculated using bottom-left as origin,
  523. // and the center rect is calculated using top-left
  524. insets.origin.x -= (_originalContentSize.width - _rect.size.width) / 2 + _unflippedOffsetPositionFromCenter.x;
  525. insets.origin.y -= (_originalContentSize.height - _rect.size.height) / 2 - _unflippedOffsetPositionFromCenter.y;
  526. // intersecting rectangle
  527. const float x1 = std::max(insets.origin.x, 0.0f);
  528. const float y1 = std::max(insets.origin.y, 0.0f);
  529. const float x2 = std::min(insets.origin.x + insets.size.width, 0.0f + _rect.size.width);
  530. const float y2 = std::min(insets.origin.y + insets.size.height, 0.0f + _rect.size.height);
  531. // centerRect uses the trimmed frame origin as 0,0.
  532. // so, recenter inset rect
  533. insets.setRect(x1,
  534. y1,
  535. x2 - x1,
  536. y2 - y1);
  537. // Only update center rect while in slice mode.
  538. if (_renderingType == RenderingType::SLICE && _renderMode != RenderMode::POLYGON)
  539. {
  540. setCenterRect(insets);
  541. }
  542. }
  543. Rect Scale9Sprite::getCapInsets() const
  544. {
  545. return Rect(_insetLeft,
  546. _insetTop,
  547. _originalContentSize.width - _insetLeft - _insetRight,
  548. _originalContentSize.height - _insetTop - _insetBottom);
  549. }