ngx-spine
Version:
[](https://travis-ci.org/PoiScript/ngx-spine)
1,019 lines • 163 kB
JavaScript
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated May 1, 2019. Replaces all prior versions.
*
* Copyright (c) 2013-2019, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS
* INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
import { Animation, AttachmentTimeline, ColorTimeline, TwoColorTimeline, RotateTimeline, TranslateTimeline, ScaleTimeline, ShearTimeline, IkConstraintTimeline, TransformConstraintTimeline, PathConstraintPositionTimeline, PathConstraintSpacingTimeline, PathConstraintMixTimeline, DeformTimeline, DrawOrderTimeline, EventTimeline } from "./Animation";
import { SkeletonData } from "./SkeletonData";
import { BoneData, TransformMode } from "./BoneData";
import { SlotData } from "./SlotData";
import { Color, Utils } from "./Utils";
import { IkConstraintData } from "./IkConstraintData";
import { TransformConstraintData } from "./TransformConstraintData";
import { PathConstraintData, PositionMode, SpacingMode, RotateMode } from "./PathConstraintData";
import { Skin } from "./Skin";
import { EventData } from "./EventData";
import { BlendMode } from "./BlendMode";
import { Event } from "./Event";
export class SkeletonJson {
/**
* @param {?} attachmentLoader
*/
constructor(attachmentLoader) {
this.scale = 1;
this.linkedMeshes = new Array();
this.attachmentLoader = attachmentLoader;
}
/**
* @param {?} json
* @return {?}
*/
readSkeletonData(json) {
/** @type {?} */
let scale = this.scale;
/** @type {?} */
let skeletonData = new SkeletonData();
/** @type {?} */
let root = typeof json === "string" ? JSON.parse(json) : json;
// Skeleton
/** @type {?} */
let skeletonMap = root.skeleton;
if (skeletonMap != null) {
skeletonData.hash = skeletonMap.hash;
skeletonData.version = skeletonMap.spine;
skeletonData.x = skeletonMap.x;
skeletonData.y = skeletonMap.y;
skeletonData.width = skeletonMap.width;
skeletonData.height = skeletonMap.height;
skeletonData.fps = skeletonMap.fps;
skeletonData.imagesPath = skeletonMap.images;
}
// Bones
if (root.bones) {
for (let i = 0; i < root.bones.length; i++) {
/** @type {?} */
let boneMap = root.bones[i];
/** @type {?} */
let parent = null;
/** @type {?} */
let parentName = this.getValue(boneMap, "parent", null);
if (parentName != null) {
parent = skeletonData.findBone(parentName);
if (parent == null)
throw new Error("Parent bone not found: " + parentName);
}
/** @type {?} */
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;
data.rotation = this.getValue(boneMap, "rotation", 0);
data.scaleX = this.getValue(boneMap, "scaleX", 1);
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.skinRequired = this.getValue(boneMap, "skin", false);
skeletonData.bones.push(data);
}
}
// Slots.
if (root.slots) {
for (let i = 0; i < root.slots.length; i++) {
/** @type {?} */
let slotMap = root.slots[i];
/** @type {?} */
let slotName = slotMap.name;
/** @type {?} */
let boneName = slotMap.bone;
/** @type {?} */
let boneData = skeletonData.findBone(boneName);
if (boneData == null)
throw new Error("Slot bone not found: " + boneName);
/** @type {?} */
let data = new SlotData(skeletonData.slots.length, slotName, boneData);
/** @type {?} */
let color = this.getValue(slotMap, "color", null);
if (color != null)
data.color.setFromString(color);
/** @type {?} */
let dark = this.getValue(slotMap, "dark", null);
if (dark != null) {
data.darkColor = new Color(1, 1, 1, 1);
data.darkColor.setFromString(dark);
}
data.attachmentName = this.getValue(slotMap, "attachment", null);
data.blendMode = SkeletonJson.blendModeFromString(this.getValue(slotMap, "blend", "normal"));
skeletonData.slots.push(data);
}
}
// IK constraints
if (root.ik) {
for (let i = 0; i < root.ik.length; i++) {
/** @type {?} */
let constraintMap = root.ik[i];
/** @type {?} */
let data = new IkConstraintData(constraintMap.name);
data.order = this.getValue(constraintMap, "order", 0);
data.skinRequired = this.getValue(constraintMap, "skin", false);
for (let j = 0; j < constraintMap.bones.length; j++) {
/** @type {?} */
let boneName = constraintMap.bones[j];
/** @type {?} */
let bone = skeletonData.findBone(boneName);
if (bone == null)
throw new Error("IK bone not found: " + boneName);
data.bones.push(bone);
}
/** @type {?} */
let targetName = constraintMap.target;
data.target = skeletonData.findBone(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.compress = this.getValue(constraintMap, "compress", false);
data.stretch = this.getValue(constraintMap, "stretch", false);
data.uniform = this.getValue(constraintMap, "uniform", false);
skeletonData.ikConstraints.push(data);
}
}
// Transform constraints.
if (root.transform) {
for (let i = 0; i < root.transform.length; i++) {
/** @type {?} */
let constraintMap = root.transform[i];
/** @type {?} */
let data = new TransformConstraintData(constraintMap.name);
data.order = this.getValue(constraintMap, "order", 0);
data.skinRequired = this.getValue(constraintMap, "skin", false);
for (let j = 0; j < constraintMap.bones.length; j++) {
/** @type {?} */
let boneName = constraintMap.bones[j];
/** @type {?} */
let bone = skeletonData.findBone(boneName);
if (bone == null)
throw new Error("Transform constraint bone not found: " + boneName);
data.bones.push(bone);
}
/** @type {?} */
let targetName = constraintMap.target;
data.target = skeletonData.findBone(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.offsetX = this.getValue(constraintMap, "x", 0) * scale;
data.offsetY = this.getValue(constraintMap, "y", 0) * scale;
data.offsetScaleX = this.getValue(constraintMap, "scaleX", 0);
data.offsetScaleY = this.getValue(constraintMap, "scaleY", 0);
data.offsetShearY = this.getValue(constraintMap, "shearY", 0);
data.rotateMix = this.getValue(constraintMap, "rotateMix", 1);
data.translateMix = this.getValue(constraintMap, "translateMix", 1);
data.scaleMix = this.getValue(constraintMap, "scaleMix", 1);
data.shearMix = this.getValue(constraintMap, "shearMix", 1);
skeletonData.transformConstraints.push(data);
}
}
// Path constraints.
if (root.path) {
for (let i = 0; i < root.path.length; i++) {
/** @type {?} */
let constraintMap = root.path[i];
/** @type {?} */
let data = new PathConstraintData(constraintMap.name);
data.order = this.getValue(constraintMap, "order", 0);
data.skinRequired = this.getValue(constraintMap, "skin", false);
for (let j = 0; j < constraintMap.bones.length; j++) {
/** @type {?} */
let boneName = constraintMap.bones[j];
/** @type {?} */
let bone = skeletonData.findBone(boneName);
if (bone == null)
throw new Error("Transform constraint bone not found: " + boneName);
data.bones.push(bone);
}
/** @type {?} */
let targetName = 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);
data.position = this.getValue(constraintMap, "position", 0);
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;
data.rotateMix = this.getValue(constraintMap, "rotateMix", 1);
data.translateMix = this.getValue(constraintMap, "translateMix", 1);
skeletonData.pathConstraints.push(data);
}
}
// Skins.
if (root.skins) {
for (let i = 0; i < root.skins.length; i++) {
/** @type {?} */
let skinMap = root.skins[i];
/** @type {?} */
let skin = new Skin(skinMap.name);
if (skinMap.bones) {
for (let ii = 0; ii < skinMap.bones.length; ii++) {
/** @type {?} */
let bone = skeletonData.findBone(skinMap.bones[ii]);
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++) {
/** @type {?} */
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++) {
/** @type {?} */
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++) {
/** @type {?} */
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) {
/** @type {?} */
let slot = skeletonData.findSlot(slotName);
if (slot == null)
throw new Error("Slot not found: " + slotName);
/** @type {?} */
let slotMap = skinMap.attachments[slotName];
for (let entryName in slotMap) {
/** @type {?} */
let attachment = this.readAttachment(slotMap[entryName], skin, slot.index, entryName, skeletonData);
if (attachment != null)
skin.setAttachment(slot.index, entryName, attachment);
}
}
skeletonData.skins.push(skin);
if (skin.name == "default")
skeletonData.defaultSkin = skin;
}
}
// Linked meshes.
for (let i = 0, n = this.linkedMeshes.length; i < n; i++) {
/** @type {?} */
let linkedMesh = this.linkedMeshes[i];
/** @type {?} */
let skin = linkedMesh.skin == null
? skeletonData.defaultSkin
: skeletonData.findSkin(linkedMesh.skin);
if (skin == null)
throw new Error("Skin not found: " + linkedMesh.skin);
/** @type {?} */
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
? (/** @type {?} */ (parent))
: (/** @type {?} */ (linkedMesh.mesh));
linkedMesh.mesh.setParentMesh((/** @type {?} */ (parent)));
linkedMesh.mesh.updateUVs();
}
this.linkedMeshes.length = 0;
// Events.
if (root.events) {
for (let eventName in root.events) {
/** @type {?} */
let eventMap = root.events[eventName];
/** @type {?} */
let data = new EventData(eventName);
data.intValue = this.getValue(eventMap, "int", 0);
data.floatValue = this.getValue(eventMap, "float", 0);
data.stringValue = this.getValue(eventMap, "string", "");
data.audioPath = this.getValue(eventMap, "audio", null);
if (data.audioPath != null) {
data.volume = this.getValue(eventMap, "volume", 1);
data.balance = this.getValue(eventMap, "balance", 0);
}
skeletonData.events.push(data);
}
}
// Animations.
if (root.animations) {
for (let animationName in root.animations) {
/** @type {?} */
let animationMap = root.animations[animationName];
this.readAnimation(animationMap, animationName, skeletonData);
}
}
return skeletonData;
}
/**
* @param {?} map
* @param {?} skin
* @param {?} slotIndex
* @param {?} name
* @param {?} skeletonData
* @return {?}
*/
readAttachment(map, skin, slotIndex, name, skeletonData) {
/** @type {?} */
let scale = this.scale;
name = this.getValue(map, "name", name);
/** @type {?} */
let type = this.getValue(map, "type", "region");
switch (type) {
case "region": {
/** @type {?} */
let path = this.getValue(map, "path", name);
/** @type {?} */
let region = this.attachmentLoader.newRegionAttachment(skin, name, path);
if (region == null)
return null;
region.path = path;
region.x = this.getValue(map, "x", 0) * scale;
region.y = this.getValue(map, "y", 0) * scale;
region.scaleX = this.getValue(map, "scaleX", 1);
region.scaleY = this.getValue(map, "scaleY", 1);
region.rotation = this.getValue(map, "rotation", 0);
region.width = map.width * scale;
region.height = map.height * scale;
/** @type {?} */
let color = this.getValue(map, "color", null);
if (color != null)
region.color.setFromString(color);
region.updateOffset();
return region;
}
case "boundingbox": {
/** @type {?} */
let box = this.attachmentLoader.newBoundingBoxAttachment(skin, name);
if (box == null)
return null;
this.readVertices(map, box, map.vertexCount << 1);
/** @type {?} */
let color = this.getValue(map, "color", null);
if (color != null)
box.color.setFromString(color);
return box;
}
case "mesh":
case "linkedmesh": {
/** @type {?} */
let path = this.getValue(map, "path", name);
/** @type {?} */
let mesh = this.attachmentLoader.newMeshAttachment(skin, name, path);
if (mesh == null)
return null;
mesh.path = path;
/** @type {?} */
let color = this.getValue(map, "color", null);
if (color != null)
mesh.color.setFromString(color);
mesh.width = this.getValue(map, "width", 0) * scale;
mesh.height = this.getValue(map, "height", 0) * scale;
/** @type {?} */
let parent = this.getValue(map, "parent", null);
if (parent != null) {
this.linkedMeshes.push(new LinkedMesh(mesh, (/** @type {?} */ (this.getValue(map, "skin", null))), slotIndex, parent, this.getValue(map, "deform", true)));
return mesh;
}
/** @type {?} */
let uvs = map.uvs;
this.readVertices(map, mesh, uvs.length);
mesh.triangles = map.triangles;
mesh.regionUVs = uvs;
mesh.updateUVs();
mesh.edges = this.getValue(map, "edges", null);
mesh.hullLength = this.getValue(map, "hull", 0) * 2;
return mesh;
}
case "path": {
/** @type {?} */
let path = this.attachmentLoader.newPathAttachment(skin, name);
if (path == null)
return null;
path.closed = this.getValue(map, "closed", false);
path.constantSpeed = this.getValue(map, "constantSpeed", true);
/** @type {?} */
let vertexCount = map.vertexCount;
this.readVertices(map, path, vertexCount << 1);
/** @type {?} */
let lengths = Utils.newArray(vertexCount / 3, 0);
for (let i = 0; i < map.lengths.length; i++)
lengths[i] = map.lengths[i] * scale;
path.lengths = lengths;
/** @type {?} */
let color = this.getValue(map, "color", null);
if (color != null)
path.color.setFromString(color);
return path;
}
case "point": {
/** @type {?} */
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;
point.rotation = this.getValue(map, "rotation", 0);
/** @type {?} */
let color = this.getValue(map, "color", null);
if (color != null)
point.color.setFromString(color);
return point;
}
case "clipping": {
/** @type {?} */
let clip = this.attachmentLoader.newClippingAttachment(skin, name);
if (clip == null)
return null;
/** @type {?} */
let end = this.getValue(map, "end", null);
if (end != null) {
/** @type {?} */
let slot = skeletonData.findSlot(end);
if (slot == null)
throw new Error("Clipping end slot not found: " + end);
clip.endSlot = slot;
}
/** @type {?} */
let vertexCount = map.vertexCount;
this.readVertices(map, clip, vertexCount << 1);
/** @type {?} */
let color = this.getValue(map, "color", null);
if (color != null)
clip.color.setFromString(color);
return clip;
}
}
return null;
}
/**
* @param {?} map
* @param {?} attachment
* @param {?} verticesLength
* @return {?}
*/
readVertices(map, attachment, verticesLength) {
/** @type {?} */
let scale = this.scale;
attachment.worldVerticesLength = verticesLength;
/** @type {?} */
let vertices = map.vertices;
if (verticesLength == vertices.length) {
/** @type {?} */
let scaledVertices = Utils.toFloatArray(vertices);
if (scale != 1) {
for (let i = 0, n = vertices.length; i < n; i++)
scaledVertices[i] *= scale;
}
attachment.vertices = scaledVertices;
return;
}
/** @type {?} */
let weights = new Array();
/** @type {?} */
let bones = new Array();
for (let i = 0, n = vertices.length; i < n;) {
/** @type {?} */
let boneCount = vertices[i++];
bones.push(boneCount);
for (let nn = i + boneCount * 4; i < nn; i += 4) {
bones.push(vertices[i]);
weights.push(vertices[i + 1] * scale);
weights.push(vertices[i + 2] * scale);
weights.push(vertices[i + 3]);
}
}
attachment.bones = bones;
attachment.vertices = Utils.toFloatArray(weights);
}
/**
* @param {?} map
* @param {?} name
* @param {?} skeletonData
* @return {?}
*/
readAnimation(map, name, skeletonData) {
/** @type {?} */
let scale = this.scale;
/** @type {?} */
let timelines = new Array();
/** @type {?} */
let duration = 0;
// Slot timelines.
if (map.slots) {
for (let slotName in map.slots) {
/** @type {?} */
let slotMap = map.slots[slotName];
/** @type {?} */
let slotIndex = skeletonData.findSlotIndex(slotName);
if (slotIndex == -1)
throw new Error("Slot not found: " + slotName);
for (let timelineName in slotMap) {
/** @type {?} */
let timelineMap = slotMap[timelineName];
if (timelineName == "attachment") {
/** @type {?} */
let timeline = new AttachmentTimeline(timelineMap.length);
timeline.slotIndex = slotIndex;
/** @type {?} */
let frameIndex = 0;
for (let i = 0; i < timelineMap.length; i++) {
/** @type {?} */
let valueMap = timelineMap[i];
timeline.setFrame(frameIndex++, this.getValue(valueMap, "time", 0), valueMap.name);
}
timelines.push(timeline);
duration = Math.max(duration, timeline.frames[timeline.getFrameCount() - 1]);
}
else if (timelineName == "color") {
/** @type {?} */
let timeline = new ColorTimeline(timelineMap.length);
timeline.slotIndex = slotIndex;
/** @type {?} */
let frameIndex = 0;
for (let i = 0; i < timelineMap.length; i++) {
/** @type {?} */
let valueMap = timelineMap[i];
/** @type {?} */
let color = new Color();
color.setFromString(valueMap.color);
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]);
}
else if (timelineName == "twoColor") {
/** @type {?} */
let timeline = new TwoColorTimeline(timelineMap.length);
timeline.slotIndex = slotIndex;
/** @type {?} */
let frameIndex = 0;
for (let i = 0; i < timelineMap.length; i++) {
/** @type {?} */
let valueMap = timelineMap[i];
/** @type {?} */
let light = new Color();
/** @type {?} */
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);
this.readCurve(valueMap, timeline, frameIndex);
frameIndex++;
}
timelines.push(timeline);
duration = Math.max(duration, timeline.frames[(timeline.getFrameCount() - 1) * TwoColorTimeline.ENTRIES]);
}
else
throw new Error("Invalid timeline type for a slot: " +
timelineName +
" (" +
slotName +
")");
}
}
}
// Bone timelines.
if (map.bones) {
for (let boneName in map.bones) {
/** @type {?} */
let boneMap = map.bones[boneName];
/** @type {?} */
let boneIndex = skeletonData.findBoneIndex(boneName);
if (boneIndex == -1)
throw new Error("Bone not found: " + boneName);
for (let timelineName in boneMap) {
/** @type {?} */
let timelineMap = boneMap[timelineName];
if (timelineName === "rotate") {
/** @type {?} */
let timeline = new RotateTimeline(timelineMap.length);
timeline.boneIndex = boneIndex;
/** @type {?} */
let frameIndex = 0;
for (let i = 0; i < timelineMap.length; i++) {
/** @type {?} */
let valueMap = timelineMap[i];
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") {
/** @type {?} */
let timeline = null;
/** @type {?} */
let timelineScale = 1;
/** @type {?} */
let 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);
timelineScale = scale;
}
timeline.boneIndex = boneIndex;
/** @type {?} */
let frameIndex = 0;
for (let i = 0; i < timelineMap.length; i++) {
/** @type {?} */
let valueMap = timelineMap[i];
/** @type {?} */
let x = this.getValue(valueMap, "x", defaultValue);
/** @type {?} */
let 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]);
}
else
throw new Error("Invalid timeline type for a bone: " +
timelineName +
" (" +
boneName +
")");
}
}
}
// IK constraint timelines.
if (map.ik) {
for (let constraintName in map.ik) {
/** @type {?} */
let constraintMap = map.ik[constraintName];
/** @type {?} */
let constraint = skeletonData.findIkConstraint(constraintName);
/** @type {?} */
let timeline = new IkConstraintTimeline(constraintMap.length);
timeline.ikConstraintIndex = skeletonData.ikConstraints.indexOf(constraint);
/** @type {?} */
let frameIndex = 0;
for (let i = 0; i < constraintMap.length; i++) {
/** @type {?} */
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));
this.readCurve(valueMap, timeline, frameIndex);
frameIndex++;
}
timelines.push(timeline);
duration = Math.max(duration, timeline.frames[(timeline.getFrameCount() - 1) * IkConstraintTimeline.ENTRIES]);
}
}
// Transform constraint timelines.
if (map.transform) {
for (let constraintName in map.transform) {
/** @type {?} */
let constraintMap = map.transform[constraintName];
/** @type {?} */
let constraint = skeletonData.findTransformConstraint(constraintName);
/** @type {?} */
let timeline = new TransformConstraintTimeline(constraintMap.length);
timeline.transformConstraintIndex = skeletonData.transformConstraints.indexOf(constraint);
/** @type {?} */
let frameIndex = 0;
for (let i = 0; i < constraintMap.length; i++) {
/** @type {?} */
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));
this.readCurve(valueMap, timeline, frameIndex);
frameIndex++;
}
timelines.push(timeline);
duration = Math.max(duration, timeline.frames[(timeline.getFrameCount() - 1) * TransformConstraintTimeline.ENTRIES]);
}
}
// Path constraint timelines.
if (map.path) {
for (let constraintName in map.path) {
/** @type {?} */
let constraintMap = map.path[constraintName];
/** @type {?} */
let index = skeletonData.findPathConstraintIndex(constraintName);
if (index == -1)
throw new Error("Path constraint not found: " + constraintName);
/** @type {?} */
let data = skeletonData.pathConstraints[index];
for (let timelineName in constraintMap) {
/** @type {?} */
let timelineMap = constraintMap[timelineName];
if (timelineName === "position" || timelineName === "spacing") {
/** @type {?} */
let timeline = null;
/** @type {?} */
let timelineScale = 1;
if (timelineName === "spacing") {
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.pathConstraintIndex = index;
/** @type {?} */
let frameIndex = 0;
for (let i = 0; i < timelineMap.length; i++) {
/** @type {?} */
let valueMap = timelineMap[i];
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]);
}
else if (timelineName === "mix") {
/** @type {?} */
let timeline = new PathConstraintMixTimeline(timelineMap.length);
timeline.pathConstraintIndex = index;
/** @type {?} */
let frameIndex = 0;
for (let i = 0; i < timelineMap.length; i++) {
/** @type {?} */
let valueMap = timelineMap[i];
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]);
}
}
}
}
// Deform timelines.
if (map.deform) {
for (let deformName in map.deform) {
/** @type {?} */
let deformMap = map.deform[deformName];
/** @type {?} */
let skin = skeletonData.findSkin(deformName);
if (skin == null)
throw new Error("Skin not found: " + deformName);
for (let slotName in deformMap) {
/** @type {?} */
let slotMap = deformMap[slotName];
/** @type {?} */
let slotIndex = skeletonData.findSlotIndex(slotName);
if (slotIndex == -1)
throw new Error("Slot not found: " + slotMap.name);
for (let timelineName in slotMap) {
/** @type {?} */
let timelineMap = slotMap[timelineName];
/** @type {?} */
let attachment = (/** @type {?} */ ((skin.getAttachment(slotIndex, timelineName))));
if (attachment == null)
throw new Error("Deform attachment not found: " + timelineMap.name);
/** @type {?} */
let weighted = attachment.bones != null;
/** @type {?} */
let vertices = attachment.vertices;
/** @type {?} */
let deformLength = weighted
? (vertices.length / 3) * 2
: vertices.length;
/** @type {?} */
let timeline = new DeformTimeline(timelineMap.length);
timeline.slotIndex = slotIndex;
timeline.attachment = attachment;
/** @type {?} */
let frameIndex = 0;
for (let j = 0; j < timelineMap.length; j++) {
/** @type {?} */
let valueMap = timelineMap[j];
/** @type {?} */
let deform;
/** @type {?} */
let verticesValue = this.getValue(valueMap, "vertices", null);
if (verticesValue == null)
deform = weighted
? Utils.newFloatArray(deformLength)
: vertices;
else {
deform = Utils.newFloatArray(deformLength);
/** @type {?} */
let start = (/** @type {?} */ (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++)
deform[i] *= scale;
}
if (!weighted) {
for (let i = 0; i < deformLength; i++)
deform[i] += vertices[i];
}
}
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]);
}
}
}
}
// Draw order timeline.
/** @type {?} */
let drawOrderNode = map.drawOrder;
if (drawOrderNode == null)
drawOrderNode = map.draworder;
if (drawOrderNode != null) {
/** @type {?} */
let timeline = new DrawOrderTimeline(drawOrderNode.length);
/** @type {?} */
let slotCount = skeletonData.slots.length;
/** @type {?} */
let frameIndex = 0;
for (let j = 0; j < drawOrderNode.length; j++) {
/** @type {?} */
let drawOrderMap = drawOrderNode[j];
/** @type {?} */
let drawOrder = null;
/** @type {?} */
let offsets = this.getValue(drawOrderMap, "offsets", null);
if (offsets != null) {
drawOrder = Utils.newArray(slotCount, -1);
/** @type {?} */
let unchanged = Utils.newArray(slotCount - offsets.length, 0);
/** @type {?} */
let originalIndex = 0;
/** @type {?} */
let unchangedIndex = 0;
for (let i = 0; i < offsets.length; i++) {
/** @type {?} */
let offsetMap = offsets[i];
/** @type {?} */
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++;
}
// 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];
}
timeline.setFrame(frameIndex++, this.getValue(drawOrderMap, "time", 0), drawOrder);
}
timelines.push(timeline);
duration = Math.max(duration, timeline.frames[timeline.getFrameCount() - 1]);
}
// Event timeline.
if (map.events) {
/** @type {?} */
let timeline = new EventTimeline(map.events.length);
/** @type {?} */
let frameIndex = 0;
for (let i = 0; i < map.events.length; i++) {
/** @type {?} */
let eventMap = map.events[i];
/** @type {?} */
let eventData = skeletonData.findEvent(eventMap.name);
if (eventData == null)
throw new Error("Event not found: " + eventMap.name);
/** @type {?} */
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);
}
timeline.setFrame(frameIndex++, event);
}
timelines.push(timeline);
duration = Math.max(duration, timeline.frames[timeline.getFrameCount() - 1]);
}
if (isNaN(duration)) {
throw new Error("Error while parsing animation, duration is NaN");
}
skeletonData.animations.push(new Animation(name, timelines, duration));
}
/**
* @param {?} map
* @param {?} timeline
* @param {?} frameIndex
* @return {?}
*/
readCurve(map, timeline, frameIndex) {
if (!map.curve)
return;
if (map.curve == "stepped")
timeline.setStepped(frameIndex);
else {
/** @type {?} */
let curve = map.curve;
timeline.setCurve(frameIndex, curve, this.getValue(map, "c2", 0), this.getValue(map, "c3", 1), this.getValue(map, "c4", 1));
}
}
/**
* @param {?} map
* @param {?} prop
* @param {?} defaultValue
* @return {?}
*/
getValue(map, prop, defaultValue) {
return map[prop] !== undefined ? map[prop] : defaultValue;
}
/**
* @param {?} str
* @return {?}
*/
static blendModeFromString(str) {
str = str.toLowerCase();
if (str == "normal")
return BlendMode.Normal;
if (str == "additive")
return BlendMode.Additive;
if (str == "multiply")
return BlendMode.Multiply;
if (str == "screen")
return BlendMode.Screen;
throw new Error(`Unknown blend mode: ${str}`);
}
/**
* @param {?} str
* @return {?}
*/
static positionModeFromString(str) {
str = str.toLowerCase();
if (str == "fixed")
return PositionMode.Fixed;
if (st