CCSkin.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 "2d/CCSpriteFrame.h"
  21. #include "2d/CCSpriteFrameCache.h"
  22. #include "base/CCDirector.h"
  23. #include "renderer/CCRenderer.h"
  24. #include "editor-support/cocostudio/CCSkin.h"
  25. #include "editor-support/cocostudio/CCTransformHelp.h"
  26. #include "editor-support/cocostudio/CCArmature.h"
  27. using namespace cocos2d;
  28. namespace cocostudio {
  29. #if CC_SPRITEBATCHNODE_RENDER_SUBPIXEL
  30. #define RENDER_IN_SUBPIXEL
  31. #else
  32. #define RENDER_IN_SUBPIXEL(__ARGS__) (ceil(__ARGS__))
  33. #endif
  34. Skin *Skin::create()
  35. {
  36. Skin *skin = new (std::nothrow) Skin();
  37. if(skin && skin->init())
  38. {
  39. skin->autorelease();
  40. return skin;
  41. }
  42. CC_SAFE_DELETE(skin);
  43. return nullptr;
  44. }
  45. Skin *Skin::createWithSpriteFrameName(const std::string& pszSpriteFrameName)
  46. {
  47. Skin *skin = new (std::nothrow) Skin();
  48. if(skin && skin->initWithSpriteFrameName(pszSpriteFrameName))
  49. {
  50. skin->autorelease();
  51. return skin;
  52. }
  53. CC_SAFE_DELETE(skin);
  54. return nullptr;
  55. }
  56. Skin *Skin::create(const std::string& pszFileName)
  57. {
  58. Skin *skin = new (std::nothrow) Skin();
  59. if(skin && skin->initWithFile(pszFileName))
  60. {
  61. skin->autorelease();
  62. return skin;
  63. }
  64. CC_SAFE_DELETE(skin);
  65. return nullptr;
  66. }
  67. Skin::Skin()
  68. : _bone(nullptr)
  69. , _armature(nullptr)
  70. , _displayName("")
  71. {
  72. _skinTransform = Mat4::IDENTITY;
  73. }
  74. bool Skin::initWithSpriteFrameName(const std::string& spriteFrameName)
  75. {
  76. CCAssert(spriteFrameName != "", "");
  77. SpriteFrame *pFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(spriteFrameName);
  78. bool ret = true;
  79. if (pFrame != nullptr)
  80. {
  81. ret = initWithSpriteFrame(pFrame);
  82. }
  83. else
  84. {
  85. CCLOG("Can't find CCSpriteFrame with %s. Please check your .plist file", spriteFrameName.c_str());
  86. ret = false;
  87. }
  88. _displayName = spriteFrameName;
  89. return ret;
  90. }
  91. bool Skin::initWithFile(const std::string& filename)
  92. {
  93. bool ret = Sprite::initWithFile(filename);
  94. _displayName = filename;
  95. return ret;
  96. }
  97. void Skin::setSkinData(const BaseData &var)
  98. {
  99. _skinData = var;
  100. setScaleX(_skinData.scaleX);
  101. setScaleY(_skinData.scaleY);
  102. setRotationSkewX(CC_RADIANS_TO_DEGREES(_skinData.skewX));
  103. setRotationSkewY(CC_RADIANS_TO_DEGREES(-_skinData.skewY));
  104. setPosition(_skinData.x, _skinData.y);
  105. _skinTransform = getNodeToParentTransform();
  106. updateArmatureTransform();
  107. }
  108. const BaseData &Skin::getSkinData() const
  109. {
  110. return _skinData;
  111. }
  112. void Skin::updateArmatureTransform()
  113. {
  114. _transform = TransformConcat(_bone->getNodeToArmatureTransform(), _skinTransform);
  115. // if(_armature && _armature->getBatchNode())
  116. // {
  117. // _transform = TransformConcat(_transform, _armature->getNodeToParentTransform());
  118. // }
  119. }
  120. void Skin::updateTransform()
  121. {
  122. // If it is not visible, or one of its ancestors is not visible, then do nothing:
  123. if( !_visible)
  124. {
  125. _quad.br.vertices.setZero();
  126. _quad.tl.vertices.setZero();
  127. _quad.tr.vertices.setZero();
  128. _quad.bl.vertices.setZero();
  129. }
  130. else
  131. {
  132. //
  133. // calculate the Quad based on the Affine Matrix
  134. //
  135. Mat4 transform = getNodeToParentTransform();
  136. Size &size = _rect.size;
  137. float x1 = _offsetPosition.x;
  138. float y1 = _offsetPosition.y;
  139. float x2 = x1 + size.width;
  140. float y2 = y1 + size.height;
  141. if (_flippedX)
  142. {
  143. std::swap(x1, x2);
  144. }
  145. if (_flippedY)
  146. {
  147. std::swap(y1, y2);
  148. }
  149. float x = transform.m[12];
  150. float y = transform.m[13];
  151. float cr = transform.m[0];
  152. float sr = transform.m[1];
  153. float cr2 = transform.m[5];
  154. float sr2 = -transform.m[4];
  155. float ax = x1 * cr - y1 * sr2 + x;
  156. float ay = x1 * sr + y1 * cr2 + y;
  157. float bx = x2 * cr - y1 * sr2 + x;
  158. float by = x2 * sr + y1 * cr2 + y;
  159. float cx = x2 * cr - y2 * sr2 + x;
  160. float cy = x2 * sr + y2 * cr2 + y;
  161. float dx = x1 * cr - y2 * sr2 + x;
  162. float dy = x1 * sr + y2 * cr2 + y;
  163. _quad.bl.vertices.set(RENDER_IN_SUBPIXEL(ax), RENDER_IN_SUBPIXEL(ay), _positionZ);
  164. _quad.br.vertices.set(RENDER_IN_SUBPIXEL(bx), RENDER_IN_SUBPIXEL(by), _positionZ);
  165. _quad.tl.vertices.set(RENDER_IN_SUBPIXEL(dx), RENDER_IN_SUBPIXEL(dy), _positionZ);
  166. _quad.tr.vertices.set(RENDER_IN_SUBPIXEL(cx), RENDER_IN_SUBPIXEL(cy), _positionZ);
  167. }
  168. // MARMALADE CHANGE: ADDED CHECK FOR nullptr, TO PERMIT SPRITES WITH NO BATCH NODE / TEXTURE ATLAS
  169. if (_textureAtlas)
  170. {
  171. _textureAtlas->updateQuad(&_quad, _textureAtlas->getTotalQuads());
  172. }
  173. }
  174. Mat4 Skin::getNodeToWorldTransform() const
  175. {
  176. return TransformConcat( _bone->getArmature()->getNodeToWorldTransform(), _transform);
  177. }
  178. Mat4 Skin::getNodeToWorldTransformAR() const
  179. {
  180. Mat4 displayTransform = _transform;
  181. Vec2 anchorPoint = _anchorPointInPoints;
  182. anchorPoint = PointApplyTransform(anchorPoint, displayTransform);
  183. displayTransform.m[12] = anchorPoint.x;
  184. displayTransform.m[13] = anchorPoint.y;
  185. return TransformConcat( _bone->getArmature()->getNodeToWorldTransform(),displayTransform);
  186. }
  187. void Skin::draw(Renderer *renderer, const Mat4 &/*transform*/, uint32_t flags)
  188. {
  189. auto mv = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  190. // TODO: implement z order
  191. _quadCommand.init(_globalZOrder,
  192. _texture,
  193. getGLProgramState(),
  194. _blendFunc,
  195. &_quad,
  196. 1,
  197. mv,
  198. flags);
  199. renderer->addCommand(&_quadCommand);
  200. }
  201. void Skin::setBone(Bone *bone)
  202. {
  203. _bone = bone;
  204. if(Armature *armature = _bone->getArmature())
  205. {
  206. _armature = armature;
  207. }
  208. }
  209. Bone *Skin::getBone() const
  210. {
  211. return _bone;
  212. }
  213. }