Animation.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/Animation.h>
  33. #include <spine/Timeline.h>
  34. #include <spine/Skeleton.h>
  35. #include <spine/Event.h>
  36. #include <spine/ContainerUtil.h>
  37. #include <stdint.h>
  38. using namespace spine;
  39. Animation::Animation(const String &name, Vector<Timeline *> &timelines, float duration) :
  40. _timelines(timelines),
  41. _timelineIds(),
  42. _duration(duration),
  43. _name(name) {
  44. assert(_name.length() > 0);
  45. for (int i = 0; i < (int)timelines.size(); i++)
  46. _timelineIds.put(timelines[i]->getPropertyId(), true);
  47. }
  48. bool Animation::hasTimeline(int id) {
  49. return _timelineIds.containsKey(id);
  50. }
  51. Animation::~Animation() {
  52. ContainerUtil::cleanUpVectorOfPointers(_timelines);
  53. }
  54. void Animation::apply(Skeleton &skeleton, float lastTime, float time, bool loop, Vector<Event *> *pEvents, float alpha,
  55. MixBlend blend, MixDirection direction
  56. ) {
  57. if (loop && _duration != 0) {
  58. time = MathUtil::fmod(time, _duration);
  59. if (lastTime > 0) {
  60. lastTime = MathUtil::fmod(lastTime, _duration);
  61. }
  62. }
  63. for (size_t i = 0, n = _timelines.size(); i < n; ++i) {
  64. _timelines[i]->apply(skeleton, lastTime, time, pEvents, alpha, blend, direction);
  65. }
  66. }
  67. const String &Animation::getName() {
  68. return _name;
  69. }
  70. Vector<Timeline *> &Animation::getTimelines() {
  71. return _timelines;
  72. }
  73. float Animation::getDuration() {
  74. return _duration;
  75. }
  76. void Animation::setDuration(float inValue) {
  77. _duration = inValue;
  78. }
  79. int Animation::binarySearch(Vector<float> &values, float target, int step) {
  80. int low = 0;
  81. int size = (int)values.size();
  82. int high = size / step - 2;
  83. if (high == 0) {
  84. return step;
  85. }
  86. int current = (int) (static_cast<uint32_t>(high) >> 1);
  87. while (true) {
  88. if (values[(current + 1) * step] <= target)
  89. low = current + 1;
  90. else
  91. high = current;
  92. if (low == high) return (low + 1) * step;
  93. current = (int) (static_cast<uint32_t>(low + high) >> 1);
  94. }
  95. }
  96. int Animation::binarySearch(Vector<float> &values, float target) {
  97. int low = 0;
  98. int size = (int)values.size();
  99. int high = size - 2;
  100. if (high == 0) return 1;
  101. int current = (int) (static_cast<uint32_t>(high) >> 1);
  102. while (true) {
  103. if (values[(current + 1)] <= target)
  104. low = current + 1;
  105. else
  106. high = current;
  107. if (low == high) return (low + 1);
  108. current = (int) (static_cast<uint32_t>(low + high) >> 1);
  109. }
  110. }
  111. int Animation::linearSearch(Vector<float> &values, float target, int step) {
  112. for (int i = 0, last = (int)values.size() - step; i <= last; i += step) {
  113. if (values[i] > target) {
  114. return i;
  115. }
  116. }
  117. return -1;
  118. }