Skin.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef Spine_Skin_h
  30. #define Spine_Skin_h
  31. #include <spine/Vector.h>
  32. #include <spine/SpineString.h>
  33. namespace spine {
  34. class Attachment;
  35. class Skeleton;
  36. class BoneData;
  37. class ConstraintData;
  38. /// Stores attachments by slot index and attachment name.
  39. /// See SkeletonData::getDefaultSkin, Skeleton::getSkin, and
  40. /// http://esotericsoftware.com/spine-runtime-skins in the Spine Runtimes Guide.
  41. class SP_API Skin : public SpineObject {
  42. friend class Skeleton;
  43. public:
  44. class SP_API AttachmentMap : public SpineObject {
  45. friend class Skin;
  46. public:
  47. struct SP_API Entry {
  48. size_t _slotIndex;
  49. String _name;
  50. Attachment *_attachment;
  51. Entry(size_t slotIndex, const String &name, Attachment *attachment) :
  52. _slotIndex(slotIndex),
  53. _name(name),
  54. _attachment(attachment) {
  55. }
  56. };
  57. class SP_API Entries {
  58. friend class AttachmentMap;
  59. public:
  60. bool hasNext() {
  61. while(true) {
  62. if (_slotIndex >= _buckets.size()) return false;
  63. if (_bucketIndex >= _buckets[_slotIndex].size()) {
  64. _bucketIndex = 0;
  65. ++_slotIndex;
  66. continue;
  67. };
  68. return true;
  69. }
  70. }
  71. Entry &next() {
  72. Entry &result = _buckets[_slotIndex][_bucketIndex];
  73. ++_bucketIndex;
  74. return result;
  75. }
  76. protected:
  77. Entries(Vector< Vector<Entry> > &buckets) : _buckets(buckets), _slotIndex(0), _bucketIndex(0) {
  78. }
  79. private:
  80. Vector< Vector<Entry> > &_buckets;
  81. size_t _slotIndex;
  82. size_t _bucketIndex;
  83. };
  84. void put(size_t slotIndex, const String &attachmentName, Attachment *attachment);
  85. Attachment *get(size_t slotIndex, const String &attachmentName);
  86. void remove(size_t slotIndex, const String &attachmentName);
  87. Entries getEntries();
  88. protected:
  89. AttachmentMap();
  90. private:
  91. int findInBucket(Vector <Entry> &, const String &attachmentName);
  92. Vector <Vector<Entry> > _buckets;
  93. };
  94. explicit Skin(const String &name);
  95. ~Skin();
  96. /// Adds an attachment to the skin for the specified slot index and name.
  97. /// If the name already exists for the slot, the previous value is replaced.
  98. void setAttachment(size_t slotIndex, const String &name, Attachment *attachment);
  99. /// Returns the attachment for the specified slot index and name, or NULL.
  100. Attachment *getAttachment(size_t slotIndex, const String &name);
  101. // Removes the attachment from the skin.
  102. void removeAttachment(size_t slotIndex, const String& name);
  103. /// Finds the skin keys for a given slot. The results are added to the passed array of names.
  104. /// @param slotIndex The target slotIndex. To find the slot index, use Skeleton::findSlotIndex or SkeletonData::findSlotIndex
  105. /// @param names Found skin key names will be added to this array.
  106. void findNamesForSlot(size_t slotIndex, Vector <String> &names);
  107. /// Finds the attachments for a given slot. The results are added to the passed array of Attachments.
  108. /// @param slotIndex The target slotIndex. To find the slot index, use Skeleton::findSlotIndex or SkeletonData::findSlotIndex
  109. /// @param attachments Found Attachments will be added to this array.
  110. void findAttachmentsForSlot(size_t slotIndex, Vector<Attachment *> &attachments);
  111. const String &getName();
  112. /// Adds all attachments, bones, and constraints from the specified skin to this skin.
  113. void addSkin(Skin* other);
  114. /// Adds all attachments, bones, and constraints from the specified skin to this skin. Attachments are deep copied.
  115. void copySkin(Skin* other);
  116. AttachmentMap::Entries getAttachments();
  117. Vector<BoneData*>& getBones();
  118. Vector<ConstraintData*>& getConstraints();
  119. private:
  120. const String _name;
  121. AttachmentMap _attachments;
  122. Vector<BoneData*> _bones;
  123. Vector<ConstraintData*> _constraints;
  124. /// Attach all attachments from this skin if the corresponding attachment from the old skin is currently attached.
  125. void attachAll(Skeleton &skeleton, Skin &oldSkin);
  126. };
  127. }
  128. #endif /* Spine_Skin_h */