SkeletonJson.cs 59 KB

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