SkeletonJson.lua 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. local table_insert = table.insert
  31. local SkeletonData = require "spine-lua.SkeletonData"
  32. local BoneData = require "spine-lua.BoneData"
  33. local SlotData = require "spine-lua.SlotData"
  34. local Skin = require "spine-lua.Skin"
  35. local AttachmentLoader = require "spine-lua.AttachmentLoader"
  36. local Animation = require "spine-lua.Animation"
  37. local IkConstraintData = require "spine-lua.IkConstraintData"
  38. local IkConstraint = require "spine-lua.IkConstraint"
  39. local PathConstraintData = require "spine-lua.PathConstraintData"
  40. local PathConstraint = require "spine-lua.PathConstraint"
  41. local TransformConstraintData = require "spine-lua.TransformConstraintData"
  42. local TransformConstraint = require "spine-lua.TransformConstraint"
  43. local EventData = require "spine-lua.EventData"
  44. local Event = require "spine-lua.Event"
  45. local AttachmentType = require "spine-lua.attachments.AttachmentType"
  46. local BlendMode = require "spine-lua.BlendMode"
  47. local TransformMode = require "spine-lua.TransformMode"
  48. local utils = require "spine-lua.utils"
  49. local Color = require "spine-lua.Color"
  50. local SkeletonJson = {}
  51. function SkeletonJson.new (attachmentLoader)
  52. if not attachmentLoader then attachmentLoader = AttachmentLoader.new() end
  53. local self = {
  54. attachmentLoader = attachmentLoader,
  55. scale = 1,
  56. linkedMeshes = {}
  57. }
  58. function self:readSkeletonDataFile (fileName, base)
  59. return self:readSkeletonData(spine.utils.readFile(fileName, base))
  60. end
  61. local readAttachment
  62. local readAnimation
  63. local readCurve
  64. local getArray
  65. local getValue = function (map, name, default)
  66. local value = map[name]
  67. if value == nil then return default else return value end
  68. end
  69. function self:readSkeletonData (jsonText)
  70. local scale = self.scale
  71. local skeletonData = SkeletonData.new(self.attachmentLoader)
  72. local root = spine.utils.readJSON(jsonText)
  73. if not root then error("Invalid JSON: " .. jsonText, 2) end
  74. -- Skeleton.
  75. local skeletonMap = root["skeleton"]
  76. if skeletonMap then
  77. skeletonData.hash = skeletonMap["hash"]
  78. skeletonData.version = skeletonMap["spine"]
  79. skeletonData.width = skeletonMap["width"]
  80. skeletonData.height = skeletonMap["height"]
  81. skeletonData.fps = skeletonMap["fps"]
  82. skeletonData.imagesPath = skeletonMap["images"]
  83. end
  84. -- Bones.
  85. for i,boneMap in ipairs(root["bones"]) do
  86. local boneName = boneMap["name"]
  87. local parent = nil
  88. local parentName = boneMap["parent"]
  89. if parentName then
  90. parent = skeletonData:findBone(parentName)
  91. if not parent then error("Parent bone not found: " .. parentName) end
  92. end
  93. local data = BoneData.new(i, boneName, parent)
  94. data.length = getValue(boneMap, "length", 0) * scale;
  95. data.x = getValue(boneMap, "x", 0) * scale;
  96. data.y = getValue(boneMap, "y", 0) * scale;
  97. data.rotation = getValue(boneMap, "rotation", 0);
  98. data.scaleX = getValue(boneMap, "scaleX", 1);
  99. data.scaleY = getValue(boneMap, "scaleY", 1);
  100. data.shearX = getValue(boneMap, "shearX", 0);
  101. data.shearY = getValue(boneMap, "shearY", 0);
  102. data.transformMode = TransformMode[getValue(boneMap, "transform", "normal")]
  103. table_insert(skeletonData.bones, data)
  104. end
  105. -- Slots.
  106. if root["slots"] then
  107. for i,slotMap in ipairs(root["slots"]) do
  108. local index = i
  109. local slotName = slotMap["name"]
  110. local boneName = slotMap["bone"]
  111. local boneData = skeletonData:findBone(boneName)
  112. if not boneData then error("Slot bone not found: " .. boneName) end
  113. local data = SlotData.new(i, slotName, boneData)
  114. local color = slotMap["color"]
  115. if color then
  116. data.color:set(tonumber(color:sub(1, 2), 16) / 255,
  117. tonumber(color:sub(3, 4), 16) / 255,
  118. tonumber(color:sub(5, 6), 16) / 255,
  119. tonumber(color:sub(7, 8), 16) / 255)
  120. end
  121. local dark = slotMap["dark"]
  122. if dark then
  123. data.darkColor = Color.newWith(1, 1, 1, 1)
  124. data.darkColor:set(tonumber(dark:sub(1, 2), 16) / 255,
  125. tonumber(dark:sub(3, 4), 16) / 255,
  126. tonumber(dark:sub(5, 6), 16) / 255,
  127. 0)
  128. end
  129. data.attachmentName = getValue(slotMap, "attachment", nil)
  130. data.blendMode = BlendMode[getValue(slotMap, "blend", "normal")]
  131. table_insert(skeletonData.slots, data)
  132. skeletonData.slotNameIndices[data.name] = #skeletonData.slots
  133. end
  134. end
  135. -- IK constraints.
  136. if root["ik"] then
  137. for i,constraintMap in ipairs(root["ik"]) do
  138. local data = IkConstraintData.new(constraintMap["name"])
  139. data.order = getValue(constraintMap, "order", 0)
  140. for i,boneName in ipairs(constraintMap["bones"]) do
  141. local bone = skeletonData:findBone(boneName)
  142. if not bone then error("IK bone not found: " .. boneName) end
  143. table_insert(data.bones, bone)
  144. end
  145. local targetName = constraintMap["target"]
  146. data.target = skeletonData:findBone(targetName)
  147. if not data.target then error("Target bone not found: " .. targetName) end
  148. if constraintMap["bendPositive"] == false then data.bendDirection = -1 else data.bendDirection = 1 end
  149. data.mix = getValue(constraintMap, "mix", 1)
  150. table_insert(skeletonData.ikConstraints, data)
  151. end
  152. end
  153. -- Transform constraints
  154. if root["transform"] then
  155. for i,constraintMap in ipairs(root["transform"]) do
  156. data = TransformConstraintData.new(constraintMap.name)
  157. data.order = getValue(constraintMap, "order", 0)
  158. for i,boneName in ipairs(constraintMap.bones) do
  159. local bone = skeletonData:findBone(boneName)
  160. if not bone then error("Transform constraint bone not found: " .. boneName, 2) end
  161. table_insert(data.bones, bone)
  162. end
  163. local targetName = constraintMap.target
  164. data.target = skeletonData:findBone(targetName)
  165. if not data.target then error("Transform constraint target bone not found: " .. boneName, 2) end
  166. data.offsetRotation = getValue(constraintMap, "rotation", 0);
  167. data.offsetX = getValue(constraintMap, "x", 0) * scale;
  168. data.offsetY = getValue(constraintMap, "y", 0) * scale;
  169. data.offsetScaleX = getValue(constraintMap, "scaleX", 0);
  170. data.offsetScaleY = getValue(constraintMap, "scaleY", 0);
  171. data.offsetShearY = getValue(constraintMap, "shearY", 0);
  172. data.rotateMix = getValue(constraintMap, "rotateMix", 1);
  173. data.translateMix = getValue(constraintMap, "translateMix", 1);
  174. data.scaleMix = getValue(constraintMap, "scaleMix", 1);
  175. data.shearMix = getValue(constraintMap, "shearMix", 1);
  176. table_insert(skeletonData.transformConstraints, data)
  177. end
  178. end
  179. -- Path constraints
  180. if root["path"] then
  181. for i,constraintMap in ipairs(root.path) do
  182. local data = PathConstraintData.new(constraintMap.name);
  183. data.order = getValue(constraintMap, "order", 0)
  184. for i,boneName in ipairs(constraintMap.bones) do
  185. local bone = skeletonData:findBone(boneName)
  186. if not bone then error("Path constraint bone not found: " .. boneName, 2) end
  187. table_insert(data.bones, bone)
  188. end
  189. local targetName = constraintMap.target;
  190. data.target = skeletonData:findSlot(targetName)
  191. if data.target == nil then error("Path target slot not found: " .. targetName, 2) end
  192. data.positionMode = PathConstraintData.PositionMode[getValue(constraintMap, "positionMode", "percent"):lower()]
  193. data.spacingMode = PathConstraintData.SpacingMode[getValue(constraintMap, "spacingMode", "length"):lower()]
  194. data.rotateMode = PathConstraintData.RotateMode[getValue(constraintMap, "rotateMode", "tangent"):lower()]
  195. data.offsetRotation = getValue(constraintMap, "rotation", 0);
  196. data.position = getValue(constraintMap, "position", 0);
  197. if data.positionMode == PathConstraintData.PositionMode.fixed then data.position = data.position * scale end
  198. data.spacing = getValue(constraintMap, "spacing", 0);
  199. if data.spacingMode == PathConstraintData.SpacingMode.length or data.spacingMode == PathConstraintData.SpacingMode.fixed then data.spacing = data.spacing * scale end
  200. data.rotateMix = getValue(constraintMap, "rotateMix", 1);
  201. data.translateMix = getValue(constraintMap, "translateMix", 1);
  202. table_insert(skeletonData.pathConstraints, data)
  203. end
  204. end
  205. -- Skins.
  206. if root["skins"] then
  207. for skinName,skinMap in pairs(root["skins"]) do
  208. local skin = Skin.new(skinName)
  209. for slotName,slotMap in pairs(skinMap) do
  210. local slotIndex = skeletonData.slotNameIndices[slotName]
  211. for attachmentName,attachmentMap in pairs(slotMap) do
  212. local attachment = readAttachment(attachmentMap, skin, slotIndex, attachmentName, skeletonData)
  213. if attachment then
  214. skin:addAttachment(slotIndex, attachmentName, attachment)
  215. end
  216. end
  217. end
  218. table_insert(skeletonData.skins, skin)
  219. if skin.name == "default" then skeletonData.defaultSkin = skin end
  220. end
  221. end
  222. -- Linked meshes
  223. for i, linkedMesh in ipairs(self.linkedMeshes) do
  224. local skin = skeletonData.defaultSkin
  225. if linkedMesh.skin then skin = skeletonData:findSkin(linkedMesh.skin) end
  226. if not skin then error("Skin not found: " .. linkedMesh.skin) end
  227. local parent = skin:getAttachment(linkedMesh.slotIndex, linkedMesh.parent)
  228. if not parent then error("Parent mesh not found: " + linkedMesh.parent) end
  229. linkedMesh.mesh:setParentMesh(parent)
  230. linkedMesh.mesh:updateUVs()
  231. end
  232. self.linkedMeshes = {}
  233. -- Events.
  234. if root["events"] then
  235. for eventName,eventMap in pairs(root["events"]) do
  236. local data = EventData.new(eventName)
  237. data.intValue = getValue(eventMap, "int", 0)
  238. data.floatValue = getValue(eventMap, "float", 0)
  239. data.stringValue = getValue(eventMap, "string", "")
  240. table_insert(skeletonData.events, data)
  241. end
  242. end
  243. -- Animations.
  244. if root["animations"] then
  245. for animationName,animationMap in pairs(root["animations"]) do
  246. readAnimation(animationMap, animationName, skeletonData)
  247. end
  248. end
  249. return skeletonData
  250. end
  251. readAttachment = function (map, skin, slotIndex, name, skeletonData)
  252. local scale = self.scale
  253. name = getValue(map, "name", name)
  254. local type = AttachmentType[getValue(map, "type", "region")]
  255. local path = getValue(map, "path", name)
  256. if type == AttachmentType.region then
  257. local region = attachmentLoader:newRegionAttachment(skin, name, path)
  258. if not region then return nil end
  259. region.path = path
  260. region.x = getValue(map, "x", 0) * scale
  261. region.y = getValue(map, "y", 0) * scale
  262. region.scaleX = getValue(map, "scaleX", 1);
  263. region.scaleY = getValue(map, "scaleY", 1);
  264. region.rotation = getValue(map, "rotation", 0);
  265. region.width = map.width * scale;
  266. region.height = map.height * scale;
  267. local color = map["color"]
  268. if color then
  269. region.color:set(tonumber(color:sub(1, 2), 16) / 255,
  270. tonumber(color:sub(3, 4), 16) / 255,
  271. tonumber(color:sub(5, 6), 16) / 255,
  272. tonumber(color:sub(7, 8), 16) / 255)
  273. end
  274. region:updateOffset()
  275. return region
  276. elseif type == AttachmentType.boundingbox then
  277. local box = attachmentLoader:newBoundingBoxAttachment(skin, name)
  278. if not box then return nil end
  279. readVertices(map, box, map.vertexCount * 2)
  280. local color = map.color
  281. if color then
  282. box.color:set(tonumber(color:sub(1, 2), 16) / 255,
  283. tonumber(color:sub(3, 4), 16) / 255,
  284. tonumber(color:sub(5, 6), 16) / 255,
  285. tonumber(color:sub(7, 8), 16) / 255)
  286. end
  287. return box
  288. elseif type == AttachmentType.mesh or type == AttachmentType.linkedmesh then
  289. local mesh = attachmentLoader:newMeshAttachment(skin, name, path)
  290. if not mesh then return null end
  291. mesh.path = path
  292. local color = map.color
  293. if color then
  294. mesh.color:set(tonumber(color:sub(1, 2), 16) / 255,
  295. tonumber(color:sub(3, 4), 16) / 255,
  296. tonumber(color:sub(5, 6), 16) / 255,
  297. tonumber(color:sub(7, 8), 16) / 255)
  298. end
  299. local parent = map.parent
  300. if parent then
  301. mesh.inheritDeform = getValue(map, "deform", true)
  302. table_insert(self.linkedMeshes, {
  303. mesh = mesh,
  304. skin = getValue(map, "skin", nil),
  305. slotIndex = slotIndex,
  306. parent = parent
  307. })
  308. return mesh
  309. end
  310. local uvs = getArray(map, "uvs", 1)
  311. readVertices(map, mesh, #uvs)
  312. mesh.triangles = getArray(map, "triangles", 1)
  313. -- adjust triangle indices by 1, vertices are one-indexed
  314. for i,v in ipairs(mesh.triangles) do
  315. mesh.triangles[i] = v + 1
  316. end
  317. mesh.regionUVs = uvs
  318. mesh:updateUVs()
  319. mesh.hullLength = getValue(map, "hull", 0) * 2
  320. return mesh
  321. elseif type == AttachmentType.path then
  322. local path = self.attachmentLoader:newPathAttachment(skin, name)
  323. if not path then return nil end
  324. path.closed = getValue(map, "closed", false)
  325. path.constantSpeed = getValue(map, "constantSpeed", true)
  326. local vertexCount = map.vertexCount
  327. readVertices(map, path, vertexCount * 2)
  328. local lengths = utils.newNumberArray(vertexCount / 3, 0)
  329. for i,v in ipairs(map.lengths) do
  330. lengths[i] = v * scale
  331. end
  332. path.lengths = lengths
  333. local color = map.color
  334. if color then
  335. path.color:set(tonumber(color:sub(1, 2), 16) / 255,
  336. tonumber(color:sub(3, 4), 16) / 255,
  337. tonumber(color:sub(5, 6), 16) / 255,
  338. tonumber(color:sub(7, 8), 16) / 255)
  339. end
  340. return path;
  341. elseif type == AttachmentType.point then
  342. local point = self.attachmentLoader:newPointAttachment(skin, name)
  343. if not point then return nil end
  344. point.x = getValue(map, "x", 0) * scale
  345. point.y = getValue(map, "y", 0) * scale
  346. point.rotation = getValue(map, "rotation", 0)
  347. local color = map.color
  348. if color then
  349. path.color:set(tonumber(color:sub(1, 2), 16) / 255,
  350. tonumber(color:sub(3, 4), 16) / 255,
  351. tonumber(color:sub(5, 6), 16) / 255,
  352. tonumber(color:sub(7, 8), 16) / 255)
  353. end
  354. return point
  355. elseif type == AttachmentType.clipping then
  356. local clip = attachmentLoader:newClippingAttachment(skin, name)
  357. if not clip then return nil end
  358. local _end = getValue(map, "end", nil)
  359. if _end then
  360. local slot = skeletonData:findSlot(_end)
  361. if not slot then error("Clipping end slot not found: " + _end) end
  362. clip.endSlot = slot
  363. end
  364. readVertices(map, clip, map.vertexCount * 2)
  365. local color = map.color
  366. if color then
  367. clip.color:set(tonumber(color:sub(1, 2), 16) / 255,
  368. tonumber(color:sub(3, 4), 16) / 255,
  369. tonumber(color:sub(5, 6), 16) / 255,
  370. tonumber(color:sub(7, 8), 16) / 255)
  371. end
  372. return clip
  373. end
  374. error("Unknown attachment type: " .. type .. " (" .. name .. ")")
  375. end
  376. readVertices = function (map, attachment, verticesLength)
  377. local scale = self.scale
  378. attachment.worldVerticesLength = verticesLength
  379. local vertices = getArray(map, "vertices", 1)
  380. if verticesLength == #vertices then
  381. if scale ~= 1 then
  382. local i = 0
  383. local n = #vertices
  384. while i < n do
  385. vertices[i + 1] = vertices[i + 1] * scale
  386. i = i + 1
  387. end
  388. end
  389. attachment.vertices = vertices
  390. return
  391. end
  392. local weights = {}
  393. local bones = {}
  394. local i = 0
  395. local n = #vertices
  396. while i < n do
  397. local boneCount = vertices[i + 1]
  398. i = i + 1
  399. table_insert(bones, boneCount)
  400. local nn = i + boneCount * 4
  401. while i < nn do
  402. table_insert(bones, vertices[i + 1] + 1) -- +1 because bones are one-indexed
  403. table_insert(weights, vertices[i + 2] * scale)
  404. table_insert(weights, vertices[i + 3] * scale)
  405. table_insert(weights, vertices[i + 4])
  406. i = i + 4
  407. end
  408. end
  409. attachment.bones = bones
  410. attachment.vertices = weights
  411. end
  412. readAnimation = function (map, name, skeletonData)
  413. local timelines = {}
  414. local duration = 0
  415. local scale = self.scale
  416. -- Slot timelines
  417. local slotsMap = map["slots"]
  418. if slotsMap then
  419. for slotName,timelineMap in pairs(slotsMap) do
  420. local slotIndex = skeletonData.slotNameIndices[slotName]
  421. for timelineName,values in pairs(timelineMap) do
  422. if timelineName == "color" then
  423. local timeline = Animation.ColorTimeline.new(#values)
  424. timeline.slotIndex = slotIndex
  425. local frameIndex = 0
  426. for i,valueMap in ipairs(values) do
  427. local color = valueMap["color"]
  428. timeline:setFrame(
  429. frameIndex, valueMap["time"],
  430. tonumber(color:sub(1, 2), 16) / 255,
  431. tonumber(color:sub(3, 4), 16) / 255,
  432. tonumber(color:sub(5, 6), 16) / 255,
  433. tonumber(color:sub(7, 8), 16) / 255
  434. )
  435. readCurve(valueMap, timeline, frameIndex)
  436. frameIndex = frameIndex + 1
  437. end
  438. table_insert(timelines, timeline)
  439. duration = math.max(duration, timeline.frames[(timeline:getFrameCount() - 1) * Animation.ColorTimeline.ENTRIES])
  440. elseif timelineName == "twoColor" then
  441. local timeline = Animation.TwoColorTimeline.new(#values)
  442. timeline.slotIndex = slotIndex
  443. local frameIndex = 0
  444. for i,valueMap in ipairs(values) do
  445. local light = valueMap["light"]
  446. local dark = valueMap["dark"]
  447. timeline:setFrame(
  448. frameIndex, valueMap["time"],
  449. tonumber(light:sub(1, 2), 16) / 255,
  450. tonumber(light:sub(3, 4), 16) / 255,
  451. tonumber(light:sub(5, 6), 16) / 255,
  452. tonumber(light:sub(7, 8), 16) / 255,
  453. tonumber(dark:sub(1, 2), 16) / 255,
  454. tonumber(dark:sub(3, 4), 16) / 255,
  455. tonumber(dark:sub(5, 6), 16) / 255
  456. )
  457. readCurve(valueMap, timeline, frameIndex)
  458. frameIndex = frameIndex + 1
  459. end
  460. table_insert(timelines, timeline)
  461. duration = math.max(duration, timeline.frames[(timeline:getFrameCount() - 1) * Animation.TwoColorTimeline.ENTRIES])
  462. elseif timelineName == "attachment" then
  463. local timeline = Animation.AttachmentTimeline.new(#values)
  464. timeline.slotIndex = slotIndex
  465. local frameIndex = 0
  466. for i,valueMap in ipairs(values) do
  467. local attachmentName = valueMap["name"]
  468. timeline:setFrame(frameIndex, valueMap["time"], attachmentName)
  469. frameIndex = frameIndex + 1
  470. end
  471. table_insert(timelines, timeline)
  472. duration = math.max(duration, timeline.frames[timeline:getFrameCount() - 1])
  473. else
  474. error("Invalid frame type for a slot: " .. timelineName .. " (" .. slotName .. ")")
  475. end
  476. end
  477. end
  478. end
  479. -- Bone timelines
  480. local bonesMap = map["bones"]
  481. if bonesMap then
  482. for boneName,timelineMap in pairs(bonesMap) do
  483. local boneIndex = skeletonData:findBoneIndex(boneName)
  484. if boneIndex == -1 then error("Bone not found: " .. boneName) end
  485. for timelineName,values in pairs(timelineMap) do
  486. if timelineName == "rotate" then
  487. local timeline = Animation.RotateTimeline.new(#values)
  488. timeline.boneIndex = boneIndex
  489. local frameIndex = 0
  490. for i,valueMap in ipairs(values) do
  491. timeline:setFrame(frameIndex, valueMap["time"], valueMap["angle"])
  492. readCurve(valueMap, timeline, frameIndex)
  493. frameIndex = frameIndex + 1
  494. end
  495. table_insert(timelines, timeline)
  496. duration = math.max(duration, timeline.frames[(timeline:getFrameCount() - 1) * Animation.RotateTimeline.ENTRIES])
  497. elseif timelineName == "translate" or timelineName == "scale" or timelineName == "shear" then
  498. local timeline
  499. local timelineScale = 1
  500. if timelineName == "scale" then
  501. timeline = Animation.ScaleTimeline.new(#values)
  502. elseif timelineName == "shear" then
  503. timeline = Animation.ShearTimeline.new(#values)
  504. else
  505. timeline = Animation.TranslateTimeline.new(#values)
  506. timelineScale = self.scale
  507. end
  508. timeline.boneIndex = boneIndex
  509. local frameIndex = 0
  510. for i,valueMap in ipairs(values) do
  511. local x = (valueMap["x"] or 0) * timelineScale
  512. local y = (valueMap["y"] or 0) * timelineScale
  513. timeline:setFrame(frameIndex, valueMap["time"], x, y)
  514. readCurve(valueMap, timeline, frameIndex)
  515. frameIndex = frameIndex + 1
  516. end
  517. table_insert(timelines, timeline)
  518. duration = math.max(duration, timeline.frames[(timeline:getFrameCount() - 1) * Animation.TranslateTimeline.ENTRIES])
  519. else
  520. error("Invalid timeline type for a bone: " .. timelineName .. " (" .. boneName .. ")")
  521. end
  522. end
  523. end
  524. end
  525. -- IK timelines.
  526. local ik = map["ik"]
  527. if ik then
  528. for ikConstraintName,values in pairs(ik) do
  529. local ikConstraint = skeletonData:findIkConstraint(ikConstraintName)
  530. local timeline = Animation.IkConstraintTimeline.new(#values)
  531. for i,other in pairs(skeletonData.ikConstraints) do
  532. if other == ikConstraint then
  533. timeline.ikConstraintIndex = i
  534. break
  535. end
  536. end
  537. local frameIndex = 0
  538. for i,valueMap in ipairs(values) do
  539. local mix = 1
  540. if valueMap["mix"] ~= nil then mix = valueMap["mix"] end
  541. local bendPositive = 1
  542. if valueMap["bendPositive"] == false then bendPositive = -1 end
  543. timeline:setFrame(frameIndex, valueMap["time"], mix, bendPositive)
  544. readCurve(valueMap, timeline, frameIndex)
  545. frameIndex = frameIndex + 1
  546. end
  547. table_insert(timelines, timeline)
  548. duration = math.max(duration, timeline.frames[(timeline:getFrameCount() - 1) * Animation.IkConstraintTimeline.ENTRIES])
  549. end
  550. end
  551. -- Transform constraint timelines.
  552. local transform = map["transform"]
  553. if transform then
  554. for constraintName, values in pairs(transform) do
  555. local constraint = skeletonData:findTransformConstraint(constraintName)
  556. local timeline = Animation.TransformConstraintTimeline.new(#values)
  557. for i,other in pairs(skeletonData.transformConstraints) do
  558. if other == constraint then
  559. timeline.transformConstraintIndex = i
  560. break
  561. end
  562. end
  563. local frameIndex = 0
  564. for i,valueMap in ipairs(values) do
  565. timeline:setFrame(frameIndex, valueMap.time, getValue(valueMap, "rotateMix", 1), getValue(valueMap, "translateMix", 1), getValue(valueMap, "scaleMix", 1), getValue(valueMap, "shearMix", 1))
  566. readCurve(valueMap, timeline, frameIndex)
  567. frameIndex = frameIndex + 1
  568. end
  569. table_insert(timelines, timeline)
  570. duration = math.max(duration, timeline.frames[(timeline:getFrameCount() - 1) * Animation.TransformConstraintTimeline.ENTRIES])
  571. end
  572. end
  573. -- Path constraint timelines.
  574. if map.paths then
  575. for constraintName,constraintMap in pairs(map.paths) do
  576. local index = skeletonData:findPathConstraintIndex(constraintName)
  577. if index == -1 then error("Path constraint not found: " .. constraintName, 2) end
  578. local data = skeletonData.pathConstraints[index]
  579. for timelineName, timelineMap in pairs(constraintMap) do
  580. if timelineName == "position" or timelineName == "spacing" then
  581. local timeline = nil
  582. local timelineScale = 1
  583. if timelineName == "spacing" then
  584. timeline = Animation.PathConstraintSpacingTimeline.new(#timelineMap)
  585. if data.spacingMode == PathConstraintData.SpacingMode.length or data.spacingMode == PathConstraintData.SpacingMode.fixed then timelineScale = scale end
  586. else
  587. timeline = Animation.PathConstraintPositionTimeline.new(#timelineMap)
  588. if data.positionMode == PathConstraintData.PositionMode.fixed then timelineScale = scale end
  589. end
  590. timeline.pathConstraintIndex = index
  591. local frameIndex = 0
  592. for i,valueMap in ipairs(timelineMap) do
  593. timeline:setFrame(frameIndex, valueMap.time, getValue(valueMap, timelineName, 0) * timelineScale)
  594. readCurve(valueMap, timeline, frameIndex)
  595. frameIndex = frameIndex + 1
  596. end
  597. table_insert(timelines, timeline)
  598. duration = math.max(duration, timeline.frames[(timeline:getFrameCount() - 1) * Animation.PathConstraintPositionTimeline.ENTRIES])
  599. elseif timelineName == "mix" then
  600. local timeline = Animation.PathConstraintMixTimeline.new(#timelineMap)
  601. timeline.pathConstraintIndex = index
  602. local frameIndex = 0
  603. for i,valueMap in ipairs(timelineMap) do
  604. timeline:setFrame(frameIndex, valueMap.time, getValue(valueMap, "rotateMix", 1), getValue(valueMap, "translateMix", 1))
  605. readCurve(valueMap, timeline, frameIndex)
  606. frameIndex = frameIndex + 1
  607. end
  608. table_insert(timelines, timeline)
  609. duration = math.max(duration, timeline.frames[(timeline:getFrameCount() - 1) * Animation.PathConstraintMixTimeline.ENTRIES])
  610. end
  611. end
  612. end
  613. end
  614. -- Deform timelines.
  615. if map.deform then
  616. for deformName, deformMap in pairs(map.deform) do
  617. local skin = skeletonData:findSkin(deformName)
  618. if not skin then error("Skin not found: " .. deformName, 2) end
  619. for slotName,slotMap in pairs(deformMap) do
  620. local slotIndex = skeletonData:findSlotIndex(slotName)
  621. if slotIndex == -1 then error("Slot not found: " .. slotMap.name, 2) end
  622. for timelineName,timelineMap in pairs(slotMap) do
  623. local attachment = skin:getAttachment(slotIndex, timelineName)
  624. if not attachment then error("Deform attachment not found: " .. timelineMap.name, 2) end
  625. local weighted = attachment.bones ~= nil
  626. local vertices = attachment.vertices;
  627. local deformLength = #vertices
  628. if weighted then deformLength = math.floor(#vertices / 3) * 2 end
  629. local timeline = Animation.DeformTimeline.new(#timelineMap)
  630. timeline.slotIndex = slotIndex
  631. timeline.attachment = attachment
  632. local frameIndex = 0
  633. for i,valueMap in ipairs(timelineMap) do
  634. local deform = nil
  635. local verticesValue = getValue(valueMap, "vertices", nil)
  636. if verticesValue == nil then
  637. deform = vertices
  638. if weighted then deform = utils.newNumberArray(deformLength) end
  639. else
  640. deform = utils.newNumberArray(deformLength)
  641. local start = getValue(valueMap, "offset", 0) + 1
  642. utils.arrayCopy(verticesValue, 1, deform, start, #verticesValue)
  643. if scale ~= 1 then
  644. local i = start
  645. local n = i + #verticesValue
  646. while i < n do
  647. deform[i] = deform[i] * scale
  648. i = i + 1
  649. end
  650. end
  651. if not weighted then
  652. local i = 1
  653. local n = i + deformLength
  654. while i < n do
  655. deform[i] = deform[i] + vertices[i]
  656. i = i + 1
  657. end
  658. end
  659. end
  660. timeline:setFrame(frameIndex, valueMap.time, deform)
  661. readCurve(valueMap, timeline, frameIndex)
  662. frameIndex = frameIndex + 1
  663. end
  664. table_insert(timelines, timeline)
  665. duration = math.max(duration, timeline.frames[timeline:getFrameCount() - 1])
  666. end
  667. end
  668. end
  669. end
  670. -- Draworder timeline.
  671. local drawOrderValues = map["drawOrder"]
  672. if not drawOrderValues then drawOrderValues = map["draworder"] end
  673. if drawOrderValues then
  674. local timeline = Animation.DrawOrderTimeline.new(#drawOrderValues)
  675. local slotCount = #skeletonData.slots
  676. local frameIndex = 0
  677. for i,drawOrderMap in ipairs(drawOrderValues) do
  678. local drawOrder = nil
  679. local offsets = drawOrderMap["offsets"]
  680. if offsets then
  681. drawOrder = {}
  682. local unchanged = {}
  683. local originalIndex = 1
  684. local unchangedIndex = 1
  685. for ii,offsetMap in ipairs(offsets) do
  686. local slotIndex = skeletonData:findSlotIndex(offsetMap["slot"])
  687. if slotIndex == -1 then error("Slot not found: " .. offsetMap["slot"]) end
  688. -- Collect unchanged items.
  689. while originalIndex ~= slotIndex do
  690. unchanged[unchangedIndex] = originalIndex
  691. unchangedIndex = unchangedIndex + 1
  692. originalIndex = originalIndex + 1
  693. end
  694. -- Set changed items.
  695. drawOrder[originalIndex + offsetMap["offset"]] = originalIndex
  696. originalIndex = originalIndex + 1
  697. end
  698. -- Collect remaining unchanged items.
  699. while originalIndex <= slotCount do
  700. unchanged[unchangedIndex] = originalIndex
  701. unchangedIndex = unchangedIndex + 1
  702. originalIndex = originalIndex + 1
  703. end
  704. -- Fill in unchanged items.
  705. for ii = slotCount, 1, -1 do
  706. if not drawOrder[ii] then
  707. unchangedIndex = unchangedIndex - 1
  708. drawOrder[ii] = unchanged[unchangedIndex]
  709. end
  710. end
  711. end
  712. timeline:setFrame(frameIndex, drawOrderMap["time"], drawOrder)
  713. frameIndex = frameIndex + 1
  714. end
  715. table_insert(timelines, timeline)
  716. duration = math.max(duration, timeline.frames[timeline:getFrameCount() - 1])
  717. end
  718. -- Event timeline.
  719. local events = map["events"]
  720. if events then
  721. local timeline = Animation.EventTimeline.new(#events)
  722. local frameIndex = 0
  723. for i,eventMap in ipairs(events) do
  724. local eventData = skeletonData:findEvent(eventMap["name"])
  725. if not eventData then error("Event not found: " .. eventMap["name"]) end
  726. local event = Event.new(eventMap["time"], eventData)
  727. if eventMap["int"] ~= nil then
  728. event.intValue = eventMap["int"]
  729. else
  730. event.intValue = eventData.intValue
  731. end
  732. if eventMap["float"] ~= nil then
  733. event.floatValue = eventMap["float"]
  734. else
  735. event.floatValue = eventData.floatValue
  736. end
  737. if eventMap["string"] ~= nil then
  738. event.stringValue = eventMap["string"]
  739. else
  740. event.stringValue = eventData.stringValue
  741. end
  742. timeline:setFrame(frameIndex, event)
  743. frameIndex = frameIndex + 1
  744. end
  745. table_insert(timelines, timeline)
  746. duration = math.max(duration, timeline.frames[timeline:getFrameCount() - 1])
  747. end
  748. table_insert(skeletonData.animations, Animation.new(name, timelines, duration))
  749. end
  750. readCurve = function (map, timeline, frameIndex)
  751. local curve = map["curve"]
  752. if not curve then return end
  753. if curve == "stepped" then
  754. timeline:setStepped(frameIndex)
  755. elseif #curve > 0 then
  756. timeline:setCurve(frameIndex, curve[1], curve[2], curve[3], curve[4])
  757. end
  758. end
  759. getArray = function (map, name, scale)
  760. local list = map[name]
  761. local values = {}
  762. if scale == 1 then
  763. for i = 1, #list do
  764. values[i] = list[i]
  765. end
  766. else
  767. for i = 1, #list do
  768. values[i] = list[i] * scale
  769. end
  770. end
  771. return values
  772. end
  773. return self
  774. end
  775. return SkeletonJson