AnimationState.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. -------------------------------------------------------------------------------
  2. -- Spine Runtimes Software License
  3. -- Version 2.1
  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 (typically granted by licensing Spine), you
  12. -- may not (a) modify, translate, adapt or otherwise create derivative works,
  13. -- improvements of the Software or develop new applications using the Software
  14. -- or (b) remove, delete, alter or obscure any trademarks or any copyright,
  15. -- trademark, patent or other intellectual property or proprietary rights
  16. -- notices on or in the Software, including any copy thereof. Redistributions
  17. -- in binary or source 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 SOFTARE 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; LOSS OF USE, DATA, OR PROFITS;
  25. -- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. -- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. -- OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. -- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. -------------------------------------------------------------------------------
  30. local AnimationState = {}
  31. function AnimationState.new (data)
  32. if not data then error("data cannot be nil", 2) end
  33. local self = {
  34. data = data,
  35. tracks = {},
  36. trackCount = 0,
  37. events = {},
  38. onStart = nil, onEnd = nil, onComplete = nil, onEvent = nil,
  39. timeScale = 1
  40. }
  41. local function setCurrent (index, entry)
  42. local current = self.tracks[index]
  43. if current then
  44. local previous = current.previous
  45. current.previous = nil
  46. if current.onEnd then current.onEnd(index) end
  47. if self.onEnd then self.onEnd(index) end
  48. entry.mixDuration = self.data:getMix(current.animation.name, entry.animation.name)
  49. if entry.mixDuration > 0 then
  50. entry.mixTime = 0
  51. -- If a mix is in progress, mix from the closest animation.
  52. if previous and current.mixTime / current.mixDuration < 0.5 then
  53. entry.previous = previous
  54. else
  55. entry.previous = current
  56. end
  57. end
  58. end
  59. self.tracks[index] = entry
  60. self.trackCount = math.max(self.trackCount, index + 1)
  61. if entry.onStart then entry.onStart(index) end
  62. if self.onStart then self.onStart(index) end
  63. end
  64. function self:update (delta)
  65. delta = delta * self.timeScale
  66. for i = 0, self.trackCount - 1 do
  67. local current = self.tracks[i]
  68. if current then
  69. current.time = current.time + delta * current.timeScale
  70. if current.previous then
  71. local previousDelta = delta * current.previous.timeScale
  72. current.previous.time = current.previous.time + previousDelta
  73. current.mixTime = current.mixTime + previousDelta
  74. end
  75. local next = current.next
  76. if next then
  77. next.time = current.lastTime - next.delay
  78. if next.time >= 0 then setCurrent(i, next) end
  79. else
  80. -- End non-looping animation when it reaches its end time and there is no next entry.
  81. if not current.loop and current.lastTime >= current.endTime then self:clearTrack(i) end
  82. end
  83. end
  84. end
  85. end
  86. function self:apply(skeleton)
  87. for i = 0, self.trackCount - 1 do
  88. local current = self.tracks[i]
  89. if current then
  90. local time = current.time
  91. local lastTime = current.lastTime
  92. local endTime = current.endTime
  93. local loop = current.loop
  94. if not loop and time > endTime then time = endTime end
  95. local previous = current.previous
  96. if not previous then
  97. if current.mix == 1 then
  98. current.animation:apply(skeleton, current.lastTime, time, loop, self.events)
  99. else
  100. current.animation:mix(skeleton, current.lastTime, time, loop, self.events, current.mix)
  101. end
  102. else
  103. local previousTime = previous.time
  104. if not previous.loop and previousTime > previous.endTime then previousTime = previous.endTime end
  105. previous.animation:apply(skeleton, previousTime, previousTime, previous.loop, nil)
  106. local alpha = current.mixTime / current.mixDuration * current.mix
  107. if alpha >= 1 then
  108. alpha = 1
  109. current.previous = nil
  110. end
  111. current.animation:mix(skeleton, current.lastTime, time, loop, self.events, alpha)
  112. end
  113. local eventCount = #self.events
  114. for ii = 1, eventCount, 1 do
  115. local event = self.events[ii]
  116. if current.onEvent then current.onEvent(i, event) end
  117. if self.onEvent then self.onEvent(i, event) end
  118. end
  119. for ii = 1, eventCount, 1 do
  120. table.remove(self.events)
  121. end
  122. -- Check if completed the animation or a loop iteration.
  123. local complete
  124. if current.loop then
  125. complete = lastTime % endTime > time % endTime
  126. else
  127. complete = lastTime < endTime and time >= endTime
  128. end
  129. if complete then
  130. local count = math.floor(time / endTime)
  131. if current.onComplete then current.onComplete(i, count) end
  132. if self.onComplete then self.onComplete(i, count) end
  133. end
  134. current.lastTime = current.time
  135. end
  136. end
  137. end
  138. function self:clearTracks ()
  139. for i,current in pairs(self.tracks) do
  140. self:clearTrack(i)
  141. end
  142. self.tracks = {}
  143. self.trackCount = 0
  144. end
  145. function self:clearTrack (trackIndex)
  146. local current = self.tracks[trackIndex]
  147. if not current then return end
  148. if current.onEnd then current.onEnd(trackIndex) end
  149. if self.onEnd then self.onEnd(trackIndex) end
  150. self.tracks[trackIndex] = nil
  151. if trackIndex == self.trackCount - 1 then
  152. self.trackCount = self.trackCount - 1
  153. end
  154. end
  155. function self:setAnimationByName (trackIndex, animationName, loop)
  156. local animation = self.data.skeletonData:findAnimation(animationName)
  157. if not animation then error("Animation not found: " .. animationName) end
  158. return self:setAnimation(trackIndex, animation, loop)
  159. end
  160. -- Set the current animation. Any queued animations are cleared.
  161. function self:setAnimation (trackIndex, animation, loop)
  162. local entry = AnimationState.TrackEntry.new()
  163. entry.animation = animation
  164. entry.loop = loop
  165. entry.endTime = animation.duration
  166. setCurrent(trackIndex, entry)
  167. return entry
  168. end
  169. function self:addAnimationByName (trackIndex, animationName, loop, delay)
  170. local animation = self.data.skeletonData:findAnimation(animationName)
  171. if not animation then error("Animation not found: " .. animationName) end
  172. return self:addAnimation(trackIndex, animation, loop, delay)
  173. end
  174. -- Adds an animation to be played delay seconds after the current or last queued animation.
  175. -- @param delay May be <= 0 to use duration of previous animation minus any mix duration plus the negative delay.
  176. function self:addAnimation (trackIndex, animation, loop, delay)
  177. local entry = AnimationState.TrackEntry.new()
  178. entry.animation = animation
  179. entry.loop = loop
  180. entry.endTime = animation.duration
  181. local last = self.tracks[trackIndex]
  182. if last then
  183. while (last.next) do
  184. last = last.next
  185. end
  186. last.next = entry
  187. else
  188. setCurrent(trackIndex, entry)
  189. end
  190. delay = delay or 0
  191. if delay <= 0 then
  192. if last then
  193. delay = delay + last.endTime - self.data:getMix(last.animation.name, animation.name)
  194. else
  195. delay = 0
  196. end
  197. end
  198. entry.delay = delay
  199. return entry
  200. end
  201. -- May return nil.
  202. function self:getCurrent (trackIndex)
  203. return self.tracks[trackIndex]
  204. end
  205. return self
  206. end
  207. AnimationState.TrackEntry = {}
  208. function AnimationState.TrackEntry.new (data)
  209. local self = {
  210. next = nil, previous = nil,
  211. animation = nil,
  212. loop = false,
  213. delay = 0, time = 0, lastTime = -1, endTime = 0,
  214. timeScale = 1,
  215. mixTime = 0, mixDuration = 0, mix = 1,
  216. onStart = nil, onEnd = nil, onComplete = nil, onEvent = nil
  217. }
  218. return self
  219. end
  220. return AnimationState