SkeletonJson.cs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, 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. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #if (UNITY_5 || UNITY_5_3_OR_NEWER || UNITY_WSA || UNITY_WP8 || UNITY_WP8_1)
  30. #define IS_UNITY
  31. #endif
  32. using System;
  33. using System.Collections.Generic;
  34. using System.IO;
  35. #if WINDOWS_STOREAPP
  36. using System.Threading.Tasks;
  37. using Windows.Storage;
  38. #endif
  39. namespace Spine {
  40. /// <summary>
  41. /// Loads skeleton data in the Spine JSON format.
  42. /// <para>
  43. /// JSON is human readable but the binary format is much smaller on disk and faster to load. See <see cref="SkeletonBinary"/>.</para>
  44. /// <para>
  45. /// See <a href="http://esotericsoftware.com/spine-json-format">Spine JSON format</a> and
  46. /// <a href = "http://esotericsoftware.com/spine-loading-skeleton-data#JSON-and-binary-data" > JSON and binary data</a> in the Spine
  47. /// Runtimes Guide.</para>
  48. /// </summary>
  49. public class SkeletonJson : SkeletonLoader {
  50. public SkeletonJson (AttachmentLoader attachmentLoader)
  51. : base(attachmentLoader) {
  52. }
  53. public SkeletonJson (params Atlas[] atlasArray)
  54. : base(atlasArray) {
  55. }
  56. #if !IS_UNITY && WINDOWS_STOREAPP
  57. private async Task<SkeletonData> ReadFile(string path) {
  58. var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
  59. var file = await folder.GetFileAsync(path).AsTask().ConfigureAwait(false);
  60. using (var reader = new StreamReader(await file.OpenStreamForReadAsync().ConfigureAwait(false))) {
  61. SkeletonData skeletonData = ReadSkeletonData(reader);
  62. skeletonData.Name = Path.GetFileNameWithoutExtension(path);
  63. return skeletonData;
  64. }
  65. }
  66. public override SkeletonData ReadSkeletonData (string path) {
  67. return this.ReadFile(path).Result;
  68. }
  69. #else
  70. public override SkeletonData ReadSkeletonData (string path) {
  71. #if WINDOWS_PHONE
  72. using (var reader = new StreamReader(Microsoft.Xna.Framework.TitleContainer.OpenStream(path))) {
  73. #else
  74. using (var reader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))) {
  75. #endif
  76. SkeletonData skeletonData = ReadSkeletonData(reader);
  77. skeletonData.name = Path.GetFileNameWithoutExtension(path);
  78. return skeletonData;
  79. }
  80. }
  81. #endif
  82. public SkeletonData ReadSkeletonData (TextReader reader) {
  83. if (reader == null) throw new ArgumentNullException("reader", "reader cannot be null.");
  84. float scale = this.scale;
  85. var skeletonData = new SkeletonData();
  86. var root = Json.Deserialize(reader) as Dictionary<string, Object>;
  87. if (root == null) throw new Exception("Invalid JSON.");
  88. // Skeleton.
  89. if (root.ContainsKey("skeleton")) {
  90. var skeletonMap = (Dictionary<string, Object>)root["skeleton"];
  91. skeletonData.hash = (string)skeletonMap["hash"];
  92. skeletonData.version = (string)skeletonMap["spine"];
  93. skeletonData.x = GetFloat(skeletonMap, "x", 0);
  94. skeletonData.y = GetFloat(skeletonMap, "y", 0);
  95. skeletonData.width = GetFloat(skeletonMap, "width", 0);
  96. skeletonData.height = GetFloat(skeletonMap, "height", 0);
  97. skeletonData.fps = GetFloat(skeletonMap, "fps", 30);
  98. skeletonData.imagesPath = GetString(skeletonMap, "images", null);
  99. skeletonData.audioPath = GetString(skeletonMap, "audio", null);
  100. }
  101. // Bones.
  102. if (root.ContainsKey("bones")) {
  103. foreach (Dictionary<string, Object> boneMap in (List<Object>)root["bones"]) {
  104. BoneData parent = null;
  105. if (boneMap.ContainsKey("parent")) {
  106. parent = skeletonData.FindBone((string)boneMap["parent"]);
  107. if (parent == null)
  108. throw new Exception("Parent bone not found: " + boneMap["parent"]);
  109. }
  110. var data = new BoneData(skeletonData.Bones.Count, (string)boneMap["name"], parent);
  111. data.length = GetFloat(boneMap, "length", 0) * scale;
  112. data.x = GetFloat(boneMap, "x", 0) * scale;
  113. data.y = GetFloat(boneMap, "y", 0) * scale;
  114. data.rotation = GetFloat(boneMap, "rotation", 0);
  115. data.scaleX = GetFloat(boneMap, "scaleX", 1);
  116. data.scaleY = GetFloat(boneMap, "scaleY", 1);
  117. data.shearX = GetFloat(boneMap, "shearX", 0);
  118. data.shearY = GetFloat(boneMap, "shearY", 0);
  119. string tm = GetString(boneMap, "transform", TransformMode.Normal.ToString());
  120. data.transformMode = (TransformMode)Enum.Parse(typeof(TransformMode), tm, true);
  121. data.skinRequired = GetBoolean(boneMap, "skin", false);
  122. skeletonData.bones.Add(data);
  123. }
  124. }
  125. // Slots.
  126. if (root.ContainsKey("slots")) {
  127. foreach (Dictionary<string, Object> slotMap in (List<Object>)root["slots"]) {
  128. var slotName = (string)slotMap["name"];
  129. var boneName = (string)slotMap["bone"];
  130. BoneData boneData = skeletonData.FindBone(boneName);
  131. if (boneData == null) throw new Exception("Slot bone not found: " + boneName);
  132. var data = new SlotData(skeletonData.Slots.Count, slotName, boneData);
  133. if (slotMap.ContainsKey("color")) {
  134. string color = (string)slotMap["color"];
  135. data.r = ToColor(color, 0);
  136. data.g = ToColor(color, 1);
  137. data.b = ToColor(color, 2);
  138. data.a = ToColor(color, 3);
  139. }
  140. if (slotMap.ContainsKey("dark")) {
  141. var color2 = (string)slotMap["dark"];
  142. data.r2 = ToColor(color2, 0, 6); // expectedLength = 6. ie. "RRGGBB"
  143. data.g2 = ToColor(color2, 1, 6);
  144. data.b2 = ToColor(color2, 2, 6);
  145. data.hasSecondColor = true;
  146. }
  147. data.attachmentName = GetString(slotMap, "attachment", null);
  148. if (slotMap.ContainsKey("blend"))
  149. data.blendMode = (BlendMode)Enum.Parse(typeof(BlendMode), (string)slotMap["blend"], true);
  150. else
  151. data.blendMode = BlendMode.Normal;
  152. skeletonData.slots.Add(data);
  153. }
  154. }
  155. // IK constraints.
  156. if (root.ContainsKey("ik")) {
  157. foreach (Dictionary<string, Object> constraintMap in (List<Object>)root["ik"]) {
  158. IkConstraintData data = new IkConstraintData((string)constraintMap["name"]);
  159. data.order = GetInt(constraintMap, "order", 0);
  160. data.skinRequired = GetBoolean(constraintMap, "skin", false);
  161. if (constraintMap.ContainsKey("bones")) {
  162. foreach (string boneName in (List<Object>)constraintMap["bones"]) {
  163. BoneData bone = skeletonData.FindBone(boneName);
  164. if (bone == null) throw new Exception("IK bone not found: " + boneName);
  165. data.bones.Add(bone);
  166. }
  167. }
  168. string targetName = (string)constraintMap["target"];
  169. data.target = skeletonData.FindBone(targetName);
  170. if (data.target == null) throw new Exception("IK target bone not found: " + targetName);
  171. data.mix = GetFloat(constraintMap, "mix", 1);
  172. data.softness = GetFloat(constraintMap, "softness", 0) * scale;
  173. data.bendDirection = GetBoolean(constraintMap, "bendPositive", true) ? 1 : -1;
  174. data.compress = GetBoolean(constraintMap, "compress", false);
  175. data.stretch = GetBoolean(constraintMap, "stretch", false);
  176. data.uniform = GetBoolean(constraintMap, "uniform", false);
  177. skeletonData.ikConstraints.Add(data);
  178. }
  179. }
  180. // Transform constraints.
  181. if (root.ContainsKey("transform")) {
  182. foreach (Dictionary<string, Object> constraintMap in (List<Object>)root["transform"]) {
  183. TransformConstraintData data = new TransformConstraintData((string)constraintMap["name"]);
  184. data.order = GetInt(constraintMap, "order", 0);
  185. data.skinRequired = GetBoolean(constraintMap, "skin", false);
  186. if (constraintMap.ContainsKey("bones")) {
  187. foreach (string boneName in (List<Object>)constraintMap["bones"]) {
  188. BoneData bone = skeletonData.FindBone(boneName);
  189. if (bone == null) throw new Exception("Transform constraint bone not found: " + boneName);
  190. data.bones.Add(bone);
  191. }
  192. }
  193. string targetName = (string)constraintMap["target"];
  194. data.target = skeletonData.FindBone(targetName);
  195. if (data.target == null) throw new Exception("Transform constraint target bone not found: " + targetName);
  196. data.local = GetBoolean(constraintMap, "local", false);
  197. data.relative = GetBoolean(constraintMap, "relative", false);
  198. data.offsetRotation = GetFloat(constraintMap, "rotation", 0);
  199. data.offsetX = GetFloat(constraintMap, "x", 0) * scale;
  200. data.offsetY = GetFloat(constraintMap, "y", 0) * scale;
  201. data.offsetScaleX = GetFloat(constraintMap, "scaleX", 0);
  202. data.offsetScaleY = GetFloat(constraintMap, "scaleY", 0);
  203. data.offsetShearY = GetFloat(constraintMap, "shearY", 0);
  204. data.mixRotate = GetFloat(constraintMap, "mixRotate", 1);
  205. data.mixX = GetFloat(constraintMap, "mixX", 1);
  206. data.mixY = GetFloat(constraintMap, "mixY", data.mixX);
  207. data.mixScaleX = GetFloat(constraintMap, "mixScaleX", 1);
  208. data.mixScaleY = GetFloat(constraintMap, "mixScaleY", data.mixScaleX);
  209. data.mixShearY = GetFloat(constraintMap, "mixShearY", 1);
  210. skeletonData.transformConstraints.Add(data);
  211. }
  212. }
  213. // Path constraints.
  214. if (root.ContainsKey("path")) {
  215. foreach (Dictionary<string, Object> constraintMap in (List<Object>)root["path"]) {
  216. PathConstraintData data = new PathConstraintData((string)constraintMap["name"]);
  217. data.order = GetInt(constraintMap, "order", 0);
  218. data.skinRequired = GetBoolean(constraintMap, "skin", false);
  219. if (constraintMap.ContainsKey("bones")) {
  220. foreach (string boneName in (List<Object>)constraintMap["bones"]) {
  221. BoneData bone = skeletonData.FindBone(boneName);
  222. if (bone == null) throw new Exception("Path bone not found: " + boneName);
  223. data.bones.Add(bone);
  224. }
  225. }
  226. string targetName = (string)constraintMap["target"];
  227. data.target = skeletonData.FindSlot(targetName);
  228. if (data.target == null) throw new Exception("Path target slot not found: " + targetName);
  229. data.positionMode = (PositionMode)Enum.Parse(typeof(PositionMode), GetString(constraintMap, "positionMode", "percent"), true);
  230. data.spacingMode = (SpacingMode)Enum.Parse(typeof(SpacingMode), GetString(constraintMap, "spacingMode", "length"), true);
  231. data.rotateMode = (RotateMode)Enum.Parse(typeof(RotateMode), GetString(constraintMap, "rotateMode", "tangent"), true);
  232. data.offsetRotation = GetFloat(constraintMap, "rotation", 0);
  233. data.position = GetFloat(constraintMap, "position", 0);
  234. if (data.positionMode == PositionMode.Fixed) data.position *= scale;
  235. data.spacing = GetFloat(constraintMap, "spacing", 0);
  236. if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) data.spacing *= scale;
  237. data.mixRotate = GetFloat(constraintMap, "mixRotate", 1);
  238. data.mixX = GetFloat(constraintMap, "mixX", 1);
  239. data.mixY = GetFloat(constraintMap, "mixY", data.mixX);
  240. skeletonData.pathConstraints.Add(data);
  241. }
  242. }
  243. // Skins.
  244. if (root.ContainsKey("skins")) {
  245. foreach (Dictionary<string, object> skinMap in (List<object>)root["skins"]) {
  246. Skin skin = new Skin((string)skinMap["name"]);
  247. if (skinMap.ContainsKey("bones")) {
  248. foreach (string entryName in (List<Object>)skinMap["bones"]) {
  249. BoneData bone = skeletonData.FindBone(entryName);
  250. if (bone == null) throw new Exception("Skin bone not found: " + entryName);
  251. skin.bones.Add(bone);
  252. }
  253. }
  254. skin.bones.TrimExcess();
  255. if (skinMap.ContainsKey("ik")) {
  256. foreach (string entryName in (List<Object>)skinMap["ik"]) {
  257. IkConstraintData constraint = skeletonData.FindIkConstraint(entryName);
  258. if (constraint == null) throw new Exception("Skin IK constraint not found: " + entryName);
  259. skin.constraints.Add(constraint);
  260. }
  261. }
  262. if (skinMap.ContainsKey("transform")) {
  263. foreach (string entryName in (List<Object>)skinMap["transform"]) {
  264. TransformConstraintData constraint = skeletonData.FindTransformConstraint(entryName);
  265. if (constraint == null) throw new Exception("Skin transform constraint not found: " + entryName);
  266. skin.constraints.Add(constraint);
  267. }
  268. }
  269. if (skinMap.ContainsKey("path")) {
  270. foreach (string entryName in (List<Object>)skinMap["path"]) {
  271. PathConstraintData constraint = skeletonData.FindPathConstraint(entryName);
  272. if (constraint == null) throw new Exception("Skin path constraint not found: " + entryName);
  273. skin.constraints.Add(constraint);
  274. }
  275. }
  276. skin.constraints.TrimExcess();
  277. if (skinMap.ContainsKey("attachments")) {
  278. foreach (KeyValuePair<string, Object> slotEntry in (Dictionary<string, Object>)skinMap["attachments"]) {
  279. int slotIndex = FindSlotIndex(skeletonData, slotEntry.Key);
  280. foreach (KeyValuePair<string, Object> entry in ((Dictionary<string, Object>)slotEntry.Value)) {
  281. try {
  282. Attachment attachment = ReadAttachment((Dictionary<string, Object>)entry.Value, skin, slotIndex, entry.Key, skeletonData);
  283. if (attachment != null) skin.SetAttachment(slotIndex, entry.Key, attachment);
  284. } catch (Exception e) {
  285. throw new Exception("Error reading attachment: " + entry.Key + ", skin: " + skin, e);
  286. }
  287. }
  288. }
  289. }
  290. skeletonData.skins.Add(skin);
  291. if (skin.name == "default") skeletonData.defaultSkin = skin;
  292. }
  293. }
  294. // Linked meshes.
  295. for (int i = 0, n = linkedMeshes.Count; i < n; i++) {
  296. LinkedMesh linkedMesh = linkedMeshes[i];
  297. Skin skin = linkedMesh.skin == null ? skeletonData.defaultSkin : skeletonData.FindSkin(linkedMesh.skin);
  298. if (skin == null) throw new Exception("Slot not found: " + linkedMesh.skin);
  299. Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent);
  300. if (parent == null) throw new Exception("Parent mesh not found: " + linkedMesh.parent);
  301. linkedMesh.mesh.DeformAttachment = linkedMesh.inheritDeform ? (VertexAttachment)parent : linkedMesh.mesh;
  302. linkedMesh.mesh.ParentMesh = (MeshAttachment)parent;
  303. linkedMesh.mesh.UpdateUVs();
  304. }
  305. linkedMeshes.Clear();
  306. // Events.
  307. if (root.ContainsKey("events")) {
  308. foreach (KeyValuePair<string, Object> entry in (Dictionary<string, Object>)root["events"]) {
  309. var entryMap = (Dictionary<string, Object>)entry.Value;
  310. var data = new EventData(entry.Key);
  311. data.Int = GetInt(entryMap, "int", 0);
  312. data.Float = GetFloat(entryMap, "float", 0);
  313. data.String = GetString(entryMap, "string", string.Empty);
  314. data.AudioPath = GetString(entryMap, "audio", null);
  315. if (data.AudioPath != null) {
  316. data.Volume = GetFloat(entryMap, "volume", 1);
  317. data.Balance = GetFloat(entryMap, "balance", 0);
  318. }
  319. skeletonData.events.Add(data);
  320. }
  321. }
  322. // Animations.
  323. if (root.ContainsKey("animations")) {
  324. foreach (KeyValuePair<string, Object> entry in (Dictionary<string, Object>)root["animations"]) {
  325. try {
  326. ReadAnimation((Dictionary<string, Object>)entry.Value, entry.Key, skeletonData);
  327. } catch (Exception e) {
  328. throw new Exception("Error reading animation: " + entry.Key + "\n" + e.Message, e);
  329. }
  330. }
  331. }
  332. skeletonData.bones.TrimExcess();
  333. skeletonData.slots.TrimExcess();
  334. skeletonData.skins.TrimExcess();
  335. skeletonData.events.TrimExcess();
  336. skeletonData.animations.TrimExcess();
  337. skeletonData.ikConstraints.TrimExcess();
  338. return skeletonData;
  339. }
  340. private Attachment ReadAttachment (Dictionary<string, Object> map, Skin skin, int slotIndex, string name, SkeletonData skeletonData) {
  341. float scale = this.scale;
  342. name = GetString(map, "name", name);
  343. var typeName = GetString(map, "type", "region");
  344. var type = (AttachmentType)Enum.Parse(typeof(AttachmentType), typeName, true);
  345. string path = GetString(map, "path", name);
  346. switch (type) {
  347. case AttachmentType.Region:
  348. RegionAttachment region = attachmentLoader.NewRegionAttachment(skin, name, path);
  349. if (region == null) return null;
  350. region.Path = path;
  351. region.x = GetFloat(map, "x", 0) * scale;
  352. region.y = GetFloat(map, "y", 0) * scale;
  353. region.scaleX = GetFloat(map, "scaleX", 1);
  354. region.scaleY = GetFloat(map, "scaleY", 1);
  355. region.rotation = GetFloat(map, "rotation", 0);
  356. region.width = GetFloat(map, "width", 32) * scale;
  357. region.height = GetFloat(map, "height", 32) * scale;
  358. if (map.ContainsKey("color")) {
  359. var color = (string)map["color"];
  360. region.r = ToColor(color, 0);
  361. region.g = ToColor(color, 1);
  362. region.b = ToColor(color, 2);
  363. region.a = ToColor(color, 3);
  364. }
  365. region.UpdateOffset();
  366. return region;
  367. case AttachmentType.Boundingbox:
  368. BoundingBoxAttachment box = attachmentLoader.NewBoundingBoxAttachment(skin, name);
  369. if (box == null) return null;
  370. ReadVertices(map, box, GetInt(map, "vertexCount", 0) << 1);
  371. return box;
  372. case AttachmentType.Mesh:
  373. case AttachmentType.Linkedmesh: {
  374. MeshAttachment mesh = attachmentLoader.NewMeshAttachment(skin, name, path);
  375. if (mesh == null) return null;
  376. mesh.Path = path;
  377. if (map.ContainsKey("color")) {
  378. var color = (string)map["color"];
  379. mesh.r = ToColor(color, 0);
  380. mesh.g = ToColor(color, 1);
  381. mesh.b = ToColor(color, 2);
  382. mesh.a = ToColor(color, 3);
  383. }
  384. mesh.Width = GetFloat(map, "width", 0) * scale;
  385. mesh.Height = GetFloat(map, "height", 0) * scale;
  386. string parent = GetString(map, "parent", null);
  387. if (parent != null) {
  388. linkedMeshes.Add(new LinkedMesh(mesh, GetString(map, "skin", null), slotIndex, parent, GetBoolean(map, "deform", true)));
  389. return mesh;
  390. }
  391. float[] uvs = GetFloatArray(map, "uvs", 1);
  392. ReadVertices(map, mesh, uvs.Length);
  393. mesh.triangles = GetIntArray(map, "triangles");
  394. mesh.regionUVs = uvs;
  395. mesh.UpdateUVs();
  396. if (map.ContainsKey("hull")) mesh.HullLength = GetInt(map, "hull", 0) << 1;
  397. if (map.ContainsKey("edges")) mesh.Edges = GetIntArray(map, "edges");
  398. return mesh;
  399. }
  400. case AttachmentType.Path: {
  401. PathAttachment pathAttachment = attachmentLoader.NewPathAttachment(skin, name);
  402. if (pathAttachment == null) return null;
  403. pathAttachment.closed = GetBoolean(map, "closed", false);
  404. pathAttachment.constantSpeed = GetBoolean(map, "constantSpeed", true);
  405. int vertexCount = GetInt(map, "vertexCount", 0);
  406. ReadVertices(map, pathAttachment, vertexCount << 1);
  407. // potential BOZO see Java impl
  408. pathAttachment.lengths = GetFloatArray(map, "lengths", scale);
  409. return pathAttachment;
  410. }
  411. case AttachmentType.Point: {
  412. PointAttachment point = attachmentLoader.NewPointAttachment(skin, name);
  413. if (point == null) return null;
  414. point.x = GetFloat(map, "x", 0) * scale;
  415. point.y = GetFloat(map, "y", 0) * scale;
  416. point.rotation = GetFloat(map, "rotation", 0);
  417. //string color = GetString(map, "color", null);
  418. //if (color != null) point.color = color;
  419. return point;
  420. }
  421. case AttachmentType.Clipping: {
  422. ClippingAttachment clip = attachmentLoader.NewClippingAttachment(skin, name);
  423. if (clip == null) return null;
  424. string end = GetString(map, "end", null);
  425. if (end != null) {
  426. SlotData slot = skeletonData.FindSlot(end);
  427. if (slot == null) throw new Exception("Clipping end slot not found: " + end);
  428. clip.EndSlot = slot;
  429. }
  430. ReadVertices(map, clip, GetInt(map, "vertexCount", 0) << 1);
  431. //string color = GetString(map, "color", null);
  432. // if (color != null) clip.color = color;
  433. return clip;
  434. }
  435. }
  436. return null;
  437. }
  438. private void ReadVertices (Dictionary<string, Object> map, VertexAttachment attachment, int verticesLength) {
  439. attachment.WorldVerticesLength = verticesLength;
  440. float[] vertices = GetFloatArray(map, "vertices", 1);
  441. float scale = Scale;
  442. if (verticesLength == vertices.Length) {
  443. if (scale != 1) {
  444. for (int i = 0; i < vertices.Length; i++) {
  445. vertices[i] *= scale;
  446. }
  447. }
  448. attachment.vertices = vertices;
  449. return;
  450. }
  451. ExposedList<float> weights = new ExposedList<float>(verticesLength * 3 * 3);
  452. ExposedList<int> bones = new ExposedList<int>(verticesLength * 3);
  453. for (int i = 0, n = vertices.Length; i < n;) {
  454. int boneCount = (int)vertices[i++];
  455. bones.Add(boneCount);
  456. for (int nn = i + (boneCount << 2); i < nn; i += 4) {
  457. bones.Add((int)vertices[i]);
  458. weights.Add(vertices[i + 1] * this.Scale);
  459. weights.Add(vertices[i + 2] * this.Scale);
  460. weights.Add(vertices[i + 3]);
  461. }
  462. }
  463. attachment.bones = bones.ToArray();
  464. attachment.vertices = weights.ToArray();
  465. }
  466. private int FindSlotIndex (SkeletonData skeletonData, string slotName) {
  467. SlotData[] slots = skeletonData.slots.Items;
  468. for (int i = 0, n = skeletonData.slots.Count; i < n; i++)
  469. if (slots[i].name == slotName) return i;
  470. throw new Exception("Slot not found: " + slotName);
  471. }
  472. private void ReadAnimation (Dictionary<string, Object> map, string name, SkeletonData skeletonData) {
  473. var scale = this.scale;
  474. var timelines = new ExposedList<Timeline>();
  475. // Slot timelines.
  476. if (map.ContainsKey("slots")) {
  477. foreach (KeyValuePair<string, Object> entry in (Dictionary<string, Object>)map["slots"]) {
  478. string slotName = entry.Key;
  479. int slotIndex = FindSlotIndex(skeletonData, slotName);
  480. var timelineMap = (Dictionary<string, Object>)entry.Value;
  481. foreach (KeyValuePair<string, Object> timelineEntry in timelineMap) {
  482. var values = (List<Object>)timelineEntry.Value;
  483. int frames = values.Count;
  484. if (frames == 0) continue;
  485. var timelineName = (string)timelineEntry.Key;
  486. if (timelineName == "attachment") {
  487. var timeline = new AttachmentTimeline(frames, slotIndex);
  488. int frame = 0;
  489. foreach (Dictionary<string, Object> keyMap in values) {
  490. timeline.SetFrame(frame++, GetFloat(keyMap, "time", 0), (string)keyMap["name"]);
  491. }
  492. timelines.Add(timeline);
  493. } else if (timelineName == "rgba") {
  494. var timeline = new RGBATimeline(frames, frames << 2, slotIndex);
  495. var keyMapEnumerator = values.GetEnumerator();
  496. keyMapEnumerator.MoveNext();
  497. var keyMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  498. float time = GetFloat(keyMap, "time", 0);
  499. string color = (string)keyMap["color"];
  500. float r = ToColor(color, 0);
  501. float g = ToColor(color, 1);
  502. float b = ToColor(color, 2);
  503. float a = ToColor(color, 3);
  504. for (int frame = 0, bezier = 0; ; frame++) {
  505. timeline.SetFrame(frame, time, r, g, b, a);
  506. if (!keyMapEnumerator.MoveNext()) {
  507. timeline.Shrink(bezier);
  508. break;
  509. }
  510. var nextMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  511. float time2 = GetFloat(nextMap, "time", 0);
  512. color = (string)nextMap["color"];
  513. float nr = ToColor(color, 0);
  514. float ng = ToColor(color, 1);
  515. float nb = ToColor(color, 2);
  516. float na = ToColor(color, 3);
  517. if (keyMap.ContainsKey("curve")) {
  518. object curve = keyMap["curve"];
  519. bezier = ReadCurve(curve, timeline, bezier, frame, 0, time, time2, r, nr, 1);
  520. bezier = ReadCurve(curve, timeline, bezier, frame, 1, time, time2, g, ng, 1);
  521. bezier = ReadCurve(curve, timeline, bezier, frame, 2, time, time2, b, nb, 1);
  522. bezier = ReadCurve(curve, timeline, bezier, frame, 3, time, time2, a, na, 1);
  523. }
  524. time = time2;
  525. r = nr;
  526. g = ng;
  527. b = nb;
  528. a = na;
  529. keyMap = nextMap;
  530. }
  531. timelines.Add(timeline);
  532. } else if (timelineName == "rgb") {
  533. var timeline = new RGBTimeline(frames, frames * 3, slotIndex);
  534. var keyMapEnumerator = values.GetEnumerator();
  535. keyMapEnumerator.MoveNext();
  536. var keyMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  537. float time = GetFloat(keyMap, "time", 0);
  538. string color = (string)keyMap["color"];
  539. float r = ToColor(color, 0, 6);
  540. float g = ToColor(color, 1, 6);
  541. float b = ToColor(color, 2, 6);
  542. for (int frame = 0, bezier = 0; ; frame++) {
  543. timeline.SetFrame(frame, time, r, g, b);
  544. if (!keyMapEnumerator.MoveNext()) {
  545. timeline.Shrink(bezier);
  546. break;
  547. }
  548. var nextMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  549. float time2 = GetFloat(nextMap, "time", 0);
  550. color = (string)nextMap["color"];
  551. float nr = ToColor(color, 0, 6);
  552. float ng = ToColor(color, 1, 6);
  553. float nb = ToColor(color, 2, 6);
  554. if (keyMap.ContainsKey("curve")) {
  555. object curve = keyMap["curve"];
  556. bezier = ReadCurve(curve, timeline, bezier, frame, 0, time, time2, r, nr, 1);
  557. bezier = ReadCurve(curve, timeline, bezier, frame, 1, time, time2, g, ng, 1);
  558. bezier = ReadCurve(curve, timeline, bezier, frame, 2, time, time2, b, nb, 1);
  559. }
  560. time = time2;
  561. r = nr;
  562. g = ng;
  563. b = nb;
  564. keyMap = nextMap;
  565. }
  566. timelines.Add(timeline);
  567. } else if (timelineName == "alpha") {
  568. var keyMapEnumerator = values.GetEnumerator();
  569. keyMapEnumerator.MoveNext();
  570. timelines.Add(ReadTimeline(ref keyMapEnumerator, new AlphaTimeline(frames, frames, slotIndex), 0, 1));
  571. } else if (timelineName == "rgba2") {
  572. var timeline = new RGBA2Timeline(frames, frames * 7, slotIndex);
  573. var keyMapEnumerator = values.GetEnumerator();
  574. keyMapEnumerator.MoveNext();
  575. var keyMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  576. float time = GetFloat(keyMap, "time", 0);
  577. string color = (string)keyMap["light"];
  578. float r = ToColor(color, 0);
  579. float g = ToColor(color, 1);
  580. float b = ToColor(color, 2);
  581. float a = ToColor(color, 3);
  582. color = (string)keyMap["dark"];
  583. float r2 = ToColor(color, 0, 6);
  584. float g2 = ToColor(color, 1, 6);
  585. float b2 = ToColor(color, 2, 6);
  586. for (int frame = 0, bezier = 0; ; frame++) {
  587. timeline.SetFrame(frame, time, r, g, b, a, r2, g2, b2);
  588. if (!keyMapEnumerator.MoveNext()) {
  589. timeline.Shrink(bezier);
  590. break;
  591. }
  592. var nextMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  593. float time2 = GetFloat(nextMap, "time", 0);
  594. color = (string)nextMap["light"];
  595. float nr = ToColor(color, 0);
  596. float ng = ToColor(color, 1);
  597. float nb = ToColor(color, 2);
  598. float na = ToColor(color, 3);
  599. color = (string)nextMap["dark"];
  600. float nr2 = ToColor(color, 0, 6);
  601. float ng2 = ToColor(color, 1, 6);
  602. float nb2 = ToColor(color, 2, 6);
  603. if (keyMap.ContainsKey("curve")) {
  604. object curve = keyMap["curve"];
  605. bezier = ReadCurve(curve, timeline, bezier, frame, 0, time, time2, r, nr, 1);
  606. bezier = ReadCurve(curve, timeline, bezier, frame, 1, time, time2, g, ng, 1);
  607. bezier = ReadCurve(curve, timeline, bezier, frame, 2, time, time2, b, nb, 1);
  608. bezier = ReadCurve(curve, timeline, bezier, frame, 3, time, time2, a, na, 1);
  609. bezier = ReadCurve(curve, timeline, bezier, frame, 4, time, time2, r2, nr2, 1);
  610. bezier = ReadCurve(curve, timeline, bezier, frame, 5, time, time2, g2, ng2, 1);
  611. bezier = ReadCurve(curve, timeline, bezier, frame, 6, time, time2, b2, nb2, 1);
  612. }
  613. time = time2;
  614. r = nr;
  615. g = ng;
  616. b = nb;
  617. a = na;
  618. r2 = nr2;
  619. g2 = ng2;
  620. b2 = nb2;
  621. keyMap = nextMap;
  622. }
  623. timelines.Add(timeline);
  624. } else if (timelineName == "rgb2") {
  625. var timeline = new RGB2Timeline(frames, frames * 6, slotIndex);
  626. var keyMapEnumerator = values.GetEnumerator();
  627. keyMapEnumerator.MoveNext();
  628. var keyMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  629. float time = GetFloat(keyMap, "time", 0);
  630. string color = (string)keyMap["light"];
  631. float r = ToColor(color, 0, 6);
  632. float g = ToColor(color, 1, 6);
  633. float b = ToColor(color, 2, 6);
  634. color = (string)keyMap["dark"];
  635. float r2 = ToColor(color, 0, 6);
  636. float g2 = ToColor(color, 1, 6);
  637. float b2 = ToColor(color, 2, 6);
  638. for (int frame = 0, bezier = 0; ; frame++) {
  639. timeline.SetFrame(frame, time, r, g, b, r2, g2, b2);
  640. if (!keyMapEnumerator.MoveNext()) {
  641. timeline.Shrink(bezier);
  642. break;
  643. }
  644. var nextMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  645. float time2 = GetFloat(nextMap, "time", 0);
  646. color = (string)nextMap["light"];
  647. float nr = ToColor(color, 0, 6);
  648. float ng = ToColor(color, 1, 6);
  649. float nb = ToColor(color, 2, 6);
  650. color = (string)nextMap["dark"];
  651. float nr2 = ToColor(color, 0, 6);
  652. float ng2 = ToColor(color, 1, 6);
  653. float nb2 = ToColor(color, 2, 6);
  654. if (keyMap.ContainsKey("curve")) {
  655. object curve = keyMap["curve"];
  656. bezier = ReadCurve(curve, timeline, bezier, frame, 0, time, time2, r, nr, 1);
  657. bezier = ReadCurve(curve, timeline, bezier, frame, 1, time, time2, g, ng, 1);
  658. bezier = ReadCurve(curve, timeline, bezier, frame, 2, time, time2, b, nb, 1);
  659. bezier = ReadCurve(curve, timeline, bezier, frame, 3, time, time2, r2, nr2, 1);
  660. bezier = ReadCurve(curve, timeline, bezier, frame, 4, time, time2, g2, ng2, 1);
  661. bezier = ReadCurve(curve, timeline, bezier, frame, 5, time, time2, b2, nb2, 1);
  662. }
  663. time = time2;
  664. r = nr;
  665. g = ng;
  666. b = nb;
  667. r2 = nr2;
  668. g2 = ng2;
  669. b2 = nb2;
  670. keyMap = nextMap;
  671. }
  672. timelines.Add(timeline);
  673. } else
  674. throw new Exception("Invalid timeline type for a slot: " + timelineName + " (" + slotName + ")");
  675. }
  676. }
  677. }
  678. // Bone timelines.
  679. if (map.ContainsKey("bones")) {
  680. foreach (KeyValuePair<string, Object> entry in (Dictionary<string, Object>)map["bones"]) {
  681. string boneName = entry.Key;
  682. int boneIndex = -1;
  683. var bones = skeletonData.bones.Items;
  684. for (int i = 0, n = skeletonData.bones.Count; i < n; i++) {
  685. if (bones[i].name == boneName) {
  686. boneIndex = i;
  687. break;
  688. }
  689. }
  690. if (boneIndex == -1) throw new Exception("Bone not found: " + boneName);
  691. var timelineMap = (Dictionary<string, Object>)entry.Value;
  692. foreach (KeyValuePair<string, Object> timelineEntry in timelineMap) {
  693. var values = (List<Object>)timelineEntry.Value;
  694. var keyMapEnumerator = values.GetEnumerator();
  695. if (!keyMapEnumerator.MoveNext()) continue;
  696. int frames = values.Count;
  697. var timelineName = (string)timelineEntry.Key;
  698. if (timelineName == "rotate")
  699. timelines.Add(ReadTimeline(ref keyMapEnumerator, new RotateTimeline(frames, frames, boneIndex), 0, 1));
  700. else if (timelineName == "translate") {
  701. TranslateTimeline timeline = new TranslateTimeline(frames, frames << 1, boneIndex);
  702. timelines.Add(ReadTimeline(ref keyMapEnumerator, timeline, "x", "y", 0, scale));
  703. } else if (timelineName == "translatex") {
  704. timelines
  705. .Add(ReadTimeline(ref keyMapEnumerator, new TranslateXTimeline(frames, frames, boneIndex), 0, scale));
  706. } else if (timelineName == "translatey") {
  707. timelines
  708. .Add(ReadTimeline(ref keyMapEnumerator, new TranslateYTimeline(frames, frames, boneIndex), 0, scale));
  709. } else if (timelineName == "scale") {
  710. ScaleTimeline timeline = new ScaleTimeline(frames, frames << 1, boneIndex);
  711. timelines.Add(ReadTimeline(ref keyMapEnumerator, timeline, "x", "y", 1, 1));
  712. } else if (timelineName == "scalex")
  713. timelines.Add(ReadTimeline(ref keyMapEnumerator, new ScaleXTimeline(frames, frames, boneIndex), 1, 1));
  714. else if (timelineName == "scaley")
  715. timelines.Add(ReadTimeline(ref keyMapEnumerator, new ScaleYTimeline(frames, frames, boneIndex), 1, 1));
  716. else if (timelineName == "shear") {
  717. ShearTimeline timeline = new ShearTimeline(frames, frames << 1, boneIndex);
  718. timelines.Add(ReadTimeline(ref keyMapEnumerator, timeline, "x", "y", 0, 1));
  719. } else if (timelineName == "shearx")
  720. timelines.Add(ReadTimeline(ref keyMapEnumerator, new ShearXTimeline(frames, frames, boneIndex), 0, 1));
  721. else if (timelineName == "sheary")
  722. timelines.Add(ReadTimeline(ref keyMapEnumerator, new ShearYTimeline(frames, frames, boneIndex), 0, 1));
  723. else
  724. throw new Exception("Invalid timeline type for a bone: " + timelineName + " (" + boneName + ")");
  725. }
  726. }
  727. }
  728. // IK constraint timelines.
  729. if (map.ContainsKey("ik")) {
  730. foreach (KeyValuePair<string, Object> timelineMap in (Dictionary<string, Object>)map["ik"]) {
  731. var values = (List<Object>)timelineMap.Value;
  732. var keyMapEnumerator = values.GetEnumerator();
  733. if (!keyMapEnumerator.MoveNext()) continue;
  734. var keyMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  735. IkConstraintData constraint = skeletonData.FindIkConstraint(timelineMap.Key);
  736. IkConstraintTimeline timeline = new IkConstraintTimeline(values.Count, values.Count << 1,
  737. skeletonData.IkConstraints.IndexOf(constraint));
  738. float time = GetFloat(keyMap, "time", 0);
  739. float mix = GetFloat(keyMap, "mix", 1), softness = GetFloat(keyMap, "softness", 0) * scale;
  740. for (int frame = 0, bezier = 0; ; frame++) {
  741. timeline.SetFrame(frame, time, mix, softness, GetBoolean(keyMap, "bendPositive", true) ? 1 : -1,
  742. GetBoolean(keyMap, "compress", false), GetBoolean(keyMap, "stretch", false));
  743. if (!keyMapEnumerator.MoveNext()) {
  744. timeline.Shrink(bezier);
  745. break;
  746. }
  747. var nextMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  748. float time2 = GetFloat(nextMap, "time", 0);
  749. float mix2 = GetFloat(nextMap, "mix", 1), softness2 = GetFloat(nextMap, "softness", 0) * scale;
  750. if (keyMap.ContainsKey("curve")) {
  751. object curve = keyMap["curve"];
  752. bezier = ReadCurve(curve, timeline, bezier, frame, 0, time, time2, mix, mix2, 1);
  753. bezier = ReadCurve(curve, timeline, bezier, frame, 1, time, time2, softness, softness2, scale);
  754. }
  755. time = time2;
  756. mix = mix2;
  757. softness = softness2;
  758. keyMap = nextMap;
  759. }
  760. timelines.Add(timeline);
  761. }
  762. }
  763. // Transform constraint timelines.
  764. if (map.ContainsKey("transform")) {
  765. foreach (KeyValuePair<string, Object> timelineMap in (Dictionary<string, Object>)map["transform"]) {
  766. var values = (List<Object>)timelineMap.Value;
  767. var keyMapEnumerator = values.GetEnumerator();
  768. if (!keyMapEnumerator.MoveNext()) continue;
  769. var keyMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  770. TransformConstraintData constraint = skeletonData.FindTransformConstraint(timelineMap.Key);
  771. TransformConstraintTimeline timeline = new TransformConstraintTimeline(values.Count, values.Count * 6,
  772. skeletonData.TransformConstraints.IndexOf(constraint));
  773. float time = GetFloat(keyMap, "time", 0);
  774. float mixRotate = GetFloat(keyMap, "mixRotate", 1), mixShearY = GetFloat(keyMap, "mixShearY", 1);
  775. float mixX = GetFloat(keyMap, "mixX", 1), mixY = GetFloat(keyMap, "mixY", mixX);
  776. float mixScaleX = GetFloat(keyMap, "mixScaleX", 1), mixScaleY = GetFloat(keyMap, "mixScaleY", mixScaleX);
  777. for (int frame = 0, bezier = 0; ; frame++) {
  778. timeline.SetFrame(frame, time, mixRotate, mixX, mixY, mixScaleX, mixScaleY, mixShearY);
  779. if (!keyMapEnumerator.MoveNext()) {
  780. timeline.Shrink(bezier);
  781. break;
  782. }
  783. var nextMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  784. float time2 = GetFloat(nextMap, "time", 0);
  785. float mixRotate2 = GetFloat(nextMap, "mixRotate", 1), mixShearY2 = GetFloat(nextMap, "mixShearY", 1);
  786. float mixX2 = GetFloat(nextMap, "mixX", 1), mixY2 = GetFloat(nextMap, "mixY", mixX2);
  787. float mixScaleX2 = GetFloat(nextMap, "mixScaleX", 1), mixScaleY2 = GetFloat(nextMap, "mixScaleY", mixScaleX2);
  788. if (keyMap.ContainsKey("curve")) {
  789. object curve = keyMap["curve"];
  790. bezier = ReadCurve(curve, timeline, bezier, frame, 0, time, time2, mixRotate, mixRotate2, 1);
  791. bezier = ReadCurve(curve, timeline, bezier, frame, 1, time, time2, mixX, mixX2, 1);
  792. bezier = ReadCurve(curve, timeline, bezier, frame, 2, time, time2, mixY, mixY2, 1);
  793. bezier = ReadCurve(curve, timeline, bezier, frame, 3, time, time2, mixScaleX, mixScaleX2, 1);
  794. bezier = ReadCurve(curve, timeline, bezier, frame, 4, time, time2, mixScaleY, mixScaleY2, 1);
  795. bezier = ReadCurve(curve, timeline, bezier, frame, 5, time, time2, mixShearY, mixShearY2, 1);
  796. }
  797. time = time2;
  798. mixRotate = mixRotate2;
  799. mixX = mixX2;
  800. mixY = mixY2;
  801. mixScaleX = mixScaleX2;
  802. mixScaleY = mixScaleY2;
  803. mixScaleX = mixScaleX2;
  804. keyMap = nextMap;
  805. }
  806. timelines.Add(timeline);
  807. }
  808. }
  809. // Path constraint timelines.
  810. if (map.ContainsKey("path")) {
  811. foreach (KeyValuePair<string, Object> constraintMap in (Dictionary<string, Object>)map["path"]) {
  812. PathConstraintData constraint = skeletonData.FindPathConstraint(constraintMap.Key);
  813. if (constraint == null) throw new Exception("Path constraint not found: " + constraintMap.Key);
  814. int constraintIndex = skeletonData.pathConstraints.IndexOf(constraint);
  815. var timelineMap = (Dictionary<string, Object>)constraintMap.Value;
  816. foreach (KeyValuePair<string, Object> timelineEntry in timelineMap) {
  817. var values = (List<Object>)timelineEntry.Value;
  818. var keyMapEnumerator = values.GetEnumerator();
  819. if (!keyMapEnumerator.MoveNext()) continue;
  820. int frames = values.Count;
  821. var timelineName = (string)timelineEntry.Key;
  822. if (timelineName == "position") {
  823. CurveTimeline1 timeline = new PathConstraintPositionTimeline(frames, frames, constraintIndex);
  824. timelines.Add(ReadTimeline(ref keyMapEnumerator, timeline, 0, constraint.positionMode == PositionMode.Fixed ? scale : 1));
  825. } else if (timelineName == "spacing") {
  826. CurveTimeline1 timeline = new PathConstraintSpacingTimeline(frames, frames, constraintIndex);
  827. timelines.Add(ReadTimeline(ref keyMapEnumerator, timeline, 0,
  828. constraint.spacingMode == SpacingMode.Length || constraint.spacingMode == SpacingMode.Fixed ? scale : 1));
  829. } else if (timelineName == "mix") {
  830. PathConstraintMixTimeline timeline = new PathConstraintMixTimeline(frames, frames * 3, constraintIndex);
  831. var keyMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  832. float time = GetFloat(keyMap, "time", 0);
  833. float mixRotate = GetFloat(keyMap, "mixRotate", 1);
  834. float mixX = GetFloat(keyMap, "mixX", 1), mixY = GetFloat(keyMap, "mixY", mixX);
  835. for (int frame = 0, bezier = 0; ; frame++) {
  836. timeline.SetFrame(frame, time, mixRotate, mixX, mixY);
  837. if (!keyMapEnumerator.MoveNext()) {
  838. timeline.Shrink(bezier);
  839. break;
  840. }
  841. var nextMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  842. float time2 = GetFloat(nextMap, "time", 0);
  843. float mixRotate2 = GetFloat(nextMap, "mixRotate", 1);
  844. float mixX2 = GetFloat(nextMap, "mixX", 1), mixY2 = GetFloat(nextMap, "mixY", mixX2);
  845. if (keyMap.ContainsKey("curve")) {
  846. object curve = keyMap["curve"];
  847. bezier = ReadCurve(curve, timeline, bezier, frame, 0, time, time2, mixRotate, mixRotate2, 1);
  848. bezier = ReadCurve(curve, timeline, bezier, frame, 1, time, time2, mixX, mixX2, 1);
  849. bezier = ReadCurve(curve, timeline, bezier, frame, 2, time, time2, mixY, mixY2, 1);
  850. }
  851. time = time2;
  852. mixRotate = mixRotate2;
  853. mixX = mixX2;
  854. mixY = mixY2;
  855. keyMap = nextMap;
  856. }
  857. timelines.Add(timeline);
  858. }
  859. }
  860. }
  861. }
  862. // Deform timelines.
  863. if (map.ContainsKey("deform")) {
  864. foreach (KeyValuePair<string, Object> deformMap in (Dictionary<string, Object>)map["deform"]) {
  865. Skin skin = skeletonData.FindSkin(deformMap.Key);
  866. foreach (KeyValuePair<string, Object> slotMap in (Dictionary<string, Object>)deformMap.Value) {
  867. int slotIndex = FindSlotIndex(skeletonData, slotMap.Key);
  868. foreach (KeyValuePair<string, Object> timelineMap in (Dictionary<string, Object>)slotMap.Value) {
  869. var values = (List<Object>)timelineMap.Value;
  870. var keyMapEnumerator = values.GetEnumerator();
  871. if (!keyMapEnumerator.MoveNext()) continue;
  872. var keyMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  873. VertexAttachment attachment = (VertexAttachment)skin.GetAttachment(slotIndex, timelineMap.Key);
  874. if (attachment == null) throw new Exception("Deform attachment not found: " + timelineMap.Key);
  875. bool weighted = attachment.bones != null;
  876. float[] vertices = attachment.vertices;
  877. int deformLength = weighted ? (vertices.Length / 3) << 1 : vertices.Length;
  878. DeformTimeline timeline = new DeformTimeline(values.Count, values.Count, slotIndex, attachment);
  879. float time = GetFloat(keyMap, "time", 0);
  880. for (int frame = 0, bezier = 0; ; frame++) {
  881. float[] deform;
  882. if (!keyMap.ContainsKey("vertices")) {
  883. deform = weighted ? new float[deformLength] : vertices;
  884. } else {
  885. deform = new float[deformLength];
  886. int start = GetInt(keyMap, "offset", 0);
  887. float[] verticesValue = GetFloatArray(keyMap, "vertices", 1);
  888. Array.Copy(verticesValue, 0, deform, start, verticesValue.Length);
  889. if (scale != 1) {
  890. for (int i = start, n = i + verticesValue.Length; i < n; i++)
  891. deform[i] *= scale;
  892. }
  893. if (!weighted) {
  894. for (int i = 0; i < deformLength; i++)
  895. deform[i] += vertices[i];
  896. }
  897. }
  898. timeline.SetFrame(frame, time, deform);
  899. if (!keyMapEnumerator.MoveNext()) {
  900. timeline.Shrink(bezier);
  901. break;
  902. }
  903. var nextMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  904. float time2 = GetFloat(nextMap, "time", 0);
  905. if (keyMap.ContainsKey("curve")) {
  906. object curve = keyMap["curve"];
  907. bezier = ReadCurve(curve, timeline, bezier, frame, 0, time, time2, 0, 1, 1);
  908. }
  909. time = time2;
  910. keyMap = nextMap;
  911. }
  912. timelines.Add(timeline);
  913. }
  914. }
  915. }
  916. }
  917. // Draw order timeline.
  918. if (map.ContainsKey("drawOrder")) {
  919. var values = (List<Object>)map["drawOrder"];
  920. var timeline = new DrawOrderTimeline(values.Count);
  921. int slotCount = skeletonData.slots.Count;
  922. int frame = 0;
  923. foreach (Dictionary<string, Object> drawOrderMap in values) {
  924. int[] drawOrder = null;
  925. if (drawOrderMap.ContainsKey("offsets")) {
  926. drawOrder = new int[slotCount];
  927. for (int i = slotCount - 1; i >= 0; i--)
  928. drawOrder[i] = -1;
  929. var offsets = (List<Object>)drawOrderMap["offsets"];
  930. int[] unchanged = new int[slotCount - offsets.Count];
  931. int originalIndex = 0, unchangedIndex = 0;
  932. foreach (Dictionary<string, Object> offsetMap in offsets) {
  933. int slotIndex = FindSlotIndex(skeletonData, (string)offsetMap["slot"]);
  934. // Collect unchanged items.
  935. while (originalIndex != slotIndex)
  936. unchanged[unchangedIndex++] = originalIndex++;
  937. // Set changed items.
  938. int index = originalIndex + (int)(float)offsetMap["offset"];
  939. drawOrder[index] = originalIndex++;
  940. }
  941. // Collect remaining unchanged items.
  942. while (originalIndex < slotCount)
  943. unchanged[unchangedIndex++] = originalIndex++;
  944. // Fill in unchanged items.
  945. for (int i = slotCount - 1; i >= 0; i--)
  946. if (drawOrder[i] == -1) drawOrder[i] = unchanged[--unchangedIndex];
  947. }
  948. timeline.SetFrame(frame, GetFloat(drawOrderMap, "time", 0), drawOrder);
  949. ++frame;
  950. }
  951. timelines.Add(timeline);
  952. }
  953. // Event timeline.
  954. if (map.ContainsKey("events")) {
  955. var eventsMap = (List<Object>)map["events"];
  956. var timeline = new EventTimeline(eventsMap.Count);
  957. int frame = 0;
  958. foreach (Dictionary<string, Object> eventMap in eventsMap) {
  959. EventData eventData = skeletonData.FindEvent((string)eventMap["name"]);
  960. if (eventData == null) throw new Exception("Event not found: " + eventMap["name"]);
  961. var e = new Event(GetFloat(eventMap, "time", 0), eventData) {
  962. intValue = GetInt(eventMap, "int", eventData.Int),
  963. floatValue = GetFloat(eventMap, "float", eventData.Float),
  964. stringValue = GetString(eventMap, "string", eventData.String)
  965. };
  966. if (e.data.AudioPath != null) {
  967. e.volume = GetFloat(eventMap, "volume", eventData.Volume);
  968. e.balance = GetFloat(eventMap, "balance", eventData.Balance);
  969. }
  970. timeline.SetFrame(frame, e);
  971. ++frame;
  972. }
  973. timelines.Add(timeline);
  974. }
  975. timelines.TrimExcess();
  976. float duration = 0;
  977. var items = timelines.Items;
  978. for (int i = 0, n = timelines.Count; i < n; i++)
  979. duration = Math.Max(duration, items[i].Duration);
  980. skeletonData.animations.Add(new Animation(name, timelines, duration));
  981. }
  982. static Timeline ReadTimeline (ref List<object>.Enumerator keyMapEnumerator, CurveTimeline1 timeline, float defaultValue, float scale) {
  983. var keyMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  984. float time = GetFloat(keyMap, "time", 0);
  985. float value = GetFloat(keyMap, "value", defaultValue) * scale;
  986. for (int frame = 0, bezier = 0; ; frame++) {
  987. timeline.SetFrame(frame, time, value);
  988. if (!keyMapEnumerator.MoveNext()) {
  989. timeline.Shrink(bezier);
  990. return timeline;
  991. }
  992. var nextMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  993. float time2 = GetFloat(nextMap, "time", 0);
  994. float value2 = GetFloat(nextMap, "value", defaultValue) * scale;
  995. if (keyMap.ContainsKey("curve")) {
  996. object curve = keyMap["curve"];
  997. bezier = ReadCurve(curve, timeline, bezier, frame, 0, time, time2, value, value2, scale);
  998. }
  999. time = time2;
  1000. value = value2;
  1001. keyMap = nextMap;
  1002. }
  1003. }
  1004. static Timeline ReadTimeline (ref List<object>.Enumerator keyMapEnumerator, CurveTimeline2 timeline, String name1, String name2, float defaultValue,
  1005. float scale) {
  1006. var keyMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  1007. float time = GetFloat(keyMap, "time", 0);
  1008. float value1 = GetFloat(keyMap, name1, defaultValue) * scale, value2 = GetFloat(keyMap, name2, defaultValue) * scale;
  1009. for (int frame = 0, bezier = 0; ; frame++) {
  1010. timeline.SetFrame(frame, time, value1, value2);
  1011. if (!keyMapEnumerator.MoveNext()) {
  1012. timeline.Shrink(bezier);
  1013. return timeline;
  1014. }
  1015. var nextMap = (Dictionary<string, Object>)keyMapEnumerator.Current;
  1016. float time2 = GetFloat(nextMap, "time", 0);
  1017. float nvalue1 = GetFloat(nextMap, name1, defaultValue) * scale, nvalue2 = GetFloat(nextMap, name2, defaultValue) * scale;
  1018. if (keyMap.ContainsKey("curve")) {
  1019. object curve = keyMap["curve"];
  1020. bezier = ReadCurve(curve, timeline, bezier, frame, 0, time, time2, value1, nvalue1, scale);
  1021. bezier = ReadCurve(curve, timeline, bezier, frame, 1, time, time2, value2, nvalue2, scale);
  1022. }
  1023. time = time2;
  1024. value1 = nvalue1;
  1025. value2 = nvalue2;
  1026. keyMap = nextMap;
  1027. }
  1028. }
  1029. static int ReadCurve (object curve, CurveTimeline timeline, int bezier, int frame, int value, float time1, float time2,
  1030. float value1, float value2, float scale) {
  1031. string curveString = curve as string;
  1032. if (curveString != null) {
  1033. if (curveString == "stepped") timeline.SetStepped(frame);
  1034. return bezier;
  1035. }
  1036. var curveValues = (List<object>)curve;
  1037. int i = value << 2;
  1038. float cx1 = (float)curveValues[i];
  1039. float cy1 = (float)curveValues[i + 1] * scale;
  1040. float cx2 = (float)curveValues[i + 2];
  1041. float cy2 = (float)curveValues[i + 3] * scale;
  1042. SetBezier(timeline, frame, value, bezier, time1, value1, cx1, cy1, cx2, cy2, time2, value2);
  1043. return bezier + 1;
  1044. }
  1045. static void SetBezier (CurveTimeline timeline, int frame, int value, int bezier, float time1, float value1, float cx1, float cy1,
  1046. float cx2, float cy2, float time2, float value2) {
  1047. timeline.SetBezier(bezier, frame, value, time1, value1, cx1, cy1, cx2, cy2, time2, value2);
  1048. }
  1049. static float[] GetFloatArray (Dictionary<string, Object> map, string name, float scale) {
  1050. var list = (List<Object>)map[name];
  1051. var values = new float[list.Count];
  1052. if (scale == 1) {
  1053. for (int i = 0, n = list.Count; i < n; i++)
  1054. values[i] = (float)list[i];
  1055. } else {
  1056. for (int i = 0, n = list.Count; i < n; i++)
  1057. values[i] = (float)list[i] * scale;
  1058. }
  1059. return values;
  1060. }
  1061. static int[] GetIntArray (Dictionary<string, Object> map, string name) {
  1062. var list = (List<Object>)map[name];
  1063. var values = new int[list.Count];
  1064. for (int i = 0, n = list.Count; i < n; i++)
  1065. values[i] = (int)(float)list[i];
  1066. return values;
  1067. }
  1068. static float GetFloat (Dictionary<string, Object> map, string name, float defaultValue) {
  1069. if (!map.ContainsKey(name)) return defaultValue;
  1070. return (float)map[name];
  1071. }
  1072. static int GetInt (Dictionary<string, Object> map, string name, int defaultValue) {
  1073. if (!map.ContainsKey(name)) return defaultValue;
  1074. return (int)(float)map[name];
  1075. }
  1076. static bool GetBoolean (Dictionary<string, Object> map, string name, bool defaultValue) {
  1077. if (!map.ContainsKey(name)) return defaultValue;
  1078. return (bool)map[name];
  1079. }
  1080. static string GetString (Dictionary<string, Object> map, string name, string defaultValue) {
  1081. if (!map.ContainsKey(name)) return defaultValue;
  1082. return (string)map[name];
  1083. }
  1084. static float ToColor (string hexString, int colorIndex, int expectedLength = 8) {
  1085. if (hexString.Length != expectedLength)
  1086. throw new ArgumentException("Color hexidecimal length must be " + expectedLength + ", recieved: " + hexString, "hexString");
  1087. return Convert.ToInt32(hexString.Substring(colorIndex * 2, 2), 16) / (float)255;
  1088. }
  1089. }
  1090. }