Skeleton.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. for (size_t i = 0, n = _bones.size(); i < n; ++i) {
  113. Bone* bone = _bones[i];
  114. bone->_sorted = bone->_data.isSkinRequired();
  115. bone->_active = !bone->_sorted;
  116. }
  117. if (_skin) {
  118. Vector<BoneData*>& skinBones = _skin->getBones();
  119. for (size_t i = 0, n = skinBones.size(); i < n; i++) {
  120. Bone* bone = _bones[skinBones[i]->getIndex()];
  121. do {
  122. bone->_sorted = false;
  123. bone->_active = true;
  124. bone = bone->_parent;
  125. } while (bone);
  126. }
  127. }
  128. size_t ikCount = _ikConstraints.size();
  129. size_t transformCount = _transformConstraints.size();
  130. size_t pathCount = _pathConstraints.size();
  131. size_t constraintCount = ikCount + transformCount + pathCount;
  132. size_t i = 0;
  133. continue_outer:
  134. for (; i < constraintCount; ++i) {
  135. for (size_t ii = 0; ii < ikCount; ++ii) {
  136. IkConstraint *constraint = _ikConstraints[ii];
  137. if (constraint->getData().getOrder() == i) {
  138. sortIkConstraint(constraint);
  139. i++;
  140. goto continue_outer;
  141. }
  142. }
  143. for (size_t ii = 0; ii < transformCount; ++ii) {
  144. TransformConstraint *constraint = _transformConstraints[ii];
  145. if (constraint->getData().getOrder() == i) {
  146. sortTransformConstraint(constraint);
  147. i++;
  148. goto continue_outer;
  149. }
  150. }
  151. for (size_t ii = 0; ii < pathCount; ++ii) {
  152. PathConstraint *constraint = _pathConstraints[ii];
  153. if (constraint->getData().getOrder() == i) {
  154. sortPathConstraint(constraint);
  155. i++;
  156. goto continue_outer;
  157. }
  158. }
  159. }
  160. size_t n = _bones.size();
  161. for (i = 0; i < n; ++i) {
  162. sortBone(_bones[i]);
  163. }
  164. }
  165. void Skeleton::printUpdateCache() {
  166. for (size_t i = 0; i < _updateCache.size(); i++) {
  167. Updatable *updatable = _updateCache[i];
  168. if (updatable->getRTTI().isExactly(Bone::rtti)) {
  169. printf("bone %s\n", ((Bone *) updatable)->getData().getName().buffer());
  170. } else if (updatable->getRTTI().isExactly(TransformConstraint::rtti)) {
  171. printf("transform constraint %s\n", ((TransformConstraint *) updatable)->getData().getName().buffer());
  172. } else if (updatable->getRTTI().isExactly(IkConstraint::rtti)) {
  173. printf("ik constraint %s\n", ((IkConstraint *) updatable)->getData().getName().buffer());
  174. } else if (updatable->getRTTI().isExactly(PathConstraint::rtti)) {
  175. printf("path constraint %s\n", ((PathConstraint *) updatable)->getData().getName().buffer());
  176. }
  177. }
  178. }
  179. void Skeleton::updateWorldTransform() {
  180. for (size_t i = 0, n = _updateCache.size(); i < n; ++i) {
  181. _updateCache[i]->update();
  182. }
  183. }
  184. void Skeleton::updateWorldTransform(Bone* parent) {
  185. // Apply the parent bone transform to the root bone. The root bone always inherits scale, rotation and reflection.
  186. Bone& rootBone = *getRootBone();
  187. float pa = parent->_a, pb = parent->_b, pc = parent->_c, pd = parent->_d;
  188. rootBone._worldX = pa * _x + pb * _y + parent->_worldX;
  189. rootBone._worldY = pc * _x + pd * _y + parent->_worldY;
  190. float rotationY = rootBone._rotation + 90 + rootBone._shearY;
  191. float la = MathUtil::cosDeg(rootBone._rotation + rootBone._shearX) * rootBone._scaleX;
  192. float lb = MathUtil::cosDeg(rotationY) * rootBone._scaleY;
  193. float lc = MathUtil::sinDeg(rootBone._rotation + rootBone._shearX) * rootBone._scaleX;
  194. float ld = MathUtil::sinDeg(rotationY) * rootBone._scaleY;
  195. rootBone._a = (pa * la + pb * lc) * _scaleX;
  196. rootBone._b = (pa * lb + pb * ld) * _scaleX;
  197. rootBone._c = (pc * la + pd * lc) * _scaleY;
  198. rootBone._d = (pc * lb + pd * ld) * _scaleY;
  199. // Update everything except root bone.
  200. Bone *rb = getRootBone();
  201. for (size_t i = 0, n = _updateCache.size(); i < n; i++) {
  202. Updatable *updatable = _updateCache[i];
  203. if (updatable != rb) updatable->update();
  204. }
  205. }
  206. void Skeleton::setToSetupPose() {
  207. setBonesToSetupPose();
  208. setSlotsToSetupPose();
  209. }
  210. void Skeleton::setBonesToSetupPose() {
  211. for (size_t i = 0, n = _bones.size(); i < n; ++i) {
  212. _bones[i]->setToSetupPose();
  213. }
  214. for (size_t i = 0, n = _ikConstraints.size(); i < n; ++i) {
  215. IkConstraint *constraintP = _ikConstraints[i];
  216. IkConstraint &constraint = *constraintP;
  217. constraint._bendDirection = constraint._data._bendDirection;
  218. constraint._compress = constraint._data._compress;
  219. constraint._stretch = constraint._data._stretch;
  220. constraint._mix = constraint._data._mix;
  221. constraint._softness = constraint._data._softness;
  222. }
  223. for (size_t i = 0, n = _transformConstraints.size(); i < n; ++i) {
  224. TransformConstraint *constraintP = _transformConstraints[i];
  225. TransformConstraint &constraint = *constraintP;
  226. TransformConstraintData &constraintData = constraint._data;
  227. constraint._mixRotate = constraintData._mixRotate;
  228. constraint._mixX = constraintData._mixX;
  229. constraint._mixY = constraintData._mixY;
  230. constraint._mixScaleX = constraintData._mixScaleX;
  231. constraint._mixScaleY = constraintData._mixScaleY;
  232. constraint._mixShearY = constraintData._mixShearY;
  233. }
  234. for (size_t i = 0, n = _pathConstraints.size(); i < n; ++i) {
  235. PathConstraint *constraintP = _pathConstraints[i];
  236. PathConstraint &constraint = *constraintP;
  237. PathConstraintData &constraintData = constraint._data;
  238. constraint._position = constraintData._position;
  239. constraint._spacing = constraintData._spacing;
  240. constraint._mixRotate = constraintData._mixRotate;
  241. constraint._mixX = constraintData._mixX;
  242. constraint._mixY = constraintData._mixY;
  243. }
  244. }
  245. void Skeleton::setSlotsToSetupPose() {
  246. _drawOrder.clear();
  247. for (size_t i = 0, n = _slots.size(); i < n; ++i) {
  248. _drawOrder.add(_slots[i]);
  249. }
  250. for (size_t i = 0, n = _slots.size(); i < n; ++i) {
  251. _slots[i]->setToSetupPose();
  252. }
  253. }
  254. Bone *Skeleton::findBone(const String &boneName) {
  255. return ContainerUtil::findWithDataName(_bones, boneName);
  256. }
  257. int Skeleton::findBoneIndex(const String &boneName) {
  258. return ContainerUtil::findIndexWithDataName(_bones, boneName);
  259. }
  260. Slot *Skeleton::findSlot(const String &slotName) {
  261. return ContainerUtil::findWithDataName(_slots, slotName);
  262. }
  263. int Skeleton::findSlotIndex(const String &slotName) {
  264. return ContainerUtil::findIndexWithDataName(_slots, slotName);
  265. }
  266. void Skeleton::setSkin(const String &skinName) {
  267. Skin *foundSkin = skinName.isEmpty() ? NULL : _data->findSkin(skinName);
  268. setSkin(foundSkin);
  269. }
  270. void Skeleton::setSkin(Skin *newSkin) {
  271. if (_skin == newSkin) return;
  272. if (newSkin != NULL) {
  273. if (_skin != NULL) {
  274. Skeleton &thisRef = *this;
  275. newSkin->attachAll(thisRef, *_skin);
  276. } else {
  277. for (size_t i = 0, n = _slots.size(); i < n; ++i) {
  278. Slot *slotP = _slots[i];
  279. Slot &slot = *slotP;
  280. const String &name = slot._data.getAttachmentName();
  281. if (name.length() > 0) {
  282. Attachment *attachment = newSkin->getAttachment(i, name);
  283. if (attachment != NULL) {
  284. slot.setAttachment(attachment);
  285. }
  286. }
  287. }
  288. }
  289. }
  290. _skin = newSkin;
  291. updateCache();
  292. }
  293. Attachment *Skeleton::getAttachment(const String &slotName, const String &attachmentName) {
  294. return getAttachment(_data->findSlotIndex(slotName), attachmentName);
  295. }
  296. Attachment *Skeleton::getAttachment(int slotIndex, const String &attachmentName) {
  297. if (attachmentName.isEmpty()) return NULL;
  298. if (_skin != NULL) {
  299. Attachment *attachment = _skin->getAttachment(slotIndex, attachmentName);
  300. if (attachment != NULL) {
  301. return attachment;
  302. }
  303. }
  304. return _data->getDefaultSkin() != NULL ? _data->getDefaultSkin()->getAttachment(slotIndex, attachmentName) : NULL;
  305. }
  306. void Skeleton::setAttachment(const String &slotName, const String &attachmentName) {
  307. assert(slotName.length() > 0);
  308. for (size_t i = 0, n = _slots.size(); i < n; ++i) {
  309. Slot *slot = _slots[i];
  310. if (slot->_data.getName() == slotName) {
  311. Attachment *attachment = NULL;
  312. if (attachmentName.length() > 0) {
  313. attachment = getAttachment(i, attachmentName);
  314. assert(attachment != NULL);
  315. }
  316. slot->setAttachment(attachment);
  317. return;
  318. }
  319. }
  320. printf("Slot not found: %s", slotName.buffer());
  321. assert(false);
  322. }
  323. IkConstraint *Skeleton::findIkConstraint(const String &constraintName) {
  324. assert(constraintName.length() > 0);
  325. for (size_t i = 0, n = _ikConstraints.size(); i < n; ++i) {
  326. IkConstraint *ikConstraint = _ikConstraints[i];
  327. if (ikConstraint->_data.getName() == constraintName) {
  328. return ikConstraint;
  329. }
  330. }
  331. return NULL;
  332. }
  333. TransformConstraint *Skeleton::findTransformConstraint(const String &constraintName) {
  334. assert(constraintName.length() > 0);
  335. for (size_t i = 0, n = _transformConstraints.size(); i < n; ++i) {
  336. TransformConstraint *transformConstraint = _transformConstraints[i];
  337. if (transformConstraint->_data.getName() == constraintName) {
  338. return transformConstraint;
  339. }
  340. }
  341. return NULL;
  342. }
  343. PathConstraint *Skeleton::findPathConstraint(const String &constraintName) {
  344. assert(constraintName.length() > 0);
  345. for (size_t i = 0, n = _pathConstraints.size(); i < n; ++i) {
  346. PathConstraint *constraint = _pathConstraints[i];
  347. if (constraint->_data.getName() == constraintName) {
  348. return constraint;
  349. }
  350. }
  351. return NULL;
  352. }
  353. void Skeleton::update(float delta) {
  354. _time += delta;
  355. }
  356. void Skeleton::getBounds(float &outX, float &outY, float &outWidth, float &outHeight, Vector<float> &outVertexBuffer) {
  357. float minX = FLT_MAX;
  358. float minY = FLT_MAX;
  359. float maxX = FLT_MIN;
  360. float maxY = FLT_MIN;
  361. for (size_t i = 0; i < _drawOrder.size(); ++i) {
  362. Slot *slot = _drawOrder[i];
  363. if (!slot->_bone._active) continue;
  364. size_t verticesLength = 0;
  365. Attachment *attachment = slot->getAttachment();
  366. if (attachment != NULL && attachment->getRTTI().instanceOf(RegionAttachment::rtti)) {
  367. RegionAttachment *regionAttachment = static_cast<RegionAttachment *>(attachment);
  368. verticesLength = 8;
  369. if (outVertexBuffer.size() < 8) {
  370. outVertexBuffer.setSize(8, 0);
  371. }
  372. regionAttachment->computeWorldVertices(slot->getBone(), outVertexBuffer, 0);
  373. } else if (attachment != NULL && attachment->getRTTI().instanceOf(MeshAttachment::rtti)) {
  374. MeshAttachment *mesh = static_cast<MeshAttachment *>(attachment);
  375. verticesLength = mesh->getWorldVerticesLength();
  376. if (outVertexBuffer.size() < verticesLength) {
  377. outVertexBuffer.setSize(verticesLength, 0);
  378. }
  379. mesh->computeWorldVertices(*slot, 0, verticesLength, outVertexBuffer, 0);
  380. }
  381. for (size_t ii = 0; ii < verticesLength; ii += 2) {
  382. float vx = outVertexBuffer[ii];
  383. float vy = outVertexBuffer[ii + 1];
  384. minX = MathUtil::min(minX, vx);
  385. minY = MathUtil::min(minY, vy);
  386. maxX = MathUtil::max(maxX, vx);
  387. maxY = MathUtil::max(maxY, vy);
  388. }
  389. }
  390. outX = minX;
  391. outY = minY;
  392. outWidth = maxX - minX;
  393. outHeight = maxY - minY;
  394. }
  395. Bone *Skeleton::getRootBone() {
  396. return _bones.size() == 0 ? NULL : _bones[0];
  397. }
  398. SkeletonData *Skeleton::getData() {
  399. return _data;
  400. }
  401. Vector<Bone *> &Skeleton::getBones() {
  402. return _bones;
  403. }
  404. Vector<Updatable *> &Skeleton::getUpdateCacheList() {
  405. return _updateCache;
  406. }
  407. Vector<Slot *> &Skeleton::getSlots() {
  408. return _slots;
  409. }
  410. Vector<Slot *> &Skeleton::getDrawOrder() {
  411. return _drawOrder;
  412. }
  413. Vector<IkConstraint *> &Skeleton::getIkConstraints() {
  414. return _ikConstraints;
  415. }
  416. Vector<PathConstraint *> &Skeleton::getPathConstraints() {
  417. return _pathConstraints;
  418. }
  419. Vector<TransformConstraint *> &Skeleton::getTransformConstraints() {
  420. return _transformConstraints;
  421. }
  422. Skin *Skeleton::getSkin() {
  423. return _skin;
  424. }
  425. Color &Skeleton::getColor() {
  426. return _color;
  427. }
  428. float Skeleton::getTime() {
  429. return _time;
  430. }
  431. void Skeleton::setTime(float inValue) {
  432. _time = inValue;
  433. }
  434. void Skeleton::setPosition(float x, float y) {
  435. _x = x;
  436. _y = y;
  437. }
  438. float Skeleton::getX() {
  439. return _x;
  440. }
  441. void Skeleton::setX(float inValue) {
  442. _x = inValue;
  443. }
  444. float Skeleton::getY() {
  445. return _y;
  446. }
  447. void Skeleton::setY(float inValue) {
  448. _y = inValue;
  449. }
  450. float Skeleton::getScaleX() {
  451. return _scaleX;
  452. }
  453. void Skeleton::setScaleX(float inValue) {
  454. _scaleX = inValue;
  455. }
  456. float Skeleton::getScaleY() {
  457. return _scaleY * (Bone::isYDown() ? -1 : 1);
  458. }
  459. void Skeleton::setScaleY(float inValue) {
  460. _scaleY = inValue;
  461. }
  462. void Skeleton::sortIkConstraint(IkConstraint *constraint) {
  463. constraint->_active = constraint->_target->_active && (!constraint->_data.isSkinRequired() || (_skin && _skin->_constraints.contains(&constraint->_data)));
  464. if (!constraint->_active) return;
  465. Bone *target = constraint->getTarget();
  466. sortBone(target);
  467. Vector<Bone *> &constrained = constraint->getBones();
  468. Bone *parent = constrained[0];
  469. sortBone(parent);
  470. if (constrained.size() == 1) {
  471. _updateCache.add(constraint);
  472. sortReset(parent->_children);
  473. } else {
  474. Bone* child = constrained[constrained.size() - 1];
  475. sortBone(child);
  476. _updateCache.add(constraint);
  477. sortReset(parent->_children);
  478. child->_sorted = true;
  479. }
  480. }
  481. void Skeleton::sortPathConstraint(PathConstraint *constraint) {
  482. constraint->_active = constraint->_target->_bone._active && (!constraint->_data.isSkinRequired() || (_skin && _skin->_constraints.contains(&constraint->_data)));
  483. if (!constraint->_active) return;
  484. Slot *slot = constraint->getTarget();
  485. int slotIndex = slot->getData().getIndex();
  486. Bone &slotBone = slot->getBone();
  487. if (_skin != NULL) sortPathConstraintAttachment(_skin, slotIndex, slotBone);
  488. if (_data->_defaultSkin != NULL && _data->_defaultSkin != _skin)
  489. sortPathConstraintAttachment(_data->_defaultSkin, slotIndex, slotBone);
  490. for (size_t ii = 0, nn = _data->_skins.size(); ii < nn; ii++)
  491. sortPathConstraintAttachment(_data->_skins[ii], slotIndex, slotBone);
  492. Attachment *attachment = slot->getAttachment();
  493. if (attachment != NULL && attachment->getRTTI().instanceOf(PathAttachment::rtti))
  494. sortPathConstraintAttachment(attachment, slotBone);
  495. Vector<Bone *> &constrained = constraint->getBones();
  496. size_t boneCount = constrained.size();
  497. for (size_t i = 0; i < boneCount; ++i) {
  498. sortBone(constrained[i]);
  499. }
  500. _updateCache.add(constraint);
  501. for (size_t i = 0; i < boneCount; i++)
  502. sortReset(constrained[i]->getChildren());
  503. for (size_t i = 0; i < boneCount; i++)
  504. constrained[i]->_sorted = true;
  505. }
  506. void Skeleton::sortTransformConstraint(TransformConstraint *constraint) {
  507. constraint->_active = constraint->_target->_active && (!constraint->_data.isSkinRequired() || (_skin && _skin->_constraints.contains(&constraint->_data)));
  508. if (!constraint->_active) return;
  509. sortBone(constraint->getTarget());
  510. Vector<Bone *> &constrained = constraint->getBones();
  511. size_t boneCount = constrained.size();
  512. if (constraint->_data.isLocal()) {
  513. for (size_t i = 0; i < boneCount; i++) {
  514. Bone *child = constrained[i];
  515. sortBone(child->getParent());
  516. sortBone(child);
  517. }
  518. } else {
  519. for (size_t i = 0; i < boneCount; ++i) {
  520. sortBone(constrained[i]);
  521. }
  522. }
  523. _updateCache.add(constraint);
  524. for (size_t i = 0; i < boneCount; ++i)
  525. sortReset(constrained[i]->getChildren());
  526. for (size_t i = 0; i < boneCount; ++i)
  527. constrained[i]->_sorted = true;
  528. }
  529. void Skeleton::sortPathConstraintAttachment(Skin *skin, size_t slotIndex, Bone &slotBone) {
  530. Skin::AttachmentMap::Entries attachments = skin->getAttachments();
  531. while (attachments.hasNext()) {
  532. Skin::AttachmentMap::Entry entry = attachments.next();
  533. if (entry._slotIndex == slotIndex) {
  534. Attachment *value = entry._attachment;
  535. sortPathConstraintAttachment(value, slotBone);
  536. }
  537. }
  538. }
  539. void Skeleton::sortPathConstraintAttachment(Attachment *attachment, Bone &slotBone) {
  540. if (attachment == NULL || !attachment->getRTTI().instanceOf(PathAttachment::rtti)) return;
  541. Vector<size_t> &pathBones = static_cast<PathAttachment *>(attachment)->getBones();
  542. if (pathBones.size() == 0)
  543. sortBone(&slotBone);
  544. else {
  545. for (size_t i = 0, n = pathBones.size(); i < n;) {
  546. size_t nn = pathBones[i++];
  547. nn += i;
  548. while (i < nn) {
  549. sortBone(_bones[pathBones[i++]]);
  550. }
  551. }
  552. }
  553. }
  554. void Skeleton::sortBone(Bone *bone) {
  555. if (bone->_sorted) return;
  556. Bone *parent = bone->_parent;
  557. if (parent != NULL) sortBone(parent);
  558. bone->_sorted = true;
  559. _updateCache.add(bone);
  560. }
  561. void Skeleton::sortReset(Vector<Bone *> &bones) {
  562. for (size_t i = 0, n = bones.size(); i < n; ++i) {
  563. Bone *bone = bones[i];
  564. if (!bone->_active) continue;
  565. if (bone->_sorted) sortReset(bone->getChildren());
  566. bone->_sorted = false;
  567. }
  568. }