From 78ff95588adb9fd5b2c3cc1edaa27bf42098f0d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail=20Hatip?= Date: Thu, 9 Jun 2022 16:07:29 +0300 Subject: [PATCH] pixi 5.3.8 changes and build --- bundles/all-3.8/rollup.config.js | 36 +- bundles/all-4.0/rollup.config.js | 36 +- bundles/pixi-spine/rollup.config.js | 40 +- packages/base/package.json | 12 +- packages/base/src/SpineBase.ts | 299 ++-- packages/base/src/core/ISkeleton.ts | 131 +- packages/base/src/core/TextureAtlas.ts | 155 ++- packages/base/src/core/TextureRegion.ts | 52 +- packages/loader-3.8/package.json | 5 +- packages/loader-3.8/src/index.ts | 21 +- packages/loader-4.0/package.json | 5 +- packages/loader-4.0/src/index.ts | 20 +- packages/loader-base/package.json | 6 +- packages/loader-base/src/loaders.ts | 213 ++- packages/loader-uni/package.json | 2 +- packages/loader-uni/src/SpineLoader.ts | 48 +- packages/runtime-3.7/package.json | 3 +- packages/runtime-3.7/src/core/Bone.ts | 130 +- packages/runtime-3.7/src/core/SkeletonJson.ts | 672 ++++++--- packages/runtime-3.7/src/core/SlotData.ts | 11 +- packages/runtime-3.8/package.json | 97 +- packages/runtime-3.8/src/Spine.ts | 17 +- packages/runtime-3.8/src/core/Bone.ts | 132 +- .../runtime-3.8/src/core/SkeletonBinary.ts | 1006 +++++++++----- packages/runtime-3.8/src/core/SkeletonJson.ts | 731 +++++++--- packages/runtime-3.8/src/core/SlotData.ts | 11 +- packages/runtime-4.0/package.json | 3 +- packages/runtime-4.0/src/core/Bone.ts | 150 +- .../runtime-4.0/src/core/SkeletonBinary.ts | 1167 +++++++++++++--- packages/runtime-4.0/src/core/SkeletonJson.ts | 1218 ++++++++++++++--- packages/runtime-4.0/src/core/SlotData.ts | 13 +- 31 files changed, 4816 insertions(+), 1626 deletions(-) diff --git a/bundles/all-3.8/rollup.config.js b/bundles/all-3.8/rollup.config.js index 90d4eb10..8e0d98b1 100644 --- a/bundles/all-3.8/rollup.config.js +++ b/bundles/all-3.8/rollup.config.js @@ -1,31 +1,37 @@ -const { main } = require('@pixi-spine/rollup-config/main'); +const { main } = require("@pixi-spine/rollup-config/main"); const results = main({ globals: { - '@pixi-spine/base': 'PIXI.spine', - '@pixi-spine/runtime-3.8': 'PIXI.spine38', - '@pixi-spine/loader-base': 'PIXI.spine', - '@pixi-spine/loader-3.8': 'PIXI.spine', + "@pixi-spine/base": "PIXI.spine", + "@pixi-spine/runtime-3.8": "PIXI.spine38", + "@pixi-spine/loader-base": "PIXI.spine", + "@pixi-spine/loader-3.8": "PIXI.spine", }, }); // TODO: get sorted deps of all our @pixi-spine deps -const umdDeps = ['@pixi/app', '@pixi/constants', '@pixi/core', '@pixi/display', '@pixi/graphics', - '@pixi/loaders', '@pixi/math', '@pixi/mesh-extras', '@pixi/sprite', '@pixi/utils']; +const umdDeps = ["pixi.js-legacy"]; -const license1 = 'is licensed under the MIT License.\n * http://www.opensource.org/licenses/mit-license'; -const licenseSpine = 'is licensed under SPINE-LICENSE\n * http://esotericsoftware.com/spine-runtimes-license'; +const license1 = + "is licensed under the MIT License.\n * http://www.opensource.org/licenses/mit-license"; +const licenseSpine = + "is licensed under SPINE-LICENSE\n * http://esotericsoftware.com/spine-runtimes-license"; results.forEach((entry) => { if (entry.output.banner) { - entry.output.banner = entry.output.banner.replace(license1, licenseSpine); + entry.output.banner = entry.output.banner.replace( + license1, + licenseSpine + ); } - if (entry.output.format === 'umd') { - entry.external = entry.external.filter((moduleName) => { - return moduleName.indexOf('@pixi-spine') !== 0; - }).concat(umdDeps); + if (entry.output.format === "umd") { + entry.external = entry.external + .filter((moduleName) => { + return moduleName.indexOf("@pixi-spine") !== 0; + }) + .concat(umdDeps); } -}) +}); module.exports = results; diff --git a/bundles/all-4.0/rollup.config.js b/bundles/all-4.0/rollup.config.js index f8b9175e..d7ffe8ff 100644 --- a/bundles/all-4.0/rollup.config.js +++ b/bundles/all-4.0/rollup.config.js @@ -1,31 +1,37 @@ -const { main } = require('@pixi-spine/rollup-config/main'); +const { main } = require("@pixi-spine/rollup-config/main"); const results = main({ globals: { - '@pixi-spine/base': 'PIXI.spine', - '@pixi-spine/runtime-4.0': 'PIXI.spine40', - '@pixi-spine/loader-base': 'PIXI.spine', - '@pixi-spine/loader-4.0': 'PIXI.spine', + "@pixi-spine/base": "PIXI.spine", + "@pixi-spine/runtime-4.0": "PIXI.spine40", + "@pixi-spine/loader-base": "PIXI.spine", + "@pixi-spine/loader-4.0": "PIXI.spine", }, }); // TODO: get sorted deps of all our @pixi-spine deps -const umdDeps = ['@pixi/app', '@pixi/constants', '@pixi/core', '@pixi/display', '@pixi/graphics', - '@pixi/loaders', '@pixi/math', '@pixi/mesh-extras', '@pixi/sprite', '@pixi/utils']; +const umdDeps = ["pixi.js-legacy"]; -const license1 = 'is licensed under the MIT License.\n * http://www.opensource.org/licenses/mit-license'; -const licenseSpine = 'is licensed under SPINE-LICENSE\n * http://esotericsoftware.com/spine-runtimes-license'; +const license1 = + "is licensed under the MIT License.\n * http://www.opensource.org/licenses/mit-license"; +const licenseSpine = + "is licensed under SPINE-LICENSE\n * http://esotericsoftware.com/spine-runtimes-license"; results.forEach((entry) => { if (entry.output.banner) { - entry.output.banner = entry.output.banner.replace(license1, licenseSpine); + entry.output.banner = entry.output.banner.replace( + license1, + licenseSpine + ); } - if (entry.output.format === 'umd') { - entry.external = entry.external.filter((moduleName) => { - return moduleName.indexOf('@pixi-spine') !== 0; - }).concat(umdDeps); + if (entry.output.format === "umd") { + entry.external = entry.external + .filter((moduleName) => { + return moduleName.indexOf("@pixi-spine") !== 0; + }) + .concat(umdDeps); } -}) +}); module.exports = results; diff --git a/bundles/pixi-spine/rollup.config.js b/bundles/pixi-spine/rollup.config.js index 45a1908a..e513577c 100644 --- a/bundles/pixi-spine/rollup.config.js +++ b/bundles/pixi-spine/rollup.config.js @@ -1,33 +1,39 @@ -const { main } = require('@pixi-spine/rollup-config/main'); +const { main } = require("@pixi-spine/rollup-config/main"); const results = main({ globals: { - '@pixi-spine/base': 'PIXI.spine', - '@pixi-spine/runtime-3.7': 'PIXI.spine37', - '@pixi-spine/runtime-3.8': 'PIXI.spine38', - '@pixi-spine/runtime-4.0': 'PIXI.spine40', - '@pixi-spine/loader-base': 'PIXI.spine', - '@pixi-spine/loader-uni': 'PIXI.spine', + "@pixi-spine/base": "PIXI.spine", + "@pixi-spine/runtime-3.7": "PIXI.spine37", + "@pixi-spine/runtime-3.8": "PIXI.spine38", + "@pixi-spine/runtime-4.0": "PIXI.spine40", + "@pixi-spine/loader-base": "PIXI.spine", + "@pixi-spine/loader-uni": "PIXI.spine", }, }); // TODO: get sorted deps of all our @pixi-spine deps -const umdDeps = ['@pixi/app', '@pixi/constants', '@pixi/core', '@pixi/display', '@pixi/graphics', - '@pixi/loaders', '@pixi/math', '@pixi/mesh-extras', '@pixi/sprite', '@pixi/utils']; +const umdDeps = ["pixi.js-legacy"]; -const license1 = 'is licensed under the MIT License.\n * http://www.opensource.org/licenses/mit-license'; -const licenseSpine = 'is licensed under SPINE-LICENSE\n * http://esotericsoftware.com/spine-runtimes-license'; +const license1 = + "is licensed under the MIT License.\n * http://www.opensource.org/licenses/mit-license"; +const licenseSpine = + "is licensed under SPINE-LICENSE\n * http://esotericsoftware.com/spine-runtimes-license"; results.forEach((entry) => { if (entry.output.banner) { - entry.output.banner = entry.output.banner.replace(license1, licenseSpine); + entry.output.banner = entry.output.banner.replace( + license1, + licenseSpine + ); } - if (entry.output.format === 'umd') { - entry.external = entry.external.filter((moduleName) => { - return moduleName.indexOf('@pixi-spine') !== 0; - }).concat(umdDeps); + if (entry.output.format === "umd") { + entry.external = entry.external + .filter((moduleName) => { + return moduleName.indexOf("@pixi-spine") !== 0; + }) + .concat(umdDeps); } -}) +}); module.exports = results; diff --git a/packages/base/package.json b/packages/base/package.json index fab7c1db..5c8d959e 100644 --- a/packages/base/package.json +++ b/packages/base/package.json @@ -8,17 +8,7 @@ "namespace": "PIXI.spine", "types": "./index.d.ts", "peerDependencies": { - "@pixi/constants": "^6.1.0", - "@pixi/core": "^6.1.0", - "@pixi/display": "^6.1.0", - "@pixi/graphics": "^6.1.0", - "@pixi/math": "^6.1.0", - "@pixi/mesh": "^6.1.0", - "@pixi/mesh-extras": "^6.1.0", - "@pixi/settings": "^6.1.0", - "@pixi/runner": "^6.1.0", - "@pixi/sprite": "^6.1.0", - "@pixi/utils": "^6.1.0" + "pixi.js-legacy": "5.3.8" }, "scripts": { "build": "rollup -c node_modules/@pixi-spine/rollup-config/index.js --silent", diff --git a/packages/base/src/SpineBase.ts b/packages/base/src/SpineBase.ts index 53a346d2..43bb559e 100644 --- a/packages/base/src/SpineBase.ts +++ b/packages/base/src/SpineBase.ts @@ -1,36 +1,28 @@ -import {AttachmentType} from './core/AttachmentType'; -import {TextureRegion} from './core/TextureRegion'; -import {TextureAtlasRegion} from './core/TextureAtlas'; -import {MathUtils} from './core/Utils'; +import { AttachmentType } from "./core/AttachmentType"; +import { TextureRegion } from "./core/TextureRegion"; +import { TextureAtlasRegion } from "./core/TextureAtlas"; +import { MathUtils } from "./core/Utils"; +import type { IAnimationState, IAnimationStateData } from "./core/IAnimation"; import type { - IAnimationState, - IAnimationStateData -} from './core/IAnimation'; -import type { - IAttachment, IClippingAttachment, IMeshAttachment, + IAttachment, + IClippingAttachment, + IMeshAttachment, IRegionAttachment, ISkeleton, ISkeletonData, ISlot, - IVertexAttachment -} from './core/ISkeleton'; - -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 {Rectangle, Polygon, Transform} from '@pixi/math'; -import {hex2rgb, rgb2hex} from '@pixi/utils'; -import type {Texture} from '@pixi/core'; -import {settings} from "./settings"; + IVertexAttachment, +} from "./core/ISkeleton"; + +import { settings } from "./settings"; +import * as PIXI from "pixi.js-legacy"; let tempRgb = [0, 0, 0]; /** * @public */ -export interface ISpineDisplayObject extends DisplayObject { +export interface ISpineDisplayObject extends PIXI.DisplayObject { region?: TextureRegion; attachment?: IAttachment; } @@ -38,7 +30,7 @@ export interface ISpineDisplayObject extends DisplayObject { /** * @public */ -export class SpineSprite extends Sprite implements ISpineDisplayObject { +export class SpineSprite extends PIXI.Sprite implements ISpineDisplayObject { region?: TextureRegion = null; attachment?: IAttachment = null; } @@ -46,11 +38,17 @@ export class SpineSprite extends Sprite implements ISpineDisplayObject { /** * @public */ -export class SpineMesh extends SimpleMesh implements ISpineDisplayObject { +export class SpineMesh extends PIXI.SimpleMesh implements ISpineDisplayObject { region?: TextureRegion = null; attachment?: IAttachment = null; - constructor(texture: Texture, vertices?: Float32Array, uvs?: Float32Array, indices?: Uint16Array, drawMode?: number) { + constructor( + texture: PIXI.Texture, + vertices?: Float32Array, + uvs?: Float32Array, + indices?: Uint16Array, + drawMode?: number + ) { super(texture, vertices, uvs, indices, drawMode); } } @@ -70,19 +68,22 @@ export class SpineMesh extends SimpleMesh implements ISpineDisplayObject { * @memberof spine * @param spineData {object} The spine data loaded from a spine atlas. */ -export abstract class SpineBase - extends Container implements GlobalMixins.Spine { - +export abstract class SpineBase< + Skeleton extends ISkeleton, + SkeletonData extends ISkeletonData, + AnimationState extends IAnimationState, + AnimationStateData extends IAnimationStateData + > + extends PIXI.Container + implements GlobalMixins.Spine +{ tintRgb: ArrayLike; spineData: SkeletonData; skeleton: Skeleton; stateData: AnimationStateData; state: AnimationState; - slotContainers: Array; - tempClipContainers: Array; + slotContainers: Array; + tempClipContainers: Array; localDelayLimit: number; private _autoUpdate: boolean; protected _visible: boolean; @@ -93,11 +94,13 @@ export abstract class SpineBase = null, dark: ArrayLike = null; + let light: ArrayLike = null, + dark: ArrayLike = null; if (globalClr) { light = globalClr.light; @@ -273,7 +284,10 @@ export abstract class SpineBase = []; //@ts-ignore createGraphics(slot: ISlot, clip: IClippingAttachment) { let graphics = this.newGraphics(); - let poly = new Polygon([]); + let poly = new PIXI.Polygon([]); graphics.clear(); graphics.beginFill(0xffffff, 1); graphics.drawPolygon(poly as any); @@ -588,7 +641,7 @@ export abstract class SpineBase>; - getAttachment (slotIndex: number, name: string): IAttachment | null; + getAttachment(slotIndex: number, name: string): IAttachment | null; } /** @@ -49,10 +50,18 @@ export interface IAttachment { /** * @public */ -export interface IVertexAttachment extends IAttachment { +export interface IVertexAttachment + extends IAttachment { id: number; computeWorldVerticesOld(slot: Slot, worldVertices: ArrayLike): void; - computeWorldVertices(slot: Slot, start: number, count: number, worldVertices: ArrayLike, offset: number, stride: number): void; + computeWorldVertices( + slot: Slot, + start: number, + count: number, + worldVertices: ArrayLike, + offset: number, + stride: number + ): void; worldVerticesLength: number; } @@ -69,7 +78,13 @@ export interface IClippingAttachment extends IVertexAttachment { export interface IRegionAttachment extends IAttachment { region: TextureRegion; color: Color; - x, y, scaleX, scaleY, rotation, width, height: number; + x; + y; + scaleX; + scaleY; + rotation; + width; + height: number; } /** @@ -78,8 +93,8 @@ export interface IRegionAttachment extends IAttachment { export interface IMeshAttachment extends IVertexAttachment { region: TextureRegion; color: Color; - regionUVs: Float32Array, - triangles: number[], + regionUVs: Float32Array; + triangles: number[]; } /** @@ -92,7 +107,7 @@ export interface ISlotData { color: Color; darkColor: Color; attachmentName: string; - blendMode: BLEND_MODES; + blendMode: PIXI.BLEND_MODES; } /** @@ -143,29 +158,31 @@ export interface ISlot { /** * @public */ -export interface ISkeleton { - bones: Bone[] - slots: Slot[] - drawOrder: Slot[] +export interface ISkeleton< + SkeletonData extends ISkeletonData = ISkeletonData, + Bone extends IBone = IBone, + Slot extends ISlot = ISlot, + Skin extends ISkin = ISkin +> { + bones: Bone[]; + slots: Slot[]; + drawOrder: Slot[]; skin: Skin; data: SkeletonData; - updateWorldTransform (): void; - setToSetupPose (): void; - findSlotIndex (slotName: string): number; - getAttachmentByName (slotName: string, attachmentName: string): IAttachment; - - setBonesToSetupPose (): void; - setSlotsToSetupPose (): void; - findBone (boneName: string): Bone; - findSlot (slotName: string): Slot; - findBoneIndex (boneName: string): number; - findSlotIndex (slotName: string): number; - setSkinByName (skinName: string): void; - setAttachment (slotName: string, attachmentName: string): void; - getBounds (offset: Vector2, size: Vector2, temp: Array): void; + updateWorldTransform(): void; + setToSetupPose(): void; + findSlotIndex(slotName: string): number; + getAttachmentByName(slotName: string, attachmentName: string): IAttachment; + + setBonesToSetupPose(): void; + setSlotsToSetupPose(): void; + findBone(boneName: string): Bone; + findSlot(slotName: string): Slot; + findBoneIndex(boneName: string): number; + findSlotIndex(slotName: string): number; + setSkinByName(skinName: string): void; + setAttachment(slotName: string, attachmentName: string): void; + getBounds(offset: Vector2, size: Vector2, temp: Array): void; } /** @@ -178,14 +195,16 @@ export interface ISkeletonParser { /** * @public */ -export interface ISkeletonData { +export interface ISkeletonData< + BoneData extends IBoneData = IBoneData, + SlotData extends ISlotData = ISlotData, + Skin extends ISkin = ISkin, + Animation extends IAnimation = IAnimation, + EventData extends IEventData = IEventData, + IkConstraintData extends IIkConstraintData = IIkConstraintData, + TransformConstraintData extends ITransformConstraintData = ITransformConstraintData, + PathConstraintData extends IPathConstraintData = IPathConstraintData +> { name: string; bones: BoneData[]; slots: SlotData[]; @@ -204,12 +223,14 @@ export interface ISkeletonData(); regions = new Array(); - constructor(atlasText?: string, textureLoader?: (path: string, loaderFunction: (tex: BaseTexture) => any) => any, callback?: (obj: TextureAtlas) => any) { + constructor( + atlasText?: string, + textureLoader?: ( + path: string, + loaderFunction: (tex: PIXI.BaseTexture) => any + ) => any, + callback?: (obj: TextureAtlas) => any + ) { if (atlasText) { this.addSpineAtlas(atlasText, textureLoader, callback); } } - addTexture(name: string, texture: Texture) { + addTexture(name: string, texture: PIXI.Texture) { let pages = this.pages; let page: TextureAtlasPage = null; for (let i = 0; i < pages.length; i++) { @@ -42,7 +49,7 @@ export class TextureAtlas implements Disposable { } if (page === null) { page = new TextureAtlasPage(); - page.name = 'texturePage'; + page.name = "texturePage"; let baseTexture = texture.baseTexture; page.width = baseTexture.realWidth; page.height = baseTexture.realHeight; @@ -62,19 +69,38 @@ export class TextureAtlas implements Disposable { return region; } - addTextureHash(textures: Map, stripExtension: boolean) { + addTextureHash(textures: Map, stripExtension: boolean) { for (let key in textures) { if (textures.hasOwnProperty(key)) { - this.addTexture(stripExtension && key.indexOf('.') !== -1 ? key.substr(0, key.lastIndexOf('.')) : key, textures[key]); + this.addTexture( + stripExtension && key.indexOf(".") !== -1 + ? key.substr(0, key.lastIndexOf(".")) + : key, + textures[key] + ); } } } - public addSpineAtlas(atlasText: string, textureLoader: (path: string, loaderFunction: (tex: BaseTexture) => any) => any, callback: (obj: TextureAtlas) => any) { + public addSpineAtlas( + atlasText: string, + textureLoader: ( + path: string, + loaderFunction: (tex: PIXI.BaseTexture) => any + ) => any, + callback: (obj: TextureAtlas) => any + ) { return this.load(atlasText, textureLoader, callback); } - private load(atlasText: string, textureLoader: (path: string, loaderFunction: (tex: BaseTexture) => any) => any, callback: (obj: TextureAtlas) => any) { + private load( + atlasText: string, + textureLoader: ( + path: string, + loaderFunction: (tex: PIXI.BaseTexture) => any + ) => any, + callback: (obj: TextureAtlas) => any + ) { if (textureLoader == null) throw new Error("textureLoader cannot be null."); @@ -95,19 +121,21 @@ export class TextureAtlas implements Disposable { page.magFilter = filterFromString(entry[2]); }; pageFields["repeat"] = () => { - if (entry[1].indexOf('x') != -1) page.uWrap = TextureWrap.Repeat; - if (entry[1].indexOf('y') != -1) page.vWrap = TextureWrap.Repeat; + if (entry[1].indexOf("x") != -1) page.uWrap = TextureWrap.Repeat; + if (entry[1].indexOf("y") != -1) page.vWrap = TextureWrap.Repeat; }; pageFields["pma"] = () => { page.pma = entry[1] == "true"; }; let regionFields: Map = {}; - regionFields["xy"] = () => { // Deprecated, use bounds. + regionFields["xy"] = () => { + // Deprecated, use bounds. region.x = parseInt(entry[1]); region.y = parseInt(entry[2]); }; - regionFields["size"] = () => { // Deprecated, use bounds. + regionFields["size"] = () => { + // Deprecated, use bounds. region.width = parseInt(entry[1]); region.height = parseInt(entry[2]); }; @@ -117,11 +145,13 @@ export class TextureAtlas implements Disposable { region.width = parseInt(entry[3]); region.height = parseInt(entry[4]); }; - regionFields["offset"] = () => { // Deprecated, use offsets. + regionFields["offset"] = () => { + // Deprecated, use offsets. region.offsetX = parseInt(entry[1]); region.offsetY = parseInt(entry[2]); }; - regionFields["orig"] = () => { // Deprecated, use offsets. + regionFields["orig"] = () => { + // Deprecated, use offsets. region.originalWidth = parseInt(entry[1]); region.originalHeight = parseInt(entry[2]); }; @@ -171,13 +201,19 @@ export class TextureAtlas implements Disposable { page.name = line.trim(); while (true) { - if (reader.readEntry(entry, line = reader.readLine()) == 0) break; + if ( + reader.readEntry( + entry, + (line = reader.readLine()) + ) == 0 + ) + break; let field: Function = pageFields[entry[0]]; if (field) field(); } this.pages.push(page); - textureLoader(page.name, (texture: BaseTexture) => { + textureLoader(page.name, (texture: PIXI.BaseTexture) => { if (texture === null) { this.pages.splice(this.pages.indexOf(page), 1); return callback && callback(null); @@ -185,7 +221,7 @@ export class TextureAtlas implements Disposable { page.baseTexture = texture; //TODO: set scaleMode and mipmapMode from spine if (page.pma) { - texture.alphaMode = ALPHA_MODES.PMA; + texture.alphaMode = PIXI.ALPHA_MODES.PMA; } if (!texture.valid) { texture.setSize(page.width, page.height); @@ -197,7 +233,11 @@ export class TextureAtlas implements Disposable { page.width = texture.realWidth; page.height = texture.realHeight; if (!page.width || !page.height) { - console.log("ERROR spine atlas page " + page.name + ": meshes wont work if you dont specify size in atlas (http://www.html5gamedevs.com/topic/18888-pixi-spines-and-meshes/?p=107121)"); + console.log( + "ERROR spine atlas page " + + page.name + + ": meshes wont work if you dont specify size in atlas (http://www.html5gamedevs.com/topic/18888-pixi-spines-and-meshes/?p=107121)" + ); } } iterateParser(); @@ -212,15 +252,17 @@ export class TextureAtlas implements Disposable { let names: string[] = null; let values: number[][] = null; while (true) { - let count = reader.readEntry(entry, line = reader.readLine()); + let count = reader.readEntry( + entry, + (line = reader.readLine()) + ); if (count == 0) break; let field: Function = regionFields[entry[0]]; - if (field) - field(); + if (field) field(); else { if (names == null) { names = []; - values = [] + values = []; } names.push(entry[0]); let entryValues: number[] = []; @@ -229,7 +271,10 @@ export class TextureAtlas implements Disposable { values.push(entryValues); } } - if (region.originalWidth == 0 && region.originalHeight == 0) { + if ( + region.originalWidth == 0 && + region.originalHeight == 0 + ) { region.originalWidth = region.width; region.originalHeight = region.height; } @@ -245,12 +290,33 @@ export class TextureAtlas implements Disposable { region.offsetY /= resolution; const swapWH = region.rotate % 4 !== 0; - let frame = new Rectangle(region.x, region.y, swapWH ? region.height : region.width, swapWH ? region.width : region.height); + let frame = new PIXI.Rectangle( + region.x, + region.y, + swapWH ? region.height : region.width, + swapWH ? region.width : region.height + ); - let orig = new Rectangle(0, 0, region.originalWidth, region.originalHeight); - let trim = new Rectangle(region.offsetX, region.originalHeight - region.height - region.offsetY, region.width, region.height); + let orig = new PIXI.Rectangle( + 0, + 0, + region.originalWidth, + region.originalHeight + ); + let trim = new PIXI.Rectangle( + region.offsetX, + region.originalHeight - region.height - region.offsetY, + region.width, + region.height + ); - atlasRegion.texture = new Texture(atlasRegion.page.baseTexture, frame, orig, trim, region.rotate); + atlasRegion.texture = new PIXI.Texture( + atlasRegion.page.baseTexture, + frame, + orig, + trim, + region.rotate + ); atlasRegion.index = region.index; atlasRegion.texture.updateUvs(); @@ -290,21 +356,20 @@ class TextureAtlasReader { } readLine(): string { - if (this.index >= this.lines.length) - return null; + if (this.index >= this.lines.length) return null; return this.lines[this.index++]; } - readEntry (entry: string[], line: string): number { + readEntry(entry: string[], line: string): number { if (line == null) return 0; line = line.trim(); if (line.length == 0) return 0; - let colon = line.indexOf(':'); + let colon = line.indexOf(":"); if (colon == -1) return 0; entry[0] = line.substr(0, colon).trim(); - for (let i = 1, lastMatch = colon + 1;; i++) { - let comma = line.indexOf(',', lastMatch); + for (let i = 1, lastMatch = colon + 1; ; i++) { + let comma = line.indexOf(",", lastMatch); if (comma == -1) { entry[i] = line.substr(lastMatch).trim(); return i; @@ -325,7 +390,7 @@ export class TextureAtlasPage { magFilter: TextureFilter = TextureFilter.Nearest; uWrap: TextureWrap = TextureWrap.ClampToEdge; vWrap: TextureWrap = TextureWrap.ClampToEdge; - baseTexture: BaseTexture; + baseTexture: PIXI.BaseTexture; width: number; height: number; pma: boolean; @@ -334,15 +399,15 @@ export class TextureAtlasPage { let tex = this.baseTexture; let filter = this.minFilter; if (filter == TextureFilter.Linear) { - tex.scaleMode = SCALE_MODES.LINEAR; + tex.scaleMode = PIXI.SCALE_MODES.LINEAR; } else if (this.minFilter == TextureFilter.Nearest) { - tex.scaleMode = SCALE_MODES.NEAREST; + tex.scaleMode = PIXI.SCALE_MODES.NEAREST; } else { - tex.mipmap = MIPMAP_MODES.POW2; + tex.mipmap = PIXI.MIPMAP_MODES.POW2; if (filter == TextureFilter.MipMapNearestNearest) { - tex.scaleMode = SCALE_MODES.NEAREST; + tex.scaleMode = PIXI.SCALE_MODES.NEAREST; } else { - tex.scaleMode = SCALE_MODES.LINEAR; + tex.scaleMode = PIXI.SCALE_MODES.LINEAR; } } } diff --git a/packages/base/src/core/TextureRegion.ts b/packages/base/src/core/TextureRegion.ts index 6d91f282..e6e76154 100644 --- a/packages/base/src/core/TextureRegion.ts +++ b/packages/base/src/core/TextureRegion.ts @@ -1,32 +1,40 @@ - -import { Texture } from '@pixi/core'; -import { Rectangle } from '@pixi/math'; - /** * @public */ -export function filterFromString (text: string): TextureFilter { +export function filterFromString(text: string): TextureFilter { switch (text.toLowerCase()) { - case "nearest": return TextureFilter.Nearest; - case "linear": return TextureFilter.Linear; - case "mipmap": return TextureFilter.MipMap; - case "mipmapnearestnearest": return TextureFilter.MipMapNearestNearest; - case "mipmaplinearnearest": return TextureFilter.MipMapLinearNearest; - case "mipmapnearestlinear": return TextureFilter.MipMapNearestLinear; - case "mipmaplinearlinear": return TextureFilter.MipMapLinearLinear; - default: throw new Error(`Unknown texture filter ${text}`); + case "nearest": + return TextureFilter.Nearest; + case "linear": + return TextureFilter.Linear; + case "mipmap": + return TextureFilter.MipMap; + case "mipmapnearestnearest": + return TextureFilter.MipMapNearestNearest; + case "mipmaplinearnearest": + return TextureFilter.MipMapLinearNearest; + case "mipmapnearestlinear": + return TextureFilter.MipMapNearestLinear; + case "mipmaplinearlinear": + return TextureFilter.MipMapLinearLinear; + default: + throw new Error(`Unknown texture filter ${text}`); } } /** * @public */ -export function wrapFromString (text: string): TextureWrap { +export function wrapFromString(text: string): TextureWrap { switch (text.toLowerCase()) { - case "mirroredtepeat": return TextureWrap.MirroredRepeat; - case "clamptoedge": return TextureWrap.ClampToEdge; - case "repeat": return TextureWrap.Repeat; - default: throw new Error(`Unknown texture wrap ${text}`); + case "mirroredtepeat": + return TextureWrap.MirroredRepeat; + case "clamptoedge": + return TextureWrap.ClampToEdge; + case "repeat": + return TextureWrap.Repeat; + default: + throw new Error(`Unknown texture wrap ${text}`); } } @@ -40,7 +48,7 @@ export enum TextureFilter { MipMapNearestNearest = 9984, // WebGLRenderingContext.NEAREST_MIPMAP_NEAREST MipMapLinearNearest = 9985, // WebGLRenderingContext.LINEAR_MIPMAP_NEAREST MipMapNearestLinear = 9986, // WebGLRenderingContext.NEAREST_MIPMAP_LINEAR - MipMapLinearLinear = 9987 // WebGLRenderingContext.LINEAR_MIPMAP_LINEAR + MipMapLinearLinear = 9987, // WebGLRenderingContext.LINEAR_MIPMAP_LINEAR } /** @@ -49,17 +57,17 @@ export enum TextureFilter { export enum TextureWrap { MirroredRepeat = 33648, // WebGLRenderingContext.MIRRORED_REPEAT ClampToEdge = 33071, // WebGLRenderingContext.CLAMP_TO_EDGE - Repeat = 10497 // WebGLRenderingContext.REPEAT + Repeat = 10497, // WebGLRenderingContext.REPEAT } /** * @public */ export class TextureRegion { - texture: Texture; + texture: PIXI.Texture; //thats for overrides - size: Rectangle = null; + size: PIXI.Rectangle = null; names: string[] = null; values: number[][] = null; diff --git a/packages/loader-3.8/package.json b/packages/loader-3.8/package.json index 99a88375..c58f1f67 100644 --- a/packages/loader-3.8/package.json +++ b/packages/loader-3.8/package.json @@ -8,10 +8,7 @@ "types": "./index.d.ts", "namespace": "PIXI.spine", "peerDependencies": { - "@pixi/app": "^6.1.0", - "@pixi/core": "^6.1.0", - "@pixi/display": "^6.1.0", - "@pixi/loaders": "^6.1.0", + "pixi.js-legacy": "5.3.8", "resource-loader": "~3.0.1" }, "dependencies": { diff --git a/packages/loader-3.8/src/index.ts b/packages/loader-3.8/src/index.ts index 3977b159..5d6d6dee 100644 --- a/packages/loader-3.8/src/index.ts +++ b/packages/loader-3.8/src/index.ts @@ -1,8 +1,12 @@ /// -import {AbstractSpineParser} from '@pixi-spine/loader-base'; -import {ISkeletonParser, TextureAtlas} from '@pixi-spine/base'; -import {LoaderResource, Loader} from "@pixi/loaders"; -import {AtlasAttachmentLoader, SkeletonBinary, SkeletonJson} from "@pixi-spine/runtime-3.8"; + +import { ISkeletonParser, TextureAtlas } from "@pixi-spine/base"; +import { AbstractSpineParser } from "@pixi-spine/loader-base"; +import { + AtlasAttachmentLoader, + SkeletonBinary, + SkeletonJson, +} from "@pixi-spine/runtime-3.8"; /** * @public @@ -16,7 +20,12 @@ export class SpineParser extends AbstractSpineParser { return new SkeletonJson(null); } - parseData(resource: LoaderResource, parser: ISkeletonParser, atlas: TextureAtlas, dataToParse: any): void { + parseData( + resource: PIXI.LoaderResource, + parser: ISkeletonParser, + atlas: TextureAtlas, + dataToParse: any + ): void { const parserCast = parser as SkeletonBinary | SkeletonJson; parserCast.attachmentLoader = new AtlasAttachmentLoader(atlas); @@ -27,6 +36,6 @@ export class SpineParser extends AbstractSpineParser { static use = new SpineParser().genMiddleware().use; static registerLoaderPlugin() { - Loader.registerPlugin(SpineParser); + PIXI.Loader.registerPlugin(SpineParser); } } diff --git a/packages/loader-4.0/package.json b/packages/loader-4.0/package.json index b0e5d15c..d28d43a1 100644 --- a/packages/loader-4.0/package.json +++ b/packages/loader-4.0/package.json @@ -8,10 +8,7 @@ "types": "./index.d.ts", "namespace": "PIXI.spine", "peerDependencies": { - "@pixi/app": "^6.1.0", - "@pixi/core": "^6.1.0", - "@pixi/display": "^6.1.0", - "@pixi/loaders": "^6.1.0", + "pixi.js-legacy": "5.3.8", "resource-loader": "~3.0.1" }, "dependencies": { diff --git a/packages/loader-4.0/src/index.ts b/packages/loader-4.0/src/index.ts index 2df7cc42..8dbfa6d0 100644 --- a/packages/loader-4.0/src/index.ts +++ b/packages/loader-4.0/src/index.ts @@ -1,8 +1,11 @@ /// -import {AbstractSpineParser} from '@pixi-spine/loader-base'; -import {ISkeletonParser, TextureAtlas} from '@pixi-spine/base'; -import {LoaderResource, Loader} from "@pixi/loaders"; -import {AtlasAttachmentLoader, SkeletonBinary, SkeletonJson} from "@pixi-spine/runtime-4.0"; +import { AbstractSpineParser } from "@pixi-spine/loader-base"; +import { ISkeletonParser, TextureAtlas } from "@pixi-spine/base"; +import { + AtlasAttachmentLoader, + SkeletonBinary, + SkeletonJson, +} from "@pixi-spine/runtime-4.0"; /** * @public @@ -16,7 +19,12 @@ export class SpineParser extends AbstractSpineParser { return new SkeletonJson(null); } - parseData(resource: LoaderResource, parser: ISkeletonParser, atlas: TextureAtlas, dataToParse: any): void { + parseData( + resource: PIXI.LoaderResource, + parser: ISkeletonParser, + atlas: TextureAtlas, + dataToParse: any + ): void { const parserCast = parser as SkeletonBinary | SkeletonJson; parserCast.attachmentLoader = new AtlasAttachmentLoader(atlas); @@ -27,6 +35,6 @@ export class SpineParser extends AbstractSpineParser { static use = new SpineParser().genMiddleware().use; static registerLoaderPlugin() { - Loader.registerPlugin(SpineParser); + PIXI.Loader.registerPlugin(SpineParser); } } diff --git a/packages/loader-base/package.json b/packages/loader-base/package.json index 1d52057d..1e419b72 100644 --- a/packages/loader-base/package.json +++ b/packages/loader-base/package.json @@ -8,11 +8,7 @@ "types": "./index.d.ts", "namespace": "PIXI.spine", "peerDependencies": { - "@pixi/app": "^6.1.0", - "@pixi/constants": "^6.1.0", - "@pixi/core": "^6.1.0", - "@pixi/display": "^6.1.0", - "@pixi/loaders": "^6.1.0", + "pixi.js-legacy": "5.3.8", "resource-loader": "~3.0.1" }, "dependencies": { diff --git a/packages/loader-base/src/loaders.ts b/packages/loader-base/src/loaders.ts index 5568acf4..e6777f24 100644 --- a/packages/loader-base/src/loaders.ts +++ b/packages/loader-base/src/loaders.ts @@ -1,17 +1,20 @@ -import {IResourceMetadata, Loader, LoaderResource} from "@pixi/loaders"; -import {BaseTexture, Texture} from "@pixi/core"; -import {ISkeletonParser, TextureAtlas} from "@pixi-spine/base"; -import {ALPHA_MODES} from "@pixi/constants"; +import { ISkeletonParser, TextureAtlas } from "@pixi-spine/base"; -function isJson(resource: LoaderResource) { - return resource.type === LoaderResource.TYPE.JSON; +function isJson(resource: PIXI.LoaderResource) { + return resource.type === PIXI.LoaderResource.TYPE.JSON; } -function isBuffer(resource: LoaderResource) { - return resource.xhrType === (LoaderResource as any).XHR_RESPONSE_TYPE.BUFFER; +function isBuffer(resource: PIXI.LoaderResource) { + return ( + resource.xhrType === + (PIXI.LoaderResource as any).XHR_RESPONSE_TYPE.BUFFER + ); } -LoaderResource.setExtensionXhrType('skel', LoaderResource.XHR_RESPONSE_TYPE.BUFFER); +PIXI.LoaderResource.setExtensionXhrType( + "skel", + PIXI.LoaderResource.XHR_RESPONSE_TYPE.BUFFER +); /** * @public @@ -21,21 +24,34 @@ export abstract class AbstractSpineParser { abstract createBinaryParser(): ISkeletonParser; - abstract parseData(resource: LoaderResource, parser: ISkeletonParser, atlas: TextureAtlas, dataToParse: any): void; + abstract parseData( + resource: PIXI.LoaderResource, + parser: ISkeletonParser, + atlas: TextureAtlas, + dataToParse: any + ): void; genMiddleware() { const self = this; return { - use(this: Loader, resource: LoaderResource, next: () => any) { + use( + this: PIXI.Loader, + resource: PIXI.LoaderResource, + next: () => any + ) { // skip if no data, its not json, or it isn't atlas data if (!resource.data) { return next(); } - const isJsonSpineModel = isJson(resource) && resource.data.bones; - const isBinarySpineModel = isBuffer(resource) && (resource.extension === 'skel' || resource.metadata - && (resource.metadata as any).spineMetadata); + const isJsonSpineModel = + isJson(resource) && resource.data.bones; + const isBinarySpineModel = + isBuffer(resource) && + (resource.extension === "skel" || + (resource.metadata && + (resource.metadata as any).spineMetadata)); if (!isJsonSpineModel && !isBinarySpineModel) { return next(); @@ -53,8 +69,10 @@ export abstract class AbstractSpineParser { } } - const metadata = (resource.metadata || {}) as IResourceMetadata; - const metadataSkeletonScale = metadata ? (metadata as any).spineSkeletonScale : null; + const metadata = resource.metadata || {}; + const metadataSkeletonScale = metadata + ? (metadata as any).spineSkeletonScale + : null; if (metadataSkeletonScale) { parser.scale = metadataSkeletonScale; @@ -65,11 +83,17 @@ export abstract class AbstractSpineParser { return next(); } if (metadataAtlas && metadataAtlas.pages) { - self.parseData(resource, parser, metadataAtlas, dataToParse); + self.parseData( + resource, + parser, + metadataAtlas, + dataToParse + ); return next(); } - const metadataAtlasSuffix = metadata.spineAtlasSuffix || '.atlas'; + const metadataAtlasSuffix = + metadata.spineAtlasSuffix || ".atlas"; /** * use a bit of hackery to load the atlas file, here we assume that the .json, .atlas and .png files @@ -77,125 +101,168 @@ export abstract class AbstractSpineParser { * have the same name */ let atlasPath = resource.url; - let queryStringPos = atlasPath.indexOf('?'); + let queryStringPos = atlasPath.indexOf("?"); if (queryStringPos > 0) { //remove querystring - atlasPath = atlasPath.substr(0, queryStringPos) + atlasPath = atlasPath.substr(0, queryStringPos); } - atlasPath = atlasPath.substr(0, atlasPath.lastIndexOf('.')) + metadataAtlasSuffix; -// use atlas path as a params. (no need to use same atlas file name with json file name) + atlasPath = + atlasPath.substr(0, atlasPath.lastIndexOf(".")) + + metadataAtlasSuffix; + // use atlas path as a params. (no need to use same atlas file name with json file name) if (metadata.spineAtlasFile) { atlasPath = metadata.spineAtlasFile; } -//remove the baseUrl - atlasPath = atlasPath.replace(this.baseUrl, ''); + //remove the baseUrl + atlasPath = atlasPath.replace(this.baseUrl, ""); const atlasOptions = { crossOrigin: resource.crossOrigin, - xhrType: LoaderResource.XHR_RESPONSE_TYPE.TEXT, + xhrType: PIXI.LoaderResource.XHR_RESPONSE_TYPE.TEXT, metadata: metadata.spineMetadata || null, - parentResource: resource + parentResource: resource, }; const imageOptions = { crossOrigin: resource.crossOrigin, metadata: metadata.imageMetadata || null, - parentResource: resource + parentResource: resource, }; - let baseUrl = resource.url.substr(0, resource.url.lastIndexOf('/') + 1); -//remove the baseUrl - baseUrl = baseUrl.replace(this.baseUrl, ''); + let baseUrl = resource.url.substr( + 0, + resource.url.lastIndexOf("/") + 1 + ); + //remove the baseUrl + baseUrl = baseUrl.replace(this.baseUrl, ""); - const namePrefix = metadata.imageNamePrefix || (resource.name + '_atlas_page_'); + const namePrefix = + metadata.imageNamePrefix || resource.name + "_atlas_page_"; - const adapter = metadata.images ? staticImageLoader(metadata.images) - : metadata.image ? staticImageLoader({'default': metadata.image}) - : metadata.imageLoader ? metadata.imageLoader(this, namePrefix, baseUrl, imageOptions) - : imageLoaderAdapter(this, namePrefix, baseUrl, imageOptions); + const adapter = metadata.images + ? staticImageLoader(metadata.images) + : metadata.image + ? staticImageLoader({ default: metadata.image }) + : metadata.imageLoader + ? metadata.imageLoader( + this, + namePrefix, + baseUrl, + imageOptions + ) + : imageLoaderAdapter( + this, + namePrefix, + baseUrl, + imageOptions + ); function createSkeletonWithRawAtlas(rawData: string) { - new TextureAtlas(rawData, adapter, function(spineAtlas) { + new TextureAtlas(rawData, adapter, function (spineAtlas) { if (spineAtlas) { - self.parseData(resource, parser, spineAtlas, dataToParse); + self.parseData( + resource, + parser, + spineAtlas, + dataToParse + ); } next(); }); } if (metadata.atlasRawData) { - createSkeletonWithRawAtlas(metadata.atlasRawData) + createSkeletonWithRawAtlas(metadata.atlasRawData); } else { - this.add(resource.name + '_atlas', atlasPath, atlasOptions, function (atlasResource: any) { - if (!atlasResource.error) { - createSkeletonWithRawAtlas(atlasResource.data); - } else { - next(); + this.add( + resource.name + "_atlas", + atlasPath, + atlasOptions, + function (atlasResource: any) { + if (!atlasResource.error) { + createSkeletonWithRawAtlas(atlasResource.data); + } else { + next(); + } } - }); + ); } - } - } + }, + }; } } /** * @public */ -export function imageLoaderAdapter(loader: any, namePrefix: any, baseUrl: any, imageOptions: any) { - if (baseUrl && baseUrl.lastIndexOf('/') !== (baseUrl.length - 1)) { - baseUrl += '/'; +export function imageLoaderAdapter( + loader: any, + namePrefix: any, + baseUrl: any, + imageOptions: any +) { + if (baseUrl && baseUrl.lastIndexOf("/") !== baseUrl.length - 1) { + baseUrl += "/"; } - return function (line: string, callback: (baseTexture: BaseTexture) => any) { + return function ( + line: string, + callback: (baseTexture: PIXI.BaseTexture) => any + ) { const name = namePrefix + line; const url = baseUrl + line; const cachedResource = loader.resources[name]; if (cachedResource) { const done = () => { - callback(cachedResource.texture.baseTexture) - } + callback(cachedResource.texture.baseTexture); + }; if (cachedResource.texture) { done(); } else { cachedResource.onAfterMiddleware.add(done); } } else { - loader.add(name, url, imageOptions, (resource: LoaderResource) => { - if (!resource.error) { - if (line.indexOf('-pma.') >= 0) { - resource.texture.baseTexture.alphaMode = ALPHA_MODES.PMA; - } + loader.add( + name, + url, + imageOptions, + (resource: PIXI.LoaderResource) => { + if (!resource.error) { + if (line.indexOf("-pma.") >= 0) { + resource.texture.baseTexture.alphaMode = + PIXI.ALPHA_MODES.PMA; + } - callback(resource.texture.baseTexture); - } else { - callback(null); + callback(resource.texture.baseTexture); + } else { + callback(null); + } } - }); + ); } - } + }; } /** * @public */ export function syncImageLoaderAdapter(baseUrl: any, crossOrigin: any) { - if (baseUrl && baseUrl.lastIndexOf('/') !== (baseUrl.length - 1)) { - baseUrl += '/'; + if (baseUrl && baseUrl.lastIndexOf("/") !== baseUrl.length - 1) { + baseUrl += "/"; } return function (line: any, callback: any) { - callback(BaseTexture.from(line, crossOrigin)); - } + callback(PIXI.BaseTexture.from(line, crossOrigin)); + }; } /** * @public */ -export function staticImageLoader(pages: { [key: string]: (BaseTexture | Texture) }) { +export function staticImageLoader(pages: { + [key: string]: PIXI.BaseTexture | PIXI.Texture; +}) { return function (line: any, callback: any) { - let page = pages[line] || pages['default'] as any; - if (page && page.baseTexture) - callback(page.baseTexture); - else - callback(page); - } + let page = pages[line] || (pages["default"] as any); + if (page && page.baseTexture) callback(page.baseTexture); + else callback(page); + }; } diff --git a/packages/loader-uni/package.json b/packages/loader-uni/package.json index 65df5684..f7467efc 100644 --- a/packages/loader-uni/package.json +++ b/packages/loader-uni/package.json @@ -7,7 +7,7 @@ "types": "./index.d.ts", "namespace": "PIXI.spine", "peerDependencies": { - "@pixi/loaders": "^6.1.0" + "pixi.js-legacy": "5.3.8" }, "dependencies": { "@pixi-spine/base": "~3.0.15", diff --git a/packages/loader-uni/src/SpineLoader.ts b/packages/loader-uni/src/SpineLoader.ts index 9ae7d1af..91b0f2d2 100644 --- a/packages/loader-uni/src/SpineLoader.ts +++ b/packages/loader-uni/src/SpineLoader.ts @@ -1,15 +1,22 @@ -import {AbstractSpineParser} from '@pixi-spine/loader-base'; -import {BinaryInput, ISkeletonData, ISkeletonParser, TextureAtlas} from "@pixi-spine/base"; -import {LoaderResource, Loader} from "@pixi/loaders"; +import { AbstractSpineParser } from "@pixi-spine/loader-base"; +import { + BinaryInput, + ISkeletonData, + ISkeletonParser, + TextureAtlas, +} from "@pixi-spine/base"; import * as spine38 from "@pixi-spine/runtime-3.8"; import * as spine37 from "@pixi-spine/runtime-3.7"; import * as spine40 from "@pixi-spine/runtime-4.0"; -import {detectSpineVersion, SPINE_VERSION} from "./versions"; +import { detectSpineVersion, SPINE_VERSION } from "./versions"; class UniBinaryParser implements ISkeletonParser { scale = 1; - readSkeletonData(atlas: TextureAtlas, dataToParse: Uint8Array): ISkeletonData { + readSkeletonData( + atlas: TextureAtlas, + dataToParse: Uint8Array + ): ISkeletonData { let input = new BinaryInput(dataToParse); input.readString(); let version = input.readString(); @@ -17,7 +24,9 @@ class UniBinaryParser implements ISkeletonParser { let parser: any = null; if (ver === SPINE_VERSION.VER38) { - parser = new spine38.SkeletonBinary(new spine38.AtlasAttachmentLoader(atlas)); + parser = new spine38.SkeletonBinary( + new spine38.AtlasAttachmentLoader(atlas) + ); } input = new BinaryInput(dataToParse); @@ -26,7 +35,9 @@ class UniBinaryParser implements ISkeletonParser { version = input.readString(); ver = detectSpineVersion(version); if (ver === SPINE_VERSION.VER40) { - parser = new spine40.SkeletonBinary(new spine40.AtlasAttachmentLoader(atlas)); + parser = new spine40.SkeletonBinary( + new spine40.AtlasAttachmentLoader(atlas) + ); } if (!parser) { let error = `Unsupported version of spine model ${version}, please update pixi-spine`; @@ -47,13 +58,19 @@ class UniJsonParser implements ISkeletonParser { let parser: any = null; if (ver === SPINE_VERSION.VER37) { - parser = new spine37.SkeletonJson(new spine37.AtlasAttachmentLoader(atlas)); + parser = new spine37.SkeletonJson( + new spine37.AtlasAttachmentLoader(atlas) + ); } if (ver === SPINE_VERSION.VER38) { - parser = new spine38.SkeletonJson(new spine38.AtlasAttachmentLoader(atlas)); + parser = new spine38.SkeletonJson( + new spine38.AtlasAttachmentLoader(atlas) + ); } if (ver === SPINE_VERSION.VER40) { - parser = new spine40.SkeletonJson(new spine40.AtlasAttachmentLoader(atlas)); + parser = new spine40.SkeletonJson( + new spine40.AtlasAttachmentLoader(atlas) + ); } if (!parser) { let error = `Unsupported version of spine model ${version}, please update pixi-spine`; @@ -77,8 +94,13 @@ export class SpineParser extends AbstractSpineParser { return new UniJsonParser(); } - parseData(resource: LoaderResource, parser: ISkeletonParser, atlas: TextureAtlas, dataToParse: any): void { - const parserCast = parser as (UniBinaryParser | UniJsonParser); + parseData( + resource: PIXI.LoaderResource, + parser: ISkeletonParser, + atlas: TextureAtlas, + dataToParse: any + ): void { + const parserCast = parser as UniBinaryParser | UniJsonParser; resource.spineData = parserCast.readSkeletonData(atlas, dataToParse); resource.spineAtlas = atlas; } @@ -86,6 +108,6 @@ export class SpineParser extends AbstractSpineParser { static use = new SpineParser().genMiddleware().use; static registerLoaderPlugin() { - Loader.registerPlugin(SpineParser); + PIXI.Loader.registerPlugin(SpineParser); } } diff --git a/packages/runtime-3.7/package.json b/packages/runtime-3.7/package.json index 03880079..7623314e 100644 --- a/packages/runtime-3.7/package.json +++ b/packages/runtime-3.7/package.json @@ -8,8 +8,7 @@ "namespace": "PIXI.spine37", "types": "./index.d.ts", "peerDependencies": { - "@pixi/constants": "^6.1.0", - "@pixi/math": "^6.1.0" + "pixi.js-legacy": "5.3.8" }, "dependencies": { "@pixi-spine/base": "~3.0.15" diff --git a/packages/runtime-3.7/src/core/Bone.ts b/packages/runtime-3.7/src/core/Bone.ts index a1f74eeb..dc918fef 100644 --- a/packages/runtime-3.7/src/core/Bone.ts +++ b/packages/runtime-3.7/src/core/Bone.ts @@ -1,15 +1,20 @@ -import {Matrix} from '@pixi/math'; -import {Updatable} from "./Updatable"; -import {BoneData} from "./BoneData"; -import {Skeleton} from "./Skeleton"; -import {IBone, MathUtils, settings, TransformMode, Vector2} from "@pixi-spine/base"; +import { Updatable } from "./Updatable"; +import { BoneData } from "./BoneData"; +import { Skeleton } from "./Skeleton"; +import { + IBone, + MathUtils, + settings, + TransformMode, + Vector2, +} from "@pixi-spine/base"; /** * @public */ export class Bone implements Updatable, IBone { //be careful! Spine b,c is c,b in pixi matrix - matrix = new Matrix(); + matrix = new PIXI.Matrix(); get worldX(): number { return this.matrix.tx; @@ -53,16 +58,40 @@ export class Bone implements Updatable, IBone { /** Same as {@link #updateWorldTransform()}. This method exists for Bone to implement {@link Updatable}. */ update() { - this.updateWorldTransformWith(this.x, this.y, this.rotation, this.scaleX, this.scaleY, this.shearX, this.shearY); + this.updateWorldTransformWith( + this.x, + this.y, + this.rotation, + this.scaleX, + this.scaleY, + this.shearX, + this.shearY + ); } /** Computes the world transform using the parent bone and this bone's local transform. */ updateWorldTransform() { - this.updateWorldTransformWith(this.x, this.y, this.rotation, this.scaleX, this.scaleY, this.shearX, this.shearY); + this.updateWorldTransformWith( + this.x, + this.y, + this.rotation, + this.scaleX, + this.scaleY, + this.shearX, + this.shearY + ); } /** Computes the world transform using the parent bone and the specified local transform. */ - updateWorldTransformWith(x: number, y: number, rotation: number, scaleX: number, scaleY: number, shearX: number, shearY: number) { + updateWorldTransformWith( + x: number, + y: number, + rotation: number, + scaleX: number, + scaleY: number, + shearX: number, + shearY: number + ) { this.ax = x; this.ay = y; this.arotation = rotation; @@ -76,9 +105,10 @@ export class Bone implements Updatable, IBone { let m = this.matrix; let sx = this.skeleton.scaleX; - let sy = settings.yDown? -this.skeleton.scaleY : this.skeleton.scaleY; + let sy = settings.yDown ? -this.skeleton.scaleY : this.skeleton.scaleY; - if (parent == null) { // Root bone. + if (parent == null) { + // Root bone. let skeleton = this.skeleton; let rotationY = rotation + 90 + shearY; m.a = MathUtils.cosDeg(rotation + shearX) * scaleX * sx; @@ -90,7 +120,10 @@ export class Bone implements Updatable, IBone { return; } - let pa = parent.matrix.a, pb = parent.matrix.c, pc = parent.matrix.b, pd = parent.matrix.d; + let pa = parent.matrix.a, + pb = parent.matrix.c, + pc = parent.matrix.b, + pd = parent.matrix.d; m.tx = pa * x + pb * y + parent.matrix.tx; m.ty = pc * x + pd * y + parent.matrix.ty; switch (this.data.transformMode) { @@ -151,11 +184,15 @@ export class Bone implements Updatable, IBone { zc *= s; s = Math.sqrt(za * za + zc * zc); if ( - this.data.transformMode == TransformMode.NoScale - && (pa * pd - pb * pc < 0) != (settings.yDown? - (this.skeleton.scaleX < 0 != this.skeleton.scaleY > 0) : - (this.skeleton.scaleX < 0 != this.skeleton.scaleY < 0)) - ) s = -s; + this.data.transformMode == TransformMode.NoScale && + pa * pd - pb * pc < 0 != + (settings.yDown + ? this.skeleton.scaleX < 0 != + this.skeleton.scaleY > 0 + : this.skeleton.scaleX < 0 != + this.skeleton.scaleY < 0) + ) + s = -s; let r = Math.PI / 2 + Math.atan2(zc, za); let zb = Math.cos(r) * s; let zd = Math.sin(r) * s; @@ -220,14 +257,17 @@ export class Bone implements Updatable, IBone { this.ascaleX = Math.sqrt(m.a * m.a + m.b * m.b); this.ascaleY = Math.sqrt(m.c * m.c + m.d * m.d); this.ashearX = 0; - this.ashearY = Math.atan2(m.a * m.c + m.b * m.d, m.a * m.d - m.b * m.c) * MathUtils.radDeg; + this.ashearY = + Math.atan2(m.a * m.c + m.b * m.d, m.a * m.d - m.b * m.c) * + MathUtils.radDeg; return; } let pm = parent.matrix; let pid = 1 / (pm.a * pm.d - pm.b * pm.c); - let dx = m.tx - pm.tx, dy = m.ty - pm.ty; - this.ax = (dx * pm.d * pid - dy * pm.c * pid); - this.ay = (dy * pm.a * pid - dx * pm.b * pid); + let dx = m.tx - pm.tx, + dy = m.ty - pm.ty; + this.ax = dx * pm.d * pid - dy * pm.c * pid; + this.ay = dy * pm.a * pid - dx * pm.b * pid; let ia = pid * pm.d; let id = pid * pm.a; let ib = pid * pm.c; @@ -241,7 +281,8 @@ export class Bone implements Updatable, IBone { if (this.ascaleX > 0.0001) { let det = ra * rd - rb * rc; this.ascaleY = det / this.ascaleX; - this.ashearY = Math.atan2(ra * rb + rc * rd, det) * MathUtils.radDeg; + this.ashearY = + Math.atan2(ra * rb + rc * rd, det) * MathUtils.radDeg; this.arotation = Math.atan2(rc, ra) * MathUtils.radDeg; } else { this.ascaleX = 0; @@ -253,38 +294,55 @@ export class Bone implements Updatable, IBone { worldToLocal(world: Vector2) { let m = this.matrix; - let a = m.a, b = m.c, c = m.b, d = m.d; + let a = m.a, + b = m.c, + c = m.b, + d = m.d; let invDet = 1 / (a * d - b * c); - let x = world.x - m.tx, y = world.y - m.ty; - world.x = (x * d * invDet - y * b * invDet); - world.y = (y * a * invDet - x * c * invDet); + let x = world.x - m.tx, + y = world.y - m.ty; + world.x = x * d * invDet - y * b * invDet; + world.y = y * a * invDet - x * c * invDet; return world; } localToWorld(local: Vector2) { let m = this.matrix; - let x = local.x, y = local.y; + let x = local.x, + y = local.y; local.x = x * m.a + y * m.c + m.tx; local.y = x * m.b + y * m.d + m.ty; return local; } - worldToLocalRotation (worldRotation: number) { - let sin = MathUtils.sinDeg(worldRotation), cos = MathUtils.cosDeg(worldRotation); + worldToLocalRotation(worldRotation: number) { + let sin = MathUtils.sinDeg(worldRotation), + cos = MathUtils.cosDeg(worldRotation); let mat = this.matrix; - return Math.atan2(mat.a * sin - mat.b * cos, mat.d * cos - mat.c * sin) * MathUtils.radDeg; + return ( + Math.atan2(mat.a * sin - mat.b * cos, mat.d * cos - mat.c * sin) * + MathUtils.radDeg + ); } - localToWorldRotation (localRotation: number) { - let sin = MathUtils.sinDeg(localRotation), cos = MathUtils.cosDeg(localRotation); + localToWorldRotation(localRotation: number) { + let sin = MathUtils.sinDeg(localRotation), + cos = MathUtils.cosDeg(localRotation); let mat = this.matrix; - return Math.atan2(cos * mat.b + sin * mat.d, cos * mat.a + sin * mat.c) * MathUtils.radDeg; + return ( + Math.atan2(cos * mat.b + sin * mat.d, cos * mat.a + sin * mat.c) * + MathUtils.radDeg + ); } - rotateWorld (degrees: number) { + rotateWorld(degrees: number) { let mat = this.matrix; - let a = mat.a, b = mat.c, c = mat.b, d = mat.d; - let cos = MathUtils.cosDeg(degrees), sin = MathUtils.sinDeg(degrees); + let a = mat.a, + b = mat.c, + c = mat.b, + d = mat.d; + let cos = MathUtils.cosDeg(degrees), + sin = MathUtils.sinDeg(degrees); mat.a = cos * a - sin * c; mat.c = cos * b - sin * d; mat.b = sin * a + cos * c; diff --git a/packages/runtime-3.7/src/core/SkeletonJson.ts b/packages/runtime-3.7/src/core/SkeletonJson.ts index 9e298d4e..925325a8 100644 --- a/packages/runtime-3.7/src/core/SkeletonJson.ts +++ b/packages/runtime-3.7/src/core/SkeletonJson.ts @@ -1,17 +1,26 @@ -import type {Attachment, AttachmentLoader, MeshAttachment, VertexAttachment} from './attachments'; -import {Animation} from './Animation'; -import {Event} from './Event'; -import {SkeletonData} from './SkeletonData'; -import {SlotData} from './SlotData'; -import {BoneData} from './BoneData'; -import {IkConstraintData} from './IkConstraintData'; -import {TransformConstraintData} from './TransformConstraintData'; -import {PathConstraintData, SpacingMode} from './PathConstraintData'; -import {Skin} from './Skin'; -import {EventData} from './EventData'; +import type { + Attachment, + AttachmentLoader, + MeshAttachment, + VertexAttachment, +} from "./attachments"; +import { Animation } from "./Animation"; +import { Event } from "./Event"; +import { SkeletonData } from "./SkeletonData"; +import { SlotData } from "./SlotData"; +import { BoneData } from "./BoneData"; +import { IkConstraintData } from "./IkConstraintData"; +import { TransformConstraintData } from "./TransformConstraintData"; +import { PathConstraintData, SpacingMode } from "./PathConstraintData"; +import { Skin } from "./Skin"; +import { EventData } from "./EventData"; import { AttachmentTimeline, - ColorTimeline, CurveTimeline, DeformTimeline, DrawOrderTimeline, EventTimeline, + ColorTimeline, + CurveTimeline, + DeformTimeline, + DrawOrderTimeline, + EventTimeline, IkConstraintTimeline, PathConstraintMixTimeline, PathConstraintPositionTimeline, @@ -22,10 +31,17 @@ import { Timeline, TransformConstraintTimeline, TranslateTimeline, - TwoColorTimeline -} from './Animation'; -import {ArrayLike, Color, Utils, PositionMode, RotateMode, TransformMode, settings} from '@pixi-spine/base'; -import {BLEND_MODES} from '@pixi/constants'; + TwoColorTimeline, +} from "./Animation"; +import { + ArrayLike, + Color, + Utils, + PositionMode, + RotateMode, + TransformMode, + settings, +} from "@pixi-spine/base"; /** * @public @@ -35,14 +51,14 @@ export class SkeletonJson { scale = 1; private linkedMeshes = new Array(); - constructor (attachmentLoader: AttachmentLoader) { + constructor(attachmentLoader: AttachmentLoader) { this.attachmentLoader = attachmentLoader; } - readSkeletonData (json: string | any): SkeletonData { + readSkeletonData(json: string | any): SkeletonData { let scale = this.scale; let skeletonData = new SkeletonData(); - let root = typeof(json) === "string" ? JSON.parse(json) : json; + let root = typeof json === "string" ? JSON.parse(json) : json; // Skeleton let skeletonMap = root.skeleton; @@ -64,9 +80,14 @@ export class SkeletonJson { let parentName: string = this.getValue(boneMap, "parent", null); if (parentName != null) { parent = skeletonData.findBone(parentName); - if (parent == null) throw new Error("Parent bone not found: " + parentName); + if (parent == null) + throw new Error("Parent bone not found: " + parentName); } - let data = new BoneData(skeletonData.bones.length, boneMap.name, parent); + let data = new BoneData( + skeletonData.bones.length, + boneMap.name, + parent + ); data.length = this.getValue(boneMap, "length", 0) * scale; data.x = this.getValue(boneMap, "x", 0) * scale; data.y = this.getValue(boneMap, "y", 0) * scale; @@ -75,7 +96,9 @@ export class SkeletonJson { data.scaleY = this.getValue(boneMap, "scaleY", 1); data.shearX = this.getValue(boneMap, "shearX", 0); data.shearY = this.getValue(boneMap, "shearY", 0); - data.transformMode = SkeletonJson.transformModeFromString(this.getValue(boneMap, "transform", "normal")); + data.transformMode = SkeletonJson.transformModeFromString( + this.getValue(boneMap, "transform", "normal") + ); skeletonData.bones.push(data); } @@ -88,8 +111,13 @@ export class SkeletonJson { let slotName: string = slotMap.name; let boneName: string = slotMap.bone; let boneData = skeletonData.findBone(boneName); - if (boneData == null) throw new Error("Slot bone not found: " + boneName); - let data = new SlotData(skeletonData.slots.length, slotName, boneData); + if (boneData == null) + throw new Error("Slot bone not found: " + boneName); + let data = new SlotData( + skeletonData.slots.length, + slotName, + boneData + ); let color: string = this.getValue(slotMap, "color", null); if (color != null) data.color.setFromString(color); @@ -100,8 +128,14 @@ export class SkeletonJson { data.darkColor.setFromString(dark); } - data.attachmentName = this.getValue(slotMap, "attachment", null); - data.blendMode = SkeletonJson.blendModeFromString(this.getValue(slotMap, "blend", "normal")); + data.attachmentName = this.getValue( + slotMap, + "attachment", + null + ); + data.blendMode = SkeletonJson.blendModeFromString( + this.getValue(slotMap, "blend", "normal") + ); skeletonData.slots.push(data); } } @@ -116,15 +150,23 @@ export class SkeletonJson { for (let j = 0; j < constraintMap.bones.length; j++) { let boneName = constraintMap.bones[j]; let bone = skeletonData.findBone(boneName); - if (bone == null) throw new Error("IK bone not found: " + boneName); + if (bone == null) + throw new Error("IK bone not found: " + boneName); data.bones.push(bone); } let targetName: string = constraintMap.target; data.target = skeletonData.findBone(targetName); - if (data.target == null) throw new Error("IK target bone not found: " + targetName); - - data.bendDirection = this.getValue(constraintMap, "bendPositive", true) ? 1 : -1; + if (data.target == null) + throw new Error("IK target bone not found: " + targetName); + + data.bendDirection = this.getValue( + constraintMap, + "bendPositive", + true + ) + ? 1 + : -1; data.mix = this.getValue(constraintMap, "mix", 1); skeletonData.ikConstraints.push(data); @@ -141,17 +183,28 @@ export class SkeletonJson { for (let j = 0; j < constraintMap.bones.length; j++) { let boneName = constraintMap.bones[j]; let bone = skeletonData.findBone(boneName); - if (bone == null) throw new Error("Transform constraint bone not found: " + boneName); + if (bone == null) + throw new Error( + "Transform constraint bone not found: " + boneName + ); data.bones.push(bone); } let targetName: string = constraintMap.target; data.target = skeletonData.findBone(targetName); - if (data.target == null) throw new Error("Transform constraint target bone not found: " + targetName); + if (data.target == null) + throw new Error( + "Transform constraint target bone not found: " + + targetName + ); data.local = this.getValue(constraintMap, "local", false); data.relative = this.getValue(constraintMap, "relative", false); - data.offsetRotation = this.getValue(constraintMap, "rotation", 0); + data.offsetRotation = this.getValue( + constraintMap, + "rotation", + 0 + ); data.offsetX = this.getValue(constraintMap, "x", 0) * scale; data.offsetY = this.getValue(constraintMap, "y", 0) * scale; data.offsetScaleX = this.getValue(constraintMap, "scaleX", 0); @@ -159,7 +212,11 @@ export class SkeletonJson { data.offsetShearY = this.getValue(constraintMap, "shearY", 0); data.rotateMix = this.getValue(constraintMap, "rotateMix", 1); - data.translateMix = this.getValue(constraintMap, "translateMix", 1); + data.translateMix = this.getValue( + constraintMap, + "translateMix", + 1 + ); data.scaleMix = this.getValue(constraintMap, "scaleMix", 1); data.shearMix = this.getValue(constraintMap, "shearMix", 1); @@ -177,24 +234,49 @@ export class SkeletonJson { for (let j = 0; j < constraintMap.bones.length; j++) { let boneName = constraintMap.bones[j]; let bone = skeletonData.findBone(boneName); - if (bone == null) throw new Error("Transform constraint bone not found: " + boneName); + if (bone == null) + throw new Error( + "Transform constraint bone not found: " + boneName + ); data.bones.push(bone); } let targetName: string = constraintMap.target; data.target = skeletonData.findSlot(targetName); - if (data.target == null) throw new Error("Path target slot not found: " + targetName); - - data.positionMode = SkeletonJson.positionModeFromString(this.getValue(constraintMap, "positionMode", "percent")); - data.spacingMode = SkeletonJson.spacingModeFromString(this.getValue(constraintMap, "spacingMode", "length")); - data.rotateMode = SkeletonJson.rotateModeFromString(this.getValue(constraintMap, "rotateMode", "tangent")); - data.offsetRotation = this.getValue(constraintMap, "rotation", 0); + if (data.target == null) + throw new Error( + "Path target slot not found: " + targetName + ); + + data.positionMode = SkeletonJson.positionModeFromString( + this.getValue(constraintMap, "positionMode", "percent") + ); + data.spacingMode = SkeletonJson.spacingModeFromString( + this.getValue(constraintMap, "spacingMode", "length") + ); + data.rotateMode = SkeletonJson.rotateModeFromString( + this.getValue(constraintMap, "rotateMode", "tangent") + ); + data.offsetRotation = this.getValue( + constraintMap, + "rotation", + 0 + ); data.position = this.getValue(constraintMap, "position", 0); - if (data.positionMode == PositionMode.Fixed) data.position *= scale; + if (data.positionMode == PositionMode.Fixed) + data.position *= scale; data.spacing = this.getValue(constraintMap, "spacing", 0); - if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) data.spacing *= scale; + if ( + data.spacingMode == SpacingMode.Length || + data.spacingMode == SpacingMode.Fixed + ) + data.spacing *= scale; data.rotateMix = this.getValue(constraintMap, "rotateMix", 1); - data.translateMix = this.getValue(constraintMap, "translateMix", 1); + data.translateMix = this.getValue( + constraintMap, + "translateMix", + 1 + ); skeletonData.pathConstraints.push(data); } @@ -203,15 +285,27 @@ export class SkeletonJson { // Skins. if (root.skins) { for (let skinName in root.skins) { - let skinMap = root.skins[skinName] + let skinMap = root.skins[skinName]; let skin = new Skin(skinName); for (let slotName in skinMap) { let slotIndex = skeletonData.findSlotIndex(slotName); - if (slotIndex == -1) throw new Error("Slot not found: " + slotName); + if (slotIndex == -1) + throw new Error("Slot not found: " + slotName); let slotMap = skinMap[slotName]; for (let entryName in slotMap) { - let attachment = this.readAttachment(slotMap[entryName], skin, slotIndex, entryName, skeletonData); - if (attachment != null) skin.addAttachment(slotIndex, entryName, attachment); + let attachment = this.readAttachment( + slotMap[entryName], + skin, + slotIndex, + entryName, + skeletonData + ); + if (attachment != null) + skin.addAttachment( + slotIndex, + entryName, + attachment + ); } } skeletonData.skins.push(skin); @@ -222,11 +316,19 @@ export class SkeletonJson { // Linked meshes. for (let i = 0, n = this.linkedMeshes.length; i < n; i++) { let linkedMesh = this.linkedMeshes[i]; - let skin = linkedMesh.skin == null ? skeletonData.defaultSkin : skeletonData.findSkin(linkedMesh.skin); - if (skin == null) throw new Error("Skin not found: " + linkedMesh.skin); - let parent = skin.getAttachment(linkedMesh.slotIndex, linkedMesh.parent); - if (parent == null) throw new Error("Parent mesh not found: " + linkedMesh.parent); - linkedMesh.mesh.setParentMesh( parent); + let skin = + linkedMesh.skin == null + ? skeletonData.defaultSkin + : skeletonData.findSkin(linkedMesh.skin); + if (skin == null) + throw new Error("Skin not found: " + linkedMesh.skin); + let parent = skin.getAttachment( + linkedMesh.slotIndex, + linkedMesh.parent + ); + if (parent == null) + throw new Error("Parent mesh not found: " + linkedMesh.parent); + linkedMesh.mesh.setParentMesh(parent); //linkedMesh.mesh.updateUVs(); } this.linkedMeshes.length = 0; @@ -259,7 +361,13 @@ export class SkeletonJson { return skeletonData; } - readAttachment (map: any, skin: Skin, slotIndex: number, name: string, skeletonData: SkeletonData): Attachment { + readAttachment( + map: any, + skin: Skin, + slotIndex: number, + name: string, + skeletonData: SkeletonData + ): Attachment { let scale = this.scale; name = this.getValue(map, "name", name); @@ -268,7 +376,11 @@ export class SkeletonJson { switch (type) { case "region": { let path = this.getValue(map, "path", name); - let region = this.attachmentLoader.newRegionAttachment(skin, name, path); + let region = this.attachmentLoader.newRegionAttachment( + skin, + name, + path + ); if (region == null) return null; region.path = path; region.x = this.getValue(map, "x", 0) * scale; @@ -286,7 +398,10 @@ export class SkeletonJson { return region; } case "boundingbox": { - let box = this.attachmentLoader.newBoundingBoxAttachment(skin, name); + let box = this.attachmentLoader.newBoundingBoxAttachment( + skin, + name + ); if (box == null) return null; this.readVertices(map, box, map.vertexCount << 1); let color: string = this.getValue(map, "color", null); @@ -296,7 +411,11 @@ export class SkeletonJson { case "mesh": case "linkedmesh": { let path = this.getValue(map, "path", name); - let mesh = this.attachmentLoader.newMeshAttachment(skin, name, path); + let mesh = this.attachmentLoader.newMeshAttachment( + skin, + name, + path + ); if (mesh == null) return null; mesh.path = path; @@ -306,7 +425,14 @@ export class SkeletonJson { let parent: string = this.getValue(map, "parent", null); if (parent != null) { mesh.inheritDeform = this.getValue(map, "deform", true); - this.linkedMeshes.push(new LinkedMesh(mesh, this.getValue(map, "skin", null), slotIndex, parent)); + this.linkedMeshes.push( + new LinkedMesh( + mesh, + this.getValue(map, "skin", null), + slotIndex, + parent + ) + ); return mesh; } @@ -338,7 +464,10 @@ export class SkeletonJson { return path; } case "point": { - let point = this.attachmentLoader.newPointAttachment(skin, name); + let point = this.attachmentLoader.newPointAttachment( + skin, + name + ); if (point == null) return null; point.x = this.getValue(map, "x", 0) * scale; point.y = this.getValue(map, "y", 0) * scale; @@ -349,13 +478,17 @@ export class SkeletonJson { return point; } case "clipping": { - let clip = this.attachmentLoader.newClippingAttachment(skin, name); + let clip = this.attachmentLoader.newClippingAttachment( + skin, + name + ); if (clip == null) return null; let end = this.getValue(map, "end", null); if (end != null) { let slot = skeletonData.findSlot(end); - if (slot == null) throw new Error("Clipping end slot not found: " + end); + if (slot == null) + throw new Error("Clipping end slot not found: " + end); clip.endSlot = slot; } @@ -370,7 +503,11 @@ export class SkeletonJson { return null; } - readVertices (map: any, attachment: VertexAttachment, verticesLength: number) { + readVertices( + map: any, + attachment: VertexAttachment, + verticesLength: number + ) { let scale = this.scale; attachment.worldVerticesLength = verticesLength; let vertices: Array = map.vertices; @@ -385,7 +522,7 @@ export class SkeletonJson { } let weights = new Array(); let bones = new Array(); - for (let i = 0, n = vertices.length; i < n;) { + for (let i = 0, n = vertices.length; i < n; ) { let boneCount = vertices[i++]; bones.push(boneCount); for (let nn = i + boneCount * 4; i < nn; i += 4) { @@ -399,7 +536,7 @@ export class SkeletonJson { attachment.vertices = Utils.toFloatArray(weights); } - readAnimation (map: any, name: string, skeletonData: SkeletonData) { + readAnimation(map: any, name: string, skeletonData: SkeletonData) { let scale = this.scale; let timelines = new Array(); let duration = 0; @@ -409,20 +546,30 @@ export class SkeletonJson { for (let slotName in map.slots) { let slotMap = map.slots[slotName]; let slotIndex = skeletonData.findSlotIndex(slotName); - if (slotIndex == -1) throw new Error("Slot not found: " + slotName); + if (slotIndex == -1) + throw new Error("Slot not found: " + slotName); for (let timelineName in slotMap) { let timelineMap = slotMap[timelineName]; if (timelineName == "attachment") { - let timeline = new AttachmentTimeline(timelineMap.length); + let timeline = new AttachmentTimeline( + timelineMap.length + ); timeline.slotIndex = slotIndex; let frameIndex = 0; for (let i = 0; i < timelineMap.length; i++) { let valueMap = timelineMap[i]; - timeline.setFrame(frameIndex++, valueMap.time, valueMap.name); + timeline.setFrame( + frameIndex++, + valueMap.time, + valueMap.name + ); } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[timeline.getFrameCount() - 1]); + duration = Math.max( + duration, + timeline.frames[timeline.getFrameCount() - 1] + ); } else if (timelineName == "color") { let timeline = new ColorTimeline(timelineMap.length); timeline.slotIndex = slotIndex; @@ -432,13 +579,25 @@ export class SkeletonJson { let valueMap = timelineMap[i]; let color = new Color(); color.setFromString(valueMap.color || "ffffffff"); - timeline.setFrame(frameIndex, valueMap.time, color.r, color.g, color.b, color.a); + timeline.setFrame( + frameIndex, + valueMap.time, + color.r, + color.g, + color.b, + color.a + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(timeline.getFrameCount() - 1) * ColorTimeline.ENTRIES]); - + duration = Math.max( + duration, + timeline.frames[ + (timeline.getFrameCount() - 1) * + ColorTimeline.ENTRIES + ] + ); } else if (timelineName == "twoColor") { let timeline = new TwoColorTimeline(timelineMap.length); timeline.slotIndex = slotIndex; @@ -450,15 +609,36 @@ export class SkeletonJson { let dark = new Color(); light.setFromString(valueMap.light); dark.setFromString(valueMap.dark); - timeline.setFrame(frameIndex, valueMap.time, light.r, light.g, light.b, light.a, dark.r, dark.g, dark.b); + timeline.setFrame( + frameIndex, + valueMap.time, + light.r, + light.g, + light.b, + light.a, + dark.r, + dark.g, + dark.b + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(timeline.getFrameCount() - 1) * TwoColorTimeline.ENTRIES]); - + duration = Math.max( + duration, + timeline.frames[ + (timeline.getFrameCount() - 1) * + TwoColorTimeline.ENTRIES + ] + ); } else - throw new Error("Invalid timeline type for a slot: " + timelineName + " (" + slotName + ")"); + throw new Error( + "Invalid timeline type for a slot: " + + timelineName + + " (" + + slotName + + ")" + ); } } } @@ -468,7 +648,8 @@ export class SkeletonJson { for (let boneName in map.bones) { let boneMap = map.bones[boneName]; let boneIndex = skeletonData.findBoneIndex(boneName); - if (boneIndex == -1) throw new Error("Bone not found: " + boneName); + if (boneIndex == -1) + throw new Error("Bone not found: " + boneName); for (let timelineName in boneMap) { let timelineMap = boneMap[timelineName]; if (timelineName === "rotate") { @@ -478,14 +659,27 @@ export class SkeletonJson { let frameIndex = 0; for (let i = 0; i < timelineMap.length; i++) { let valueMap = timelineMap[i]; - timeline.setFrame(frameIndex, valueMap.time, valueMap.angle); + timeline.setFrame( + frameIndex, + valueMap.time, + valueMap.angle + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(timeline.getFrameCount() - 1) * RotateTimeline.ENTRIES]); - - } else if (timelineName === "translate" || timelineName === "scale" || timelineName === "shear") { + duration = Math.max( + duration, + timeline.frames[ + (timeline.getFrameCount() - 1) * + RotateTimeline.ENTRIES + ] + ); + } else if ( + timelineName === "translate" || + timelineName === "scale" || + timelineName === "shear" + ) { let timeline: TranslateTimeline = null; let timelineScale = 1; if (timelineName === "scale") @@ -493,7 +687,9 @@ export class SkeletonJson { else if (timelineName === "shear") timeline = new ShearTimeline(timelineMap.length); else { - timeline = new TranslateTimeline(timelineMap.length); + timeline = new TranslateTimeline( + timelineMap.length + ); timelineScale = scale; } timeline.boneIndex = boneIndex; @@ -501,16 +697,33 @@ export class SkeletonJson { let frameIndex = 0; for (let i = 0; i < timelineMap.length; i++) { let valueMap = timelineMap[i]; - let x = this.getValue(valueMap, "x", 0), y = this.getValue(valueMap, "y", 0); - timeline.setFrame(frameIndex, valueMap.time, x * timelineScale, y * timelineScale); + let x = this.getValue(valueMap, "x", 0), + y = this.getValue(valueMap, "y", 0); + timeline.setFrame( + frameIndex, + valueMap.time, + x * timelineScale, + y * timelineScale + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(timeline.getFrameCount() - 1) * TranslateTimeline.ENTRIES]); - + duration = Math.max( + duration, + timeline.frames[ + (timeline.getFrameCount() - 1) * + TranslateTimeline.ENTRIES + ] + ); } else - throw new Error("Invalid timeline type for a bone: " + timelineName + " (" + boneName + ")"); + throw new Error( + "Invalid timeline type for a bone: " + + timelineName + + " (" + + boneName + + ")" + ); } } } @@ -521,17 +734,30 @@ export class SkeletonJson { let constraintMap = map.ik[constraintName]; let constraint = skeletonData.findIkConstraint(constraintName); let timeline = new IkConstraintTimeline(constraintMap.length); - timeline.ikConstraintIndex = skeletonData.ikConstraints.indexOf(constraint); + timeline.ikConstraintIndex = + skeletonData.ikConstraints.indexOf(constraint); let frameIndex = 0; for (let i = 0; i < constraintMap.length; i++) { let valueMap = constraintMap[i]; - timeline.setFrame(frameIndex, valueMap.time, this.getValue(valueMap, "mix", 1), - this.getValue(valueMap, "bendPositive", true) ? 1 : -1, this.getValue(valueMap, "compress", false), this.getValue(valueMap, "stretch", false)); + timeline.setFrame( + frameIndex, + valueMap.time, + this.getValue(valueMap, "mix", 1), + this.getValue(valueMap, "bendPositive", true) ? 1 : -1, + this.getValue(valueMap, "compress", false), + this.getValue(valueMap, "stretch", false) + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(timeline.getFrameCount() - 1) * IkConstraintTimeline.ENTRIES]); + duration = Math.max( + duration, + timeline.frames[ + (timeline.getFrameCount() - 1) * + IkConstraintTimeline.ENTRIES + ] + ); } } @@ -539,20 +765,35 @@ export class SkeletonJson { if (map.transform) { for (let constraintName in map.transform) { let constraintMap = map.transform[constraintName]; - let constraint = skeletonData.findTransformConstraint(constraintName); - let timeline = new TransformConstraintTimeline(constraintMap.length); - timeline.transformConstraintIndex = skeletonData.transformConstraints.indexOf(constraint); + let constraint = + skeletonData.findTransformConstraint(constraintName); + let timeline = new TransformConstraintTimeline( + constraintMap.length + ); + timeline.transformConstraintIndex = + skeletonData.transformConstraints.indexOf(constraint); let frameIndex = 0; for (let i = 0; i < constraintMap.length; i++) { let valueMap = constraintMap[i]; - timeline.setFrame(frameIndex, valueMap.time, this.getValue(valueMap, "rotateMix", 1), - this.getValue(valueMap, "translateMix", 1), this.getValue(valueMap, "scaleMix", 1), this.getValue(valueMap, "shearMix", 1)); + timeline.setFrame( + frameIndex, + valueMap.time, + this.getValue(valueMap, "rotateMix", 1), + this.getValue(valueMap, "translateMix", 1), + this.getValue(valueMap, "scaleMix", 1), + this.getValue(valueMap, "shearMix", 1) + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, - timeline.frames[(timeline.getFrameCount() - 1) * TransformConstraintTimeline.ENTRIES]); + duration = Math.max( + duration, + timeline.frames[ + (timeline.getFrameCount() - 1) * + TransformConstraintTimeline.ENTRIES + ] + ); } } @@ -560,46 +801,83 @@ export class SkeletonJson { if (map.paths) { for (let constraintName in map.paths) { let constraintMap = map.paths[constraintName]; - let index = skeletonData.findPathConstraintIndex(constraintName); - if (index == -1) throw new Error("Path constraint not found: " + constraintName); + let index = + skeletonData.findPathConstraintIndex(constraintName); + if (index == -1) + throw new Error( + "Path constraint not found: " + constraintName + ); let data = skeletonData.pathConstraints[index]; for (let timelineName in constraintMap) { let timelineMap = constraintMap[timelineName]; - if (timelineName === "position" || timelineName === "spacing") { + if ( + timelineName === "position" || + timelineName === "spacing" + ) { let timeline: PathConstraintPositionTimeline = null; let timelineScale = 1; if (timelineName === "spacing") { - timeline = new PathConstraintSpacingTimeline(timelineMap.length); - if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) timelineScale = scale; + timeline = new PathConstraintSpacingTimeline( + timelineMap.length + ); + if ( + data.spacingMode == SpacingMode.Length || + data.spacingMode == SpacingMode.Fixed + ) + timelineScale = scale; } else { - timeline = new PathConstraintPositionTimeline(timelineMap.length); - if (data.positionMode == PositionMode.Fixed) timelineScale = scale; + timeline = new PathConstraintPositionTimeline( + timelineMap.length + ); + if (data.positionMode == PositionMode.Fixed) + timelineScale = scale; } timeline.pathConstraintIndex = index; let frameIndex = 0; for (let i = 0; i < timelineMap.length; i++) { let valueMap = timelineMap[i]; - timeline.setFrame(frameIndex, valueMap.time, this.getValue(valueMap, timelineName, 0) * timelineScale); + timeline.setFrame( + frameIndex, + valueMap.time, + this.getValue(valueMap, timelineName, 0) * + timelineScale + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, - timeline.frames[(timeline.getFrameCount() - 1) * PathConstraintPositionTimeline.ENTRIES]); + duration = Math.max( + duration, + timeline.frames[ + (timeline.getFrameCount() - 1) * + PathConstraintPositionTimeline.ENTRIES + ] + ); } else if (timelineName === "mix") { - let timeline = new PathConstraintMixTimeline(timelineMap.length); + let timeline = new PathConstraintMixTimeline( + timelineMap.length + ); timeline.pathConstraintIndex = index; let frameIndex = 0; for (let i = 0; i < timelineMap.length; i++) { let valueMap = timelineMap[i]; - timeline.setFrame(frameIndex, valueMap.time, this.getValue(valueMap, "rotateMix", 1), - this.getValue(valueMap, "translateMix", 1)); + timeline.setFrame( + frameIndex, + valueMap.time, + this.getValue(valueMap, "rotateMix", 1), + this.getValue(valueMap, "translateMix", 1) + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, - timeline.frames[(timeline.getFrameCount() - 1) * PathConstraintMixTimeline.ENTRIES]); + duration = Math.max( + duration, + timeline.frames[ + (timeline.getFrameCount() - 1) * + PathConstraintMixTimeline.ENTRIES + ] + ); } } } @@ -616,17 +894,27 @@ export class SkeletonJson { } else { continue; } - } for (let slotName in deformMap) { + } + for (let slotName in deformMap) { let slotMap = deformMap[slotName]; let slotIndex = skeletonData.findSlotIndex(slotName); - if (slotIndex == -1) throw new Error("Slot not found: " + slotMap.name); + if (slotIndex == -1) + throw new Error("Slot not found: " + slotMap.name); for (let timelineName in slotMap) { let timelineMap = slotMap[timelineName]; - let attachment = skin.getAttachment(slotIndex, timelineName); - if (attachment == null) throw new Error("Deform attachment not found: " + timelineMap.name); + let attachment = ( + skin.getAttachment(slotIndex, timelineName) + ); + if (attachment == null) + throw new Error( + "Deform attachment not found: " + + timelineMap.name + ); let weighted = attachment.bones != null; let vertices = attachment.vertices; - let deformLength = weighted ? vertices.length / 3 * 2 : vertices.length; + let deformLength = weighted + ? (vertices.length / 3) * 2 + : vertices.length; let timeline = new DeformTimeline(timelineMap.length); timeline.slotIndex = slotIndex; @@ -636,15 +924,34 @@ export class SkeletonJson { for (let j = 0; j < timelineMap.length; j++) { let valueMap = timelineMap[j]; let deform: ArrayLike; - let verticesValue: Array = this.getValue(valueMap, "vertices", null); + let verticesValue: Array = this.getValue( + valueMap, + "vertices", + null + ); if (verticesValue == null) - deform = weighted ? Utils.newFloatArray(deformLength) : vertices; + deform = weighted + ? Utils.newFloatArray(deformLength) + : vertices; else { deform = Utils.newFloatArray(deformLength); - let start = this.getValue(valueMap, "offset", 0); - Utils.arrayCopy(verticesValue, 0, deform, start, verticesValue.length); + let start = ( + this.getValue(valueMap, "offset", 0) + ); + Utils.arrayCopy( + verticesValue, + 0, + deform, + start, + verticesValue.length + ); if (scale != 1) { - for (let i = start, n = i + verticesValue.length; i < n; i++) + for ( + let i = start, + n = i + verticesValue.length; + i < n; + i++ + ) deform[i] *= scale; } if (!weighted) { @@ -653,12 +960,19 @@ export class SkeletonJson { } } - timeline.setFrame(frameIndex, valueMap.time, deform); + timeline.setFrame( + frameIndex, + valueMap.time, + deform + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[timeline.getFrameCount() - 1]); + duration = Math.max( + duration, + timeline.frames[timeline.getFrameCount() - 1] + ); } } } @@ -677,29 +991,43 @@ export class SkeletonJson { let offsets = this.getValue(drawOrderMap, "offsets", null); if (offsets != null) { drawOrder = Utils.newArray(slotCount, -1); - let unchanged = Utils.newArray(slotCount - offsets.length, 0); - let originalIndex = 0, unchangedIndex = 0; + let unchanged = Utils.newArray( + slotCount - offsets.length, + 0 + ); + let originalIndex = 0, + unchangedIndex = 0; for (let i = 0; i < offsets.length; i++) { let offsetMap = offsets[i]; - let slotIndex = skeletonData.findSlotIndex(offsetMap.slot); - if (slotIndex == -1) throw new Error("Slot not found: " + offsetMap.slot); + let slotIndex = skeletonData.findSlotIndex( + offsetMap.slot + ); + if (slotIndex == -1) + throw new Error( + "Slot not found: " + offsetMap.slot + ); // Collect unchanged items. while (originalIndex != slotIndex) unchanged[unchangedIndex++] = originalIndex++; // Set changed items. - drawOrder[originalIndex + offsetMap.offset] = originalIndex++; + drawOrder[originalIndex + offsetMap.offset] = + originalIndex++; } // Collect remaining unchanged items. while (originalIndex < slotCount) unchanged[unchangedIndex++] = originalIndex++; // Fill in unchanged items. for (let i = slotCount - 1; i >= 0; i--) - if (drawOrder[i] == -1) drawOrder[i] = unchanged[--unchangedIndex]; + if (drawOrder[i] == -1) + drawOrder[i] = unchanged[--unchangedIndex]; } timeline.setFrame(frameIndex++, drawOrderMap.time, drawOrder); } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[timeline.getFrameCount() - 1]); + duration = Math.max( + duration, + timeline.frames[timeline.getFrameCount() - 1] + ); } // Event timeline. @@ -709,11 +1037,27 @@ export class SkeletonJson { for (let i = 0; i < map.events.length; i++) { let eventMap = map.events[i]; let eventData = skeletonData.findEvent(eventMap.name); - if (eventData == null) throw new Error("Event not found: " + eventMap.name); - let event = new Event(Utils.toSinglePrecision(eventMap.time), eventData); - event.intValue = this.getValue(eventMap, "int", eventData.intValue); - event.floatValue = this.getValue(eventMap, "float", eventData.floatValue); - event.stringValue = this.getValue(eventMap, "string", eventData.stringValue); + if (eventData == null) + throw new Error("Event not found: " + eventMap.name); + let event = new Event( + Utils.toSinglePrecision(eventMap.time), + eventData + ); + event.intValue = this.getValue( + eventMap, + "int", + eventData.intValue + ); + event.floatValue = this.getValue( + eventMap, + "float", + eventData.floatValue + ); + event.stringValue = this.getValue( + eventMap, + "string", + eventData.stringValue + ); if (event.data.audioPath != null) { event.volume = this.getValue(eventMap, "volume", 1); event.balance = this.getValue(eventMap, "balance", 0); @@ -721,7 +1065,10 @@ export class SkeletonJson { timeline.setFrame(frameIndex++, event); } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[timeline.getFrameCount() - 1]); + duration = Math.max( + duration, + timeline.frames[timeline.getFrameCount() - 1] + ); } if (isNaN(duration)) { @@ -731,37 +1078,44 @@ export class SkeletonJson { skeletonData.animations.push(new Animation(name, timelines, duration)); } - readCurve (map: any, timeline: CurveTimeline, frameIndex: number) { + readCurve(map: any, timeline: CurveTimeline, frameIndex: number) { if (!map.curve) return; - if (map.curve === "stepped") - timeline.setStepped(frameIndex); - else if (Object.prototype.toString.call(map.curve) === '[object Array]') { + if (map.curve === "stepped") timeline.setStepped(frameIndex); + else if ( + Object.prototype.toString.call(map.curve) === "[object Array]" + ) { let curve: Array = map.curve; - timeline.setCurve(frameIndex, curve[0], curve[1], curve[2], curve[3]); + timeline.setCurve( + frameIndex, + curve[0], + curve[1], + curve[2], + curve[3] + ); } } - getValue (map: any, prop: string, defaultValue: any) { + getValue(map: any, prop: string, defaultValue: any) { return map[prop] !== undefined ? map[prop] : defaultValue; } - static blendModeFromString (str: string) { + static blendModeFromString(str: string) { str = str.toLowerCase(); - if (str == "normal") return BLEND_MODES.NORMAL; - if (str == "additive") return BLEND_MODES.ADD; - if (str == "multiply") return BLEND_MODES.MULTIPLY; - if (str == "screen") return BLEND_MODES.SCREEN; + if (str == "normal") return PIXI.BLEND_MODES.NORMAL; + if (str == "additive") return PIXI.BLEND_MODES.ADD; + if (str == "multiply") return PIXI.BLEND_MODES.MULTIPLY; + if (str == "screen") return PIXI.BLEND_MODES.SCREEN; throw new Error(`Unknown blend mode: ${str}`); } - static positionModeFromString (str: string) { + static positionModeFromString(str: string) { str = str.toLowerCase(); if (str == "fixed") return PositionMode.Fixed; if (str == "percent") return PositionMode.Percent; throw new Error(`Unknown position mode: ${str}`); } - static spacingModeFromString (str: string) { + static spacingModeFromString(str: string) { str = str.toLowerCase(); if (str == "length") return SpacingMode.Length; if (str == "fixed") return SpacingMode.Fixed; @@ -769,7 +1123,7 @@ export class SkeletonJson { throw new Error(`Unknown position mode: ${str}`); } - static rotateModeFromString (str: string) { + static rotateModeFromString(str: string) { str = str.toLowerCase(); if (str == "tangent") return RotateMode.Tangent; if (str == "chain") return RotateMode.Chain; @@ -781,19 +1135,27 @@ export class SkeletonJson { str = str.toLowerCase(); if (str == "normal") return TransformMode.Normal; if (str == "onlytranslation") return TransformMode.OnlyTranslation; - if (str == "norotationorreflection") return TransformMode.NoRotationOrReflection; + if (str == "norotationorreflection") + return TransformMode.NoRotationOrReflection; if (str == "noscale") return TransformMode.NoScale; - if (str == "noscaleorreflection") return TransformMode.NoScaleOrReflection; + if (str == "noscaleorreflection") + return TransformMode.NoScaleOrReflection; throw new Error(`Unknown transform mode: ${str}`); } } class LinkedMesh { - parent: string; skin: string; + parent: string; + skin: string; slotIndex: number; mesh: MeshAttachment; - constructor (mesh: MeshAttachment, skin: string, slotIndex: number, parent: string) { + constructor( + mesh: MeshAttachment, + skin: string, + slotIndex: number, + parent: string + ) { this.mesh = mesh; this.skin = skin; this.slotIndex = slotIndex; diff --git a/packages/runtime-3.7/src/core/SlotData.ts b/packages/runtime-3.7/src/core/SlotData.ts index 9a40db86..355f4fb3 100644 --- a/packages/runtime-3.7/src/core/SlotData.ts +++ b/packages/runtime-3.7/src/core/SlotData.ts @@ -1,8 +1,7 @@ -import {Color} from '@pixi-spine/base'; +import { Color } from "@pixi-spine/base"; -import type {ISlotData} from '@pixi-spine/base'; -import type {BLEND_MODES} from '@pixi/constants'; -import {BoneData} from "./BoneData"; +import type { ISlotData } from "@pixi-spine/base"; +import { BoneData } from "./BoneData"; /** * @public @@ -14,9 +13,9 @@ export class SlotData implements ISlotData { color = new Color(1, 1, 1, 1); darkColor: Color; attachmentName: string; - blendMode: BLEND_MODES; + blendMode: PIXI.BLEND_MODES; - constructor (index: number, name: string, boneData: BoneData) { + constructor(index: number, name: string, boneData: BoneData) { if (index < 0) throw new Error("index must be >= 0."); if (name == null) throw new Error("name cannot be null."); if (boneData == null) throw new Error("boneData cannot be null."); diff --git a/packages/runtime-3.8/package.json b/packages/runtime-3.8/package.json index d54b78b3..5ac046c1 100644 --- a/packages/runtime-3.8/package.json +++ b/packages/runtime-3.8/package.json @@ -1,51 +1,50 @@ { - "name": "@pixi-spine/runtime-3.8", - "version": "3.0.15", - "description": "Pixi runtime for spine 3.8 models", - "main": "lib/runtime-3.8.js", - "module": "lib/runtime-3.8.es.js", - "bundle": "dist/runtime-3.8.js", - "namespace": "PIXI.spine38", - "types": "./index.d.ts", - "peerDependencies": { - "@pixi/constants": "^6.1.0", - "@pixi/math": "^6.1.0" - }, - "dependencies": { - "@pixi-spine/base": "~3.0.15" - }, - "scripts": { - "build": "rollup -c rollup.config.js --silent", - "build:types": "rimraf compile && tsc -p tsconfig-api.json && api-extractor run" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/pixijs/pixi-spine.git" - }, - "files": [ - "lib", - "dist", - "index.d.ts" - ], - "keywords": [ - "pixi.js", - "pixi", - "spine" - ], - "author": "Ivan Igorevich Popelyshev ", - "bugs": { - "url": "https://github.com/pixijs/pixi-spine/issues" - }, - "license": "SEE SPINE-LICENSE", - "homepage": "https://github.com/pixijs/pixi-spine/#readme", - "devDependencies": { - "@pixi-spine/eslint-config": "~1.0.0", - "@pixi-spine/rollup-config": "~1.0.0", - "chai": "~4.2.0", - "eslint": "~7.13.0", - "rimraf": "3.0.2", - "rollup": "^2.53.3", - "tslib": "~2.2.0", - "typescript": "~4.3.0" - } + "name": "@pixi-spine/runtime-3.8", + "version": "3.0.15", + "description": "Pixi runtime for spine 3.8 models", + "main": "lib/runtime-3.8.js", + "module": "lib/runtime-3.8.es.js", + "bundle": "dist/runtime-3.8.js", + "namespace": "PIXI.spine38", + "types": "./index.d.ts", + "peerDependencies": { + "pixi.js-legacy": "5.3.8" + }, + "dependencies": { + "@pixi-spine/base": "~3.0.15" + }, + "scripts": { + "build": "rollup -c rollup.config.js --silent", + "build:types": "rimraf compile && tsc -p tsconfig-api.json && api-extractor run" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/pixijs/pixi-spine.git" + }, + "files": [ + "lib", + "dist", + "index.d.ts" + ], + "keywords": [ + "pixi.js", + "pixi", + "spine" + ], + "author": "Ivan Igorevich Popelyshev ", + "bugs": { + "url": "https://github.com/pixijs/pixi-spine/issues" + }, + "license": "SEE SPINE-LICENSE", + "homepage": "https://github.com/pixijs/pixi-spine/#readme", + "devDependencies": { + "@pixi-spine/eslint-config": "~1.0.0", + "@pixi-spine/rollup-config": "~1.0.0", + "chai": "~4.2.0", + "eslint": "~7.13.0", + "rimraf": "3.0.2", + "rollup": "^2.53.3", + "tslib": "~2.2.0", + "typescript": "~4.3.0" + } } diff --git a/packages/runtime-3.8/src/Spine.ts b/packages/runtime-3.8/src/Spine.ts index 6faecd39..5031d885 100644 --- a/packages/runtime-3.8/src/Spine.ts +++ b/packages/runtime-3.8/src/Spine.ts @@ -1,13 +1,18 @@ -import {SpineBase} from '@pixi-spine/base'; -import {Skeleton} from "./core/Skeleton"; -import {SkeletonData} from "./core/SkeletonData"; -import {AnimationState} from "./core/AnimationState"; -import {AnimationStateData} from "./core/AnimationStateData"; +import { SpineBase } from "@pixi-spine/base"; +import { Skeleton } from "./core/Skeleton"; +import { SkeletonData } from "./core/SkeletonData"; +import { AnimationState } from "./core/AnimationState"; +import { AnimationStateData } from "./core/AnimationStateData"; /** * @public */ -export class Spine extends SpineBase { +export class Spine extends SpineBase< + Skeleton, + SkeletonData, + AnimationState, + AnimationStateData +> { createSkeleton(spineData: SkeletonData) { this.skeleton = new Skeleton(spineData); this.skeleton.updateWorldTransform(); diff --git a/packages/runtime-3.8/src/core/Bone.ts b/packages/runtime-3.8/src/core/Bone.ts index cf23f0e7..27a962f9 100644 --- a/packages/runtime-3.8/src/core/Bone.ts +++ b/packages/runtime-3.8/src/core/Bone.ts @@ -1,15 +1,20 @@ -import {Matrix} from '@pixi/math'; -import {Updatable} from "./Updatable"; -import {BoneData} from "./BoneData"; -import {Skeleton} from "./Skeleton"; -import {IBone, MathUtils, settings, TransformMode, Vector2} from "@pixi-spine/base"; +import { Updatable } from "./Updatable"; +import { BoneData } from "./BoneData"; +import { Skeleton } from "./Skeleton"; +import { + IBone, + MathUtils, + settings, + TransformMode, + Vector2, +} from "@pixi-spine/base"; /** * @public */ export class Bone implements Updatable, IBone { //be careful! Spine b,c is c,b in pixi matrix - matrix = new Matrix(); + matrix = new PIXI.Matrix(); get worldX(): number { return this.matrix.tx; @@ -52,22 +57,46 @@ export class Bone implements Updatable, IBone { this.setToSetupPose(); } - isActive () { + isActive() { return this.active; } /** Same as {@link #updateWorldTransform()}. This method exists for Bone to implement {@link Updatable}. */ update() { - this.updateWorldTransformWith(this.x, this.y, this.rotation, this.scaleX, this.scaleY, this.shearX, this.shearY); + this.updateWorldTransformWith( + this.x, + this.y, + this.rotation, + this.scaleX, + this.scaleY, + this.shearX, + this.shearY + ); } /** Computes the world transform using the parent bone and this bone's local transform. */ updateWorldTransform() { - this.updateWorldTransformWith(this.x, this.y, this.rotation, this.scaleX, this.scaleY, this.shearX, this.shearY); + this.updateWorldTransformWith( + this.x, + this.y, + this.rotation, + this.scaleX, + this.scaleY, + this.shearX, + this.shearY + ); } /** Computes the world transform using the parent bone and the specified local transform. */ - updateWorldTransformWith(x: number, y: number, rotation: number, scaleX: number, scaleY: number, shearX: number, shearY: number) { + updateWorldTransformWith( + x: number, + y: number, + rotation: number, + scaleX: number, + scaleY: number, + shearX: number, + shearY: number + ) { this.ax = x; this.ay = y; this.arotation = rotation; @@ -81,9 +110,10 @@ export class Bone implements Updatable, IBone { let m = this.matrix; let sx = this.skeleton.scaleX; - let sy = settings.yDown? -this.skeleton.scaleY : this.skeleton.scaleY; + let sy = settings.yDown ? -this.skeleton.scaleY : this.skeleton.scaleY; - if (parent == null) { // Root bone. + if (parent == null) { + // Root bone. let skeleton = this.skeleton; let rotationY = rotation + 90 + shearY; m.a = MathUtils.cosDeg(rotation + shearX) * scaleX * sx; @@ -95,7 +125,10 @@ export class Bone implements Updatable, IBone { return; } - let pa = parent.matrix.a, pb = parent.matrix.c, pc = parent.matrix.b, pd = parent.matrix.d; + let pa = parent.matrix.a, + pb = parent.matrix.c, + pc = parent.matrix.b, + pd = parent.matrix.d; m.tx = pa * x + pb * y + parent.matrix.tx; m.ty = pc * x + pd * y + parent.matrix.ty; switch (this.data.transformMode) { @@ -158,11 +191,15 @@ export class Bone implements Updatable, IBone { zc *= s; s = Math.sqrt(za * za + zc * zc); if ( - this.data.transformMode == TransformMode.NoScale - && (pa * pd - pb * pc < 0) != (settings.yDown? - (this.skeleton.scaleX < 0 != this.skeleton.scaleY > 0) : - (this.skeleton.scaleX < 0 != this.skeleton.scaleY < 0)) - ) s = -s; + this.data.transformMode == TransformMode.NoScale && + pa * pd - pb * pc < 0 != + (settings.yDown + ? this.skeleton.scaleX < 0 != + this.skeleton.scaleY > 0 + : this.skeleton.scaleX < 0 != + this.skeleton.scaleY < 0) + ) + s = -s; let r = Math.PI / 2 + Math.atan2(zc, za); let zb = Math.cos(r) * s; let zd = Math.sin(r) * s; @@ -227,14 +264,17 @@ export class Bone implements Updatable, IBone { this.ascaleX = Math.sqrt(m.a * m.a + m.b * m.b); this.ascaleY = Math.sqrt(m.c * m.c + m.d * m.d); this.ashearX = 0; - this.ashearY = Math.atan2(m.a * m.c + m.b * m.d, m.a * m.d - m.b * m.c) * MathUtils.radDeg; + this.ashearY = + Math.atan2(m.a * m.c + m.b * m.d, m.a * m.d - m.b * m.c) * + MathUtils.radDeg; return; } let pm = parent.matrix; let pid = 1 / (pm.a * pm.d - pm.b * pm.c); - let dx = m.tx - pm.tx, dy = m.ty - pm.ty; - this.ax = (dx * pm.d * pid - dy * pm.c * pid); - this.ay = (dy * pm.a * pid - dx * pm.b * pid); + let dx = m.tx - pm.tx, + dy = m.ty - pm.ty; + this.ax = dx * pm.d * pid - dy * pm.c * pid; + this.ay = dy * pm.a * pid - dx * pm.b * pid; let ia = pid * pm.d; let id = pid * pm.a; let ib = pid * pm.c; @@ -248,7 +288,8 @@ export class Bone implements Updatable, IBone { if (this.ascaleX > 0.0001) { let det = ra * rd - rb * rc; this.ascaleY = det / this.ascaleX; - this.ashearY = Math.atan2(ra * rb + rc * rd, det) * MathUtils.radDeg; + this.ashearY = + Math.atan2(ra * rb + rc * rd, det) * MathUtils.radDeg; this.arotation = Math.atan2(rc, ra) * MathUtils.radDeg; } else { this.ascaleX = 0; @@ -260,38 +301,55 @@ export class Bone implements Updatable, IBone { worldToLocal(world: Vector2) { let m = this.matrix; - let a = m.a, b = m.c, c = m.b, d = m.d; + let a = m.a, + b = m.c, + c = m.b, + d = m.d; let invDet = 1 / (a * d - b * c); - let x = world.x - m.tx, y = world.y - m.ty; - world.x = (x * d * invDet - y * b * invDet); - world.y = (y * a * invDet - x * c * invDet); + let x = world.x - m.tx, + y = world.y - m.ty; + world.x = x * d * invDet - y * b * invDet; + world.y = y * a * invDet - x * c * invDet; return world; } localToWorld(local: Vector2) { let m = this.matrix; - let x = local.x, y = local.y; + let x = local.x, + y = local.y; local.x = x * m.a + y * m.c + m.tx; local.y = x * m.b + y * m.d + m.ty; return local; } - worldToLocalRotation (worldRotation: number) { - let sin = MathUtils.sinDeg(worldRotation), cos = MathUtils.cosDeg(worldRotation); + worldToLocalRotation(worldRotation: number) { + let sin = MathUtils.sinDeg(worldRotation), + cos = MathUtils.cosDeg(worldRotation); let mat = this.matrix; - return Math.atan2(mat.a * sin - mat.b * cos, mat.d * cos - mat.c * sin) * MathUtils.radDeg; + return ( + Math.atan2(mat.a * sin - mat.b * cos, mat.d * cos - mat.c * sin) * + MathUtils.radDeg + ); } - localToWorldRotation (localRotation: number) { - let sin = MathUtils.sinDeg(localRotation), cos = MathUtils.cosDeg(localRotation); + localToWorldRotation(localRotation: number) { + let sin = MathUtils.sinDeg(localRotation), + cos = MathUtils.cosDeg(localRotation); let mat = this.matrix; - return Math.atan2(cos * mat.b + sin * mat.d, cos * mat.a + sin * mat.c) * MathUtils.radDeg; + return ( + Math.atan2(cos * mat.b + sin * mat.d, cos * mat.a + sin * mat.c) * + MathUtils.radDeg + ); } - rotateWorld (degrees: number) { + rotateWorld(degrees: number) { let mat = this.matrix; - let a = mat.a, b = mat.c, c = mat.b, d = mat.d; - let cos = MathUtils.cosDeg(degrees), sin = MathUtils.sinDeg(degrees); + let a = mat.a, + b = mat.c, + c = mat.b, + d = mat.d; + let cos = MathUtils.cosDeg(degrees), + sin = MathUtils.sinDeg(degrees); mat.a = cos * a - sin * c; mat.c = cos * b - sin * d; mat.b = sin * a + cos * c; diff --git a/packages/runtime-3.8/src/core/SkeletonBinary.ts b/packages/runtime-3.8/src/core/SkeletonBinary.ts index ff36923d..2289e264 100644 --- a/packages/runtime-3.8/src/core/SkeletonBinary.ts +++ b/packages/runtime-3.8/src/core/SkeletonBinary.ts @@ -1,17 +1,26 @@ -import type {Attachment, AttachmentLoader, MeshAttachment, VertexAttachment} from './attachments'; -import {Animation} from './Animation'; -import {Event} from './Event'; -import {SkeletonData} from './SkeletonData'; -import {SlotData} from './SlotData'; -import {BoneData} from './BoneData'; -import {IkConstraintData} from './IkConstraintData'; -import {TransformConstraintData} from './TransformConstraintData'; -import {PathConstraintData, SpacingMode} from './PathConstraintData'; -import {Skin} from './Skin'; -import {EventData} from './EventData'; +import type { + Attachment, + AttachmentLoader, + MeshAttachment, + VertexAttachment, +} from "./attachments"; +import { Animation } from "./Animation"; +import { Event } from "./Event"; +import { SkeletonData } from "./SkeletonData"; +import { SlotData } from "./SlotData"; +import { BoneData } from "./BoneData"; +import { IkConstraintData } from "./IkConstraintData"; +import { TransformConstraintData } from "./TransformConstraintData"; +import { PathConstraintData, SpacingMode } from "./PathConstraintData"; +import { Skin } from "./Skin"; +import { EventData } from "./EventData"; import { AttachmentTimeline, - ColorTimeline, CurveTimeline, DeformTimeline, DrawOrderTimeline, EventTimeline, + ColorTimeline, + CurveTimeline, + DeformTimeline, + DrawOrderTimeline, + EventTimeline, IkConstraintTimeline, PathConstraintMixTimeline, PathConstraintPositionTimeline, @@ -22,21 +31,52 @@ import { Timeline, TransformConstraintTimeline, TranslateTimeline, - TwoColorTimeline -} from './Animation'; -import {AttachmentType, BinaryInput, Color, PositionMode, RotateMode, TransformMode, Utils} from '@pixi-spine/base'; -import {BLEND_MODES} from '@pixi/constants'; + TwoColorTimeline, +} from "./Animation"; +import { + AttachmentType, + BinaryInput, + Color, + PositionMode, + RotateMode, + TransformMode, + Utils, +} from "@pixi-spine/base"; /** * @public */ export class SkeletonBinary { - static AttachmentTypeValues = [ 0 /*AttachmentType.Region*/, 1/*AttachmentType.BoundingBox*/, 2/*AttachmentType.Mesh*/, 3/*AttachmentType.LinkedMesh*/, 4/*AttachmentType.Path*/, 5/*AttachmentType.Point*/, 6/*AttachmentType.Clipping*/ ]; - static TransformModeValues = [TransformMode.Normal, TransformMode.OnlyTranslation, TransformMode.NoRotationOrReflection, TransformMode.NoScale, TransformMode.NoScaleOrReflection]; - static PositionModeValues = [ PositionMode.Fixed, PositionMode.Percent ]; - static SpacingModeValues = [ SpacingMode.Length, SpacingMode.Fixed, SpacingMode.Percent]; - static RotateModeValues = [ RotateMode.Tangent, RotateMode.Chain, RotateMode.ChainScale ]; - static BlendModeValues = [ BLEND_MODES.NORMAL, BLEND_MODES.ADD, BLEND_MODES.MULTIPLY, BLEND_MODES.SCREEN]; + static AttachmentTypeValues = [ + 0 /*AttachmentType.Region*/, 1 /*AttachmentType.BoundingBox*/, + 2 /*AttachmentType.Mesh*/, 3 /*AttachmentType.LinkedMesh*/, + 4 /*AttachmentType.Path*/, 5 /*AttachmentType.Point*/, + 6 /*AttachmentType.Clipping*/, + ]; + static TransformModeValues = [ + TransformMode.Normal, + TransformMode.OnlyTranslation, + TransformMode.NoRotationOrReflection, + TransformMode.NoScale, + TransformMode.NoScaleOrReflection, + ]; + static PositionModeValues = [PositionMode.Fixed, PositionMode.Percent]; + static SpacingModeValues = [ + SpacingMode.Length, + SpacingMode.Fixed, + SpacingMode.Percent, + ]; + static RotateModeValues = [ + RotateMode.Tangent, + RotateMode.Chain, + RotateMode.ChainScale, + ]; + static BlendModeValues = [ + PIXI.BLEND_MODES.NORMAL, + PIXI.BLEND_MODES.ADD, + PIXI.BLEND_MODES.MULTIPLY, + PIXI.BLEND_MODES.SCREEN, + ]; static BONE_ROTATE = 0; static BONE_TRANSLATE = 1; @@ -59,11 +99,11 @@ export class SkeletonBinary { scale = 1; private linkedMeshes = new Array(); - constructor (attachmentLoader: AttachmentLoader) { + constructor(attachmentLoader: AttachmentLoader) { this.attachmentLoader = attachmentLoader; } - readSkeletonData (binary: Uint8Array): SkeletonData { + readSkeletonData(binary: Uint8Array): SkeletonData { let scale = this.scale; let skeletonData = new SkeletonData(); @@ -73,8 +113,7 @@ export class SkeletonBinary { skeletonData.hash = input.readString(); skeletonData.version = input.readString(); - if (skeletonData.version === '3.8.75') - { + if (skeletonData.version === "3.8.75") { let error = `Unsupported skeleton data, 3.8.75 is deprecated, please export with a newer version of Spine.`; console.error(error); } @@ -93,15 +132,15 @@ export class SkeletonBinary { let n = 0; // Strings. - n = input.readInt(true) - for (let i = 0; i < n; i++) - input.strings.push(input.readString()); + n = input.readInt(true); + for (let i = 0; i < n; i++) input.strings.push(input.readString()); // Bones. - n = input.readInt(true) + n = input.readInt(true); for (let i = 0; i < n; i++) { let name = input.readString(); - let parent = i == 0 ? null : skeletonData.bones[input.readInt(true)]; + let parent = + i == 0 ? null : skeletonData.bones[input.readInt(true)]; let data = new BoneData(i, name, parent); data.rotation = input.readFloat(); data.x = input.readFloat() * scale; @@ -111,9 +150,11 @@ export class SkeletonBinary { data.shearX = input.readFloat(); data.shearY = input.readFloat(); data.length = input.readFloat() * scale; - data.transformMode = SkeletonBinary.TransformModeValues[input.readInt(true)]; + data.transformMode = + SkeletonBinary.TransformModeValues[input.readInt(true)]; data.skinRequired = input.readBoolean(); - if (nonessential) Color.rgba8888ToColor(data.color, input.readInt32()); + if (nonessential) + Color.rgba8888ToColor(data.color, input.readInt32()); skeletonData.bones.push(data); } @@ -126,10 +167,12 @@ export class SkeletonBinary { Color.rgba8888ToColor(data.color, input.readInt32()); let darkColor = input.readInt32(); - if (darkColor != -1) Color.rgb888ToColor(data.darkColor = new Color(), darkColor); + if (darkColor != -1) + Color.rgb888ToColor((data.darkColor = new Color()), darkColor); data.attachmentName = input.readStringRef(); - data.blendMode = SkeletonBinary.BlendModeValues[input.readInt(true)]; + data.blendMode = + SkeletonBinary.BlendModeValues[input.readInt(true)]; skeletonData.slots.push(data); } @@ -187,21 +230,33 @@ export class SkeletonBinary { for (let ii = 0; ii < nn; ii++) data.bones.push(skeletonData.bones[input.readInt(true)]); data.target = skeletonData.slots[input.readInt(true)]; - data.positionMode = SkeletonBinary.PositionModeValues[input.readInt(true)]; - data.spacingMode = SkeletonBinary.SpacingModeValues[input.readInt(true)]; - data.rotateMode = SkeletonBinary.RotateModeValues[input.readInt(true)]; + data.positionMode = + SkeletonBinary.PositionModeValues[input.readInt(true)]; + data.spacingMode = + SkeletonBinary.SpacingModeValues[input.readInt(true)]; + data.rotateMode = + SkeletonBinary.RotateModeValues[input.readInt(true)]; data.offsetRotation = input.readFloat(); data.position = input.readFloat(); if (data.positionMode == PositionMode.Fixed) data.position *= scale; data.spacing = input.readFloat(); - if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) data.spacing *= scale; + if ( + data.spacingMode == SpacingMode.Length || + data.spacingMode == SpacingMode.Fixed + ) + data.spacing *= scale; data.rotateMix = input.readFloat(); data.translateMix = input.readFloat(); skeletonData.pathConstraints.push(data); } // Default skin. - let defaultSkin = this.readSkin(input, skeletonData, true, nonessential); + let defaultSkin = this.readSkin( + input, + skeletonData, + true, + nonessential + ); if (defaultSkin != null) { skeletonData.defaultSkin = defaultSkin; skeletonData.skins.push(defaultSkin); @@ -210,20 +265,38 @@ export class SkeletonBinary { // Skins. { let i = skeletonData.skins.length; - Utils.setArraySize(skeletonData.skins, n = i + input.readInt(true)); + Utils.setArraySize( + skeletonData.skins, + (n = i + input.readInt(true)) + ); for (; i < n; i++) - skeletonData.skins[i] = this.readSkin(input, skeletonData, false, nonessential); + skeletonData.skins[i] = this.readSkin( + input, + skeletonData, + false, + nonessential + ); } // Linked meshes. n = this.linkedMeshes.length; for (let i = 0; i < n; i++) { let linkedMesh = this.linkedMeshes[i]; - let skin = linkedMesh.skin == null ? skeletonData.defaultSkin : skeletonData.findSkin(linkedMesh.skin); - if (skin == null) throw new Error("Skin not found: " + linkedMesh.skin); - let parent = skin.getAttachment(linkedMesh.slotIndex, linkedMesh.parent); - if (parent == null) throw new Error("Parent mesh not found: " + linkedMesh.parent); - linkedMesh.mesh.deformAttachment = linkedMesh.inheritDeform ? parent as VertexAttachment : linkedMesh.mesh; + let skin = + linkedMesh.skin == null + ? skeletonData.defaultSkin + : skeletonData.findSkin(linkedMesh.skin); + if (skin == null) + throw new Error("Skin not found: " + linkedMesh.skin); + let parent = skin.getAttachment( + linkedMesh.slotIndex, + linkedMesh.parent + ); + if (parent == null) + throw new Error("Parent mesh not found: " + linkedMesh.parent); + linkedMesh.mesh.deformAttachment = linkedMesh.inheritDeform + ? (parent as VertexAttachment) + : linkedMesh.mesh; linkedMesh.mesh.setParentMesh(parent as MeshAttachment); // linkedMesh.mesh.updateUVs(); } @@ -247,16 +320,23 @@ export class SkeletonBinary { // Animations. n = input.readInt(true); for (let i = 0; i < n; i++) - skeletonData.animations.push(this.readAnimation(input, input.readString(), skeletonData)); + skeletonData.animations.push( + this.readAnimation(input, input.readString(), skeletonData) + ); return skeletonData; } - private readSkin (input: BinaryInput, skeletonData: SkeletonData, defaultSkin: boolean, nonessential: boolean): Skin { + private readSkin( + input: BinaryInput, + skeletonData: SkeletonData, + defaultSkin: boolean, + nonessential: boolean + ): Skin { let skin = null; let slotCount = 0; if (defaultSkin) { - slotCount = input.readInt(true) + slotCount = input.readInt(true); if (slotCount == 0) return null; skin = new Skin("default"); } else { @@ -266,11 +346,17 @@ export class SkeletonBinary { skin.bones[i] = skeletonData.bones[input.readInt(true)]; for (let i = 0, n = input.readInt(true); i < n; i++) - skin.constraints.push(skeletonData.ikConstraints[input.readInt(true)]); + skin.constraints.push( + skeletonData.ikConstraints[input.readInt(true)] + ); for (let i = 0, n = input.readInt(true); i < n; i++) - skin.constraints.push(skeletonData.transformConstraints[input.readInt(true)]); + skin.constraints.push( + skeletonData.transformConstraints[input.readInt(true)] + ); for (let i = 0, n = input.readInt(true); i < n; i++) - skin.constraints.push(skeletonData.pathConstraints[input.readInt(true)]); + skin.constraints.push( + skeletonData.pathConstraints[input.readInt(true)] + ); slotCount = input.readInt(true); } @@ -279,14 +365,29 @@ export class SkeletonBinary { let slotIndex = input.readInt(true); for (let ii = 0, nn = input.readInt(true); ii < nn; ii++) { let name = input.readStringRef(); - let attachment = this.readAttachment(input, skeletonData, skin, slotIndex, name, nonessential); - if (attachment != null) skin.setAttachment(slotIndex, name, attachment); + let attachment = this.readAttachment( + input, + skeletonData, + skin, + slotIndex, + name, + nonessential + ); + if (attachment != null) + skin.setAttachment(slotIndex, name, attachment); } } return skin; } - private readAttachment(input: BinaryInput, skeletonData: SkeletonData, skin: Skin, slotIndex: number, attachmentName: string, nonessential: boolean): Attachment { + private readAttachment( + input: BinaryInput, + skeletonData: SkeletonData, + skin: Skin, + slotIndex: number, + attachmentName: string, + nonessential: boolean + ): Attachment { let scale = this.scale; let name = input.readStringRef(); @@ -295,164 +396,199 @@ export class SkeletonBinary { let typeIndex = input.readByte(); let type = SkeletonBinary.AttachmentTypeValues[typeIndex]; switch (type) { - case AttachmentType.Region: { - let path = input.readStringRef(); - let rotation = input.readFloat(); - let x = input.readFloat(); - let y = input.readFloat(); - let scaleX = input.readFloat(); - let scaleY = input.readFloat(); - let width = input.readFloat(); - let height = input.readFloat(); - let color = input.readInt32(); - - if (path == null) path = name; - let region = this.attachmentLoader.newRegionAttachment(skin, name, path); - if (region == null) return null; - region.path = path; - region.x = x * scale; - region.y = y * scale; - region.scaleX = scaleX; - region.scaleY = scaleY; - region.rotation = rotation; - region.width = width * scale; - region.height = height * scale; - Color.rgba8888ToColor(region.color, color); - // region.updateOffset(); - return region; - } - case AttachmentType.BoundingBox: { - let vertexCount = input.readInt(true); - let vertices = this.readVertices(input, vertexCount); - let color = nonessential ? input.readInt32() : 0; - - let box = this.attachmentLoader.newBoundingBoxAttachment(skin, name); - if (box == null) return null; - box.worldVerticesLength = vertexCount << 1; - box.vertices = vertices.vertices; - box.bones = vertices.bones; - if (nonessential) Color.rgba8888ToColor(box.color, color); - return box; - } - case AttachmentType.Mesh: { - let path = input.readStringRef(); - let color = input.readInt32(); - let vertexCount = input.readInt(true); - let uvs = this.readFloatArray(input, vertexCount << 1, 1); - let triangles = this.readShortArray(input); - let vertices = this.readVertices(input, vertexCount); - let hullLength = input.readInt(true); - let edges = null; - let width = 0, height = 0; - if (nonessential) { - edges = this.readShortArray(input); - width = input.readFloat(); - height = input.readFloat(); + case AttachmentType.Region: { + let path = input.readStringRef(); + let rotation = input.readFloat(); + let x = input.readFloat(); + let y = input.readFloat(); + let scaleX = input.readFloat(); + let scaleY = input.readFloat(); + let width = input.readFloat(); + let height = input.readFloat(); + let color = input.readInt32(); + + if (path == null) path = name; + let region = this.attachmentLoader.newRegionAttachment( + skin, + name, + path + ); + if (region == null) return null; + region.path = path; + region.x = x * scale; + region.y = y * scale; + region.scaleX = scaleX; + region.scaleY = scaleY; + region.rotation = rotation; + region.width = width * scale; + region.height = height * scale; + Color.rgba8888ToColor(region.color, color); + // region.updateOffset(); + return region; } + case AttachmentType.BoundingBox: { + let vertexCount = input.readInt(true); + let vertices = this.readVertices(input, vertexCount); + let color = nonessential ? input.readInt32() : 0; - if (path == null) path = name; - let mesh = this.attachmentLoader.newMeshAttachment(skin, name, path); - if (mesh == null) return null; - mesh.path = path; - Color.rgba8888ToColor(mesh.color, color); - mesh.bones = vertices.bones; - mesh.vertices = vertices.vertices; - mesh.worldVerticesLength = vertexCount << 1; - mesh.triangles = triangles; - mesh.regionUVs = new Float32Array(uvs); - // mesh.updateUVs(); - mesh.hullLength = hullLength << 1; - if (nonessential) { - mesh.edges = edges; - mesh.width = width * scale; - mesh.height = height * scale; + let box = this.attachmentLoader.newBoundingBoxAttachment( + skin, + name + ); + if (box == null) return null; + box.worldVerticesLength = vertexCount << 1; + box.vertices = vertices.vertices; + box.bones = vertices.bones; + if (nonessential) Color.rgba8888ToColor(box.color, color); + return box; } - return mesh; - } - case AttachmentType.LinkedMesh: { - let path = input.readStringRef(); - let color = input.readInt32(); - let skinName = input.readStringRef(); - let parent = input.readStringRef(); - let inheritDeform = input.readBoolean(); - let width = 0, height = 0; - if (nonessential) { - width = input.readFloat(); - height = input.readFloat(); + case AttachmentType.Mesh: { + let path = input.readStringRef(); + let color = input.readInt32(); + let vertexCount = input.readInt(true); + let uvs = this.readFloatArray(input, vertexCount << 1, 1); + let triangles = this.readShortArray(input); + let vertices = this.readVertices(input, vertexCount); + let hullLength = input.readInt(true); + let edges = null; + let width = 0, + height = 0; + if (nonessential) { + edges = this.readShortArray(input); + width = input.readFloat(); + height = input.readFloat(); + } + + if (path == null) path = name; + let mesh = this.attachmentLoader.newMeshAttachment( + skin, + name, + path + ); + if (mesh == null) return null; + mesh.path = path; + Color.rgba8888ToColor(mesh.color, color); + mesh.bones = vertices.bones; + mesh.vertices = vertices.vertices; + mesh.worldVerticesLength = vertexCount << 1; + mesh.triangles = triangles; + mesh.regionUVs = new Float32Array(uvs); + // mesh.updateUVs(); + mesh.hullLength = hullLength << 1; + if (nonessential) { + mesh.edges = edges; + mesh.width = width * scale; + mesh.height = height * scale; + } + return mesh; } + case AttachmentType.LinkedMesh: { + let path = input.readStringRef(); + let color = input.readInt32(); + let skinName = input.readStringRef(); + let parent = input.readStringRef(); + let inheritDeform = input.readBoolean(); + let width = 0, + height = 0; + if (nonessential) { + width = input.readFloat(); + height = input.readFloat(); + } - if (path == null) path = name; - let mesh = this.attachmentLoader.newMeshAttachment(skin, name, path); - if (mesh == null) return null; - mesh.path = path; - Color.rgba8888ToColor(mesh.color, color); - if (nonessential) { - mesh.width = width * scale; - mesh.height = height * scale; + if (path == null) path = name; + let mesh = this.attachmentLoader.newMeshAttachment( + skin, + name, + path + ); + if (mesh == null) return null; + mesh.path = path; + Color.rgba8888ToColor(mesh.color, color); + if (nonessential) { + mesh.width = width * scale; + mesh.height = height * scale; + } + this.linkedMeshes.push( + new LinkedMesh( + mesh, + skinName, + slotIndex, + parent, + inheritDeform + ) + ); + return mesh; + } + case AttachmentType.Path: { + let closed = input.readBoolean(); + let constantSpeed = input.readBoolean(); + let vertexCount = input.readInt(true); + let vertices = this.readVertices(input, vertexCount); + let lengths = Utils.newArray(vertexCount / 3, 0); + for (let i = 0, n = lengths.length; i < n; i++) + lengths[i] = input.readFloat() * scale; + let color = nonessential ? input.readInt32() : 0; + + let path = this.attachmentLoader.newPathAttachment(skin, name); + if (path == null) return null; + path.closed = closed; + path.constantSpeed = constantSpeed; + path.worldVerticesLength = vertexCount << 1; + path.vertices = vertices.vertices; + path.bones = vertices.bones; + path.lengths = lengths; + if (nonessential) Color.rgba8888ToColor(path.color, color); + return path; + } + case AttachmentType.Point: { + let rotation = input.readFloat(); + let x = input.readFloat(); + let y = input.readFloat(); + let color = nonessential ? input.readInt32() : 0; + + let point = this.attachmentLoader.newPointAttachment( + skin, + name + ); + if (point == null) return null; + point.x = x * scale; + point.y = y * scale; + point.rotation = rotation; + if (nonessential) Color.rgba8888ToColor(point.color, color); + return point; + } + case AttachmentType.Clipping: { + let endSlotIndex = input.readInt(true); + let vertexCount = input.readInt(true); + let vertices = this.readVertices(input, vertexCount); + let color = nonessential ? input.readInt32() : 0; + + let clip = this.attachmentLoader.newClippingAttachment( + skin, + name + ); + if (clip == null) return null; + clip.endSlot = skeletonData.slots[endSlotIndex]; + clip.worldVerticesLength = vertexCount << 1; + clip.vertices = vertices.vertices; + clip.bones = vertices.bones; + if (nonessential) Color.rgba8888ToColor(clip.color, color); + return clip; } - this.linkedMeshes.push(new LinkedMesh(mesh, skinName, slotIndex, parent, inheritDeform)); - return mesh; - } - case AttachmentType.Path: { - let closed = input.readBoolean(); - let constantSpeed = input.readBoolean(); - let vertexCount = input.readInt(true); - let vertices = this.readVertices(input, vertexCount); - let lengths = Utils.newArray(vertexCount / 3, 0); - for (let i = 0, n = lengths.length; i < n; i++) - lengths[i] = input.readFloat() * scale; - let color = nonessential ? input.readInt32() : 0; - - let path = this.attachmentLoader.newPathAttachment(skin, name); - if (path == null) return null; - path.closed = closed; - path.constantSpeed = constantSpeed; - path.worldVerticesLength = vertexCount << 1; - path.vertices = vertices.vertices; - path.bones = vertices.bones; - path.lengths = lengths; - if (nonessential) Color.rgba8888ToColor(path.color, color); - return path; - } - case AttachmentType.Point: { - let rotation = input.readFloat(); - let x = input.readFloat(); - let y = input.readFloat(); - let color = nonessential ? input.readInt32() : 0; - - let point = this.attachmentLoader.newPointAttachment(skin, name); - if (point == null) return null; - point.x = x * scale; - point.y = y * scale; - point.rotation = rotation; - if (nonessential) Color.rgba8888ToColor(point.color, color); - return point; - } - case AttachmentType.Clipping: { - let endSlotIndex = input.readInt(true); - let vertexCount = input.readInt(true); - let vertices = this.readVertices(input, vertexCount); - let color = nonessential ? input.readInt32() : 0; - - let clip = this.attachmentLoader.newClippingAttachment(skin, name); - if (clip == null) return null; - clip.endSlot = skeletonData.slots[endSlotIndex]; - clip.worldVerticesLength = vertexCount << 1; - clip.vertices = vertices.vertices; - clip.bones = vertices.bones; - if (nonessential) Color.rgba8888ToColor(clip.color, color); - return clip; - } } return null; } - private readVertices (input: BinaryInput, vertexCount: number): Vertices { + private readVertices(input: BinaryInput, vertexCount: number): Vertices { let verticesLength = vertexCount << 1; let vertices = new Vertices(); let scale = this.scale; if (!input.readBoolean()) { - vertices.vertices = this.readFloatArray(input, verticesLength, scale); + vertices.vertices = this.readFloatArray( + input, + verticesLength, + scale + ); return vertices; } let weights = new Array(); @@ -472,27 +608,32 @@ export class SkeletonBinary { return vertices; } - private readFloatArray (input: BinaryInput, n: number, scale: number): number[] { + private readFloatArray( + input: BinaryInput, + n: number, + scale: number + ): number[] { let array = new Array(n); if (scale == 1) { - for (let i = 0; i < n; i++) - array[i] = input.readFloat(); + for (let i = 0; i < n; i++) array[i] = input.readFloat(); } else { - for (let i = 0; i < n; i++) - array[i] = input.readFloat() * scale; + for (let i = 0; i < n; i++) array[i] = input.readFloat() * scale; } return array; } - private readShortArray (input: BinaryInput): number[] { + private readShortArray(input: BinaryInput): number[] { let n = input.readInt(true); let array = new Array(n); - for (let i = 0; i < n; i++) - array[i] = input.readShort(); + for (let i = 0; i < n; i++) array[i] = input.readShort(); return array; } - private readAnimation (input: BinaryInput, name: string, skeletonData: SkeletonData): Animation { + private readAnimation( + input: BinaryInput, + name: string, + skeletonData: SkeletonData + ): Animation { let timelines = new Array(); let scale = this.scale; let duration = 0; @@ -506,43 +647,96 @@ export class SkeletonBinary { let timelineType = input.readByte(); let frameCount = input.readInt(true); switch (timelineType) { - case SkeletonBinary.SLOT_ATTACHMENT: { - let timeline = new AttachmentTimeline(frameCount); - timeline.slotIndex = slotIndex; - for (let frameIndex = 0; frameIndex < frameCount; frameIndex++) - timeline.setFrame(frameIndex, input.readFloat(), input.readStringRef()); - timelines.push(timeline); - duration = Math.max(duration, timeline.frames[frameCount - 1]); - break; - } - case SkeletonBinary.SLOT_COLOR: { - let timeline = new ColorTimeline(frameCount); - timeline.slotIndex = slotIndex; - for (let frameIndex = 0; frameIndex < frameCount; frameIndex++) { - let time = input.readFloat(); - Color.rgba8888ToColor(tempColor1, input.readInt32()); - timeline.setFrame(frameIndex, time, tempColor1.r, tempColor1.g, tempColor1.b, tempColor1.a); - if (frameIndex < frameCount - 1) this.readCurve(input, frameIndex, timeline); + case SkeletonBinary.SLOT_ATTACHMENT: { + let timeline = new AttachmentTimeline(frameCount); + timeline.slotIndex = slotIndex; + for ( + let frameIndex = 0; + frameIndex < frameCount; + frameIndex++ + ) + timeline.setFrame( + frameIndex, + input.readFloat(), + input.readStringRef() + ); + timelines.push(timeline); + duration = Math.max( + duration, + timeline.frames[frameCount - 1] + ); + break; } - timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(frameCount - 1) * ColorTimeline.ENTRIES]); - break; - } - case SkeletonBinary.SLOT_TWO_COLOR: { - let timeline = new TwoColorTimeline(frameCount); - timeline.slotIndex = slotIndex; - for (let frameIndex = 0; frameIndex < frameCount; frameIndex++) { - let time = input.readFloat(); - Color.rgba8888ToColor(tempColor1, input.readInt32()); - Color.rgb888ToColor(tempColor2, input.readInt32()); - timeline.setFrame(frameIndex, time, tempColor1.r, tempColor1.g, tempColor1.b, tempColor1.a, tempColor2.r, - tempColor2.g, tempColor2.b); - if (frameIndex < frameCount - 1) this.readCurve(input, frameIndex, timeline); + case SkeletonBinary.SLOT_COLOR: { + let timeline = new ColorTimeline(frameCount); + timeline.slotIndex = slotIndex; + for ( + let frameIndex = 0; + frameIndex < frameCount; + frameIndex++ + ) { + let time = input.readFloat(); + Color.rgba8888ToColor( + tempColor1, + input.readInt32() + ); + timeline.setFrame( + frameIndex, + time, + tempColor1.r, + tempColor1.g, + tempColor1.b, + tempColor1.a + ); + if (frameIndex < frameCount - 1) + this.readCurve(input, frameIndex, timeline); + } + timelines.push(timeline); + duration = Math.max( + duration, + timeline.frames[ + (frameCount - 1) * ColorTimeline.ENTRIES + ] + ); + break; + } + case SkeletonBinary.SLOT_TWO_COLOR: { + let timeline = new TwoColorTimeline(frameCount); + timeline.slotIndex = slotIndex; + for ( + let frameIndex = 0; + frameIndex < frameCount; + frameIndex++ + ) { + let time = input.readFloat(); + Color.rgba8888ToColor( + tempColor1, + input.readInt32() + ); + Color.rgb888ToColor(tempColor2, input.readInt32()); + timeline.setFrame( + frameIndex, + time, + tempColor1.r, + tempColor1.g, + tempColor1.b, + tempColor1.a, + tempColor2.r, + tempColor2.g, + tempColor2.b + ); + if (frameIndex < frameCount - 1) + this.readCurve(input, frameIndex, timeline); + } + timelines.push(timeline); + duration = Math.max( + duration, + timeline.frames[ + (frameCount - 1) * TwoColorTimeline.ENTRIES + ] + ); + break; } - timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(frameCount - 1) * TwoColorTimeline.ENTRIES]); - break; - } } } } @@ -554,40 +748,68 @@ export class SkeletonBinary { let timelineType = input.readByte(); let frameCount = input.readInt(true); switch (timelineType) { - case SkeletonBinary.BONE_ROTATE: { - let timeline = new RotateTimeline(frameCount); - timeline.boneIndex = boneIndex; - for (let frameIndex = 0; frameIndex < frameCount; frameIndex++) { - timeline.setFrame(frameIndex, input.readFloat(), input.readFloat()); - if (frameIndex < frameCount - 1) this.readCurve(input, frameIndex, timeline); - } - timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(frameCount - 1) * RotateTimeline.ENTRIES]); - break; - } - case SkeletonBinary.BONE_TRANSLATE: - case SkeletonBinary.BONE_SCALE: - case SkeletonBinary.BONE_SHEAR: { - let timeline; - let timelineScale = 1; - if (timelineType == SkeletonBinary.BONE_SCALE) - timeline = new ScaleTimeline(frameCount); - else if (timelineType == SkeletonBinary.BONE_SHEAR) - timeline = new ShearTimeline(frameCount); - else { - timeline = new TranslateTimeline(frameCount); - timelineScale = scale; + case SkeletonBinary.BONE_ROTATE: { + let timeline = new RotateTimeline(frameCount); + timeline.boneIndex = boneIndex; + for ( + let frameIndex = 0; + frameIndex < frameCount; + frameIndex++ + ) { + timeline.setFrame( + frameIndex, + input.readFloat(), + input.readFloat() + ); + if (frameIndex < frameCount - 1) + this.readCurve(input, frameIndex, timeline); + } + timelines.push(timeline); + duration = Math.max( + duration, + timeline.frames[ + (frameCount - 1) * RotateTimeline.ENTRIES + ] + ); + break; } - timeline.boneIndex = boneIndex; - for (let frameIndex = 0; frameIndex < frameCount; frameIndex++) { - timeline.setFrame(frameIndex, input.readFloat(), input.readFloat() * timelineScale, - input.readFloat() * timelineScale); - if (frameIndex < frameCount - 1) this.readCurve(input, frameIndex, timeline); + case SkeletonBinary.BONE_TRANSLATE: + case SkeletonBinary.BONE_SCALE: + case SkeletonBinary.BONE_SHEAR: { + let timeline; + let timelineScale = 1; + if (timelineType == SkeletonBinary.BONE_SCALE) + timeline = new ScaleTimeline(frameCount); + else if (timelineType == SkeletonBinary.BONE_SHEAR) + timeline = new ShearTimeline(frameCount); + else { + timeline = new TranslateTimeline(frameCount); + timelineScale = scale; + } + timeline.boneIndex = boneIndex; + for ( + let frameIndex = 0; + frameIndex < frameCount; + frameIndex++ + ) { + timeline.setFrame( + frameIndex, + input.readFloat(), + input.readFloat() * timelineScale, + input.readFloat() * timelineScale + ); + if (frameIndex < frameCount - 1) + this.readCurve(input, frameIndex, timeline); + } + timelines.push(timeline); + duration = Math.max( + duration, + timeline.frames[ + (frameCount - 1) * TranslateTimeline.ENTRIES + ] + ); + break; } - timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(frameCount - 1) * TranslateTimeline.ENTRIES]); - break; - } } } } @@ -599,12 +821,23 @@ export class SkeletonBinary { let timeline = new IkConstraintTimeline(frameCount); timeline.ikConstraintIndex = index; for (let frameIndex = 0; frameIndex < frameCount; frameIndex++) { - timeline.setFrame(frameIndex, input.readFloat(), input.readFloat(), input.readFloat() * scale, input.readByte(), input.readBoolean(), - input.readBoolean()); - if (frameIndex < frameCount - 1) this.readCurve(input, frameIndex, timeline); + timeline.setFrame( + frameIndex, + input.readFloat(), + input.readFloat(), + input.readFloat() * scale, + input.readByte(), + input.readBoolean(), + input.readBoolean() + ); + if (frameIndex < frameCount - 1) + this.readCurve(input, frameIndex, timeline); } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(frameCount - 1) * IkConstraintTimeline.ENTRIES]); + duration = Math.max( + duration, + timeline.frames[(frameCount - 1) * IkConstraintTimeline.ENTRIES] + ); } // Transform constraint timelines. @@ -614,12 +847,24 @@ export class SkeletonBinary { let timeline = new TransformConstraintTimeline(frameCount); timeline.transformConstraintIndex = index; for (let frameIndex = 0; frameIndex < frameCount; frameIndex++) { - timeline.setFrame(frameIndex, input.readFloat(), input.readFloat(), input.readFloat(), input.readFloat(), - input.readFloat()); - if (frameIndex < frameCount - 1) this.readCurve(input, frameIndex, timeline); + timeline.setFrame( + frameIndex, + input.readFloat(), + input.readFloat(), + input.readFloat(), + input.readFloat(), + input.readFloat() + ); + if (frameIndex < frameCount - 1) + this.readCurve(input, frameIndex, timeline); } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(frameCount - 1) * TransformConstraintTimeline.ENTRIES]); + duration = Math.max( + duration, + timeline.frames[ + (frameCount - 1) * TransformConstraintTimeline.ENTRIES + ] + ); } // Path constraint timelines. @@ -630,37 +875,79 @@ export class SkeletonBinary { let timelineType = input.readByte(); let frameCount = input.readInt(true); switch (timelineType) { - case SkeletonBinary.PATH_POSITION: - case SkeletonBinary.PATH_SPACING: { - let timeline; - let timelineScale = 1; - if (timelineType == SkeletonBinary.PATH_SPACING) { - timeline = new PathConstraintSpacingTimeline(frameCount); - if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) timelineScale = scale; - } else { - timeline = new PathConstraintPositionTimeline(frameCount); - if (data.positionMode == PositionMode.Fixed) timelineScale = scale; - } - timeline.pathConstraintIndex = index; - for (let frameIndex = 0; frameIndex < frameCount; frameIndex++) { - timeline.setFrame(frameIndex, input.readFloat(), input.readFloat() * timelineScale); - if (frameIndex < frameCount - 1) this.readCurve(input, frameIndex, timeline); + case SkeletonBinary.PATH_POSITION: + case SkeletonBinary.PATH_SPACING: { + let timeline; + let timelineScale = 1; + if (timelineType == SkeletonBinary.PATH_SPACING) { + timeline = new PathConstraintSpacingTimeline( + frameCount + ); + if ( + data.spacingMode == SpacingMode.Length || + data.spacingMode == SpacingMode.Fixed + ) + timelineScale = scale; + } else { + timeline = new PathConstraintPositionTimeline( + frameCount + ); + if (data.positionMode == PositionMode.Fixed) + timelineScale = scale; + } + timeline.pathConstraintIndex = index; + for ( + let frameIndex = 0; + frameIndex < frameCount; + frameIndex++ + ) { + timeline.setFrame( + frameIndex, + input.readFloat(), + input.readFloat() * timelineScale + ); + if (frameIndex < frameCount - 1) + this.readCurve(input, frameIndex, timeline); + } + timelines.push(timeline); + duration = Math.max( + duration, + timeline.frames[ + (frameCount - 1) * + PathConstraintPositionTimeline.ENTRIES + ] + ); + break; } - timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(frameCount - 1) * PathConstraintPositionTimeline.ENTRIES]); - break; - } - case SkeletonBinary.PATH_MIX: { - let timeline = new PathConstraintMixTimeline(frameCount); - timeline.pathConstraintIndex = index; - for (let frameIndex = 0; frameIndex < frameCount; frameIndex++) { - timeline.setFrame(frameIndex, input.readFloat(), input.readFloat(), input.readFloat()); - if (frameIndex < frameCount - 1) this.readCurve(input, frameIndex, timeline); + case SkeletonBinary.PATH_MIX: { + let timeline = new PathConstraintMixTimeline( + frameCount + ); + timeline.pathConstraintIndex = index; + for ( + let frameIndex = 0; + frameIndex < frameCount; + frameIndex++ + ) { + timeline.setFrame( + frameIndex, + input.readFloat(), + input.readFloat(), + input.readFloat() + ); + if (frameIndex < frameCount - 1) + this.readCurve(input, frameIndex, timeline); + } + timelines.push(timeline); + duration = Math.max( + duration, + timeline.frames[ + (frameCount - 1) * + PathConstraintMixTimeline.ENTRIES + ] + ); + break; } - timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(frameCount - 1) * PathConstraintMixTimeline.ENTRIES]); - break; - } } } } @@ -671,22 +958,33 @@ export class SkeletonBinary { for (let ii = 0, nn = input.readInt(true); ii < nn; ii++) { let slotIndex = input.readInt(true); for (let iii = 0, nnn = input.readInt(true); iii < nnn; iii++) { - let attachment = skin.getAttachment(slotIndex, input.readStringRef()) as VertexAttachment; + let attachment = skin.getAttachment( + slotIndex, + input.readStringRef() + ) as VertexAttachment; let weighted = attachment.bones != null; let vertices = attachment.vertices; - let deformLength = weighted ? vertices.length / 3 * 2 : vertices.length; + let deformLength = weighted + ? (vertices.length / 3) * 2 + : vertices.length; let frameCount = input.readInt(true); let timeline = new DeformTimeline(frameCount); timeline.slotIndex = slotIndex; timeline.attachment = attachment; - for (let frameIndex = 0; frameIndex < frameCount; frameIndex++) { + for ( + let frameIndex = 0; + frameIndex < frameCount; + frameIndex++ + ) { let time = input.readFloat(); let deform; let end = input.readInt(true); if (end == 0) - deform = weighted ? Utils.newFloatArray(deformLength) : vertices; + deform = weighted + ? Utils.newFloatArray(deformLength) + : vertices; else { deform = Utils.newFloatArray(deformLength); let start = input.readInt(true); @@ -705,10 +1003,14 @@ export class SkeletonBinary { } timeline.setFrame(frameIndex, time, deform); - if (frameIndex < frameCount - 1) this.readCurve(input, frameIndex, timeline); + if (frameIndex < frameCount - 1) + this.readCurve(input, frameIndex, timeline); } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[frameCount - 1]); + duration = Math.max( + duration, + timeline.frames[frameCount - 1] + ); } } } @@ -722,24 +1024,26 @@ export class SkeletonBinary { let time = input.readFloat(); let offsetCount = input.readInt(true); let drawOrder = Utils.newArray(slotCount, 0); - for (let ii = slotCount - 1; ii >= 0; ii--) - drawOrder[ii] = -1; + for (let ii = slotCount - 1; ii >= 0; ii--) drawOrder[ii] = -1; let unchanged = Utils.newArray(slotCount - offsetCount, 0); - let originalIndex = 0, unchangedIndex = 0; + let originalIndex = 0, + unchangedIndex = 0; for (let ii = 0; ii < offsetCount; ii++) { let slotIndex = input.readInt(true); // Collect unchanged items. while (originalIndex != slotIndex) unchanged[unchangedIndex++] = originalIndex++; // Set changed items. - drawOrder[originalIndex + input.readInt(true)] = originalIndex++; + drawOrder[originalIndex + input.readInt(true)] = + originalIndex++; } // Collect remaining unchanged items. while (originalIndex < slotCount) unchanged[unchangedIndex++] = originalIndex++; // Fill in unchanged items. for (let ii = slotCount - 1; ii >= 0; ii--) - if (drawOrder[ii] == -1) drawOrder[ii] = unchanged[--unchangedIndex]; + if (drawOrder[ii] == -1) + drawOrder[ii] = unchanged[--unchangedIndex]; timeline.setFrame(i, time, drawOrder); } timelines.push(timeline); @@ -756,7 +1060,9 @@ export class SkeletonBinary { let event = new Event(time, eventData); event.intValue = input.readInt(false); event.floatValue = input.readFloat(); - event.stringValue = input.readBoolean() ? input.readString() : eventData.stringValue; + event.stringValue = input.readBoolean() + ? input.readString() + : eventData.stringValue; if (event.data.audioPath != null) { event.volume = input.readFloat(); event.balance = input.readFloat(); @@ -770,29 +1076,54 @@ export class SkeletonBinary { return new Animation(name, timelines, duration); } - private readCurve (input: BinaryInput, frameIndex: number, timeline: CurveTimeline) { + private readCurve( + input: BinaryInput, + frameIndex: number, + timeline: CurveTimeline + ) { switch (input.readByte()) { - case SkeletonBinary.CURVE_STEPPED: - timeline.setStepped(frameIndex); - break; - case SkeletonBinary.CURVE_BEZIER: - this.setCurve(timeline, frameIndex, input.readFloat(), input.readFloat(), input.readFloat(), input.readFloat()); - break; + case SkeletonBinary.CURVE_STEPPED: + timeline.setStepped(frameIndex); + break; + case SkeletonBinary.CURVE_BEZIER: + this.setCurve( + timeline, + frameIndex, + input.readFloat(), + input.readFloat(), + input.readFloat(), + input.readFloat() + ); + break; } } - setCurve (timeline: CurveTimeline, frameIndex: number, cx1: number, cy1: number, cx2: number, cy2: number) { + setCurve( + timeline: CurveTimeline, + frameIndex: number, + cx1: number, + cy1: number, + cx2: number, + cy2: number + ) { timeline.setCurve(frameIndex, cx1, cy1, cx2, cy2); } } class LinkedMesh { - parent: string; skin: string; + parent: string; + skin: string; slotIndex: number; mesh: MeshAttachment; inheritDeform: boolean; - constructor (mesh: MeshAttachment, skin: string, slotIndex: number, parent: string, inheritDeform: boolean) { + constructor( + mesh: MeshAttachment, + skin: string, + slotIndex: number, + parent: string, + inheritDeform: boolean + ) { this.mesh = mesh; this.skin = skin; this.slotIndex = slotIndex; @@ -802,5 +1133,8 @@ class LinkedMesh { } class Vertices { - constructor(public bones: Array = null, public vertices: Array | Float32Array = null) { } + constructor( + public bones: Array = null, + public vertices: Array | Float32Array = null + ) {} } diff --git a/packages/runtime-3.8/src/core/SkeletonJson.ts b/packages/runtime-3.8/src/core/SkeletonJson.ts index 1145d88c..3bf76b18 100644 --- a/packages/runtime-3.8/src/core/SkeletonJson.ts +++ b/packages/runtime-3.8/src/core/SkeletonJson.ts @@ -1,17 +1,26 @@ -import type {Attachment, AttachmentLoader, MeshAttachment, VertexAttachment} from './attachments'; -import {Animation} from './Animation'; -import {Event} from './Event'; -import {SkeletonData} from './SkeletonData'; -import {SlotData} from './SlotData'; -import {BoneData} from './BoneData'; -import {IkConstraintData} from './IkConstraintData'; -import {TransformConstraintData} from './TransformConstraintData'; -import {PathConstraintData, SpacingMode} from './PathConstraintData'; -import {Skin} from './Skin'; -import {EventData} from './EventData'; +import type { + Attachment, + AttachmentLoader, + MeshAttachment, + VertexAttachment, +} from "./attachments"; +import { Animation } from "./Animation"; +import { Event } from "./Event"; +import { SkeletonData } from "./SkeletonData"; +import { SlotData } from "./SlotData"; +import { BoneData } from "./BoneData"; +import { IkConstraintData } from "./IkConstraintData"; +import { TransformConstraintData } from "./TransformConstraintData"; +import { PathConstraintData, SpacingMode } from "./PathConstraintData"; +import { Skin } from "./Skin"; +import { EventData } from "./EventData"; import { AttachmentTimeline, - ColorTimeline, CurveTimeline, DeformTimeline, DrawOrderTimeline, EventTimeline, + ColorTimeline, + CurveTimeline, + DeformTimeline, + DrawOrderTimeline, + EventTimeline, IkConstraintTimeline, PathConstraintMixTimeline, PathConstraintPositionTimeline, @@ -22,10 +31,17 @@ import { Timeline, TransformConstraintTimeline, TranslateTimeline, - TwoColorTimeline -} from './Animation'; -import {ArrayLike, Color, PositionMode, RotateMode, TransformMode, Utils, settings} from '@pixi-spine/base'; -import {BLEND_MODES} from '@pixi/constants'; + TwoColorTimeline, +} from "./Animation"; +import { + ArrayLike, + Color, + PositionMode, + RotateMode, + TransformMode, + Utils, + settings, +} from "@pixi-spine/base"; /** * @public @@ -35,26 +51,25 @@ export class SkeletonJson { scale = 1; private linkedMeshes = new Array(); - constructor (attachmentLoader: AttachmentLoader) { + constructor(attachmentLoader: AttachmentLoader) { this.attachmentLoader = attachmentLoader; } - readSkeletonData (json: string | any): SkeletonData { + readSkeletonData(json: string | any): SkeletonData { let scale = this.scale; let skeletonData = new SkeletonData(); - let root = typeof(json) === "string" ? JSON.parse(json) : json; + let root = typeof json === "string" ? JSON.parse(json) : json; // Skeleton let skeletonMap = root.skeleton; if (skeletonMap != null) { skeletonData.hash = skeletonMap.hash; skeletonData.version = skeletonMap.spine; - if (skeletonData.version.substr(0, 3) !== '3.8') { + if (skeletonData.version.substr(0, 3) !== "3.8") { let error = `Spine 3.8 loader cant load version ${skeletonMap.spine}. Please configure your pixi-spine bundle`; console.error(error); } - if (skeletonData.version === '3.8.75') - { + if (skeletonData.version === "3.8.75") { let error = `Unsupported skeleton data, 3.8.75 is deprecated, please export with a newer version of Spine.`; console.error(error); } @@ -75,9 +90,14 @@ export class SkeletonJson { let parentName: string = this.getValue(boneMap, "parent", null); if (parentName != null) { parent = skeletonData.findBone(parentName); - if (parent == null) throw new Error("Parent bone not found: " + parentName); + if (parent == null) + throw new Error("Parent bone not found: " + parentName); } - let data = new BoneData(skeletonData.bones.length, boneMap.name, parent); + let data = new BoneData( + skeletonData.bones.length, + boneMap.name, + parent + ); data.length = this.getValue(boneMap, "length", 0) * scale; data.x = this.getValue(boneMap, "x", 0) * scale; data.y = this.getValue(boneMap, "y", 0) * scale; @@ -86,7 +106,9 @@ export class SkeletonJson { data.scaleY = this.getValue(boneMap, "scaleY", 1); data.shearX = this.getValue(boneMap, "shearX", 0); data.shearY = this.getValue(boneMap, "shearY", 0); - data.transformMode = SkeletonJson.transformModeFromString(this.getValue(boneMap, "transform", "normal")); + data.transformMode = SkeletonJson.transformModeFromString( + this.getValue(boneMap, "transform", "normal") + ); data.skinRequired = this.getValue(boneMap, "skin", false); skeletonData.bones.push(data); @@ -100,8 +122,13 @@ export class SkeletonJson { let slotName: string = slotMap.name; let boneName: string = slotMap.bone; let boneData = skeletonData.findBone(boneName); - if (boneData == null) throw new Error("Slot bone not found: " + boneName); - let data = new SlotData(skeletonData.slots.length, slotName, boneData); + if (boneData == null) + throw new Error("Slot bone not found: " + boneName); + let data = new SlotData( + skeletonData.slots.length, + slotName, + boneData + ); let color: string = this.getValue(slotMap, "color", null); if (color != null) data.color.setFromString(color); @@ -112,8 +139,14 @@ export class SkeletonJson { data.darkColor.setFromString(dark); } - data.attachmentName = this.getValue(slotMap, "attachment", null); - data.blendMode = SkeletonJson.blendModeFromString(this.getValue(slotMap, "blend", "normal")); + data.attachmentName = this.getValue( + slotMap, + "attachment", + null + ); + data.blendMode = SkeletonJson.blendModeFromString( + this.getValue(slotMap, "blend", "normal") + ); skeletonData.slots.push(data); } } @@ -129,17 +162,26 @@ export class SkeletonJson { for (let j = 0; j < constraintMap.bones.length; j++) { let boneName = constraintMap.bones[j]; let bone = skeletonData.findBone(boneName); - if (bone == null) throw new Error("IK bone not found: " + boneName); + if (bone == null) + throw new Error("IK bone not found: " + boneName); data.bones.push(bone); } let targetName: string = constraintMap.target; data.target = skeletonData.findBone(targetName); - if (data.target == null) throw new Error("IK target bone not found: " + targetName); + if (data.target == null) + throw new Error("IK target bone not found: " + targetName); data.mix = this.getValue(constraintMap, "mix", 1); - data.softness = this.getValue(constraintMap, "softness", 0) * scale; - data.bendDirection = this.getValue(constraintMap, "bendPositive", true) ? 1 : -1; + data.softness = + this.getValue(constraintMap, "softness", 0) * scale; + data.bendDirection = this.getValue( + constraintMap, + "bendPositive", + true + ) + ? 1 + : -1; data.compress = this.getValue(constraintMap, "compress", false); data.stretch = this.getValue(constraintMap, "stretch", false); data.uniform = this.getValue(constraintMap, "uniform", false); @@ -159,17 +201,28 @@ export class SkeletonJson { for (let j = 0; j < constraintMap.bones.length; j++) { let boneName = constraintMap.bones[j]; let bone = skeletonData.findBone(boneName); - if (bone == null) throw new Error("Transform constraint bone not found: " + boneName); + if (bone == null) + throw new Error( + "Transform constraint bone not found: " + boneName + ); data.bones.push(bone); } let targetName: string = constraintMap.target; data.target = skeletonData.findBone(targetName); - if (data.target == null) throw new Error("Transform constraint target bone not found: " + targetName); + if (data.target == null) + throw new Error( + "Transform constraint target bone not found: " + + targetName + ); data.local = this.getValue(constraintMap, "local", false); data.relative = this.getValue(constraintMap, "relative", false); - data.offsetRotation = this.getValue(constraintMap, "rotation", 0); + data.offsetRotation = this.getValue( + constraintMap, + "rotation", + 0 + ); data.offsetX = this.getValue(constraintMap, "x", 0) * scale; data.offsetY = this.getValue(constraintMap, "y", 0) * scale; data.offsetScaleX = this.getValue(constraintMap, "scaleX", 0); @@ -177,7 +230,11 @@ export class SkeletonJson { data.offsetShearY = this.getValue(constraintMap, "shearY", 0); data.rotateMix = this.getValue(constraintMap, "rotateMix", 1); - data.translateMix = this.getValue(constraintMap, "translateMix", 1); + data.translateMix = this.getValue( + constraintMap, + "translateMix", + 1 + ); data.scaleMix = this.getValue(constraintMap, "scaleMix", 1); data.shearMix = this.getValue(constraintMap, "shearMix", 1); @@ -196,24 +253,49 @@ export class SkeletonJson { for (let j = 0; j < constraintMap.bones.length; j++) { let boneName = constraintMap.bones[j]; let bone = skeletonData.findBone(boneName); - if (bone == null) throw new Error("Transform constraint bone not found: " + boneName); + if (bone == null) + throw new Error( + "Transform constraint bone not found: " + boneName + ); data.bones.push(bone); } let targetName: string = constraintMap.target; data.target = skeletonData.findSlot(targetName); - if (data.target == null) throw new Error("Path target slot not found: " + targetName); - - data.positionMode = SkeletonJson.positionModeFromString(this.getValue(constraintMap, "positionMode", "percent")); - data.spacingMode = SkeletonJson.spacingModeFromString(this.getValue(constraintMap, "spacingMode", "length")); - data.rotateMode = SkeletonJson.rotateModeFromString(this.getValue(constraintMap, "rotateMode", "tangent")); - data.offsetRotation = this.getValue(constraintMap, "rotation", 0); + if (data.target == null) + throw new Error( + "Path target slot not found: " + targetName + ); + + data.positionMode = SkeletonJson.positionModeFromString( + this.getValue(constraintMap, "positionMode", "percent") + ); + data.spacingMode = SkeletonJson.spacingModeFromString( + this.getValue(constraintMap, "spacingMode", "length") + ); + data.rotateMode = SkeletonJson.rotateModeFromString( + this.getValue(constraintMap, "rotateMode", "tangent") + ); + data.offsetRotation = this.getValue( + constraintMap, + "rotation", + 0 + ); data.position = this.getValue(constraintMap, "position", 0); - if (data.positionMode == PositionMode.Fixed) data.position *= scale; + if (data.positionMode == PositionMode.Fixed) + data.position *= scale; data.spacing = this.getValue(constraintMap, "spacing", 0); - if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) data.spacing *= scale; + if ( + data.spacingMode == SpacingMode.Length || + data.spacingMode == SpacingMode.Fixed + ) + data.spacing *= scale; data.rotateMix = this.getValue(constraintMap, "rotateMix", 1); - data.translateMix = this.getValue(constraintMap, "translateMix", 1); + data.translateMix = this.getValue( + constraintMap, + "translateMix", + 1 + ); skeletonData.pathConstraints.push(data); } @@ -222,48 +304,80 @@ export class SkeletonJson { // Skins. if (root.skins) { for (let i = 0; i < root.skins.length; i++) { - let skinMap = root.skins[i] + let skinMap = root.skins[i]; let skin = new Skin(skinMap.name); if (skinMap.bones) { for (let ii = 0; ii < skinMap.bones.length; ii++) { let bone = skeletonData.findBone(skinMap.bones[ii]); - if (bone == null) throw new Error("Skin bone not found: " + skinMap.bones[i]); + if (bone == null) + throw new Error( + "Skin bone not found: " + skinMap.bones[i] + ); skin.bones.push(bone); } } if (skinMap.ik) { for (let ii = 0; ii < skinMap.ik.length; ii++) { - let constraint = skeletonData.findIkConstraint(skinMap.ik[ii]); - if (constraint == null) throw new Error("Skin IK constraint not found: " + skinMap.ik[i]); + let constraint = skeletonData.findIkConstraint( + skinMap.ik[ii] + ); + if (constraint == null) + throw new Error( + "Skin IK constraint not found: " + skinMap.ik[i] + ); skin.constraints.push(constraint); } } if (skinMap.transform) { for (let ii = 0; ii < skinMap.transform.length; ii++) { - let constraint = skeletonData.findTransformConstraint(skinMap.transform[ii]); - if (constraint == null) throw new Error("Skin transform constraint not found: " + skinMap.transform[i]); + let constraint = skeletonData.findTransformConstraint( + skinMap.transform[ii] + ); + if (constraint == null) + throw new Error( + "Skin transform constraint not found: " + + skinMap.transform[i] + ); skin.constraints.push(constraint); } } if (skinMap.path) { for (let ii = 0; ii < skinMap.path.length; ii++) { - let constraint = skeletonData.findPathConstraint(skinMap.path[ii]); - if (constraint == null) throw new Error("Skin path constraint not found: " + skinMap.path[i]); + let constraint = skeletonData.findPathConstraint( + skinMap.path[ii] + ); + if (constraint == null) + throw new Error( + "Skin path constraint not found: " + + skinMap.path[i] + ); skin.constraints.push(constraint); } } for (let slotName in skinMap.attachments) { let slot = skeletonData.findSlot(slotName); - if (slot == null) throw new Error("Slot not found: " + slotName); + if (slot == null) + throw new Error("Slot not found: " + slotName); let slotMap = skinMap.attachments[slotName]; for (let entryName in slotMap) { - let attachment = this.readAttachment(slotMap[entryName], skin, slot.index, entryName, skeletonData); - if (attachment != null) skin.setAttachment(slot.index, entryName, attachment); + let attachment = this.readAttachment( + slotMap[entryName], + skin, + slot.index, + entryName, + skeletonData + ); + if (attachment != null) + skin.setAttachment( + slot.index, + entryName, + attachment + ); } } skeletonData.skins.push(skin); @@ -274,12 +388,22 @@ export class SkeletonJson { // Linked meshes. for (let i = 0, n = this.linkedMeshes.length; i < n; i++) { let linkedMesh = this.linkedMeshes[i]; - let skin = linkedMesh.skin == null ? skeletonData.defaultSkin : skeletonData.findSkin(linkedMesh.skin); - if (skin == null) throw new Error("Skin not found: " + linkedMesh.skin); - let parent = skin.getAttachment(linkedMesh.slotIndex, linkedMesh.parent); - if (parent == null) throw new Error("Parent mesh not found: " + linkedMesh.parent); - linkedMesh.mesh.deformAttachment = linkedMesh.inheritDeform ? parent : linkedMesh.mesh; - linkedMesh.mesh.setParentMesh( parent); + let skin = + linkedMesh.skin == null + ? skeletonData.defaultSkin + : skeletonData.findSkin(linkedMesh.skin); + if (skin == null) + throw new Error("Skin not found: " + linkedMesh.skin); + let parent = skin.getAttachment( + linkedMesh.slotIndex, + linkedMesh.parent + ); + if (parent == null) + throw new Error("Parent mesh not found: " + linkedMesh.parent); + linkedMesh.mesh.deformAttachment = linkedMesh.inheritDeform + ? parent + : linkedMesh.mesh; + linkedMesh.mesh.setParentMesh(parent); // linkedMesh.mesh.updateUVs(); } this.linkedMeshes.length = 0; @@ -312,7 +436,13 @@ export class SkeletonJson { return skeletonData; } - readAttachment (map: any, skin: Skin, slotIndex: number, name: string, skeletonData: SkeletonData): Attachment { + readAttachment( + map: any, + skin: Skin, + slotIndex: number, + name: string, + skeletonData: SkeletonData + ): Attachment { let scale = this.scale; name = this.getValue(map, "name", name); @@ -321,7 +451,11 @@ export class SkeletonJson { switch (type) { case "region": { let path = this.getValue(map, "path", name); - let region = this.attachmentLoader.newRegionAttachment(skin, name, path); + let region = this.attachmentLoader.newRegionAttachment( + skin, + name, + path + ); if (region == null) return null; region.path = path; region.x = this.getValue(map, "x", 0) * scale; @@ -339,7 +473,10 @@ export class SkeletonJson { return region; } case "boundingbox": { - let box = this.attachmentLoader.newBoundingBoxAttachment(skin, name); + let box = this.attachmentLoader.newBoundingBoxAttachment( + skin, + name + ); if (box == null) return null; this.readVertices(map, box, map.vertexCount << 1); let color: string = this.getValue(map, "color", null); @@ -349,7 +486,11 @@ export class SkeletonJson { case "mesh": case "linkedmesh": { let path = this.getValue(map, "path", name); - let mesh = this.attachmentLoader.newMeshAttachment(skin, name, path); + let mesh = this.attachmentLoader.newMeshAttachment( + skin, + name, + path + ); if (mesh == null) return null; mesh.path = path; @@ -361,7 +502,15 @@ export class SkeletonJson { let parent: string = this.getValue(map, "parent", null); if (parent != null) { - this.linkedMeshes.push(new LinkedMesh(mesh, this.getValue(map, "skin", null), slotIndex, parent, this.getValue(map, "deform", true))); + this.linkedMeshes.push( + new LinkedMesh( + mesh, + this.getValue(map, "skin", null), + slotIndex, + parent, + this.getValue(map, "deform", true) + ) + ); return mesh; } @@ -394,7 +543,10 @@ export class SkeletonJson { return path; } case "point": { - let point = this.attachmentLoader.newPointAttachment(skin, name); + let point = this.attachmentLoader.newPointAttachment( + skin, + name + ); if (point == null) return null; point.x = this.getValue(map, "x", 0) * scale; point.y = this.getValue(map, "y", 0) * scale; @@ -405,13 +557,17 @@ export class SkeletonJson { return point; } case "clipping": { - let clip = this.attachmentLoader.newClippingAttachment(skin, name); + let clip = this.attachmentLoader.newClippingAttachment( + skin, + name + ); if (clip == null) return null; let end = this.getValue(map, "end", null); if (end != null) { let slot = skeletonData.findSlot(end); - if (slot == null) throw new Error("Clipping end slot not found: " + end); + if (slot == null) + throw new Error("Clipping end slot not found: " + end); clip.endSlot = slot; } @@ -426,7 +582,11 @@ export class SkeletonJson { return null; } - readVertices (map: any, attachment: VertexAttachment, verticesLength: number) { + readVertices( + map: any, + attachment: VertexAttachment, + verticesLength: number + ) { let scale = this.scale; attachment.worldVerticesLength = verticesLength; let vertices: Array = map.vertices; @@ -441,7 +601,7 @@ export class SkeletonJson { } let weights = new Array(); let bones = new Array(); - for (let i = 0, n = vertices.length; i < n;) { + for (let i = 0, n = vertices.length; i < n; ) { let boneCount = vertices[i++]; bones.push(boneCount); for (let nn = i + boneCount * 4; i < nn; i += 4) { @@ -455,7 +615,7 @@ export class SkeletonJson { attachment.vertices = Utils.toFloatArray(weights); } - readAnimation (map: any, name: string, skeletonData: SkeletonData) { + readAnimation(map: any, name: string, skeletonData: SkeletonData) { let scale = this.scale; let timelines = new Array(); let duration = 0; @@ -465,20 +625,30 @@ export class SkeletonJson { for (let slotName in map.slots) { let slotMap = map.slots[slotName]; let slotIndex = skeletonData.findSlotIndex(slotName); - if (slotIndex == -1) throw new Error("Slot not found: " + slotName); + if (slotIndex == -1) + throw new Error("Slot not found: " + slotName); for (let timelineName in slotMap) { let timelineMap = slotMap[timelineName]; if (timelineName == "attachment") { - let timeline = new AttachmentTimeline(timelineMap.length); + let timeline = new AttachmentTimeline( + timelineMap.length + ); timeline.slotIndex = slotIndex; let frameIndex = 0; for (let i = 0; i < timelineMap.length; i++) { let valueMap = timelineMap[i]; - timeline.setFrame(frameIndex++, this.getValue(valueMap, "time", 0), valueMap.name); + timeline.setFrame( + frameIndex++, + this.getValue(valueMap, "time", 0), + valueMap.name + ); } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[timeline.getFrameCount() - 1]); + duration = Math.max( + duration, + timeline.frames[timeline.getFrameCount() - 1] + ); } else if (timelineName == "color") { let timeline = new ColorTimeline(timelineMap.length); timeline.slotIndex = slotIndex; @@ -488,13 +658,25 @@ export class SkeletonJson { let valueMap = timelineMap[i]; let color = new Color(); color.setFromString(valueMap.color || "ffffffff"); - timeline.setFrame(frameIndex, this.getValue(valueMap, "time", 0), color.r, color.g, color.b, color.a); + timeline.setFrame( + frameIndex, + this.getValue(valueMap, "time", 0), + color.r, + color.g, + color.b, + color.a + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(timeline.getFrameCount() - 1) * ColorTimeline.ENTRIES]); - + duration = Math.max( + duration, + timeline.frames[ + (timeline.getFrameCount() - 1) * + ColorTimeline.ENTRIES + ] + ); } else if (timelineName == "twoColor") { let timeline = new TwoColorTimeline(timelineMap.length); timeline.slotIndex = slotIndex; @@ -506,15 +688,36 @@ export class SkeletonJson { let dark = new Color(); light.setFromString(valueMap.light); dark.setFromString(valueMap.dark); - timeline.setFrame(frameIndex, this.getValue(valueMap, "time", 0), light.r, light.g, light.b, light.a, dark.r, dark.g, dark.b); + timeline.setFrame( + frameIndex, + this.getValue(valueMap, "time", 0), + light.r, + light.g, + light.b, + light.a, + dark.r, + dark.g, + dark.b + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(timeline.getFrameCount() - 1) * TwoColorTimeline.ENTRIES]); - + duration = Math.max( + duration, + timeline.frames[ + (timeline.getFrameCount() - 1) * + TwoColorTimeline.ENTRIES + ] + ); } else - throw new Error("Invalid timeline type for a slot: " + timelineName + " (" + slotName + ")"); + throw new Error( + "Invalid timeline type for a slot: " + + timelineName + + " (" + + slotName + + ")" + ); } } } @@ -524,7 +727,8 @@ export class SkeletonJson { for (let boneName in map.bones) { let boneMap = map.bones[boneName]; let boneIndex = skeletonData.findBoneIndex(boneName); - if (boneIndex == -1) throw new Error("Bone not found: " + boneName); + if (boneIndex == -1) + throw new Error("Bone not found: " + boneName); for (let timelineName in boneMap) { let timelineMap = boneMap[timelineName]; if (timelineName === "rotate") { @@ -534,23 +738,39 @@ export class SkeletonJson { let frameIndex = 0; for (let i = 0; i < timelineMap.length; i++) { let valueMap = timelineMap[i]; - timeline.setFrame(frameIndex, this.getValue(valueMap, "time", 0), this.getValue(valueMap, "angle", 0)); + timeline.setFrame( + frameIndex, + this.getValue(valueMap, "time", 0), + this.getValue(valueMap, "angle", 0) + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(timeline.getFrameCount() - 1) * RotateTimeline.ENTRIES]); - - } else if (timelineName === "translate" || timelineName === "scale" || timelineName === "shear") { + duration = Math.max( + duration, + timeline.frames[ + (timeline.getFrameCount() - 1) * + RotateTimeline.ENTRIES + ] + ); + } else if ( + timelineName === "translate" || + timelineName === "scale" || + timelineName === "shear" + ) { let timeline: TranslateTimeline = null; - let timelineScale = 1, defaultValue = 0; + let timelineScale = 1, + defaultValue = 0; if (timelineName === "scale") { timeline = new ScaleTimeline(timelineMap.length); defaultValue = 1; } else if (timelineName === "shear") timeline = new ShearTimeline(timelineMap.length); else { - timeline = new TranslateTimeline(timelineMap.length); + timeline = new TranslateTimeline( + timelineMap.length + ); timelineScale = scale; } timeline.boneIndex = boneIndex; @@ -558,16 +778,33 @@ export class SkeletonJson { let frameIndex = 0; for (let i = 0; i < timelineMap.length; i++) { let valueMap = timelineMap[i]; - let x = this.getValue(valueMap, "x", defaultValue), y = this.getValue(valueMap, "y", defaultValue); - timeline.setFrame(frameIndex, this.getValue(valueMap, "time", 0), x * timelineScale, y * timelineScale); + let x = this.getValue(valueMap, "x", defaultValue), + y = this.getValue(valueMap, "y", defaultValue); + timeline.setFrame( + frameIndex, + this.getValue(valueMap, "time", 0), + x * timelineScale, + y * timelineScale + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(timeline.getFrameCount() - 1) * TranslateTimeline.ENTRIES]); - + duration = Math.max( + duration, + timeline.frames[ + (timeline.getFrameCount() - 1) * + TranslateTimeline.ENTRIES + ] + ); } else - throw new Error("Invalid timeline type for a bone: " + timelineName + " (" + boneName + ")"); + throw new Error( + "Invalid timeline type for a bone: " + + timelineName + + " (" + + boneName + + ")" + ); } } } @@ -578,17 +815,31 @@ export class SkeletonJson { let constraintMap = map.ik[constraintName]; let constraint = skeletonData.findIkConstraint(constraintName); let timeline = new IkConstraintTimeline(constraintMap.length); - timeline.ikConstraintIndex = skeletonData.ikConstraints.indexOf(constraint); + timeline.ikConstraintIndex = + skeletonData.ikConstraints.indexOf(constraint); let frameIndex = 0; for (let i = 0; i < constraintMap.length; i++) { let valueMap = constraintMap[i]; - timeline.setFrame(frameIndex, this.getValue(valueMap, "time", 0), this.getValue(valueMap, "mix", 1), this.getValue(valueMap, "softness", 0) * scale, - this.getValue(valueMap, "bendPositive", true) ? 1 : -1, this.getValue(valueMap, "compress", false), this.getValue(valueMap, "stretch", false)); + timeline.setFrame( + frameIndex, + this.getValue(valueMap, "time", 0), + this.getValue(valueMap, "mix", 1), + this.getValue(valueMap, "softness", 0) * scale, + this.getValue(valueMap, "bendPositive", true) ? 1 : -1, + this.getValue(valueMap, "compress", false), + this.getValue(valueMap, "stretch", false) + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[(timeline.getFrameCount() - 1) * IkConstraintTimeline.ENTRIES]); + duration = Math.max( + duration, + timeline.frames[ + (timeline.getFrameCount() - 1) * + IkConstraintTimeline.ENTRIES + ] + ); } } @@ -596,20 +847,35 @@ export class SkeletonJson { if (map.transform) { for (let constraintName in map.transform) { let constraintMap = map.transform[constraintName]; - let constraint = skeletonData.findTransformConstraint(constraintName); - let timeline = new TransformConstraintTimeline(constraintMap.length); - timeline.transformConstraintIndex = skeletonData.transformConstraints.indexOf(constraint); + let constraint = + skeletonData.findTransformConstraint(constraintName); + let timeline = new TransformConstraintTimeline( + constraintMap.length + ); + timeline.transformConstraintIndex = + skeletonData.transformConstraints.indexOf(constraint); let frameIndex = 0; for (let i = 0; i < constraintMap.length; i++) { let valueMap = constraintMap[i]; - timeline.setFrame(frameIndex, this.getValue(valueMap, "time", 0), this.getValue(valueMap, "rotateMix", 1), - this.getValue(valueMap, "translateMix", 1), this.getValue(valueMap, "scaleMix", 1), this.getValue(valueMap, "shearMix", 1)); + timeline.setFrame( + frameIndex, + this.getValue(valueMap, "time", 0), + this.getValue(valueMap, "rotateMix", 1), + this.getValue(valueMap, "translateMix", 1), + this.getValue(valueMap, "scaleMix", 1), + this.getValue(valueMap, "shearMix", 1) + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, - timeline.frames[(timeline.getFrameCount() - 1) * TransformConstraintTimeline.ENTRIES]); + duration = Math.max( + duration, + timeline.frames[ + (timeline.getFrameCount() - 1) * + TransformConstraintTimeline.ENTRIES + ] + ); } } @@ -617,46 +883,83 @@ export class SkeletonJson { if (map.path) { for (let constraintName in map.path) { let constraintMap = map.path[constraintName]; - let index = skeletonData.findPathConstraintIndex(constraintName); - if (index == -1) throw new Error("Path constraint not found: " + constraintName); + let index = + skeletonData.findPathConstraintIndex(constraintName); + if (index == -1) + throw new Error( + "Path constraint not found: " + constraintName + ); let data = skeletonData.pathConstraints[index]; for (let timelineName in constraintMap) { let timelineMap = constraintMap[timelineName]; - if (timelineName === "position" || timelineName === "spacing") { + if ( + timelineName === "position" || + timelineName === "spacing" + ) { let timeline: PathConstraintPositionTimeline = null; let timelineScale = 1; if (timelineName === "spacing") { - timeline = new PathConstraintSpacingTimeline(timelineMap.length); - if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) timelineScale = scale; + timeline = new PathConstraintSpacingTimeline( + timelineMap.length + ); + if ( + data.spacingMode == SpacingMode.Length || + data.spacingMode == SpacingMode.Fixed + ) + timelineScale = scale; } else { - timeline = new PathConstraintPositionTimeline(timelineMap.length); - if (data.positionMode == PositionMode.Fixed) timelineScale = scale; + timeline = new PathConstraintPositionTimeline( + timelineMap.length + ); + if (data.positionMode == PositionMode.Fixed) + timelineScale = scale; } timeline.pathConstraintIndex = index; let frameIndex = 0; for (let i = 0; i < timelineMap.length; i++) { let valueMap = timelineMap[i]; - timeline.setFrame(frameIndex, this.getValue(valueMap, "time", 0), this.getValue(valueMap, timelineName, 0) * timelineScale); + timeline.setFrame( + frameIndex, + this.getValue(valueMap, "time", 0), + this.getValue(valueMap, timelineName, 0) * + timelineScale + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, - timeline.frames[(timeline.getFrameCount() - 1) * PathConstraintPositionTimeline.ENTRIES]); + duration = Math.max( + duration, + timeline.frames[ + (timeline.getFrameCount() - 1) * + PathConstraintPositionTimeline.ENTRIES + ] + ); } else if (timelineName === "mix") { - let timeline = new PathConstraintMixTimeline(timelineMap.length); + let timeline = new PathConstraintMixTimeline( + timelineMap.length + ); timeline.pathConstraintIndex = index; let frameIndex = 0; for (let i = 0; i < timelineMap.length; i++) { let valueMap = timelineMap[i]; - timeline.setFrame(frameIndex, this.getValue(valueMap, "time", 0), this.getValue(valueMap, "rotateMix", 1), - this.getValue(valueMap, "translateMix", 1)); + timeline.setFrame( + frameIndex, + this.getValue(valueMap, "time", 0), + this.getValue(valueMap, "rotateMix", 1), + this.getValue(valueMap, "translateMix", 1) + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, - timeline.frames[(timeline.getFrameCount() - 1) * PathConstraintMixTimeline.ENTRIES]); + duration = Math.max( + duration, + timeline.frames[ + (timeline.getFrameCount() - 1) * + PathConstraintMixTimeline.ENTRIES + ] + ); } } } @@ -668,23 +971,32 @@ export class SkeletonJson { let deformMap = map.deform[deformName]; let skin = skeletonData.findSkin(deformName); if (skin == null) { - if (settings.FAIL_ON_NON_EXISTING_SKIN) { - throw new Error("Skin not found: " + deformName); - } else { - continue; - } + if (settings.FAIL_ON_NON_EXISTING_SKIN) { + throw new Error("Skin not found: " + deformName); + } else { + continue; + } } for (let slotName in deformMap) { let slotMap = deformMap[slotName]; let slotIndex = skeletonData.findSlotIndex(slotName); - if (slotIndex == -1) throw new Error("Slot not found: " + slotMap.name); + if (slotIndex == -1) + throw new Error("Slot not found: " + slotMap.name); for (let timelineName in slotMap) { let timelineMap = slotMap[timelineName]; - let attachment = skin.getAttachment(slotIndex, timelineName); - if (attachment == null) throw new Error("Deform attachment not found: " + timelineMap.name); + let attachment = ( + skin.getAttachment(slotIndex, timelineName) + ); + if (attachment == null) + throw new Error( + "Deform attachment not found: " + + timelineMap.name + ); let weighted = attachment.bones != null; let vertices = attachment.vertices; - let deformLength = weighted ? vertices.length / 3 * 2 : vertices.length; + let deformLength = weighted + ? (vertices.length / 3) * 2 + : vertices.length; let timeline = new DeformTimeline(timelineMap.length); timeline.slotIndex = slotIndex; @@ -694,15 +1006,34 @@ export class SkeletonJson { for (let j = 0; j < timelineMap.length; j++) { let valueMap = timelineMap[j]; let deform: ArrayLike; - let verticesValue: Array = this.getValue(valueMap, "vertices", null); + let verticesValue: Array = this.getValue( + valueMap, + "vertices", + null + ); if (verticesValue == null) - deform = weighted ? Utils.newFloatArray(deformLength) : vertices; + deform = weighted + ? Utils.newFloatArray(deformLength) + : vertices; else { deform = Utils.newFloatArray(deformLength); - let start = this.getValue(valueMap, "offset", 0); - Utils.arrayCopy(verticesValue, 0, deform, start, verticesValue.length); + let start = ( + this.getValue(valueMap, "offset", 0) + ); + Utils.arrayCopy( + verticesValue, + 0, + deform, + start, + verticesValue.length + ); if (scale != 1) { - for (let i = start, n = i + verticesValue.length; i < n; i++) + for ( + let i = start, + n = i + verticesValue.length; + i < n; + i++ + ) deform[i] *= scale; } if (!weighted) { @@ -711,12 +1042,19 @@ export class SkeletonJson { } } - timeline.setFrame(frameIndex, this.getValue(valueMap, "time", 0), deform); + timeline.setFrame( + frameIndex, + this.getValue(valueMap, "time", 0), + deform + ); this.readCurve(valueMap, timeline, frameIndex); frameIndex++; } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[timeline.getFrameCount() - 1]); + duration = Math.max( + duration, + timeline.frames[timeline.getFrameCount() - 1] + ); } } } @@ -735,29 +1073,47 @@ export class SkeletonJson { let offsets = this.getValue(drawOrderMap, "offsets", null); if (offsets != null) { drawOrder = Utils.newArray(slotCount, -1); - let unchanged = Utils.newArray(slotCount - offsets.length, 0); - let originalIndex = 0, unchangedIndex = 0; + let unchanged = Utils.newArray( + slotCount - offsets.length, + 0 + ); + let originalIndex = 0, + unchangedIndex = 0; for (let i = 0; i < offsets.length; i++) { let offsetMap = offsets[i]; - let slotIndex = skeletonData.findSlotIndex(offsetMap.slot); - if (slotIndex == -1) throw new Error("Slot not found: " + offsetMap.slot); + let slotIndex = skeletonData.findSlotIndex( + offsetMap.slot + ); + if (slotIndex == -1) + throw new Error( + "Slot not found: " + offsetMap.slot + ); // Collect unchanged items. while (originalIndex != slotIndex) unchanged[unchangedIndex++] = originalIndex++; // Set changed items. - drawOrder[originalIndex + offsetMap.offset] = originalIndex++; + drawOrder[originalIndex + offsetMap.offset] = + originalIndex++; } // Collect remaining unchanged items. while (originalIndex < slotCount) unchanged[unchangedIndex++] = originalIndex++; // Fill in unchanged items. for (let i = slotCount - 1; i >= 0; i--) - if (drawOrder[i] == -1) drawOrder[i] = unchanged[--unchangedIndex]; + if (drawOrder[i] == -1) + drawOrder[i] = unchanged[--unchangedIndex]; } - timeline.setFrame(frameIndex++, this.getValue(drawOrderMap, "time", 0), drawOrder); + timeline.setFrame( + frameIndex++, + this.getValue(drawOrderMap, "time", 0), + drawOrder + ); } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[timeline.getFrameCount() - 1]); + duration = Math.max( + duration, + timeline.frames[timeline.getFrameCount() - 1] + ); } // Event timeline. @@ -767,11 +1123,27 @@ export class SkeletonJson { for (let i = 0; i < map.events.length; i++) { let eventMap = map.events[i]; let eventData = skeletonData.findEvent(eventMap.name); - if (eventData == null) throw new Error("Event not found: " + eventMap.name); - let event = new Event(Utils.toSinglePrecision(this.getValue(eventMap, "time", 0)), eventData); - event.intValue = this.getValue(eventMap, "int", eventData.intValue); - event.floatValue = this.getValue(eventMap, "float", eventData.floatValue); - event.stringValue = this.getValue(eventMap, "string", eventData.stringValue); + if (eventData == null) + throw new Error("Event not found: " + eventMap.name); + let event = new Event( + Utils.toSinglePrecision(this.getValue(eventMap, "time", 0)), + eventData + ); + event.intValue = this.getValue( + eventMap, + "int", + eventData.intValue + ); + event.floatValue = this.getValue( + eventMap, + "float", + eventData.floatValue + ); + event.stringValue = this.getValue( + eventMap, + "string", + eventData.stringValue + ); if (event.data.audioPath != null) { event.volume = this.getValue(eventMap, "volume", 1); event.balance = this.getValue(eventMap, "balance", 0); @@ -779,7 +1151,10 @@ export class SkeletonJson { timeline.setFrame(frameIndex++, event); } timelines.push(timeline); - duration = Math.max(duration, timeline.frames[timeline.getFrameCount() - 1]); + duration = Math.max( + duration, + timeline.frames[timeline.getFrameCount() - 1] + ); } if (isNaN(duration)) { @@ -789,37 +1164,42 @@ export class SkeletonJson { skeletonData.animations.push(new Animation(name, timelines, duration)); } - readCurve (map: any, timeline: CurveTimeline, frameIndex: number) { + readCurve(map: any, timeline: CurveTimeline, frameIndex: number) { if (!map.hasOwnProperty("curve")) return; - if (map.curve === "stepped") - timeline.setStepped(frameIndex); + if (map.curve === "stepped") timeline.setStepped(frameIndex); else { let curve: number = map.curve; - timeline.setCurve(frameIndex, curve, this.getValue(map, "c2", 0), this.getValue(map, "c3", 1), this.getValue(map, "c4", 1)); + timeline.setCurve( + frameIndex, + curve, + this.getValue(map, "c2", 0), + this.getValue(map, "c3", 1), + this.getValue(map, "c4", 1) + ); } } - getValue (map: any, prop: string, defaultValue: any) { + getValue(map: any, prop: string, defaultValue: any) { return map[prop] !== undefined ? map[prop] : defaultValue; } - static blendModeFromString (str: string) { + static blendModeFromString(str: string) { str = str.toLowerCase(); - if (str == "normal") return BLEND_MODES.NORMAL; - if (str == "additive") return BLEND_MODES.ADD; - if (str == "multiply") return BLEND_MODES.MULTIPLY; - if (str == "screen") return BLEND_MODES.SCREEN; + if (str == "normal") return PIXI.BLEND_MODES.NORMAL; + if (str == "additive") return PIXI.BLEND_MODES.ADD; + if (str == "multiply") return PIXI.BLEND_MODES.MULTIPLY; + if (str == "screen") return PIXI.BLEND_MODES.SCREEN; throw new Error(`Unknown blend mode: ${str}`); } - static positionModeFromString (str: string) { + static positionModeFromString(str: string) { str = str.toLowerCase(); if (str == "fixed") return PositionMode.Fixed; if (str == "percent") return PositionMode.Percent; throw new Error(`Unknown position mode: ${str}`); } - static spacingModeFromString (str: string) { + static spacingModeFromString(str: string) { str = str.toLowerCase(); if (str == "length") return SpacingMode.Length; if (str == "fixed") return SpacingMode.Fixed; @@ -827,7 +1207,7 @@ export class SkeletonJson { throw new Error(`Unknown position mode: ${str}`); } - static rotateModeFromString (str: string) { + static rotateModeFromString(str: string) { str = str.toLowerCase(); if (str == "tangent") return RotateMode.Tangent; if (str == "chain") return RotateMode.Chain; @@ -839,20 +1219,29 @@ export class SkeletonJson { str = str.toLowerCase(); if (str == "normal") return TransformMode.Normal; if (str == "onlytranslation") return TransformMode.OnlyTranslation; - if (str == "norotationorreflection") return TransformMode.NoRotationOrReflection; + if (str == "norotationorreflection") + return TransformMode.NoRotationOrReflection; if (str == "noscale") return TransformMode.NoScale; - if (str == "noscaleorreflection") return TransformMode.NoScaleOrReflection; + if (str == "noscaleorreflection") + return TransformMode.NoScaleOrReflection; throw new Error(`Unknown transform mode: ${str}`); } } class LinkedMesh { - parent: string; skin: string; + parent: string; + skin: string; slotIndex: number; mesh: MeshAttachment; inheritDeform: boolean; - constructor (mesh: MeshAttachment, skin: string, slotIndex: number, parent: string, inheritDeform: boolean) { + constructor( + mesh: MeshAttachment, + skin: string, + slotIndex: number, + parent: string, + inheritDeform: boolean + ) { this.mesh = mesh; this.skin = skin; this.slotIndex = slotIndex; diff --git a/packages/runtime-3.8/src/core/SlotData.ts b/packages/runtime-3.8/src/core/SlotData.ts index 9a40db86..355f4fb3 100644 --- a/packages/runtime-3.8/src/core/SlotData.ts +++ b/packages/runtime-3.8/src/core/SlotData.ts @@ -1,8 +1,7 @@ -import {Color} from '@pixi-spine/base'; +import { Color } from "@pixi-spine/base"; -import type {ISlotData} from '@pixi-spine/base'; -import type {BLEND_MODES} from '@pixi/constants'; -import {BoneData} from "./BoneData"; +import type { ISlotData } from "@pixi-spine/base"; +import { BoneData } from "./BoneData"; /** * @public @@ -14,9 +13,9 @@ export class SlotData implements ISlotData { color = new Color(1, 1, 1, 1); darkColor: Color; attachmentName: string; - blendMode: BLEND_MODES; + blendMode: PIXI.BLEND_MODES; - constructor (index: number, name: string, boneData: BoneData) { + constructor(index: number, name: string, boneData: BoneData) { if (index < 0) throw new Error("index must be >= 0."); if (name == null) throw new Error("name cannot be null."); if (boneData == null) throw new Error("boneData cannot be null."); diff --git a/packages/runtime-4.0/package.json b/packages/runtime-4.0/package.json index 14b8b79f..0437597f 100644 --- a/packages/runtime-4.0/package.json +++ b/packages/runtime-4.0/package.json @@ -8,8 +8,7 @@ "namespace": "PIXI.spine40", "types": "./index.d.ts", "peerDependencies": { - "@pixi/constants": "^6.1.0", - "@pixi/math": "^6.1.0" + "pixi.js-legacy": "5.3.8" }, "dependencies": { "@pixi-spine/base": "~3.0.15" diff --git a/packages/runtime-4.0/src/core/Bone.ts b/packages/runtime-4.0/src/core/Bone.ts index 4c8d2fee..81a1e870 100644 --- a/packages/runtime-4.0/src/core/Bone.ts +++ b/packages/runtime-4.0/src/core/Bone.ts @@ -1,8 +1,13 @@ -import {Matrix} from '@pixi/math'; -import {Updatable} from "./Updatable"; -import {BoneData} from "./BoneData"; -import {Skeleton} from "./Skeleton"; -import {IBone, MathUtils, settings, TransformMode, Vector2} from "@pixi-spine/base"; +import { Updatable } from "./Updatable"; +import { BoneData } from "./BoneData"; +import { Skeleton } from "./Skeleton"; +import { + IBone, + MathUtils, + settings, + TransformMode, + Vector2, +} from "@pixi-spine/base"; /** Stores a bone's current pose. * @@ -13,7 +18,7 @@ import {IBone, MathUtils, settings, TransformMode, Vector2} from "@pixi-spine/ba * */ export class Bone implements Updatable, IBone { //be careful! Spine b,c is c,b in pixi matrix - matrix = new Matrix(); + matrix = new PIXI.Matrix(); get worldX(): number { return this.matrix.tx; @@ -81,7 +86,7 @@ export class Bone implements Updatable, IBone { active = false; /** @param parent May be null. */ - constructor (data: BoneData, skeleton: Skeleton, parent: Bone) { + constructor(data: BoneData, skeleton: Skeleton, parent: Bone) { if (!data) throw new Error("data cannot be null."); if (!skeleton) throw new Error("skeleton cannot be null."); this.data = data; @@ -92,20 +97,36 @@ export class Bone implements Updatable, IBone { /** Returns false when the bone has not been computed because {@link BoneData#skinRequired} is true and the * {@link Skeleton#skin active skin} does not {@link Skin#bones contain} this bone. */ - isActive () { + isActive() { return this.active; } /** Computes the world transform using the parent bone and this bone's local applied transform. */ - update () { - this.updateWorldTransformWith(this.ax, this.ay, this.arotation, this.ascaleX, this.ascaleY, this.ashearX, this.ashearY); + update() { + this.updateWorldTransformWith( + this.ax, + this.ay, + this.arotation, + this.ascaleX, + this.ascaleY, + this.ashearX, + this.ashearY + ); } /** Computes the world transform using the parent bone and this bone's local transform. * * See {@link #updateWorldTransformWith()}. */ - updateWorldTransform () { - this.updateWorldTransformWith(this.x, this.y, this.rotation, this.scaleX, this.scaleY, this.shearX, this.shearY); + updateWorldTransform() { + this.updateWorldTransformWith( + this.x, + this.y, + this.rotation, + this.scaleX, + this.scaleY, + this.shearX, + this.shearY + ); } /** Computes the world transform using the parent bone and the specified local transform. The applied transform is set to the @@ -113,7 +134,15 @@ export class Bone implements Updatable, IBone { * * See [World transforms](http://esotericsoftware.com/spine-runtime-skeletons#World-transforms) in the Spine * Runtimes Guide. */ - updateWorldTransformWith (x: number, y: number, rotation: number, scaleX: number, scaleY: number, shearX: number, shearY: number) { + updateWorldTransformWith( + x: number, + y: number, + rotation: number, + scaleX: number, + scaleY: number, + shearX: number, + shearY: number + ) { this.ax = x; this.ay = y; this.arotation = rotation; @@ -126,8 +155,9 @@ export class Bone implements Updatable, IBone { let m = this.matrix; let sx = this.skeleton.scaleX; - let sy = settings.yDown? -this.skeleton.scaleY : this.skeleton.scaleY; - if (!parent) { // Root bone. + let sy = settings.yDown ? -this.skeleton.scaleY : this.skeleton.scaleY; + if (!parent) { + // Root bone. let skeleton = this.skeleton; let rotationY = rotation + 90 + shearY; m.a = MathUtils.cosDeg(rotation + shearX) * scaleX * sx; @@ -139,7 +169,10 @@ export class Bone implements Updatable, IBone { return; } - let pa = parent.matrix.a, pb = parent.matrix.c, pc = parent.matrix.b, pd = parent.matrix.d; + let pa = parent.matrix.a, + pb = parent.matrix.c, + pc = parent.matrix.b, + pd = parent.matrix.d; m.tx = pa * x + pb * y + parent.matrix.tx; m.ty = pc * x + pd * y + parent.matrix.ty; @@ -203,11 +236,15 @@ export class Bone implements Updatable, IBone { zc *= s; s = Math.sqrt(za * za + zc * zc); if ( - this.data.transformMode == TransformMode.NoScale - && (pa * pd - pb * pc < 0) != (settings.yDown? - (this.skeleton.scaleX < 0 != this.skeleton.scaleY > 0) : - (this.skeleton.scaleX < 0 != this.skeleton.scaleY < 0)) - ) s = -s; + this.data.transformMode == TransformMode.NoScale && + pa * pd - pb * pc < 0 != + (settings.yDown + ? this.skeleton.scaleX < 0 != + this.skeleton.scaleY > 0 + : this.skeleton.scaleX < 0 != + this.skeleton.scaleY < 0) + ) + s = -s; let r = Math.PI / 2 + Math.atan2(zc, za); let zb = Math.cos(r) * s; let zd = Math.sin(r) * s; @@ -229,7 +266,7 @@ export class Bone implements Updatable, IBone { } /** Sets this bone's local transform to the setup pose. */ - setToSetupPose () { + setToSetupPose() { let data = this.data; this.x = data.x; this.y = data.y; @@ -241,23 +278,23 @@ export class Bone implements Updatable, IBone { } /** The world rotation for the X axis, calculated using {@link #a} and {@link #c}. */ - getWorldRotationX () { + getWorldRotationX() { return Math.atan2(this.matrix.b, this.matrix.a) * MathUtils.radDeg; } /** The world rotation for the Y axis, calculated using {@link #b} and {@link #d}. */ - getWorldRotationY () { + getWorldRotationY() { return Math.atan2(this.matrix.d, this.matrix.c) * MathUtils.radDeg; } /** The magnitude (always positive) of the world scale X, calculated using {@link #a} and {@link #c}. */ - getWorldScaleX () { + getWorldScaleX() { let m = this.matrix; return Math.sqrt(m.a * m.a + m.b * m.b); } /** The magnitude (always positive) of the world scale Y, calculated using {@link #b} and {@link #d}. */ - getWorldScaleY () { + getWorldScaleY() { let m = this.matrix; return Math.sqrt(m.c * m.c + m.d * m.d); } @@ -270,7 +307,7 @@ export class Bone implements Updatable, IBone { * * Some information is ambiguous in the world transform, such as -1,-1 scale versus 180 rotation. The applied transform after * calling this method is equivalent to the local transform used to compute the world transform, but may not be identical. */ - updateAppliedTransform () { + updateAppliedTransform() { let parent = this.parent; let m = this.matrix; if (!parent) { @@ -280,14 +317,17 @@ export class Bone implements Updatable, IBone { this.ascaleX = Math.sqrt(m.a * m.a + m.b * m.b); this.ascaleY = Math.sqrt(m.c * m.c + m.d * m.d); this.ashearX = 0; - this.ashearY = Math.atan2(m.a * m.c + m.b * m.d, m.a * m.d - m.b * m.c) * MathUtils.radDeg; + this.ashearY = + Math.atan2(m.a * m.c + m.b * m.d, m.a * m.d - m.b * m.c) * + MathUtils.radDeg; return; } let pm = parent.matrix; let pid = 1 / (pm.a * pm.d - pm.b * pm.c); - let dx = m.tx - pm.tx, dy = m.ty - pm.ty; - this.ax = (dx * pm.d * pid - dy * pm.c * pid); - this.ay = (dy * pm.a * pid - dx * pm.b * pid); + let dx = m.tx - pm.tx, + dy = m.ty - pm.ty; + this.ax = dx * pm.d * pid - dy * pm.c * pid; + this.ay = dy * pm.a * pid - dx * pm.b * pid; let ia = pid * pm.d; let id = pid * pm.a; let ib = pid * pm.c; @@ -301,7 +341,8 @@ export class Bone implements Updatable, IBone { if (this.ascaleX > 0.0001) { let det = ra * rd - rb * rc; this.ascaleY = det / this.ascaleX; - this.ashearY = Math.atan2(ra * rb + rc * rd, det) * MathUtils.radDeg; + this.ashearY = + Math.atan2(ra * rb + rc * rd, det) * MathUtils.radDeg; this.arotation = Math.atan2(rc, ra) * MathUtils.radDeg; } else { this.ascaleX = 0; @@ -314,46 +355,63 @@ export class Bone implements Updatable, IBone { /** Transforms a point from world coordinates to the bone's local coordinates. */ worldToLocal(world: Vector2) { let m = this.matrix; - let a = m.a, b = m.c, c = m.b, d = m.d; + let a = m.a, + b = m.c, + c = m.b, + d = m.d; let invDet = 1 / (a * d - b * c); - let x = world.x - m.tx, y = world.y - m.ty; - world.x = (x * d * invDet - y * b * invDet); - world.y = (y * a * invDet - x * c * invDet); + let x = world.x - m.tx, + y = world.y - m.ty; + world.x = x * d * invDet - y * b * invDet; + world.y = y * a * invDet - x * c * invDet; return world; } /** Transforms a point from the bone's local coordinates to world coordinates. */ localToWorld(local: Vector2) { let m = this.matrix; - let x = local.x, y = local.y; + let x = local.x, + y = local.y; local.x = x * m.a + y * m.c + m.tx; local.y = x * m.b + y * m.d + m.ty; return local; } /** Transforms a world rotation to a local rotation. */ - worldToLocalRotation (worldRotation: number) { - let sin = MathUtils.sinDeg(worldRotation), cos = MathUtils.cosDeg(worldRotation); + worldToLocalRotation(worldRotation: number) { + let sin = MathUtils.sinDeg(worldRotation), + cos = MathUtils.cosDeg(worldRotation); let mat = this.matrix; - return Math.atan2(mat.a * sin - mat.b * cos, mat.d * cos - mat.c * sin) * MathUtils.radDeg; + return ( + Math.atan2(mat.a * sin - mat.b * cos, mat.d * cos - mat.c * sin) * + MathUtils.radDeg + ); } /** Transforms a local rotation to a world rotation. */ - localToWorldRotation (localRotation: number) { + localToWorldRotation(localRotation: number) { localRotation -= this.rotation - this.shearX; - let sin = MathUtils.sinDeg(localRotation), cos = MathUtils.cosDeg(localRotation); + let sin = MathUtils.sinDeg(localRotation), + cos = MathUtils.cosDeg(localRotation); let mat = this.matrix; - return Math.atan2(cos * mat.b + sin * mat.d, cos * mat.a + sin * mat.c) * MathUtils.radDeg; + return ( + Math.atan2(cos * mat.b + sin * mat.d, cos * mat.a + sin * mat.c) * + MathUtils.radDeg + ); } /** Rotates the world transform the specified amount. *

