Skin.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/Skin.h>
  33. #include <spine/Attachment.h>
  34. #include <spine/MeshAttachment.h>
  35. #include <spine/Skeleton.h>
  36. #include <spine/Slot.h>
  37. #include <spine/ConstraintData.h>
  38. #include <assert.h>
  39. using namespace spine;
  40. Skin::AttachmentMap::AttachmentMap() {
  41. }
  42. static void disposeAttachment(Attachment* attachment) {
  43. if (!attachment) return;
  44. attachment->dereference();
  45. if (attachment->getRefCount() == 0) delete attachment;
  46. }
  47. void Skin::AttachmentMap::put(size_t slotIndex, const String &attachmentName, Attachment *attachment) {
  48. if (slotIndex >= _buckets.size())
  49. _buckets.setSize(slotIndex + 1, Vector<Entry>());
  50. Vector<Entry> &bucket = _buckets[slotIndex];
  51. int existing = findInBucket(bucket, attachmentName);
  52. attachment->reference();
  53. if (existing >= 0) {
  54. disposeAttachment(bucket[existing]._attachment);
  55. bucket[existing]._attachment = attachment;
  56. } else {
  57. bucket.add(Entry(slotIndex, attachmentName, attachment));
  58. }
  59. }
  60. Attachment *Skin::AttachmentMap::get(size_t slotIndex, const String &attachmentName) {
  61. if (slotIndex >= _buckets.size()) return NULL;
  62. int existing = findInBucket(_buckets[slotIndex], attachmentName);
  63. return existing >= 0 ? _buckets[slotIndex][existing]._attachment : NULL;
  64. }
  65. void Skin::AttachmentMap::remove(size_t slotIndex, const String &attachmentName) {
  66. if (slotIndex >= _buckets.size()) return;
  67. int existing = findInBucket(_buckets[slotIndex], attachmentName);
  68. if (existing >= 0) {
  69. disposeAttachment(_buckets[slotIndex][existing]._attachment);
  70. _buckets[slotIndex].removeAt(existing);
  71. }
  72. }
  73. int Skin::AttachmentMap::findInBucket(Vector<Entry> &bucket, const String &attachmentName) {
  74. for (size_t i = 0; i < bucket.size(); i++)
  75. if (bucket[i]._name == attachmentName) return i;
  76. return -1;
  77. }
  78. Skin::AttachmentMap::Entries Skin::AttachmentMap::getEntries() {
  79. return Skin::AttachmentMap::Entries(_buckets);
  80. }
  81. Skin::Skin(const String &name) : _name(name), _attachments() {
  82. assert(_name.length() > 0);
  83. }
  84. Skin::~Skin() {
  85. Skin::AttachmentMap::Entries entries = _attachments.getEntries();
  86. while (entries.hasNext()) {
  87. Skin::AttachmentMap::Entry entry = entries.next();
  88. disposeAttachment(entry._attachment);
  89. }
  90. }
  91. void Skin::setAttachment(size_t slotIndex, const String &name, Attachment *attachment) {
  92. assert(attachment);
  93. _attachments.put(slotIndex, name, attachment);
  94. }
  95. Attachment *Skin::getAttachment(size_t slotIndex, const String &name) {
  96. return _attachments.get(slotIndex, name);
  97. }
  98. void Skin::removeAttachment(size_t slotIndex, const String& name) {
  99. _attachments.remove(slotIndex, name);
  100. }
  101. void Skin::findNamesForSlot(size_t slotIndex, Vector<String> &names) {
  102. Skin::AttachmentMap::Entries entries = _attachments.getEntries();
  103. while (entries.hasNext()) {
  104. Skin::AttachmentMap::Entry &entry = entries.next();
  105. if (entry._slotIndex == slotIndex) {
  106. names.add(entry._name);
  107. }
  108. }
  109. }
  110. void Skin::findAttachmentsForSlot(size_t slotIndex, Vector<Attachment *> &attachments) {
  111. Skin::AttachmentMap::Entries entries = _attachments.getEntries();
  112. while (entries.hasNext()) {
  113. Skin::AttachmentMap::Entry &entry = entries.next();
  114. if (entry._slotIndex == slotIndex) attachments.add(entry._attachment);
  115. }
  116. }
  117. const String &Skin::getName() {
  118. return _name;
  119. }
  120. Skin::AttachmentMap::Entries Skin::getAttachments() {
  121. return _attachments.getEntries();
  122. }
  123. void Skin::attachAll(Skeleton &skeleton, Skin &oldSkin) {
  124. Vector<Slot *> &slots = skeleton.getSlots();
  125. Skin::AttachmentMap::Entries entries = oldSkin.getAttachments();
  126. while (entries.hasNext()) {
  127. Skin::AttachmentMap::Entry &entry = entries.next();
  128. int slotIndex = entry._slotIndex;
  129. Slot *slot = slots[slotIndex];
  130. if (slot->getAttachment() == entry._attachment) {
  131. Attachment *attachment = getAttachment(slotIndex, entry._name);
  132. if (attachment) slot->setAttachment(attachment);
  133. }
  134. }
  135. }
  136. void Skin::addSkin(Skin* other) {
  137. for (size_t i = 0; i < other->getBones().size(); i++)
  138. if (!_bones.contains(other->getBones()[i])) _bones.add(other->getBones()[i]);
  139. for (size_t i = 0; i < other->getConstraints().size(); i++)
  140. if (!_constraints.contains(other->getConstraints()[i])) _constraints.add(other->getConstraints()[i]);
  141. AttachmentMap::Entries entries = other->getAttachments();
  142. while(entries.hasNext()) {
  143. AttachmentMap::Entry& entry = entries.next();
  144. setAttachment(entry._slotIndex, entry._name, entry._attachment);
  145. }
  146. }
  147. void Skin::copySkin(Skin* other) {
  148. for (size_t i = 0; i < other->getBones().size(); i++)
  149. if (!_bones.contains(other->getBones()[i])) _bones.add(other->getBones()[i]);
  150. for (size_t i = 0; i < other->getConstraints().size(); i++)
  151. if (!_constraints.contains(other->getConstraints()[i])) _constraints.add(other->getConstraints()[i]);
  152. AttachmentMap::Entries entries = other->getAttachments();
  153. while(entries.hasNext()) {
  154. AttachmentMap::Entry& entry = entries.next();
  155. if (entry._attachment->getRTTI().isExactly(MeshAttachment::rtti))
  156. setAttachment(entry._slotIndex, entry._name, static_cast<MeshAttachment*>(entry._attachment)->newLinkedMesh());
  157. else
  158. setAttachment(entry._slotIndex, entry._name, entry._attachment->copy());
  159. }
  160. }
  161. Vector<ConstraintData*>& Skin::getConstraints() {
  162. return _constraints;
  163. }
  164. Vector<BoneData*>& Skin::getBones() {
  165. return _bones;
  166. }