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 {
public class Bone {
public class Bone implements Updatable {
static public var yDown:Boolean;
internal var _data:BoneData;
@ -40,23 +40,20 @@ public class Bone {
public var x:Number;
public var y:Number;
public var rotation:Number;
public var rotationIK:Number;
public var scaleX:Number;
public var scaleY:Number;
public var flipX:Boolean;
public var flipY:Boolean;
public var appliedRotation:Number;
public var appliedScaleX:Number;
public var appliedScaleY:Number;
internal var _m00:Number;
internal var _m01:Number;
internal var _m10:Number;
internal var _m11:Number;
internal var _a:Number;
internal var _b:Number;
internal var _c:Number;
internal var _d:Number;
internal var _worldX:Number;
internal var _worldY:Number;
internal var _worldRotation:Number;
internal var _worldScaleX:Number;
internal var _worldScaleY:Number;
internal var _worldFlipX:Boolean;
internal var _worldFlipY:Boolean;
internal var _worldSignX:Number;
internal var _worldSignY:Number;
/** @param parent May be null. */
public function Bone (data:BoneData, skeleton:Skeleton, parent:Bone) {
@ -68,48 +65,136 @@ public class Bone {
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 {
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;
if (parent) {
_worldX = x * parent._m00 + y * parent._m01 + parent._worldX;
_worldY = x * parent._m10 + y * parent._m11 + parent._worldY;
if (_data.inheritScale) {
_worldScaleX = parent._worldScaleX * scaleX;
_worldScaleY = parent._worldScaleY * scaleY;
} else {
_worldScaleX = scaleX;
_worldScaleY = scaleY;
if (!parent) { // Root bone.
var skeleton:Skeleton = this.skeleton;
if (skeleton.flipX) {
x = -x;
la = -la;
lb = -lb;
}
_worldRotation = _data.inheritRotation ? parent._worldRotation + rotationIK : rotationIK;
_worldFlipX = parent._worldFlipX != flipX;
_worldFlipY = parent._worldFlipY != flipY;
} else {
var skeletonFlipX:Boolean = _skeleton.flipX, skeletonFlipY:Boolean = _skeleton.flipY;
_worldX = skeletonFlipX ? -x : x;
_worldY = skeletonFlipY != yDown ? -y : y;
_worldScaleX = scaleX;
_worldScaleY = scaleY;
_worldRotation = rotationIK;
_worldFlipX = skeletonFlipX != flipX;
_worldFlipY = skeletonFlipY != flipY;
if (skeleton.flipY != yDown) {
y = -y;
lc = -lc;
ld = -ld;
}
_a = la;
_b = lb;
_c = lc;
_d = ld;
_worldX = x;
_worldY = y;
_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 sin:Number = Math.sin(radians);
if (_worldFlipX) {
_m00 = -cos * _worldScaleX;
_m01 = sin * _worldScaleY;
var pa:Number = parent._a, pb:Number = parent._b, pc:Number = parent._c, pd:Number = parent._d;
_worldX = pa * x + pb * y + parent._worldX;
_worldY = pc * x + pd * y + parent._worldY;
_worldSignX = parent._worldSignX * (scaleX < 0 ? -1 : 1);
_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 {
_m00 = cos * _worldScaleX;
_m01 = -sin * _worldScaleY;
}
if (_worldFlipY != yDown) {
_m10 = -sin * _worldScaleX;
_m11 = -cos * _worldScaleY;
} else {
_m10 = sin * _worldScaleX;
_m11 = cos * _worldScaleY;
_a = la;
_b = lb;
_c = lc;
_d = ld;
}
}
@ -117,11 +202,8 @@ public class Bone {
x = _data.x;
y = _data.y;
rotation = _data.rotation;
rotationIK = rotation;
scaleX = _data.scaleX;
scaleY = _data.scaleY;
flipX = _data.flipX;
flipY = _data.flipY;
}
public function get data () : BoneData {
@ -136,20 +218,20 @@ public class Bone {
return _skeleton;
}
public function get m00 () : Number {
return _m00;
public function get a () : Number {
return _a;
}
public function get m01 () : Number {
return _m01;
public function get b () : Number {
return _b;
}
public function get m10 () : Number {
return _m10;
public function get c () : Number {
return _c;
}
public function get m11 () : Number {
return _m11;
public function get d () : Number {
return _d;
}
public function get worldX () : Number {
@ -160,42 +242,42 @@ public class Bone {
return _worldY;
}
public function get worldRotation () : Number {
return _worldRotation;
public function get worldSignX () : Number {
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 {
return _worldScaleX;
return Math.sqrt(_a * _a + _b * _b) * _worldSignX;
}
public function get worldScaleY () : Number {
return _worldScaleY;
}
public function get worldFlipX () : Boolean {
return _worldFlipX;
}
public function get worldFlipY () : Boolean {
return _worldFlipY;
return Math.sqrt(_c * _c + _d * _d) * _worldSignY;
}
public function worldToLocal (world:Vector.<Number>) : void {
var dx:Number = world[0] - _worldX, dy:Number = world[1] - _worldY;
var m00:Number = _m00, m10:Number = _m10, m01:Number = _m01, m11:Number = _m11;
if (_worldFlipX != (_worldFlipY != yDown)) {
m00 = -m00;
m11 = -m11;
}
var invDet:Number = 1 / (m00 * m11 - m01 * m10);
world[0] = (dx * m00 * invDet - dy * m01 * invDet);
world[1] = (dy * m11 * invDet - dx * m10 * invDet);
var x:Number = world[0] - _worldX, y:Number = world[1] - _worldY;
var a:Number = _a, b:Number = _b, c:Number = _c, d:Number = _d;
var invDet:Number = 1 / (a * d - b * c);
world[0] = (x * a * invDet - y * b * invDet);
world[1] = (y * d * invDet - x * c * invDet);
}
public function localToWorld (local:Vector.<Number>) : void {
var localX:Number = local[0], localY:Number = local[1];
local[0] = localX * _m00 + localY * _m01 + _worldX;
local[1] = localX * _m10 + localY * _m11 + _worldY;
local[0] = localX * _a + localY * _b + _worldX;
local[1] = localX * _c + localY * _d + _worldY;
}
public function toString () : String {

View File

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

View File

@ -33,12 +33,14 @@ package spine {
public class Event {
internal var _data:EventData;
public var intValue:int;;
public var time:Number;
public var intValue:int;
public var floatValue:Number;
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.");
this.time = time;
_data = data;
}

View File

@ -31,10 +31,7 @@
package spine {
public class IkConstraint {
static private const tempPosition:Vector.<Number> = new Vector.<Number>(2, true);
static private const radDeg:Number = 180 / Math.PI;
public class IkConstraint implements Updatable {
internal var _data:IkConstraintData;
public var bones:Vector.<Bone>;
public var target:Bone;
@ -55,6 +52,10 @@ public class IkConstraint {
}
public function apply () : void {
update();
}
public function update () : void {
switch (bones.length) {
case 1:
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
* coordinate system. */
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 rotationIK:Number = Math.atan2(targetY - bone._worldY, targetX - bone._worldX) * radDeg;
if (bone._worldFlipX != (bone._worldFlipY != Bone.yDown)) rotationIK = -rotationIK;
rotationIK -= parentRotation;
bone.rotationIK = rotation + (rotationIK - rotation) * alpha;
var rotationIK:Number = Math.atan2(targetY - bone.worldY, targetX - bone.worldX) * MathUtils.radDeg - parentRotation;
if (bone.worldSignX != bone.worldSignY) rotationIK = 360 - rotationIK;
if (rotationIK > 180) rotationIK -= 360;
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
* target is specified in the world coordinate system.
* @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 {
var childRotation:Number = child.rotation, parentRotation:Number = parent.rotation;
if (alpha == 0) {
child.rotationIK = childRotation;
parent.rotationIK = parentRotation;
return;
}
var positionX:Number, positionY:Number;
var parentParent:Bone = parent._parent;
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;
static public function apply2 (parent:Bone, child:Bone, targetX:Number, targetY:Number, bendDir:int, alpha:Number) : void {
if (alpha == 0) return;
var px:Number = parent.x, py:Number = parent.y, psx:Number = parent.scaleX, psy:Number = parent.scaleY;
var csx:Number = child.scaleX, cy:Number = child.y;
var offset1:int, offset2:int, sign2:int;
if (psx < 0) {
psx = -psx;
offset1 = 180;
sign2 = -1;
} else {
targetX -= parent.x;
targetY -= parent.y;
offset1 = 0;
sign2 = 1;
}
if (child._parent == parent) {
positionX = child.x;
positionY = child.y;
if (psy < 0) {
psy = -psy;
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 {
tempPosition[0] = child.x;
tempPosition[1] = child.y;
child._parent.localToWorld(tempPosition);
parent.worldToLocal(tempPosition);
positionX = tempPosition[0];
positionY = tempPosition[1];
var ppa:Number = pp.a, ppb:Number = pp.b, ppc:Number = pp.c, ppd:Number = pp.d;
var invDet:Number = 1 / (ppa * ppd - ppb * ppc);
var wx:Number = pp.worldX, wy:Number = pp.worldY, twx:Number = targetX - wx, twy:Number = targetY - wy;
tx = (twx * ppd - twy * ppb) * invDet - px;
ty = (twy * ppa - twx * ppc) * invDet - py;
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 offset:Number = Math.atan2(childY, childX);
var len1:Number = Math.sqrt(childX * childX + childY * childY), len2:Number = child.data.length * child._worldScaleX;
// Based on code by Ryan Juckett with permission: Copyright (c) 2008-2009 Ryan Juckett, http://www.ryanjuckett.com/
var cosDenom:Number = 2 * len1 * len2;
if (cosDenom < 0.0001) {
child.rotationIK = childRotation + (Math.atan2(targetY, targetX) * radDeg - parentRotation - childRotation) * alpha;
return;
var l1:Number = Math.sqrt(dx * dx + dy * dy), l2:Number = child.data.length * csx, a1:Number, a2:Number;
outer:
if (Math.abs(psx - psy) <= 0.0001) {
l2 *= psx;
var cos:Number = (tx * tx + ty * ty - l1 * l1 - l2 * l2) / (2 * l1 * l2);
if (cos < -1) cos = -1;
else if (cos > 1) cos = 1;
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;
if (cos < -1)
cos = -1;
else if (cos > 1)
cos = 1;
var childAngle:Number = Math.acos(cos) * bendDirection;
var adjacent:Number = len1 + len2 * cos, opposite:Number = len2 * Math.sin(childAngle);
var parentAngle:Number = Math.atan2(targetY * adjacent - targetX * opposite, targetX * adjacent + targetY * opposite);
var rotation:Number = (parentAngle - offset) * radDeg - parentRotation;
if (rotation > 180)
rotation -= 360;
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;
var offset:Number = Math.atan2(cy, child.x) * sign2;
a1 = (a1 - offset) * MathUtils.radDeg + offset1;
a2 = (a2 + offset) * MathUtils.radDeg * sign2 + offset2;
if (a1 > 180) a1 -= 360;
else if (a1 < -180) a1 += 360;
if (a2 > 180) a2 -= 360;
else if (a2 < -180) a2 += 360;
var rotation:Number = parent.rotation;
parent.updateWorldTransformWith(parent.x, parent.y, rotation + (a1 - rotation) * alpha, parent.scaleX, parent.scaleY);
rotation = child.rotation;
child.updateWorldTransformWith(child.x, cy, rotation + (a2 - rotation) * alpha, child.scaleX, child.scaleY);
}
}

View File

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

View File

@ -38,7 +38,8 @@ public class Skeleton {
public var slots:Vector.<Slot>;
public var drawOrder:Vector.<Slot>;
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;
public var r:Number = 1, g:Number = 1, b:Number = 1, a:Number = 1;
public var time:Number = 0;
@ -68,69 +69,51 @@ public class Skeleton {
ikConstraints = new Vector.<IkConstraint>();
for each (var ikConstraintData:IkConstraintData in data.ikConstraints)
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();
}
/** 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 {
var ikConstraintsCount:int = ikConstraints.length;
var arrayCount:int = ikConstraintsCount + 1;
if (_boneCache.length > arrayCount) _boneCache.splice(arrayCount, _boneCache.length - arrayCount);
for each (var cachedBones:Vector.<Bone> in _boneCache)
cachedBones.length = 0;
while (_boneCache.length < arrayCount)
_boneCache[_boneCache.length] = new Vector.<Bone>();
var nonIkBones:Vector.<Bone> = _boneCache[0];
outer:
var updateCache:Vector.<Updatable> = _updateCache;
var ikConstraints:Vector.<IkConstraint> = this.ikConstraints;
var transformConstraints:Vector.<TransformConstraint> = this.transformConstraints;
updateCache.length = bones.length + ikConstraints.length + transformConstraints.length;
var i:int = 0;
for each (var bone:Bone in bones) {
var current:Bone = bone;
do {
var ii:int = 0;
for each (var ikConstraint:IkConstraint in ikConstraints) {
var parent:Bone = ikConstraint.bones[0];
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++;
updateCache[i++] = bone;
for each (var transformConstraint:TransformConstraint in transformConstraints) {
if (bone == transformConstraint.bone) {
updateCache[i++] = transformConstraint;
break;
}
current = current.parent;
} while (current != null);
nonIkBones[nonIkBones.length] = bone;
}
for each (var ikConstraint:IkConstraint in ikConstraints) {
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 {
var bone:Bone;
for each (bone in bones)
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++;
}
for each (var updatable:Updatable in _updateCache)
updatable.update();
}
/** 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 {
setBonesToSetupPose();
setSlotsToSetupPose();
}
/** Sets the bones and constraints to their setup pose values. */
public function setBonesToSetupPose () : void {
for each (var bone:Bone in bones)
bone.setToSetupPose();
@ -139,6 +122,12 @@ public class Skeleton {
ikConstraint.bendDirection = ikConstraint._data.bendDirection;
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 {
@ -275,10 +264,18 @@ public class Skeleton {
}
/** @return May be null. */
public function findIkConstraint (ikConstraintName:String) : IkConstraint {
if (ikConstraintName == null) throw new ArgumentError("ikConstraintName cannot be null.");
public function findIkConstraint (constraintName:String) : IkConstraint {
if (constraintName == null) throw new ArgumentError("constraintName cannot be null.");
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;
}

View File

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

File diff suppressed because it is too large Load Diff

View File

@ -29,45 +29,50 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
package spine.animation {
import spine.Bone;
import spine.Event;
import spine.Skeleton;
package spine {
public class FlipXTimeline implements Timeline {
public var boneIndex:int;
public var frames:Vector.<Number>; // time, flip, ...
public class TransformConstraint implements Updatable {
internal var _data:TransformConstraintData;
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) {
frames = new Vector.<Number>(frameCount * 2, true);
public function TransformConstraint (data:TransformConstraintData, skeleton:Skeleton) {
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 {
return frames.length / 2;
public function apply () : void {
update();
}
/** Sets the time and angle of the specified keyframe. */
public function setFrame (frameIndex:int, time:Number, flip:Boolean) : void {
frameIndex *= 2;
frames[frameIndex] = time;
frames[int(frameIndex + 1)] = flip ? 1 : 0;
public function update () : void {
var translateMix:Number = translateMix;
if (translateMix > 0) {
var local:Vector.<Number> = new Vector.<Number>(2, true);
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 {
if (time < frames[0]) {
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);
public function get data () : TransformConstraintData {
return _data;
}
protected function setFlip (bone:Bone, flip:Boolean) : void {
bone.flipX = flip;
public function toString () : String {
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) {
time %= duration;
lastTime %= duration;
if (lastTime > 0) lastTime %= duration;
}
for (var i:int = 0, n:int = timelines.length; i < n; i++)
@ -70,7 +70,7 @@ public class Animation {
if (loop && duration != 0) {
time %= duration;
lastTime %= duration;
if (lastTime > 0) lastTime %= duration;
}
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. */
public function setFrame (frameIndex:int, time:Number, event:Event) : void {
frames[frameIndex] = time;
public function setFrame (frameIndex:int, event:Event) : void {
frames[frameIndex] = event.time;
events[frameIndex] = event;
}

View File

@ -83,11 +83,11 @@ public class AtlasAttachmentLoader implements AttachmentLoader {
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);
if (region == null)
throw new Error("Region not found in atlas: " + path + " (skinned mesh attachment: " + name + ")");
var attachment:SkinnedMeshAttachment = new SkinnedMeshAttachment(name);
throw new Error("Region not found in atlas: " + path + " (weighted mesh attachment: " + name + ")");
var attachment:WeightedMeshAttachment = new WeightedMeshAttachment(name);
attachment.rendererObject = region;
var scaleX:Number = region.page.width / nextPOT(region.page.width);
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;
/** @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. */
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 boundingbox:AttachmentType = new AttachmentType(2, "boundingbox");
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 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 {
x += bone.worldX;
y += bone.worldY;
var m00:Number = bone.m00;
var m01:Number = bone.m01;
var m10:Number = bone.m10;
var m11:Number = bone.m11;
var m00:Number = bone.a;
var m01:Number = bone.b;
var m10:Number = bone.c;
var m11:Number = bone.d;
var vertices:Vector.<Number> = this.vertices;
for (var i:int = 0, n:int = vertices.length; i < n; i += 2) {
var ii:int = i + 1;

View File

@ -88,10 +88,10 @@ public dynamic class MeshAttachment extends Attachment {
var bone:Bone = slot.bone;
x += bone.worldX;
y += bone.worldY;
var m00:Number = bone.m00;
var m01:Number = bone.m01;
var m10:Number = bone.m10;
var m11:Number = bone.m11;
var m00:Number = bone.a;
var m01:Number = bone.b;
var m10:Number = bone.c;
var m11:Number = bone.d;
var vertices:Vector.<Number> = this.vertices;
var verticesCount:int = vertices.length;
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 {
x += bone.worldX;
y += bone.worldY;
var m00:Number = bone.m00;
var m01:Number = bone.m01;
var m10:Number = bone.m10;
var m11:Number = bone.m11;
var m00:Number = bone.a;
var m01:Number = bone.b;
var m10:Number = bone.c;
var m11:Number = bone.d;
var x1:Number = offset[X1];
var y1:Number = offset[Y1];
var x2:Number = offset[X2];

View File

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

View File

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