* After changes are made to the world transform, {@link #updateAppliedTransform()} should be called and {@link #update()} will * need to be called on any child bones, recursively. */ - rotateWorld (degrees: number) { + rotateWorld(degrees: number) { let mat = this.matrix; - let a = mat.a, b = mat.c, c = mat.b, d = mat.d; - let cos = MathUtils.cosDeg(degrees), sin = MathUtils.sinDeg(degrees); + let a = mat.a, + b = mat.c, + c = mat.b, + d = mat.d; + let cos = MathUtils.cosDeg(degrees), + sin = MathUtils.sinDeg(degrees); mat.a = cos * a - sin * c; mat.c = cos * b - sin * d; mat.b = sin * a + cos * c; diff --git a/packages/runtime-4.0/src/core/SkeletonBinary.ts b/packages/runtime-4.0/src/core/SkeletonBinary.ts index 494291cc..c619119d 100644 --- a/packages/runtime-4.0/src/core/SkeletonBinary.ts +++ b/packages/runtime-4.0/src/core/SkeletonBinary.ts @@ -1,29 +1,56 @@ -import type {Attachment, AttachmentLoader, MeshAttachment, VertexAttachment} from './attachments'; +import type { + Attachment, + AttachmentLoader, + MeshAttachment, + VertexAttachment, +} from "./attachments"; import { - AlphaTimeline, Animation, - AttachmentTimeline, CurveTimeline, CurveTimeline1, CurveTimeline2, DeformTimeline, DrawOrderTimeline, EventTimeline, + AlphaTimeline, + Animation, + AttachmentTimeline, + CurveTimeline, + CurveTimeline1, + CurveTimeline2, + DeformTimeline, + DrawOrderTimeline, + EventTimeline, IkConstraintTimeline, PathConstraintMixTimeline, PathConstraintPositionTimeline, - PathConstraintSpacingTimeline, RGB2Timeline, RGBA2Timeline, RGBATimeline, RGBTimeline, + PathConstraintSpacingTimeline, + RGB2Timeline, + RGBA2Timeline, + RGBATimeline, + RGBTimeline, RotateTimeline, - ScaleTimeline, ScaleXTimeline, ScaleYTimeline, - ShearTimeline, ShearXTimeline, ShearYTimeline, + ScaleTimeline, + ScaleXTimeline, + ScaleYTimeline, + ShearTimeline, + ShearXTimeline, + ShearYTimeline, Timeline, TransformConstraintTimeline, - TranslateTimeline, TranslateXTimeline, TranslateYTimeline -} from './Animation'; -import {Event} from './Event'; -import {SkeletonData} from './SkeletonData'; -import {SlotData} from './SlotData'; -import {BoneData} from './BoneData'; -import {IkConstraintData} from './IkConstraintData'; -import {TransformConstraintData} from './TransformConstraintData'; -import {PathConstraintData, SpacingMode} from './PathConstraintData'; -import {Skin} from './Skin'; -import {EventData} from './EventData'; -import {AttachmentType, BinaryInput, Color, PositionMode, Utils} from '@pixi-spine/base'; -import {BLEND_MODES} from '@pixi/constants'; + TranslateTimeline, + TranslateXTimeline, + TranslateYTimeline, +} from "./Animation"; +import { Event } from "./Event"; +import { SkeletonData } from "./SkeletonData"; +import { SlotData } from "./SlotData"; +import { BoneData } from "./BoneData"; +import { IkConstraintData } from "./IkConstraintData"; +import { TransformConstraintData } from "./TransformConstraintData"; +import { PathConstraintData, SpacingMode } from "./PathConstraintData"; +import { Skin } from "./Skin"; +import { EventData } from "./EventData"; +import { + AttachmentType, + BinaryInput, + Color, + PositionMode, + Utils, +} from "@pixi-spine/base"; /** Loads skeleton data in the Spine binary format. * @@ -33,7 +60,12 @@ import {BLEND_MODES} from '@pixi/constants'; * @public * */ export class SkeletonBinary { - static BlendModeValues = [ BLEND_MODES.NORMAL, BLEND_MODES.ADD, BLEND_MODES.MULTIPLY, BLEND_MODES.SCREEN]; + static BlendModeValues = [ + PIXI.BLEND_MODES.NORMAL, + PIXI.BLEND_MODES.ADD, + PIXI.BLEND_MODES.MULTIPLY, + PIXI.BLEND_MODES.SCREEN, + ]; /** Scales bone positions, image sizes, and translations as they are loaded. This allows different size images to be used at * runtime than were used in Spine. * @@ -43,11 +75,11 @@ export class SkeletonBinary { attachmentLoader: AttachmentLoader = null; private linkedMeshes = new Array(); - constructor (attachmentLoader: AttachmentLoader) { + constructor(attachmentLoader: AttachmentLoader) { this.attachmentLoader = attachmentLoader; } - readSkeletonData (binary: Uint8Array): SkeletonData { + readSkeletonData(binary: Uint8Array): SkeletonData { let scale = this.scale; let skeletonData = new SkeletonData(); @@ -57,10 +89,12 @@ export class SkeletonBinary { let lowHash = input.readInt32(); let highHash = input.readInt32(); - skeletonData.hash = highHash == 0 && lowHash == 0 ? null : highHash.toString(16) + lowHash.toString(16); + skeletonData.hash = + highHash == 0 && lowHash == 0 + ? null + : highHash.toString(16) + lowHash.toString(16); skeletonData.version = input.readString(); - if (skeletonData.version.substr(0, 3) !== '4.0') - { + if (skeletonData.version.substr(0, 3) !== "4.0") { let error = `Spine 4.0 loader cant load version ${skeletonData.version}. Please configure your pixi-spine bundle`; console.error(error); } @@ -79,15 +113,15 @@ export class SkeletonBinary { let n = 0; // Strings. - n = input.readInt(true) - for (let i = 0; i < n; i++) - input.strings.push(input.readString()); + n = input.readInt(true); + for (let i = 0; i < n; i++) input.strings.push(input.readString()); // Bones. - n = input.readInt(true) + n = input.readInt(true); for (let i = 0; i < n; i++) { let name = input.readString(); - let parent = i == 0 ? null : skeletonData.bones[input.readInt(true)]; + let parent = + i == 0 ? null : skeletonData.bones[input.readInt(true)]; let data = new BoneData(i, name, parent); data.rotation = input.readFloat(); data.x = input.readFloat() * scale; @@ -99,7 +133,8 @@ export class SkeletonBinary { data.length = input.readFloat() * scale; data.transformMode = input.readInt(true); data.skinRequired = input.readBoolean(); - if (nonessential) Color.rgba8888ToColor(data.color, input.readInt32()); + if (nonessential) + Color.rgba8888ToColor(data.color, input.readInt32()); skeletonData.bones.push(data); } @@ -112,10 +147,12 @@ export class SkeletonBinary { Color.rgba8888ToColor(data.color, input.readInt32()); let darkColor = input.readInt32(); - if (darkColor != -1) Color.rgb888ToColor(data.darkColor = new Color(), darkColor); + if (darkColor != -1) + Color.rgb888ToColor((data.darkColor = new Color()), darkColor); data.attachmentName = input.readStringRef(); - data.blendMode = SkeletonBinary.BlendModeValues[input.readInt(true)]; + data.blendMode = + SkeletonBinary.BlendModeValues[input.readInt(true)]; skeletonData.slots.push(data); } @@ -182,7 +219,11 @@ export class SkeletonBinary { data.position = input.readFloat(); if (data.positionMode == PositionMode.Fixed) data.position *= scale; data.spacing = input.readFloat(); - if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) data.spacing *= scale; + if ( + data.spacingMode == SpacingMode.Length || + data.spacingMode == SpacingMode.Fixed + ) + data.spacing *= scale; data.mixRotate = input.readFloat(); data.mixX = input.readFloat(); data.mixY = input.readFloat(); @@ -190,7 +231,12 @@ export class SkeletonBinary { } // Default skin. - let defaultSkin = this.readSkin(input, skeletonData, true, nonessential); + let defaultSkin = this.readSkin( + input, + skeletonData, + true, + nonessential + ); if (defaultSkin) { skeletonData.defaultSkin = defaultSkin; skeletonData.skins.push(defaultSkin); @@ -199,18 +245,33 @@ export class SkeletonBinary { // Skins. { let i = skeletonData.skins.length; - Utils.setArraySize(skeletonData.skins, n = i + input.readInt(true)); + Utils.setArraySize( + skeletonData.skins, + (n = i + input.readInt(true)) + ); for (; i < n; i++) - skeletonData.skins[i] = this.readSkin(input, skeletonData, false, nonessential); + skeletonData.skins[i] = this.readSkin( + input, + skeletonData, + false, + nonessential + ); } // Linked meshes. n = this.linkedMeshes.length; for (let i = 0; i < n; i++) { let linkedMesh = this.linkedMeshes[i]; - let skin = !linkedMesh.skin ? skeletonData.defaultSkin : skeletonData.findSkin(linkedMesh.skin); - let parent = skin.getAttachment(linkedMesh.slotIndex, linkedMesh.parent); - linkedMesh.mesh.deformAttachment = linkedMesh.inheritDeform ? parent as VertexAttachment : linkedMesh.mesh; + let skin = !linkedMesh.skin + ? skeletonData.defaultSkin + : skeletonData.findSkin(linkedMesh.skin); + let parent = skin.getAttachment( + linkedMesh.slotIndex, + linkedMesh.parent + ); + linkedMesh.mesh.deformAttachment = linkedMesh.inheritDeform + ? (parent as VertexAttachment) + : linkedMesh.mesh; linkedMesh.mesh.setParentMesh(parent as MeshAttachment); // linkedMesh.mesh.updateUVs(); } @@ -234,16 +295,23 @@ export class SkeletonBinary { // Animations. n = input.readInt(true); for (let i = 0; i < n; i++) - skeletonData.animations.push(this.readAnimation(input, input.readString(), skeletonData)); + skeletonData.animations.push( + this.readAnimation(input, input.readString(), skeletonData) + ); return skeletonData; } - private readSkin (input: BinaryInput, skeletonData: SkeletonData, defaultSkin: boolean, nonessential: boolean): Skin { + private readSkin( + input: BinaryInput, + skeletonData: SkeletonData, + defaultSkin: boolean, + nonessential: boolean + ): Skin { let skin = null; let slotCount = 0; if (defaultSkin) { - slotCount = input.readInt(true) + slotCount = input.readInt(true); if (slotCount == 0) return null; skin = new Skin("default"); } else { @@ -253,11 +321,17 @@ export class SkeletonBinary { skin.bones[i] = skeletonData.bones[input.readInt(true)]; for (let i = 0, n = input.readInt(true); i < n; i++) - skin.constraints.push(skeletonData.ikConstraints[input.readInt(true)]); + skin.constraints.push( + skeletonData.ikConstraints[input.readInt(true)] + ); for (let i = 0, n = input.readInt(true); i < n; i++) - skin.constraints.push(skeletonData.transformConstraints[input.readInt(true)]); + skin.constraints.push( + skeletonData.transformConstraints[input.readInt(true)] + ); for (let i = 0, n = input.readInt(true); i < n; i++) - skin.constraints.push(skeletonData.pathConstraints[input.readInt(true)]); + skin.constraints.push( + skeletonData.pathConstraints[input.readInt(true)] + ); slotCount = input.readInt(true); } @@ -266,14 +340,28 @@ export class SkeletonBinary { let slotIndex = input.readInt(true); for (let ii = 0, nn = input.readInt(true); ii < nn; ii++) { let name = input.readStringRef(); - let attachment = this.readAttachment(input, skeletonData, skin, slotIndex, name, nonessential); + let attachment = this.readAttachment( + input, + skeletonData, + skin, + slotIndex, + name, + nonessential + ); if (attachment) skin.setAttachment(slotIndex, name, attachment); } } return skin; } - private readAttachment (input: BinaryInput, skeletonData: SkeletonData, skin: Skin, slotIndex: number, attachmentName: string, nonessential: boolean): Attachment { + private readAttachment( + input: BinaryInput, + skeletonData: SkeletonData, + skin: Skin, + slotIndex: number, + attachmentName: string, + nonessential: boolean + ): Attachment { let scale = this.scale; let name = input.readStringRef(); @@ -292,7 +380,11 @@ export class SkeletonBinary { let color = input.readInt32(); if (!path) path = name; - let region = this.attachmentLoader.newRegionAttachment(skin, name, path); + let region = this.attachmentLoader.newRegionAttachment( + skin, + name, + path + ); if (!region) return null; region.path = path; region.x = x * scale; @@ -311,7 +403,10 @@ export class SkeletonBinary { let vertices = this.readVertices(input, vertexCount); let color = nonessential ? input.readInt32() : 0; - let box = this.attachmentLoader.newBoundingBoxAttachment(skin, name); + let box = this.attachmentLoader.newBoundingBoxAttachment( + skin, + name + ); if (!box) return null; box.worldVerticesLength = vertexCount << 1; box.vertices = vertices.vertices; @@ -328,7 +423,8 @@ export class SkeletonBinary { let vertices = this.readVertices(input, vertexCount); let hullLength = input.readInt(true); let edges = null; - let width = 0, height = 0; + let width = 0, + height = 0; if (nonessential) { edges = this.readShortArray(input); width = input.readFloat(); @@ -336,7 +432,11 @@ export class SkeletonBinary { } if (!path) path = name; - let mesh = this.attachmentLoader.newMeshAttachment(skin, name, path); + let mesh = this.attachmentLoader.newMeshAttachment( + skin, + name, + path + ); if (!mesh) return null; mesh.path = path; Color.rgba8888ToColor(mesh.color, color); @@ -360,14 +460,19 @@ export class SkeletonBinary { let skinName = input.readStringRef(); let parent = input.readStringRef(); let inheritDeform = input.readBoolean(); - let width = 0, height = 0; + let width = 0, + height = 0; if (nonessential) { width = input.readFloat(); height = input.readFloat(); } if (!path) path = name; - let mesh = this.attachmentLoader.newMeshAttachment(skin, name, path); + let mesh = this.attachmentLoader.newMeshAttachment( + skin, + name, + path + ); if (!mesh) return null; mesh.path = path; Color.rgba8888ToColor(mesh.color, color); @@ -375,7 +480,15 @@ export class SkeletonBinary { mesh.width = width * scale; mesh.height = height * scale; } - this.linkedMeshes.push(new LinkedMesh(mesh, skinName, slotIndex, parent, inheritDeform)); + this.linkedMeshes.push( + new LinkedMesh( + mesh, + skinName, + slotIndex, + parent, + inheritDeform + ) + ); return mesh; } case AttachmentType.Path: { @@ -405,7 +518,10 @@ export class SkeletonBinary { let y = input.readFloat(); let color = nonessential ? input.readInt32() : 0; - let point = this.attachmentLoader.newPointAttachment(skin, name); + let point = this.attachmentLoader.newPointAttachment( + skin, + name + ); if (!point) return null; point.x = x * scale; point.y = y * scale; @@ -419,7 +535,10 @@ export class SkeletonBinary { let vertices = this.readVertices(input, vertexCount); let color = nonessential ? input.readInt32() : 0; - let clip = this.attachmentLoader.newClippingAttachment(skin, name); + let clip = this.attachmentLoader.newClippingAttachment( + skin, + name + ); if (!clip) return null; clip.endSlot = skeletonData.slots[endSlotIndex]; clip.worldVerticesLength = vertexCount << 1; @@ -432,12 +551,16 @@ export class SkeletonBinary { return null; } - private readVertices (input: BinaryInput, vertexCount: number): Vertices { + private readVertices(input: BinaryInput, vertexCount: number): Vertices { let scale = this.scale; let verticesLength = vertexCount << 1; let vertices = new Vertices(); if (!input.readBoolean()) { - vertices.vertices = this.readFloatArray(input, verticesLength, scale); + vertices.vertices = this.readFloatArray( + input, + verticesLength, + scale + ); return vertices; } let weights = new Array(); @@ -457,27 +580,32 @@ export class SkeletonBinary { return vertices; } - private readFloatArray (input: BinaryInput, n: number, scale: number): number[] { + private readFloatArray( + input: BinaryInput, + n: number, + scale: number + ): number[] { let array = new Array(n); if (scale == 1) { - for (let i = 0; i < n; i++) - array[i] = input.readFloat(); + for (let i = 0; i < n; i++) array[i] = input.readFloat(); } else { - for (let i = 0; i < n; i++) - array[i] = input.readFloat() * scale; + for (let i = 0; i < n; i++) array[i] = input.readFloat() * scale; } return array; } - private readShortArray (input: BinaryInput): number[] { + private readShortArray(input: BinaryInput): number[] { let n = input.readInt(true); let array = new Array(n); - for (let i = 0; i < n; i++) - array[i] = input.readShort(); + for (let i = 0; i < n; i++) array[i] = input.readShort(); return array; } - private readAnimation (input: BinaryInput, name: string, skeletonData: SkeletonData): Animation { + private readAnimation( + input: BinaryInput, + name: string, + skeletonData: SkeletonData + ): Animation { input.readInt(true); // Number of timelines. let timelines = new Array(); let scale = this.scale; @@ -491,15 +619,26 @@ export class SkeletonBinary { let frameLast = frameCount - 1; switch (timelineType) { case SLOT_ATTACHMENT: { - let timeline = new AttachmentTimeline(frameCount, slotIndex); + let timeline = new AttachmentTimeline( + frameCount, + slotIndex + ); for (let frame = 0; frame < frameCount; frame++) - timeline.setFrame(frame, input.readFloat(), input.readStringRef()); + timeline.setFrame( + frame, + input.readFloat(), + input.readStringRef() + ); timelines.push(timeline); break; } case SLOT_RGBA: { let bezierCount = input.readInt(true); - let timeline = new RGBATimeline(frameCount, bezierCount, slotIndex); + let timeline = new RGBATimeline( + frameCount, + bezierCount, + slotIndex + ); let time = input.readFloat(); let r = input.readUnsignedByte() / 255.0; @@ -522,10 +661,54 @@ export class SkeletonBinary { timeline.setStepped(frame); break; case CURVE_BEZIER: - setBezier(input, timeline, bezier++, frame, 0, time, time2, r, r2, 1); - setBezier(input, timeline, bezier++, frame, 1, time, time2, g, g2, 1); - setBezier(input, timeline, bezier++, frame, 2, time, time2, b, b2, 1); - setBezier(input, timeline, bezier++, frame, 3, time, time2, a, a2, 1); + setBezier( + input, + timeline, + bezier++, + frame, + 0, + time, + time2, + r, + r2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 1, + time, + time2, + g, + g2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 2, + time, + time2, + b, + b2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 3, + time, + time2, + a, + a2, + 1 + ); } time = time2; r = r2; @@ -538,7 +721,11 @@ export class SkeletonBinary { } case SLOT_RGB: { let bezierCount = input.readInt(true); - let timeline = new RGBTimeline(frameCount, bezierCount, slotIndex); + let timeline = new RGBTimeline( + frameCount, + bezierCount, + slotIndex + ); let time = input.readFloat(); let r = input.readUnsignedByte() / 255.0; @@ -559,9 +746,42 @@ export class SkeletonBinary { timeline.setStepped(frame); break; case CURVE_BEZIER: - setBezier(input, timeline, bezier++, frame, 0, time, time2, r, r2, 1); - setBezier(input, timeline, bezier++, frame, 1, time, time2, g, g2, 1); - setBezier(input, timeline, bezier++, frame, 2, time, time2, b, b2, 1); + setBezier( + input, + timeline, + bezier++, + frame, + 0, + time, + time2, + r, + r2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 1, + time, + time2, + g, + g2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 2, + time, + time2, + b, + b2, + 1 + ); } time = time2; r = r2; @@ -573,7 +793,11 @@ export class SkeletonBinary { } case SLOT_RGBA2: { let bezierCount = input.readInt(true); - let timeline = new RGBA2Timeline(frameCount, bezierCount, slotIndex); + let timeline = new RGBA2Timeline( + frameCount, + bezierCount, + slotIndex + ); let time = input.readFloat(); let r = input.readUnsignedByte() / 255.0; @@ -585,7 +809,17 @@ export class SkeletonBinary { let b2 = input.readUnsignedByte() / 255.0; for (let frame = 0, bezier = 0; ; frame++) { - timeline.setFrame(frame, time, r, g, b, a, r2, g2, b2); + timeline.setFrame( + frame, + time, + r, + g, + b, + a, + r2, + g2, + b2 + ); if (frame == frameLast) break; let time2 = input.readFloat(); let nr = input.readUnsignedByte() / 255.0; @@ -601,13 +835,90 @@ export class SkeletonBinary { timeline.setStepped(frame); break; case CURVE_BEZIER: - setBezier(input, timeline, bezier++, frame, 0, time, time2, r, nr, 1); - setBezier(input, timeline, bezier++, frame, 1, time, time2, g, ng, 1); - setBezier(input, timeline, bezier++, frame, 2, time, time2, b, nb, 1); - setBezier(input, timeline, bezier++, frame, 3, time, time2, a, na, 1); - setBezier(input, timeline, bezier++, frame, 4, time, time2, r2, nr2, 1); - setBezier(input, timeline, bezier++, frame, 5, time, time2, g2, ng2, 1); - setBezier(input, timeline, bezier++, frame, 6, time, time2, b2, nb2, 1); + setBezier( + input, + timeline, + bezier++, + frame, + 0, + time, + time2, + r, + nr, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 1, + time, + time2, + g, + ng, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 2, + time, + time2, + b, + nb, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 3, + time, + time2, + a, + na, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 4, + time, + time2, + r2, + nr2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 5, + time, + time2, + g2, + ng2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 6, + time, + time2, + b2, + nb2, + 1 + ); } time = time2; r = nr; @@ -623,7 +934,11 @@ export class SkeletonBinary { } case SLOT_RGB2: { let bezierCount = input.readInt(true); - let timeline = new RGB2Timeline(frameCount, bezierCount, slotIndex); + let timeline = new RGB2Timeline( + frameCount, + bezierCount, + slotIndex + ); let time = input.readFloat(); let r = input.readUnsignedByte() / 255.0; @@ -649,12 +964,78 @@ export class SkeletonBinary { timeline.setStepped(frame); break; case CURVE_BEZIER: - setBezier(input, timeline, bezier++, frame, 0, time, time2, r, nr, 1); - setBezier(input, timeline, bezier++, frame, 1, time, time2, g, ng, 1); - setBezier(input, timeline, bezier++, frame, 2, time, time2, b, nb, 1); - setBezier(input, timeline, bezier++, frame, 3, time, time2, r2, nr2, 1); - setBezier(input, timeline, bezier++, frame, 4, time, time2, g2, ng2, 1); - setBezier(input, timeline, bezier++, frame, 5, time, time2, b2, nb2, 1); + setBezier( + input, + timeline, + bezier++, + frame, + 0, + time, + time2, + r, + nr, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 1, + time, + time2, + g, + ng, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 2, + time, + time2, + b, + nb, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 3, + time, + time2, + r2, + nr2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 4, + time, + time2, + g2, + ng2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 5, + time, + time2, + b2, + nb2, + 1 + ); } time = time2; r = nr; @@ -668,8 +1049,13 @@ export class SkeletonBinary { break; } case SLOT_ALPHA: { - let timeline = new AlphaTimeline(frameCount, input.readInt(true), slotIndex); - let time = input.readFloat(), a = input.readUnsignedByte() / 255; + let timeline = new AlphaTimeline( + frameCount, + input.readInt(true), + slotIndex + ); + let time = input.readFloat(), + a = input.readUnsignedByte() / 255; for (let frame = 0, bezier = 0; ; frame++) { timeline.setFrame(frame, time, a); if (frame == frameLast) break; @@ -680,7 +1066,18 @@ export class SkeletonBinary { timeline.setStepped(frame); break; case CURVE_BEZIER: - setBezier(input, timeline, bezier++, frame, 0, time, time2, a, a2, 1); + setBezier( + input, + timeline, + bezier++, + frame, + 0, + time, + time2, + a, + a2, + 1 + ); } time = time2; a = a2; @@ -696,57 +1093,199 @@ export class SkeletonBinary { for (let i = 0, n = input.readInt(true); i < n; i++) { let boneIndex = input.readInt(true); for (let ii = 0, nn = input.readInt(true); ii < nn; ii++) { - let type = input.readByte(), frameCount = input.readInt(true), bezierCount = input.readInt(true); + let type = input.readByte(), + frameCount = input.readInt(true), + bezierCount = input.readInt(true); switch (type) { case BONE_ROTATE: - timelines.push(readTimeline1(input, new RotateTimeline(frameCount, bezierCount, boneIndex), 1)); + timelines.push( + readTimeline1( + input, + new RotateTimeline( + frameCount, + bezierCount, + boneIndex + ), + 1 + ) + ); break; case BONE_TRANSLATE: - timelines.push(readTimeline2(input, new TranslateTimeline(frameCount, bezierCount, boneIndex), scale)); + timelines.push( + readTimeline2( + input, + new TranslateTimeline( + frameCount, + bezierCount, + boneIndex + ), + scale + ) + ); break; case BONE_TRANSLATEX: - timelines.push(readTimeline1(input, new TranslateXTimeline(frameCount, bezierCount, boneIndex), scale)); + timelines.push( + readTimeline1( + input, + new TranslateXTimeline( + frameCount, + bezierCount, + boneIndex + ), + scale + ) + ); break; case BONE_TRANSLATEY: - timelines.push(readTimeline1(input, new TranslateYTimeline(frameCount, bezierCount, boneIndex), scale)); + timelines.push( + readTimeline1( + input, + new TranslateYTimeline( + frameCount, + bezierCount, + boneIndex + ), + scale + ) + ); break; case BONE_SCALE: - timelines.push(readTimeline2(input, new ScaleTimeline(frameCount, bezierCount, boneIndex), 1)); + timelines.push( + readTimeline2( + input, + new ScaleTimeline( + frameCount, + bezierCount, + boneIndex + ), + 1 + ) + ); break; case BONE_SCALEX: - timelines.push(readTimeline1(input, new ScaleXTimeline(frameCount, bezierCount, boneIndex), 1)); + timelines.push( + readTimeline1( + input, + new ScaleXTimeline( + frameCount, + bezierCount, + boneIndex + ), + 1 + ) + ); break; case BONE_SCALEY: - timelines.push(readTimeline1(input, new ScaleYTimeline(frameCount, bezierCount, boneIndex), 1)); + timelines.push( + readTimeline1( + input, + new ScaleYTimeline( + frameCount, + bezierCount, + boneIndex + ), + 1 + ) + ); break; case BONE_SHEAR: - timelines.push(readTimeline2(input, new ShearTimeline(frameCount, bezierCount, boneIndex), 1)); + timelines.push( + readTimeline2( + input, + new ShearTimeline( + frameCount, + bezierCount, + boneIndex + ), + 1 + ) + ); break; case BONE_SHEARX: - timelines.push(readTimeline1(input, new ShearXTimeline(frameCount, bezierCount, boneIndex), 1)); + timelines.push( + readTimeline1( + input, + new ShearXTimeline( + frameCount, + bezierCount, + boneIndex + ), + 1 + ) + ); break; case BONE_SHEARY: - timelines.push(readTimeline1(input, new ShearYTimeline(frameCount, bezierCount, boneIndex), 1)); + timelines.push( + readTimeline1( + input, + new ShearYTimeline( + frameCount, + bezierCount, + boneIndex + ), + 1 + ) + ); } } } // IK constraint timelines. for (let i = 0, n = input.readInt(true); i < n; i++) { - let index = input.readInt(true), frameCount = input.readInt(true), frameLast = frameCount - 1; - let timeline = new IkConstraintTimeline(frameCount, input.readInt(true), index); - let time = input.readFloat(), mix = input.readFloat(), softness = input.readFloat() * scale; + let index = input.readInt(true), + frameCount = input.readInt(true), + frameLast = frameCount - 1; + let timeline = new IkConstraintTimeline( + frameCount, + input.readInt(true), + index + ); + let time = input.readFloat(), + mix = input.readFloat(), + softness = input.readFloat() * scale; for (let frame = 0, bezier = 0; ; frame++) { - timeline.setFrame(frame, time, mix, softness, input.readByte(), input.readBoolean(), input.readBoolean()); + timeline.setFrame( + frame, + time, + mix, + softness, + input.readByte(), + input.readBoolean(), + input.readBoolean() + ); if (frame == frameLast) break; - let time2 = input.readFloat(), mix2 = input.readFloat(), softness2 = input.readFloat() * scale; + let time2 = input.readFloat(), + mix2 = input.readFloat(), + softness2 = input.readFloat() * scale; switch (input.readByte()) { case CURVE_STEPPED: timeline.setStepped(frame); break; case CURVE_BEZIER: - setBezier(input, timeline, bezier++, frame, 0, time, time2, mix, mix2, 1); - setBezier(input, timeline, bezier++, frame, 1, time, time2, softness, softness2, scale); + setBezier( + input, + timeline, + bezier++, + frame, + 0, + time, + time2, + mix, + mix2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 1, + time, + time2, + softness, + softness2, + scale + ); } time = time2; mix = mix2; @@ -757,26 +1296,117 @@ export class SkeletonBinary { // Transform constraint timelines. for (let i = 0, n = input.readInt(true); i < n; i++) { - let index = input.readInt(true), frameCount = input.readInt(true), frameLast = frameCount - 1; - let timeline = new TransformConstraintTimeline(frameCount, input.readInt(true), index); - let time = input.readFloat(), mixRotate = input.readFloat(), mixX = input.readFloat(), mixY = input.readFloat(), - mixScaleX = input.readFloat(), mixScaleY = input.readFloat(), mixShearY = input.readFloat(); + let index = input.readInt(true), + frameCount = input.readInt(true), + frameLast = frameCount - 1; + let timeline = new TransformConstraintTimeline( + frameCount, + input.readInt(true), + index + ); + let time = input.readFloat(), + mixRotate = input.readFloat(), + mixX = input.readFloat(), + mixY = input.readFloat(), + mixScaleX = input.readFloat(), + mixScaleY = input.readFloat(), + mixShearY = input.readFloat(); for (let frame = 0, bezier = 0; ; frame++) { - timeline.setFrame(frame, time, mixRotate, mixX, mixY, mixScaleX, mixScaleY, mixShearY); + timeline.setFrame( + frame, + time, + mixRotate, + mixX, + mixY, + mixScaleX, + mixScaleY, + mixShearY + ); if (frame == frameLast) break; - let time2 = input.readFloat(), mixRotate2 = input.readFloat(), mixX2 = input.readFloat(), mixY2 = input.readFloat(), - mixScaleX2 = input.readFloat(), mixScaleY2 = input.readFloat(), mixShearY2 = input.readFloat(); + let time2 = input.readFloat(), + mixRotate2 = input.readFloat(), + mixX2 = input.readFloat(), + mixY2 = input.readFloat(), + mixScaleX2 = input.readFloat(), + mixScaleY2 = input.readFloat(), + mixShearY2 = input.readFloat(); switch (input.readByte()) { case CURVE_STEPPED: timeline.setStepped(frame); break; case CURVE_BEZIER: - setBezier(input, timeline, bezier++, frame, 0, time, time2, mixRotate, mixRotate2, 1); - setBezier(input, timeline, bezier++, frame, 1, time, time2, mixX, mixX2, 1); - setBezier(input, timeline, bezier++, frame, 2, time, time2, mixY, mixY2, 1); - setBezier(input, timeline, bezier++, frame, 3, time, time2, mixScaleX, mixScaleX2, 1); - setBezier(input, timeline, bezier++, frame, 4, time, time2, mixScaleY, mixScaleY2, 1); - setBezier(input, timeline, bezier++, frame, 5, time, time2, mixShearY, mixShearY2, 1); + setBezier( + input, + timeline, + bezier++, + frame, + 0, + time, + time2, + mixRotate, + mixRotate2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 1, + time, + time2, + mixX, + mixX2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 2, + time, + time2, + mixY, + mixY2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 3, + time, + time2, + mixScaleX, + mixScaleX2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 4, + time, + time2, + mixScaleY, + mixScaleY2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 5, + time, + time2, + mixShearY, + mixShearY2, + 1 + ); } time = time2; mixRotate = mixRotate2; @@ -796,31 +1426,106 @@ export class SkeletonBinary { for (let ii = 0, nn = input.readInt(true); ii < nn; ii++) { switch (input.readByte()) { case PATH_POSITION: - timelines - .push(readTimeline1(input, new PathConstraintPositionTimeline(input.readInt(true), input.readInt(true), index), - data.positionMode == PositionMode.Fixed ? scale : 1)); + timelines.push( + readTimeline1( + input, + new PathConstraintPositionTimeline( + input.readInt(true), + input.readInt(true), + index + ), + data.positionMode == PositionMode.Fixed + ? scale + : 1 + ) + ); break; case PATH_SPACING: - timelines - .push(readTimeline1(input, new PathConstraintSpacingTimeline(input.readInt(true), input.readInt(true), index), - data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed ? scale : 1)); + timelines.push( + readTimeline1( + input, + new PathConstraintSpacingTimeline( + input.readInt(true), + input.readInt(true), + index + ), + data.spacingMode == SpacingMode.Length || + data.spacingMode == SpacingMode.Fixed + ? scale + : 1 + ) + ); break; case PATH_MIX: - let timeline = new PathConstraintMixTimeline(input.readInt(true), input.readInt(true), index); - let time = input.readFloat(), mixRotate = input.readFloat(), mixX = input.readFloat(), mixY = input.readFloat(); - for (let frame = 0, bezier = 0, frameLast = timeline.getFrameCount() - 1; ; frame++) { - timeline.setFrame(frame, time, mixRotate, mixX, mixY); + let timeline = new PathConstraintMixTimeline( + input.readInt(true), + input.readInt(true), + index + ); + let time = input.readFloat(), + mixRotate = input.readFloat(), + mixX = input.readFloat(), + mixY = input.readFloat(); + for ( + let frame = 0, + bezier = 0, + frameLast = timeline.getFrameCount() - 1; + ; + frame++ + ) { + timeline.setFrame( + frame, + time, + mixRotate, + mixX, + mixY + ); if (frame == frameLast) break; - let time2 = input.readFloat(), mixRotate2 = input.readFloat(), mixX2 = input.readFloat(), + let time2 = input.readFloat(), + mixRotate2 = input.readFloat(), + mixX2 = input.readFloat(), mixY2 = input.readFloat(); switch (input.readByte()) { case CURVE_STEPPED: timeline.setStepped(frame); break; case CURVE_BEZIER: - setBezier(input, timeline, bezier++, frame, 0, time, time2, mixRotate, mixRotate2, 1); - setBezier(input, timeline, bezier++, frame, 1, time, time2, mixX, mixX2, 1); - setBezier(input, timeline, bezier++, frame, 2, time, time2, mixY, mixY2, 1); + setBezier( + input, + timeline, + bezier++, + frame, + 0, + time, + time2, + mixRotate, + mixRotate2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 1, + time, + time2, + mixX, + mixX2, + 1 + ); + setBezier( + input, + timeline, + bezier++, + frame, + 2, + time, + time2, + mixY, + mixY2, + 1 + ); } time = time2; mixRotate = mixRotate2; @@ -839,22 +1544,34 @@ export class SkeletonBinary { let slotIndex = input.readInt(true); for (let iii = 0, nnn = input.readInt(true); iii < nnn; iii++) { let attachmentName = input.readStringRef(); - let attachment = skin.getAttachment(slotIndex, attachmentName) as VertexAttachment; + let attachment = skin.getAttachment( + slotIndex, + attachmentName + ) as VertexAttachment; let weighted = attachment.bones; let vertices = attachment.vertices; - let deformLength = weighted ? vertices.length / 3 * 2 : vertices.length; + let deformLength = weighted + ? (vertices.length / 3) * 2 + : vertices.length; let frameCount = input.readInt(true); let frameLast = frameCount - 1; let bezierCount = input.readInt(true); - let timeline = new DeformTimeline(frameCount, bezierCount, slotIndex, attachment); + let timeline = new DeformTimeline( + frameCount, + bezierCount, + slotIndex, + attachment + ); let time = input.readFloat(); for (let frame = 0, bezier = 0; ; frame++) { let deform; let end = input.readInt(true); if (end == 0) - deform = weighted ? Utils.newFloatArray(deformLength) : vertices; + deform = weighted + ? Utils.newFloatArray(deformLength) + : vertices; else { deform = Utils.newFloatArray(deformLength); let start = input.readInt(true); @@ -880,7 +1597,18 @@ export class SkeletonBinary { timeline.setStepped(frame); break; case CURVE_BEZIER: - setBezier(input, timeline, bezier++, frame, 0, time, time2, 0, 1, 1); + setBezier( + input, + timeline, + bezier++, + frame, + 0, + time, + time2, + 0, + 1, + 1 + ); } time = time2; } @@ -898,24 +1626,26 @@ export class SkeletonBinary { let time = input.readFloat(); let offsetCount = input.readInt(true); let drawOrder = Utils.newArray(slotCount, 0); - for (let ii = slotCount - 1; ii >= 0; ii--) - drawOrder[ii] = -1; + for (let ii = slotCount - 1; ii >= 0; ii--) drawOrder[ii] = -1; let unchanged = Utils.newArray(slotCount - offsetCount, 0); - let originalIndex = 0, unchangedIndex = 0; + let originalIndex = 0, + unchangedIndex = 0; for (let ii = 0; ii < offsetCount; ii++) { let slotIndex = input.readInt(true); // Collect unchanged items. while (originalIndex != slotIndex) unchanged[unchangedIndex++] = originalIndex++; // Set changed items. - drawOrder[originalIndex + input.readInt(true)] = originalIndex++; + drawOrder[originalIndex + input.readInt(true)] = + originalIndex++; } // Collect remaining unchanged items. while (originalIndex < slotCount) unchanged[unchangedIndex++] = originalIndex++; // Fill in unchanged items. for (let ii = slotCount - 1; ii >= 0; ii--) - if (drawOrder[ii] == -1) drawOrder[ii] = unchanged[--unchangedIndex]; + if (drawOrder[ii] == -1) + drawOrder[ii] = unchanged[--unchangedIndex]; timeline.setFrame(i, time, drawOrder); } timelines.push(timeline); @@ -931,7 +1661,9 @@ export class SkeletonBinary { let event = new Event(time, eventData); event.intValue = input.readInt(false); event.floatValue = input.readFloat(); - event.stringValue = input.readBoolean() ? input.readString() : eventData.stringValue; + event.stringValue = input.readBoolean() + ? input.readString() + : eventData.stringValue; if (event.data.audioPath) { event.volume = input.readFloat(); event.balance = input.readFloat(); @@ -949,12 +1681,19 @@ export class SkeletonBinary { } class LinkedMesh { - parent: string; skin: string; + parent: string; + skin: string; slotIndex: number; mesh: MeshAttachment; inheritDeform: boolean; - constructor (mesh: MeshAttachment, skin: string, slotIndex: number, parent: string, inheritDeform: boolean) { + constructor( + mesh: MeshAttachment, + skin: string, + slotIndex: number, + parent: string, + inheritDeform: boolean + ) { this.mesh = mesh; this.skin = skin; this.slotIndex = slotIndex; @@ -964,21 +1703,45 @@ class LinkedMesh { } class Vertices { - constructor (public bones: Array = null, public vertices: Array | Float32Array = null) { } + constructor( + public bones: Array = null, + public vertices: Array | Float32Array = null + ) {} } -function readTimeline1 (input: BinaryInput, timeline: CurveTimeline1, scale: number): CurveTimeline1 { - let time = input.readFloat(), value = input.readFloat() * scale; - for (let frame = 0, bezier = 0, frameLast = timeline.getFrameCount() - 1; ; frame++) { +function readTimeline1( + input: BinaryInput, + timeline: CurveTimeline1, + scale: number +): CurveTimeline1 { + let time = input.readFloat(), + value = input.readFloat() * scale; + for ( + let frame = 0, bezier = 0, frameLast = timeline.getFrameCount() - 1; + ; + frame++ + ) { timeline.setFrame(frame, time, value); if (frame == frameLast) break; - let time2 = input.readFloat(), value2 = input.readFloat() * scale; + let time2 = input.readFloat(), + value2 = input.readFloat() * scale; switch (input.readByte()) { case CURVE_STEPPED: timeline.setStepped(frame); break; case CURVE_BEZIER: - setBezier(input, timeline, bezier++, frame, 0, time, time2, value, value2, scale); + setBezier( + input, + timeline, + bezier++, + frame, + 0, + time, + time2, + value, + value2, + scale + ); } time = time2; value = value2; @@ -986,19 +1749,53 @@ function readTimeline1 (input: BinaryInput, timeline: CurveTimeline1, scale: num return timeline; } -function readTimeline2 (input: BinaryInput, timeline: CurveTimeline2, scale: number): CurveTimeline2 { - let time = input.readFloat(), value1 = input.readFloat() * scale, value2 = input.readFloat() * scale; - for (let frame = 0, bezier = 0, frameLast = timeline.getFrameCount() - 1; ; frame++) { +function readTimeline2( + input: BinaryInput, + timeline: CurveTimeline2, + scale: number +): CurveTimeline2 { + let time = input.readFloat(), + value1 = input.readFloat() * scale, + value2 = input.readFloat() * scale; + for ( + let frame = 0, bezier = 0, frameLast = timeline.getFrameCount() - 1; + ; + frame++ + ) { timeline.setFrame(frame, time, value1, value2); if (frame == frameLast) break; - let time2 = input.readFloat(), nvalue1 = input.readFloat() * scale, nvalue2 = input.readFloat() * scale; + let time2 = input.readFloat(), + nvalue1 = input.readFloat() * scale, + nvalue2 = input.readFloat() * scale; switch (input.readByte()) { case CURVE_STEPPED: timeline.setStepped(frame); break; case CURVE_BEZIER: - setBezier(input, timeline, bezier++, frame, 0, time, time2, value1, nvalue1, scale); - setBezier(input, timeline, bezier++, frame, 1, time, time2, value2, nvalue2, scale); + setBezier( + input, + timeline, + bezier++, + frame, + 0, + time, + time2, + value1, + nvalue1, + scale + ); + setBezier( + input, + timeline, + bezier++, + frame, + 1, + time, + time2, + value2, + nvalue2, + scale + ); } time = time2; value1 = nvalue1; @@ -1007,9 +1804,31 @@ function readTimeline2 (input: BinaryInput, timeline: CurveTimeline2, scale: num return timeline; } -function setBezier (input: BinaryInput, timeline: CurveTimeline, bezier: number, frame: number, value: number, - time1: number, time2: number, value1: number, value2: number, scale: number) { - timeline.setBezier(bezier, frame, value, time1, value1, input.readFloat(), input.readFloat() * scale, input.readFloat(), input.readFloat() * scale, time2, value2); +function setBezier( + input: BinaryInput, + timeline: CurveTimeline, + bezier: number, + frame: number, + value: number, + time1: number, + time2: number, + value1: number, + value2: number, + scale: number +) { + timeline.setBezier( + bezier, + frame, + value, + time1, + value1, + input.readFloat(), + input.readFloat() * scale, + input.readFloat(), + input.readFloat() * scale, + time2, + value2 + ); } const BONE_ROTATE = 0; @@ -1035,6 +1854,6 @@ const PATH_SPACING = 1; const PATH_MIX = 2; // @ts-ignore -const CURVE_LINEAR = 0; -const CURVE_STEPPED = 1; -const CURVE_BEZIER = 2; +const CURVE_LINEAR = 0; +const CURVE_STEPPED = 1; +const CURVE_BEZIER = 2; diff --git a/packages/runtime-4.0/src/core/SkeletonJson.ts b/packages/runtime-4.0/src/core/SkeletonJson.ts index 2c89d01e..8bb59268 100644 --- a/packages/runtime-4.0/src/core/SkeletonJson.ts +++ b/packages/runtime-4.0/src/core/SkeletonJson.ts @@ -1,29 +1,58 @@ -import type {Attachment, AttachmentLoader, MeshAttachment, VertexAttachment} from './attachments'; +import type { + Attachment, + AttachmentLoader, + MeshAttachment, + VertexAttachment, +} from "./attachments"; import { - AlphaTimeline, Animation, - AttachmentTimeline, CurveTimeline, CurveTimeline1, CurveTimeline2, DeformTimeline, DrawOrderTimeline, EventTimeline, + AlphaTimeline, + Animation, + AttachmentTimeline, + CurveTimeline, + CurveTimeline1, + CurveTimeline2, + DeformTimeline, + DrawOrderTimeline, + EventTimeline, IkConstraintTimeline, PathConstraintMixTimeline, PathConstraintPositionTimeline, - PathConstraintSpacingTimeline, RGB2Timeline, RGBA2Timeline, RGBATimeline, RGBTimeline, + PathConstraintSpacingTimeline, + RGB2Timeline, + RGBA2Timeline, + RGBATimeline, + RGBTimeline, RotateTimeline, - ScaleTimeline, ScaleXTimeline, ScaleYTimeline, - ShearTimeline, ShearXTimeline, ShearYTimeline, + ScaleTimeline, + ScaleXTimeline, + ScaleYTimeline, + ShearTimeline, + ShearXTimeline, + ShearYTimeline, Timeline, TransformConstraintTimeline, - TranslateTimeline, TranslateXTimeline, TranslateYTimeline -} from './Animation'; -import {Event} from './Event'; -import {SkeletonData} from './SkeletonData'; -import {SlotData} from './SlotData'; -import {BoneData} from './BoneData'; -import {IkConstraintData} from './IkConstraintData'; -import {TransformConstraintData} from './TransformConstraintData'; -import {PathConstraintData, SpacingMode} from './PathConstraintData'; -import {Skin} from './Skin'; -import {EventData} from './EventData'; -import {NumberArrayLike, Color, PositionMode, RotateMode, TransformMode, Utils, settings} from '@pixi-spine/base'; -import {BLEND_MODES} from '@pixi/constants'; + TranslateTimeline, + TranslateXTimeline, + TranslateYTimeline, +} from "./Animation"; +import { Event } from "./Event"; +import { SkeletonData } from "./SkeletonData"; +import { SlotData } from "./SlotData"; +import { BoneData } from "./BoneData"; +import { IkConstraintData } from "./IkConstraintData"; +import { TransformConstraintData } from "./TransformConstraintData"; +import { PathConstraintData, SpacingMode } from "./PathConstraintData"; +import { Skin } from "./Skin"; +import { EventData } from "./EventData"; +import { + NumberArrayLike, + Color, + PositionMode, + RotateMode, + TransformMode, + Utils, + settings, +} from "@pixi-spine/base"; /** Loads skeleton data in the Spine JSON format. * @@ -42,21 +71,21 @@ export class SkeletonJson { scale = 1; private linkedMeshes = new Array(); - constructor (attachmentLoader: AttachmentLoader) { + constructor(attachmentLoader: AttachmentLoader) { this.attachmentLoader = attachmentLoader; } - readSkeletonData (json: string | any): SkeletonData { + readSkeletonData(json: string | any): SkeletonData { let scale = this.scale; let skeletonData = new SkeletonData(); - let root = typeof (json) === "string" ? JSON.parse(json) : json; + let root = typeof json === "string" ? JSON.parse(json) : json; // Skeleton let skeletonMap = root.skeleton; if (skeletonMap) { skeletonData.hash = skeletonMap.hash; skeletonData.version = skeletonMap.spine; - if (skeletonData.version.substr(0, 3) !== '4.0') { + if (skeletonData.version.substr(0, 3) !== "4.0") { let error = `Spine 4.0 loader cant load version ${skeletonMap.spine}. Please configure your pixi-spine bundle`; console.error(error); } @@ -77,9 +106,14 @@ export class SkeletonJson { let parentName: string = getValue(boneMap, "parent", null); if (parentName != null) { parent = skeletonData.findBone(parentName); - if (parent == null) throw new Error("Parent bone not found: " + parentName); + if (parent == null) + throw new Error("Parent bone not found: " + parentName); } - let data = new BoneData(skeletonData.bones.length, boneMap.name, parent); + let data = new BoneData( + skeletonData.bones.length, + boneMap.name, + parent + ); data.length = getValue(boneMap, "length", 0) * scale; data.x = getValue(boneMap, "x", 0) * scale; data.y = getValue(boneMap, "y", 0) * scale; @@ -88,7 +122,10 @@ export class SkeletonJson { data.scaleY = getValue(boneMap, "scaleY", 1); data.shearX = getValue(boneMap, "shearX", 0); data.shearY = getValue(boneMap, "shearY", 0); - data.transformMode = Utils.enumValue(TransformMode, getValue(boneMap, "transform", "Normal")); + data.transformMode = Utils.enumValue( + TransformMode, + getValue(boneMap, "transform", "Normal") + ); data.skinRequired = getValue(boneMap, "skin", false); let color = getValue(boneMap, "color", null); @@ -103,7 +140,11 @@ export class SkeletonJson { for (let i = 0; i < root.slots.length; i++) { let slotMap = root.slots[i]; let boneData = skeletonData.findBone(slotMap.bone); - let data = new SlotData(skeletonData.slots.length, slotMap.name, boneData); + let data = new SlotData( + skeletonData.slots.length, + slotMap.name, + boneData + ); let color: string = getValue(slotMap, "color", null); if (color) data.color.setFromString(color); @@ -112,7 +153,9 @@ export class SkeletonJson { if (dark) data.darkColor = Color.fromString(dark); data.attachmentName = getValue(slotMap, "attachment", null); - data.blendMode = SkeletonJson.blendModeFromString(getValue(slotMap, "blend", "normal")); + data.blendMode = SkeletonJson.blendModeFromString( + getValue(slotMap, "blend", "normal") + ); skeletonData.slots.push(data); } } @@ -128,7 +171,8 @@ export class SkeletonJson { for (let ii = 0; ii < constraintMap.bones.length; ii++) { let boneName = constraintMap.bones[ii]; let bone = skeletonData.findBone(boneName); - if (bone == null) throw new Error("IK bone not found: " + boneName); + if (bone == null) + throw new Error("IK bone not found: " + boneName); data.bones.push(bone); } @@ -136,7 +180,13 @@ export class SkeletonJson { data.mix = getValue(constraintMap, "mix", 1); data.softness = getValue(constraintMap, "softness", 0) * scale; - data.bendDirection = getValue(constraintMap, "bendPositive", true) ? 1 : -1; + data.bendDirection = getValue( + constraintMap, + "bendPositive", + true + ) + ? 1 + : -1; data.compress = getValue(constraintMap, "compress", false); data.stretch = getValue(constraintMap, "stretch", false); data.uniform = getValue(constraintMap, "uniform", false); @@ -156,13 +206,20 @@ export class SkeletonJson { for (let ii = 0; ii < constraintMap.bones.length; ii++) { let boneName = constraintMap.bones[ii]; let bone = skeletonData.findBone(boneName); - if (bone == null) throw new Error("Transform constraint bone not found: " + boneName); + if (bone == null) + throw new Error( + "Transform constraint bone not found: " + boneName + ); data.bones.push(bone); } let targetName: string = constraintMap.target; data.target = skeletonData.findBone(targetName); - if (data.target == null) throw new Error("Transform constraint target bone not found: " + targetName); + if (data.target == null) + throw new Error( + "Transform constraint target bone not found: " + + targetName + ); data.local = getValue(constraintMap, "local", false); data.relative = getValue(constraintMap, "relative", false); @@ -177,7 +234,11 @@ export class SkeletonJson { data.mixX = getValue(constraintMap, "mixX", 1); data.mixY = getValue(constraintMap, "mixY", data.mixX); data.mixScaleX = getValue(constraintMap, "mixScaleX", 1); - data.mixScaleY = getValue(constraintMap, "mixScaleY", data.mixScaleX); + data.mixScaleY = getValue( + constraintMap, + "mixScaleY", + data.mixScaleX + ); data.mixShearY = getValue(constraintMap, "mixShearY", 1); skeletonData.transformConstraints.push(data); @@ -195,22 +256,42 @@ export class SkeletonJson { for (let ii = 0; ii < constraintMap.bones.length; ii++) { let boneName = constraintMap.bones[ii]; let bone = skeletonData.findBone(boneName); - if (bone == null) throw new Error("Transform constraint bone not found: " + boneName); + if (bone == null) + throw new Error( + "Transform constraint bone not found: " + boneName + ); data.bones.push(bone); } let targetName: string = constraintMap.target; data.target = skeletonData.findSlot(targetName); - if (data.target == null) throw new Error("Path target slot not found: " + targetName); - - data.positionMode = Utils.enumValue(PositionMode, getValue(constraintMap, "positionMode", "Percent")); - data.spacingMode = Utils.enumValue(SpacingMode, getValue(constraintMap, "spacingMode", "Length")); - data.rotateMode = Utils.enumValue(RotateMode, getValue(constraintMap, "rotateMode", "Tangent")); + if (data.target == null) + throw new Error( + "Path target slot not found: " + targetName + ); + + data.positionMode = Utils.enumValue( + PositionMode, + getValue(constraintMap, "positionMode", "Percent") + ); + data.spacingMode = Utils.enumValue( + SpacingMode, + getValue(constraintMap, "spacingMode", "Length") + ); + data.rotateMode = Utils.enumValue( + RotateMode, + getValue(constraintMap, "rotateMode", "Tangent") + ); data.offsetRotation = getValue(constraintMap, "rotation", 0); data.position = getValue(constraintMap, "position", 0); - if (data.positionMode == PositionMode.Fixed) data.position *= scale; + if (data.positionMode == PositionMode.Fixed) + data.position *= scale; data.spacing = getValue(constraintMap, "spacing", 0); - if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) data.spacing *= scale; + if ( + data.spacingMode == SpacingMode.Length || + data.spacingMode == SpacingMode.Fixed + ) + data.spacing *= scale; data.mixRotate = getValue(constraintMap, "mixRotate", 1); data.mixX = getValue(constraintMap, "mixX", 1); data.mixY = getValue(constraintMap, "mixY", data.mixX); @@ -222,48 +303,80 @@ export class SkeletonJson { // Skins. if (root.skins) { for (let i = 0; i < root.skins.length; i++) { - let skinMap = root.skins[i] + let skinMap = root.skins[i]; let skin = new Skin(skinMap.name); if (skinMap.bones) { for (let ii = 0; ii < skinMap.bones.length; ii++) { let bone = skeletonData.findBone(skinMap.bones[ii]); - if (bone == null) throw new Error("Skin bone not found: " + skinMap.bones[i]); + if (bone == null) + throw new Error( + "Skin bone not found: " + skinMap.bones[i] + ); skin.bones.push(bone); } } if (skinMap.ik) { for (let ii = 0; ii < skinMap.ik.length; ii++) { - let constraint = skeletonData.findIkConstraint(skinMap.ik[ii]); - if (constraint == null) throw new Error("Skin IK constraint not found: " + skinMap.ik[i]); + let constraint = skeletonData.findIkConstraint( + skinMap.ik[ii] + ); + if (constraint == null) + throw new Error( + "Skin IK constraint not found: " + skinMap.ik[i] + ); skin.constraints.push(constraint); } } if (skinMap.transform) { for (let ii = 0; ii < skinMap.transform.length; ii++) { - let constraint = skeletonData.findTransformConstraint(skinMap.transform[ii]); - if (constraint == null) throw new Error("Skin transform constraint not found: " + skinMap.transform[i]); + let constraint = skeletonData.findTransformConstraint( + skinMap.transform[ii] + ); + if (constraint == null) + throw new Error( + "Skin transform constraint not found: " + + skinMap.transform[i] + ); skin.constraints.push(constraint); } } if (skinMap.path) { for (let ii = 0; ii < skinMap.path.length; ii++) { - let constraint = skeletonData.findPathConstraint(skinMap.path[ii]); - if (constraint == null) throw new Error("Skin path constraint not found: " + skinMap.path[i]); + let constraint = skeletonData.findPathConstraint( + skinMap.path[ii] + ); + if (constraint == null) + throw new Error( + "Skin path constraint not found: " + + skinMap.path[i] + ); skin.constraints.push(constraint); } } for (let slotName in skinMap.attachments) { let slot = skeletonData.findSlot(slotName); - if (slot == null) throw new Error("Slot not found: " + slotName); + if (slot == null) + throw new Error("Slot not found: " + slotName); let slotMap = skinMap.attachments[slotName]; for (let entryName in slotMap) { - let attachment = this.readAttachment(slotMap[entryName], skin, slot.index, entryName, skeletonData); - if (attachment) skin.setAttachment(slot.index, entryName, attachment); + let attachment = this.readAttachment( + slotMap[entryName], + skin, + slot.index, + entryName, + skeletonData + ); + if (attachment) + skin.setAttachment( + slot.index, + entryName, + attachment + ); } } skeletonData.skins.push(skin); @@ -274,10 +387,17 @@ export class SkeletonJson { // Linked meshes. for (let i = 0, n = this.linkedMeshes.length; i < n; i++) { let linkedMesh = this.linkedMeshes[i]; - let skin = !linkedMesh.skin ? skeletonData.defaultSkin : skeletonData.findSkin(linkedMesh.skin); - let parent = skin.getAttachment(linkedMesh.slotIndex, linkedMesh.parent); - linkedMesh.mesh.deformAttachment = linkedMesh.inheritDeform ? parent : linkedMesh.mesh; - linkedMesh.mesh.setParentMesh( parent); + let skin = !linkedMesh.skin + ? skeletonData.defaultSkin + : skeletonData.findSkin(linkedMesh.skin); + let parent = skin.getAttachment( + linkedMesh.slotIndex, + linkedMesh.parent + ); + linkedMesh.mesh.deformAttachment = linkedMesh.inheritDeform + ? parent + : linkedMesh.mesh; + linkedMesh.mesh.setParentMesh(parent); // linkedMesh.mesh.updateUVs(); } this.linkedMeshes.length = 0; @@ -310,14 +430,24 @@ export class SkeletonJson { return skeletonData; } - readAttachment (map: any, skin: Skin, slotIndex: number, name: string, skeletonData: SkeletonData): Attachment { + readAttachment( + map: any, + skin: Skin, + slotIndex: number, + name: string, + skeletonData: SkeletonData + ): Attachment { let scale = this.scale; name = getValue(map, "name", name); switch (getValue(map, "type", "region")) { case "region": { let path = getValue(map, "path", name); - let region = this.attachmentLoader.newRegionAttachment(skin, name, path); + let region = this.attachmentLoader.newRegionAttachment( + skin, + name, + path + ); if (!region) return null; region.path = path; region.x = getValue(map, "x", 0) * scale; @@ -335,7 +465,10 @@ export class SkeletonJson { return region; } case "boundingbox": { - let box = this.attachmentLoader.newBoundingBoxAttachment(skin, name); + let box = this.attachmentLoader.newBoundingBoxAttachment( + skin, + name + ); if (!box) return null; this.readVertices(map, box, map.vertexCount << 1); let color: string = getValue(map, "color", null); @@ -345,7 +478,11 @@ export class SkeletonJson { case "mesh": case "linkedmesh": { let path = getValue(map, "path", name); - let mesh = this.attachmentLoader.newMeshAttachment(skin, name, path); + let mesh = this.attachmentLoader.newMeshAttachment( + skin, + name, + path + ); if (!mesh) return null; mesh.path = path; @@ -357,7 +494,15 @@ export class SkeletonJson { let parent: string = getValue(map, "parent", null); if (parent) { - this.linkedMeshes.push(new LinkedMesh(mesh, getValue(map, "skin", null), slotIndex, parent, getValue(map, "deform", true))); + this.linkedMeshes.push( + new LinkedMesh( + mesh, + getValue(map, "skin", null), + slotIndex, + parent, + getValue(map, "deform", true) + ) + ); return mesh; } @@ -390,7 +535,10 @@ export class SkeletonJson { return path; } case "point": { - let point = this.attachmentLoader.newPointAttachment(skin, name); + let point = this.attachmentLoader.newPointAttachment( + skin, + name + ); if (!point) return null; point.x = getValue(map, "x", 0) * scale; point.y = getValue(map, "y", 0) * scale; @@ -401,13 +549,17 @@ export class SkeletonJson { return point; } case "clipping": { - let clip = this.attachmentLoader.newClippingAttachment(skin, name); + let clip = this.attachmentLoader.newClippingAttachment( + skin, + name + ); if (!clip) return null; let end = getValue(map, "end", null); if (end != null) { let slot = skeletonData.findSlot(end); - if (slot == null) throw new Error("Clipping end slot not found: " + end); + if (slot == null) + throw new Error("Clipping end slot not found: " + end); clip.endSlot = slot; } @@ -422,7 +574,11 @@ export class SkeletonJson { return null; } - readVertices (map: any, attachment: VertexAttachment, verticesLength: number) { + readVertices( + map: any, + attachment: VertexAttachment, + verticesLength: number + ) { let scale = this.scale; attachment.worldVerticesLength = verticesLength; let vertices: Array = map.vertices; @@ -437,7 +593,7 @@ export class SkeletonJson { } let weights = new Array(); let bones = new Array(); - for (let i = 0, n = vertices.length; i < n;) { + for (let i = 0, n = vertices.length; i < n; ) { let boneCount = vertices[i++]; bones.push(boneCount); for (let nn = i + boneCount * 4; i < nn; i += 4) { @@ -451,7 +607,7 @@ export class SkeletonJson { attachment.vertices = Utils.toFloatArray(weights); } - readAnimation (map: any, name: string, skeletonData: SkeletonData) { + readAnimation(map: any, name: string, skeletonData: SkeletonData) { let scale = this.scale; let timelines = new Array(); @@ -460,27 +616,45 @@ export class SkeletonJson { for (let slotName in map.slots) { let slotMap = map.slots[slotName]; let slotIndex = skeletonData.findSlot(slotName).index; - if (slotIndex == -1) throw new Error("Slot not found: " + slotName); + if (slotIndex == -1) + throw new Error("Slot not found: " + slotName); for (let timelineName in slotMap) { let timelineMap = slotMap[timelineName]; if (!timelineMap) continue; let frames = timelineMap.length; if (timelineName == "attachment") { - let timeline = new AttachmentTimeline(frames, slotIndex); + let timeline = new AttachmentTimeline( + frames, + slotIndex + ); for (let frame = 0; frame < frames; frame++) { let keyMap = timelineMap[frame]; - timeline.setFrame(frame, getValue(keyMap, "time", 0), keyMap.name); + timeline.setFrame( + frame, + getValue(keyMap, "time", 0), + keyMap.name + ); } timelines.push(timeline); - } else if (timelineName == "rgba") { - let timeline = new RGBATimeline(frames, frames << 2, slotIndex); + let timeline = new RGBATimeline( + frames, + frames << 2, + slotIndex + ); let keyMap = timelineMap[0]; let time = getValue(keyMap, "time", 0); let color = Color.fromString(keyMap.color); for (let frame = 0, bezier = 0; ; frame++) { - timeline.setFrame(frame, time, color.r, color.g, color.b, color.a); + timeline.setFrame( + frame, + time, + color.r, + color.g, + color.b, + color.a + ); let nextMap = timelineMap[frame + 1]; if (!nextMap) { timeline.shrink(bezier); @@ -490,10 +664,54 @@ export class SkeletonJson { let newColor = Color.fromString(nextMap.color); let curve = keyMap.curve; if (curve) { - bezier = readCurve(curve, timeline, bezier, frame, 0, time, time2, color.r, newColor.r, 1); - bezier = readCurve(curve, timeline, bezier, frame, 1, time, time2, color.g, newColor.g, 1); - bezier = readCurve(curve, timeline, bezier, frame, 2, time, time2, color.b, newColor.b, 1); - bezier = readCurve(curve, timeline, bezier, frame, 3, time, time2, color.a, newColor.a, 1); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 0, + time, + time2, + color.r, + newColor.r, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 1, + time, + time2, + color.g, + newColor.g, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 2, + time, + time2, + color.b, + newColor.b, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 3, + time, + time2, + color.a, + newColor.a, + 1 + ); } time = time2; color = newColor; @@ -501,15 +719,24 @@ export class SkeletonJson { } timelines.push(timeline); - } else if (timelineName == "rgb") { - let timeline = new RGBTimeline(frames, frames * 3, slotIndex); + let timeline = new RGBTimeline( + frames, + frames * 3, + slotIndex + ); let keyMap = timelineMap[0]; let time = getValue(keyMap, "time", 0); let color = Color.fromString(keyMap.color); for (let frame = 0, bezier = 0; ; frame++) { - timeline.setFrame(frame, time, color.r, color.g, color.b); + timeline.setFrame( + frame, + time, + color.r, + color.g, + color.b + ); let nextMap = timelineMap[frame + 1]; if (!nextMap) { timeline.shrink(bezier); @@ -519,9 +746,42 @@ export class SkeletonJson { let newColor = Color.fromString(nextMap.color); let curve = keyMap.curve; if (curve) { - bezier = readCurve(curve, timeline, bezier, frame, 0, time, time2, color.r, newColor.r, 1); - bezier = readCurve(curve, timeline, bezier, frame, 1, time, time2, color.g, newColor.g, 1); - bezier = readCurve(curve, timeline, bezier, frame, 2, time, time2, color.b, newColor.b, 1); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 0, + time, + time2, + color.r, + newColor.r, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 1, + time, + time2, + color.g, + newColor.g, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 2, + time, + time2, + color.b, + newColor.b, + 1 + ); } time = time2; color = newColor; @@ -529,11 +789,21 @@ export class SkeletonJson { } timelines.push(timeline); - } else if (timelineName == "alpha") { - timelines.push(readTimeline1(timelineMap, new AlphaTimeline(frames, frames, slotIndex), 0, 1)); + timelines.push( + readTimeline1( + timelineMap, + new AlphaTimeline(frames, frames, slotIndex), + 0, + 1 + ) + ); } else if (timelineName == "rgba2") { - let timeline = new RGBA2Timeline(frames, frames * 7, slotIndex); + let timeline = new RGBA2Timeline( + frames, + frames * 7, + slotIndex + ); let keyMap = timelineMap[0]; let time = getValue(keyMap, "time", 0); @@ -541,7 +811,17 @@ export class SkeletonJson { let color2 = Color.fromString(keyMap.dark); for (let frame = 0, bezier = 0; ; frame++) { - timeline.setFrame(frame, time, color.r, color.g, color.b, color.a, color2.r, color2.g, color2.b); + timeline.setFrame( + frame, + time, + color.r, + color.g, + color.b, + color.a, + color2.r, + color2.g, + color2.b + ); let nextMap = timelineMap[frame + 1]; if (!nextMap) { timeline.shrink(bezier); @@ -552,13 +832,90 @@ export class SkeletonJson { let newColor2 = Color.fromString(nextMap.dark); let curve = keyMap.curve; if (curve) { - bezier = readCurve(curve, timeline, bezier, frame, 0, time, time2, color.r, newColor.r, 1); - bezier = readCurve(curve, timeline, bezier, frame, 1, time, time2, color.g, newColor.g, 1); - bezier = readCurve(curve, timeline, bezier, frame, 2, time, time2, color.b, newColor.b, 1); - bezier = readCurve(curve, timeline, bezier, frame, 3, time, time2, color.a, newColor.a, 1); - bezier = readCurve(curve, timeline, bezier, frame, 4, time, time2, color2.r, newColor2.r, 1); - bezier = readCurve(curve, timeline, bezier, frame, 5, time, time2, color2.g, newColor2.g, 1); - bezier = readCurve(curve, timeline, bezier, frame, 6, time, time2, color2.b, newColor2.b, 1); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 0, + time, + time2, + color.r, + newColor.r, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 1, + time, + time2, + color.g, + newColor.g, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 2, + time, + time2, + color.b, + newColor.b, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 3, + time, + time2, + color.a, + newColor.a, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 4, + time, + time2, + color2.r, + newColor2.r, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 5, + time, + time2, + color2.g, + newColor2.g, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 6, + time, + time2, + color2.b, + newColor2.b, + 1 + ); } time = time2; color = newColor; @@ -567,9 +924,12 @@ export class SkeletonJson { } timelines.push(timeline); - } else if (timelineName == "rgb2") { - let timeline = new RGB2Timeline(frames, frames * 6, slotIndex); + let timeline = new RGB2Timeline( + frames, + frames * 6, + slotIndex + ); let keyMap = timelineMap[0]; let time = getValue(keyMap, "time", 0); @@ -577,7 +937,16 @@ export class SkeletonJson { let color2 = Color.fromString(keyMap.dark); for (let frame = 0, bezier = 0; ; frame++) { - timeline.setFrame(frame, time, color.r, color.g, color.b, color2.r, color2.g, color2.b); + timeline.setFrame( + frame, + time, + color.r, + color.g, + color.b, + color2.r, + color2.g, + color2.b + ); let nextMap = timelineMap[frame + 1]; if (!nextMap) { timeline.shrink(bezier); @@ -588,12 +957,78 @@ export class SkeletonJson { let newColor2 = Color.fromString(nextMap.dark); let curve = keyMap.curve; if (curve) { - bezier = readCurve(curve, timeline, bezier, frame, 0, time, time2, color.r, newColor.r, 1); - bezier = readCurve(curve, timeline, bezier, frame, 1, time, time2, color.g, newColor.g, 1); - bezier = readCurve(curve, timeline, bezier, frame, 2, time, time2, color.b, newColor.b, 1); - bezier = readCurve(curve, timeline, bezier, frame, 3, time, time2, color2.r, newColor2.r, 1); - bezier = readCurve(curve, timeline, bezier, frame, 4, time, time2, color2.g, newColor2.g, 1); - bezier = readCurve(curve, timeline, bezier, frame, 5, time, time2, color2.b, newColor2.b, 1); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 0, + time, + time2, + color.r, + newColor.r, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 1, + time, + time2, + color.g, + newColor.g, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 2, + time, + time2, + color.b, + newColor.b, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 3, + time, + time2, + color2.r, + newColor2.r, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 4, + time, + time2, + color2.g, + newColor2.g, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 5, + time, + time2, + color2.b, + newColor2.b, + 1 + ); } time = time2; color = newColor; @@ -603,7 +1038,13 @@ export class SkeletonJson { timelines.push(timeline); } else - throw new Error("Invalid timeline type for a slot: " + timelineName + " (" + slotName + ")"); + throw new Error( + "Invalid timeline type for a slot: " + + timelineName + + " (" + + slotName + + ")" + ); } } } @@ -613,41 +1054,110 @@ export class SkeletonJson { for (let boneName in map.bones) { let boneMap = map.bones[boneName]; let boneIndex = skeletonData.findBone(boneName).index; - if (boneIndex == -1) throw new Error("Bone not found: " + boneName); + if (boneIndex == -1) + throw new Error("Bone not found: " + boneName); for (let timelineName in boneMap) { let timelineMap = boneMap[timelineName]; let frames = timelineMap.length; if (frames == 0) continue; if (timelineName === "rotate") { - timelines.push(readTimeline1(timelineMap, new RotateTimeline(frames, frames, boneIndex), 0, 1)); + timelines.push( + readTimeline1( + timelineMap, + new RotateTimeline(frames, frames, boneIndex), + 0, + 1 + ) + ); } else if (timelineName === "translate") { - let timeline = new TranslateTimeline(frames, frames << 1, boneIndex); - timelines.push(readTimeline2(timelineMap, timeline, "x", "y", 0, scale)); + let timeline = new TranslateTimeline( + frames, + frames << 1, + boneIndex + ); + timelines.push( + readTimeline2( + timelineMap, + timeline, + "x", + "y", + 0, + scale + ) + ); } else if (timelineName === "translatex") { - let timeline = new TranslateXTimeline(frames, frames, boneIndex); - timelines.push(readTimeline1(timelineMap, timeline, 0, scale)); + let timeline = new TranslateXTimeline( + frames, + frames, + boneIndex + ); + timelines.push( + readTimeline1(timelineMap, timeline, 0, scale) + ); } else if (timelineName === "translatey") { - let timeline = new TranslateYTimeline(frames, frames, boneIndex); - timelines.push(readTimeline1(timelineMap, timeline, 0, scale)); + let timeline = new TranslateYTimeline( + frames, + frames, + boneIndex + ); + timelines.push( + readTimeline1(timelineMap, timeline, 0, scale) + ); } else if (timelineName === "scale") { - let timeline = new ScaleTimeline(frames, frames << 1, boneIndex); - timelines.push(readTimeline2(timelineMap, timeline, "x", "y", 1, 1)); + let timeline = new ScaleTimeline( + frames, + frames << 1, + boneIndex + ); + timelines.push( + readTimeline2(timelineMap, timeline, "x", "y", 1, 1) + ); } else if (timelineName === "scalex") { - let timeline = new ScaleXTimeline(frames, frames, boneIndex); - timelines.push(readTimeline1(timelineMap, timeline, 1, 1)); + let timeline = new ScaleXTimeline( + frames, + frames, + boneIndex + ); + timelines.push( + readTimeline1(timelineMap, timeline, 1, 1) + ); } else if (timelineName === "scaley") { - let timeline = new ScaleYTimeline(frames, frames, boneIndex); - timelines.push(readTimeline1(timelineMap, timeline, 1, 1)); + let timeline = new ScaleYTimeline( + frames, + frames, + boneIndex + ); + timelines.push( + readTimeline1(timelineMap, timeline, 1, 1) + ); } else if (timelineName === "shear") { - let timeline = new ShearTimeline(frames, frames << 1, boneIndex); - timelines.push(readTimeline2(timelineMap, timeline, "x", "y", 0, 1)); + let timeline = new ShearTimeline( + frames, + frames << 1, + boneIndex + ); + timelines.push( + readTimeline2(timelineMap, timeline, "x", "y", 0, 1) + ); } else if (timelineName === "shearx") { - let timeline = new ShearXTimeline(frames, frames, boneIndex); - timelines.push(readTimeline1(timelineMap, timeline, 0, 1)); + let timeline = new ShearXTimeline( + frames, + frames, + boneIndex + ); + timelines.push( + readTimeline1(timelineMap, timeline, 0, 1) + ); } else if (timelineName === "sheary") { - let timeline = new ShearYTimeline(frames, frames, boneIndex); - timelines.push(readTimeline1(timelineMap, timeline, 0, 1)); + let timeline = new ShearYTimeline( + frames, + frames, + boneIndex + ); + timelines.push( + readTimeline1(timelineMap, timeline, 0, 1) + ); } } } @@ -661,15 +1171,28 @@ export class SkeletonJson { if (!keyMap) continue; let constraint = skeletonData.findIkConstraint(constraintName); - let constraintIndex = skeletonData.ikConstraints.indexOf(constraint); - let timeline = new IkConstraintTimeline(constraintMap.length, constraintMap.length << 1, constraintIndex); + let constraintIndex = + skeletonData.ikConstraints.indexOf(constraint); + let timeline = new IkConstraintTimeline( + constraintMap.length, + constraintMap.length << 1, + constraintIndex + ); let time = getValue(keyMap, "time", 0); let mix = getValue(keyMap, "mix", 1); let softness = getValue(keyMap, "softness", 0) * scale; for (let frame = 0, bezier = 0; ; frame++) { - timeline.setFrame(frame, time, mix, softness, getValue(keyMap, "bendPositive", true) ? 1 : -1, getValue(keyMap, "compress", false), getValue(keyMap, "stretch", false)); + timeline.setFrame( + frame, + time, + mix, + softness, + getValue(keyMap, "bendPositive", true) ? 1 : -1, + getValue(keyMap, "compress", false), + getValue(keyMap, "stretch", false) + ); let nextMap = constraintMap[frame + 1]; if (!nextMap) { timeline.shrink(bezier); @@ -681,8 +1204,30 @@ export class SkeletonJson { let softness2 = getValue(nextMap, "softness", 0) * scale; let curve = keyMap.curve; if (curve) { - bezier = readCurve(curve, timeline, bezier, frame, 0, time, time2, mix, mix2, 1); - bezier = readCurve(curve, timeline, bezier, frame, 1, time, time2, softness, softness2, scale); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 0, + time, + time2, + mix, + mix2, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 1, + time, + time2, + softness, + softness2, + scale + ); } time = time2; @@ -701,9 +1246,15 @@ export class SkeletonJson { let keyMap = timelineMap[0]; if (!keyMap) continue; - let constraint = skeletonData.findTransformConstraint(constraintName); - let constraintIndex = skeletonData.transformConstraints.indexOf(constraint); - let timeline = new TransformConstraintTimeline(timelineMap.length, timelineMap.length * 6, constraintIndex); + let constraint = + skeletonData.findTransformConstraint(constraintName); + let constraintIndex = + skeletonData.transformConstraints.indexOf(constraint); + let timeline = new TransformConstraintTimeline( + timelineMap.length, + timelineMap.length * 6, + constraintIndex + ); let time = getValue(keyMap, "time", 0); let mixRotate = getValue(keyMap, "mixRotate", 1); @@ -714,7 +1265,16 @@ export class SkeletonJson { let mixShearY = getValue(keyMap, "mixShearY", 1); for (let frame = 0, bezier = 0; ; frame++) { - timeline.setFrame(frame, time, mixRotate, mixX, mixY, mixScaleX, mixScaleY, mixShearY); + timeline.setFrame( + frame, + time, + mixRotate, + mixX, + mixY, + mixScaleX, + mixScaleY, + mixShearY + ); let nextMap = timelineMap[frame + 1]; if (!nextMap) { timeline.shrink(bezier); @@ -730,12 +1290,78 @@ export class SkeletonJson { let mixShearY2 = getValue(nextMap, "mixShearY", 1); let curve = keyMap.curve; if (curve) { - bezier = readCurve(curve, timeline, bezier, frame, 0, time, time2, mixRotate, mixRotate2, 1); - bezier = readCurve(curve, timeline, bezier, frame, 1, time, time2, mixX, mixX2, 1); - bezier = readCurve(curve, timeline, bezier, frame, 2, time, time2, mixY, mixY2, 1); - bezier = readCurve(curve, timeline, bezier, frame, 3, time, time2, mixScaleX, mixScaleX2, 1); - bezier = readCurve(curve, timeline, bezier, frame, 4, time, time2, mixScaleY, mixScaleY2, 1); - bezier = readCurve(curve, timeline, bezier, frame, 5, time, time2, mixShearY, mixShearY2, 1); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 0, + time, + time2, + mixRotate, + mixRotate2, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 1, + time, + time2, + mixX, + mixX2, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 2, + time, + time2, + mixY, + mixY2, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 3, + time, + time2, + mixScaleX, + mixScaleX2, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 4, + time, + time2, + mixScaleY, + mixScaleY2, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 5, + time, + time2, + mixShearY, + mixShearY2, + 1 + ); } time = time2; @@ -755,8 +1381,12 @@ export class SkeletonJson { if (map.path) { for (let constraintName in map.path) { let constraintMap = map.path[constraintName]; - let constraintIndex = skeletonData.findPathConstraintIndex(constraintName); - if (constraintIndex == -1) throw new Error("Path constraint not found: " + constraintName); + let constraintIndex = + skeletonData.findPathConstraintIndex(constraintName); + if (constraintIndex == -1) + throw new Error( + "Path constraint not found: " + constraintName + ); let constraint = skeletonData.pathConstraints[constraintIndex]; for (let timelineName in constraintMap) { let timelineMap = constraintMap[timelineName]; @@ -765,19 +1395,56 @@ export class SkeletonJson { let frames = timelineMap.length; if (timelineName === "position") { - let timeline = new PathConstraintPositionTimeline(frames, frames, constraintIndex); - timelines.push(readTimeline1(timelineMap, timeline, 0, constraint.positionMode == PositionMode.Fixed ? scale : 1)); + let timeline = new PathConstraintPositionTimeline( + frames, + frames, + constraintIndex + ); + timelines.push( + readTimeline1( + timelineMap, + timeline, + 0, + constraint.positionMode == PositionMode.Fixed + ? scale + : 1 + ) + ); } else if (timelineName === "spacing") { - let timeline = new PathConstraintSpacingTimeline(frames, frames, constraintIndex); - timelines.push(readTimeline1(timelineMap, timeline, 0, constraint.spacingMode == SpacingMode.Length || constraint.spacingMode == SpacingMode.Fixed ? scale : 1)); + let timeline = new PathConstraintSpacingTimeline( + frames, + frames, + constraintIndex + ); + timelines.push( + readTimeline1( + timelineMap, + timeline, + 0, + constraint.spacingMode == SpacingMode.Length || + constraint.spacingMode == SpacingMode.Fixed + ? scale + : 1 + ) + ); } else if (timelineName === "mix") { - let timeline = new PathConstraintMixTimeline(frames, frames * 3, constraintIndex); + let timeline = new PathConstraintMixTimeline( + frames, + frames * 3, + constraintIndex + ); let time = getValue(keyMap, "time", 0); let mixRotate = getValue(keyMap, "mixRotate", 1); let mixX = getValue(keyMap, "mixX", 1); let mixY = getValue(keyMap, "mixY", mixX); for (let frame = 0, bezier = 0; ; frame++) { - timeline.setFrame(frame, time, mixRotate, mixX, mixY); + timeline.setFrame( + frame, + time, + mixRotate, + mixX, + mixY + ); let nextMap = timelineMap[frame + 1]; if (!nextMap) { timeline.shrink(bezier); @@ -789,9 +1456,42 @@ export class SkeletonJson { let mixY2 = getValue(nextMap, "mixY", mixX2); let curve = keyMap.curve; if (curve) { - bezier = readCurve(curve, timeline, bezier, frame, 0, time, time2, mixRotate, mixRotate2, 1); - bezier = readCurve(curve, timeline, bezier, frame, 1, time, time2, mixX, mixX2, 1); - bezier = readCurve(curve, timeline, bezier, frame, 2, time, time2, mixY, mixY2, 1); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 0, + time, + time2, + mixRotate, + mixRotate2, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 1, + time, + time2, + mixX, + mixX2, + 1 + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 2, + time, + time2, + mixY, + mixY2, + 1 + ); } time = time2; mixRotate = mixRotate2; @@ -811,11 +1511,11 @@ export class SkeletonJson { let deformMap = map.deform[deformName]; let skin = skeletonData.findSkin(deformName); if (skin == null) { - if (settings.FAIL_ON_NON_EXISTING_SKIN) { - throw new Error("Skin not found: " + deformName); - } else { - continue; - } + if (settings.FAIL_ON_NON_EXISTING_SKIN) { + throw new Error("Skin not found: " + deformName); + } else { + continue; + } } for (let slotName in deformMap) { let slotMap = deformMap[slotName]; @@ -825,24 +1525,52 @@ export class SkeletonJson { let keyMap = timelineMap[0]; if (!keyMap) continue; - let attachment = skin.getAttachment(slotIndex, timelineName); + let attachment = ( + skin.getAttachment(slotIndex, timelineName) + ); let weighted = attachment.bones; let vertices = attachment.vertices; - let deformLength = weighted ? vertices.length / 3 * 2 : vertices.length; - - let timeline = new DeformTimeline(timelineMap.length, timelineMap.length, slotIndex, attachment); + let deformLength = weighted + ? (vertices.length / 3) * 2 + : vertices.length; + + let timeline = new DeformTimeline( + timelineMap.length, + timelineMap.length, + slotIndex, + attachment + ); let time = getValue(keyMap, "time", 0); for (let frame = 0, bezier = 0; ; frame++) { let deform: NumberArrayLike; - let verticesValue: Array = getValue(keyMap, "vertices", null); + let verticesValue: Array = getValue( + keyMap, + "vertices", + null + ); if (!verticesValue) - deform = weighted ? Utils.newFloatArray(deformLength) : vertices; + deform = weighted + ? Utils.newFloatArray(deformLength) + : vertices; else { deform = Utils.newFloatArray(deformLength); - let start = getValue(keyMap, "offset", 0); - Utils.arrayCopy(verticesValue, 0, deform, start, verticesValue.length); + let start = ( + getValue(keyMap, "offset", 0) + ); + Utils.arrayCopy( + verticesValue, + 0, + deform, + start, + verticesValue.length + ); if (scale != 1) { - for (let i = start, n = i + verticesValue.length; i < n; i++) + for ( + let i = start, + n = i + verticesValue.length; + i < n; + i++ + ) deform[i] *= scale; } if (!weighted) { @@ -859,7 +1587,19 @@ export class SkeletonJson { } let time2 = getValue(nextMap, "time", 0); let curve = keyMap.curve; - if (curve) bezier = readCurve(curve, timeline, bezier, frame, 0, time, time2, 0, 1, 1); + if (curve) + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 0, + time, + time2, + 0, + 1, + 1 + ); time = time2; keyMap = nextMap; } @@ -880,25 +1620,37 @@ export class SkeletonJson { let offsets = getValue(drawOrderMap, "offsets", null); if (offsets) { drawOrder = Utils.newArray(slotCount, -1); - let unchanged = Utils.newArray(slotCount - offsets.length, 0); - let originalIndex = 0, unchangedIndex = 0; + let unchanged = Utils.newArray( + slotCount - offsets.length, + 0 + ); + let originalIndex = 0, + unchangedIndex = 0; for (let ii = 0; ii < offsets.length; ii++) { let offsetMap = offsets[ii]; - let slotIndex = skeletonData.findSlot(offsetMap.slot).index; + let slotIndex = skeletonData.findSlot( + offsetMap.slot + ).index; // Collect unchanged items. while (originalIndex != slotIndex) unchanged[unchangedIndex++] = originalIndex++; // Set changed items. - drawOrder[originalIndex + offsetMap.offset] = originalIndex++; + drawOrder[originalIndex + offsetMap.offset] = + originalIndex++; } // Collect remaining unchanged items. while (originalIndex < slotCount) unchanged[unchangedIndex++] = originalIndex++; // Fill in unchanged items. for (let ii = slotCount - 1; ii >= 0; ii--) - if (drawOrder[ii] == -1) drawOrder[ii] = unchanged[--unchangedIndex]; + if (drawOrder[ii] == -1) + drawOrder[ii] = unchanged[--unchangedIndex]; } - timeline.setFrame(frame, getValue(drawOrderMap, "time", 0), drawOrder); + timeline.setFrame( + frame, + getValue(drawOrderMap, "time", 0), + drawOrder + ); } timelines.push(timeline); } @@ -910,10 +1662,21 @@ export class SkeletonJson { for (let i = 0; i < map.events.length; i++, frame++) { let eventMap = map.events[i]; let eventData = skeletonData.findEvent(eventMap.name); - let event = new Event(Utils.toSinglePrecision(getValue(eventMap, "time", 0)), eventData); + let event = new Event( + Utils.toSinglePrecision(getValue(eventMap, "time", 0)), + eventData + ); event.intValue = getValue(eventMap, "int", eventData.intValue); - event.floatValue = getValue(eventMap, "float", eventData.floatValue); - event.stringValue = getValue(eventMap, "string", eventData.stringValue); + event.floatValue = getValue( + eventMap, + "float", + eventData.floatValue + ); + event.stringValue = getValue( + eventMap, + "string", + eventData.stringValue + ); if (event.data.audioPath) { event.volume = getValue(eventMap, "volume", 1); event.balance = getValue(eventMap, "balance", 0); @@ -932,23 +1695,30 @@ export class SkeletonJson { skeletonData.animations.push(new Animation(name, timelines, duration)); } - static blendModeFromString (str: string) { + static blendModeFromString(str: string) { str = str.toLowerCase(); - if (str == "normal") return BLEND_MODES.NORMAL; - if (str == "additive") return BLEND_MODES.ADD; - if (str == "multiply") return BLEND_MODES.MULTIPLY; - if (str == "screen") return BLEND_MODES.SCREEN; + if (str == "normal") return PIXI.BLEND_MODES.NORMAL; + if (str == "additive") return PIXI.BLEND_MODES.ADD; + if (str == "multiply") return PIXI.BLEND_MODES.MULTIPLY; + if (str == "screen") return PIXI.BLEND_MODES.SCREEN; throw new Error(`Unknown blend mode: ${str}`); } } class LinkedMesh { - parent: string; skin: string; + parent: string; + skin: string; slotIndex: number; mesh: MeshAttachment; inheritDeform: boolean; - constructor (mesh: MeshAttachment, skin: string, slotIndex: number, parent: string, inheritDeform: boolean) { + constructor( + mesh: MeshAttachment, + skin: string, + slotIndex: number, + parent: string, + inheritDeform: boolean + ) { this.mesh = mesh; this.skin = skin; this.slotIndex = slotIndex; @@ -957,7 +1727,12 @@ class LinkedMesh { } } -function readTimeline1 (keys: any[], timeline: CurveTimeline1, defaultValue: number, scale: number) { +function readTimeline1( + keys: any[], + timeline: CurveTimeline1, + defaultValue: number, + scale: number +) { let keyMap = keys[0]; let time = getValue(keyMap, "time", 0); let value = getValue(keyMap, "value", defaultValue) * scale; @@ -971,14 +1746,33 @@ function readTimeline1 (keys: any[], timeline: CurveTimeline1, defaultValue: num } let time2 = getValue(nextMap, "time", 0); let value2 = getValue(nextMap, "value", defaultValue) * scale; - if (keyMap.curve) bezier = readCurve(keyMap.curve, timeline, bezier, frame, 0, time, time2, value, value2, scale); + if (keyMap.curve) + bezier = readCurve( + keyMap.curve, + timeline, + bezier, + frame, + 0, + time, + time2, + value, + value2, + scale + ); time = time2; value = value2; keyMap = nextMap; } } -function readTimeline2 (keys: any[], timeline: CurveTimeline2, name1: string, name2: string, defaultValue: number, scale: number) { +function readTimeline2( + keys: any[], + timeline: CurveTimeline2, + name1: string, + name2: string, + defaultValue: number, + scale: number +) { let keyMap = keys[0]; let time = getValue(keyMap, "time", 0); let value1 = getValue(keyMap, name1, defaultValue) * scale; @@ -996,8 +1790,30 @@ function readTimeline2 (keys: any[], timeline: CurveTimeline2, name1: string, na let nvalue2 = getValue(nextMap, name2, defaultValue) * scale; let curve = keyMap.curve; if (curve) { - bezier = readCurve(curve, timeline, bezier, frame, 0, time, time2, value1, nvalue1, scale); - bezier = readCurve(curve, timeline, bezier, frame, 1, time, time2, value2, nvalue2, scale); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 0, + time, + time2, + value1, + nvalue1, + scale + ); + bezier = readCurve( + curve, + timeline, + bezier, + frame, + 1, + time, + time2, + value2, + nvalue2, + scale + ); } time = time2; value1 = nvalue1; @@ -1006,8 +1822,18 @@ function readTimeline2 (keys: any[], timeline: CurveTimeline2, name1: string, na } } -function readCurve (curve: any, timeline: CurveTimeline, bezier: number, frame: number, value: number, time1: number, time2: number, - value1: number, value2: number, scale: number) { +function readCurve( + curve: any, + timeline: CurveTimeline, + bezier: number, + frame: number, + value: number, + time1: number, + time2: number, + value1: number, + value2: number, + scale: number +) { if (curve == "stepped") { timeline.setStepped(frame); return bezier; @@ -1017,10 +1843,22 @@ function readCurve (curve: any, timeline: CurveTimeline, bezier: number, frame: let cy1 = curve[i + 1] * scale; let cx2 = curve[i + 2]; let cy2 = curve[i + 3] * scale; - timeline.setBezier(bezier, frame, value, time1, value1, cx1, cy1, cx2, cy2, time2, value2); + timeline.setBezier( + bezier, + frame, + value, + time1, + value1, + cx1, + cy1, + cx2, + cy2, + time2, + value2 + ); return bezier + 1; } -function getValue (map: any, property: string, defaultValue: any) { +function getValue(map: any, property: string, defaultValue: any) { return map[property] !== undefined ? map[property] : defaultValue; } diff --git a/packages/runtime-4.0/src/core/SlotData.ts b/packages/runtime-4.0/src/core/SlotData.ts index d5072e5d..5edd0e5d 100644 --- a/packages/runtime-4.0/src/core/SlotData.ts +++ b/packages/runtime-4.0/src/core/SlotData.ts @@ -1,14 +1,12 @@ -import {Color} from '@pixi-spine/base'; +import { Color } from "@pixi-spine/base"; -import type {ISlotData} from '@pixi-spine/base'; -import type {BLEND_MODES} from '@pixi/constants'; -import {BoneData} from "./BoneData"; +import type { ISlotData } from "@pixi-spine/base"; +import { BoneData } from "./BoneData"; /** Stores the setup pose for a {@link Slot}. * @public * */ export class SlotData implements ISlotData { - /** The index of the slot in {@link Skeleton#getSlots()}. */ index: number; @@ -30,9 +28,9 @@ export class SlotData implements ISlotData { attachmentName: string; /** The blend mode for drawing the slot's attachment. */ - blendMode: BLEND_MODES; + blendMode: PIXI.BLEND_MODES; - constructor (index: number, name: string, boneData: BoneData) { + constructor(index: number, name: string, boneData: BoneData) { if (index < 0) throw new Error("index must be >= 0."); if (!name) throw new Error("name cannot be null."); if (!boneData) throw new Error("boneData cannot be null."); @@ -41,4 +39,3 @@ export class SlotData implements ISlotData { this.boneData = boneData; } } -