AnimationStateData.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. #include <spine/AnimationStateData.h>
  31. #include <spine/extension.h>
  32. typedef struct _ToEntry _ToEntry;
  33. struct _ToEntry {
  34. spAnimation* animation;
  35. float duration;
  36. _ToEntry* next;
  37. };
  38. _ToEntry* _ToEntry_create (spAnimation* to, float duration) {
  39. _ToEntry* self = NEW(_ToEntry);
  40. self->animation = to;
  41. self->duration = duration;
  42. return self;
  43. }
  44. void _ToEntry_dispose (_ToEntry* self) {
  45. FREE(self);
  46. }
  47. /**/
  48. typedef struct _FromEntry _FromEntry;
  49. struct _FromEntry {
  50. spAnimation* animation;
  51. _ToEntry* toEntries;
  52. _FromEntry* next;
  53. };
  54. _FromEntry* _FromEntry_create (spAnimation* from) {
  55. _FromEntry* self = NEW(_FromEntry);
  56. self->animation = from;
  57. return self;
  58. }
  59. void _FromEntry_dispose (_FromEntry* self) {
  60. FREE(self);
  61. }
  62. /**/
  63. spAnimationStateData* spAnimationStateData_create (spSkeletonData* skeletonData) {
  64. spAnimationStateData* self = NEW(spAnimationStateData);
  65. CONST_CAST(spSkeletonData*, self->skeletonData) = skeletonData;
  66. return self;
  67. }
  68. void spAnimationStateData_dispose (spAnimationStateData* self) {
  69. _ToEntry* toEntry;
  70. _ToEntry* nextToEntry;
  71. _FromEntry* nextFromEntry;
  72. _FromEntry* fromEntry = (_FromEntry*)self->entries;
  73. while (fromEntry) {
  74. toEntry = fromEntry->toEntries;
  75. while (toEntry) {
  76. nextToEntry = toEntry->next;
  77. _ToEntry_dispose(toEntry);
  78. toEntry = nextToEntry;
  79. }
  80. nextFromEntry = fromEntry->next;
  81. _FromEntry_dispose(fromEntry);
  82. fromEntry = nextFromEntry;
  83. }
  84. FREE(self);
  85. }
  86. void spAnimationStateData_setMixByName (spAnimationStateData* self, const char* fromName, const char* toName, float duration) {
  87. spAnimation* to;
  88. spAnimation* from = spSkeletonData_findAnimation(self->skeletonData, fromName);
  89. if (!from) return;
  90. to = spSkeletonData_findAnimation(self->skeletonData, toName);
  91. if (!to) return;
  92. spAnimationStateData_setMix(self, from, to, duration);
  93. }
  94. void spAnimationStateData_setMix (spAnimationStateData* self, spAnimation* from, spAnimation* to, float duration) {
  95. /* Find existing FromEntry. */
  96. _ToEntry* toEntry;
  97. _FromEntry* fromEntry = (_FromEntry*)self->entries;
  98. while (fromEntry) {
  99. if (fromEntry->animation == from) {
  100. /* Find existing ToEntry. */
  101. toEntry = fromEntry->toEntries;
  102. while (toEntry) {
  103. if (toEntry->animation == to) {
  104. toEntry->duration = duration;
  105. return;
  106. }
  107. toEntry = toEntry->next;
  108. }
  109. break; /* Add new ToEntry to the existing FromEntry. */
  110. }
  111. fromEntry = fromEntry->next;
  112. }
  113. if (!fromEntry) {
  114. fromEntry = _FromEntry_create(from);
  115. fromEntry->next = (_FromEntry*)self->entries;
  116. CONST_CAST(_FromEntry*, self->entries) = fromEntry;
  117. }
  118. toEntry = _ToEntry_create(to, duration);
  119. toEntry->next = fromEntry->toEntries;
  120. fromEntry->toEntries = toEntry;
  121. }
  122. float spAnimationStateData_getMix (spAnimationStateData* self, spAnimation* from, spAnimation* to) {
  123. _FromEntry* fromEntry = (_FromEntry*)self->entries;
  124. while (fromEntry) {
  125. if (fromEntry->animation == from) {
  126. _ToEntry* toEntry = fromEntry->toEntries;
  127. while (toEntry) {
  128. if (toEntry->animation == to) return toEntry->duration;
  129. toEntry = toEntry->next;
  130. }
  131. }
  132. fromEntry = fromEntry->next;
  133. }
  134. return self->defaultMix;
  135. }