Skip to content

Commit 03d78d4

Browse files
committed
[ts] Port of commits c83a867, f4f22cd, 1a83e96 (inherit timeline and minor fixes).
1 parent 107f5ca commit 03d78d4

File tree

7 files changed

+110
-53
lines changed

7 files changed

+110
-53
lines changed

spine-ts/spine-core/src/Animation.ts

+58-22
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { HasTextureRegion } from "./attachments/HasTextureRegion.js";
3939
import { SequenceMode, SequenceModeValues } from "./attachments/Sequence.js";
4040
import { PhysicsConstraint } from "./PhysicsConstraint.js";
4141
import { PhysicsConstraintData } from "./PhysicsConstraintData.js";
42+
import { Inherit } from "./BoneData.js";
4243

4344
/** A simple container for a list of timelines and a name. */
4445
export class Animation {
@@ -134,34 +135,35 @@ const Property = {
134135
scaleY: 4,
135136
shearX: 5,
136137
shearY: 6,
138+
inherit: 7,
137139

138-
rgb: 7,
139-
alpha: 8,
140-
rgb2: 9,
140+
rgb: 8,
141+
alpha: 9,
142+
rgb2: 10,
141143

142-
attachment: 10,
143-
deform: 11,
144+
attachment: 11,
145+
deform: 12,
144146

145-
event: 12,
146-
drawOrder: 13,
147+
event: 13,
148+
drawOrder: 14,
147149

148-
ikConstraint: 14,
149-
transformConstraint: 15,
150+
ikConstraint: 15,
151+
transformConstraint: 16,
150152

151-
pathConstraintPosition: 16,
152-
pathConstraintSpacing: 17,
153-
pathConstraintMix: 18,
153+
pathConstraintPosition: 17,
154+
pathConstraintSpacing: 18,
155+
pathConstraintMix: 19,
154156

155-
physicsConstraintInertia: 19,
156-
physicsConstraintStrength: 20,
157-
physicsConstraintDamping: 21,
158-
physicsConstraintMass: 22,
159-
physicsConstraintWind: 23,
160-
physicsConstraintGravity: 24,
161-
physicsConstraintMix: 25,
162-
physicsConstraintReset: 26,
157+
physicsConstraintInertia: 20,
158+
physicsConstraintStrength: 21,
159+
physicsConstraintDamping: 22,
160+
physicsConstraintMass: 23,
161+
physicsConstraintWind: 24,
162+
physicsConstraintGravity: 25,
163+
physicsConstraintMix: 26,
164+
physicsConstraintReset: 27,
163165

164-
sequence: 27,
166+
sequence: 28,
165167
}
166168

167169
/** The interface for all timelines. */
@@ -804,7 +806,41 @@ export class ShearYTimeline extends CurveTimeline1 implements BoneTimeline {
804806

805807
apply (skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection) {
806808
let bone = skeleton.bones[this.boneIndex];
807-
if (bone.active) bone.shearY = this.getRelativeValue(time, alpha, blend, bone.shearX, bone.data.shearY);
809+
if (bone.active) bone.shearY = this.getRelativeValue(time, alpha, blend, bone.shearY, bone.data.shearY);
810+
}
811+
}
812+
813+
export class InheritTimeline extends Timeline implements BoneTimeline {
814+
boneIndex = 0;
815+
816+
constructor (frameCount: number, boneIndex: number) {
817+
super(frameCount, [Property.inherit + "|" + boneIndex]);
818+
this.boneIndex = boneIndex;
819+
}
820+
821+
public getFrameEntries () {
822+
return 2/*ENTRIES*/;
823+
}
824+
825+
/** Sets the transform mode for the specified frame.
826+
* @param frame Between 0 and <code>frameCount</code>, inclusive.
827+
* @param time The frame time in seconds. */
828+
public setFrame (frame: number, time: number, inherit: Inherit) {
829+
frame *= 2/*ENTRIES*/;
830+
this.frames[frame] = time;
831+
this.frames[frame + 1/*INHERIT*/] = inherit;
832+
}
833+
834+
public apply (skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection) {
835+
let bone = skeleton.bones[this.boneIndex];
836+
if (!bone.active) return;
837+
838+
let frames = this.frames;
839+
if (time < frames[0]) {
840+
if (blend == MixBlend.setup || blend == MixBlend.first) bone.inherit = bone.data.inherit;
841+
return;
842+
}
843+
bone.inherit = this.frames[Timeline.search(frames, time, 2/*ENTRIES*/) + 1/*INHERIT*/];
808844
}
809845
}
810846

spine-ts/spine-core/src/Bone.ts

+17-14
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828
*****************************************************************************/
2929

30-
import { BoneData, TransformMode } from "./BoneData.js";
30+
import { BoneData, Inherit } from "./BoneData.js";
3131
import { Physics, Skeleton } from "./Skeleton.js";
3232
import { Updatable } from "./Updatable.js";
3333
import { MathUtils, Vector2 } from "./Utils.js";
@@ -110,6 +110,8 @@ export class Bone implements Updatable {
110110
/** The world Y position. If changed, {@link #updateAppliedTransform()} should be called. */
111111
worldX = 0;
112112

113+
inherit: Inherit = Inherit.Normal;
114+
113115
sorted = false;
114116
active = false;
115117

@@ -174,8 +176,8 @@ export class Bone implements Updatable {
174176
this.worldX = pa * x + pb * y + parent.worldX;
175177
this.worldY = pc * x + pd * y + parent.worldY;
176178

177-
switch (this.data.transformMode) {
178-
case TransformMode.Normal: {
179+
switch (this.inherit) {
180+
case Inherit.Normal: {
179181
const rx = (rotation + shearX) * MathUtils.degRad;
180182
const ry = (rotation + 90 + shearY) * MathUtils.degRad;
181183
const la = Math.cos(rx) * scaleX;
@@ -188,7 +190,7 @@ export class Bone implements Updatable {
188190
this.d = pc * lb + pd * ld;
189191
return;
190192
}
191-
case TransformMode.OnlyTranslation: {
193+
case Inherit.OnlyTranslation: {
192194
const rx = (rotation + shearX) * MathUtils.degRad;
193195
const ry = (rotation + 90 + shearY) * MathUtils.degRad;
194196
this.a = Math.cos(rx) * scaleX;
@@ -197,7 +199,7 @@ export class Bone implements Updatable {
197199
this.d = Math.sin(ry) * scaleY;
198200
break;
199201
}
200-
case TransformMode.NoRotationOrReflection: {
202+
case Inherit.NoRotationOrReflection: {
201203
let s = pa * pa + pc * pc;
202204
let prx = 0;
203205
if (s > 0.0001) {
@@ -224,8 +226,8 @@ export class Bone implements Updatable {
224226
this.d = pc * lb + pd * ld;
225227
break;
226228
}
227-
case TransformMode.NoScale:
228-
case TransformMode.NoScaleOrReflection: {
229+
case Inherit.NoScale:
230+
case Inherit.NoScaleOrReflection: {
229231
rotation *= MathUtils.degRad;
230232
const cos = Math.cos(rotation), sin = Math.sin(rotation);
231233
let za = (pa * cos + pb * sin) / this.skeleton.scaleX;
@@ -235,7 +237,7 @@ export class Bone implements Updatable {
235237
za *= s;
236238
zc *= s;
237239
s = Math.sqrt(za * za + zc * zc);
238-
if (this.data.transformMode == TransformMode.NoScale
240+
if (this.inherit == Inherit.NoScale
239241
&& (pa * pd - pb * pc < 0) != (this.skeleton.scaleX < 0 != this.skeleton.scaleY < 0)) s = -s;
240242
rotation = Math.PI / 2 + Math.atan2(zc, za);
241243
const zb = Math.cos(rotation) * s;
@@ -269,6 +271,7 @@ export class Bone implements Updatable {
269271
this.scaleY = data.scaleY;
270272
this.shearX = data.shearX;
271273
this.shearY = data.shearY;
274+
this.inherit = data.inherit;
272275
}
273276

274277
/** Computes the applied transform values from the world transform.
@@ -299,14 +302,14 @@ export class Bone implements Updatable {
299302
this.ay = (dy * id - dx * ic);
300303

301304
let ra, rb, rc, rd;
302-
if (this.data.transformMode == TransformMode.OnlyTranslation) {
305+
if (this.inherit == Inherit.OnlyTranslation) {
303306
ra = this.a;
304307
rb = this.b;
305308
rc = this.c;
306309
rd = this.d;
307310
} else {
308-
switch (this.data.transformMode) {
309-
case TransformMode.NoRotationOrReflection: {
311+
switch (this.inherit) {
312+
case Inherit.NoRotationOrReflection: {
310313
let s = Math.abs(pa * pd - pb * pc) / (pa * pa + pc * pc);
311314
let sa = pa / this.skeleton.scaleX;
312315
let sc = pc / this.skeleton.scaleY;
@@ -317,8 +320,8 @@ export class Bone implements Updatable {
317320
ib = pb * pid;
318321
break;
319322
}
320-
case TransformMode.NoScale:
321-
case TransformMode.NoScaleOrReflection:
323+
case Inherit.NoScale:
324+
case Inherit.NoScaleOrReflection:
322325
let cos = MathUtils.cosDeg(this.rotation), sin = MathUtils.sinDeg(this.rotation);
323326
pa = (pa * cos + pb * sin) / this.skeleton.scaleX;
324327
pc = (pc * cos + pd * sin) / this.skeleton.scaleY;
@@ -327,7 +330,7 @@ export class Bone implements Updatable {
327330
pa *= s;
328331
pc *= s;
329332
s = Math.sqrt(pa * pa + pc * pc);
330-
if (this.data.transformMode == TransformMode.NoScale && pid < 0 != (this.skeleton.scaleX < 0 != this.skeleton.scaleY < 0)) s = -s;
333+
if (this.inherit == Inherit.NoScale && pid < 0 != (this.skeleton.scaleX < 0 != this.skeleton.scaleY < 0)) s = -s;
331334
let r = MathUtils.PI / 2 + Math.atan2(pc, pa);
332335
pb = Math.cos(r) * s;
333336
pd = Math.sin(r) * s;

spine-ts/spine-core/src/BoneData.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class BoneData {
6565
shearY = 0;
6666

6767
/** The transform mode for how parent world transforms affect this bone. */
68-
transformMode = TransformMode.Normal;
68+
inherit = Inherit.Normal;
6969

7070
/** When true, {@link Skeleton#updateWorldTransform()} only updates this bone if the {@link Skeleton#skin} contains this
7171
* bone.
@@ -92,4 +92,4 @@ export class BoneData {
9292
}
9393

9494
/** Determines how a bone inherits world transforms from parent bones. */
95-
export enum TransformMode { Normal, OnlyTranslation, NoRotationOrReflection, NoScale, NoScaleOrReflection }
95+
export enum Inherit { Normal, OnlyTranslation, NoRotationOrReflection, NoScale, NoScaleOrReflection }

spine-ts/spine-core/src/IkConstraint.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*****************************************************************************/
2929

3030
import { Bone } from "./Bone.js";
31-
import { TransformMode } from "./BoneData.js";
31+
import { Inherit } from "./BoneData.js";
3232
import { IkConstraintData } from "./IkConstraintData.js";
3333
import { Physics, Skeleton } from "./Skeleton.js";
3434
import { Updatable } from "./Updatable.js";
@@ -120,12 +120,12 @@ export class IkConstraint implements Updatable {
120120
let pa = p.a, pb = p.b, pc = p.c, pd = p.d;
121121
let rotationIK = -bone.ashearX - bone.arotation, tx = 0, ty = 0;
122122

123-
switch (bone.data.transformMode) {
124-
case TransformMode.OnlyTranslation:
123+
switch (bone.inherit) {
124+
case Inherit.OnlyTranslation:
125125
tx = (targetX - bone.worldX) * MathUtils.signum(bone.skeleton.scaleX);
126126
ty = (targetY - bone.worldY) * MathUtils.signum(bone.skeleton.scaleY);
127127
break;
128-
case TransformMode.NoRotationOrReflection:
128+
case Inherit.NoRotationOrReflection:
129129
let s = Math.abs(pa * pd - pb * pc) / Math.max(0.0001, pa * pa + pc * pc);
130130
let sa = pa / bone.skeleton.scaleX;
131131
let sc = pc / bone.skeleton.scaleY;
@@ -152,9 +152,9 @@ export class IkConstraint implements Updatable {
152152
rotationIK += 360;
153153
let sx = bone.ascaleX, sy = bone.ascaleY;
154154
if (compress || stretch) {
155-
switch (bone.data.transformMode) {
156-
case TransformMode.NoScale:
157-
case TransformMode.NoScaleOrReflection:
155+
switch (bone.inherit) {
156+
case Inherit.NoScale:
157+
case Inherit.NoScaleOrReflection:
158158
tx = targetX - bone.worldX;
159159
ty = targetY - bone.worldY;
160160
}
@@ -175,6 +175,7 @@ export class IkConstraint implements Updatable {
175175
/** Applies 2 bone IK. The target is specified in the world coordinate system.
176176
* @param child A direct descendant of the parent bone. */
177177
apply2 (parent: Bone, child: Bone, targetX: number, targetY: number, bendDir: number, stretch: boolean, uniform: boolean, softness: number, alpha: number) {
178+
if (parent.inherit != Inherit.Normal || child.inherit != Inherit.Normal) return;
178179
let px = parent.ax, py = parent.ay, psx = parent.ascaleX, psy = parent.ascaleY, sx = psx, sy = psy, csx = child.ascaleX;
179180
let os1 = 0, os2 = 0, s2 = 0;
180181
if (psx < 0) {

spine-ts/spine-core/src/Skeleton.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import { Color, Utils, MathUtils, Vector2, NumberArrayLike } from "./Utils.js";
4646
*
4747
* See [Instance objects](http://esotericsoftware.com/spine-runtime-architecture#Instance-objects) in the Spine Runtimes Guide. */
4848
export class Skeleton {
49-
static yDown = false;;
49+
static yDown = false;
5050

5151
/** The skeleton's setup pose data. */
5252
data: SkeletonData;

spine-ts/spine-core/src/SkeletonBinary.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828
*****************************************************************************/
2929

30-
import { Animation, Timeline, AttachmentTimeline, RGBATimeline, RGBTimeline, RGBA2Timeline, RGB2Timeline, AlphaTimeline, RotateTimeline, TranslateTimeline, TranslateXTimeline, TranslateYTimeline, ScaleTimeline, ScaleXTimeline, ScaleYTimeline, ShearTimeline, ShearXTimeline, ShearYTimeline, IkConstraintTimeline, TransformConstraintTimeline, PathConstraintPositionTimeline, PathConstraintSpacingTimeline, PathConstraintMixTimeline, DeformTimeline, DrawOrderTimeline, EventTimeline, CurveTimeline1, CurveTimeline2, CurveTimeline, SequenceTimeline, PhysicsConstraintResetTimeline, PhysicsConstraintInertiaTimeline, PhysicsConstraintStrengthTimeline, PhysicsConstraintDampingTimeline, PhysicsConstraintMassTimeline, PhysicsConstraintWindTimeline, PhysicsConstraintGravityTimeline, PhysicsConstraintMixTimeline } from "./Animation.js";
30+
import { Animation, Timeline, InheritTimeline, AttachmentTimeline, RGBATimeline, RGBTimeline, RGBA2Timeline, RGB2Timeline, AlphaTimeline, RotateTimeline, TranslateTimeline, TranslateXTimeline, TranslateYTimeline, ScaleTimeline, ScaleXTimeline, ScaleYTimeline, ShearTimeline, ShearXTimeline, ShearYTimeline, IkConstraintTimeline, TransformConstraintTimeline, PathConstraintPositionTimeline, PathConstraintSpacingTimeline, PathConstraintMixTimeline, DeformTimeline, DrawOrderTimeline, EventTimeline, CurveTimeline1, CurveTimeline2, CurveTimeline, SequenceTimeline, PhysicsConstraintResetTimeline, PhysicsConstraintInertiaTimeline, PhysicsConstraintStrengthTimeline, PhysicsConstraintDampingTimeline, PhysicsConstraintMassTimeline, PhysicsConstraintWindTimeline, PhysicsConstraintGravityTimeline, PhysicsConstraintMixTimeline } from "./Animation.js";
3131
import { VertexAttachment, Attachment } from "./attachments/Attachment.js";
3232
import { AttachmentLoader } from "./attachments/AttachmentLoader.js";
3333
import { HasTextureRegion } from "./attachments/HasTextureRegion.js";
@@ -113,7 +113,7 @@ export class SkeletonBinary {
113113
data.shearX = input.readFloat();
114114
data.shearY = input.readFloat();
115115
data.length = input.readFloat() * scale;
116-
data.transformMode = input.readInt(true);
116+
data.inherit = input.readByte();
117117
data.skinRequired = input.readBoolean();
118118
if (nonessential) {
119119
Color.rgba8888ToColor(data.color, input.readInt32());
@@ -248,7 +248,7 @@ export class SkeletonBinary {
248248
if ((flags & 16) != 0) data.scaleX = input.readFloat();
249249
if ((flags & 32) != 0) data.shearX = input.readFloat();
250250
data.limit = ((flags & 64) != 0 ? input.readFloat() : 5000) * scale;
251-
data.step = 1 / input.readByte();
251+
data.step = 1 / input.readUnsignedByte();
252252
data.inertia = input.readFloat();
253253
data.strength = input.readFloat();
254254
data.damping = input.readFloat();
@@ -802,7 +802,16 @@ export class SkeletonBinary {
802802
for (let i = 0, n = input.readInt(true); i < n; i++) {
803803
let boneIndex = input.readInt(true);
804804
for (let ii = 0, nn = input.readInt(true); ii < nn; ii++) {
805-
let type = input.readByte(), frameCount = input.readInt(true), bezierCount = input.readInt(true);
805+
let type = input.readByte(), frameCount = input.readInt(true);
806+
if (type == BONE_INHERIT) {
807+
let timeline = new InheritTimeline(frameCount, boneIndex);
808+
for (let frame = 0; frame < frameCount; frame++) {
809+
timeline.setFrame(frame, input.readFloat(), input.readByte());
810+
}
811+
timelines.push(timeline);
812+
continue;
813+
}
814+
let bezierCount = input.readInt(true);
806815
switch (type) {
807816
case BONE_ROTATE:
808817
timelines.push(readTimeline1(input, new RotateTimeline(frameCount, bezierCount, boneIndex), 1));
@@ -1286,6 +1295,7 @@ const BONE_SCALEY = 6;
12861295
const BONE_SHEAR = 7;
12871296
const BONE_SHEARX = 8;
12881297
const BONE_SHEARY = 9;
1298+
const BONE_INHERIT = 10;
12891299

12901300
const SLOT_ATTACHMENT = 0;
12911301
const SLOT_RGBA = 1;

spine-ts/spine-core/src/SkeletonJson.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
* SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828
*****************************************************************************/
2929

30-
import { Animation, Timeline, AttachmentTimeline, RGBATimeline, RGBTimeline, AlphaTimeline, RGBA2Timeline, RGB2Timeline, RotateTimeline, TranslateTimeline, TranslateXTimeline, TranslateYTimeline, ScaleTimeline, ScaleXTimeline, ScaleYTimeline, ShearTimeline, ShearXTimeline, ShearYTimeline, IkConstraintTimeline, TransformConstraintTimeline, PathConstraintPositionTimeline, PathConstraintSpacingTimeline, PathConstraintMixTimeline, DeformTimeline, DrawOrderTimeline, EventTimeline, CurveTimeline1, CurveTimeline2, CurveTimeline, PhysicsConstraintResetTimeline, PhysicsConstraintInertiaTimeline, PhysicsConstraintStrengthTimeline, PhysicsConstraintDampingTimeline, PhysicsConstraintMassTimeline, PhysicsConstraintWindTimeline, PhysicsConstraintGravityTimeline, PhysicsConstraintMixTimeline } from "./Animation.js";
30+
import { Animation, Timeline, InheritTimeline, AttachmentTimeline, RGBATimeline, RGBTimeline, AlphaTimeline, RGBA2Timeline, RGB2Timeline, RotateTimeline, TranslateTimeline, TranslateXTimeline, TranslateYTimeline, ScaleTimeline, ScaleXTimeline, ScaleYTimeline, ShearTimeline, ShearXTimeline, ShearYTimeline, IkConstraintTimeline, TransformConstraintTimeline, PathConstraintPositionTimeline, PathConstraintSpacingTimeline, PathConstraintMixTimeline, DeformTimeline, DrawOrderTimeline, EventTimeline, CurveTimeline1, CurveTimeline2, CurveTimeline, PhysicsConstraintResetTimeline, PhysicsConstraintInertiaTimeline, PhysicsConstraintStrengthTimeline, PhysicsConstraintDampingTimeline, PhysicsConstraintMassTimeline, PhysicsConstraintWindTimeline, PhysicsConstraintGravityTimeline, PhysicsConstraintMixTimeline } from "./Animation.js";
3131
import { VertexAttachment, Attachment } from "./attachments/Attachment.js";
3232
import { AttachmentLoader } from "./attachments/AttachmentLoader.js";
3333
import { MeshAttachment } from "./attachments/MeshAttachment.js";
34-
import { BoneData, TransformMode } from "./BoneData.js";
34+
import { BoneData, Inherit } from "./BoneData.js";
3535
import { EventData } from "./EventData.js";
3636
import { Event } from "./Event.js";
3737
import { IkConstraintData } from "./IkConstraintData.js";
@@ -102,7 +102,7 @@ export class SkeletonJson {
102102
data.scaleY = getValue(boneMap, "scaleY", 1);
103103
data.shearX = getValue(boneMap, "shearX", 0);
104104
data.shearY = getValue(boneMap, "shearY", 0);
105-
data.transformMode = Utils.enumValue(TransformMode, getValue(boneMap, "transform", "Normal"));
105+
data.inherit = Utils.enumValue(Inherit, getValue(boneMap, "inherit", "Normal"));
106106
data.skinRequired = getValue(boneMap, "skin", false);
107107

108108
let color = getValue(boneMap, "color", null);
@@ -739,6 +739,13 @@ export class SkeletonJson {
739739
} else if (timelineName === "sheary") {
740740
let timeline = new ShearYTimeline(frames, frames, boneIndex);
741741
timelines.push(readTimeline1(timelineMap, timeline, 0, 1));
742+
} else if (timelineName === "inherit") {
743+
let timeline = new InheritTimeline(frames, bone.index);
744+
for (let frame = 0; frame < timelineMap.length; frame++) {
745+
let aFrame = timelineMap[frame];
746+
timeline.setFrame(frame, getValue(aFrame, "time", 0), Utils.enumValue(Inherit, getValue(aFrame, "inherit", "Normal")));
747+
}
748+
timelines.push(timeline);
742749
}
743750
}
744751
}

0 commit comments

Comments
 (0)