From 8711e55672c0a32f24c10e86c89a7ced39c24e47 Mon Sep 17 00:00:00 2001 From: Davide Tantillo Date: Tue, 1 Apr 2025 14:40:11 +0200 Subject: [PATCH] [ts] Small refactor alignment with reference runtime. --- spine-ts/spine-core/src/Animation.ts | 16 ++++--------- spine-ts/spine-core/src/AnimationState.ts | 17 ++++++++++---- spine-ts/spine-core/src/Bone.ts | 2 +- spine-ts/spine-core/src/PathConstraint.ts | 2 +- spine-ts/spine-core/src/PhysicsConstraint.ts | 11 ++++----- spine-ts/spine-core/src/Skeleton.ts | 17 +++++++------- spine-ts/spine-core/src/SkeletonBounds.ts | 9 ++++---- spine-ts/spine-core/src/Slot.ts | 2 +- .../spine-webgl/src/SkeletonDebugRenderer.ts | 23 ++++++++----------- 9 files changed, 45 insertions(+), 54 deletions(-) diff --git a/spine-ts/spine-core/src/Animation.ts b/spine-ts/spine-core/src/Animation.ts index 03de2a082..e5d9e5907 100644 --- a/spine-ts/spine-core/src/Animation.ts +++ b/spine-ts/spine-core/src/Animation.ts @@ -1421,10 +1421,9 @@ export class DeformTimeline extends CurveTimeline implements SlotTimeline { apply (skeleton: Skeleton, lastTime: number, time: number, firedEvents: Array, alpha: number, blend: MixBlend, direction: MixDirection) { let slot: Slot = skeleton.slots[this.slotIndex]; - if (!slot.bone.active) return; - let slotAttachment: Attachment | null = slot.getAttachment(); - if (!slotAttachment) return; - if (!(slotAttachment instanceof VertexAttachment) || (slotAttachment).timelineAttachment != this.attachment) return; + if (!slot.bone.active || !(slot.attachment instanceof VertexAttachment)) return; + let vertexAttachment = slot.attachment; + if (vertexAttachment.timelineAttachment != this.attachment) return; let deform: Array = slot.deform; if (deform.length == 0) blend = MixBlend.setup; @@ -1444,7 +1443,6 @@ export class DeformTimeline extends CurveTimeline implements SlotTimeline { return; } deform.length = vertexCount; - let vertexAttachment = slotAttachment; if (!vertexAttachment.bones) { // Unweighted vertex positions. let setupVertices = vertexAttachment.vertices; @@ -1465,7 +1463,6 @@ export class DeformTimeline extends CurveTimeline implements SlotTimeline { let lastVertices = vertices[frames.length - 1]; if (alpha == 1) { if (blend == MixBlend.add) { - let vertexAttachment = slotAttachment as VertexAttachment; if (!vertexAttachment.bones) { // Unweighted vertex positions, with alpha. let setupVertices = vertexAttachment.vertices; @@ -1481,7 +1478,6 @@ export class DeformTimeline extends CurveTimeline implements SlotTimeline { } else { switch (blend) { case MixBlend.setup: { - let vertexAttachment = slotAttachment as VertexAttachment; if (!vertexAttachment.bones) { // Unweighted vertex positions, with alpha. let setupVertices = vertexAttachment.vertices; @@ -1502,7 +1498,6 @@ export class DeformTimeline extends CurveTimeline implements SlotTimeline { deform[i] += (lastVertices[i] - deform[i]) * alpha; break; case MixBlend.add: - let vertexAttachment = slotAttachment as VertexAttachment; if (!vertexAttachment.bones) { // Unweighted vertex positions, with alpha. let setupVertices = vertexAttachment.vertices; @@ -1526,7 +1521,6 @@ export class DeformTimeline extends CurveTimeline implements SlotTimeline { if (alpha == 1) { if (blend == MixBlend.add) { - let vertexAttachment = slotAttachment as VertexAttachment; if (!vertexAttachment.bones) { // Unweighted vertex positions, with alpha. let setupVertices = vertexAttachment.vertices; @@ -1550,7 +1544,6 @@ export class DeformTimeline extends CurveTimeline implements SlotTimeline { } else { switch (blend) { case MixBlend.setup: { - let vertexAttachment = slotAttachment as VertexAttachment; if (!vertexAttachment.bones) { // Unweighted vertex positions, with alpha. let setupVertices = vertexAttachment.vertices; @@ -1575,7 +1568,6 @@ export class DeformTimeline extends CurveTimeline implements SlotTimeline { } break; case MixBlend.add: - let vertexAttachment = slotAttachment as VertexAttachment; if (!vertexAttachment.bones) { // Unweighted vertex positions, with alpha. let setupVertices = vertexAttachment.vertices; @@ -2327,7 +2319,7 @@ export class SequenceTimeline extends Timeline implements SlotTimeline { let attachment = this.attachment as unknown as Attachment; if (slotAttachment != attachment) { if (!(slotAttachment instanceof VertexAttachment) - || (slotAttachment as VertexAttachment).timelineAttachment != attachment) return; + || slotAttachment.timelineAttachment != attachment) return; } if (direction == MixDirection.mixOut) { diff --git a/spine-ts/spine-core/src/AnimationState.ts b/spine-ts/spine-core/src/AnimationState.ts index 9bc1a4e44..bfcb0c6e4 100644 --- a/spine-ts/spine-core/src/AnimationState.ts +++ b/spine-ts/spine-core/src/AnimationState.ts @@ -938,7 +938,7 @@ export class TrackEntry { * * The `mixDuration` can be set manually rather than use the value from * {@link AnimationStateData#getMix()}. In that case, the `mixDuration` can be set for a new - * track entry only before {@link AnimationState#update(float)} is first called. + * track entry only before {@link AnimationState#update(float)} is next called. * * When using {@link AnimationState#addAnimation()} with a `delay` <= 0, note the * {@link #delay} is set using the mix duration from the {@link AnimationStateData}, not a mix duration set @@ -963,7 +963,7 @@ export class TrackEntry { * replaces the values from the lower tracks with the animation values. {@link MixBlend#add} adds the animation values to * the values from the lower tracks. * - * The `mixBlend` can be set for a new track entry only before {@link AnimationState#apply()} is first + * The `mixBlend` can be set for a new track entry only before {@link AnimationState#apply()} is next * called. */ mixBlend = MixBlend.replace; timelineMode = new Array(); @@ -1155,11 +1155,18 @@ export enum EventType { /** The interface to implement for receiving TrackEntry events. It is always safe to call AnimationState methods when receiving * events. - * + *

+ * TrackEntry events are collected during {@link AnimationState#update} and {@link AnimationState#apply} and + * fired only after those methods are finished. + *

* See TrackEntry {@link TrackEntry#listener} and AnimationState - * {@link AnimationState#addListener()}. */ + * {@link AnimationState#addListener}. */ export interface AnimationStateListener { - /** Invoked when this entry has been set as the current entry. */ + /** Invoked when this entry has been set as the current entry. {@link #end(TrackEntry)} will occur when this entry will no + * longer be applied. + *

+ * When this event is triggered by calling {@link AnimationState#setAnimation}, take care not to + * call {@link AnimationState#update} until after the TrackEntry has been configured. */ start?: (entry: TrackEntry) => void; /** Invoked when another entry has replaced this entry as the current entry. This entry may continue being applied for diff --git a/spine-ts/spine-core/src/Bone.ts b/spine-ts/spine-core/src/Bone.ts index fc082520d..8c8d40c6c 100644 --- a/spine-ts/spine-core/src/Bone.ts +++ b/spine-ts/spine-core/src/Bone.ts @@ -132,7 +132,7 @@ export class Bone implements Updatable { } /** Computes the world transform using the parent bone and this bone's local applied transform. */ - update (physics: Physics) { + update (physics: Physics | null) { this.updateWorldTransformWith(this.ax, this.ay, this.arotation, this.ascaleX, this.ascaleY, this.ashearX, this.ashearY); } diff --git a/spine-ts/spine-core/src/PathConstraint.ts b/spine-ts/spine-core/src/PathConstraint.ts index b535d70e8..9bdd57d0e 100644 --- a/spine-ts/spine-core/src/PathConstraint.ts +++ b/spine-ts/spine-core/src/PathConstraint.ts @@ -172,7 +172,7 @@ export class PathConstraint implements Updatable { } } - let positions = this.computeWorldPositions(attachment, spacesCount, tangents); + let positions = this.computeWorldPositions(attachment, spacesCount, tangents); let boneX = positions[0], boneY = positions[1], offsetRotation = data.offsetRotation; let tip = false; if (offsetRotation == 0) diff --git a/spine-ts/spine-core/src/PhysicsConstraint.ts b/spine-ts/spine-core/src/PhysicsConstraint.ts index 139013f59..0cee49b23 100644 --- a/spine-ts/spine-core/src/PhysicsConstraint.ts +++ b/spine-ts/spine-core/src/PhysicsConstraint.ts @@ -39,13 +39,8 @@ import { MathUtils } from "./Utils.js"; * See Physics constraints in the Spine User Guide. */ export class PhysicsConstraint implements Updatable { readonly data: PhysicsConstraintData; - private _bone: Bone | null = null; - /** The bone constrained by this physics constraint. */ - public set bone (bone: Bone) { this._bone = bone; } - public get bone () { - if (!this._bone) throw new Error("Bone not set.") - else return this._bone; - } + bone: Bone; + inertia = 0; strength = 0; damping = 0; @@ -80,6 +75,8 @@ export class PhysicsConstraint implements Updatable { this.data = data; this.skeleton = skeleton; + let bone = skeleton.findBone(data.bone.name); + if (!bone) throw new Error(`Couldn't find bone ${data.bone.name}.`); this.bone = skeleton.bones[data.bone.index]; this.inertia = data.inertia; diff --git a/spine-ts/spine-core/src/Skeleton.ts b/spine-ts/spine-core/src/Skeleton.ts index 871212191..5182ef29d 100644 --- a/spine-ts/spine-core/src/Skeleton.ts +++ b/spine-ts/spine-core/src/Skeleton.ts @@ -263,7 +263,8 @@ export class Skeleton { } sortPathConstraint (constraint: PathConstraint) { - constraint.active = constraint.target.bone.isActive() && (!constraint.data.skinRequired || (this.skin && Utils.contains(this.skin.constraints, constraint.data, true)))!; + constraint.active = constraint.target.bone.isActive() + && (!constraint.data.skinRequired || (this.skin && Utils.contains(this.skin.constraints, constraint.data, true)))!; if (!constraint.active) return; let slot = constraint.target; @@ -275,8 +276,7 @@ export class Skeleton { for (let i = 0, n = this.data.skins.length; i < n; i++) this.sortPathConstraintAttachment(this.data.skins[i], slotIndex, slotBone); - let attachment = slot.getAttachment(); - if (attachment instanceof PathAttachment) this.sortPathConstraintAttachmentWith(attachment, slotBone); + this.sortPathConstraintAttachmentWith(slot.attachment, slotBone); let constrained = constraint.bones; let boneCount = constrained.length; @@ -327,9 +327,9 @@ export class Skeleton { } } - sortPathConstraintAttachmentWith (attachment: Attachment, slotBone: Bone) { + sortPathConstraintAttachmentWith (attachment: Attachment | null, slotBone: Bone) { if (!(attachment instanceof PathAttachment)) return; - let pathBones = (attachment).bones; + let pathBones = attachment.bones; if (!pathBones) this.sortBone(slotBone); else { @@ -643,11 +643,10 @@ export class Skeleton { attachment.computeWorldVertices(slot, vertices, 0, 2); triangles = Skeleton.quadTriangles; } else if (attachment instanceof MeshAttachment) { - let mesh = (attachment); - verticesLength = mesh.worldVerticesLength; + verticesLength = attachment.worldVerticesLength; vertices = Utils.setArraySize(temp, verticesLength, 0); - mesh.computeWorldVertices(slot, 0, verticesLength, vertices, 0, 2); - triangles = mesh.triangles; + attachment.computeWorldVertices(slot, 0, verticesLength, vertices, 0, 2); + triangles = attachment.triangles; } else if (attachment instanceof ClippingAttachment && clipper != null) { clipper.clipStart(slot, attachment); continue; diff --git a/spine-ts/spine-core/src/SkeletonBounds.ts b/spine-ts/spine-core/src/SkeletonBounds.ts index d65393647..5c22214bd 100644 --- a/spine-ts/spine-core/src/SkeletonBounds.ts +++ b/spine-ts/spine-core/src/SkeletonBounds.ts @@ -78,15 +78,14 @@ export class SkeletonBounds { if (!slot.bone.active) continue; let attachment = slot.getAttachment(); if (attachment instanceof BoundingBoxAttachment) { - let boundingBox = attachment as BoundingBoxAttachment; - boundingBoxes.push(boundingBox); + boundingBoxes.push(attachment); let polygon = polygonPool.obtain(); - if (polygon.length != boundingBox.worldVerticesLength) { - polygon = Utils.newFloatArray(boundingBox.worldVerticesLength); + if (polygon.length != attachment.worldVerticesLength) { + polygon = Utils.newFloatArray(attachment.worldVerticesLength); } polygons.push(polygon); - boundingBox.computeWorldVertices(slot, 0, boundingBox.worldVerticesLength, polygon, 0, 2); + attachment.computeWorldVertices(slot, 0, attachment.worldVerticesLength, polygon, 0, 2); } } diff --git a/spine-ts/spine-core/src/Slot.ts b/spine-ts/spine-core/src/Slot.ts index 263b5c89f..c03f6517f 100644 --- a/spine-ts/spine-core/src/Slot.ts +++ b/spine-ts/spine-core/src/Slot.ts @@ -91,7 +91,7 @@ export class Slot { setAttachment (attachment: Attachment | null) { if (this.attachment == attachment) return; if (!(attachment instanceof VertexAttachment) || !(this.attachment instanceof VertexAttachment) - || (attachment).timelineAttachment != (this.attachment).timelineAttachment) { + || attachment.timelineAttachment != this.attachment.timelineAttachment) { this.deform.length = 0; } this.attachment = attachment; diff --git a/spine-ts/spine-webgl/src/SkeletonDebugRenderer.ts b/spine-ts/spine-webgl/src/SkeletonDebugRenderer.ts index cb4dfdb0e..762136b03 100644 --- a/spine-ts/spine-webgl/src/SkeletonDebugRenderer.ts +++ b/spine-ts/spine-webgl/src/SkeletonDebugRenderer.ts @@ -88,11 +88,11 @@ export class SkeletonDebugRenderer implements Disposable { let slots = skeleton.slots; for (let i = 0, n = slots.length; i < n; i++) { let slot = slots[i]; + if (!slot.bone.active) continue; let attachment = slot.getAttachment(); if (attachment instanceof RegionAttachment) { - let regionAttachment = attachment; let vertices = this.vertices; - regionAttachment.computeWorldVertices(slot, vertices, 0, 2); + attachment.computeWorldVertices(slot, vertices, 0, 2); shapes.line(vertices[0], vertices[1], vertices[2], vertices[3]); shapes.line(vertices[2], vertices[3], vertices[4], vertices[5]); shapes.line(vertices[4], vertices[5], vertices[6], vertices[7]); @@ -108,11 +108,10 @@ export class SkeletonDebugRenderer implements Disposable { if (!slot.bone.active) continue; let attachment = slot.getAttachment(); if (!(attachment instanceof MeshAttachment)) continue; - let mesh = attachment; let vertices = this.vertices; - mesh.computeWorldVertices(slot, 0, mesh.worldVerticesLength, vertices, 0, 2); - let triangles = mesh.triangles; - let hullLength = mesh.hullLength; + attachment.computeWorldVertices(slot, 0, attachment.worldVerticesLength, vertices, 0, 2); + let triangles = attachment.triangles; + let hullLength = attachment.hullLength; if (this.drawMeshTriangles) { shapes.setColor(this.triangleLineColor); for (let ii = 0, nn = triangles.length; ii < nn; ii += 3) { @@ -158,13 +157,12 @@ export class SkeletonDebugRenderer implements Disposable { if (!slot.bone.active) continue; let attachment = slot.getAttachment(); if (!(attachment instanceof PathAttachment)) continue; - let path = attachment; - let nn = path.worldVerticesLength; + let nn = attachment.worldVerticesLength; let world = this.temp = Utils.setArraySize(this.temp, nn, 0); - path.computeWorldVertices(slot, 0, nn, world, 0, 2); + attachment.computeWorldVertices(slot, 0, nn, world, 0, 2); let color = this.pathColor; let x1 = world[2], y1 = world[3], x2 = 0, y2 = 0; - if (path.closed) { + if (attachment.closed) { shapes.setColor(color); let cx1 = world[0], cy1 = world[1], cx2 = world[nn - 2], cy2 = world[nn - 1]; x2 = world[nn - 4]; @@ -207,10 +205,9 @@ export class SkeletonDebugRenderer implements Disposable { if (!slot.bone.active) continue; let attachment = slot.getAttachment(); if (!(attachment instanceof ClippingAttachment)) continue; - let clip = attachment; - let nn = clip.worldVerticesLength; + let nn = attachment.worldVerticesLength; let world = this.temp = Utils.setArraySize(this.temp, nn, 0); - clip.computeWorldVertices(slot, 0, nn, world, 0, 2); + attachment.computeWorldVertices(slot, 0, nn, world, 0, 2); for (let i = 0, n = world.length; i < n; i += 2) { let x = world[i]; let y = world[i + 1];