CCBone.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 "editor-support/cocostudio/CCBone.h"
  21. #include "editor-support/cocostudio/CCArmature.h"
  22. #include "editor-support/cocostudio/CCUtilMath.h"
  23. #include "editor-support/cocostudio/CCArmatureDataManager.h"
  24. #include "editor-support/cocostudio/CCTransformHelp.h"
  25. #include "editor-support/cocostudio/CCDisplayManager.h"
  26. using namespace cocos2d;
  27. namespace cocostudio {
  28. Bone *Bone::create()
  29. {
  30. Bone *pBone = new (std::nothrow) Bone();
  31. if (pBone && pBone->init())
  32. {
  33. pBone->autorelease();
  34. return pBone;
  35. }
  36. CC_SAFE_DELETE(pBone);
  37. return nullptr;
  38. }
  39. Bone *Bone::create(const std::string& name)
  40. {
  41. Bone *pBone = new (std::nothrow) Bone();
  42. if (pBone && pBone->init(name))
  43. {
  44. pBone->autorelease();
  45. return pBone;
  46. }
  47. CC_SAFE_DELETE(pBone);
  48. return nullptr;
  49. }
  50. Bone::Bone()
  51. {
  52. _tweenData = nullptr;
  53. _parentBone = nullptr;
  54. _armature = nullptr;
  55. _childArmature = nullptr;
  56. _boneData = nullptr;
  57. _tween = nullptr;
  58. _displayManager = nullptr;
  59. _ignoreMovementBoneData = false;
  60. _worldTransform = Mat4::IDENTITY;
  61. _boneTransformDirty = true;
  62. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  63. _blendDirty = false;
  64. _worldInfo = nullptr;
  65. _armatureParentBone = nullptr;
  66. _dataVersion = 0;
  67. }
  68. Bone::~Bone(void)
  69. {
  70. CC_SAFE_DELETE(_tweenData);
  71. CC_SAFE_DELETE(_tween);
  72. CC_SAFE_DELETE(_displayManager);
  73. CC_SAFE_DELETE(_worldInfo);
  74. CC_SAFE_RELEASE_NULL(_boneData);
  75. CC_SAFE_RELEASE(_childArmature);
  76. }
  77. bool Bone::init()
  78. {
  79. return Bone::init(nullptr);
  80. }
  81. bool Bone::init(const std::string& name)
  82. {
  83. bool bRet = false;
  84. do
  85. {
  86. _name = name;
  87. CC_SAFE_DELETE(_tweenData);
  88. _tweenData = new (std::nothrow) FrameData();
  89. CC_SAFE_DELETE(_tween);
  90. _tween = new (std::nothrow) Tween();
  91. _tween->init(this);
  92. CC_SAFE_DELETE(_displayManager);
  93. _displayManager = new (std::nothrow) DisplayManager();
  94. _displayManager->init(this);
  95. CC_SAFE_DELETE(_worldInfo);
  96. _worldInfo = new (std::nothrow) BaseData();
  97. CC_SAFE_DELETE(_boneData);
  98. _boneData = new (std::nothrow) BoneData();
  99. bRet = true;
  100. }
  101. while (0);
  102. return bRet;
  103. }
  104. void Bone::setBoneData(BoneData *boneData)
  105. {
  106. CCASSERT(nullptr != boneData, "_boneData must not be nullptr");
  107. if (_boneData != boneData)
  108. {
  109. CC_SAFE_RETAIN(boneData);
  110. CC_SAFE_RELEASE(_boneData);
  111. _boneData = boneData;
  112. }
  113. _name = _boneData->name;
  114. _setLocalZOrder(_boneData->zOrder);
  115. _displayManager->initDisplayList(boneData);
  116. }
  117. BoneData *Bone::getBoneData() const
  118. {
  119. return _boneData;
  120. }
  121. void Bone::setArmature(Armature *armature)
  122. {
  123. _armature = armature;
  124. if (_armature)
  125. {
  126. _tween->setAnimation(_armature->getAnimation());
  127. _dataVersion = _armature->getArmatureData()->dataVersion;
  128. _armatureParentBone = _armature->getParentBone();
  129. }
  130. else
  131. {
  132. _armatureParentBone = nullptr;
  133. }
  134. }
  135. Armature *Bone::getArmature() const
  136. {
  137. return _armature;
  138. }
  139. void Bone::update(float delta)
  140. {
  141. if (_parentBone)
  142. _boneTransformDirty = _boneTransformDirty || _parentBone->isTransformDirty();
  143. if (_armatureParentBone && !_boneTransformDirty)
  144. {
  145. _boneTransformDirty = _armatureParentBone->isTransformDirty();
  146. }
  147. if (_boneTransformDirty)
  148. {
  149. _worldInfo->copy(_tweenData);
  150. if (_dataVersion >= VERSION_COMBINED)
  151. {
  152. TransformHelp::nodeConcat(*_worldInfo, *_boneData);
  153. _worldInfo->scaleX -= 1;
  154. _worldInfo->scaleY -= 1;
  155. }
  156. _worldInfo->x = _worldInfo->x + _position.x;
  157. _worldInfo->y = _worldInfo->y + _position.y;
  158. _worldInfo->scaleX = _worldInfo->scaleX * _scaleX;
  159. _worldInfo->scaleY = _worldInfo->scaleY * _scaleY;
  160. _worldInfo->skewX = _worldInfo->skewX + _skewX + CC_DEGREES_TO_RADIANS(_rotationZ_X);
  161. _worldInfo->skewY = _worldInfo->skewY + _skewY - CC_DEGREES_TO_RADIANS(_rotationZ_Y);
  162. if(_parentBone)
  163. {
  164. applyParentTransform(_parentBone);
  165. }
  166. else
  167. {
  168. if (_armatureParentBone)
  169. {
  170. applyParentTransform(_armatureParentBone);
  171. }
  172. }
  173. TransformHelp::nodeToMatrix(*_worldInfo, _worldTransform);
  174. if (_armatureParentBone)
  175. {
  176. _worldTransform = TransformConcat(_worldTransform, _armature->getNodeToParentTransform());
  177. }
  178. }
  179. DisplayFactory::updateDisplay(this, delta, _boneTransformDirty || _armature->getArmatureTransformDirty());
  180. for(const auto &obj: _children) {
  181. Bone *childBone = static_cast<Bone*>(obj);
  182. childBone->update(delta);
  183. }
  184. _boneTransformDirty = false;
  185. }
  186. void Bone::applyParentTransform(Bone *parent)
  187. {
  188. float x = _worldInfo->x;
  189. float y = _worldInfo->y;
  190. _worldInfo->x = x * parent->_worldTransform.m[0] + y * parent->_worldTransform.m[4] + parent->_worldInfo->x;
  191. _worldInfo->y = x * parent->_worldTransform.m[1] + y * parent->_worldTransform.m[5] + parent->_worldInfo->y;
  192. _worldInfo->scaleX = _worldInfo->scaleX * parent->_worldInfo->scaleX;
  193. _worldInfo->scaleY = _worldInfo->scaleY * parent->_worldInfo->scaleY;
  194. _worldInfo->skewX = _worldInfo->skewX + parent->_worldInfo->skewX;
  195. _worldInfo->skewY = _worldInfo->skewY + parent->_worldInfo->skewY;
  196. }
  197. void Bone::setBlendFunc(const BlendFunc& blendFunc)
  198. {
  199. if (_blendFunc.src != blendFunc.src || _blendFunc.dst != blendFunc.dst)
  200. {
  201. _blendFunc = blendFunc;
  202. _blendDirty = true;
  203. }
  204. }
  205. void Bone::updateDisplayedColor(const Color3B &parentColor)
  206. {
  207. #ifdef CC_STUDIO_ENABLED_VIEW
  208. _realColor = Color3B(255, 255, 255);
  209. #endif // CC_STUDIO_ENABLED_VIEW
  210. Node::updateDisplayedColor(parentColor);
  211. }
  212. void Bone::updateDisplayedOpacity(GLubyte parentOpacity)
  213. {
  214. #ifdef CC_STUDIO_ENABLED_VIEW
  215. _realOpacity = 255;
  216. #endif // CC_STUDIO_ENABLED_VIEW
  217. Node::updateDisplayedOpacity(parentOpacity);
  218. }
  219. void Bone::updateColor()
  220. {
  221. Node *display = _displayManager->getDisplayRenderNode();
  222. if(display != nullptr)
  223. {
  224. display->setColor(Color3B(_displayedColor.r * _tweenData->r / 255, _displayedColor.g * _tweenData->g / 255, _displayedColor.b * _tweenData->b / 255));
  225. display->setOpacity(_displayedOpacity * _tweenData->a / 255);
  226. }
  227. }
  228. void Bone::updateZOrder()
  229. {
  230. if (_dataVersion >= VERSION_COMBINED)
  231. {
  232. int zorder = _tweenData->zOrder + _boneData->zOrder;
  233. setLocalZOrder(zorder);
  234. }
  235. else
  236. {
  237. setLocalZOrder(_tweenData->zOrder);
  238. }
  239. }
  240. void Bone::addChildBone(Bone *child)
  241. {
  242. CCASSERT( nullptr != child, "Argument must be non-nil");
  243. CCASSERT( nullptr == child->_parentBone, "child already added. It can't be added again");
  244. if(_children.empty())
  245. {
  246. _children.reserve(4);
  247. }
  248. if (_children.getIndex(child) == CC_INVALID_INDEX)
  249. {
  250. _children.pushBack(child);
  251. child->setParentBone(this);
  252. }
  253. }
  254. void Bone::removeChildBone(Bone *bone, bool recursion)
  255. {
  256. if (!_children.empty() && _children.getIndex(bone) != CC_INVALID_INDEX )
  257. {
  258. if(recursion)
  259. {
  260. auto ccbones = bone->_children;
  261. for(auto& object : ccbones)
  262. {
  263. Bone *ccBone = static_cast<Bone*>(object);
  264. bone->removeChildBone(ccBone, recursion);
  265. }
  266. }
  267. bone->setParentBone(nullptr);
  268. bone->getDisplayManager()->setCurrentDecorativeDisplay(nullptr);
  269. _children.eraseObject(bone);
  270. }
  271. }
  272. void Bone::removeFromParent(bool recursion)
  273. {
  274. if (nullptr != _parentBone)
  275. {
  276. _parentBone->removeChildBone(this, recursion);
  277. }
  278. }
  279. void Bone::setParentBone(Bone *parent)
  280. {
  281. _parentBone = parent;
  282. }
  283. Bone *Bone::getParentBone()
  284. {
  285. return _parentBone;
  286. }
  287. void Bone::setChildArmature(Armature *armature)
  288. {
  289. if (_childArmature != armature)
  290. {
  291. if (armature == nullptr && _childArmature)
  292. {
  293. _childArmature->setParentBone(nullptr);
  294. }
  295. CC_SAFE_RETAIN(armature);
  296. CC_SAFE_RELEASE(_childArmature);
  297. _childArmature = armature;
  298. }
  299. }
  300. Armature *Bone::getChildArmature() const
  301. {
  302. return _childArmature;
  303. }
  304. Tween *Bone::getTween()
  305. {
  306. return _tween;
  307. }
  308. void Bone::setLocalZOrder(int zOrder)
  309. {
  310. if (getLocalZOrder() != zOrder)
  311. Node::setLocalZOrder(zOrder);
  312. }
  313. Mat4 Bone::getNodeToArmatureTransform() const
  314. {
  315. return _worldTransform;
  316. }
  317. Mat4 Bone::getNodeToWorldTransform() const
  318. {
  319. return TransformConcat(_worldTransform, _armature->getNodeToWorldTransform());
  320. }
  321. Node *Bone::getDisplayRenderNode()
  322. {
  323. return _displayManager->getDisplayRenderNode();
  324. }
  325. DisplayType Bone::getDisplayRenderNodeType()
  326. {
  327. return _displayManager->getDisplayRenderNodeType();
  328. }
  329. void Bone::addDisplay(DisplayData *displayData, int index)
  330. {
  331. _displayManager->addDisplay(displayData, index);
  332. }
  333. void Bone::addDisplay(Node *display, int index)
  334. {
  335. _displayManager->addDisplay(display, index);
  336. }
  337. void Bone::removeDisplay(int index)
  338. {
  339. _displayManager->removeDisplay(index);
  340. }
  341. void Bone::changeDisplayByIndex(int index, bool force)
  342. {
  343. changeDisplayWithIndex(index, force);
  344. }
  345. void Bone::changeDisplayByName(const std::string &name, bool force)
  346. {
  347. changeDisplayWithName(name, force);
  348. }
  349. void Bone::changeDisplayWithIndex(int index, bool force)
  350. {
  351. _displayManager->changeDisplayWithIndex(index, force);
  352. }
  353. void Bone::changeDisplayWithName(const std::string& name, bool force)
  354. {
  355. _displayManager->changeDisplayWithName(name, force);
  356. }
  357. ColliderDetector* Bone::getColliderDetector() const
  358. {
  359. if (DecorativeDisplay *decoDisplay = _displayManager->getCurrentDecorativeDisplay())
  360. {
  361. if (ColliderDetector *detector = decoDisplay->getColliderDetector())
  362. {
  363. return detector;
  364. }
  365. }
  366. return nullptr;
  367. }
  368. #if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
  369. void Bone::setColliderFilter(ColliderFilter *filter)
  370. {
  371. auto array = _displayManager->getDecorativeDisplayList();
  372. for(auto& object : array)
  373. {
  374. DecorativeDisplay *decoDisplay = static_cast<DecorativeDisplay *>(object);
  375. if (ColliderDetector *detector = decoDisplay->getColliderDetector())
  376. {
  377. detector->setColliderFilter(filter);
  378. }
  379. }
  380. }
  381. ColliderFilter *Bone::getColliderFilter()
  382. {
  383. if (DecorativeDisplay *decoDisplay = _displayManager->getCurrentDecorativeDisplay())
  384. {
  385. if (ColliderDetector *detector = decoDisplay->getColliderDetector())
  386. {
  387. return detector->getColliderFilter();
  388. }
  389. }
  390. return nullptr;
  391. }
  392. #endif
  393. }