Updated spine-as3 to Spine v3.

This commit is contained in:
NathanSweet 2016-02-17 03:54:04 +01:00
parent 0f723b9fa7
commit 4b5322d5d6
21 changed files with 1021 additions and 791 deletions

View File

@ -31,7 +31,7 @@
package spine { package spine {
public class Bone { public class Bone implements Updatable {
static public var yDown:Boolean; static public var yDown:Boolean;
internal var _data:BoneData; internal var _data:BoneData;
@ -40,23 +40,20 @@ public class Bone {
public var x:Number; public var x:Number;
public var y:Number; public var y:Number;
public var rotation:Number; public var rotation:Number;
public var rotationIK:Number;
public var scaleX:Number; public var scaleX:Number;
public var scaleY:Number; public var scaleY:Number;
public var flipX:Boolean; public var appliedRotation:Number;
public var flipY:Boolean; public var appliedScaleX:Number;
public var appliedScaleY:Number;
internal var _m00:Number; internal var _a:Number;
internal var _m01:Number; internal var _b:Number;
internal var _m10:Number; internal var _c:Number;
internal var _m11:Number; internal var _d:Number;
internal var _worldX:Number; internal var _worldX:Number;
internal var _worldY:Number; internal var _worldY:Number;
internal var _worldRotation:Number; internal var _worldSignX:Number;
internal var _worldScaleX:Number; internal var _worldSignY:Number;
internal var _worldScaleY:Number;
internal var _worldFlipX:Boolean;
internal var _worldFlipY:Boolean;
/** @param parent May be null. */ /** @param parent May be null. */
public function Bone (data:BoneData, skeleton:Skeleton, parent:Bone) { public function Bone (data:BoneData, skeleton:Skeleton, parent:Bone) {
@ -68,48 +65,136 @@ public class Bone {
setToSetupPose(); setToSetupPose();
} }
/** Computes the world SRT using the parent bone and the local SRT. */ /** Computes the world SRT using the parent bone and this bone's local SRT. */
public function updateWorldTransform () : void { public function updateWorldTransform () : void {
updateWorldTransformWith(x, y, rotation, scaleX, scaleY);
}
/** Same as updateWorldTransform(). This method exists for Bone to implement Updatable. */
public function update () : void {
updateWorldTransformWith(x, y, rotation, scaleX, scaleY);
}
/** Computes the world SRT using the parent bone and the specified local SRT. */
public function updateWorldTransformWith (x:Number, y:Number, rotation:Number, scaleX:Number, scaleY:Number) : void {
appliedRotation = rotation;
appliedScaleX = scaleX;
appliedScaleY = scaleY;
var radians:Number = rotation * MathUtils.degRad;
var cos:Number = Math.cos(radians), sin:Number = Math.sin(radians);
var la:Number = cos * scaleX, lb:Number = -sin * scaleY, lc:Number = sin * scaleX, ld:Number = cos * scaleY;
var parent:Bone = _parent; var parent:Bone = _parent;
if (parent) { if (!parent) { // Root bone.
_worldX = x * parent._m00 + y * parent._m01 + parent._worldX; var skeleton:Skeleton = this.skeleton;
_worldY = x * parent._m10 + y * parent._m11 + parent._worldY; if (skeleton.flipX) {
if (_data.inheritScale) { x = -x;
_worldScaleX = parent._worldScaleX * scaleX; la = -la;
_worldScaleY = parent._worldScaleY * scaleY; lb = -lb;
} else {
_worldScaleX = scaleX;
_worldScaleY = scaleY;
} }
_worldRotation = _data.inheritRotation ? parent._worldRotation + rotationIK : rotationIK; if (skeleton.flipY != yDown) {
_worldFlipX = parent._worldFlipX != flipX; y = -y;
_worldFlipY = parent._worldFlipY != flipY; lc = -lc;
} else { ld = -ld;
var skeletonFlipX:Boolean = _skeleton.flipX, skeletonFlipY:Boolean = _skeleton.flipY; }
_worldX = skeletonFlipX ? -x : x; _a = la;
_worldY = skeletonFlipY != yDown ? -y : y; _b = lb;
_worldScaleX = scaleX; _c = lc;
_worldScaleY = scaleY; _d = ld;
_worldRotation = rotationIK; _worldX = x;
_worldFlipX = skeletonFlipX != flipX; _worldY = y;
_worldFlipY = skeletonFlipY != flipY; _worldSignX = scaleX < 0 ? -1 : 1;
_worldSignY = scaleY < 0 ? -1 : 1;
return;
} }
var radians:Number = _worldRotation * (Math.PI / 180);
var cos:Number = Math.cos(radians); var pa:Number = parent._a, pb:Number = parent._b, pc:Number = parent._c, pd:Number = parent._d;
var sin:Number = Math.sin(radians); _worldX = pa * x + pb * y + parent._worldX;
if (_worldFlipX) { _worldY = pc * x + pd * y + parent._worldY;
_m00 = -cos * _worldScaleX; _worldSignX = parent._worldSignX * (scaleX < 0 ? -1 : 1);
_m01 = sin * _worldScaleY; _worldSignY = parent._worldSignY * (scaleY < 0 ? -1 : 1);
if (data.inheritRotation && data.inheritScale) {
_a = pa * la + pb * lc;
_b = pa * lb + pb * ld;
_c = pc * la + pd * lc;
_d = pc * lb + pd * ld;
} else if (data.inheritRotation) { // No scale inheritance.
pa = 1;
pb = 0;
pc = 0;
pd = 1;
while (parent != null) {
radians = parent.appliedRotation * MathUtils.degRad;
cos = Math.cos(radians);
sin = Math.sin(radians);
var temp1:Number = pa * cos + pb * sin;
pb = pa * -sin + pb * cos;
pa = temp1;
temp1 = pc * cos + pd * sin;
pd = pc * -sin + pd * cos;
pc = temp1;
parent = parent.parent;
}
_a = pa * la + pb * lc;
_b = pa * lb + pb * ld;
_c = pc * la + pd * lc;
_d = pc * lb + pd * ld;
if (skeleton.flipX) {
_a = -_a;
_b = -_b;
}
if (skeleton.flipY != yDown) {
_c = -_c;
_d = -_d;
}
} else if (data.inheritScale) { // No rotation inheritance.
pa = 1;
pb = 0;
pc = 0;
pd = 1;
while (parent) {
radians = parent.rotation * MathUtils.degRad;
cos = Math.cos(radians);
sin = Math.sin(radians);
var psx:Number = parent.appliedScaleX, psy:Number = parent.appliedScaleY;
var za:Number = cos * psx, zb:Number = -sin * psy, zc:Number = sin * psx, zd:Number = cos * psy;
var temp2:Number = pa * za + pb * zc;
pb = pa * zb + pb * zd;
pa = temp2;
temp2 = pc * za + pd * zc;
pd = pc * zb + pd * zd;
pc = temp2;
if (psx < 0) radians = -radians;
cos = Math.cos(-radians);
sin = Math.sin(-radians);
temp2 = pa * cos + pb * sin;
pb = pa * -sin + pb * cos;
pa = temp2;
temp2 = pc * cos + pd * sin;
pd = pc * -sin + pd * cos;
pc = temp2;
parent = parent.parent;
}
_a = pa * la + pb * lc;
_b = pa * lb + pb * ld;
_c = pc * la + pd * lc;
_d = pc * lb + pd * ld;
if (skeleton.flipX) {
_a = -_a;
_b = -_b;
}
if (skeleton.flipY != yDown) {
_c = -_c;
_d = -_d;
}
} else { } else {
_m00 = cos * _worldScaleX; _a = la;
_m01 = -sin * _worldScaleY; _b = lb;
} _c = lc;
if (_worldFlipY != yDown) { _d = ld;
_m10 = -sin * _worldScaleX;
_m11 = -cos * _worldScaleY;
} else {
_m10 = sin * _worldScaleX;
_m11 = cos * _worldScaleY;
} }
} }
@ -117,11 +202,8 @@ public class Bone {
x = _data.x; x = _data.x;
y = _data.y; y = _data.y;
rotation = _data.rotation; rotation = _data.rotation;
rotationIK = rotation;
scaleX = _data.scaleX; scaleX = _data.scaleX;
scaleY = _data.scaleY; scaleY = _data.scaleY;
flipX = _data.flipX;
flipY = _data.flipY;
} }
public function get data () : BoneData { public function get data () : BoneData {
@ -136,20 +218,20 @@ public class Bone {
return _skeleton; return _skeleton;
} }
public function get m00 () : Number { public function get a () : Number {
return _m00; return _a;
} }
public function get m01 () : Number { public function get b () : Number {
return _m01; return _b;
} }
public function get m10 () : Number { public function get c () : Number {
return _m10; return _c;
} }
public function get m11 () : Number { public function get d () : Number {
return _m11; return _d;
} }
public function get worldX () : Number { public function get worldX () : Number {
@ -160,42 +242,42 @@ public class Bone {
return _worldY; return _worldY;
} }
public function get worldRotation () : Number { public function get worldSignX () : Number {
return _worldRotation; return _worldSignX;
}
public function get worldSignY () : Number {
return _worldSignY;
}
public function get worldRotationX () : Number {
return Math.atan2(_c, _a) * MathUtils.radDeg;
}
public function get worldRotationY () : Number {
return Math.atan2(_d, _b) * MathUtils.radDeg;
} }
public function get worldScaleX () : Number { public function get worldScaleX () : Number {
return _worldScaleX; return Math.sqrt(_a * _a + _b * _b) * _worldSignX;
} }
public function get worldScaleY () : Number { public function get worldScaleY () : Number {
return _worldScaleY; return Math.sqrt(_c * _c + _d * _d) * _worldSignY;
}
public function get worldFlipX () : Boolean {
return _worldFlipX;
}
public function get worldFlipY () : Boolean {
return _worldFlipY;
} }
public function worldToLocal (world:Vector.<Number>) : void { public function worldToLocal (world:Vector.<Number>) : void {
var dx:Number = world[0] - _worldX, dy:Number = world[1] - _worldY; var x:Number = world[0] - _worldX, y:Number = world[1] - _worldY;
var m00:Number = _m00, m10:Number = _m10, m01:Number = _m01, m11:Number = _m11; var a:Number = _a, b:Number = _b, c:Number = _c, d:Number = _d;
if (_worldFlipX != (_worldFlipY != yDown)) { var invDet:Number = 1 / (a * d - b * c);
m00 = -m00; world[0] = (x * a * invDet - y * b * invDet);
m11 = -m11; world[1] = (y * d * invDet - x * c * invDet);
}
var invDet:Number = 1 / (m00 * m11 - m01 * m10);
world[0] = (dx * m00 * invDet - dy * m01 * invDet);
world[1] = (dy * m11 * invDet - dx * m10 * invDet);
} }
public function localToWorld (local:Vector.<Number>) : void { public function localToWorld (local:Vector.<Number>) : void {
var localX:Number = local[0], localY:Number = local[1]; var localX:Number = local[0], localY:Number = local[1];
local[0] = localX * _m00 + localY * _m01 + _worldX; local[0] = localX * _a + localY * _b + _worldX;
local[1] = localX * _m10 + localY * _m11 + _worldY; local[1] = localX * _c + localY * _d + _worldY;
} }
public function toString () : String { public function toString () : String {

View File

@ -42,8 +42,6 @@ public class BoneData {
public var scaleY:Number = 1; public var scaleY:Number = 1;
public var inheritScale:Boolean = true; public var inheritScale:Boolean = true;
public var inheritRotation:Boolean = true; public var inheritRotation:Boolean = true;
public var flipX:Boolean;
public var flipY:Boolean;
/** @param parent May be null. */ /** @param parent May be null. */
public function BoneData (name:String, parent:BoneData) { public function BoneData (name:String, parent:BoneData) {

View File

@ -33,12 +33,14 @@ package spine {
public class Event { public class Event {
internal var _data:EventData; internal var _data:EventData;
public var intValue:int;; public var time:Number;
public var intValue:int;
public var floatValue:Number; public var floatValue:Number;
public var stringValue:String; public var stringValue:String;
public function Event (data:EventData) { public function Event (time:Number, data:EventData) {
if (data == null) throw new ArgumentError("data cannot be null."); if (data == null) throw new ArgumentError("data cannot be null.");
this.time = time;
_data = data; _data = data;
} }

View File

@ -31,10 +31,7 @@
package spine { package spine {
public class IkConstraint { public class IkConstraint implements Updatable {
static private const tempPosition:Vector.<Number> = new Vector.<Number>(2, true);
static private const radDeg:Number = 180 / Math.PI;
internal var _data:IkConstraintData; internal var _data:IkConstraintData;
public var bones:Vector.<Bone>; public var bones:Vector.<Bone>;
public var target:Bone; public var target:Bone;
@ -55,6 +52,10 @@ public class IkConstraint {
} }
public function apply () : void { public function apply () : void {
update();
}
public function update () : void {
switch (bones.length) { switch (bones.length) {
case 1: case 1:
apply1(bones[0], target._worldX, target._worldY, mix); apply1(bones[0], target._worldX, target._worldY, mix);
@ -76,76 +77,137 @@ public class IkConstraint {
/** Adjusts the bone rotation so the tip is as close to the target position as possible. The target is specified in the world /** Adjusts the bone rotation so the tip is as close to the target position as possible. The target is specified in the world
* coordinate system. */ * coordinate system. */
static public function apply1 (bone:Bone, targetX:Number, targetY:Number, alpha:Number) : void { static public function apply1 (bone:Bone, targetX:Number, targetY:Number, alpha:Number) : void {
var parentRotation:Number = (!bone._data.inheritRotation || bone._parent == null) ? 0 : bone._parent._worldRotation; var parentRotation:Number = bone.parent == null ? 0 : bone.parent.worldRotationX;
var rotation:Number = bone.rotation; var rotation:Number = bone.rotation;
var rotationIK:Number = Math.atan2(targetY - bone._worldY, targetX - bone._worldX) * radDeg; var rotationIK:Number = Math.atan2(targetY - bone.worldY, targetX - bone.worldX) * MathUtils.radDeg - parentRotation;
if (bone._worldFlipX != (bone._worldFlipY != Bone.yDown)) rotationIK = -rotationIK; if (bone.worldSignX != bone.worldSignY) rotationIK = 360 - rotationIK;
rotationIK -= parentRotation; if (rotationIK > 180) rotationIK -= 360;
bone.rotationIK = rotation + (rotationIK - rotation) * alpha; else if (rotationIK < -180) rotationIK += 360;
bone.updateWorldTransformWith(bone.x, bone.y, rotation + (rotationIK - rotation) * alpha, bone.scaleX, bone.scaleY);
} }
/** Adjusts the parent and child bone rotations so the tip of the child is as close to the target position as possible. The /** Adjusts the parent and child bone rotations so the tip of the child is as close to the target position as possible. The
* target is specified in the world coordinate system. * target is specified in the world coordinate system.
* @param child Any descendant bone of the parent. */ * @param child Any descendant bone of the parent. */
static public function apply2 (parent:Bone, child:Bone, targetX:Number, targetY:Number, bendDirection:int, alpha:Number) : void { static public function apply2 (parent:Bone, child:Bone, targetX:Number, targetY:Number, bendDir:int, alpha:Number) : void {
var childRotation:Number = child.rotation, parentRotation:Number = parent.rotation; if (alpha == 0) return;
if (alpha == 0) { var px:Number = parent.x, py:Number = parent.y, psx:Number = parent.scaleX, psy:Number = parent.scaleY;
child.rotationIK = childRotation; var csx:Number = child.scaleX, cy:Number = child.y;
parent.rotationIK = parentRotation; var offset1:int, offset2:int, sign2:int;
return; if (psx < 0) {
} psx = -psx;
var positionX:Number, positionY:Number; offset1 = 180;
var parentParent:Bone = parent._parent; sign2 = -1;
if (parentParent) {
tempPosition[0] = targetX;
tempPosition[1] = targetY;
parentParent.worldToLocal(tempPosition);
targetX = (tempPosition[0] - parent.x) * parentParent._worldScaleX;
targetY = (tempPosition[1] - parent.y) * parentParent._worldScaleY;
} else { } else {
targetX -= parent.x; offset1 = 0;
targetY -= parent.y; sign2 = 1;
} }
if (child._parent == parent) { if (psy < 0) {
positionX = child.x; psy = -psy;
positionY = child.y; sign2 = -sign2;
}
if (csx < 0) {
csx = -csx;
offset2 = 180;
} else
offset2 = 0;
var pp:Bone = parent.parent;
var tx:Number, ty:Number, dx:Number, dy:Number;
if (!pp) {
tx = targetX - px;
ty = targetY - py;
dx = child.worldX - px;
dy = child.worldY - py;
} else { } else {
tempPosition[0] = child.x; var ppa:Number = pp.a, ppb:Number = pp.b, ppc:Number = pp.c, ppd:Number = pp.d;
tempPosition[1] = child.y; var invDet:Number = 1 / (ppa * ppd - ppb * ppc);
child._parent.localToWorld(tempPosition); var wx:Number = pp.worldX, wy:Number = pp.worldY, twx:Number = targetX - wx, twy:Number = targetY - wy;
parent.worldToLocal(tempPosition); tx = (twx * ppd - twy * ppb) * invDet - px;
positionX = tempPosition[0]; ty = (twy * ppa - twx * ppc) * invDet - py;
positionY = tempPosition[1]; twx = child.worldX - wx;
twy = child.worldY - wy;
dx = (twx * ppd - twy * ppb) * invDet - px;
dy = (twy * ppa - twx * ppc) * invDet - py;
} }
var childX:Number = positionX * parent._worldScaleX, childY:Number = positionY * parent._worldScaleY; var l1:Number = Math.sqrt(dx * dx + dy * dy), l2:Number = child.data.length * csx, a1:Number, a2:Number;
var offset:Number = Math.atan2(childY, childX); outer:
var len1:Number = Math.sqrt(childX * childX + childY * childY), len2:Number = child.data.length * child._worldScaleX; if (Math.abs(psx - psy) <= 0.0001) {
// Based on code by Ryan Juckett with permission: Copyright (c) 2008-2009 Ryan Juckett, http://www.ryanjuckett.com/ l2 *= psx;
var cosDenom:Number = 2 * len1 * len2; var cos:Number = (tx * tx + ty * ty - l1 * l1 - l2 * l2) / (2 * l1 * l2);
if (cosDenom < 0.0001) { if (cos < -1) cos = -1;
child.rotationIK = childRotation + (Math.atan2(targetY, targetX) * radDeg - parentRotation - childRotation) * alpha; else if (cos > 1) cos = 1;
return; a2 = Math.acos(cos) * bendDir;
var ad:Number = l1 + l2 * cos, o:Number = l2 * Math.sin(a2);
a1 = Math.atan2(ty * ad - tx * o, tx * ad + ty * o);
} else {
cy = 0;
var a:Number = psx * l2, b:Number = psy * l2, ta:Number = Math.atan2(ty, tx);
var aa:Number = a * a, bb:Number = b * b, ll:Number = l1 * l1, dd:Number = tx * tx + ty * ty;
var c0:Number = bb * ll + aa * dd - aa * bb, c1:Number = -2 * bb * l1, c2:Number = bb - aa;
var d:Number = c1 * c1 - 4 * c2 * c0;
if (d >= 0) {
var q:Number = Math.sqrt(d);
if (c1 < 0) q = -q;
q = -(c1 + q) / 2;
var r0:Number = q / c2, r1:Number = c0 / q;
var r:Number = Math.abs(r0) < Math.abs(r1) ? r0 : r1;
if (r * r <= dd) {
var y1:Number = Math.sqrt(dd - r * r) * bendDir;
a1 = ta - Math.atan2(y1, r);
a2 = Math.atan2(y1 / psy, (r - l1) / psx);
break outer;
}
}
var minAngle:Number = 0, minDist:Number = Number.MAX_VALUE, minX:Number = 0, minY:Number = 0;
var maxAngle:Number = 0, maxDist:Number = 0, maxX:Number = 0, maxY:Number = 0;
var x:Number = l1 + a, dist:Number = x * x;
if (dist > maxDist) {
maxAngle = 0;
maxDist = dist;
maxX = x;
}
x = l1 - a;
dist = x * x;
if (dist < minDist) {
minAngle = Math.PI;
minDist = dist;
minX = x;
}
var angle:Number = Math.acos(-a * l1 / (aa - bb));
x = a * Math.cos(angle) + l1;
var y:Number = b * Math.sin(angle);
dist = x * x + y * y;
if (dist < minDist) {
minAngle = angle;
minDist = dist;
minX = x;
minY = y;
}
if (dist > maxDist) {
maxAngle = angle;
maxDist = dist;
maxX = x;
maxY = y;
}
if (dd <= (minDist + maxDist) / 2) {
a1 = ta - Math.atan2(minY * bendDir, minX);
a2 = minAngle * bendDir;
} else {
a1 = ta - Math.atan2(maxY * bendDir, maxX);
a2 = maxAngle * bendDir;
}
} }
var cos:Number = (targetX * targetX + targetY * targetY - len1 * len1 - len2 * len2) / cosDenom; var offset:Number = Math.atan2(cy, child.x) * sign2;
if (cos < -1) a1 = (a1 - offset) * MathUtils.radDeg + offset1;
cos = -1; a2 = (a2 + offset) * MathUtils.radDeg * sign2 + offset2;
else if (cos > 1) if (a1 > 180) a1 -= 360;
cos = 1; else if (a1 < -180) a1 += 360;
var childAngle:Number = Math.acos(cos) * bendDirection; if (a2 > 180) a2 -= 360;
var adjacent:Number = len1 + len2 * cos, opposite:Number = len2 * Math.sin(childAngle); else if (a2 < -180) a2 += 360;
var parentAngle:Number = Math.atan2(targetY * adjacent - targetX * opposite, targetX * adjacent + targetY * opposite); var rotation:Number = parent.rotation;
var rotation:Number = (parentAngle - offset) * radDeg - parentRotation; parent.updateWorldTransformWith(parent.x, parent.y, rotation + (a1 - rotation) * alpha, parent.scaleX, parent.scaleY);
if (rotation > 180) rotation = child.rotation;
rotation -= 360; child.updateWorldTransformWith(child.x, cy, rotation + (a2 - rotation) * alpha, child.scaleX, child.scaleY);
else if (rotation < -180) //
rotation += 360;
parent.rotationIK = parentRotation + rotation * alpha;
rotation = (childAngle + offset) * radDeg - childRotation;
if (rotation > 180)
rotation -= 360;
else if (rotation < -180) //
rotation += 360;
child.rotationIK = childRotation + (rotation + parent._worldRotation - child._parent._worldRotation) * alpha;
} }
} }

View File

@ -29,17 +29,11 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/ *****************************************************************************/
package spine.animation { package spine {
import spine.Bone;
public class FlipYTimeline extends FlipXTimeline { public class MathUtils {
public function FlipYTimeline (frameCount:int) { static public var radDeg:Number = 180 / Math.PI;
super(frameCount); static public var degRad:Number = Math.PI / 180;
}
override protected function setFlip (bone:Bone, flip:Boolean) : void {
bone.flipY = flip;
}
} }
} }

View File

@ -38,7 +38,8 @@ public class Skeleton {
public var slots:Vector.<Slot>; public var slots:Vector.<Slot>;
public var drawOrder:Vector.<Slot>; public var drawOrder:Vector.<Slot>;
public var ikConstraints:Vector.<IkConstraint>; public var ikConstraints:Vector.<IkConstraint>;
private var _boneCache:Vector.<Vector.<Bone>> = new Vector.<Vector.<Bone>>(); public var transformConstraints:Vector.<TransformConstraint>;
private var _updateCache:Vector.<Updatable> = new Vector.<Updatable>();
private var _skin:Skin; private var _skin:Skin;
public var r:Number = 1, g:Number = 1, b:Number = 1, a:Number = 1; public var r:Number = 1, g:Number = 1, b:Number = 1, a:Number = 1;
public var time:Number = 0; public var time:Number = 0;
@ -68,69 +69,51 @@ public class Skeleton {
ikConstraints = new Vector.<IkConstraint>(); ikConstraints = new Vector.<IkConstraint>();
for each (var ikConstraintData:IkConstraintData in data.ikConstraints) for each (var ikConstraintData:IkConstraintData in data.ikConstraints)
ikConstraints[ikConstraints.length] = new IkConstraint(ikConstraintData, this); ikConstraints[ikConstraints.length] = new IkConstraint(ikConstraintData, this);
transformConstraints = new Vector.<TransformConstraint>();
for each (var transformConstraintData:TransformConstraintData in data.transformConstraints)
transformConstraints[transformConstraints.length] = new TransformConstraint(transformConstraintData, this);
updateCache(); updateCache();
} }
/** Caches information about bones and IK constraints. Must be called if bones or IK constraints are added or removed. */ /** Caches information about bones and constraints. Must be called if bones or constraints are added or removed. */
public function updateCache () : void { public function updateCache () : void {
var ikConstraintsCount:int = ikConstraints.length; var updateCache:Vector.<Updatable> = _updateCache;
var ikConstraints:Vector.<IkConstraint> = this.ikConstraints;
var arrayCount:int = ikConstraintsCount + 1; var transformConstraints:Vector.<TransformConstraint> = this.transformConstraints;
if (_boneCache.length > arrayCount) _boneCache.splice(arrayCount, _boneCache.length - arrayCount); updateCache.length = bones.length + ikConstraints.length + transformConstraints.length;
for each (var cachedBones:Vector.<Bone> in _boneCache) var i:int = 0;
cachedBones.length = 0;
while (_boneCache.length < arrayCount)
_boneCache[_boneCache.length] = new Vector.<Bone>();
var nonIkBones:Vector.<Bone> = _boneCache[0];
outer:
for each (var bone:Bone in bones) { for each (var bone:Bone in bones) {
var current:Bone = bone; updateCache[i++] = bone;
do { for each (var transformConstraint:TransformConstraint in transformConstraints) {
var ii:int = 0; if (bone == transformConstraint.bone) {
for each (var ikConstraint:IkConstraint in ikConstraints) { updateCache[i++] = transformConstraint;
var parent:Bone = ikConstraint.bones[0]; break;
var child:Bone = ikConstraint.bones[int(ikConstraint.bones.length - 1)];
while (true) {
if (current == child) {
_boneCache[ii].push(bone);
_boneCache[int(ii + 1)].push(bone);
continue outer;
}
if (child == parent) break;
child = child.parent;
}
ii++;
} }
current = current.parent; }
} while (current != null); for each (var ikConstraint:IkConstraint in ikConstraints) {
nonIkBones[nonIkBones.length] = bone; if (bone == ikConstraint.bones[ikConstraint.bones.length - 1]) {
updateCache[i++] = ikConstraint;
break;
}
}
} }
} }
/** Updates the world transform for each bone and applies IK constraints. */ /** Updates the world transform for each bone and applies constraints. */
public function updateWorldTransform () : void { public function updateWorldTransform () : void {
var bone:Bone; for each (var updatable:Updatable in _updateCache)
for each (bone in bones) updatable.update();
bone.rotationIK = bone.rotation;
var i:int = 0, last:int = _boneCache.length - 1;
while (true) {
for each (bone in _boneCache[i])
bone.updateWorldTransform();
if (i == last) break;
ikConstraints[i].apply();
i++;
}
} }
/** Sets the bones and slots to their setup pose values. */ /** Sets the bones, constraints, and slots to their setup pose values. */
public function setToSetupPose () : void { public function setToSetupPose () : void {
setBonesToSetupPose(); setBonesToSetupPose();
setSlotsToSetupPose(); setSlotsToSetupPose();
} }
/** Sets the bones and constraints to their setup pose values. */
public function setBonesToSetupPose () : void { public function setBonesToSetupPose () : void {
for each (var bone:Bone in bones) for each (var bone:Bone in bones)
bone.setToSetupPose(); bone.setToSetupPose();
@ -139,6 +122,12 @@ public class Skeleton {
ikConstraint.bendDirection = ikConstraint._data.bendDirection; ikConstraint.bendDirection = ikConstraint._data.bendDirection;
ikConstraint.mix = ikConstraint._data.mix; ikConstraint.mix = ikConstraint._data.mix;
} }
for each (var transformConstraint:TransformConstraint in transformConstraints) {
transformConstraint.translateMix = transformConstraint._data.translateMix;
transformConstraint.x = transformConstraint._data.x;
transformConstraint.y = transformConstraint._data.y;
}
} }
public function setSlotsToSetupPose () : void { public function setSlotsToSetupPose () : void {
@ -275,10 +264,18 @@ public class Skeleton {
} }
/** @return May be null. */ /** @return May be null. */
public function findIkConstraint (ikConstraintName:String) : IkConstraint { public function findIkConstraint (constraintName:String) : IkConstraint {
if (ikConstraintName == null) throw new ArgumentError("ikConstraintName cannot be null."); if (constraintName == null) throw new ArgumentError("constraintName cannot be null.");
for each (var ikConstraint:IkConstraint in ikConstraints) for each (var ikConstraint:IkConstraint in ikConstraints)
if (ikConstraint._data._name == ikConstraintName) return ikConstraint; if (ikConstraint._data._name == constraintName) return ikConstraint;
return null;
}
/** @return May be null. */
public function findTransformConstraint (constraintName:String) : TransformConstraint {
if (constraintName == null) throw new ArgumentError("constraintName cannot be null.");
for each (var transformConstraint:TransformConstraint in transformConstraints)
if (transformConstraint._data._name == constraintName) return transformConstraint;
return null; return null;
} }

View File

@ -42,6 +42,7 @@ public class SkeletonData {
public var events:Vector.<EventData> = new Vector.<EventData>(); public var events:Vector.<EventData> = new Vector.<EventData>();
public var animations:Vector.<Animation> = new Vector.<Animation>(); public var animations:Vector.<Animation> = new Vector.<Animation>();
public var ikConstraints:Vector.<IkConstraintData> = new Vector.<IkConstraintData>(); public var ikConstraints:Vector.<IkConstraintData> = new Vector.<IkConstraintData>();
public var transformConstraints:Vector.<TransformConstraintData> = new Vector.<TransformConstraintData>();
public var width:Number, height:Number; public var width:Number, height:Number;
public var version:String, hash:String; public var version:String, hash:String;
@ -118,10 +119,20 @@ public class SkeletonData {
// --- IK constraints. // --- IK constraints.
/** @return May be null. */ /** @return May be null. */
public function findIkConstraint (ikConstraintName:String) : IkConstraintData { public function findIkConstraint (constraintName:String) : IkConstraintData {
if (ikConstraintName == null) throw new ArgumentError("ikConstraintName cannot be null."); if (constraintName == null) throw new ArgumentError("constraintName cannot be null.");
for each (var ikConstraintData:IkConstraintData in ikConstraints) for each (var ikConstraintData:IkConstraintData in ikConstraints)
if (ikConstraintData._name == ikConstraintName) return ikConstraintData; if (ikConstraintData._name == constraintName) return ikConstraintData;
return null;
}
// --- Transform constraints.
/** @return May be null. */
public function findTransformConstraint (constraintName:String) : TransformConstraintData {
if (constraintName == null) throw new ArgumentError("constraintName cannot be null.");
for each (var transformConstraintData:TransformConstraintData in transformConstraints)
if (transformConstraintData._name == constraintName) return transformConstraintData;
return null; return null;
} }

File diff suppressed because it is too large Load Diff

View File

@ -29,45 +29,50 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/ *****************************************************************************/
package spine.animation { package spine {
import spine.Bone;
import spine.Event;
import spine.Skeleton;
public class FlipXTimeline implements Timeline { public class TransformConstraint implements Updatable {
public var boneIndex:int; internal var _data:TransformConstraintData;
public var frames:Vector.<Number>; // time, flip, ... public var bone:Bone;
public var target:Bone;
public var translateMix:Number;
public var x:Number;
public var y:Number;
public function FlipXTimeline (frameCount:int) { public function TransformConstraint (data:TransformConstraintData, skeleton:Skeleton) {
frames = new Vector.<Number>(frameCount * 2, true); if (data == null) throw new ArgumentError("data cannot be null.");
if (skeleton == null) throw new ArgumentError("skeleton cannot be null.");
_data = data;
translateMix = data.translateMix;
x = data.x;
y = data.y;
bone = skeleton.findBone(data.bone._name);
target = skeleton.findBone(data.target._name);
} }
public function get frameCount () : int { public function apply () : void {
return frames.length / 2; update();
} }
/** Sets the time and angle of the specified keyframe. */ public function update () : void {
public function setFrame (frameIndex:int, time:Number, flip:Boolean) : void { var translateMix:Number = translateMix;
frameIndex *= 2; if (translateMix > 0) {
frames[frameIndex] = time; var local:Vector.<Number> = new Vector.<Number>(2, true);
frames[int(frameIndex + 1)] = flip ? 1 : 0; local[0] = x;
local[1] = y;
target.localToWorld(local);
bone._worldX += (local[0] - bone._worldX) * translateMix;
bone._worldY += (local[1] - bone._worldY) * translateMix;
}
} }
public function apply (skeleton:Skeleton, lastTime:Number, time:Number, firedEvents:Vector.<Event>, alpha:Number) : void { public function get data () : TransformConstraintData {
if (time < frames[0]) { return _data;
if (lastTime > time) apply(skeleton, lastTime, int.MAX_VALUE, null, 0);
return;
} else if (lastTime > time) //
lastTime = -1;
var frameIndex:int = (time >= frames[frames.length - 2] ? frames.length : Animation.binarySearch(frames, time, 2)) - 2;
if (frames[frameIndex] < lastTime) return;
setFlip(skeleton.bones[boneIndex], frames[frameIndex + 1] != 0);
} }
protected function setFlip (bone:Bone, flip:Boolean) : void { public function toString () : String {
bone.flipX = flip; return _data._name;
} }
} }

View File

@ -0,0 +1,56 @@
/******************************************************************************
* Spine Runtimes Software License
* Version 2.3
*
* Copyright (c) 2013-2015, Esoteric Software
* All rights reserved.
*
* You are granted a perpetual, non-exclusive, non-sublicensable and
* non-transferable license to use, install, execute and perform the Spine
* Runtimes Software (the "Software") and derivative works solely for personal
* or internal use. Without the written permission of Esoteric Software (see
* Section 2 of the Spine Software License Agreement), you may not (a) modify,
* translate, adapt or otherwise create derivative works, improvements of the
* Software or develop new applications using the Software or (b) remove,
* delete, alter or obscure any trademarks or any copyright, trademark, patent
* or other intellectual property or proprietary rights notices on or in the
* Software, including any copy thereof. Redistributions in binary or source
* form must include this license and terms.
*
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
package spine {
public class TransformConstraintData {
internal var _name:String;
public var bone:BoneData;
public var target:BoneData;
public var translateMix:Number;
public var x:Number;
public var y:Number;
public function TransformConstraintData (name:String) {
if (name == null) throw new ArgumentError("name cannot be null.");
_name = name;
}
public function get name () : String {
return _name;
}
public function toString () : String {
return _name;
}
}
}

View File

@ -0,0 +1,38 @@
/******************************************************************************
* Spine Runtimes Software License
* Version 2.3
*
* Copyright (c) 2013-2015, Esoteric Software
* All rights reserved.
*
* You are granted a perpetual, non-exclusive, non-sublicensable and
* non-transferable license to use, install, execute and perform the Spine
* Runtimes Software (the "Software") and derivative works solely for personal
* or internal use. Without the written permission of Esoteric Software (see
* Section 2 of the Spine Software License Agreement), you may not (a) modify,
* translate, adapt or otherwise create derivative works, improvements of the
* Software or develop new applications using the Software or (b) remove,
* delete, alter or obscure any trademarks or any copyright, trademark, patent
* or other intellectual property or proprietary rights notices on or in the
* Software, including any copy thereof. Redistributions in binary or source
* form must include this license and terms.
*
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
package spine {
public interface Updatable {
function update () : void;
}
}

View File

@ -56,7 +56,7 @@ public class Animation {
if (loop && duration != 0) { if (loop && duration != 0) {
time %= duration; time %= duration;
lastTime %= duration; if (lastTime > 0) lastTime %= duration;
} }
for (var i:int = 0, n:int = timelines.length; i < n; i++) for (var i:int = 0, n:int = timelines.length; i < n; i++)
@ -70,7 +70,7 @@ public class Animation {
if (loop && duration != 0) { if (loop && duration != 0) {
time %= duration; time %= duration;
lastTime %= duration; if (lastTime > 0) lastTime %= duration;
} }
for (var i:int = 0, n:int = timelines.length; i < n; i++) for (var i:int = 0, n:int = timelines.length; i < n; i++)

View File

@ -47,8 +47,8 @@ public class EventTimeline implements Timeline {
} }
/** Sets the time and value of the specified keyframe. */ /** Sets the time and value of the specified keyframe. */
public function setFrame (frameIndex:int, time:Number, event:Event) : void { public function setFrame (frameIndex:int, event:Event) : void {
frames[frameIndex] = time; frames[frameIndex] = event.time;
events[frameIndex] = event; events[frameIndex] = event;
} }

View File

@ -83,11 +83,11 @@ public class AtlasAttachmentLoader implements AttachmentLoader {
return attachment; return attachment;
} }
public function newSkinnedMeshAttachment (skin:Skin, name:String, path:String) : SkinnedMeshAttachment { public function newWeightedMeshAttachment (skin:Skin, name:String, path:String) : WeightedMeshAttachment {
var region:AtlasRegion = atlas.findRegion(path); var region:AtlasRegion = atlas.findRegion(path);
if (region == null) if (region == null)
throw new Error("Region not found in atlas: " + path + " (skinned mesh attachment: " + name + ")"); throw new Error("Region not found in atlas: " + path + " (weighted mesh attachment: " + name + ")");
var attachment:SkinnedMeshAttachment = new SkinnedMeshAttachment(name); var attachment:WeightedMeshAttachment = new WeightedMeshAttachment(name);
attachment.rendererObject = region; attachment.rendererObject = region;
var scaleX:Number = region.page.width / nextPOT(region.page.width); var scaleX:Number = region.page.width / nextPOT(region.page.width);
var scaleY:Number = region.page.height / nextPOT(region.page.height); var scaleY:Number = region.page.height / nextPOT(region.page.height);

View File

@ -40,7 +40,7 @@ public interface AttachmentLoader {
function newMeshAttachment (skin:Skin, name:String, path:String) : MeshAttachment; function newMeshAttachment (skin:Skin, name:String, path:String) : MeshAttachment;
/** @return May be null to not load an attachment. */ /** @return May be null to not load an attachment. */
function newSkinnedMeshAttachment (skin:Skin, name:String, path:String) : SkinnedMeshAttachment; function newWeightedMeshAttachment (skin:Skin, name:String, path:String) : WeightedMeshAttachment;
/** @return May be null to not load an attachment. */ /** @return May be null to not load an attachment. */
function newBoundingBoxAttachment (skin:Skin, name:String) : BoundingBoxAttachment; function newBoundingBoxAttachment (skin:Skin, name:String) : BoundingBoxAttachment;

View File

@ -36,7 +36,7 @@ public class AttachmentType {
public static const regionsequence:AttachmentType = new AttachmentType(1, "regionsequence"); public static const regionsequence:AttachmentType = new AttachmentType(1, "regionsequence");
public static const boundingbox:AttachmentType = new AttachmentType(2, "boundingbox"); public static const boundingbox:AttachmentType = new AttachmentType(2, "boundingbox");
public static const mesh:AttachmentType = new AttachmentType(3, "mesh"); public static const mesh:AttachmentType = new AttachmentType(3, "mesh");
public static const skinnedmesh:AttachmentType = new AttachmentType(4, "skinnedmesh"); public static const weightedmesh:AttachmentType = new AttachmentType(4, "weightedmesh");
public var ordinal:int; public var ordinal:int;
public var name:String; public var name:String;

View File

@ -42,10 +42,10 @@ public dynamic class BoundingBoxAttachment extends Attachment {
public function computeWorldVertices (x:Number, y:Number, bone:Bone, worldVertices:Vector.<Number>) : void { public function computeWorldVertices (x:Number, y:Number, bone:Bone, worldVertices:Vector.<Number>) : void {
x += bone.worldX; x += bone.worldX;
y += bone.worldY; y += bone.worldY;
var m00:Number = bone.m00; var m00:Number = bone.a;
var m01:Number = bone.m01; var m01:Number = bone.b;
var m10:Number = bone.m10; var m10:Number = bone.c;
var m11:Number = bone.m11; var m11:Number = bone.d;
var vertices:Vector.<Number> = this.vertices; var vertices:Vector.<Number> = this.vertices;
for (var i:int = 0, n:int = vertices.length; i < n; i += 2) { for (var i:int = 0, n:int = vertices.length; i < n; i += 2) {
var ii:int = i + 1; var ii:int = i + 1;

View File

@ -88,10 +88,10 @@ public dynamic class MeshAttachment extends Attachment {
var bone:Bone = slot.bone; var bone:Bone = slot.bone;
x += bone.worldX; x += bone.worldX;
y += bone.worldY; y += bone.worldY;
var m00:Number = bone.m00; var m00:Number = bone.a;
var m01:Number = bone.m01; var m01:Number = bone.b;
var m10:Number = bone.m10; var m10:Number = bone.c;
var m11:Number = bone.m11; var m11:Number = bone.d;
var vertices:Vector.<Number> = this.vertices; var vertices:Vector.<Number> = this.vertices;
var verticesCount:int = vertices.length; var verticesCount:int = vertices.length;
if (slot.attachmentVertices.length == verticesCount) vertices = slot.attachmentVertices; if (slot.attachmentVertices.length == verticesCount) vertices = slot.attachmentVertices;

View File

@ -125,10 +125,10 @@ public dynamic class RegionAttachment extends Attachment {
public function computeWorldVertices (x:Number, y:Number, bone:Bone, worldVertices:Vector.<Number>) : void { public function computeWorldVertices (x:Number, y:Number, bone:Bone, worldVertices:Vector.<Number>) : void {
x += bone.worldX; x += bone.worldX;
y += bone.worldY; y += bone.worldY;
var m00:Number = bone.m00; var m00:Number = bone.a;
var m01:Number = bone.m01; var m01:Number = bone.b;
var m10:Number = bone.m10; var m10:Number = bone.c;
var m11:Number = bone.m11; var m11:Number = bone.d;
var x1:Number = offset[X1]; var x1:Number = offset[X1];
var y1:Number = offset[Y1]; var y1:Number = offset[Y1];
var x2:Number = offset[X2]; var x2:Number = offset[X2];

View File

@ -33,7 +33,7 @@ package spine.attachments {
import spine.Slot; import spine.Slot;
import spine.Bone; import spine.Bone;
public dynamic class SkinnedMeshAttachment extends Attachment { public dynamic class WeightedMeshAttachment extends Attachment {
public var bones:Vector.<int>; public var bones:Vector.<int>;
public var weights:Vector.<Number>; public var weights:Vector.<Number>;
public var uvs:Vector.<Number>; public var uvs:Vector.<Number>;
@ -64,7 +64,7 @@ public dynamic class SkinnedMeshAttachment extends Attachment {
public var width:Number; public var width:Number;
public var height:Number; public var height:Number;
public function SkinnedMeshAttachment (name:String) { public function WeightedMeshAttachment (name:String) {
super(name); super(name);
} }
@ -102,8 +102,8 @@ public dynamic class SkinnedMeshAttachment extends Attachment {
vx = weights[b]; vx = weights[b];
vy = weights[int(b + 1)]; vy = weights[int(b + 1)];
weight = weights[int(b + 2)]; weight = weights[int(b + 2)];
wx += (vx * bone.m00 + vy * bone.m01 + bone.worldX) * weight; wx += (vx * bone.a + vy * bone.b + bone.worldX) * weight;
wy += (vx * bone.m10 + vy * bone.m11 + bone.worldY) * weight; wy += (vx * bone.c + vy * bone.d + bone.worldY) * weight;
} }
worldVertices[w] = wx + x; worldVertices[w] = wx + x;
worldVertices[int(w + 1)] = wy + y; worldVertices[int(w + 1)] = wy + y;
@ -119,8 +119,8 @@ public dynamic class SkinnedMeshAttachment extends Attachment {
vx = weights[b] + ffd[f]; vx = weights[b] + ffd[f];
vy = weights[int(b + 1)] + ffd[int(f + 1)]; vy = weights[int(b + 1)] + ffd[int(f + 1)];
weight = weights[int(b + 2)]; weight = weights[int(b + 2)];
wx += (vx * bone.m00 + vy * bone.m01 + bone.worldX) * weight; wx += (vx * bone.a + vy * bone.b + bone.worldX) * weight;
wy += (vx * bone.m10 + vy * bone.m11 + bone.worldY) * weight; wy += (vx * bone.c + vy * bone.d + bone.worldY) * weight;
} }
worldVertices[w] = wx + x; worldVertices[w] = wx + x;
worldVertices[int(w + 1)] = wy + y; worldVertices[int(w + 1)] = wy + y;

View File

@ -129,12 +129,10 @@ public class SkeletonSprite extends Sprite {
var bone:Bone = slot.bone; var bone:Bone = slot.bone;
var flipX:int = skeleton.flipX ? -1 : 1; var flipX:int = skeleton.flipX ? -1 : 1;
var flipY:int = skeleton.flipY ? -1 : 1; var flipY:int = skeleton.flipY ? -1 : 1;
if (bone.worldFlipX) flipX = -flipX;
if (bone.worldFlipY) flipY = -flipY;
wrapper.x = bone.worldX; wrapper.x = bone.worldX;
wrapper.y = bone.worldY; wrapper.y = bone.worldY;
wrapper.rotation = -bone.worldRotation * flipX * flipY; wrapper.rotation = -bone.worldRotationX * flipX * flipY;
wrapper.scaleX = bone.worldScaleX * flipX; wrapper.scaleX = bone.worldScaleX * flipX;
wrapper.scaleY = bone.worldScaleY * flipY; wrapper.scaleY = bone.worldScaleY * flipY;
addChild(wrapper); addChild(wrapper);