SkeletonJson.lua 33 KB

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