Skeleton.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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 = skinName.isEmpty() ? NULL : _data->findSkin(skinName);
  256. setSkin(foundSkin);
  257. }
  258. void Skeleton::setSkin(Skin *newSkin) {
  259. if (_skin == newSkin) return;
  260. if (newSkin != NULL) {
  261. if (_skin != NULL) {
  262. Skeleton &thisRef = *this;
  263. newSkin->attachAll(thisRef, *_skin);
  264. } else {
  265. for (size_t i = 0, n = _slots.size(); i < n; ++i) {
  266. Slot *slotP = _slots[i];
  267. Slot &slot = *slotP;
  268. const String &name = slot._data.getAttachmentName();
  269. if (name.length() > 0) {
  270. Attachment *attachment = newSkin->getAttachment(i, name);
  271. if (attachment != NULL) {
  272. slot.setAttachment(attachment);
  273. }
  274. }
  275. }
  276. }
  277. }
  278. _skin = newSkin;
  279. updateCache();
  280. }
  281. Attachment *Skeleton::getAttachment(const String &slotName, const String &attachmentName) {
  282. return getAttachment(_data->findSlotIndex(slotName), attachmentName);
  283. }
  284. Attachment *Skeleton::getAttachment(int slotIndex, const String &attachmentName) {
  285. if (attachmentName.isEmpty()) return NULL;
  286. if (_skin != NULL) {
  287. Attachment *attachment = _skin->getAttachment(slotIndex, attachmentName);
  288. if (attachment != NULL) {
  289. return attachment;
  290. }
  291. }
  292. return _data->getDefaultSkin() != NULL ? _data->getDefaultSkin()->getAttachment(slotIndex, attachmentName) : NULL;
  293. }
  294. void Skeleton::setAttachment(const String &slotName, const String &attachmentName) {
  295. assert(slotName.length() > 0);
  296. for (size_t i = 0, n = _slots.size(); i < n; ++i) {
  297. Slot *slot = _slots[i];
  298. if (slot->_data.getName() == slotName) {
  299. Attachment *attachment = NULL;
  300. if (attachmentName.length() > 0) {
  301. attachment = getAttachment(i, attachmentName);
  302. assert(attachment != NULL);
  303. }
  304. slot->setAttachment(attachment);
  305. return;
  306. }
  307. }
  308. printf("Slot not found: %s", slotName.buffer());
  309. assert(false);
  310. }
  311. IkConstraint *Skeleton::findIkConstraint(const String &constraintName) {
  312. assert(constraintName.length() > 0);
  313. for (size_t i = 0, n = _ikConstraints.size(); i < n; ++i) {
  314. IkConstraint *ikConstraint = _ikConstraints[i];
  315. if (ikConstraint->_data.getName() == constraintName) {
  316. return ikConstraint;
  317. }
  318. }
  319. return NULL;
  320. }
  321. TransformConstraint *Skeleton::findTransformConstraint(const String &constraintName) {
  322. assert(constraintName.length() > 0);
  323. for (size_t i = 0, n = _transformConstraints.size(); i < n; ++i) {
  324. TransformConstraint *transformConstraint = _transformConstraints[i];
  325. if (transformConstraint->_data.getName() == constraintName) {
  326. return transformConstraint;
  327. }
  328. }
  329. return NULL;
  330. }
  331. PathConstraint *Skeleton::findPathConstraint(const String &constraintName) {
  332. assert(constraintName.length() > 0);
  333. for (size_t i = 0, n = _pathConstraints.size(); i < n; ++i) {
  334. PathConstraint *constraint = _pathConstraints[i];
  335. if (constraint->_data.getName() == constraintName) {
  336. return constraint;
  337. }
  338. }
  339. return NULL;
  340. }
  341. void Skeleton::update(float delta) {
  342. _time += delta;
  343. }
  344. void Skeleton::getBounds(float &outX, float &outY, float &outWidth, float &outHeight, Vector<float> &outVertexBuffer) {
  345. float minX = FLT_MAX;
  346. float minY = FLT_MAX;
  347. float maxX = FLT_MIN;
  348. float maxY = FLT_MIN;
  349. for (size_t i = 0; i < _drawOrder.size(); ++i) {
  350. Slot *slot = _drawOrder[i];
  351. if (!slot->_bone._active) continue;
  352. size_t verticesLength = 0;
  353. Attachment *attachment = slot->getAttachment();
  354. if (attachment != NULL && attachment->getRTTI().instanceOf(RegionAttachment::rtti)) {
  355. RegionAttachment *regionAttachment = static_cast<RegionAttachment *>(attachment);
  356. verticesLength = 8;
  357. if (outVertexBuffer.size() < 8) {
  358. outVertexBuffer.setSize(8, 0);
  359. }
  360. regionAttachment->computeWorldVertices(slot->getBone(), outVertexBuffer, 0);
  361. } else if (attachment != NULL && attachment->getRTTI().instanceOf(MeshAttachment::rtti)) {
  362. MeshAttachment *mesh = static_cast<MeshAttachment *>(attachment);
  363. verticesLength = mesh->getWorldVerticesLength();
  364. if (outVertexBuffer.size() < verticesLength) {
  365. outVertexBuffer.setSize(verticesLength, 0);
  366. }
  367. mesh->computeWorldVertices(*slot, 0, verticesLength, outVertexBuffer, 0);
  368. }
  369. for (size_t ii = 0; ii < verticesLength; ii += 2) {
  370. float vx = outVertexBuffer[ii];
  371. float vy = outVertexBuffer[ii + 1];
  372. minX = MathUtil::min(minX, vx);
  373. minY = MathUtil::min(minY, vy);
  374. maxX = MathUtil::max(maxX, vx);
  375. maxY = MathUtil::max(maxY, vy);
  376. }
  377. }
  378. outX = minX;
  379. outY = minY;
  380. outWidth = maxX - minX;
  381. outHeight = maxY - minY;
  382. }
  383. Bone *Skeleton::getRootBone() {
  384. return _bones.size() == 0 ? NULL : _bones[0];
  385. }
  386. SkeletonData *Skeleton::getData() {
  387. return _data;
  388. }
  389. Vector<Bone *> &Skeleton::getBones() {
  390. return _bones;
  391. }
  392. Vector<Updatable *> &Skeleton::getUpdateCacheList() {
  393. return _updateCache;
  394. }
  395. Vector<Slot *> &Skeleton::getSlots() {
  396. return _slots;
  397. }
  398. Vector<Slot *> &Skeleton::getDrawOrder() {
  399. return _drawOrder;
  400. }
  401. Vector<IkConstraint *> &Skeleton::getIkConstraints() {
  402. return _ikConstraints;
  403. }
  404. Vector<PathConstraint *> &Skeleton::getPathConstraints() {
  405. return _pathConstraints;
  406. }
  407. Vector<TransformConstraint *> &Skeleton::getTransformConstraints() {
  408. return _transformConstraints;
  409. }
  410. Skin *Skeleton::getSkin() {
  411. return _skin;
  412. }
  413. Color &Skeleton::getColor() {
  414. return _color;
  415. }
  416. float Skeleton::getTime() {
  417. return _time;
  418. }
  419. void Skeleton::setTime(float inValue) {
  420. _time = inValue;
  421. }
  422. void Skeleton::setPosition(float x, float y) {
  423. _x = x;
  424. _y = y;
  425. }
  426. float Skeleton::getX() {
  427. return _x;
  428. }
  429. void Skeleton::setX(float inValue) {
  430. _x = inValue;
  431. }
  432. float Skeleton::getY() {
  433. return _y;
  434. }
  435. void Skeleton::setY(float inValue) {
  436. _y = inValue;
  437. }
  438. float Skeleton::getScaleX() {
  439. return _scaleX;
  440. }
  441. void Skeleton::setScaleX(float inValue) {
  442. _scaleX = inValue;
  443. }
  444. float Skeleton::getScaleY() {
  445. return _scaleY * (Bone::isYDown() ? -1 : 1);
  446. }
  447. void Skeleton::setScaleY(float inValue) {
  448. _scaleY = inValue;
  449. }
  450. void Skeleton::sortIkConstraint(IkConstraint *constraint) {
  451. constraint->_active = constraint->_target->_active && (!constraint->_data.isSkinRequired() || (_skin && _skin->_constraints.contains(&constraint->_data)));
  452. if (!constraint->_active) return;
  453. Bone *target = constraint->getTarget();
  454. sortBone(target);
  455. Vector<Bone *> &constrained = constraint->getBones();
  456. Bone *parent = constrained[0];
  457. sortBone(parent);
  458. if (constrained.size() > 1) {
  459. Bone *child = constrained[constrained.size() - 1];
  460. if (!_updateCache.contains(child)) _updateCacheReset.add(child);
  461. }
  462. _updateCache.add(constraint);
  463. sortReset(parent->getChildren());
  464. constrained[constrained.size() - 1]->_sorted = true;
  465. }
  466. void Skeleton::sortPathConstraint(PathConstraint *constraint) {
  467. constraint->_active = constraint->_target->_bone._active && (!constraint->_data.isSkinRequired() || (_skin && _skin->_constraints.contains(&constraint->_data)));
  468. if (!constraint->_active) return;
  469. Slot *slot = constraint->getTarget();
  470. int slotIndex = slot->getData().getIndex();
  471. Bone &slotBone = slot->getBone();
  472. if (_skin != NULL) sortPathConstraintAttachment(_skin, slotIndex, slotBone);
  473. if (_data->_defaultSkin != NULL && _data->_defaultSkin != _skin)
  474. sortPathConstraintAttachment(_data->_defaultSkin, slotIndex, slotBone);
  475. for (size_t ii = 0, nn = _data->_skins.size(); ii < nn; ii++)
  476. sortPathConstraintAttachment(_data->_skins[ii], slotIndex, slotBone);
  477. Attachment *attachment = slot->getAttachment();
  478. if (attachment != NULL && attachment->getRTTI().instanceOf(PathAttachment::rtti))
  479. sortPathConstraintAttachment(attachment, slotBone);
  480. Vector<Bone *> &constrained = constraint->getBones();
  481. size_t boneCount = constrained.size();
  482. for (size_t i = 0; i < boneCount; ++i) {
  483. sortBone(constrained[i]);
  484. }
  485. _updateCache.add(constraint);
  486. for (size_t i = 0; i < boneCount; i++)
  487. sortReset(constrained[i]->getChildren());
  488. for (size_t i = 0; i < boneCount; i++)
  489. constrained[i]->_sorted = true;
  490. }
  491. void Skeleton::sortTransformConstraint(TransformConstraint *constraint) {
  492. constraint->_active = constraint->_target->_active && (!constraint->_data.isSkinRequired() || (_skin && _skin->_constraints.contains(&constraint->_data)));
  493. if (!constraint->_active) return;
  494. sortBone(constraint->getTarget());
  495. Vector<Bone *> &constrained = constraint->getBones();
  496. size_t boneCount = constrained.size();
  497. if (constraint->_data.isLocal()) {
  498. for (size_t i = 0; i < boneCount; i++) {
  499. Bone *child = constrained[i];
  500. sortBone(child->getParent());
  501. if (!_updateCache.contains(child)) _updateCacheReset.add(child);
  502. }
  503. } else {
  504. for (size_t i = 0; i < boneCount; ++i) {
  505. sortBone(constrained[i]);
  506. }
  507. }
  508. _updateCache.add(constraint);
  509. for (size_t i = 0; i < boneCount; ++i)
  510. sortReset(constrained[i]->getChildren());
  511. for (size_t i = 0; i < boneCount; ++i)
  512. constrained[i]->_sorted = true;
  513. }
  514. void Skeleton::sortPathConstraintAttachment(Skin *skin, size_t slotIndex, Bone &slotBone) {
  515. Skin::AttachmentMap::Entries attachments = skin->getAttachments();
  516. while (attachments.hasNext()) {
  517. Skin::AttachmentMap::Entry entry = attachments.next();
  518. if (entry._slotIndex == slotIndex) {
  519. Attachment *value = entry._attachment;
  520. sortPathConstraintAttachment(value, slotBone);
  521. }
  522. }
  523. }
  524. void Skeleton::sortPathConstraintAttachment(Attachment *attachment, Bone &slotBone) {
  525. if (attachment == NULL || !attachment->getRTTI().instanceOf(PathAttachment::rtti)) return;
  526. Vector<size_t> &pathBones = static_cast<PathAttachment *>(attachment)->getBones();
  527. if (pathBones.size() == 0)
  528. sortBone(&slotBone);
  529. else {
  530. for (size_t i = 0, n = pathBones.size(); i < n;) {
  531. size_t nn = pathBones[i++];
  532. nn += i;
  533. while (i < nn) {
  534. sortBone(_bones[pathBones[i++]]);
  535. }
  536. }
  537. }
  538. }
  539. void Skeleton::sortBone(Bone *bone) {
  540. if (bone->_sorted) return;
  541. Bone *parent = bone->_parent;
  542. if (parent != NULL) sortBone(parent);
  543. bone->_sorted = true;
  544. _updateCache.add(bone);
  545. }
  546. void Skeleton::sortReset(Vector<Bone *> &bones) {
  547. for (size_t i = 0, n = bones.size(); i < n; ++i) {
  548. Bone *bone = bones[i];
  549. if (!bone->_active) continue;
  550. if (bone->_sorted) sortReset(bone->getChildren());
  551. bone->_sorted = false;
  552. }
  553. }