RotateTimeline.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/RotateTimeline.h>
  33. #include <spine/Skeleton.h>
  34. #include <spine/Event.h>
  35. #include <spine/Bone.h>
  36. #include <spine/BoneData.h>
  37. #include <spine/Animation.h>
  38. #include <spine/TimelineType.h>
  39. using namespace spine;
  40. RTTI_IMPL(RotateTimeline, CurveTimeline)
  41. RotateTimeline::RotateTimeline(int frameCount) : CurveTimeline(frameCount), _boneIndex(0) {
  42. _frames.setSize(frameCount << 1, 0);
  43. }
  44. void RotateTimeline::apply(Skeleton &skeleton, float lastTime, float time, Vector<Event *> *pEvents, float alpha,
  45. MixBlend blend, MixDirection direction
  46. ) {
  47. SP_UNUSED(lastTime);
  48. SP_UNUSED(pEvents);
  49. SP_UNUSED(direction);
  50. Bone *bone = skeleton.getBones()[_boneIndex];
  51. if (!bone->_active) return;
  52. if (time < _frames[0]) {
  53. switch (blend) {
  54. case MixBlend_Setup: {
  55. bone->_rotation = bone->_data._rotation;
  56. break;
  57. }
  58. case MixBlend_First: {
  59. float r = bone->_data._rotation - bone->_rotation;
  60. bone->_rotation += (r - (16384 - (int) (16384.499999999996 - r / 360)) * 360) * alpha;
  61. break;
  62. }
  63. default: {
  64. // TODO?
  65. break;
  66. }
  67. }
  68. return;
  69. }
  70. if (time >= _frames[_frames.size() - ENTRIES]) {
  71. float r = _frames[_frames.size() + PREV_ROTATION];
  72. switch (blend) {
  73. case MixBlend_Setup:
  74. bone->_rotation = bone->_data._rotation + r * alpha;
  75. break;
  76. case MixBlend_First:
  77. case MixBlend_Replace:
  78. r += bone->_data._rotation - bone->_rotation;
  79. r -= (16384 - (int)(16384.499999999996 - r / 360)) * 360;
  80. // Fall through.
  81. case MixBlend_Add:
  82. bone->_rotation += r * alpha;
  83. }
  84. return;
  85. }
  86. // Interpolate between the previous frame and the current frame.
  87. int frame = Animation::binarySearch(_frames, time, ENTRIES);
  88. float prevRotation = _frames[frame + PREV_ROTATION];
  89. float frameTime = _frames[frame];
  90. float percent = getCurvePercent((frame >> 1) - 1,
  91. 1 - (time - frameTime) / (_frames[frame + PREV_TIME] - frameTime));
  92. float r = _frames[frame + ROTATION] - prevRotation;
  93. r = prevRotation + (r - (16384 - (int)(16384.499999999996 - r / 360)) * 360) * percent;
  94. switch (blend) {
  95. case MixBlend_Setup:
  96. bone->_rotation = bone->_data._rotation + (r - (16384 - (int)(16384.499999999996 - r / 360)) * 360) * alpha;
  97. break;
  98. case MixBlend_First:
  99. case MixBlend_Replace:
  100. r += bone->_data._rotation - bone->_rotation;
  101. // Fall through.
  102. case MixBlend_Add:
  103. bone->_rotation += (r - (16384 - (int)(16384.499999999996 - r / 360)) * 360) * alpha;
  104. }
  105. }
  106. int RotateTimeline::getPropertyId() {
  107. return ((int) TimelineType_Rotate << 24) + _boneIndex;
  108. }
  109. void RotateTimeline::setFrame(int frameIndex, float time, float degrees) {
  110. frameIndex <<= 1;
  111. _frames[frameIndex] = time;
  112. _frames[frameIndex + ROTATION] = degrees;
  113. }
  114. int RotateTimeline::getBoneIndex() {
  115. return _boneIndex;
  116. }
  117. void RotateTimeline::setBoneIndex(int inValue) {
  118. _boneIndex = inValue;
  119. }
  120. Vector<float> &RotateTimeline::getFrames() {
  121. return _frames;
  122. }