Skeleton.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #ifdef SPINE_UE4
  30. #include "SpinePluginPrivatePCH.h"
  31. #endif
  32. #include <spine/Skeleton.h>
  33. #include <spine/SkeletonData.h>
  34. #include <spine/Bone.h>
  35. #include <spine/Slot.h>
  36. #include <spine/IkConstraint.h>
  37. #include <spine/PathConstraint.h>
  38. #include <spine/TransformConstraint.h>
  39. #include <spine/Skin.h>
  40. #include <spine/Attachment.h>
  41. #include <spine/BoneData.h>
  42. #include <spine/SlotData.h>
  43. #include <spine/IkConstraintData.h>
  44. #include <spine/TransformConstraintData.h>
  45. #include <spine/PathConstraintData.h>
  46. #include <spine/RegionAttachment.h>
  47. #include <spine/MeshAttachment.h>
  48. #include <spine/PathAttachment.h>
  49. #include <spine/ContainerUtil.h>
  50. #include <float.h>
  51. using namespace spine;
  52. Skeleton::Skeleton(SkeletonData *skeletonData) :
  53. _data(skeletonData),
  54. _skin(NULL),
  55. _color(1, 1, 1, 1),
  56. _time(0),
  57. _scaleX(1),
  58. _scaleY(1),
  59. _x(0),
  60. _y(0) {
  61. _bones.ensureCapacity(_data->getBones().size());
  62. for (size_t i = 0; i < _data->getBones().size(); ++i) {
  63. BoneData *data = _data->getBones()[i];
  64. Bone *bone;
  65. if (data->getParent() == NULL) {
  66. bone = new(__FILE__, __LINE__) Bone(*data, *this, NULL);
  67. } else {
  68. Bone *parent = _bones[data->getParent()->getIndex()];
  69. bone = new(__FILE__, __LINE__) Bone(*data, *this, parent);
  70. parent->getChildren().add(bone);
  71. }
  72. _bones.add(bone);
  73. }
  74. _slots.ensureCapacity(_data->getSlots().size());
  75. _drawOrder.ensureCapacity(_data->getSlots().size());
  76. for (size_t i = 0; i < _data->getSlots().size(); ++i) {
  77. SlotData *data = _data->getSlots()[i];
  78. Bone *bone = _bones[data->getBoneData().getIndex()];
  79. Slot *slot = new(__FILE__, __LINE__) Slot(*data, *bone);
  80. _slots.add(slot);
  81. _drawOrder.add(slot);
  82. }
  83. _ikConstraints.ensureCapacity(_data->getIkConstraints().size());
  84. for (size_t i = 0; i < _data->getIkConstraints().size(); ++i) {
  85. IkConstraintData *data = _data->getIkConstraints()[i];
  86. IkConstraint *constraint = new(__FILE__, __LINE__) IkConstraint(*data, *this);
  87. _ikConstraints.add(constraint);
  88. }
  89. _transformConstraints.ensureCapacity(_data->getTransformConstraints().size());
  90. for (size_t i = 0; i < _data->getTransformConstraints().size(); ++i) {
  91. TransformConstraintData *data = _data->getTransformConstraints()[i];
  92. TransformConstraint *constraint = new(__FILE__, __LINE__) TransformConstraint(*data, *this);
  93. _transformConstraints.add(constraint);
  94. }
  95. _pathConstraints.ensureCapacity(_data->getPathConstraints().size());
  96. for (size_t i = 0; i < _data->getPathConstraints().size(); ++i) {
  97. PathConstraintData *data = _data->getPathConstraints()[i];
  98. PathConstraint *constraint = new(__FILE__, __LINE__) PathConstraint(*data, *this);
  99. _pathConstraints.add(constraint);
  100. }
  101. updateCache();
  102. }
  103. Skeleton::~Skeleton() {
  104. ContainerUtil::cleanUpVectorOfPointers(_bones);
  105. ContainerUtil::cleanUpVectorOfPointers(_slots);
  106. ContainerUtil::cleanUpVectorOfPointers(_ikConstraints);
  107. ContainerUtil::cleanUpVectorOfPointers(_transformConstraints);
  108. ContainerUtil::cleanUpVectorOfPointers(_pathConstraints);
  109. }
  110. void Skeleton::updateCache() {
  111. _updateCache.clear();
  112. _updateCacheReset.clear();
  113. for (size_t i = 0, n = _bones.size(); i < n; ++i) {
  114. Bone* bone = _bones[i];
  115. bone->_sorted = bone->_data.isSkinRequired();
  116. bone->_active = !bone->_sorted;
  117. }
  118. if (_skin) {
  119. Vector<BoneData*>& skinBones = _skin->getBones();
  120. for (size_t i = 0, n = skinBones.size(); i < n; i++) {
  121. Bone* bone = _bones[skinBones[i]->getIndex()];
  122. do {
  123. bone->_sorted = false;
  124. bone->_active = true;
  125. bone = bone->_parent;
  126. } while (bone);
  127. }
  128. }
  129. size_t ikCount = _ikConstraints.size();
  130. size_t transformCount = _transformConstraints.size();
  131. size_t pathCount = _pathConstraints.size();
  132. size_t constraintCount = ikCount + transformCount + pathCount;
  133. size_t i = 0;
  134. continue_outer:
  135. for (; i < constraintCount; ++i) {
  136. for (size_t ii = 0; ii < ikCount; ++ii) {
  137. IkConstraint *constraint = _ikConstraints[ii];
  138. if (constraint->getData().getOrder() == i) {
  139. sortIkConstraint(constraint);
  140. i++;
  141. goto continue_outer;
  142. }
  143. }
  144. for (size_t ii = 0; ii < transformCount; ++ii) {
  145. TransformConstraint *constraint = _transformConstraints[ii];
  146. if (constraint->getData().getOrder() == i) {
  147. sortTransformConstraint(constraint);
  148. i++;
  149. goto continue_outer;
  150. }
  151. }
  152. for (size_t ii = 0; ii < pathCount; ++ii) {
  153. PathConstraint *constraint = _pathConstraints[ii];
  154. if (constraint->getData().getOrder() == i) {
  155. sortPathConstraint(constraint);
  156. i++;
  157. goto continue_outer;
  158. }
  159. }
  160. }
  161. size_t n = _bones.size();
  162. for (i = 0; i < n; ++i) {
  163. sortBone(_bones[i]);
  164. }
  165. }
  166. void Skeleton::printUpdateCache() {
  167. for (size_t i = 0; i < _updateCache.size(); i++) {
  168. Updatable *updatable = _updateCache[i];
  169. if (updatable->getRTTI().isExactly(Bone::rtti)) {
  170. printf("bone %s\n", ((Bone *) updatable)->getData().getName().buffer());
  171. } else if (updatable->getRTTI().isExactly(TransformConstraint::rtti)) {
  172. printf("transform constraint %s\n", ((TransformConstraint *) updatable)->getData().getName().buffer());
  173. } else if (updatable->getRTTI().isExactly(IkConstraint::rtti)) {
  174. printf("ik constraint %s\n", ((IkConstraint *) updatable)->getData().getName().buffer());
  175. } else if (updatable->getRTTI().isExactly(PathConstraint::rtti)) {
  176. printf("path constraint %s\n", ((PathConstraint *) updatable)->getData().getName().buffer());
  177. }
  178. }
  179. }
  180. void Skeleton::updateWorldTransform() {
  181. for (size_t i = 0, n = _updateCacheReset.size(); i < n; ++i) {
  182. Bone *boneP = _updateCacheReset[i];
  183. Bone &bone = *boneP;
  184. bone._ax = bone._x;
  185. bone._ay = bone._y;
  186. bone._arotation = bone._rotation;
  187. bone._ascaleX = bone._scaleX;
  188. bone._ascaleY = bone._scaleY;
  189. bone._ashearX = bone._shearX;
  190. bone._ashearY = bone._shearY;
  191. bone._appliedValid = true;
  192. }
  193. for (size_t i = 0, n = _updateCache.size(); i < n; ++i) {
  194. _updateCache[i]->update();
  195. }
  196. }
  197. void Skeleton::setToSetupPose() {
  198. setBonesToSetupPose();
  199. setSlotsToSetupPose();
  200. }
  201. void Skeleton::setBonesToSetupPose() {
  202. for (size_t i = 0, n = _bones.size(); i < n; ++i) {
  203. _bones[i]->setToSetupPose();
  204. }
  205. for (size_t i = 0, n = _ikConstraints.size(); i < n; ++i) {
  206. IkConstraint *constraintP = _ikConstraints[i];
  207. IkConstraint &constraint = *constraintP;
  208. constraint._bendDirection = constraint._data._bendDirection;
  209. constraint._compress = constraint._data._compress;
  210. constraint._stretch = constraint._data._stretch;
  211. constraint._mix = constraint._data._mix;
  212. constraint._softness = constraint._data._softness;
  213. }
  214. for (size_t i = 0, n = _transformConstraints.size(); i < n; ++i) {
  215. TransformConstraint *constraintP = _transformConstraints[i];
  216. TransformConstraint &constraint = *constraintP;
  217. TransformConstraintData &constraintData = constraint._data;
  218. constraint._rotateMix = constraintData._rotateMix;
  219. constraint._translateMix = constraintData._translateMix;
  220. constraint._scaleMix = constraintData._scaleMix;
  221. constraint._shearMix = constraintData._shearMix;
  222. }
  223. for (size_t i = 0, n = _pathConstraints.size(); i < n; ++i) {
  224. PathConstraint *constraintP = _pathConstraints[i];
  225. PathConstraint &constraint = *constraintP;
  226. PathConstraintData &constraintData = constraint._data;
  227. constraint._position = constraintData._position;
  228. constraint._spacing = constraintData._spacing;
  229. constraint._rotateMix = constraintData._rotateMix;
  230. constraint._translateMix = constraintData._translateMix;
  231. }
  232. }
  233. void Skeleton::setSlotsToSetupPose() {
  234. _drawOrder.clear();
  235. for (size_t i = 0, n = _slots.size(); i < n; ++i) {
  236. _drawOrder.add(_slots[i]);
  237. }
  238. for (size_t i = 0, n = _slots.size(); i < n; ++i) {
  239. _slots[i]->setToSetupPose();
  240. }
  241. }
  242. Bone *Skeleton::findBone(const String &boneName) {
  243. return ContainerUtil::findWithDataName(_bones, boneName);
  244. }
  245. int Skeleton::findBoneIndex(const String &boneName) {
  246. return ContainerUtil::findIndexWithDataName(_bones, boneName);
  247. }
  248. Slot *Skeleton::findSlot(const String &slotName) {
  249. return ContainerUtil::findWithDataName(_slots, slotName);
  250. }
  251. int Skeleton::findSlotIndex(const String &slotName) {
  252. return ContainerUtil::findIndexWithDataName(_slots, slotName);
  253. }
  254. void Skeleton::setSkin(const String &skinName) {
  255. Skin *foundSkin = _data->findSkin(skinName);
  256. assert(foundSkin != NULL);
  257. setSkin(foundSkin);
  258. }
  259. void Skeleton::setSkin(Skin *newSkin) {
  260. if (_skin == newSkin) return;
  261. if (newSkin != NULL) {
  262. if (_skin != NULL) {
  263. Skeleton &thisRef = *this;
  264. newSkin->attachAll(thisRef, *_skin);
  265. } else {
  266. for (size_t i = 0, n = _slots.size(); i < n; ++i) {
  267. Slot *slotP = _slots[i];
  268. Slot &slot = *slotP;
  269. const String &name = slot._data.getAttachmentName();
  270. if (name.length() > 0) {
  271. Attachment *attachment = newSkin->getAttachment(i, name);
  272. if (attachment != NULL) {
  273. slot.setAttachment(attachment);
  274. }
  275. }
  276. }
  277. }
  278. }
  279. _skin = newSkin;
  280. updateCache();
  281. }
  282. Attachment *Skeleton::getAttachment(const String &slotName, const String &attachmentName) {
  283. return getAttachment(_data->findSlotIndex(slotName), attachmentName);
  284. }
  285. Attachment *Skeleton::getAttachment(int slotIndex, const String &attachmentName) {
  286. assert(attachmentName.length() > 0);
  287. if (_skin != NULL) {
  288. Attachment *attachment = _skin->getAttachment(slotIndex, attachmentName);
  289. if (attachment != NULL) {
  290. return attachment;
  291. }
  292. }
  293. return _data->getDefaultSkin() != NULL ? _data->getDefaultSkin()->getAttachment(slotIndex, attachmentName) : NULL;
  294. }
  295. void Skeleton::setAttachment(const String &slotName, const String &attachmentName) {
  296. assert(slotName.length() > 0);
  297. for (size_t i = 0, n = _slots.size(); i < n; ++i) {
  298. Slot *slot = _slots[i];
  299. if (slot->_data.getName() == slotName) {
  300. Attachment *attachment = NULL;
  301. if (attachmentName.length() > 0) {
  302. attachment = getAttachment(i, attachmentName);
  303. assert(attachment != NULL);
  304. }
  305. slot->setAttachment(attachment);
  306. return;
  307. }
  308. }
  309. printf("Slot not found: %s", slotName.buffer());
  310. assert(false);
  311. }
  312. IkConstraint *Skeleton::findIkConstraint(const String &constraintName) {
  313. assert(constraintName.length() > 0);
  314. for (size_t i = 0, n = _ikConstraints.size(); i < n; ++i) {
  315. IkConstraint *ikConstraint = _ikConstraints[i];
  316. if (ikConstraint->_data.getName() == constraintName) {
  317. return ikConstraint;
  318. }
  319. }
  320. return NULL;
  321. }
  322. TransformConstraint *Skeleton::findTransformConstraint(const String &constraintName) {
  323. assert(constraintName.length() > 0);
  324. for (size_t i = 0, n = _transformConstraints.size(); i < n; ++i) {
  325. TransformConstraint *transformConstraint = _transformConstraints[i];
  326. if (transformConstraint->_data.getName() == constraintName) {
  327. return transformConstraint;
  328. }
  329. }
  330. return NULL;
  331. }
  332. PathConstraint *Skeleton::findPathConstraint(const String &constraintName) {
  333. assert(constraintName.length() > 0);
  334. for (size_t i = 0, n = _pathConstraints.size(); i < n; ++i) {
  335. PathConstraint *constraint = _pathConstraints[i];
  336. if (constraint->_data.getName() == constraintName) {
  337. return constraint;
  338. }
  339. }
  340. return NULL;
  341. }
  342. void Skeleton::update(float delta) {
  343. _time += delta;
  344. }
  345. void Skeleton::getBounds(float &outX, float &outY, float &outWidth, float &outHeight, Vector<float> &outVertexBuffer) {
  346. float minX = FLT_MAX;
  347. float minY = FLT_MAX;
  348. float maxX = FLT_MIN;
  349. float maxY = FLT_MIN;
  350. for (size_t i = 0; i < _drawOrder.size(); ++i) {
  351. Slot *slot = _drawOrder[i];
  352. if (!slot->_bone._active) continue;
  353. size_t verticesLength = 0;
  354. Attachment *attachment = slot->getAttachment();
  355. if (attachment != NULL && attachment->getRTTI().instanceOf(RegionAttachment::rtti)) {
  356. RegionAttachment *regionAttachment = static_cast<RegionAttachment *>(attachment);
  357. verticesLength = 8;
  358. if (outVertexBuffer.size() < 8) {
  359. outVertexBuffer.setSize(8, 0);
  360. }
  361. regionAttachment->computeWorldVertices(slot->getBone(), outVertexBuffer, 0);
  362. } else if (attachment != NULL && attachment->getRTTI().instanceOf(MeshAttachment::rtti)) {
  363. MeshAttachment *mesh = static_cast<MeshAttachment *>(attachment);
  364. verticesLength = mesh->getWorldVerticesLength();
  365. if (outVertexBuffer.size() < verticesLength) {
  366. outVertexBuffer.setSize(verticesLength, 0);
  367. }
  368. mesh->computeWorldVertices(*slot, 0, verticesLength, outVertexBuffer, 0);
  369. }
  370. for (size_t ii = 0; ii < verticesLength; ii += 2) {
  371. float vx = outVertexBuffer[ii];
  372. float vy = outVertexBuffer[ii + 1];
  373. minX = MathUtil::min(minX, vx);
  374. minY = MathUtil::min(minY, vy);
  375. maxX = MathUtil::max(maxX, vx);
  376. maxY = MathUtil::max(maxY, vy);
  377. }
  378. }
  379. outX = minX;
  380. outY = minY;
  381. outWidth = maxX - minX;
  382. outHeight = maxY - minY;
  383. }
  384. Bone *Skeleton::getRootBone() {
  385. return _bones.size() == 0 ? NULL : _bones[0];
  386. }
  387. SkeletonData *Skeleton::getData() {
  388. return _data;
  389. }
  390. Vector<Bone *> &Skeleton::getBones() {
  391. return _bones;
  392. }
  393. Vector<Updatable *> &Skeleton::getUpdateCacheList() {
  394. return _updateCache;
  395. }
  396. Vector<Slot *> &Skeleton::getSlots() {
  397. return _slots;
  398. }
  399. Vector<Slot *> &Skeleton::getDrawOrder() {
  400. return _drawOrder;
  401. }
  402. Vector<IkConstraint *> &Skeleton::getIkConstraints() {
  403. return _ikConstraints;
  404. }
  405. Vector<PathConstraint *> &Skeleton::getPathConstraints() {
  406. return _pathConstraints;
  407. }
  408. Vector<TransformConstraint *> &Skeleton::getTransformConstraints() {
  409. return _transformConstraints;
  410. }
  411. Skin *Skeleton::getSkin() {
  412. return _skin;
  413. }
  414. Color &Skeleton::getColor() {
  415. return _color;
  416. }
  417. float Skeleton::getTime() {
  418. return _time;
  419. }
  420. void Skeleton::setTime(float inValue) {
  421. _time = inValue;
  422. }
  423. void Skeleton::setPosition(float x, float y) {
  424. _x = x;
  425. _y = y;
  426. }
  427. float Skeleton::getX() {
  428. return _x;
  429. }
  430. void Skeleton::setX(float inValue) {
  431. _x = inValue;
  432. }
  433. float Skeleton::getY() {
  434. return _y;
  435. }
  436. void Skeleton::setY(float inValue) {
  437. _y = inValue;
  438. }
  439. float Skeleton::getScaleX() {
  440. return _scaleX;
  441. }
  442. void Skeleton::setScaleX(float inValue) {
  443. _scaleX = inValue;
  444. }
  445. float Skeleton::getScaleY() {
  446. return _scaleY * (Bone::isYDown() ? -1 : 1);
  447. }
  448. void Skeleton::setScaleY(float inValue) {
  449. _scaleY = inValue;
  450. }
  451. void Skeleton::sortIkConstraint(IkConstraint *constraint) {
  452. constraint->_active = constraint->_target->_active && (!constraint->_data.isSkinRequired() || (_skin && _skin->_constraints.contains(&constraint->_data)));
  453. if (!constraint->_active) return;
  454. Bone *target = constraint->getTarget();
  455. sortBone(target);
  456. Vector<Bone *> &constrained = constraint->getBones();
  457. Bone *parent = constrained[0];
  458. sortBone(parent);
  459. if (constrained.size() > 1) {
  460. Bone *child = constrained[constrained.size() - 1];
  461. if (!_updateCache.contains(child)) _updateCacheReset.add(child);
  462. }
  463. _updateCache.add(constraint);
  464. sortReset(parent->getChildren());
  465. constrained[constrained.size() - 1]->_sorted = true;
  466. }
  467. void Skeleton::sortPathConstraint(PathConstraint *constraint) {
  468. constraint->_active = constraint->_target->_bone._active && (!constraint->_data.isSkinRequired() || (_skin && _skin->_constraints.contains(&constraint->_data)));
  469. if (!constraint->_active) return;
  470. Slot *slot = constraint->getTarget();
  471. int slotIndex = slot->getData().getIndex();
  472. Bone &slotBone = slot->getBone();
  473. if (_skin != NULL) sortPathConstraintAttachment(_skin, slotIndex, slotBone);
  474. if (_data->_defaultSkin != NULL && _data->_defaultSkin != _skin)
  475. sortPathConstraintAttachment(_data->_defaultSkin, slotIndex, slotBone);
  476. for (size_t ii = 0, nn = _data->_skins.size(); ii < nn; ii++)
  477. sortPathConstraintAttachment(_data->_skins[ii], slotIndex, slotBone);
  478. Attachment *attachment = slot->getAttachment();
  479. if (attachment != NULL && attachment->getRTTI().instanceOf(PathAttachment::rtti))
  480. sortPathConstraintAttachment(attachment, slotBone);
  481. Vector<Bone *> &constrained = constraint->getBones();
  482. size_t boneCount = constrained.size();
  483. for (size_t i = 0; i < boneCount; ++i) {
  484. sortBone(constrained[i]);
  485. }
  486. _updateCache.add(constraint);
  487. for (size_t i = 0; i < boneCount; i++)
  488. sortReset(constrained[i]->getChildren());
  489. for (size_t i = 0; i < boneCount; i++)
  490. constrained[i]->_sorted = true;
  491. }
  492. void Skeleton::sortTransformConstraint(TransformConstraint *constraint) {
  493. constraint->_active = constraint->_target->_active && (!constraint->_data.isSkinRequired() || (_skin && _skin->_constraints.contains(&constraint->_data)));
  494. if (!constraint->_active) return;
  495. sortBone(constraint->getTarget());
  496. Vector<Bone *> &constrained = constraint->getBones();
  497. size_t boneCount = constrained.size();
  498. if (constraint->_data.isLocal()) {
  499. for (size_t i = 0; i < boneCount; i++) {
  500. Bone *child = constrained[i];
  501. sortBone(child->getParent());
  502. if (!_updateCache.contains(child)) _updateCacheReset.add(child);
  503. }
  504. } else {
  505. for (size_t i = 0; i < boneCount; ++i) {
  506. sortBone(constrained[i]);
  507. }
  508. }
  509. _updateCache.add(constraint);
  510. for (size_t i = 0; i < boneCount; ++i)
  511. sortReset(constrained[i]->getChildren());
  512. for (size_t i = 0; i < boneCount; ++i)
  513. constrained[i]->_sorted = true;
  514. }
  515. void Skeleton::sortPathConstraintAttachment(Skin *skin, size_t slotIndex, Bone &slotBone) {
  516. Skin::AttachmentMap::Entries attachments = skin->getAttachments();
  517. while (attachments.hasNext()) {
  518. Skin::AttachmentMap::Entry entry = attachments.next();
  519. if (entry._slotIndex == slotIndex) {
  520. Attachment *value = entry._attachment;
  521. sortPathConstraintAttachment(value, slotBone);
  522. }
  523. }
  524. }
  525. void Skeleton::sortPathConstraintAttachment(Attachment *attachment, Bone &slotBone) {
  526. if (attachment == NULL || !attachment->getRTTI().instanceOf(PathAttachment::rtti)) return;
  527. Vector<size_t> &pathBones = static_cast<PathAttachment *>(attachment)->getBones();
  528. if (pathBones.size() == 0)
  529. sortBone(&slotBone);
  530. else {
  531. for (size_t i = 0, n = pathBones.size(); i < n;) {
  532. size_t nn = pathBones[i++];
  533. nn += i;
  534. while (i < nn) {
  535. sortBone(_bones[pathBones[i++]]);
  536. }
  537. }
  538. }
  539. }
  540. void Skeleton::sortBone(Bone *bone) {
  541. if (bone->_sorted) return;
  542. Bone *parent = bone->_parent;
  543. if (parent != NULL) sortBone(parent);
  544. bone->_sorted = true;
  545. _updateCache.add(bone);
  546. }
  547. void Skeleton::sortReset(Vector<Bone *> &bones) {
  548. for (size_t i = 0, n = bones.size(); i < n; ++i) {
  549. Bone *bone = bones[i];
  550. if (!bone->_active) continue;
  551. if (bone->_sorted) sortReset(bone->getChildren());
  552. bone->_sorted = false;
  553. }
  554. }