Skip to content

Commit 77b6a5b

Browse files
committed
[ts][pixi] Add JSDoc.
1 parent d0480da commit 77b6a5b

File tree

1 file changed

+66
-8
lines changed

1 file changed

+66
-8
lines changed

spine-ts/spine-pixi/src/Spine.ts

+66-8
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,32 @@ import {
4747
import type { SpineTexture } from "./SpineTexture.js";
4848
import { SlotMesh } from "./SlotMesh.js";
4949
import { DarkSlotMesh } from "./DarkSlotMesh.js";
50-
import type { ISpineDebugRenderer } from "./SpineDebugRenderer.js";
50+
import type { ISpineDebugRenderer, SpineDebugRenderer } from "./SpineDebugRenderer.js";
5151
import { Assets } from "@pixi/assets";
5252
import type { IPointData } from "@pixi/core";
53-
import { Ticker, utils } from "@pixi/core";
53+
import { Ticker } from "@pixi/core";
5454
import type { IDestroyOptions, DisplayObject } from "@pixi/display";
5555
import { Container } from "@pixi/display";
5656

57+
/**
58+
* Options to configure a {@link Spine} game object.
59+
*/
5760
export interface ISpineOptions {
61+
/** Set the {@link Spine.autoUpdate} value. If omitted, it is set to `true`. */
5862
autoUpdate?: boolean;
63+
/** The value passed to the skeleton reader. If omitted, 1 is passed. See {@link SkeletonBinary.scale} for details. */
64+
scale?: number;
65+
/**
66+
* A factory to override the default ones to render Spine meshes ({@link DarkSlotMesh} or {@link SlotMesh}).
67+
* If omitted, a factory returning a ({@link DarkSlotMesh} or {@link SlotMesh}) will be used depending on the presence of
68+
* a dark tint mesh in the skeleton.
69+
* */
5970
slotMeshFactory?: () => ISlotMesh;
6071
}
6172

73+
/**
74+
* AnimationStateListener {@link https://en.esotericsoftware.com/spine-api-reference#AnimationStateListener events} exposed for Pixi.
75+
*/
6276
export interface SpineEvents {
6377
complete: [trackEntry: TrackEntry];
6478
dispose: [trackEntry: TrackEntry];
@@ -68,14 +82,23 @@ export interface SpineEvents {
6882
start: [trackEntry: TrackEntry];
6983
}
7084

85+
/**
86+
* The class to instantiate a {@link Spine} game object in Pixi.
87+
* The static method {@link Spine.from} should be used to instantiate a Spine game object.
88+
*/
7189
export class Spine extends Container {
90+
/** The skeleton for this Spine game object. */
7291
public skeleton: Skeleton;
92+
/** The animation state for this Spine game object. */
7393
public state: AnimationState;
7494

7595
private _debug?: ISpineDebugRenderer | undefined = undefined;
7696
public get debug (): ISpineDebugRenderer | undefined {
7797
return this._debug;
7898
}
99+
/** Pass a {@link SpineDebugRenderer} or create your own {@link ISpineDebugRenderer} to render bones, meshes, ...
100+
* @example spineGO.debug = new SpineDebugRenderer();
101+
*/
79102
public set debug (value: ISpineDebugRenderer | undefined) {
80103
if (this._debug) {
81104
this._debug.unregisterSpine(this);
@@ -86,13 +109,14 @@ export class Spine extends Container {
86109
this._debug = value;
87110
}
88111

89-
protected slotMeshFactory: () => ISlotMesh = ((): ISlotMesh => new SlotMesh());
112+
protected slotMeshFactory: () => ISlotMesh = () => new SlotMesh();
90113

91114
private autoUpdateWarned: boolean = false;
92115
private _autoUpdate: boolean = true;
93116
public get autoUpdate (): boolean {
94117
return this._autoUpdate;
95118
}
119+
/** When `true`, the Spine AnimationState and the Skeleton will be automatically updated using the {@link Ticker.shared} instance. */
96120
public set autoUpdate (value: boolean) {
97121
if (value) {
98122
Ticker.shared.add(this.internalUpdate, this);
@@ -115,7 +139,6 @@ export class Spine extends Container {
115139
private lightColor = new Color();
116140
private darkColor = new Color();
117141

118-
119142
constructor (skeletonData: SkeletonData, options?: ISpineOptions) {
120143
super();
121144

@@ -134,13 +157,14 @@ export class Spine extends Container {
134157
} else {
135158
for (let i = 0; i < this.skeleton.slots.length; i++) {
136159
if (this.skeleton.slots[i].data.darkColor) {
137-
this.slotMeshFactory = options?.slotMeshFactory ?? ((): ISlotMesh => new DarkSlotMesh());
160+
this.slotMeshFactory = options?.slotMeshFactory ?? (() => new DarkSlotMesh());
138161
break;
139162
}
140163
}
141164
}
142165
}
143166

167+
/** If {@link Spine.autoUpdate} is `false`, this method allows to update the AnimationState and the Skeleton with the given delta. */
144168
public update (deltaSeconds: number): void {
145169
if (this.autoUpdate && !this.autoUpdateWarned) {
146170
console.warn("You are calling update on a Spine instance that has autoUpdate set to true. This is probably not what you want.");
@@ -156,6 +180,7 @@ export class Spine extends Container {
156180
this.skeleton.update(delta);
157181
}
158182

183+
/** Before rendering, apply the state change to the Spine AnimationState and update the skeleton transform, then call the {@link Container.updateTransform}. */
159184
public override updateTransform (): void {
160185
this.updateSpineTransform();
161186
this.debug?.renderDebug(this);
@@ -171,6 +196,7 @@ export class Spine extends Container {
171196
this.sortChildren();
172197
}
173198

199+
/** Destroy Spine game object elements, then call the {@link Container.destroy} with the given options */
174200
public override destroy (options?: boolean | IDestroyOptions | undefined): void {
175201
for (const [, mesh] of this.meshesCache) {
176202
mesh?.destroy();
@@ -320,13 +346,19 @@ export class Spine extends Container {
320346
Spine.clipper.clipEnd();
321347
}
322348

349+
/**
350+
* Set the position of the bone given in input through a {@link IPointData}.
351+
* @param bone: the bone name or the bone instance to set the position
352+
* @param outPos: the new position of the bone.
353+
* @throws {Error}: if the given bone is not found in the skeleton, an error is thrown
354+
*/
323355
public setBonePosition (bone: string | Bone, position: IPointData): void {
324356
const boneAux = bone;
325357
if (typeof bone === "string") {
326358
bone = this.skeleton.findBone(bone)!;
327359
}
328360

329-
if (!bone) throw Error(`Cant set bone position, bone ${String(boneAux)} not found`);
361+
if (!bone) throw Error(`Cannot set bone position, bone ${String(boneAux)} not found`);
330362
Spine.vectorAux.set(position.x, position.y);
331363

332364
if (bone.parent) {
@@ -340,14 +372,20 @@ export class Spine extends Container {
340372
}
341373
}
342374

375+
/**
376+
* Return the position of the bone given in input into an {@link IPointData}.
377+
* @param bone: the bone name or the bone instance to get the position from
378+
* @param outPos: an optional {@link IPointData} to use to return the bone position, rathern than instantiating a new object.
379+
* @returns {IPointData | undefined}: the position of the bone, or undefined if no matching bone is found in the skeleton
380+
*/
343381
public getBonePosition (bone: string | Bone, outPos?: IPointData): IPointData | undefined {
344382
const boneAux = bone;
345383
if (typeof bone === "string") {
346384
bone = this.skeleton.findBone(bone)!;
347385
}
348386

349387
if (!bone) {
350-
console.error(`Cant set bone position! Bone ${String(boneAux)} not found`);
388+
console.error(`Cannot get bone position! Bone ${String(boneAux)} not found`);
351389
return outPos;
352390
}
353391

@@ -360,9 +398,26 @@ export class Spine extends Container {
360398
return outPos;
361399
}
362400

401+
/** A cache containing skeleton data and atlases already loaded by {@link Spine.from}. */
363402
public static readonly skeletonCache: Record<string, SkeletonData> = Object.create(null);
364403

365-
public static from (skeletonAssetName: string, atlasAssetName: string, options?: ISpineOptions & { scale?: number }): Spine {
404+
/**
405+
* Use this method to instantiate a Spine game object.
406+
* Before instantiating a Spine game object, the skeleton (`.skel` or `.json`) and the atlas text files must be loaded into the Assets. For example:
407+
* ```
408+
* PIXI.Assets.add("sackData", "./assets/sack-pro.skel");
409+
* PIXI.Assets.add("sackAtlas", "./assets/sack-pma.atlas");
410+
* await PIXI.Assets.load(["sackData", "sackAtlas"]);
411+
* ```
412+
* Once a Spine game object is created, its skeleton data is cached into {@link Spine.skeletonCache} using the key:
413+
* `${skeletonAssetName}-${atlasAssetName}-${options?.scale ?? 1}`
414+
*
415+
* @param skeletonAssetName - the asset name for the skeleton `.skel` or `.json` file previously loaded into the Assets
416+
* @param atlasAssetName - the asset name for the atlas file previously loaded into the Assets
417+
* @param options - Options to configure the Spine game object
418+
* @returns {Spine} The Spine game object instantiated
419+
*/
420+
public static from (skeletonAssetName: string, atlasAssetName: string, options?: ISpineOptions): Spine {
366421
const cacheKey = `${skeletonAssetName}-${atlasAssetName}-${options?.scale ?? 1}`;
367422
let skeletonData = Spine.skeletonCache[cacheKey];
368423
if (skeletonData) {
@@ -381,6 +436,9 @@ export class Spine extends Container {
381436

382437
Skeleton.yDown = true;
383438

439+
/**
440+
* Represents the mesh type used in a Spine objects. Available implementations are {@link DarkSlotMesh} and {@link SlotMesh}.
441+
*/
384442
export interface ISlotMesh extends DisplayObject {
385443
name: string;
386444
updateFromSpineData (

0 commit comments

Comments
 (0)