Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Spine configurable with custom Sprite and Mesh constructors #490

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions packages/base/src/SpineBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {DRAW_MODES} from '@pixi/constants';
import {Container, DisplayObject} from '@pixi/display';
import {Sprite} from '@pixi/sprite';
import {SimpleMesh} from '@pixi/mesh-extras';
import {Graphics} from '@pixi/graphics'
import {Graphics} from '@pixi/graphics';
import {Rectangle, Polygon, Transform} from '@pixi/math';
import {hex2rgb, rgb2hex} from '@pixi/utils';
import type {Texture} from '@pixi/core';
Expand All @@ -33,26 +33,32 @@ let tempRgb = [0, 0, 0];
export interface ISpineDisplayObject extends DisplayObject {
region?: TextureRegion;
attachment?: IAttachment;
spineAnimation?: SpineBase<ISkeleton, ISkeletonData, IAnimationState, IAnimationStateData>;
}

/**
* @public
*/
export class SpineSprite extends Sprite implements ISpineDisplayObject {

export class SpineSprite extends Sprite {
region?: TextureRegion = null;
attachment?: IAttachment = null;
spineAnimation?: SpineBase<ISkeleton, ISkeletonData, IAnimationState, IAnimationStateData> = null;
}


/**
* @public
*/
export class SpineMesh extends SimpleMesh implements ISpineDisplayObject {
region?: TextureRegion = null;
attachment?: IAttachment = null;
spineAnimation?: SpineBase<ISkeleton, ISkeletonData, IAnimationState, IAnimationStateData> = null;
}

constructor(texture: Texture, vertices?: Float32Array, uvs?: Float32Array, indices?: Uint16Array, drawMode?: number) {
super(texture, vertices, uvs, indices, drawMode);
}
export interface SpineSpriteConstructor {
new (texture?: Texture): SpineSprite;
}

export interface SpineMeshConstructor {
new (texture: Texture, vertices?: Float32Array, uvs?: Float32Array, indices?: Uint16Array, drawMode?: number): SpineMesh;
}

/**
Expand Down Expand Up @@ -98,10 +104,12 @@ export abstract class SpineBase<Skeleton extends ISkeleton,
this._debug = value;
}

private SpriteCtor: SpineSpriteConstructor;
private MeshCtor: SpineMeshConstructor;

abstract createSkeleton(spineData: ISkeletonData);

constructor(spineData: SkeletonData) {
constructor(spineData: SkeletonData, ctors?: { SpriteCtor?: SpineSpriteConstructor, MeshCtor?: SpineMeshConstructor }) {
super();

if (!spineData) {
Expand All @@ -112,6 +120,9 @@ export abstract class SpineBase<Skeleton extends ISkeleton,
throw new Error('spineData param cant be string. Please use spine.Spine.fromAtlas("YOUR_RESOURCE_NAME") from now on.');
}

this.SpriteCtor = ctors?.SpriteCtor || SpineSprite;
this.MeshCtor = ctors?.MeshCtor || SpineMesh;

/**
* The spineData object
*
Expand Down Expand Up @@ -562,6 +573,8 @@ export abstract class SpineBase<Skeleton extends ISkeleton,
let texture = region ? region.texture : null;
let sprite = this.newSprite(texture);

sprite.spineAnimation = this;

sprite.anchor.set(0.5);
if (region) {
this.setSpriteRegion(attachment, sprite, attachment.region);
Expand Down Expand Up @@ -592,6 +605,8 @@ export abstract class SpineBase<Skeleton extends ISkeleton,
new Uint16Array(attachment.triangles),
DRAW_MODES.TRIANGLES);

strip.spineAnimation = this;

if (typeof (strip as any)._canvasPadding !== "undefined") {
(strip as any)._canvasPadding = 1.5;
}
Expand Down Expand Up @@ -741,15 +756,15 @@ export abstract class SpineBase<Skeleton extends ISkeleton,
}

newSprite(tex: Texture) {
return new SpineSprite(tex);
return new this.SpriteCtor(tex);
}

newGraphics() {
return new Graphics();
}

newMesh(texture: Texture, vertices?: Float32Array, uvs?: Float32Array, indices?: Uint16Array, drawMode?: number) {
return new SpineMesh(texture, vertices, uvs, indices, drawMode);
return new this.MeshCtor(texture, vertices, uvs, indices, drawMode);
}

transformHack() {
Expand Down