Bone.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Spine
  4. {
  5. public class Bone
  6. {
  7. public Bone(BoneData data, Skeleton skeleton, Bone parent)
  8. {
  9. if (data == null)
  10. {
  11. throw new ArgumentNullException("data cannot be null.");
  12. }
  13. if (skeleton == null)
  14. {
  15. throw new ArgumentNullException("skeleton cannot be null.");
  16. }
  17. this.data = data;
  18. this.skeleton = skeleton;
  19. this.parent = parent;
  20. this.SetToSetupPose();
  21. }
  22. public BoneData Data
  23. {
  24. get
  25. {
  26. return this.data;
  27. }
  28. }
  29. public Skeleton Skeleton
  30. {
  31. get
  32. {
  33. return this.skeleton;
  34. }
  35. }
  36. public Bone Parent
  37. {
  38. get
  39. {
  40. return this.parent;
  41. }
  42. }
  43. public List<Bone> Children
  44. {
  45. get
  46. {
  47. return this.children;
  48. }
  49. }
  50. public float X
  51. {
  52. get
  53. {
  54. return this.x;
  55. }
  56. set
  57. {
  58. this.x = value;
  59. }
  60. }
  61. public float Y
  62. {
  63. get
  64. {
  65. return this.y;
  66. }
  67. set
  68. {
  69. this.y = value;
  70. }
  71. }
  72. public float Rotation
  73. {
  74. get
  75. {
  76. return this.rotation;
  77. }
  78. set
  79. {
  80. this.rotation = value;
  81. }
  82. }
  83. public float RotationIK
  84. {
  85. get
  86. {
  87. return this.rotationIK;
  88. }
  89. set
  90. {
  91. this.rotationIK = value;
  92. }
  93. }
  94. public float ScaleX
  95. {
  96. get
  97. {
  98. return this.scaleX;
  99. }
  100. set
  101. {
  102. this.scaleX = value;
  103. }
  104. }
  105. public float ScaleY
  106. {
  107. get
  108. {
  109. return this.scaleY;
  110. }
  111. set
  112. {
  113. this.scaleY = value;
  114. }
  115. }
  116. public bool FlipX
  117. {
  118. get
  119. {
  120. return this.flipX;
  121. }
  122. set
  123. {
  124. this.flipX = value;
  125. }
  126. }
  127. public bool FlipY
  128. {
  129. get
  130. {
  131. return this.flipY;
  132. }
  133. set
  134. {
  135. this.flipY = value;
  136. }
  137. }
  138. public float M00
  139. {
  140. get
  141. {
  142. return this.m00;
  143. }
  144. }
  145. public float M01
  146. {
  147. get
  148. {
  149. return this.m01;
  150. }
  151. }
  152. public float M10
  153. {
  154. get
  155. {
  156. return this.m10;
  157. }
  158. }
  159. public float M11
  160. {
  161. get
  162. {
  163. return this.m11;
  164. }
  165. }
  166. public float WorldX
  167. {
  168. get
  169. {
  170. return this.worldX;
  171. }
  172. }
  173. public float WorldY
  174. {
  175. get
  176. {
  177. return this.worldY;
  178. }
  179. }
  180. public float WorldRotation
  181. {
  182. get
  183. {
  184. return this.worldRotation;
  185. }
  186. }
  187. public float WorldScaleX
  188. {
  189. get
  190. {
  191. return this.worldScaleX;
  192. }
  193. }
  194. public float WorldScaleY
  195. {
  196. get
  197. {
  198. return this.worldScaleY;
  199. }
  200. }
  201. public bool WorldFlipX
  202. {
  203. get
  204. {
  205. return this.worldFlipX;
  206. }
  207. set
  208. {
  209. this.worldFlipX = value;
  210. }
  211. }
  212. public bool WorldFlipY
  213. {
  214. get
  215. {
  216. return this.worldFlipY;
  217. }
  218. set
  219. {
  220. this.worldFlipY = value;
  221. }
  222. }
  223. public void UpdateWorldTransform()
  224. {
  225. Bone bone = this.parent;
  226. float num = this.x;
  227. float num2 = this.y;
  228. if (bone != null)
  229. {
  230. this.worldX = num * bone.m00 + num2 * bone.m01 + bone.worldX;
  231. this.worldY = num * bone.m10 + num2 * bone.m11 + bone.worldY;
  232. if (this.data.inheritScale)
  233. {
  234. this.worldScaleX = bone.worldScaleX * this.scaleX;
  235. this.worldScaleY = bone.worldScaleY * this.scaleY;
  236. }
  237. else
  238. {
  239. this.worldScaleX = this.scaleX;
  240. this.worldScaleY = this.scaleY;
  241. }
  242. this.worldRotation = ((!this.data.inheritRotation) ? this.rotationIK : (bone.worldRotation + this.rotationIK));
  243. this.worldFlipX = (bone.worldFlipX != this.flipX);
  244. this.worldFlipY = (bone.worldFlipY != this.flipY);
  245. }
  246. else
  247. {
  248. Skeleton skeleton = this.skeleton;
  249. bool flag = skeleton.flipX;
  250. bool flag2 = skeleton.flipY;
  251. this.worldX = ((!flag) ? num : (-num));
  252. this.worldY = ((flag2 == Bone.yDown) ? num2 : (-num2));
  253. this.worldScaleX = this.scaleX;
  254. this.worldScaleY = this.scaleY;
  255. this.worldRotation = this.rotationIK;
  256. this.worldFlipX = (flag != this.flipX);
  257. this.worldFlipY = (flag2 != this.flipY);
  258. }
  259. float num3 = this.worldRotation * 3.14159274f / 180f;
  260. float num4 = (float)Math.Cos((double)num3);
  261. float num5 = (float)Math.Sin((double)num3);
  262. if (this.worldFlipX)
  263. {
  264. this.m00 = -num4 * this.worldScaleX;
  265. this.m01 = num5 * this.worldScaleY;
  266. }
  267. else
  268. {
  269. this.m00 = num4 * this.worldScaleX;
  270. this.m01 = -num5 * this.worldScaleY;
  271. }
  272. if (this.worldFlipY != Bone.yDown)
  273. {
  274. this.m10 = -num5 * this.worldScaleX;
  275. this.m11 = -num4 * this.worldScaleY;
  276. }
  277. else
  278. {
  279. this.m10 = num5 * this.worldScaleX;
  280. this.m11 = num4 * this.worldScaleY;
  281. }
  282. }
  283. public void SetToSetupPose()
  284. {
  285. BoneData boneData = this.data;
  286. this.x = boneData.x;
  287. this.y = boneData.y;
  288. this.rotation = boneData.rotation;
  289. this.rotationIK = this.rotation;
  290. this.scaleX = boneData.scaleX;
  291. this.scaleY = boneData.scaleY;
  292. this.flipX = boneData.flipX;
  293. this.flipY = boneData.flipY;
  294. }
  295. public void worldToLocal(float worldX, float worldY, out float localX, out float localY)
  296. {
  297. float num = worldX - this.worldX;
  298. float num2 = worldY - this.worldY;
  299. float num3 = this.m00;
  300. float num4 = this.m10;
  301. float num5 = this.m01;
  302. float num6 = this.m11;
  303. if (this.worldFlipX != (this.worldFlipY != Bone.yDown))
  304. {
  305. num3 = -num3;
  306. num6 = -num6;
  307. }
  308. float num7 = 1f / (num3 * num6 - num5 * num4);
  309. localX = num * num3 * num7 - num2 * num5 * num7;
  310. localY = num2 * num6 * num7 - num * num4 * num7;
  311. }
  312. public void localToWorld(float localX, float localY, out float worldX, out float worldY)
  313. {
  314. worldX = localX * this.m00 + localY * this.m01 + this.worldX;
  315. worldY = localX * this.m10 + localY * this.m11 + this.worldY;
  316. }
  317. public override string ToString()
  318. {
  319. return this.data.name;
  320. }
  321. public static bool yDown;
  322. internal BoneData data;
  323. internal Skeleton skeleton;
  324. internal Bone parent;
  325. internal List<Bone> children = new List<Bone>();
  326. internal float x;
  327. internal float y;
  328. internal float rotation;
  329. internal float rotationIK;
  330. internal float scaleX;
  331. internal float scaleY;
  332. internal bool flipX;
  333. internal bool flipY;
  334. internal float m00;
  335. internal float m01;
  336. internal float m10;
  337. internal float m11;
  338. internal float worldX;
  339. internal float worldY;
  340. internal float worldRotation;
  341. internal float worldScaleX;
  342. internal float worldScaleY;
  343. internal bool worldFlipX;
  344. internal bool worldFlipY;
  345. }
  346. }