IkConstraintTimeline.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/IkConstraintTimeline.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/IkConstraint.h>
  40. #include <spine/IkConstraintData.h>
  41. using namespace spine;
  42. RTTI_IMPL(IkConstraintTimeline, CurveTimeline)
  43. const int IkConstraintTimeline::ENTRIES = 6;
  44. const int IkConstraintTimeline::PREV_TIME = -6;
  45. const int IkConstraintTimeline::PREV_MIX = -5;
  46. const int IkConstraintTimeline::PREV_SOFTNESS = -4;
  47. const int IkConstraintTimeline::PREV_BEND_DIRECTION = -3;
  48. const int IkConstraintTimeline::PREV_COMPRESS = -2;
  49. const int IkConstraintTimeline::PREV_STRETCH = -1;
  50. const int IkConstraintTimeline::MIX = 1;
  51. const int IkConstraintTimeline::SOFTNESS = 2;
  52. const int IkConstraintTimeline::BEND_DIRECTION = 3;
  53. const int IkConstraintTimeline::COMPRESS = 4;
  54. const int IkConstraintTimeline::STRETCH = 5;
  55. IkConstraintTimeline::IkConstraintTimeline(int frameCount) : CurveTimeline(frameCount), _ikConstraintIndex(0) {
  56. _frames.setSize(frameCount * ENTRIES, 0);
  57. }
  58. void IkConstraintTimeline::apply(Skeleton &skeleton, float lastTime, float time, Vector<Event *> *pEvents, float alpha,
  59. MixBlend blend, MixDirection direction) {
  60. SP_UNUSED(lastTime);
  61. SP_UNUSED(pEvents);
  62. IkConstraint *constraintP = skeleton._ikConstraints[_ikConstraintIndex];
  63. IkConstraint &constraint = *constraintP;
  64. if (!constraint.isActive()) return;
  65. if (time < _frames[0]) {
  66. switch (blend) {
  67. case MixBlend_Setup:
  68. constraint._mix = constraint._data._mix;
  69. constraint._softness = constraint._data._softness;
  70. constraint._bendDirection = constraint._data._bendDirection;
  71. constraint._compress = constraint._data._compress;
  72. constraint._stretch = constraint._data._stretch;
  73. return;
  74. case MixBlend_First:
  75. constraint._mix += (constraint._data._mix - constraint._mix) * alpha;
  76. constraint._softness += (constraint._data._softness - constraint._softness) * alpha;
  77. constraint._bendDirection = constraint._data._bendDirection;
  78. constraint._compress = constraint._data._compress;
  79. constraint._stretch = constraint._data._stretch;
  80. return;
  81. default:
  82. return;
  83. }
  84. }
  85. if (time >= _frames[_frames.size() - ENTRIES]) {
  86. // Time is after last frame.
  87. if (blend == MixBlend_Setup) {
  88. constraint._mix =
  89. constraint._data._mix + (_frames[_frames.size() + PREV_MIX] - constraint._data._mix) * alpha;
  90. constraint._softness = constraint._data._softness
  91. + (_frames[_frames.size() + PREV_SOFTNESS] - constraint._data._softness) * alpha;
  92. if (direction == MixDirection_Out) {
  93. constraint._bendDirection = constraint._data._bendDirection;
  94. constraint._compress = constraint._data._compress;
  95. constraint._stretch = constraint._data._stretch;
  96. } else {
  97. constraint._bendDirection = (int) _frames[_frames.size() + PREV_BEND_DIRECTION];
  98. constraint._compress = _frames[_frames.size() + PREV_COMPRESS] != 0;
  99. constraint._stretch = _frames[_frames.size() + PREV_STRETCH] != 0;
  100. }
  101. } else {
  102. constraint._mix += (_frames[_frames.size() + PREV_MIX] - constraint._mix) * alpha;
  103. constraint._softness += (_frames[_frames.size() + PREV_SOFTNESS] - constraint._softness) * alpha;
  104. if (direction == MixDirection_In) {
  105. constraint._bendDirection = (int) _frames[_frames.size() + PREV_BEND_DIRECTION];
  106. constraint._compress = _frames[_frames.size() + PREV_COMPRESS] != 0;
  107. constraint._stretch = _frames[_frames.size() + PREV_STRETCH] != 0;
  108. }
  109. }
  110. return;
  111. }
  112. // Interpolate between the previous frame and the current frame.
  113. int frame = Animation::binarySearch(_frames, time, ENTRIES);
  114. float mix = _frames[frame + PREV_MIX];
  115. float softness = _frames[frame + PREV_SOFTNESS];
  116. float frameTime = _frames[frame];
  117. float percent = getCurvePercent(frame / ENTRIES - 1,
  118. 1 - (time - frameTime) / (_frames[frame + PREV_TIME] - frameTime));
  119. if (blend == MixBlend_Setup) {
  120. constraint._mix =
  121. constraint._data._mix + (mix + (_frames[frame + MIX] - mix) * percent - constraint._data._mix) * alpha;
  122. constraint._softness = constraint._data._softness
  123. + (softness + (_frames[frame + SOFTNESS] - softness) * percent - constraint._data._softness) * alpha;
  124. if (direction == MixDirection_Out) {
  125. constraint._bendDirection = constraint._data._bendDirection;
  126. constraint._compress = constraint._data._compress;
  127. constraint._stretch = constraint._data._stretch;
  128. } else {
  129. constraint._bendDirection = (int) _frames[_frames.size() + PREV_BEND_DIRECTION];
  130. constraint._compress = _frames[frame + PREV_COMPRESS] != 0;
  131. constraint._stretch = _frames[frame + PREV_STRETCH] != 0;
  132. }
  133. } else {
  134. constraint._mix += (mix + (_frames[frame + MIX] - mix) * percent - constraint._mix) * alpha;
  135. constraint._softness += (softness + (_frames[frame + SOFTNESS] - softness) * percent - constraint._softness) * alpha;
  136. if (direction == MixDirection_In) {
  137. constraint._bendDirection = (int) _frames[frame + PREV_BEND_DIRECTION];
  138. constraint._compress = _frames[frame + PREV_COMPRESS] != 0;
  139. constraint._stretch = _frames[frame + PREV_STRETCH] != 0;
  140. }
  141. }
  142. }
  143. int IkConstraintTimeline::getPropertyId() {
  144. return ((int) TimelineType_IkConstraint << 24) + _ikConstraintIndex;
  145. }
  146. void IkConstraintTimeline::setFrame(int frameIndex, float time, float mix, float softness, int bendDirection, bool compress, bool stretch) {
  147. frameIndex *= ENTRIES;
  148. _frames[frameIndex] = time;
  149. _frames[frameIndex + MIX] = mix;
  150. _frames[frameIndex + SOFTNESS] = softness;
  151. _frames[frameIndex + BEND_DIRECTION] = (float)bendDirection;
  152. _frames[frameIndex + COMPRESS] = compress ? 1 : 0;
  153. _frames[frameIndex + STRETCH] = stretch ? 1 : 0;
  154. }