CCBoneNode.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /****************************************************************************
  2. Copyright (c) 2015-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 "base/CCDirector.h"
  21. #include "renderer/CCRenderer.h"
  22. #include "renderer/ccGLStateCache.h"
  23. #include "renderer/CCGLProgram.h"
  24. #include "renderer/CCGLProgramState.h"
  25. #include "editor-support/cocostudio/ActionTimeline/CCBoneNode.h"
  26. #include "editor-support/cocostudio/ActionTimeline/CCSkeletonNode.h"
  27. NS_TIMELINE_BEGIN
  28. BoneNode::BoneNode()
  29. : _isRackShow(false)
  30. , _rackColor(cocos2d::Color4F::WHITE)
  31. , _rackLength(50)
  32. , _rackWidth(20)
  33. , _rootSkeleton(nullptr)
  34. , _blendFunc(cocos2d::BlendFunc::ALPHA_NON_PREMULTIPLIED)
  35. {
  36. }
  37. BoneNode* BoneNode::create()
  38. {
  39. BoneNode* ret = new (std::nothrow) BoneNode();
  40. if (ret && ret->init())
  41. {
  42. ret->autorelease();
  43. return ret;
  44. }
  45. CC_SAFE_DELETE(ret);
  46. return nullptr;
  47. }
  48. BoneNode* BoneNode::create(int length)
  49. {
  50. BoneNode* ret = new (std::nothrow) BoneNode();
  51. if (ret && ret->init())
  52. {
  53. ret->setDebugDrawLength(length);
  54. ret->autorelease();
  55. }
  56. else
  57. {
  58. CC_SAFE_DELETE(ret);
  59. }
  60. return ret;
  61. }
  62. void BoneNode::addChild(cocos2d::Node* child, int localZOrder, int tag)
  63. {
  64. addToChildrenListHelper(child);
  65. Node::addChild(child, localZOrder, tag);
  66. }
  67. void BoneNode::addChild(Node* child, int localZOrder, const std::string &name)
  68. {
  69. addToChildrenListHelper(child);
  70. Node::addChild(child, localZOrder, name);
  71. }
  72. void BoneNode::addSkin(SkinNode* skin, bool isDisplay, bool hideOthers)
  73. {
  74. CCASSERT(skin != nullptr, "Argument must be non-nil");
  75. if (hideOthers)
  76. {
  77. for (auto &bonskin : _boneSkins)
  78. {
  79. bonskin->setVisible(false);
  80. }
  81. }
  82. Node::addChild(skin);
  83. skin->setVisible(isDisplay);
  84. }
  85. void BoneNode::addSkin(SkinNode* skin, bool display)
  86. {
  87. addSkin(skin, display, false);
  88. }
  89. void BoneNode::removeChild(Node* child, bool cleanup /* = true */)
  90. {
  91. ssize_t index = _children.getIndex(child);
  92. if (index != cocos2d::CC_INVALID_INDEX)
  93. {
  94. removeFromChildrenListHelper(child);
  95. Node::removeChild(child, cleanup);
  96. }
  97. }
  98. void BoneNode::removeFromBoneList(BoneNode* bone)
  99. {
  100. if (_rootSkeleton != nullptr)
  101. {
  102. auto skeletonNode = dynamic_cast<SkeletonNode*>(bone);
  103. if (skeletonNode == nullptr) // is not a nested skeleton
  104. {
  105. auto subBones = bone->getAllSubBones();
  106. subBones.pushBack(bone);
  107. for (auto &subBone : subBones)
  108. {
  109. if (subBone->_rootSkeleton == nullptr)
  110. continue;
  111. subBone->_rootSkeleton = nullptr;
  112. auto toremoveIter = _rootSkeleton->_subBonesMap.find(subBone->getName());
  113. if (toremoveIter != _rootSkeleton->_subBonesMap.end())
  114. {
  115. _rootSkeleton->_subBonesMap.erase(toremoveIter);
  116. _rootSkeleton->_subBonesDirty = true;
  117. _rootSkeleton->_subBonesOrderDirty = true;
  118. }
  119. }
  120. }
  121. else
  122. {
  123. _rootSkeleton->_subBonesDirty = true;
  124. _rootSkeleton->_subBonesOrderDirty = true;
  125. }
  126. }
  127. _childBones.eraseObject(bone);
  128. }
  129. void BoneNode::addToBoneList(BoneNode* bone)
  130. {
  131. _childBones.pushBack(bone);
  132. if (_rootSkeleton != nullptr)
  133. {
  134. auto skeletonNode = dynamic_cast<SkeletonNode*>(bone);
  135. if (skeletonNode == nullptr && bone->_rootSkeleton == nullptr) // not nest skeleton
  136. {
  137. auto subBones = bone->getAllSubBones();
  138. subBones.pushBack(bone);
  139. for (auto &subBone : subBones)
  140. {
  141. subBone->_rootSkeleton = _rootSkeleton;
  142. auto bonename = subBone->getName();
  143. if (_rootSkeleton->_subBonesMap.find(bonename) == _rootSkeleton->_subBonesMap.end())
  144. {
  145. _rootSkeleton->_subBonesMap.insert(subBone->getName(), subBone);
  146. _rootSkeleton->_subBonesDirty = true;
  147. _rootSkeleton->_subBonesOrderDirty = true;
  148. }
  149. else
  150. CCLOG("already has a bone named %s in skeleton %s", bonename.c_str(), _rootSkeleton->getName().c_str());
  151. }
  152. }
  153. else
  154. {
  155. _rootSkeleton->_subBonesDirty = true;
  156. _rootSkeleton->_subBonesOrderDirty = true;
  157. }
  158. }
  159. }
  160. void BoneNode::addToSkinList(SkinNode* skin)
  161. {
  162. _boneSkins.pushBack(skin);
  163. auto blendSkin = dynamic_cast<BlendProtocol*>(skin);
  164. if (nullptr != blendSkin && _blendFunc != blendSkin->getBlendFunc())
  165. {
  166. blendSkin->setBlendFunc(_blendFunc);
  167. }
  168. }
  169. void BoneNode::removeFromSkinList(SkinNode* skin)
  170. {
  171. _boneSkins.eraseObject(skin);
  172. }
  173. void BoneNode::displaySkin(SkinNode* skin, bool hideOthers)
  174. {
  175. for (auto boneskin : _boneSkins)
  176. {
  177. if (boneskin == skin)
  178. {
  179. boneskin->setVisible(true);
  180. }
  181. else if (hideOthers)
  182. {
  183. boneskin->setVisible(false);
  184. }
  185. }
  186. }
  187. void BoneNode::displaySkin(const std::string &skinName, bool hideOthers)
  188. {
  189. for (auto &skin : _boneSkins)
  190. {
  191. if (skinName == skin->getName())
  192. {
  193. skin->setVisible(true);
  194. }
  195. else if (hideOthers)
  196. {
  197. skin->setVisible(false);
  198. }
  199. }
  200. }
  201. cocos2d::Vector<SkinNode*> BoneNode::getVisibleSkins() const
  202. {
  203. cocos2d::Vector<SkinNode*> displayingSkins;
  204. for (const auto &boneskin : _boneSkins)
  205. {
  206. if (boneskin->isVisible())
  207. {
  208. displayingSkins.pushBack(boneskin);
  209. }
  210. }
  211. return displayingSkins;
  212. }
  213. cocos2d::Rect BoneNode::getBoundingBox() const
  214. {
  215. cocos2d::Rect boundingBox = getVisibleSkinsRect();
  216. return RectApplyAffineTransform(boundingBox, this->getNodeToParentAffineTransform());
  217. }
  218. cocos2d::Rect BoneNode::getVisibleSkinsRect() const
  219. {
  220. float minx, miny, maxx, maxy = 0;
  221. minx = miny = maxx = maxy;
  222. bool first = true;
  223. cocos2d::Rect displayRect = cocos2d::Rect(0, 0, 0, 0);
  224. if (_isRackShow && _rootSkeleton != nullptr && _rootSkeleton->_isRackShow)
  225. {
  226. maxx = _rackLength;
  227. maxy = _rackWidth;
  228. first = false;
  229. }
  230. for (const auto& skin : _boneSkins)
  231. {
  232. cocos2d::Rect r = skin->getBoundingBox();
  233. if (!skin->isVisible() || r.equals(cocos2d::Rect::ZERO))
  234. continue;
  235. if (first)
  236. {
  237. minx = r.getMinX();
  238. miny = r.getMinY();
  239. maxx = r.getMaxX();
  240. maxy = r.getMaxY();
  241. first = false;
  242. }
  243. else
  244. {
  245. minx = MIN(r.getMinX(), minx);
  246. miny = MIN(r.getMinY(), miny);
  247. maxx = MAX(r.getMaxX(), maxx);
  248. maxy = MAX(r.getMaxY(), maxy);
  249. }
  250. displayRect.setRect(minx, miny, maxx - minx, maxy - miny);
  251. }
  252. return displayRect;
  253. }
  254. void BoneNode::setBlendFunc(const cocos2d::BlendFunc& blendFunc)
  255. {
  256. if (_blendFunc != blendFunc)
  257. {
  258. _blendFunc = blendFunc;
  259. for (auto & skin : _boneSkins)
  260. {
  261. auto blendSkin = dynamic_cast<BlendProtocol*>(skin);
  262. if (nullptr != blendSkin)
  263. {
  264. blendSkin->setBlendFunc(_blendFunc);
  265. }
  266. }
  267. }
  268. }
  269. void BoneNode::setDebugDrawLength(float length)
  270. {
  271. _rackLength = length;
  272. updateVertices();
  273. }
  274. void BoneNode::setDebugDrawWidth(float width)
  275. {
  276. _rackWidth = width;
  277. updateVertices();
  278. }
  279. void BoneNode::setDebugDrawEnabled(bool isDebugDraw)
  280. {
  281. if (_isRackShow == isDebugDraw)
  282. return;
  283. _isRackShow = isDebugDraw;
  284. }
  285. void BoneNode::setDebugDrawColor(const cocos2d::Color4F &color)
  286. {
  287. _rackColor = color;
  288. updateColor();
  289. }
  290. void BoneNode::visit(cocos2d::Renderer *renderer, const cocos2d::Mat4& parentTransform, uint32_t parentFlags)
  291. {
  292. // quick return if not visible. children won't be drawn.
  293. if (!_visible)
  294. {
  295. return;
  296. }
  297. uint32_t flags = processParentFlags(parentTransform, parentFlags);
  298. // IMPORTANT:
  299. // To ease the migration to v3.0, we still support the Mat4 stack,
  300. // but it is deprecated and your code should not rely on it
  301. _director->pushMatrix(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  302. _director->loadMatrix(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
  303. bool visibleByCamera = isVisitableByVisitingCamera();
  304. bool isdebugdraw = visibleByCamera && _isRackShow && nullptr == _rootSkeleton;
  305. int i = 0;
  306. if (!_children.empty())
  307. {
  308. sortAllChildren();
  309. // draw children zOrder < 0
  310. for (; i < _children.size(); i++)
  311. {
  312. auto node = _children.at(i);
  313. if (_rootSkeleton != nullptr && _boneSkins.contains(node)) // skip skin when bone is in a skeleton
  314. continue;
  315. if (node && node->getLocalZOrder() < 0)
  316. node->visit(renderer, _modelViewTransform, flags);
  317. else
  318. break;
  319. }
  320. // self draw
  321. if (isdebugdraw)
  322. this->draw(renderer, _modelViewTransform, flags);
  323. for (auto it = _children.cbegin() + i; it != _children.cend(); ++it)
  324. {
  325. auto node = (*it);
  326. if (_rootSkeleton != nullptr && _boneSkins.contains(node)) // skip skin when bone is in a skeleton
  327. continue;
  328. node->visit(renderer, _modelViewTransform, flags);
  329. }
  330. }
  331. else if (isdebugdraw)
  332. {
  333. this->draw(renderer, _modelViewTransform, flags);
  334. }
  335. _director->popMatrix(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  336. // FIX ME: Why need to set _orderOfArrival to 0??
  337. // Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
  338. // reset for next frame
  339. // _orderOfArrival = 0;
  340. }
  341. void BoneNode::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags)
  342. {
  343. _customCommand.init(_globalZOrder, transform, flags);
  344. _customCommand.func = CC_CALLBACK_0(BoneNode::onDraw, this, transform, flags);
  345. renderer->addCommand(&_customCommand);
  346. for (int i = 0; i < 4; ++i)
  347. {
  348. cocos2d::Vec4 pos;
  349. pos.x = _squareVertices[i].x; pos.y = _squareVertices[i].y; pos.z = _positionZ;
  350. pos.w = 1;
  351. _modelViewTransform.transformVector(&pos);
  352. _noMVPVertices[i] = cocos2d::Vec3(pos.x, pos.y, pos.z) / pos.w;
  353. }
  354. }
  355. BoneNode::~BoneNode()
  356. {
  357. }
  358. bool BoneNode::init()
  359. {
  360. _rackLength = 50;
  361. _rackWidth = 20;
  362. updateVertices();
  363. updateColor();
  364. setGLProgramState(cocos2d::GLProgramState::getOrCreateWithGLProgramName(cocos2d::GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP));
  365. return true;
  366. }
  367. void BoneNode::updateVertices()
  368. {
  369. if (_rackLength != _squareVertices[2].x - _anchorPointInPoints.x || _squareVertices[3].y != _rackWidth / 2 - _anchorPointInPoints.y)
  370. {
  371. _squareVertices[1].x = _squareVertices[1].y = _squareVertices[3].y = .0f;
  372. _squareVertices[0].x = _squareVertices[2].x = _rackLength * .1f;
  373. _squareVertices[2].y = _rackWidth * .5f;
  374. _squareVertices[0].y = -_squareVertices[2].y;
  375. _squareVertices[3].x = _rackLength;
  376. for (int i = 0; i < 4; i++)
  377. {
  378. _squareVertices[i] += _anchorPointInPoints;
  379. }
  380. _transformUpdated = _transformDirty = _inverseDirty = _contentSizeDirty = true;
  381. }
  382. }
  383. void BoneNode::updateColor()
  384. {
  385. for (unsigned int i = 0; i < 4; i++)
  386. {
  387. _squareColors[i] = _rackColor;
  388. }
  389. _transformUpdated = _transformDirty = _inverseDirty = _contentSizeDirty = true;
  390. }
  391. void BoneNode::updateDisplayedColor(const cocos2d::Color3B& /*parentColor*/)
  392. {
  393. if (_cascadeColorEnabled)
  394. {
  395. for (const auto &child : _boneSkins)
  396. {
  397. child->updateDisplayedColor(_displayedColor);
  398. }
  399. }
  400. }
  401. void BoneNode::updateDisplayedOpacity(GLubyte /*parentOpacity*/)
  402. {
  403. if (_cascadeOpacityEnabled)
  404. {
  405. for (const auto& child : _boneSkins)
  406. {
  407. child->updateDisplayedOpacity(_displayedOpacity);
  408. }
  409. }
  410. }
  411. void BoneNode::disableCascadeOpacity()
  412. {
  413. for (const auto& child : _boneSkins)
  414. {
  415. child->updateDisplayedOpacity(255);
  416. }
  417. }
  418. void BoneNode::disableCascadeColor()
  419. {
  420. for (const auto& child : _boneSkins)
  421. {
  422. child->updateDisplayedColor(cocos2d::Color3B::WHITE);
  423. }
  424. }
  425. void BoneNode::onDraw(const cocos2d::Mat4 &transform, uint32_t /*flags*/)
  426. {
  427. getGLProgram()->use();
  428. getGLProgram()->setUniformsForBuiltins(transform);
  429. cocos2d::GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION | cocos2d::GL::VERTEX_ATTRIB_FLAG_COLOR);
  430. glBindBuffer(GL_ARRAY_BUFFER, 0);
  431. glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _noMVPVertices);
  432. glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, _squareColors);
  433. cocos2d::GL::blendFunc(_blendFunc.src, _blendFunc.dst);
  434. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  435. #ifdef CC_STUDIO_ENABLED_VIEW
  436. glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _noMVPVertices);
  437. glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, _squareColors);
  438. glEnable(GL_LINE_SMOOTH);
  439. glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
  440. glDrawArrays(GL_LINE_LOOP, 0, 4);
  441. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 8);
  442. #else
  443. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
  444. #endif //CC_STUDIO_ENABLED_VIEW
  445. }
  446. cocos2d::Vector<BoneNode*> BoneNode::getAllSubBones() const
  447. {
  448. cocos2d::Vector<BoneNode*> allBones;
  449. std::stack<BoneNode*> boneStack; // for avoid recursive
  450. for (const auto& bone : _childBones)
  451. {
  452. boneStack.push(bone);
  453. }
  454. while (boneStack.size() > 0)
  455. {
  456. auto top = boneStack.top();
  457. allBones.pushBack(top);
  458. boneStack.pop();
  459. auto topchildren = top->getChildBones();
  460. for (const auto& childbone : topchildren)
  461. {
  462. boneStack.push(childbone);
  463. }
  464. }
  465. return allBones;
  466. }
  467. cocos2d::Vector<SkinNode*> BoneNode::getAllSubSkins() const
  468. {
  469. auto allbones = getAllSubBones();
  470. cocos2d::Vector<SkinNode*> allskins;
  471. for (const auto& bone : allbones)
  472. {
  473. for (const auto& skin : bone->getSkins())
  474. {
  475. allskins.pushBack(skin);
  476. }
  477. }
  478. return allskins;
  479. }
  480. void BoneNode::sortAllChildren()
  481. {
  482. if (_reorderChildDirty)
  483. {
  484. sortNodes(_childBones);
  485. sortNodes(_boneSkins);
  486. Node::sortAllChildren();
  487. }
  488. }
  489. SkeletonNode* BoneNode::getRootSkeletonNode() const
  490. {
  491. return _rootSkeleton;
  492. }
  493. #ifdef CC_STUDIO_ENABLED_VIEW
  494. bool BoneNode::isPointOnRack(const cocos2d::Vec2& bonePoint)
  495. {
  496. if (bonePoint.x >= 0.0f && bonePoint.y >= _squareVertices[0].y
  497. && bonePoint.x <= _rackLength &&
  498. bonePoint.y <= _squareVertices[2].y)
  499. {
  500. if (_rackLength != 0.0f && _rackWidth != 0.0f)
  501. {
  502. float a1 = (_squareVertices[2].y - _squareVertices[3].y) / (_squareVertices[3].x - _squareVertices[0].x);
  503. float a2 = (_squareVertices[2].y - _squareVertices[3].y) / (_squareVertices[0].x - _squareVertices[1].x);
  504. float b1 = a1 * _squareVertices[3].x;
  505. float y1 = bonePoint.y - _squareVertices[1].y;
  506. if (y1 >= a1 * bonePoint.x - b1 &&
  507. y1 <= a2 * bonePoint.x &&
  508. y1 >= -a2 * bonePoint.x &&
  509. y1 <= -a1 * bonePoint.x + b1)
  510. return true;
  511. }
  512. }
  513. return false;
  514. }
  515. #endif //CC_STUDIO_ENABLED_VIEW
  516. void BoneNode::batchBoneDrawToSkeleton(BoneNode* bone) const
  517. {
  518. bool visibleByCamera = bone->isVisitableByVisitingCamera();
  519. if (!visibleByCamera)
  520. {
  521. return;
  522. }
  523. cocos2d::Vec3 vpos[4];
  524. for (int i = 0; i < 4; i++)
  525. {
  526. cocos2d::Vec4 pos;
  527. pos.x = bone->_squareVertices[i].x; pos.y = bone->_squareVertices[i].y; pos.z = bone->_positionZ;
  528. pos.w = 1;
  529. bone->_modelViewTransform.transformVector(&pos); // call after visit
  530. vpos[i] = cocos2d::Vec3(pos.x, pos.y, pos.z) / pos.w;
  531. }
  532. int count = bone->_rootSkeleton->_batchedVeticesCount;
  533. if (count + 8 >(int)(bone->_rootSkeleton->_batchedBoneVetices.size()))
  534. {
  535. bone->_rootSkeleton->_batchedBoneVetices.resize(count + 100);
  536. bone->_rootSkeleton->_batchedBoneColors.resize(count + 100);
  537. }
  538. for (int i = 0; i < 4; i++)
  539. {
  540. bone->_rootSkeleton->_batchedBoneVetices[count + i] = vpos[i];
  541. bone->_rootSkeleton->_batchedBoneColors[count + i] = bone->_squareColors[i];
  542. }
  543. bone->_rootSkeleton->_batchedVeticesCount += 4;
  544. }
  545. // call after self visit
  546. void BoneNode::visitSkins(cocos2d::Renderer* renderer, BoneNode* bone) const
  547. {
  548. // quick return if not visible. children won't be drawn.
  549. if (!bone->_visible)
  550. {
  551. return;
  552. }
  553. // IMPORTANT:
  554. // To ease the migration to v3.0, we still support the Mat4 stack,
  555. // but it is deprecated and your code should not rely on it
  556. _director->pushMatrix(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  557. _director->loadMatrix(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, bone->_modelViewTransform);
  558. if (!bone->_boneSkins.empty())
  559. {
  560. bone->sortAllChildren();
  561. for (auto it = bone->_boneSkins.cbegin(); it != bone->_boneSkins.cend(); ++it)
  562. (*it)->visit(renderer, bone->_modelViewTransform, true);
  563. }
  564. _director->popMatrix(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  565. // FIX ME: Why need to set _orderOfArrival to 0??
  566. // Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
  567. // reset for next frame
  568. // _orderOfArrival = 0;
  569. }
  570. void BoneNode::setRootSkeleton(BoneNode* bone, SkeletonNode* skeleton) const
  571. {
  572. bone->_rootSkeleton = skeleton;
  573. }
  574. void BoneNode::setLocalZOrder(int localZOrder)
  575. {
  576. Node::setLocalZOrder(localZOrder);
  577. if (_rootSkeleton != nullptr)
  578. _rootSkeleton->_subBonesOrderDirty = true;
  579. }
  580. void BoneNode::setName(const std::string& name)
  581. {
  582. auto oldname = getName();
  583. Node::setName(name);
  584. if (_rootSkeleton != nullptr)
  585. {
  586. auto oiter = _rootSkeleton->_subBonesMap.find(oldname);
  587. auto niter = _rootSkeleton->_subBonesMap.find(name);
  588. if (oiter != _rootSkeleton->_subBonesMap.end() &&
  589. niter == _rootSkeleton->_subBonesMap.end())
  590. {
  591. auto node = oiter->second;
  592. _rootSkeleton->_subBonesMap.erase(oiter);
  593. _rootSkeleton->_subBonesMap.insert(name, node);
  594. }
  595. }
  596. }
  597. void BoneNode::addToChildrenListHelper(Node * child)
  598. {
  599. BoneNode* bone = dynamic_cast<BoneNode*>(child);
  600. if (nullptr != bone)
  601. {
  602. addToBoneList(bone);
  603. }
  604. else
  605. {
  606. SkinNode* skin = dynamic_cast<SkinNode*>(child);
  607. if (nullptr != skin)
  608. {
  609. addToSkinList(skin);
  610. }
  611. }
  612. }
  613. void BoneNode::removeFromChildrenListHelper(Node * child)
  614. {
  615. BoneNode* bone = dynamic_cast<BoneNode*>(child);
  616. if (nullptr != bone)
  617. {
  618. removeFromBoneList(bone);
  619. }
  620. else
  621. {
  622. SkinNode* skin = dynamic_cast<SkinNode*>(child);
  623. if (nullptr != skin)
  624. {
  625. removeFromSkinList(skin);
  626. }
  627. }
  628. }
  629. void BoneNode::setVisible(bool visible)
  630. {
  631. if (_visible == visible)
  632. return;
  633. Node::setVisible(visible);
  634. if (_rootSkeleton != nullptr)
  635. {
  636. _rootSkeleton->_subBonesDirty = true;
  637. _rootSkeleton->_subBonesOrderDirty = true;
  638. }
  639. }
  640. void BoneNode::setContentSize(const cocos2d::Size& contentSize)
  641. {
  642. Node::setContentSize(contentSize);
  643. updateVertices();
  644. }
  645. void BoneNode::setAnchorPoint(const cocos2d::Vec2& anchorPoint)
  646. {
  647. Node::setAnchorPoint(anchorPoint);
  648. updateVertices();
  649. }
  650. NS_TIMELINE_END