Animation.lua 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  1. -------------------------------------------------------------------------------
  2. -- Spine Runtimes License Agreement
  3. -- Last updated May 1, 2019. Replaces all prior versions.
  4. --
  5. -- Copyright (c) 2013-2019, 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. -- THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY EXPRESS
  19. -- OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. -- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  21. -- NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. -- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. -- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS
  24. -- INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY
  25. -- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. -- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  27. -- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. -------------------------------------------------------------------------------
  29. -- FIXME
  30. -- All the indexing in this file is zero based. We use zlen()
  31. -- instead of the # operator. Initialization of number arrays
  32. -- is performed via utils.newNumberArrayZero. This needs
  33. -- to be rewritten using one-based indexing for better performance
  34. local utils = require "spine-lua.utils"
  35. local AttachmentType = require "spine-lua.attachments.AttachmentType"
  36. local math_floor = math.floor
  37. local math_abs = math.abs
  38. local math_signum = utils.signum
  39. local function zlen(array)
  40. return #array + 1
  41. end
  42. local Animation = {}
  43. function Animation.new (name, timelines, duration)
  44. if not timelines then error("timelines cannot be nil", 2) end
  45. local self = {
  46. name = name,
  47. timelines = timelines,
  48. duration = duration
  49. }
  50. function self:apply (skeleton, lastTime, time, loop, events, alpha, blend, direction)
  51. if not skeleton then error("skeleton cannot be nil.", 2) end
  52. if loop and duration > 0 then
  53. time = time % self.duration
  54. if lastTime > 0 then lastTime = lastTime % self.duration end
  55. end
  56. for i,timeline in ipairs(self.timelines) do
  57. timeline:apply(skeleton, lastTime, time, events, alpha, blend, direction)
  58. end
  59. end
  60. return self
  61. end
  62. local function binarySearch (values, target, step)
  63. local low = 0
  64. local high = math.floor(zlen(values) / step - 2)
  65. if high == 0 then return step end
  66. local current = math.floor(high / 2)
  67. while true do
  68. if values[(current + 1) * step] <= target then
  69. low = current + 1
  70. else
  71. high = current
  72. end
  73. if low == high then return (low + 1) * step end
  74. current = math.floor((low + high) / 2)
  75. end
  76. end
  77. Animation.binarySearch = binarySearch
  78. local function binarySearch1 (values, target)
  79. local low = 0
  80. local high = math.floor(zlen(values) - 2)
  81. if high == 0 then return 1 end
  82. local current = math.floor(high / 2)
  83. while true do
  84. if values[current + 1] <= target then
  85. low = current + 1
  86. else
  87. high = current
  88. end
  89. if low == high then return low + 1 end
  90. current = math.floor((low + high) / 2)
  91. end
  92. end
  93. local function linearSearch (values, target, step)
  94. local i = 0
  95. local last = zlen(values) - step
  96. while i <= last do
  97. if (values[i] > target) then return i end
  98. i = i + step
  99. end
  100. return -1
  101. end
  102. Animation.MixBlend = {
  103. setup = 0,
  104. first = 1,
  105. replace = 2,
  106. add = 3
  107. }
  108. local MixBlend = Animation.MixBlend
  109. Animation.MixDirection = {
  110. _in = 0, out = 1
  111. }
  112. local MixDirection = Animation.MixDirection
  113. Animation.TimelineType = {
  114. rotate = 0, translate = 1, scale = 2, shear = 3,
  115. attachment = 4, color = 5, deform = 6,
  116. event = 7, drawOrder = 8,
  117. ikConstraint = 9, transformConstraint = 10,
  118. pathConstraintPosition = 11, pathConstraintSpacing = 12, pathConstraintMix = 13,
  119. twoColor = 14
  120. }
  121. local TimelineType = Animation.TimelineType
  122. local SHL_24 = 16777216
  123. local SHL_27 = 134217728
  124. Animation.CurveTimeline = {}
  125. function Animation.CurveTimeline.new (frameCount)
  126. local LINEAR = 0
  127. local STEPPED = 1
  128. local BEZIER = 2
  129. local BEZIER_SIZE = 10 * 2 - 1
  130. local self = {
  131. curves = utils.newNumberArrayZero((frameCount - 1) * BEZIER_SIZE) -- type, x, y, ...
  132. }
  133. function self:getFrameCount ()
  134. return math.floor(zlen(self.curves) / BEZIER_SIZE) + 1
  135. end
  136. function self:setStepped (frameIndex)
  137. self.curves[frameIndex * BEZIER_SIZE] = STEPPED
  138. end
  139. function self:getCurveType (frameIndex)
  140. local index = frameIndex * BEZIER_SIZE
  141. if index == zlen(self.curves) then return LINEAR end
  142. local type = self.curves[index]
  143. if type == LINEAR then return LINEAR end
  144. if type == STEPPED then return STEPPED end
  145. return BEZIER
  146. end
  147. function self:setCurve (frameIndex, cx1, cy1, cx2, cy2)
  148. local tmpx = (-cx1 * 2 + cx2) * 0.03
  149. local tmpy = (-cy1 * 2 + cy2) * 0.03
  150. local dddfx = ((cx1 - cx2) * 3 + 1) * 0.006
  151. local dddfy = ((cy1 - cy2) * 3 + 1) * 0.006
  152. local ddfx = tmpx * 2 + dddfx
  153. local ddfy = tmpy * 2 + dddfy
  154. local dfx = cx1 * 0.3 + tmpx + dddfx * 0.16666667
  155. local dfy = cy1 * 0.3 + tmpy + dddfy * 0.16666667
  156. local i = frameIndex * BEZIER_SIZE
  157. local curves = self.curves
  158. curves[i] = BEZIER
  159. i = i + 1
  160. local x = dfx
  161. local y = dfy
  162. local n = i + BEZIER_SIZE - 1
  163. while i < n do
  164. curves[i] = x
  165. curves[i + 1] = y
  166. dfx = dfx + ddfx
  167. dfy = dfy + ddfy
  168. ddfx = ddfx + dddfx
  169. ddfy = ddfy + dddfy
  170. x = x + dfx
  171. y = y + dfy
  172. i = i + 2
  173. end
  174. end
  175. function self:getCurvePercent (frameIndex, percent)
  176. percent = utils.clamp(percent, 0, 1)
  177. local curves = self.curves
  178. local i = frameIndex * BEZIER_SIZE
  179. local type = curves[i]
  180. if type == LINEAR then return percent end
  181. if type == STEPPED then return 0 end
  182. i = i + 1
  183. local x
  184. local n = i + BEZIER_SIZE - 1
  185. local start = i
  186. while i < n do
  187. x = curves[i]
  188. if x >= percent then
  189. local prevX, prevY
  190. if i == start then
  191. prevX = 0
  192. prevY = 0
  193. else
  194. prevX = curves[i - 2]
  195. prevY = curves[i - 1]
  196. end
  197. return prevY + (curves[i + 1] - prevY) * (percent - prevX) / (x - prevX)
  198. end
  199. i = i + 2
  200. end
  201. local y = curves[i - 1]
  202. return y + (1 - y) * (percent - x) / (1 - x) -- Last point is 1,1.
  203. end
  204. return self
  205. end
  206. Animation.RotateTimeline = {}
  207. Animation.RotateTimeline.ENTRIES = 2
  208. Animation.RotateTimeline.PREV_TIME = -2
  209. Animation.RotateTimeline.PREV_ROTATION = -1
  210. Animation.RotateTimeline.ROTATION = 1
  211. function Animation.RotateTimeline.new (frameCount)
  212. local ENTRIES = Animation.RotateTimeline.ENTRIES
  213. local PREV_TIME = Animation.RotateTimeline.PREV_TIME
  214. local PREV_ROTATION = Animation.RotateTimeline.PREV_ROTATION
  215. local ROTATION = Animation.RotateTimeline.ROTATION
  216. local self = Animation.CurveTimeline.new(frameCount)
  217. self.boneIndex = -1
  218. self.frames = utils.newNumberArrayZero(frameCount * 2)
  219. self.type = TimelineType.rotate
  220. function self:getPropertyId ()
  221. return TimelineType.rotate * SHL_24 + self.boneIndex
  222. end
  223. function self:setFrame (frameIndex, time, degrees)
  224. frameIndex = frameIndex * 2
  225. self.frames[frameIndex] = time
  226. self.frames[frameIndex + ROTATION] = degrees
  227. end
  228. function self:apply (skeleton, lastTime, time, firedEvents, alpha, blend, direction)
  229. local frames = self.frames
  230. local bone = skeleton.bones[self.boneIndex]
  231. if time < frames[0] then
  232. if blend == MixBlend.setup then
  233. bone.rotation = bone.data.rotation
  234. elseif blend == MixBlend.first then
  235. local r = bone.data.rotation - bone.rotation
  236. bone.rotation = bone.rotation + (r - (16384 - math_floor(16384.499999999996 - r / 360)) * 360) * alpha
  237. end
  238. return
  239. end
  240. if time >= frames[zlen(frames) - ENTRIES] then -- Time is after last frame.
  241. local r = frames[zlen(frames) + PREV_ROTATION]
  242. if blend == MixBlend.setup then
  243. bone.rotation = bone.data.rotation + r * alpha
  244. elseif blend == MixBlend.first or blend == MixBlend.replace then
  245. r = r + bone.data.rotation - bone.rotation
  246. r = r - (16384 - math_floor(16384.499999999996 - r / 360)) * 360 -- Wrap within -180 and 180.
  247. bone.rotation = bone.rotation + r * alpha;
  248. elseif blend == MixBlend.add then
  249. bone.rotation = bone.rotation + r * alpha;
  250. end
  251. return;
  252. end
  253. -- Interpolate between the last frame and the current frame.
  254. local frame = binarySearch(frames, time, ENTRIES)
  255. local prevRotation = frames[frame + PREV_ROTATION]
  256. local frameTime = frames[frame]
  257. local percent = self:getCurvePercent((math.floor(frame / 2)) - 1, 1 - (time - frameTime) / (frames[frame + PREV_TIME] - frameTime))
  258. local r = frames[frame + ROTATION] - prevRotation
  259. r = prevRotation + (r - (16384 - math_floor(16384.499999999996 - r / 360)) * 360) * percent
  260. if blend == MixBlend.setup then
  261. bone.rotation = bone.data.rotation + (r - (16384 - math_floor(16384.499999999996 - r / 360)) * 360) * alpha
  262. elseif blend == MixBlend.first or blend == MixBlend.replace then
  263. r = r + bone.data.rotation - bone.rotation;
  264. bone.rotation = bone.rotation + (r - (16384 - math_floor(16384.499999999996 - r / 360)) * 360) * alpha
  265. elseif blend == MixBlend.add then
  266. bone.rotation = bone.rotation + (r - (16384 - math_floor(16384.499999999996 - r / 360)) * 360) * alpha
  267. end
  268. end
  269. return self
  270. end
  271. Animation.TranslateTimeline = {}
  272. Animation.TranslateTimeline.ENTRIES = 3
  273. function Animation.TranslateTimeline.new (frameCount)
  274. local ENTRIES = Animation.TranslateTimeline.ENTRIES
  275. local PREV_TIME = -3
  276. local PREV_X = -2
  277. local PREV_Y = -1
  278. local X = 1
  279. local Y = 2
  280. local self = Animation.CurveTimeline.new(frameCount)
  281. self.frames = utils.newNumberArrayZero(frameCount * ENTRIES)
  282. self.boneIndex = -1
  283. self.type = TimelineType.translate
  284. function self:getPropertyId ()
  285. return TimelineType.translate * SHL_24 + self.boneIndex
  286. end
  287. function self:setFrame (frameIndex, time, x, y)
  288. frameIndex = frameIndex * ENTRIES
  289. self.frames[frameIndex] = time
  290. self.frames[frameIndex + X] = x
  291. self.frames[frameIndex + Y] = y
  292. end
  293. function self:apply (skeleton, lastTime, time, firedEvents, alpha, blend, direction)
  294. local frames = self.frames
  295. local bone = skeleton.bones[self.boneIndex]
  296. if time < frames[0] then
  297. if blend == MixBlend.setup then
  298. bone.x = bone.data.x
  299. bone.y = bone.data.y
  300. elseif blend == MixBlend.first then
  301. bone.x = bone.x + (bone.data.x - bone.x) * alpha
  302. bone.y = bone.y + (bone.data.y - bone.y) * alpha
  303. end
  304. return
  305. end
  306. local x = 0
  307. local y = 0
  308. if time >= frames[zlen(frames) - ENTRIES] then -- // Time is after last frame.
  309. x = frames[zlen(frames) + PREV_X];
  310. y = frames[zlen(frames) + PREV_Y];
  311. else
  312. -- Interpolate between the previous frame and the current frame.
  313. local frame = binarySearch(frames, time, ENTRIES)
  314. x = frames[frame + PREV_X]
  315. y = frames[frame + PREV_Y]
  316. local frameTime = frames[frame]
  317. local percent = self:getCurvePercent(math_floor(frame / ENTRIES) - 1,
  318. 1 - (time - frameTime) / (frames[frame + PREV_TIME] - frameTime));
  319. x = x + (frames[frame + X] - x) * percent
  320. y = y + (frames[frame + Y] - y) * percent
  321. end
  322. if blend == MixBlend.setup then
  323. bone.x = bone.data.x + x * alpha
  324. bone.y = bone.data.y + y * alpha
  325. elseif blend == MixBlend.first or blend == MixBlend.replace then
  326. bone.x = bone.x + (bone.data.x + x - bone.x) * alpha
  327. bone.y = bone.y + (bone.data.y + y - bone.y) * alpha
  328. elseif blend == MixBlend.add then
  329. bone.x = bone.x + x * alpha
  330. bone.y = bone.y + y * alpha
  331. end
  332. end
  333. return self
  334. end
  335. Animation.ScaleTimeline = {}
  336. Animation.ScaleTimeline.ENTRIES = Animation.TranslateTimeline.ENTRIES
  337. function Animation.ScaleTimeline.new (frameCount)
  338. local ENTRIES = Animation.ScaleTimeline.ENTRIES
  339. local PREV_TIME = -3
  340. local PREV_X = -2
  341. local PREV_Y = -1
  342. local X = 1
  343. local Y = 2
  344. local self = Animation.TranslateTimeline.new(frameCount)
  345. self.type = TimelineType.scale
  346. function self:getPropertyId ()
  347. return TimelineType.scale * SHL_24 + self.boneIndex
  348. end
  349. function self:apply (skeleton, lastTime, time, firedEvents, alpha, blend, direction)
  350. local frames = self.frames
  351. local bone = skeleton.bones[self.boneIndex]
  352. if time < frames[0] then
  353. if blend == MixBlend.setup then
  354. bone.scaleX = bone.data.scaleX
  355. bone.scaleY = bone.data.scaleY
  356. elseif blend == MixBlend.first then
  357. bone.scaleX = bone.scaleX + (bone.data.scaleX - bone.scaleX) * alpha
  358. bone.scaleY = bone.scaleY + (bone.data.scaleY - bone.scaleY) * alpha
  359. end
  360. return
  361. end
  362. local x = 0
  363. local y = 0
  364. if time >= frames[zlen(frames) - ENTRIES] then -- Time is after last frame.
  365. x = frames[zlen(frames) + PREV_X] * bone.data.scaleX
  366. y = frames[zlen(frames) + PREV_Y] * bone.data.scaleY
  367. else
  368. -- Interpolate between the previous frame and the current frame.
  369. local frame = binarySearch(frames, time, ENTRIES)
  370. x = frames[frame + PREV_X]
  371. y = frames[frame + PREV_Y]
  372. local frameTime = frames[frame]
  373. local percent = self:getCurvePercent(math_floor(frame / ENTRIES) - 1,
  374. 1 - (time - frameTime) / (frames[frame + PREV_TIME] - frameTime))
  375. x = (x + (frames[frame + X] - x) * percent) * bone.data.scaleX
  376. y = (y + (frames[frame + Y] - y) * percent) * bone.data.scaleY
  377. end
  378. if alpha == 1 then
  379. if blend == MixBlend.add then
  380. bone.scaleX = bone.scaleX + x - bone.data.scaleX
  381. bone.scaleY = bone.scaleY + y - bone.data.scaleY
  382. else
  383. bone.scaleX = x
  384. bone.scaleY = y
  385. end
  386. else
  387. local bx = 0
  388. local by = 0
  389. if direction == MixDirection.out then
  390. if blend == MixBlend.setup then
  391. bx = bone.data.scaleX
  392. by = bone.data.scaleY
  393. bone.scaleX = bx + (math_abs(x) * math_signum(bx) - bx) * alpha
  394. bone.scaleY = by + (math_abs(y) * math_signum(by) - by) * alpha
  395. elseif blend == MixBlend.first or blend == MixBlend.replace then
  396. bx = bone.scaleX
  397. by = bone.scaleY
  398. bone.scaleX = bx + (math_abs(x) * math_signum(bx) - bx) * alpha
  399. bone.scaleY = by + (math_abs(y) * math_signum(by) - by) * alpha
  400. elseif blend == MixBlend.add then
  401. bx = bone.scaleX
  402. by = bone.scaleY
  403. bone.scaleX = bx + (math_abs(x) * math_signum(bx) - bone.data.scaleX) * alpha
  404. bone.scaleY = by + (math_abs(y) * math_signum(by) - bone.data.scaleY) * alpha
  405. end
  406. else
  407. if blend == MixBlend.setup then
  408. bx = math_abs(bone.data.scaleX) * math_signum(x)
  409. by = math_abs(bone.data.scaleY) * math_signum(y)
  410. bone.scaleX = bx + (x - bx) * alpha
  411. bone.scaleY = by + (y - by) * alpha
  412. elseif blend == MixBlend.first or blend == MixBlend.replace then
  413. bx = math_abs(bone.scaleX) * math_signum(x)
  414. by = math_abs(bone.scaleY) * math_signum(y)
  415. bone.scaleX = bx + (x - bx) * alpha
  416. bone.scaleY = by + (y - by) * alpha
  417. elseif blend == MixBlend.add then
  418. bx = math_signum(x)
  419. by = math_signum(y)
  420. bone.scaleX = math_abs(bone.scaleX) * bx + (x - math_abs(bone.data.scaleX) * bx) * alpha
  421. bone.scaleY = math_abs(bone.scaleY) * by + (y - math_abs(bone.data.scaleY) * by) * alpha
  422. end
  423. end
  424. end
  425. end
  426. return self
  427. end
  428. Animation.ShearTimeline = {}
  429. Animation.ShearTimeline.ENTRIES = Animation.TranslateTimeline.ENTRIES
  430. function Animation.ShearTimeline.new (frameCount)
  431. local ENTRIES = Animation.ShearTimeline.ENTRIES
  432. local PREV_TIME = -3
  433. local PREV_X = -2
  434. local PREV_Y = -1
  435. local X = 1
  436. local Y = 2
  437. local self = Animation.TranslateTimeline.new(frameCount)
  438. self.type = TimelineType.shear
  439. function self:getPropertyId ()
  440. return TimelineType.shear * SHL_24 + self.boneIndex
  441. end
  442. function self:apply (skeleton, lastTime, time, firedEvents, alpha, blend, direction)
  443. local frames = self.frames
  444. local bone = skeleton.bones[self.boneIndex]
  445. if time < frames[0] then
  446. if blend == MixBlend.setup then
  447. bone.shearX = bone.data.shearX
  448. bone.shearY = bone.data.shearY
  449. elseif blend == MixBlend.first then
  450. bone.shearX = bone.shearX + (bone.data.shearX - bone.shearX) * alpha
  451. bone.shearY = bone.shearX + (bone.data.shearY - bone.shearY) * alpha
  452. end
  453. return
  454. end
  455. local x = 0
  456. local y = 0
  457. if time >= frames[zlen(frames) - ENTRIES] then -- // Time is after last frame.
  458. x = frames[zlen(frames) + PREV_X]
  459. y = frames[zlen(frames) + PREV_Y]
  460. else
  461. -- Interpolate between the previous frame and the current frame.
  462. local frame = binarySearch(frames, time, ENTRIES)
  463. x = frames[frame + PREV_X]
  464. y = frames[frame + PREV_Y]
  465. local frameTime = frames[frame]
  466. local percent = self:getCurvePercent(math_floor(frame / ENTRIES) - 1,
  467. 1 - (time - frameTime) / (frames[frame + PREV_TIME] - frameTime))
  468. x = x + (frames[frame + X] - x) * percent
  469. y = y + (frames[frame + Y] - y) * percent
  470. end
  471. if blend == MixBlend.setup then
  472. bone.shearX = bone.data.shearX + x * alpha
  473. bone.shearY = bone.data.shearY + y * alpha
  474. elseif blend == MixBlend.first or blend == MixBlend.replace then
  475. bone.shearX = bone.shearX + (bone.data.shearX + x - bone.shearX) * alpha
  476. bone.shearY = bone.shearY + (bone.data.shearY + y - bone.shearY) * alpha
  477. elseif blend == MixBlend.add then
  478. bone.shearX = bone.shearX + x * alpha
  479. bone.shearY = bone.shearY + y * alpha
  480. end
  481. end
  482. return self
  483. end
  484. Animation.ColorTimeline = {}
  485. Animation.ColorTimeline.ENTRIES = 5
  486. function Animation.ColorTimeline.new (frameCount)
  487. local ENTRIES = Animation.ColorTimeline.ENTRIES
  488. local PREV_TIME = -5
  489. local PREV_R = -4
  490. local PREV_G = -3
  491. local PREV_B = -2
  492. local PREV_A = -1
  493. local R = 1
  494. local G = 2
  495. local B = 3
  496. local A = 4
  497. local self = Animation.CurveTimeline.new(frameCount)
  498. self.frames = utils.newNumberArrayZero(frameCount * ENTRIES)
  499. self.slotIndex = -1
  500. self.type = TimelineType.color
  501. function self:getPropertyId ()
  502. return TimelineType.color * SHL_24 + self.slotIndex
  503. end
  504. function self:setFrame (frameIndex, time, r, g, b, a)
  505. frameIndex = frameIndex * ENTRIES
  506. self.frames[frameIndex] = time
  507. self.frames[frameIndex + R] = r
  508. self.frames[frameIndex + G] = g
  509. self.frames[frameIndex + B] = b
  510. self.frames[frameIndex + A] = a
  511. end
  512. function self:apply (skeleton, lastTime, time, firedEvents, alpha, blend, direction)
  513. local frames = self.frames
  514. local slot = skeleton.slots[self.slotIndex]
  515. if time < frames[0] then
  516. if blend == MixBlend.setup then
  517. slot.color:setFrom(slot.data.color)
  518. elseif blend == MixBlend.first then
  519. local color = slot.color
  520. local setup = slot.data.color
  521. color:add((setup.r - color.r) * alpha, (setup.g - color.g) * alpha, (setup.b - color.b) * alpha,
  522. (setup.a - color.a) * alpha)
  523. end
  524. return
  525. end
  526. local r, g, b, a
  527. if time >= frames[zlen(frames) - ENTRIES] then -- Time is after last frame.
  528. local i = zlen(frames)
  529. r = frames[i + PREV_R]
  530. g = frames[i + PREV_G]
  531. b = frames[i + PREV_B]
  532. a = frames[i + PREV_A]
  533. else
  534. -- Interpolate between the last frame and the current frame.
  535. local frame = binarySearch(frames, time, ENTRIES)
  536. r = frames[frame + PREV_R]
  537. g = frames[frame + PREV_G]
  538. b = frames[frame + PREV_B]
  539. a = frames[frame + PREV_A]
  540. local frameTime = frames[frame]
  541. local percent = self:getCurvePercent(math.floor(frame / ENTRIES) - 1,
  542. 1 - (time - frameTime) / (frames[frame + PREV_TIME] - frameTime))
  543. r = r + (frames[frame + R] - r) * percent
  544. g = g + (frames[frame + G] - g) * percent
  545. b = b + (frames[frame + B] - b) * percent
  546. a = a + (frames[frame + A] - a) * percent
  547. end
  548. if alpha == 1 then
  549. slot.color:set(r, g, b, a)
  550. else
  551. local color = slot.color
  552. if blend == MixBlend.setup then color:setFrom(slot.data.color) end
  553. color:add((r - color.r) * alpha, (g - color.g) * alpha, (b - color.b) * alpha, (a - color.a) * alpha)
  554. end
  555. end
  556. return self
  557. end
  558. Animation.TwoColorTimeline = {}
  559. Animation.TwoColorTimeline.ENTRIES = 8
  560. function Animation.TwoColorTimeline.new (frameCount)
  561. local ENTRIES = Animation.TwoColorTimeline.ENTRIES
  562. local PREV_TIME = -8
  563. local PREV_R = -7
  564. local PREV_G = -6
  565. local PREV_B = -5
  566. local PREV_A = -4
  567. local PREV_R2 = -3
  568. local PREV_G2 = -2
  569. local PREV_B2 = -1
  570. local R = 1
  571. local G = 2
  572. local B = 3
  573. local A = 4
  574. local R2 = 5
  575. local G2 = 6
  576. local B2 = 7
  577. local self = Animation.CurveTimeline.new(frameCount)
  578. self.frames = utils.newNumberArrayZero(frameCount * ENTRIES)
  579. self.slotIndex = -1
  580. self.type = TimelineType.twoColor
  581. function self:getPropertyId ()
  582. return TimelineType.twoColor * SHL_24 + self.slotIndex
  583. end
  584. function self:setFrame (frameIndex, time, r, g, b, a, r2, g2, b2)
  585. frameIndex = frameIndex * ENTRIES
  586. self.frames[frameIndex] = time
  587. self.frames[frameIndex + R] = r
  588. self.frames[frameIndex + G] = g
  589. self.frames[frameIndex + B] = b
  590. self.frames[frameIndex + A] = a
  591. self.frames[frameIndex + R2] = r2
  592. self.frames[frameIndex + G2] = g2
  593. self.frames[frameIndex + B2] = b2
  594. end
  595. function self:apply (skeleton, lastTime, time, firedEvents, alpha, blend, direction)
  596. local frames = self.frames
  597. local slot = skeleton.slots[self.slotIndex]
  598. if time < frames[0] then
  599. if blend == MixBlend.setup then
  600. slot.color:setFrom(slot.data.color)
  601. slot.darkColor:setFrom(slot.data.darkColor)
  602. elseif blend == MixBlend.first then
  603. local light = slot.color
  604. local dark = slot.darkColor
  605. local setupLight = slot.data.color
  606. local setupDark = slot.data.darkColor
  607. light:add((setupLight.r - light.r) * alpha, (setupLight.g - light.g) * alpha, (setupLight.b - light.b) * alpha,
  608. (setupLight.a - light.a) * alpha)
  609. dark:add((setupDark.r - dark.r) * alpha, (setupDark.g - dark.g) * alpha, (setupDark.b - dark.b) * alpha, 0)
  610. end
  611. return
  612. end
  613. local r, g, b, a, r2, g2, b2
  614. if time >= frames[zlen(frames) - ENTRIES] then -- Time is after last frame.
  615. local i = zlen(frames)
  616. r = frames[i + PREV_R]
  617. g = frames[i + PREV_G]
  618. b = frames[i + PREV_B]
  619. a = frames[i + PREV_A]
  620. r2 = frames[i + PREV_R2]
  621. g2 = frames[i + PREV_G2]
  622. b2 = frames[i + PREV_B2]
  623. else
  624. -- Interpolate between the last frame and the current frame.
  625. local frame = binarySearch(frames, time, ENTRIES)
  626. r = frames[frame + PREV_R]
  627. g = frames[frame + PREV_G]
  628. b = frames[frame + PREV_B]
  629. a = frames[frame + PREV_A]
  630. r2 = frames[frame + PREV_R2]
  631. g2 = frames[frame + PREV_G2]
  632. b2 = frames[frame + PREV_B2]
  633. local frameTime = frames[frame]
  634. local percent = self:getCurvePercent(math.floor(frame / ENTRIES) - 1,
  635. 1 - (time - frameTime) / (frames[frame + PREV_TIME] - frameTime))
  636. r = r + (frames[frame + R] - r) * percent
  637. g = g + (frames[frame + G] - g) * percent
  638. b = b + (frames[frame + B] - b) * percent
  639. a = a + (frames[frame + A] - a) * percent
  640. r2 = r2 + (frames[frame + R2] - r2) * percent
  641. g2 = g2 + (frames[frame + G2] - g2) * percent
  642. b2 = b2 + (frames[frame + B2] - b2) * percent
  643. end
  644. if alpha == 1 then
  645. slot.color:set(r, g, b, a)
  646. slot.darkColor:set(r2, g2, b2, 1)
  647. else
  648. local light = slot.color
  649. local dark = slot.darkColor
  650. if blend == MixBlend.setup then
  651. light:setFrom(slot.data.color)
  652. dark:setFrom(slot.data.darkColor)
  653. end
  654. light:add((r - light.r) * alpha, (g - light.g) * alpha, (b - light.b) * alpha, (a - light.a) * alpha)
  655. dark:add((r2 - dark.r) * alpha, (g2 - dark.g) * alpha, (b2 - dark.b) * alpha, 0)
  656. end
  657. end
  658. return self
  659. end
  660. Animation.AttachmentTimeline = {}
  661. function Animation.AttachmentTimeline.new (frameCount)
  662. local self = {
  663. frames = utils.newNumberArrayZero(frameCount), -- time, ...
  664. attachmentNames = {},
  665. slotIndex = -1,
  666. type = TimelineType.attachment
  667. }
  668. function self:getFrameCount ()
  669. return zlen(self.frames)
  670. end
  671. function self:setFrame (frameIndex, time, attachmentName)
  672. self.frames[frameIndex] = time
  673. self.attachmentNames[frameIndex] = attachmentName
  674. end
  675. function self:getPropertyId ()
  676. return TimelineType.attachment * SHL_24 + self.slotIndex
  677. end
  678. function self:apply (skeleton, lastTime, time, firedEvents, alpha, blend, direction)
  679. local slot = skeleton.slots[self.slotIndex]
  680. local attachmentName
  681. if direction == MixDirection.out and blend == MixBlend.setup then
  682. attachmentName = slot.data.attachmentName
  683. if not attachmentName then
  684. slot:setAttachment(nil)
  685. else
  686. slot:setAttachment(skeleton:getAttachmentByIndex(self.slotIndex, attachmentName))
  687. end
  688. return;
  689. end
  690. local frames = self.frames
  691. if time < frames[0] then
  692. if blend == MixBlend.setup or blend == MixBlend.first then
  693. attachmentName = slot.data.attachmentName
  694. if not attachmentName then
  695. slot:setAttachment(nil)
  696. else
  697. slot:setAttachment(skeleton:getAttachmentByIndex(self.slotIndex, attachmentName))
  698. end
  699. end
  700. return
  701. end
  702. local frameIndex = 0
  703. if time >= frames[zlen(frames) - 1] then
  704. frameIndex = zlen(frames) - 1
  705. else
  706. frameIndex = binarySearch(frames, time, 1) - 1
  707. end
  708. attachmentName = self.attachmentNames[frameIndex]
  709. if not attachmentName then
  710. slot:setAttachment(nil)
  711. else
  712. slot:setAttachment(skeleton:getAttachmentByIndex(self.slotIndex, attachmentName))
  713. end
  714. end
  715. return self
  716. end
  717. Animation.DeformTimeline = {}
  718. function Animation.DeformTimeline.new (frameCount)
  719. local self = Animation.CurveTimeline.new(frameCount)
  720. self.frames = utils.newNumberArrayZero(frameCount)
  721. self.frameVertices = utils.newNumberArrayZero(frameCount)
  722. self.slotIndex = -1
  723. self.attachment = nil
  724. self.type = TimelineType.deform
  725. function self:getPropertyId ()
  726. return TimelineType.deform * SHL_27 + self.attachment.id + self.slotIndex
  727. end
  728. function self:setFrame (frameIndex, time, vertices)
  729. self.frames[frameIndex] = time
  730. self.frameVertices[frameIndex] = vertices
  731. end
  732. function self:apply (skeleton, lastTime, time, firedEvents, alpha, blend, direction)
  733. local slot = skeleton.slots[self.slotIndex]
  734. local slotAttachment = slot.attachment
  735. if not slotAttachment then return end
  736. if not (slotAttachment.type == AttachmentType.mesh or slotAttachment.type == AttachmentType.linkedmesh or slotAttachment.type == AttachmentType.path or slotAttachment.type == AttachmentType.boundingbox) then return end
  737. if not slotAttachment:applyDeform(self.attachment) then return end
  738. local frames = self.frames
  739. local verticesArray = slot.attachmentVertices
  740. if #(verticesArray) == 0 then blend = MixBlend.setup end
  741. local frameVertices = self.frameVertices
  742. local vertexCount = #(frameVertices[0])
  743. if time < frames[0] then
  744. local vertexAttachment = slotAttachment;
  745. if blend == MixBlend.setup then
  746. slot.attachmentVertices = {}
  747. return;
  748. elseif blend == MixBlend.first then
  749. if (alpha == 1) then
  750. slot.attachmentVertices = {}
  751. return;
  752. end
  753. local vertices = utils.setArraySize(verticesArray, vertexCount)
  754. if (vertexAttachment.bones == nil) then
  755. local setupVertices = vertexAttachment.vertices
  756. local i = 1
  757. while i <= vertexCount do
  758. vertices[i] = vertices[i] + (setupVertices[i] - vertices[i]) * alpha
  759. i = i + 1
  760. end
  761. else
  762. alpha = 1 - alpha
  763. local i = 1
  764. while i <= vertexCount do
  765. vertices[i] = vertices[i] * alpha
  766. i = i + 1
  767. end
  768. end
  769. end
  770. return
  771. end
  772. local vertices = utils.setArraySize(verticesArray, vertexCount)
  773. if time >= frames[zlen(frames) - 1] then -- Time is after last frame.
  774. local lastVertices = frameVertices[zlen(frames) - 1]
  775. if alpha == 1 then
  776. if blend == MixBlend.add then
  777. local vertexAttachment = slotAttachment
  778. if vertexAttachment.bones == nil then
  779. -- Unweighted vertex positions, with alpha.
  780. local setupVertices = vertexAttachment.vertices
  781. local i = 1
  782. while i <= vertexCount do
  783. vertices[i] = vertices[i] + lastVertices[i] - setupVertices[i]
  784. i = i + 1
  785. end
  786. else
  787. -- Weighted deform offsets, with alpha.
  788. local i = 1
  789. while i <= vertexCount do
  790. vertices[i] = vertices[i] + lastVertices[i]
  791. i = i + 1
  792. end
  793. end
  794. else
  795. local i = 1
  796. while i <= vertexCount do
  797. vertices[i] = lastVertices[i]
  798. i = i + 1
  799. end
  800. end
  801. else
  802. if blend == MixBlend.setup then
  803. local vertexAttachment = slotAttachment
  804. if vertexAttachment.bones == nil then
  805. -- Unweighted vertex positions, with alpha.
  806. local setupVertices = vertexAttachment.vertices
  807. local i = 1
  808. while i <= vertexCount do
  809. local setup = setupVertices[i]
  810. vertices[i] = setup + (lastVertices[i] - setup) * alpha
  811. i = i + 1
  812. end
  813. else
  814. -- Weighted deform offsets, with alpha.
  815. local i = 1
  816. while i <= vertexCount do
  817. vertices[i] = lastVertices[i] * alpha
  818. i = i + 1
  819. end
  820. end
  821. elseif blend == MixBlend.first or blend == MixBlend.replace then
  822. local i = 1
  823. while i <= vertexCount do
  824. vertices[i] = vertices[i] + (lastVertices[i] - vertices[i]) * alpha
  825. i = i + 1
  826. end
  827. local vertexAttachment = slotAttachment
  828. if vertexAttachment.bones == nil then
  829. local setupVertices = vertexAttachment.vertices
  830. local i = 1
  831. while i <= vertexCount do
  832. vertices[i] = vertices[i] + (lastVertices[i] - setupVertices[i]) * alpha
  833. i = i + 1
  834. end
  835. else
  836. -- Weighted deform offsets, with alpha.
  837. local i = 1
  838. while i <= vertexCount do
  839. vertices[i] = vertices[i] + lastVertices[i] * alpha
  840. i = i + 1
  841. end
  842. end
  843. elseif blend == MixBlend.add then
  844. local vertexAttachment = slotAttachment
  845. if vertexAttachment.bones == nil then
  846. local setupVertices = vertexAttachment.vertices
  847. local i = 1
  848. while i <= vertexCount do
  849. vertices[i] = vertices[i] + (lastVertices[i] - setupVertices[i]) * alpha
  850. i = i + 1
  851. end
  852. else
  853. -- Weighted deform offsets, with alpha.
  854. local i = 1
  855. while i <= vertexCount do
  856. vertices[i] = vertices[i] + lastVertices[i] * alpha
  857. i = i + 1
  858. end
  859. end
  860. end
  861. end
  862. return;
  863. end
  864. -- Interpolate between the previous frame and the current frame.
  865. local frame = binarySearch(frames, time, 1)
  866. local prevVertices = frameVertices[frame - 1]
  867. local nextVertices = frameVertices[frame]
  868. local frameTime = frames[frame]
  869. local percent = self:getCurvePercent(frame - 1, 1 - (time - frameTime) / (frames[frame - 1] - frameTime))
  870. if alpha == 1 then
  871. if blend == MixBlend.add then
  872. local vertexAttachment = slotAttachment
  873. if vertexAttachment.bones == nil then
  874. -- Unweighted vertex positions, with alpha.
  875. local setupVertices = vertexAttachment.vertices
  876. local i = 1
  877. while i <= vertexCount do
  878. local prev = prevVertices[i]
  879. vertices[i] = vertices[i] + prev + (nextVertices[i] - prev) * precent - setupVertices[i]
  880. i = i + 1
  881. end
  882. else
  883. -- Weighted deform offsets, with alpha.
  884. local i = 1
  885. while i <= vertexCount do
  886. local prev = prevVertices[i]
  887. vertices[i] = vertices[i] + prev + (nextVertices[i] - prev) * percent
  888. i = i + 1
  889. end
  890. end
  891. else
  892. local i = 1
  893. while i <= vertexCount do
  894. local prev = prevVertices[i]
  895. vertices[i] = prev + (nextVertices[i] - prev) * percent
  896. i = i + 1
  897. end
  898. end
  899. else
  900. if blend == MixBlend.setup then
  901. local vertexAttachment = slotAttachment
  902. if vertexAttachment.bones == nil then
  903. -- Unweighted vertex positions, with alpha.
  904. local setupVertices = vertexAttachment.vertices
  905. local i = 1
  906. while i <= vertexCount do
  907. local prev = prevVertices[i]
  908. local setup = setupVertices[i]
  909. vertices[i] = setup + (prev + (nextVertices[i] - prev) * percent - setup) * alpha
  910. i = i + 1
  911. end
  912. else
  913. -- Weighted deform offsets, with alpha.
  914. local i = 1
  915. while i <= vertexCount do
  916. local prev = prevVertices[i]
  917. vertices[i] = (prev + (nextVertices[i] - prev) * percent) * alpha
  918. i = i + 1
  919. end
  920. end
  921. elseif blend == MixBlend.first or blend == MixBlend.replace then
  922. local i = 1
  923. while i <= vertexCount do
  924. local prev = prevVertices[i]
  925. vertices[i] = vertices[i] + (prev + (nextVertices[i] - prev) * percent - vertices[i]) * alpha
  926. i = i + 1
  927. end
  928. elseif blend == MixBlend.add then
  929. local vertexAttachment = slotAttachment
  930. if vertexAttachment.bones == nil then
  931. local setupVertices = vertexAttachment.vertices
  932. local i = 1
  933. while i <= vertexCount do
  934. local prev = prevVertices[i]
  935. vertices[i] = vertices[i] + (prev + (nextVertices[i] - prev) * percent - setupVertices[i]) * alpha
  936. i = i + 1
  937. end
  938. else
  939. -- Weighted deform offsets, with alpha.
  940. local i = 1
  941. while i <= vertexCount do
  942. local prev = prevVertices[i]
  943. vertices[i] = vertices[i] + (prev + (nextVertices[i] - prev) * percent) * alpha
  944. i = i + 1
  945. end
  946. end
  947. end
  948. end
  949. end
  950. return self
  951. end
  952. Animation.EventTimeline = {}
  953. function Animation.EventTimeline.new (frameCount)
  954. local self = {
  955. frames = utils.newNumberArrayZero(frameCount),
  956. events = {},
  957. type = TimelineType.event
  958. }
  959. function self:getPropertyId ()
  960. return TimelineType.event * SHL_24
  961. end
  962. function self:getFrameCount ()
  963. return zlen(self.frames)
  964. end
  965. function self:setFrame (frameIndex, event)
  966. self.frames[frameIndex] = event.time
  967. self.events[frameIndex] = event
  968. end
  969. -- Fires events for frames > lastTime and <= time.
  970. function self:apply (skeleton, lastTime, time, firedEvents, alpha, blend, direction)
  971. if not firedEvents then return end
  972. local frames = self.frames
  973. local frameCount = zlen(frames)
  974. if lastTime > time then -- Fire events after last time for looped animations.
  975. self:apply(skeleton, lastTime, 999999, firedEvents, alpha, blend, direction)
  976. lastTime = -1
  977. elseif lastTime >= frames[frameCount - 1] then -- Last time is after last frame.
  978. return
  979. end
  980. if time < frames[0] then return end -- Time is before first frame.
  981. local frame
  982. if lastTime < frames[0] then
  983. frame = 0
  984. else
  985. frame = binarySearch1(frames, lastTime)
  986. local frame = frames[frame]
  987. while frame > 0 do -- Fire multiple events with the same frame.
  988. if frames[frame - 1] ~= frame then break end
  989. frame = frame - 1
  990. end
  991. end
  992. local events = self.events
  993. while frame < frameCount and time >= frames[frame] do
  994. table.insert(firedEvents, events[frame])
  995. frame = frame + 1
  996. end
  997. end
  998. return self
  999. end
  1000. Animation.DrawOrderTimeline = {}
  1001. function Animation.DrawOrderTimeline.new (frameCount)
  1002. local self = {
  1003. frames = utils.newNumberArrayZero(frameCount),
  1004. drawOrders = {},
  1005. type = TimelineType.drawOrder
  1006. }
  1007. function self:getPropertyId ()
  1008. return TimelineType.drawOrder * SHL_24
  1009. end
  1010. function self:getFrameCount ()
  1011. return zlen(self.frames)
  1012. end
  1013. function self:setFrame (frameIndex, time, drawOrder)
  1014. self.frames[frameIndex] = time
  1015. self.drawOrders[frameIndex] = drawOrder
  1016. end
  1017. function self:apply (skeleton, lastTime, time, firedEvents, alpha, blend, direction)
  1018. local drawOrder = skeleton.drawOrder
  1019. local slots = skeleton.slots
  1020. if direction == MixDirection.out and blend == MixBlend.setup then
  1021. for i,slot in ipairs(slots) do
  1022. drawOrder[i] = slots[i]
  1023. end
  1024. return;
  1025. end
  1026. local frames = self.frames
  1027. if time < frames[0] then
  1028. if blend == MixBlend.setup or blend == MixBlend.first then
  1029. for i,slot in ipairs(slots) do
  1030. drawOrder[i] = slots[i]
  1031. end
  1032. end
  1033. return
  1034. end
  1035. local frame
  1036. if time >= frames[zlen(frames) - 1] then -- Time is after last frame.
  1037. frame = zlen(frames) - 1
  1038. else
  1039. frame = binarySearch1(frames, time) - 1
  1040. end
  1041. local drawOrderToSetupIndex = self.drawOrders[frame]
  1042. if not drawOrderToSetupIndex then
  1043. for i,slot in ipairs(slots) do
  1044. drawOrder[i] = slots[i]
  1045. end
  1046. else
  1047. for i,setupIndex in ipairs(drawOrderToSetupIndex) do
  1048. drawOrder[i] = skeleton.slots[setupIndex]
  1049. end
  1050. end
  1051. end
  1052. return self
  1053. end
  1054. Animation.IkConstraintTimeline = {}
  1055. Animation.IkConstraintTimeline.ENTRIES = 5
  1056. function Animation.IkConstraintTimeline.new (frameCount)
  1057. local ENTRIES = Animation.IkConstraintTimeline.ENTRIES
  1058. local PREV_TIME = -5
  1059. local PREV_MIX = -4
  1060. local PREV_BEND_DIRECTION = -3
  1061. local PREV_COMPRESS = -2
  1062. local PREV_STRETCH = -1
  1063. local MIX = 1
  1064. local BEND_DIRECTION = 2
  1065. local COMPRESS = 3
  1066. local STRETCH = 4
  1067. local self = Animation.CurveTimeline.new(frameCount)
  1068. self.frames = utils.newNumberArrayZero(frameCount * ENTRIES) -- time, mix, bendDirection, compress, stretch, ...
  1069. self.ikConstraintIndex = -1
  1070. self.type = TimelineType.ikConstraint
  1071. function self:getPropertyId ()
  1072. return TimelineType.ikConstraint * SHL_24 + self.ikConstraintIndex
  1073. end
  1074. function self:setFrame (frameIndex, time, mix, bendDirection, compress, stretch)
  1075. frameIndex = frameIndex * ENTRIES
  1076. self.frames[frameIndex] = time
  1077. self.frames[frameIndex + MIX] = mix
  1078. self.frames[frameIndex + BEND_DIRECTION] = bendDirection
  1079. if (compress) then
  1080. self.frames[frameIndex + COMPRESS] = 1
  1081. else
  1082. self.frames[frameIndex + COMPRESS] = 0
  1083. end
  1084. if (stretch) then
  1085. self.frames[frameIndex + STRETCH] = 1
  1086. else
  1087. self.frames[frameIndex + STRETCH] = 0
  1088. end
  1089. end
  1090. function self:apply (skeleton, lastTime, time, firedEvents, alpha, blend, direction)
  1091. local frames = self.frames
  1092. local constraint = skeleton.ikConstraints[self.ikConstraintIndex]
  1093. if time < frames[0] then
  1094. if blend == MixBlend.setup then
  1095. constraint.mix = constraint.data.mix
  1096. constraint.bendDirection = constraint.data.bendDirection
  1097. constraint.compress = constraint.data.compress
  1098. constraint.stretch = constraint.data.stretch
  1099. elseif blend == MixBlend.first then
  1100. constraint.mix = constraint.mix + (constraint.data.mix - constraint.mix) * alpha
  1101. constraint.bendDirection = constraint.data.bendDirection
  1102. constraint.compress = constraint.data.compress
  1103. constraint.stretch = constraint.data.stretch
  1104. end
  1105. return
  1106. end
  1107. if time >= frames[zlen(frames) - ENTRIES] then -- Time is after last frame.
  1108. if blend == MixBlend.setup then
  1109. constraint.mix = constraint.data.mix + (frames[zlen(frames) + PREV_MIX] - constraint.data.mix) * alpha
  1110. if direction == MixDirection.out then
  1111. constraint.bendDirection = constraint.data.bendDirection
  1112. constraint.compress = constraint.data.compress
  1113. constraint.stretch = constraint.data.stretch
  1114. else
  1115. constraint.bendDirection = math_floor(frames[zlen(frames) + PREV_BEND_DIRECTION]);
  1116. if (math_floor(frames[zlen(frames) + PREV_COMPRESS]) == 1) then constraint.compress = true else constraint.compress = false end
  1117. if (math_floor(frames[zlen(frames) + PREV_STRETCH]) == 1) then constraint.stretch = true else constraint.stretch = false end
  1118. end
  1119. else
  1120. constraint.mix = constraint.mix + (frames[zlen(frames) + PREV_MIX] - constraint.mix) * alpha;
  1121. if direction == MixDirection._in then
  1122. constraint.bendDirection = math_floor(frames[zlen(frames) + PREV_BEND_DIRECTION])
  1123. if (math_floor(frames[zlen(frames) + PREV_COMPRESS]) == 1) then constraint.compress = true else constraint.compress = false end
  1124. if (math_floor(frames[zlen(frames) + PREV_STRETCH]) == 1) then constraint.stretch = true else constraint.stretch = false end
  1125. end
  1126. end
  1127. return
  1128. end
  1129. -- Interpolate between the previous frame and the current frame.
  1130. local frame = binarySearch(frames, time, ENTRIES)
  1131. local mix = frames[frame + PREV_MIX]
  1132. local frameTime = frames[frame]
  1133. local percent = self:getCurvePercent(math.floor(frame / ENTRIES) - 1,
  1134. 1 - (time - frameTime) / (frames[frame + PREV_TIME] - frameTime))
  1135. if blend == MixBlend.setup then
  1136. constraint.mix = constraint.data.mix + (mix + (frames[frame + MIX] - mix) * percent - constraint.data.mix) * alpha
  1137. if direction == MixDirection.out then
  1138. constraint.bendDirection = constraint.data.bendDirection
  1139. constraint.compress = constraint.data.compress
  1140. constraint.stretch = constraint.data.stretch
  1141. else
  1142. constraint.bendDirection = math_floor(frames[frame + PREV_BEND_DIRECTION])
  1143. if (math_floor(frames[frame + PREV_COMPRESS]) == 1) then constraint.compress = true else constraint.compress = false end
  1144. if (math_floor(frames[frame + PREV_STRETCH]) == 1) then constraint.stretch = true else constraint.stretch = false end
  1145. end
  1146. else
  1147. constraint.mix = constraint.mix + (mix + (frames[frame + MIX] - mix) * percent - constraint.mix) * alpha;
  1148. if direction == MixDirection._in then
  1149. constraint.bendDirection = math_floor(frames[frame + PREV_BEND_DIRECTION])
  1150. if (math_floor(frames[frame + PREV_COMPRESS]) == 1) then constraint.compress = true else constraint.compress = false end
  1151. if (math_floor(frames[frame + PREV_STRETCH]) == 1) then constraint.stretch = true else constraint.stretch = false end
  1152. end
  1153. end
  1154. end
  1155. return self
  1156. end
  1157. Animation.TransformConstraintTimeline = {}
  1158. Animation.TransformConstraintTimeline.ENTRIES = 5
  1159. function Animation.TransformConstraintTimeline.new (frameCount)
  1160. local ENTRIES = Animation.TransformConstraintTimeline.ENTRIES
  1161. local PREV_TIME = -5
  1162. local PREV_ROTATE = -4
  1163. local PREV_TRANSLATE = -3
  1164. local PREV_SCALE = -2
  1165. local PREV_SHEAR = -1
  1166. local ROTATE = 1
  1167. local TRANSLATE = 2
  1168. local SCALE = 3
  1169. local SHEAR = 4
  1170. local self = Animation.CurveTimeline.new(frameCount)
  1171. self.frames = utils.newNumberArrayZero(frameCount * ENTRIES)
  1172. self.transformConstraintIndex = -1
  1173. self.type = TimelineType.transformConstraint
  1174. function self:getPropertyId ()
  1175. return TimelineType.transformConstraint * SHL_24 + self.transformConstraintIndex
  1176. end
  1177. function self:setFrame (frameIndex, time, rotateMix, translateMix, scaleMix, shearMix)
  1178. frameIndex = frameIndex * ENTRIES
  1179. self.frames[frameIndex] = time
  1180. self.frames[frameIndex + ROTATE] = rotateMix
  1181. self.frames[frameIndex + TRANSLATE] = translateMix
  1182. self.frames[frameIndex + SCALE] = scaleMix
  1183. self.frames[frameIndex + SHEAR] = shearMix
  1184. end
  1185. function self:apply (skeleton, lastTime, time, firedEvents, alpha, blend, direction)
  1186. local frames = self.frames
  1187. local constraint = skeleton.transformConstraints[self.transformConstraintIndex]
  1188. if time < frames[0] then
  1189. local data = constraint.data
  1190. if blend == MixBlend.setup then
  1191. constraint.rotateMix = data.rotateMix
  1192. constraint.translateMix = data.translateMix
  1193. constraint.scaleMix = data.scaleMix
  1194. constraint.shearMix = data.shearMix
  1195. elseif blend == MixBlend.first then
  1196. constraint.rotateMix = constraint.rotateMix + (data.rotateMix - constraint.rotateMix) * alpha
  1197. constraint.translateMix = constraint.translateMix + (data.translateMix - constraint.translateMix) * alpha
  1198. constraint.scaleMix = constraint.scaleMix + (data.scaleMix - constraint.scaleMix) * alpha
  1199. constraint.shearMix = constraint.shearMix + (data.shearMix - constraint.shearMix) * alpha
  1200. end
  1201. return
  1202. end
  1203. local rotate = 0
  1204. local translate = 0
  1205. local scale = 0
  1206. local shear = 0
  1207. if time >= frames[zlen(frames) - ENTRIES] then -- Time is after last frame.
  1208. local i = zlen(frames.length)
  1209. rotate = frames[i + PREV_ROTATE]
  1210. translate = frames[i + PREV_TRANSLATE]
  1211. scale = frames[i + PREV_SCALE]
  1212. shear = frames[i + PREV_SHEAR]
  1213. else
  1214. -- Interpolate between the previous frame and the current frame.
  1215. local frame = binarySearch(frames, time, ENTRIES)
  1216. rotate = frames[frame + PREV_ROTATE]
  1217. translate = frames[frame + PREV_TRANSLATE]
  1218. scale = frames[frame + PREV_SCALE]
  1219. shear = frames[frame + PREV_SHEAR]
  1220. local frameTime = frames[frame]
  1221. local percent = self:getCurvePercent(math_floor(frame / ENTRIES) - 1,
  1222. 1 - (time - frameTime) / (frames[frame + PREV_TIME] - frameTime));
  1223. rotate = rotate + (frames[frame + ROTATE] - rotate) * percent
  1224. translate = translate + (frames[frame + TRANSLATE] - translate) * percent
  1225. scale = scale + (frames[frame + SCALE] - scale) * percent
  1226. shear = shear + (frames[frame + SHEAR] - shear) * percent
  1227. end
  1228. if blend == MixBlend.setup then
  1229. local data = constraint.data
  1230. constraint.rotateMix = data.rotateMix + (rotate - data.rotateMix) * alpha
  1231. constraint.translateMix = data.translateMix + (translate - data.translateMix) * alpha
  1232. constraint.scaleMix = data.scaleMix + (scale - data.scaleMix) * alpha
  1233. constraint.shearMix = data.shearMix + (shear - data.shearMix) * alpha
  1234. else
  1235. constraint.rotateMix = constraint.rotateMix + (rotate - constraint.rotateMix) * alpha
  1236. constraint.translateMix = constraint.translateMix + (translate - constraint.translateMix) * alpha
  1237. constraint.scaleMix = constraint.scaleMix + (scale - constraint.scaleMix) * alpha
  1238. constraint.shearMix = constraint.shearMix + (shear - constraint.shearMix) * alpha
  1239. end
  1240. end
  1241. return self
  1242. end
  1243. Animation.PathConstraintPositionTimeline = {}
  1244. Animation.PathConstraintPositionTimeline.ENTRIES = 2
  1245. function Animation.PathConstraintPositionTimeline.new (frameCount)
  1246. local ENTRIES = Animation.PathConstraintPositionTimeline.ENTRIES
  1247. local PREV_TIME = -2
  1248. local PREV_VALUE = -1
  1249. local VALUE = 1
  1250. local self = Animation.CurveTimeline.new(frameCount)
  1251. self.frames = utils.newNumberArrayZero(frameCount * ENTRIES)
  1252. self.pathConstraintIndex = -1
  1253. self.type = TimelineType.pathConstraintPosition
  1254. function self:getPropertyId ()
  1255. return TimelineType.pathConstraintPosition * SHL_24 + self.pathConstraintIndex
  1256. end
  1257. function self:setFrame (frameIndex, time, value)
  1258. frameIndex = frameIndex * ENTRIES
  1259. self.frames[frameIndex] = time
  1260. self.frames[frameIndex + VALUE] = value
  1261. end
  1262. function self:apply (skeleton, lastTime, time, firedEvents, alpha, blend, direction)
  1263. local frames = self.frames
  1264. local constraint = skeleton.pathConstraints[self.pathConstraintIndex]
  1265. if (time < frames[0]) then
  1266. if blend == MixBlend.setup then
  1267. constraint.position = constraint.data.position
  1268. elseif blend == MixBlend.first then
  1269. constraint.position = constraint.position + (constraint.data.position - constraint.position) * alpha
  1270. end
  1271. return
  1272. end
  1273. local position = 0
  1274. if time >= frames[zlen(frames) - ENTRIES] then -- Time is after last frame.
  1275. position = frames[zlen(frames) + PREV_VALUE]
  1276. else
  1277. -- Interpolate between the previous frame and the current frame.
  1278. local frame = binarySearch(frames, time, ENTRIES)
  1279. position = frames[frame + PREV_VALUE]
  1280. local frameTime = frames[frame]
  1281. local percent = self:getCurvePercent(math_floor(frame / ENTRIES) - 1,
  1282. 1 - (time - frameTime) / (frames[frame + PREV_TIME] - frameTime))
  1283. position = position + (frames[frame + VALUE] - position) * percent
  1284. end
  1285. if blend == MixBlend.setup then
  1286. constraint.position = constraint.data.position + (position - constraint.data.position) * alpha
  1287. else
  1288. constraint.position = constraint.position + (position - constraint.position) * alpha
  1289. end
  1290. end
  1291. return self
  1292. end
  1293. Animation.PathConstraintSpacingTimeline = {}
  1294. Animation.PathConstraintSpacingTimeline.ENTRIES = 2
  1295. function Animation.PathConstraintSpacingTimeline.new (frameCount)
  1296. local ENTRIES = Animation.PathConstraintSpacingTimeline.ENTRIES
  1297. local PREV_TIME = -2
  1298. local PREV_VALUE = -1
  1299. local VALUE = 1
  1300. local self = Animation.CurveTimeline.new(frameCount)
  1301. self.frames = utils.newNumberArrayZero(frameCount * ENTRIES)
  1302. self.pathConstraintIndex = -1
  1303. self.type = TimelineType.pathConstraintSpacing
  1304. function self:getPropertyId ()
  1305. return TimelineType.pathConstraintSpacing * SHL_24 + self.pathConstraintIndex
  1306. end
  1307. function self:setFrame (frameIndex, time, value)
  1308. frameIndex = frameIndex * ENTRIES
  1309. self.frames[frameIndex] = time
  1310. self.frames[frameIndex + VALUE] = value
  1311. end
  1312. function self:apply (skeleton, lastTime, time, firedEvents, alpha, blend, direction)
  1313. local frames = self.frames
  1314. local constraint = skeleton.pathConstraints[self.pathConstraintIndex]
  1315. if (time < frames[0]) then
  1316. if blend == MixBlend.setup then
  1317. constraint.spacing = constraint.data.spacing
  1318. elseif blend == MixBlend.first then
  1319. constraint.spacing = constraint.spacing + (constraint.data.spacing - constraint.spacing) * alpha
  1320. end
  1321. return
  1322. end
  1323. local spacing = 0
  1324. if time >= frames[zlen(frames) - ENTRIES] then -- Time is after last frame.
  1325. spacing = frames[zlen(frames) + PREV_VALUE]
  1326. else
  1327. -- Interpolate between the previous frame and the current frame.
  1328. local frame = binarySearch(frames, time, ENTRIES)
  1329. spacing = frames[frame + PREV_VALUE]
  1330. local frameTime = frames[frame]
  1331. local percent = self:getCurvePercent(math_floor(frame / ENTRIES) - 1,
  1332. 1 - (time - frameTime) / (frames[frame + PREV_TIME] - frameTime))
  1333. spacing = spacing + (frames[frame + VALUE] - spacing) * percent
  1334. end
  1335. if blend == MixBlend.setup then
  1336. constraint.spacing = constraint.data.spacing + (spacing - constraint.data.spacing) * alpha
  1337. else
  1338. constraint.spacing = constraint.spacing + (spacing - constraint.spacing) * alpha
  1339. end
  1340. end
  1341. return self
  1342. end
  1343. Animation.PathConstraintMixTimeline = {}
  1344. Animation.PathConstraintMixTimeline.ENTRIES = 3
  1345. function Animation.PathConstraintMixTimeline.new (frameCount)
  1346. local ENTRIES = Animation.PathConstraintMixTimeline.ENTRIES
  1347. local PREV_TIME = -3
  1348. local PREV_ROTATE = -2
  1349. local PREV_TRANSLATE = -1
  1350. local ROTATE = 1
  1351. local TRANSLATE = 2
  1352. local self = Animation.CurveTimeline.new(frameCount)
  1353. self.frames = utils.newNumberArrayZero(frameCount * ENTRIES)
  1354. self.pathConstraintIndex = -1
  1355. self.type = TimelineType.pathConstraintMix
  1356. function self:getPropertyId ()
  1357. return TimelineType.pathConstraintMix * SHL_24 + self.pathConstraintIndex
  1358. end
  1359. function self:setFrame (frameIndex, time, rotateMix, translateMix)
  1360. frameIndex = frameIndex * ENTRIES
  1361. self.frames[frameIndex] = time
  1362. self.frames[frameIndex + ROTATE] = rotateMix
  1363. self.frames[frameIndex + TRANSLATE] = translateMix
  1364. end
  1365. function self:apply (skeleton, lastTime, time, firedEvents, alpha, blend, direction)
  1366. local frames = self.frames
  1367. local constraint = skeleton.pathConstraints[self.pathConstraintIndex]
  1368. if (time < frames[0]) then
  1369. if blend == MixBlend.setup then
  1370. constraint.rotateMix = constraint.data.rotateMix
  1371. constraint.translateMix = constraint.data.translateMix
  1372. elseif blend == MixBlend.first then
  1373. constraint.rotateMix = constraint.rotateMix + (constraint.data.rotateMix - constraint.rotateMix) * alpha
  1374. constraint.translateMix = constraint.translateMix + (constraint.data.translateMix - constraint.translateMix) * alpha
  1375. end
  1376. return
  1377. end
  1378. local rotate = 0
  1379. local translate = 0
  1380. if time >= frames[zlen(frames) - ENTRIES] then -- Time is after last frame.
  1381. rotate = frames[zlen(frames) + PREV_ROTATE]
  1382. translate = frames[zlen(frames) + PREV_TRANSLATE]
  1383. else
  1384. -- Interpolate between the previous frame and the current frame.
  1385. local frame = binarySearch(frames, time, ENTRIES)
  1386. rotate = frames[frame + PREV_ROTATE]
  1387. translate = frames[frame + PREV_TRANSLATE]
  1388. local frameTime = frames[frame]
  1389. local percent = self:getCurvePercent(math_floor(frame / ENTRIES) - 1,
  1390. 1 - (time - frameTime) / (frames[frame + PREV_TIME] - frameTime))
  1391. rotate = rotate + (frames[frame + ROTATE] - rotate) * percent
  1392. translate = translate + (frames[frame + TRANSLATE] - translate) * percent
  1393. end
  1394. if blend == MixBlend.setup then
  1395. constraint.rotateMix = constraint.data.rotateMix + (rotate - constraint.data.rotateMix) * alpha
  1396. constraint.translateMix = constraint.data.translateMix + (translate - constraint.data.translateMix) * alpha
  1397. else
  1398. constraint.rotateMix = constraint.rotateMix + (rotate - constraint.rotateMix) * alpha
  1399. constraint.translateMix = constraint.translateMix + (translate - constraint.translateMix) * alpha
  1400. end
  1401. end
  1402. return self
  1403. end
  1404. return Animation