CurveTimeline.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/CurveTimeline.h>
  33. #include <spine/MathUtil.h>
  34. using namespace spine;
  35. RTTI_IMPL(CurveTimeline, Timeline)
  36. const float CurveTimeline::LINEAR = 0;
  37. const float CurveTimeline::STEPPED = 1;
  38. const float CurveTimeline::BEZIER = 2;
  39. const int CurveTimeline::BEZIER_SIZE = 10 * 2 - 1;
  40. CurveTimeline::CurveTimeline(int frameCount) {
  41. assert(frameCount > 0);
  42. _curves.setSize((frameCount - 1) * BEZIER_SIZE, 0);
  43. }
  44. CurveTimeline::~CurveTimeline() {
  45. }
  46. size_t CurveTimeline::getFrameCount() {
  47. return _curves.size() / BEZIER_SIZE + 1;
  48. }
  49. void CurveTimeline::setLinear(size_t frameIndex) {
  50. _curves[frameIndex * BEZIER_SIZE] = LINEAR;
  51. }
  52. void CurveTimeline::setStepped(size_t frameIndex) {
  53. _curves[frameIndex * BEZIER_SIZE] = STEPPED;
  54. }
  55. void CurveTimeline::setCurve(size_t frameIndex, float cx1, float cy1, float cx2, float cy2) {
  56. float tmpx = (-cx1 * 2 + cx2) * 0.03f, tmpy = (-cy1 * 2 + cy2) * 0.03f;
  57. float dddfx = ((cx1 - cx2) * 3 + 1) * 0.006f, dddfy = ((cy1 - cy2) * 3 + 1) * 0.006f;
  58. float ddfx = tmpx * 2 + dddfx, ddfy = tmpy * 2 + dddfy;
  59. float dfx = cx1 * 0.3f + tmpx + dddfx * 0.16666667f, dfy = cy1 * 0.3f + tmpy + dddfy * 0.16666667f;
  60. size_t i = frameIndex * BEZIER_SIZE;
  61. _curves[i++] = BEZIER;
  62. float x = dfx, y = dfy;
  63. for (size_t n = i + BEZIER_SIZE - 1; i < n; i += 2) {
  64. _curves[i] = x;
  65. _curves[i + 1] = y;
  66. dfx += ddfx;
  67. dfy += ddfy;
  68. ddfx += dddfx;
  69. ddfy += dddfy;
  70. x += dfx;
  71. y += dfy;
  72. }
  73. }
  74. float CurveTimeline::getCurvePercent(size_t frameIndex, float percent) {
  75. percent = MathUtil::clamp(percent, 0, 1);
  76. size_t i = frameIndex * BEZIER_SIZE;
  77. float type = _curves[i];
  78. if (type == LINEAR) {
  79. return percent;
  80. }
  81. if (type == STEPPED) {
  82. return 0;
  83. }
  84. i++;
  85. float x = 0;
  86. for (size_t start = i, n = i + BEZIER_SIZE - 1; i < n; i += 2) {
  87. x = _curves[i];
  88. if (x >= percent) {
  89. float prevX, prevY;
  90. if (i == start) {
  91. prevX = 0;
  92. prevY = 0;
  93. } else {
  94. prevX = _curves[i - 2];
  95. prevY = _curves[i - 1];
  96. }
  97. return prevY + (_curves[i + 1] - prevY) * (percent - prevX) / (x - prevX);
  98. }
  99. }
  100. float y = _curves[i - 1];
  101. return y + (1 - y) * (percent - x) / (1 - x); // Last point is 1,1.
  102. }
  103. float CurveTimeline::getCurveType(size_t frameIndex) {
  104. return _curves[frameIndex * BEZIER_SIZE];
  105. }