AnimationStateData.c 4.8 KB

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