1
0

UIScale9Sprite.cpp 18 KB

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