Skip to content

Commit

Permalink
[as3] SkeletonBinary 4.0 port.
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSweet committed May 30, 2021
1 parent 05327b8 commit 6ab69e6
Show file tree
Hide file tree
Showing 73 changed files with 3,942 additions and 2,242 deletions.
2 changes: 1 addition & 1 deletion spine-as3/spine-as3-example/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"type": "swf",
"request": "launch",
"name": "Launch Spine AS3 SWF",
"name": "Launch spine-as3-example",
"preLaunchTask": "Compile debug"
}
]
Expand Down
5 changes: 3 additions & 2 deletions spine-as3/spine-as3-example/src/spine/examples/Main.as
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ package spine.examples {

[SWF(width = "800", height = "600", frameRate = "60", backgroundColor = "#dddddd")]
public class Main extends Sprite {
[Embed(source = "/spineboy-ess.json", mimeType = "application/octet-stream")]
static public const SpineboyJson : Class;

[Embed(source = "/spineboy.atlas", mimeType = "application/octet-stream")]
static public const SpineboyAtlas : Class;

[Embed(source = "/spineboy.png")]
static public const SpineboyAtlasTexture : Class;

[Embed(source = "/spineboy-ess.json", mimeType = "application/octet-stream")]
static public const SpineboyJson : Class;
private var skeleton : SkeletonAnimation;

public function Main() {
Expand Down
7 changes: 6 additions & 1 deletion spine-as3/spine-as3/src/spine/BinaryInput.as
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ package spine {
public function BinaryInput(bytes: ByteArray) {
this.bytes = bytes;
}

public function readByte() : int {
return bytes.readByte();
return bytes.readByte();
}

public function readUnsignedByte() : int {
return bytes.readUnsignedByte();
}

public function readShort() : int {
Expand Down
100 changes: 49 additions & 51 deletions spine-as3/spine-as3/src/spine/Bone.as
Original file line number Diff line number Diff line change
Expand Up @@ -103,48 +103,47 @@ package spine {
var parent : Bone = _parent;
if (!parent) { // Root bone.
rotationY = rotation + 90 + shearY;
var skeleton : Skeleton = _skeleton;
this.a = MathUtils.cosDeg(rotation + shearX) * scaleX * sx;
this.b = MathUtils.cosDeg(rotationY) * scaleY * sx;
this.c = MathUtils.sinDeg(rotation + shearX) * scaleX * sy;
this.d = MathUtils.sinDeg(rotationY) * scaleY * sy;
worldX = x * sx + skeleton.x;
worldY = y * sy + skeleton.y;
a = MathUtils.cosDeg(rotation + shearX) * scaleX * sx;
b = MathUtils.cosDeg(rotationY) * scaleY * sx;
c = MathUtils.sinDeg(rotation + shearX) * scaleX * sy;
d = MathUtils.sinDeg(rotationY) * scaleY * sy;
worldX = x * sx + _skeleton.x;
worldY = y * sy + _skeleton.y;
return;
}

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;

switch (this.data.transformMode) {
switch (data.transformMode) {
case TransformMode.normal: {
rotationY = rotation + 90 + shearY;
la = MathUtils.cosDeg(rotation + shearX) * scaleX;
lb = MathUtils.cosDeg(rotationY) * scaleY;
lc = MathUtils.sinDeg(rotation + shearX) * scaleX;
ld = MathUtils.sinDeg(rotationY) * scaleY;
this.a = pa * la + pb * lc;
this.b = pa * lb + pb * ld;
this.c = pc * la + pd * lc;
this.d = pc * lb + pd * ld;
a = pa * la + pb * lc;
b = pa * lb + pb * ld;
c = pc * la + pd * lc;
d = pc * lb + pd * ld;
return;
}
case TransformMode.onlyTranslation: {
rotationY = rotation + 90 + shearY;
this.a = MathUtils.cosDeg(rotation + shearX) * scaleX;
this.b = MathUtils.cosDeg(rotationY) * scaleY;
this.c = MathUtils.sinDeg(rotation + shearX) * scaleX;
this.d = MathUtils.sinDeg(rotationY) * scaleY;
a = MathUtils.cosDeg(rotation + shearX) * scaleX;
b = MathUtils.cosDeg(rotationY) * scaleY;
c = MathUtils.sinDeg(rotation + shearX) * scaleX;
d = MathUtils.sinDeg(rotationY) * scaleY;
break;
}
case TransformMode.noRotationOrReflection: {
s = pa * pa + pc * pc;
var prx : Number = 0;
if (s > 0.0001) {
s = Math.abs(pa * pd - pb * pc) / s;
pa /= this.skeleton.scaleX;
pc /= this.skeleton.scaleY;
pa /= _skeleton.scaleX;
pc /= _skeleton.scaleY;
pb = pc * s;
pd = pa * s;
prx = Math.atan2(pc, pa) * MathUtils.radDeg;
Expand All @@ -159,10 +158,10 @@ package spine {
lb = MathUtils.cosDeg(ry) * scaleY;
lc = MathUtils.sinDeg(rx) * scaleX;
ld = MathUtils.sinDeg(ry) * scaleY;
this.a = pa * la - pb * lc;
this.b = pa * lb - pb * ld;
this.c = pc * la + pd * lc;
this.d = pc * lb + pd * ld;
a = pa * la - pb * lc;
b = pa * lb - pb * ld;
c = pc * la + pd * lc;
d = pc * lb + pd * ld;
break;
}
case TransformMode.noScale:
Expand All @@ -185,27 +184,27 @@ package spine {
lb = MathUtils.cosDeg(90 + shearY) * scaleY;
lc = MathUtils.sinDeg(shearX) * scaleX;
ld = MathUtils.sinDeg(90 + shearY) * scaleY;
this.a = za * la + zb * lc;
this.b = za * lb + zb * ld;
this.c = zc * la + zd * lc;
this.d = zc * lb + zd * ld;
a = za * la + zb * lc;
b = za * lb + zb * ld;
c = zc * la + zd * lc;
d = zc * lb + zd * ld;
break;
}
}
this.a *= sx;
this.b *= sx;
this.c *= sy;
this.d *= sy;
a *= sx;
b *= sx;
c *= sy;
d *= sy;
}

public function setToSetupPose() : void {
x = this.data.x;
y = this.data.y;
rotation = this.data.rotation;
scaleX = this.data.scaleX;
scaleY = this.data.scaleY;
shearX = this.data.shearX;
shearY = this.data.shearY;
x = data.x;
y = data.y;
rotation = data.rotation;
scaleX = data.scaleX;
scaleY = data.scaleY;
shearX = data.shearX;
shearY = data.shearY;
}

public function get data() : BoneData {
Expand All @@ -226,19 +225,19 @@ package spine {
}

public function get worldRotationX() : Number {
return Math.atan2(this.c, this.a) * MathUtils.radDeg;
return Math.atan2(c, a) * MathUtils.radDeg;
}

public function get worldRotationY() : Number {
return Math.atan2(this.d, this.b) * MathUtils.radDeg;
return Math.atan2(d, b) * MathUtils.radDeg;
}

public function get worldScaleX() : Number {
return Math.sqrt(this.a * this.a + this.c * this.c);
return Math.sqrt(a * a + c * c);
}

public function get worldScaleY() : Number {
return Math.sqrt(this.b * this.b + this.d * this.d);
return Math.sqrt(b * b + d * d);
}

/** Computes the individual applied transform values from the world transform. This can be useful to perform processing using
Expand Down Expand Up @@ -287,37 +286,36 @@ package spine {
}

public function worldToLocal(world : Vector.<Number>) : void {
var a : Number = this.a, b : Number = this.b, c : Number = this.c, d : Number = this.d;
var invDet : Number = 1 / (a * d - b * c);
var x : Number = world[0] - this.worldX, y : Number = world[1] - this.worldY;
world[0] = (x * d * invDet - y * b * invDet);
world[1] = (y * a * invDet - x * c * invDet);
var x : Number = world[0] - worldX, y : Number = world[1] - worldY;
world[0] = x * d * invDet - y * b * invDet;
world[1] = y * a * invDet - x * c * invDet;
}

public function localToWorld(local : Vector.<Number>) : void {
var localX : Number = local[0], localY : Number = local[1];
local[0] = localX * this.a + localY * this.b + this.worldX;
local[1] = localX * this.c + localY * this.d + this.worldY;
local[0] = localX * a + localY * b + worldX;
local[1] = localX * c + localY * d + worldY;
}

public function worldToLocalRotation(worldRotation : Number) : Number {
var sin : Number = MathUtils.sinDeg(worldRotation), cos : Number = MathUtils.cosDeg(worldRotation); return Math.atan2(this.a * sin - this.c * cos, this.d * cos - this.b * sin) * MathUtils.radDeg + rotation - shearX;
var sin : Number = MathUtils.sinDeg(worldRotation), cos : Number = MathUtils.cosDeg(worldRotation);
return Math.atan2(a * sin - c * cos, d * cos - b * sin) * MathUtils.radDeg + rotation - shearX;
}

public function localToWorldRotation(localRotation : Number) : Number {
localRotation -= rotation - shearX;
var sin : Number = MathUtils.sinDeg(localRotation), cos : Number = MathUtils.cosDeg(localRotation);
return Math.atan2(cos * this.c + sin * this.d, cos * this.a + sin * this.b) * MathUtils.radDeg;
return Math.atan2(cos * c + sin * d, cos * a + sin * b) * MathUtils.radDeg;
}

public function rotateWorld(degrees : Number) : void {
var a : Number = this.a, b : Number = this.b, c : Number = this.c, d : Number = this.d;
var cos : Number = MathUtils.cosDeg(degrees), sin : Number = MathUtils.sinDeg(degrees);
var a : Number = this.a, b : Number = this.b, c : Number = this.c, d : Number = this.d;
this.a = cos * a - sin * c;
this.b = cos * b - sin * d;
this.c = sin * a + cos * c;
this.d = sin * b + cos * d;
this.appliedValid = false;
}

public function toString() : String {
Expand Down
61 changes: 31 additions & 30 deletions spine-as3/spine-as3/src/spine/Color.as
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,43 @@ package spine {
public static var GREEN : Color = new Color(0, 1, 0, 1);
public static var BLUE : Color = new Color(0, 0, 1, 1);
public static var MAGENTA : Color = new Color(1, 0, 1, 1);
public var r : Number = 0;
public var g : Number = 0;
public var b : Number = 0;
public var a : Number = 0;

public function Color(r : Number, g : Number, b : Number, a : Number = 0) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
public var r : Number;
public var g : Number;
public var b : Number;
public var a : Number;

public function setFrom(r : Number, g : Number, b : Number, a : Number) : Color {
public function Color(r : Number, g : Number, b : Number, a : Number = 0) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
this.clamp();
return this;
}

public function setFromColor(c : Color) : Color {
this.r = c.r;
this.g = c.g;
this.b = c.b;
this.a = c.a;
r = c.r;
g = c.g;
b = c.b;
a = c.a;
return this;
}

public function setFromString(hex : String) : Color {
if (hex.length != 8 && hex.length != 6) throw new ArgumentError("Hexadecimal color length must be 6 or 8: " + hex);
hex = hex.charAt(0) == '#' ? hex.substr(1) : hex;
this.r = parseInt(hex.substr(0, 2), 16) / 255.0;
this.g = parseInt(hex.substr(2, 2), 16) / 255.0;
this.b = parseInt(hex.substr(4, 2), 16) / 255.0;
this.a = (hex.length != 8 ? 255 : parseInt(hex.substr(6, 2), 16)) / 255.0;
r = parseInt(hex.substr(0, 2), 16) / 255.0;
g = parseInt(hex.substr(2, 2), 16) / 255.0;
b = parseInt(hex.substr(4, 2), 16) / 255.0;
a = (hex.length != 8 ? 255 : parseInt(hex.substr(6, 2), 16)) / 255.0;
return this;
}

public function set(r : Number, g : Number, b : Number, a : Number) : Color {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
clamp();
return this;
}

Expand All @@ -78,22 +79,22 @@ package spine {
this.g += g;
this.b += b;
this.a += a;
this.clamp();
clamp();
return this;
}

public function clamp() : Color {
if (this.r < 0) this.r = 0;
else if (this.r > 1) this.r = 1;
if (r < 0) r = 0;
else if (r > 1) r = 1;

if (this.g < 0) this.g = 0;
else if (this.g > 1) this.g = 1;
if (g < 0) g = 0;
else if (g > 1) g = 1;

if (this.b < 0) this.b = 0;
else if (this.b > 1) this.b = 1;
if (b < 0) b = 0;
else if (b > 1) b = 1;

if (this.a < 0) this.a = 0;
else if (this.a > 1) this.a = 1;
if (a < 0) a = 0;
else if (a > 1) a = 1;
return this;
}

Expand Down
9 changes: 1 addition & 8 deletions spine-as3/spine-as3/src/spine/IkConstraint.as
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,8 @@ package spine {
return active;
}

public function apply() : void {
update();
}

public function update() : void {
if (mix == 0) return;
switch (bones.length) {
case 1:
apply1(bones[0], target.worldX, target.worldY, compress, stretch, _data.uniform, mix);
Expand Down Expand Up @@ -138,10 +135,6 @@ package spine {
* 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, bendDir : int, stretch : Boolean, softness: Number, alpha : Number) : void {
if (alpha == 0) {
child.updateWorldTransform();
return;
}
if (!parent.appliedValid) parent.updateAppliedTransform();
if (!child.appliedValid) child.updateAppliedTransform();
var px : Number = parent.ax, py : Number = parent.ay, psx : Number = parent.ascaleX, sx : Number = psx, psy : Number = parent.ascaleY, csx : Number = child.ascaleX;
Expand Down
Loading

0 comments on commit 6ab69e6

Please sign in to comment.