Skeleton.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Spine
  4. {
  5. public class Skeleton
  6. {
  7. public Skeleton(SkeletonData data)
  8. {
  9. if (data == null)
  10. {
  11. throw new ArgumentNullException("data cannot be null.");
  12. }
  13. this.data = data;
  14. this.bones = new List<Bone>(data.bones.Count);
  15. foreach (BoneData boneData in data.bones)
  16. {
  17. Bone bone = (boneData.parent != null) ? this.bones[data.bones.IndexOf(boneData.parent)] : null;
  18. Bone item = new Bone(boneData, this, bone);
  19. if (bone != null)
  20. {
  21. bone.children.Add(item);
  22. }
  23. this.bones.Add(item);
  24. }
  25. this.slots = new List<Slot>(data.slots.Count);
  26. this.drawOrder = new List<Slot>(data.slots.Count);
  27. foreach (SlotData slotData in data.slots)
  28. {
  29. Bone bone2 = this.bones[data.bones.IndexOf(slotData.boneData)];
  30. Slot item2 = new Slot(slotData, bone2);
  31. this.slots.Add(item2);
  32. this.drawOrder.Add(item2);
  33. }
  34. this.ikConstraints = new List<IkConstraint>(data.ikConstraints.Count);
  35. foreach (IkConstraintData ikConstraintData in data.ikConstraints)
  36. {
  37. this.ikConstraints.Add(new IkConstraint(ikConstraintData, this));
  38. }
  39. this.UpdateCache();
  40. }
  41. public SkeletonData Data
  42. {
  43. get
  44. {
  45. return this.data;
  46. }
  47. }
  48. public List<Bone> Bones
  49. {
  50. get
  51. {
  52. return this.bones;
  53. }
  54. }
  55. public List<Slot> Slots
  56. {
  57. get
  58. {
  59. return this.slots;
  60. }
  61. }
  62. public List<Slot> DrawOrder
  63. {
  64. get
  65. {
  66. return this.drawOrder;
  67. }
  68. }
  69. public List<IkConstraint> IkConstraints
  70. {
  71. get
  72. {
  73. return this.ikConstraints;
  74. }
  75. set
  76. {
  77. this.ikConstraints = value;
  78. }
  79. }
  80. public Skin Skin
  81. {
  82. get
  83. {
  84. return this.skin;
  85. }
  86. set
  87. {
  88. this.skin = value;
  89. }
  90. }
  91. public float R
  92. {
  93. get
  94. {
  95. return this.r;
  96. }
  97. set
  98. {
  99. this.r = value;
  100. }
  101. }
  102. public float G
  103. {
  104. get
  105. {
  106. return this.g;
  107. }
  108. set
  109. {
  110. this.g = value;
  111. }
  112. }
  113. public float B
  114. {
  115. get
  116. {
  117. return this.b;
  118. }
  119. set
  120. {
  121. this.b = value;
  122. }
  123. }
  124. public float A
  125. {
  126. get
  127. {
  128. return this.a;
  129. }
  130. set
  131. {
  132. this.a = value;
  133. }
  134. }
  135. public float Time
  136. {
  137. get
  138. {
  139. return this.time;
  140. }
  141. set
  142. {
  143. this.time = value;
  144. }
  145. }
  146. public float X
  147. {
  148. get
  149. {
  150. return this.x;
  151. }
  152. set
  153. {
  154. this.x = value;
  155. }
  156. }
  157. public float Y
  158. {
  159. get
  160. {
  161. return this.y;
  162. }
  163. set
  164. {
  165. this.y = value;
  166. }
  167. }
  168. public bool FlipX
  169. {
  170. get
  171. {
  172. return this.flipX;
  173. }
  174. set
  175. {
  176. this.flipX = value;
  177. }
  178. }
  179. public bool FlipY
  180. {
  181. get
  182. {
  183. return this.flipY;
  184. }
  185. set
  186. {
  187. this.flipY = value;
  188. }
  189. }
  190. public Bone RootBone
  191. {
  192. get
  193. {
  194. return (this.bones.Count != 0) ? this.bones[0] : null;
  195. }
  196. }
  197. public void UpdateCache()
  198. {
  199. List<List<Bone>> list = this.boneCache;
  200. List<IkConstraint> list2 = this.ikConstraints;
  201. int count = list2.Count;
  202. int num = count + 1;
  203. if (list.Count > num)
  204. {
  205. list.RemoveRange(num, list.Count - num);
  206. }
  207. int i = 0;
  208. int count2 = list.Count;
  209. while (i < count2)
  210. {
  211. list[i].Clear();
  212. i++;
  213. }
  214. while (list.Count < num)
  215. {
  216. list.Add(new List<Bone>());
  217. }
  218. List<Bone> list3 = list[0];
  219. int j = 0;
  220. int count3 = this.bones.Count;
  221. while (j < count3)
  222. {
  223. Bone bone = this.bones[j];
  224. Bone bone2 = bone;
  225. int k;
  226. for (;;)
  227. {
  228. k = 0;
  229. IL_13A:
  230. while (k < count)
  231. {
  232. IkConstraint ikConstraint = list2[k];
  233. Bone bone3 = ikConstraint.bones[0];
  234. Bone bone4 = ikConstraint.bones[ikConstraint.bones.Count - 1];
  235. while (bone2 != bone4)
  236. {
  237. if (bone4 == bone3)
  238. {
  239. k++;
  240. goto IL_13A;
  241. }
  242. bone4 = bone4.parent;
  243. }
  244. goto Block_4;
  245. }
  246. bone2 = bone2.parent;
  247. if (bone2 == null)
  248. {
  249. goto Block_7;
  250. }
  251. }
  252. IL_15B:
  253. j++;
  254. continue;
  255. Block_4:
  256. list[k].Add(bone);
  257. list[k + 1].Add(bone);
  258. goto IL_15B;
  259. Block_7:
  260. list3.Add(bone);
  261. goto IL_15B;
  262. }
  263. }
  264. public void UpdateWorldTransform()
  265. {
  266. List<Bone> list = this.bones;
  267. int i = 0;
  268. int count = list.Count;
  269. while (i < count)
  270. {
  271. Bone bone = list[i];
  272. bone.rotationIK = bone.rotation;
  273. i++;
  274. }
  275. List<List<Bone>> list2 = this.boneCache;
  276. List<IkConstraint> list3 = this.ikConstraints;
  277. int num = 0;
  278. int num2 = list2.Count - 1;
  279. for (;;)
  280. {
  281. List<Bone> list4 = list2[num];
  282. int j = 0;
  283. int count2 = list4.Count;
  284. while (j < count2)
  285. {
  286. list4[j].UpdateWorldTransform();
  287. j++;
  288. }
  289. if (num == num2)
  290. {
  291. break;
  292. }
  293. list3[num].apply();
  294. num++;
  295. }
  296. }
  297. public void SetToSetupPose()
  298. {
  299. this.SetBonesToSetupPose();
  300. this.SetSlotsToSetupPose();
  301. }
  302. public void SetBonesToSetupPose()
  303. {
  304. List<Bone> list = this.bones;
  305. int i = 0;
  306. int count = list.Count;
  307. while (i < count)
  308. {
  309. list[i].SetToSetupPose();
  310. i++;
  311. }
  312. List<IkConstraint> list2 = this.ikConstraints;
  313. int j = 0;
  314. int count2 = list2.Count;
  315. while (j < count2)
  316. {
  317. IkConstraint ikConstraint = list2[j];
  318. ikConstraint.bendDirection = ikConstraint.data.bendDirection;
  319. ikConstraint.mix = ikConstraint.data.mix;
  320. j++;
  321. }
  322. }
  323. public void SetSlotsToSetupPose()
  324. {
  325. List<Slot> list = this.slots;
  326. this.drawOrder.Clear();
  327. this.drawOrder.AddRange(list);
  328. int i = 0;
  329. int count = list.Count;
  330. while (i < count)
  331. {
  332. list[i].SetToSetupPose(i);
  333. i++;
  334. }
  335. }
  336. public Bone FindBone(string boneName)
  337. {
  338. if (boneName == null)
  339. {
  340. throw new ArgumentNullException("boneName cannot be null.");
  341. }
  342. List<Bone> list = this.bones;
  343. int i = 0;
  344. int count = list.Count;
  345. while (i < count)
  346. {
  347. Bone bone = list[i];
  348. if (bone.data.name == boneName)
  349. {
  350. return bone;
  351. }
  352. i++;
  353. }
  354. return null;
  355. }
  356. public int FindBoneIndex(string boneName)
  357. {
  358. if (boneName == null)
  359. {
  360. throw new ArgumentNullException("boneName cannot be null.");
  361. }
  362. List<Bone> list = this.bones;
  363. int i = 0;
  364. int count = list.Count;
  365. while (i < count)
  366. {
  367. if (list[i].data.name == boneName)
  368. {
  369. return i;
  370. }
  371. i++;
  372. }
  373. return -1;
  374. }
  375. public Slot FindSlot(string slotName)
  376. {
  377. if (slotName == null)
  378. {
  379. throw new ArgumentNullException("slotName cannot be null.");
  380. }
  381. List<Slot> list = this.slots;
  382. int i = 0;
  383. int count = list.Count;
  384. while (i < count)
  385. {
  386. Slot slot = list[i];
  387. if (slot.data.name == slotName)
  388. {
  389. return slot;
  390. }
  391. i++;
  392. }
  393. return null;
  394. }
  395. public int FindSlotIndex(string slotName)
  396. {
  397. if (slotName == null)
  398. {
  399. throw new ArgumentNullException("slotName cannot be null.");
  400. }
  401. List<Slot> list = this.slots;
  402. int i = 0;
  403. int count = list.Count;
  404. while (i < count)
  405. {
  406. if (list[i].data.name.Equals(slotName))
  407. {
  408. return i;
  409. }
  410. i++;
  411. }
  412. return -1;
  413. }
  414. public void SetSkin(string skinName)
  415. {
  416. Skin skin = this.data.FindSkin(skinName);
  417. if (skin == null)
  418. {
  419. throw new ArgumentException("Skin not found: " + skinName);
  420. }
  421. this.SetSkin(skin);
  422. }
  423. public void SetSkin(Skin newSkin)
  424. {
  425. if (newSkin != null)
  426. {
  427. if (this.skin != null)
  428. {
  429. newSkin.AttachAll(this, this.skin);
  430. }
  431. else
  432. {
  433. List<Slot> list = this.slots;
  434. int i = 0;
  435. int count = list.Count;
  436. while (i < count)
  437. {
  438. Slot slot = list[i];
  439. string attachmentName = slot.data.attachmentName;
  440. if (attachmentName != null)
  441. {
  442. Attachment attachment = newSkin.GetAttachment(i, attachmentName);
  443. if (attachment != null)
  444. {
  445. slot.Attachment = attachment;
  446. }
  447. }
  448. i++;
  449. }
  450. }
  451. }
  452. this.skin = newSkin;
  453. }
  454. public Attachment GetAttachment(string slotName, string attachmentName)
  455. {
  456. return this.GetAttachment(this.data.FindSlotIndex(slotName), attachmentName);
  457. }
  458. public Attachment GetAttachment(int slotIndex, string attachmentName)
  459. {
  460. if (attachmentName == null)
  461. {
  462. throw new ArgumentNullException("attachmentName cannot be null.");
  463. }
  464. if (this.skin != null)
  465. {
  466. Attachment attachment = this.skin.GetAttachment(slotIndex, attachmentName);
  467. if (attachment != null)
  468. {
  469. return attachment;
  470. }
  471. }
  472. if (this.data.defaultSkin != null)
  473. {
  474. return this.data.defaultSkin.GetAttachment(slotIndex, attachmentName);
  475. }
  476. return null;
  477. }
  478. public void SetAttachment(string slotName, string attachmentName)
  479. {
  480. if (slotName == null)
  481. {
  482. throw new ArgumentNullException("slotName cannot be null.");
  483. }
  484. List<Slot> list = this.slots;
  485. int i = 0;
  486. int count = list.Count;
  487. while (i < count)
  488. {
  489. Slot slot = list[i];
  490. if (slot.data.name == slotName)
  491. {
  492. Attachment attachment = null;
  493. if (attachmentName != null)
  494. {
  495. attachment = this.GetAttachment(i, attachmentName);
  496. if (attachment == null)
  497. {
  498. throw new Exception("Attachment not found: " + attachmentName + ", for slot: " + slotName);
  499. }
  500. }
  501. slot.Attachment = attachment;
  502. return;
  503. }
  504. i++;
  505. }
  506. throw new Exception("Slot not found: " + slotName);
  507. }
  508. public IkConstraint FindIkConstraint(string ikConstraintName)
  509. {
  510. if (ikConstraintName == null)
  511. {
  512. throw new ArgumentNullException("ikConstraintName cannot be null.");
  513. }
  514. List<IkConstraint> list = this.ikConstraints;
  515. int i = 0;
  516. int count = list.Count;
  517. while (i < count)
  518. {
  519. IkConstraint ikConstraint = list[i];
  520. if (ikConstraint.data.name == ikConstraintName)
  521. {
  522. return ikConstraint;
  523. }
  524. i++;
  525. }
  526. return null;
  527. }
  528. public void Update(float delta)
  529. {
  530. this.time += delta;
  531. }
  532. public void UpdateToTime(float _time)
  533. {
  534. this.time = _time;
  535. }
  536. internal SkeletonData data;
  537. internal List<Bone> bones;
  538. internal List<Slot> slots;
  539. internal List<Slot> drawOrder;
  540. internal List<IkConstraint> ikConstraints;
  541. private List<List<Bone>> boneCache = new List<List<Bone>>();
  542. internal Skin skin;
  543. internal float r = 1f;
  544. internal float g = 1f;
  545. internal float b = 1f;
  546. internal float a = 1f;
  547. internal float time;
  548. internal bool flipX;
  549. internal bool flipY;
  550. internal float x;
  551. internal float y;
  552. }
  553. }