Skeleton.hx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. package spine;
  2. import openfl.geom.Rectangle;
  3. import openfl.errors.ArgumentError;
  4. import openfl.utils.Dictionary;
  5. import openfl.Vector;
  6. import spine.attachments.Attachment;
  7. import spine.attachments.MeshAttachment;
  8. import spine.attachments.PathAttachment;
  9. import spine.attachments.RegionAttachment;
  10. class Skeleton {
  11. private var _data:SkeletonData;
  12. public var bones:Vector<Bone>;
  13. public var slots:Vector<Slot>;
  14. public var drawOrder:Vector<Slot>;
  15. public var ikConstraints:Vector<IkConstraint>;
  16. public var transformConstraints:Vector<TransformConstraint>;
  17. public var pathConstraints:Vector<PathConstraint>;
  18. private var _updateCache:Vector<Updatable> = new Vector<Updatable>();
  19. private var _skin:Skin;
  20. public var color:Color = new Color(1, 1, 1, 1);
  21. public var scaleX:Float = 1;
  22. public var scaleY:Float = 1;
  23. public var x:Float = 0;
  24. public var y:Float = 0;
  25. public function new(data:SkeletonData) {
  26. if (data == null) {
  27. throw new ArgumentError("data cannot be null.");
  28. }
  29. _data = data;
  30. bones = new Vector<Bone>();
  31. for (boneData in data.bones) {
  32. var bone:Bone;
  33. if (boneData.parent == null) {
  34. bone = new Bone(boneData, this, null);
  35. } else {
  36. var parent:Bone = bones[boneData.parent.index];
  37. bone = new Bone(boneData, this, parent);
  38. parent.children.push(bone);
  39. }
  40. bones.push(bone);
  41. }
  42. slots = new Vector<Slot>();
  43. drawOrder = new Vector<Slot>();
  44. for (slotData in data.slots) {
  45. var bone = bones[slotData.boneData.index];
  46. var slot:Slot = new Slot(slotData, bone);
  47. slots.push(slot);
  48. drawOrder.push(slot);
  49. }
  50. ikConstraints = new Vector<IkConstraint>();
  51. for (ikConstraintData in data.ikConstraints) {
  52. ikConstraints.push(new IkConstraint(ikConstraintData, this));
  53. }
  54. transformConstraints = new Vector<TransformConstraint>();
  55. for (transformConstraintData in data.transformConstraints) {
  56. transformConstraints.push(new TransformConstraint(transformConstraintData, this));
  57. }
  58. pathConstraints = new Vector<PathConstraint>();
  59. for (pathConstraintData in data.pathConstraints) {
  60. pathConstraints.push(new PathConstraint(pathConstraintData, this));
  61. }
  62. updateCache();
  63. }
  64. /** Caches information about bones and constraints. Must be called if bones, constraints, or weighted path attachments are
  65. * added or removed. */
  66. public function updateCache():Void {
  67. _updateCache.length = 0;
  68. for (bone in bones) {
  69. bone.sorted = bone.data.skinRequired;
  70. bone.active = !bone.sorted;
  71. }
  72. if (skin != null) {
  73. var skinBones:Vector<BoneData> = skin.bones;
  74. for (i in 0...skin.bones.length) {
  75. var bone:Bone = bones[skinBones[i].index];
  76. do {
  77. bone.sorted = false;
  78. bone.active = true;
  79. bone = bone.parent;
  80. } while (bone != null);
  81. }
  82. }
  83. // IK first, lowest hierarchy depth first.
  84. var ikCount:Int = ikConstraints.length;
  85. var transformCount:Int = transformConstraints.length;
  86. var pathCount:Int = pathConstraints.length;
  87. var constraintCount:Int = ikCount + transformCount + pathCount;
  88. var continueOuter:Bool;
  89. for (i in 0...constraintCount) {
  90. continueOuter = false;
  91. for (ikConstraint in ikConstraints) {
  92. if (ikConstraint.data.order == i) {
  93. sortIkConstraint(ikConstraint);
  94. continueOuter = true;
  95. break;
  96. }
  97. }
  98. if (continueOuter)
  99. continue;
  100. for (transformConstraint in transformConstraints) {
  101. if (transformConstraint.data.order == i) {
  102. sortTransformConstraint(transformConstraint);
  103. continueOuter = true;
  104. break;
  105. }
  106. }
  107. if (continueOuter)
  108. continue;
  109. for (pathConstraint in pathConstraints) {
  110. if (pathConstraint.data.order == i) {
  111. sortPathConstraint(pathConstraint);
  112. break;
  113. }
  114. }
  115. }
  116. for (bone in bones) {
  117. sortBone(bone);
  118. }
  119. }
  120. private static function contains(list:Vector<ConstraintData>, element:ConstraintData):Bool {
  121. return list.indexOf(element) != -1;
  122. }
  123. private function sortIkConstraint(constraint:IkConstraint):Void {
  124. constraint.active = constraint.target.isActive()
  125. && (!constraint.data.skinRequired || (this.skin != null && contains(this.skin.constraints, constraint.data)));
  126. if (!constraint.active)
  127. return;
  128. var target:Bone = constraint.target;
  129. sortBone(target);
  130. var constrained:Vector<Bone> = constraint.bones;
  131. var parent:Bone = constrained[0];
  132. sortBone(parent);
  133. if (constrained.length == 1) {
  134. _updateCache.push(constraint);
  135. sortReset(parent.children);
  136. } else {
  137. var child:Bone = constrained[constrained.length - 1];
  138. sortBone(child);
  139. _updateCache.push(constraint);
  140. sortReset(parent.children);
  141. child.sorted = true;
  142. }
  143. _updateCache.push(constraint);
  144. sortReset(parent.children);
  145. constrained[constrained.length - 1].sorted = true;
  146. }
  147. private function sortPathConstraint(constraint:PathConstraint):Void {
  148. constraint.active = constraint.target.bone.isActive()
  149. && (!constraint.data.skinRequired || (this.skin != null && contains(this.skin.constraints, constraint.data)));
  150. if (!constraint.active)
  151. return;
  152. var slot:Slot = constraint.target;
  153. var slotIndex:Int = slot.data.index;
  154. var slotBone:Bone = slot.bone;
  155. if (skin != null)
  156. sortPathConstraintAttachment(skin, slotIndex, slotBone);
  157. if (data.defaultSkin != null && data.defaultSkin != skin) {
  158. sortPathConstraintAttachment(data.defaultSkin, slotIndex, slotBone);
  159. }
  160. for (i in 0...data.skins.length) {
  161. sortPathConstraintAttachment(data.skins[i], slotIndex, slotBone);
  162. }
  163. var attachment:Attachment = slot.attachment;
  164. if (Std.isOfType(attachment, PathAttachment))
  165. sortPathConstraintAttachment2(attachment, slotBone);
  166. var constrainedBones:Vector<Bone> = constraint.bones;
  167. for (bone in constrainedBones) {
  168. sortBone(bone);
  169. }
  170. _updateCache.push(constraint);
  171. for (bone in constrainedBones) {
  172. sortReset(bone.children);
  173. }
  174. for (bone in constrainedBones) {
  175. bone.sorted = true;
  176. }
  177. }
  178. private function sortTransformConstraint(constraint:TransformConstraint):Void {
  179. constraint.active = constraint.target.isActive()
  180. && (!constraint.data.skinRequired || (this.skin != null && contains(this.skin.constraints, constraint.data)));
  181. if (!constraint.active)
  182. return;
  183. sortBone(constraint.target);
  184. var constrainedBones:Vector<Bone> = constraint.bones;
  185. if (constraint.data.local) {
  186. for (bone in constrainedBones) {
  187. sortBone(bone.parent);
  188. sortBone(bone);
  189. }
  190. } else {
  191. for (bone in constrainedBones) {
  192. sortBone(bone);
  193. }
  194. }
  195. _updateCache.push(constraint);
  196. for (bone in constrainedBones) {
  197. sortReset(bone.children);
  198. }
  199. for (bone in constrainedBones) {
  200. bone.sorted = true;
  201. }
  202. }
  203. private function sortPathConstraintAttachment(skin:Skin, slotIndex:Int, slotBone:Bone):Void {
  204. var dict:Dictionary<String, Attachment> = skin.attachments[slotIndex];
  205. if (dict != null) {
  206. for (attachment in dict.each()) {
  207. sortPathConstraintAttachment2(attachment, slotBone);
  208. }
  209. }
  210. }
  211. private function sortPathConstraintAttachment2(attachment:Attachment, slotBone:Bone):Void {
  212. var pathAttachment:PathAttachment = cast(attachment, PathAttachment);
  213. if (pathAttachment == null)
  214. return;
  215. var pathBones:Vector<Int> = pathAttachment.bones;
  216. if (pathBones == null) {
  217. sortBone(slotBone);
  218. } else {
  219. var i:Int = 0;
  220. var n:Int = pathBones.length;
  221. while (i < n) {
  222. var nn:Int = pathBones[i++];
  223. nn += i;
  224. while (i < nn) {
  225. sortBone(bones[pathBones[i++]]);
  226. }
  227. }
  228. }
  229. }
  230. private function sortBone(bone:Bone):Void {
  231. if (bone.sorted)
  232. return;
  233. var parent:Bone = bone.parent;
  234. if (parent != null)
  235. sortBone(parent);
  236. bone.sorted = true;
  237. _updateCache.push(bone);
  238. }
  239. private function sortReset(bones:Vector<Bone>):Void {
  240. for (bone in bones) {
  241. if (!bone.active)
  242. continue;
  243. if (bone.sorted)
  244. sortReset(bone.children);
  245. bone.sorted = false;
  246. }
  247. }
  248. /** Updates the world transform for each bone and applies constraints. */
  249. public function updateWorldTransform():Void {
  250. for (bone in bones) {
  251. bone.ax = bone.x;
  252. bone.ay = bone.y;
  253. bone.arotation = bone.rotation;
  254. bone.ascaleX = bone.scaleX;
  255. bone.ascaleY = bone.scaleY;
  256. bone.ashearX = bone.shearX;
  257. bone.ashearY = bone.shearY;
  258. }
  259. for (updatable in _updateCache) {
  260. updatable.update();
  261. }
  262. }
  263. public function updateWorldTransformWith(parent:Bone):Void {
  264. // Apply the parent bone transform to the root bone. The root bone always inherits scale, rotation and reflection.
  265. var rootBone:Bone = rootBone;
  266. var pa:Float = parent.a,
  267. pb:Float = parent.b,
  268. pc:Float = parent.c,
  269. pd:Float = parent.d;
  270. rootBone.worldX = pa * x + pb * y + parent.worldX;
  271. rootBone.worldY = pc * x + pd * y + parent.worldY;
  272. var rotationY:Float = rootBone.rotation + 90 + rootBone.shearY;
  273. var la:Float = MathUtils.cosDeg(rootBone.rotation + rootBone.shearX) * rootBone.scaleX;
  274. var lb:Float = MathUtils.cosDeg(rotationY) * rootBone.scaleY;
  275. var lc:Float = MathUtils.sinDeg(rootBone.rotation + rootBone.shearX) * rootBone.scaleX;
  276. var ld:Float = MathUtils.sinDeg(rotationY) * rootBone.scaleY;
  277. rootBone.a = (pa * la + pb * lc) * scaleX;
  278. rootBone.b = (pa * lb + pb * ld) * scaleX;
  279. rootBone.c = (pc * la + pd * lc) * scaleY;
  280. rootBone.d = (pc * lb + pd * ld) * scaleY;
  281. // Update everything except root bone.
  282. for (updatable in _updateCache) {
  283. if (updatable != rootBone)
  284. updatable.update();
  285. }
  286. }
  287. /** Sets the bones, constraints, and slots to their setup pose values. */
  288. public function setToSetupPose():Void {
  289. setBonesToSetupPose();
  290. setSlotsToSetupPose();
  291. }
  292. /** Sets the bones and constraints to their setup pose values. */
  293. public function setBonesToSetupPose():Void {
  294. for (bone in bones) {
  295. bone.setToSetupPose();
  296. }
  297. for (ikConstraint in ikConstraints) {
  298. ikConstraint.mix = ikConstraint.data.mix;
  299. ikConstraint.softness = ikConstraint.data.softness;
  300. ikConstraint.bendDirection = ikConstraint.data.bendDirection;
  301. ikConstraint.compress = ikConstraint.data.compress;
  302. ikConstraint.stretch = ikConstraint.data.stretch;
  303. }
  304. for (transformConstraint in transformConstraints) {
  305. transformConstraint.mixRotate = transformConstraint.data.mixRotate;
  306. transformConstraint.mixX = transformConstraint.data.mixX;
  307. transformConstraint.mixY = transformConstraint.data.mixY;
  308. transformConstraint.mixScaleX = transformConstraint.data.mixScaleX;
  309. transformConstraint.mixScaleY = transformConstraint.data.mixScaleY;
  310. transformConstraint.mixShearY = transformConstraint.data.mixShearY;
  311. }
  312. for (pathConstraint in pathConstraints) {
  313. pathConstraint.position = pathConstraint.data.position;
  314. pathConstraint.spacing = pathConstraint.data.spacing;
  315. pathConstraint.mixRotate = pathConstraint.data.mixRotate;
  316. pathConstraint.mixX = pathConstraint.data.mixX;
  317. pathConstraint.mixY = pathConstraint.data.mixY;
  318. }
  319. }
  320. public function setSlotsToSetupPose():Void {
  321. var i:Int = 0;
  322. for (slot in slots) {
  323. drawOrder[i++] = slot;
  324. slot.setToSetupPose();
  325. }
  326. }
  327. public var data(get, never):SkeletonData;
  328. private function get_data():SkeletonData {
  329. return _data;
  330. }
  331. public var getUpdateCache(get, never):Vector<Updatable>;
  332. private function get_getUpdateCache():Vector<Updatable> {
  333. return _updateCache;
  334. }
  335. public var rootBone(get, never):Bone;
  336. private function get_rootBone():Bone {
  337. if (bones.length == 0)
  338. return null;
  339. return bones[0];
  340. }
  341. /** @return May be null. */
  342. public function findBone(boneName:String):Bone {
  343. if (boneName == null) {
  344. throw new ArgumentError("boneName cannot be null.");
  345. }
  346. for (bone in bones) {
  347. if (bone.data.name == boneName)
  348. return bone;
  349. }
  350. return null;
  351. }
  352. /** @return -1 if the bone was not found. */
  353. public function findBoneIndex(boneName:String):Int {
  354. if (boneName == null) {
  355. throw new ArgumentError("boneName cannot be null.");
  356. }
  357. var i:Int = 0;
  358. for (bone in bones) {
  359. if (bone.data.name == boneName)
  360. return i;
  361. i++;
  362. }
  363. return -1;
  364. }
  365. /** @return May be null. */
  366. public function findSlot(slotName:String):Slot {
  367. if (slotName == null) {
  368. throw new ArgumentError("slotName cannot be null.");
  369. }
  370. for (slot in slots) {
  371. if (slot.data.name == slotName)
  372. return slot;
  373. }
  374. return null;
  375. }
  376. public var skinName(get, set):String;
  377. private function set_skinName(skinName:String):String {
  378. var skin:Skin = data.findSkin(skinName);
  379. if (skin == null)
  380. throw new ArgumentError("Skin not found: " + skinName);
  381. this.skin = skin;
  382. return skinName;
  383. }
  384. /** @return May be null. */
  385. private function get_skinName():String {
  386. return _skin == null ? null : _skin.name;
  387. }
  388. public var skin(get, set):Skin;
  389. private function get_skin():Skin {
  390. return _skin;
  391. }
  392. /** Sets the skin used to look up attachments before looking in the {@link SkeletonData#getDefaultSkin() default skin}.
  393. * Attachments from the new skin are attached if the corresponding attachment from the old skin was attached. If there was
  394. * no old skin, each slot's setup mode attachment is attached from the new skin.
  395. * @param newSkin May be null. */
  396. private function set_skin(newSkin:Skin):Skin {
  397. if (newSkin == _skin)
  398. return null;
  399. if (newSkin != null) {
  400. if (skin != null) {
  401. newSkin.attachAll(this, skin);
  402. } else {
  403. var i:Int = 0;
  404. for (slot in slots) {
  405. var name:String = slot.data.attachmentName;
  406. if (name != null) {
  407. var attachment:Attachment = newSkin.getAttachment(i, name);
  408. if (attachment != null)
  409. slot.attachment = attachment;
  410. }
  411. i++;
  412. }
  413. }
  414. }
  415. _skin = newSkin;
  416. updateCache();
  417. return _skin;
  418. }
  419. /** @return May be null. */
  420. public function getAttachmentForSlotName(slotName:String, attachmentName:String):Attachment {
  421. return getAttachmentForSlotIndex(data.findSlot(slotName).index, attachmentName);
  422. }
  423. /** @return May be null. */
  424. public function getAttachmentForSlotIndex(slotIndex:Int, attachmentName:String):Attachment {
  425. if (attachmentName == null)
  426. throw new ArgumentError("attachmentName cannot be null.");
  427. if (skin != null) {
  428. var attachment:Attachment = skin.getAttachment(slotIndex, attachmentName);
  429. if (attachment != null)
  430. return attachment;
  431. }
  432. if (data.defaultSkin != null)
  433. return data.defaultSkin.getAttachment(slotIndex, attachmentName);
  434. return null;
  435. }
  436. /** @param attachmentName May be null. */
  437. public function setAttachment(slotName:String, attachmentName:String):Void {
  438. if (slotName == null)
  439. throw new ArgumentError("slotName cannot be null.");
  440. var i:Int = 0;
  441. for (slot in slots) {
  442. if (slot.data.name == slotName) {
  443. var attachment:Attachment = null;
  444. if (attachmentName != null) {
  445. attachment = getAttachmentForSlotIndex(i, attachmentName);
  446. if (attachment == null) {
  447. throw new ArgumentError("Attachment not found: " + attachmentName + ", for slot: " + slotName);
  448. }
  449. }
  450. slot.attachment = attachment;
  451. return;
  452. }
  453. i++;
  454. }
  455. throw new ArgumentError("Slot not found: " + slotName);
  456. }
  457. /** @return May be null. */
  458. public function findIkConstraint(constraintName:String):IkConstraint {
  459. if (constraintName == null)
  460. throw new ArgumentError("constraintName cannot be null.");
  461. for (ikConstraint in ikConstraints) {
  462. if (ikConstraint.data.name == constraintName)
  463. return ikConstraint;
  464. }
  465. return null;
  466. }
  467. /** @return May be null. */
  468. public function findTransformConstraint(constraintName:String):TransformConstraint {
  469. if (constraintName == null)
  470. throw new ArgumentError("constraintName cannot be null.");
  471. for (transformConstraint in transformConstraints) {
  472. if (transformConstraint.data.name == constraintName)
  473. return transformConstraint;
  474. }
  475. return null;
  476. }
  477. /** @return May be null. */
  478. public function findPathConstraint(constraintName:String):PathConstraint {
  479. if (constraintName == null)
  480. throw new ArgumentError("constraintName cannot be null.");
  481. for (pathConstraint in pathConstraints) {
  482. if (pathConstraint.data.name == constraintName)
  483. return pathConstraint;
  484. }
  485. return null;
  486. }
  487. public function toString():String {
  488. return _data.name != null ? _data.name : "Skeleton?";
  489. }
  490. private var _tempVertices = new Vector<Float>();
  491. private var _bounds = new Rectangle();
  492. public function getBounds():Rectangle {
  493. var minX:Float = Math.POSITIVE_INFINITY;
  494. var minY:Float = Math.POSITIVE_INFINITY;
  495. var maxX:Float = Math.NEGATIVE_INFINITY;
  496. var maxY:Float = Math.NEGATIVE_INFINITY;
  497. for (slot in drawOrder) {
  498. var verticesLength:Int = 0;
  499. var vertices:Vector<Float> = null;
  500. var attachment:Attachment = slot.attachment;
  501. if (Std.isOfType(attachment, RegionAttachment)) {
  502. verticesLength = 8;
  503. _tempVertices.length = verticesLength;
  504. vertices = _tempVertices;
  505. cast(attachment, RegionAttachment).computeWorldVertices(slot, vertices, 0, 2);
  506. } else if (Std.isOfType(attachment, MeshAttachment)) {
  507. var mesh:MeshAttachment = cast(attachment, MeshAttachment);
  508. verticesLength = mesh.worldVerticesLength;
  509. _tempVertices.length = verticesLength;
  510. vertices = _tempVertices;
  511. mesh.computeWorldVertices(slot, 0, verticesLength, vertices, 0, 2);
  512. }
  513. if (vertices != null) {
  514. var ii:Int = 0;
  515. var nn:Int = vertices.length;
  516. while (ii < nn) {
  517. var x:Float = vertices[ii], y:Float = vertices[ii + 1];
  518. minX = Math.min(minX, x);
  519. minY = Math.min(minY, y);
  520. maxX = Math.max(maxX, x);
  521. maxY = Math.max(maxY, y);
  522. ii += 2;
  523. }
  524. }
  525. }
  526. _bounds.x = minX;
  527. _bounds.y = minY;
  528. _bounds.width = maxX - minX;
  529. _bounds.height = maxY - minY;
  530. return _bounds;
  531. }
  532. }