AttachmentTimeline.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/AttachmentTimeline.h>
  33. #include <spine/Skeleton.h>
  34. #include <spine/Event.h>
  35. #include <spine/Animation.h>
  36. #include <spine/Bone.h>
  37. #include <spine/TimelineType.h>
  38. #include <spine/Slot.h>
  39. #include <spine/SlotData.h>
  40. using namespace spine;
  41. RTTI_IMPL(AttachmentTimeline, Timeline)
  42. AttachmentTimeline::AttachmentTimeline(int frameCount) : Timeline(), _slotIndex(0) {
  43. _frames.ensureCapacity(frameCount);
  44. _attachmentNames.ensureCapacity(frameCount);
  45. _frames.setSize(frameCount, 0);
  46. for (int i = 0; i < frameCount; ++i) {
  47. _attachmentNames.add(String());
  48. }
  49. }
  50. void AttachmentTimeline::apply(Skeleton &skeleton, float lastTime, float time, Vector<Event *> *pEvents, float alpha,
  51. MixBlend blend, MixDirection direction
  52. ) {
  53. SP_UNUSED(lastTime);
  54. SP_UNUSED(pEvents);
  55. SP_UNUSED(alpha);
  56. assert(_slotIndex < skeleton._slots.size());
  57. String *attachmentName;
  58. Slot *slotP = skeleton._slots[_slotIndex];
  59. Slot &slot = *slotP;
  60. if (!slot._bone.isActive()) return;
  61. if (direction == MixDirection_Out && blend == MixBlend_Setup) {
  62. attachmentName = &slot._data._attachmentName;
  63. slot.setAttachment(attachmentName->length() == 0 ? NULL : skeleton.getAttachment(_slotIndex, *attachmentName));
  64. return;
  65. }
  66. if (time < _frames[0]) {
  67. // Time is before first frame.
  68. if (blend == MixBlend_Setup || blend == MixBlend_First) {
  69. attachmentName = &slot._data._attachmentName;
  70. slot.setAttachment(attachmentName->length() == 0 ? NULL : skeleton.getAttachment(_slotIndex, *attachmentName));
  71. }
  72. return;
  73. }
  74. size_t frameIndex;
  75. if (time >= _frames[_frames.size() - 1]) {
  76. // Time is after last frame.
  77. frameIndex = _frames.size() - 1;
  78. } else {
  79. frameIndex = Animation::binarySearch(_frames, time, 1) - 1;
  80. }
  81. attachmentName = &_attachmentNames[frameIndex];
  82. slot.setAttachment(attachmentName->length() == 0 ? NULL : skeleton.getAttachment(_slotIndex, *attachmentName));
  83. }
  84. int AttachmentTimeline::getPropertyId() {
  85. return ((int) TimelineType_Attachment << 24) + _slotIndex;
  86. }
  87. void AttachmentTimeline::setFrame(int frameIndex, float time, const String &attachmentName) {
  88. _frames[frameIndex] = time;
  89. _attachmentNames[frameIndex] = attachmentName;
  90. }
  91. size_t AttachmentTimeline::getSlotIndex() {
  92. return _slotIndex;
  93. }
  94. void AttachmentTimeline::setSlotIndex(size_t inValue) {
  95. _slotIndex = inValue;
  96. }
  97. const Vector<float> &AttachmentTimeline::getFrames() {
  98. return _frames;
  99. }
  100. const Vector<String> &AttachmentTimeline::getAttachmentNames() {
  101. return _attachmentNames;
  102. }
  103. size_t AttachmentTimeline::getFrameCount() {
  104. return _frames.size();
  105. }