TransformConstraintTimeline.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/TransformConstraintTimeline.h>
  33. #include <spine/Skeleton.h>
  34. #include <spine/Event.h>
  35. #include <spine/Animation.h>
  36. #include <spine/TimelineType.h>
  37. #include <spine/Slot.h>
  38. #include <spine/SlotData.h>
  39. #include <spine/TransformConstraint.h>
  40. #include <spine/TransformConstraintData.h>
  41. using namespace spine;
  42. RTTI_IMPL(TransformConstraintTimeline, CurveTimeline)
  43. const int TransformConstraintTimeline::ENTRIES = 5;
  44. const int TransformConstraintTimeline::PREV_TIME = -5;
  45. const int TransformConstraintTimeline::PREV_ROTATE = -4;
  46. const int TransformConstraintTimeline::PREV_TRANSLATE = -3;
  47. const int TransformConstraintTimeline::PREV_SCALE = -2;
  48. const int TransformConstraintTimeline::PREV_SHEAR = -1;
  49. const int TransformConstraintTimeline::ROTATE = 1;
  50. const int TransformConstraintTimeline::TRANSLATE = 2;
  51. const int TransformConstraintTimeline::SCALE = 3;
  52. const int TransformConstraintTimeline::SHEAR = 4;
  53. TransformConstraintTimeline::TransformConstraintTimeline(int frameCount) : CurveTimeline(frameCount),
  54. _transformConstraintIndex(0)
  55. {
  56. _frames.setSize(frameCount * ENTRIES, 0);
  57. }
  58. void TransformConstraintTimeline::apply(Skeleton &skeleton, float lastTime, float time, Vector<Event *> *pEvents,
  59. float alpha, MixBlend blend, MixDirection direction) {
  60. SP_UNUSED(lastTime);
  61. SP_UNUSED(pEvents);
  62. SP_UNUSED(direction);
  63. TransformConstraint *constraintP = skeleton._transformConstraints[_transformConstraintIndex];
  64. TransformConstraint &constraint = *constraintP;
  65. if (!constraint.isActive()) return;
  66. if (time < _frames[0]) {
  67. switch (blend) {
  68. case MixBlend_Setup:
  69. constraint._rotateMix = constraint._data._rotateMix;
  70. constraint._translateMix = constraint._data._translateMix;
  71. constraint._scaleMix = constraint._data._scaleMix;
  72. constraint._shearMix = constraint._data._shearMix;
  73. return;
  74. case MixBlend_First:
  75. constraint._rotateMix += (constraint._data._rotateMix - constraint._rotateMix) * alpha;
  76. constraint._translateMix += (constraint._data._translateMix - constraint._translateMix) * alpha;
  77. constraint._scaleMix += (constraint._data._scaleMix - constraint._scaleMix) * alpha;
  78. constraint._shearMix += (constraint._data._shearMix - constraint._shearMix) * alpha;
  79. return;
  80. default:
  81. return;
  82. }
  83. }
  84. float rotate, translate, scale, shear;
  85. if (time >= _frames[_frames.size() - ENTRIES]) {
  86. // Time is after last frame.
  87. size_t i = _frames.size();
  88. rotate = _frames[i + PREV_ROTATE];
  89. translate = _frames[i + PREV_TRANSLATE];
  90. scale = _frames[i + PREV_SCALE];
  91. shear = _frames[i + PREV_SHEAR];
  92. } else {
  93. // Interpolate between the previous frame and the current frame.
  94. int frame = Animation::binarySearch(_frames, time, ENTRIES);
  95. rotate = _frames[frame + PREV_ROTATE];
  96. translate = _frames[frame + PREV_TRANSLATE];
  97. scale = _frames[frame + PREV_SCALE];
  98. shear = _frames[frame + PREV_SHEAR];
  99. float frameTime = _frames[frame];
  100. float percent = getCurvePercent(frame / ENTRIES - 1,
  101. 1 - (time - frameTime) / (_frames[frame + PREV_TIME] - frameTime));
  102. rotate += (_frames[frame + ROTATE] - rotate) * percent;
  103. translate += (_frames[frame + TRANSLATE] - translate) * percent;
  104. scale += (_frames[frame + SCALE] - scale) * percent;
  105. shear += (_frames[frame + SHEAR] - shear) * percent;
  106. }
  107. if (blend == MixBlend_Setup) {
  108. TransformConstraintData &data = constraint._data;
  109. constraint._rotateMix = data._rotateMix + (rotate - data._rotateMix) * alpha;
  110. constraint._translateMix = data._translateMix + (translate - data._translateMix) * alpha;
  111. constraint._scaleMix = data._scaleMix + (scale - data._scaleMix) * alpha;
  112. constraint._shearMix = data._shearMix + (shear - data._shearMix) * alpha;
  113. } else {
  114. constraint._rotateMix += (rotate - constraint._rotateMix) * alpha;
  115. constraint._translateMix += (translate - constraint._translateMix) * alpha;
  116. constraint._scaleMix += (scale - constraint._scaleMix) * alpha;
  117. constraint._shearMix += (shear - constraint._shearMix) * alpha;
  118. }
  119. }
  120. int TransformConstraintTimeline::getPropertyId() {
  121. return ((int) TimelineType_TransformConstraint << 24) + _transformConstraintIndex;
  122. }
  123. void TransformConstraintTimeline::setFrame(size_t frameIndex, float time, float rotateMix, float translateMix, float scaleMix,
  124. float shearMix
  125. ) {
  126. frameIndex *= ENTRIES;
  127. _frames[frameIndex] = time;
  128. _frames[frameIndex + ROTATE] = rotateMix;
  129. _frames[frameIndex + TRANSLATE] = translateMix;
  130. _frames[frameIndex + SCALE] = scaleMix;
  131. _frames[frameIndex + SHEAR] = shearMix;
  132. }