[ts] Fixed JSON parsing of SequenceTimeline

This commit is contained in:
Mario Zechner 2021-10-13 23:53:13 +02:00
parent b48e2c33b8
commit 49f039fb6a
2 changed files with 69 additions and 55 deletions

View File

@ -41,7 +41,9 @@ import { Skin } from "./Skin";
import { SlotData, BlendMode } from "./SlotData";
import { TransformConstraintData } from "./TransformConstraintData";
import { Utils, Color, NumberArrayLike } from "./Utils";
import { Sequence } from "./attachments/Sequence";
import { Sequence, SequenceMode } from "./attachments/Sequence";
import { SequenceTimeline } from "src";
import { HasTextureRegion } from "./attachments/HasTextureRegion";
/** Loads skeleton data in the Spine JSON format.
*
@ -797,21 +799,24 @@ export class SkeletonJson {
for (let attachmentsName in map.attachments) {
let attachmentsMap = map.attachments[attachmentsName];
let skin = skeletonData.findSkin(attachmentsName);
for (let slotName in attachmentsMap) {
let slotMap = attachmentsMap[slotName];
let slotIndex = skeletonData.findSlot(slotName).index;
for (let timelineName in slotMap) {
let attachmentMap = slotMap[timelineName];
let attachmentMapName = timelineName;
let keyMap = attachmentMap[0];
for (let slotMapName in attachmentsMap) {
let slotMap = attachmentsMap[slotMapName];
let slotIndex = skeletonData.findSlot(slotMapName).index;
for (let attachmentMapName in slotMap) {
let attachmentMap = slotMap[attachmentMapName];
let attachment = <VertexAttachment>skin.getAttachment(slotIndex, attachmentMapName);
for (let timelineMapName in attachmentMap) {
let timelineMap = attachmentMap[timelineMapName];
let keyMap = timelineMap[0];
if (!keyMap) continue;
let attachment = <VertexAttachment>skin.getAttachment(slotIndex, timelineName);
if (timelineMapName == "deform") {
let weighted = attachment.bones;
let vertices = attachment.vertices;
let deformLength = weighted ? vertices.length / 3 * 2 : vertices.length;
let timeline = new DeformTimeline(attachmentMap.length, attachmentMap.length, slotIndex, attachment);
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;
@ -833,7 +838,7 @@ export class SkeletonJson {
}
timeline.setFrame(frame, time, deform);
let nextMap = attachmentMap[frame + 1];
let nextMap = timelineMap[frame + 1];
if (!nextMap) {
timeline.shrink(bezier);
break;
@ -845,6 +850,21 @@ export class SkeletonJson {
keyMap = nextMap;
}
timelines.push(timeline);
} else if (timelineMapName == "sequence") {
let timeline = new SequenceTimeline(timelineMap.length, slotIndex, attachment as unknown as HasTextureRegion);
let lastDelay = 0;
for (let frame = 0; frame < timelineMap.length; frame++) {
let delay = getValue(keyMap, "delay", lastDelay);
let time = getValue(keyMap, "time", 0);
let mode = SequenceMode[getValue(keyMap, "mode", "hold")] as unknown as number;
let index = getValue(keyMap, "index", 0);
timeline.setFrame(frame, time, mode, index, delay);
lastDelay = delay;
keyMap = timelineMap[frame + 1];
}
timelines.push(timeline);
}
}
}
}
}

View File

@ -32,25 +32,19 @@ import { Color } from "../Utils"
import { Sequence } from "./Sequence"
export interface HasTextureRegion {
/** The name used to find the {@link #getRegion()}. */
getPath (): string;
/** The name used to find the {@link #region()}. */
path: string;
setPath (path: string): void;
getRegion (): TextureRegion;
/** Sets the region used to draw the attachment. After setting the region or if the region's properties are changed,
/** The region used to draw the attachment. After setting the region or if the region's properties are changed,
* {@link #updateRegion()} must be called. */
setRegion (region: TextureRegion): void;
region: TextureRegion;
/** Updates any values the attachment calculates using the {@link #getRegion()}. Must be called after setting the
* {@link #getRegion()} or if the region's properties are changed. */
updateRegion (): void;
/** The color to tint the attachment. */
getColor (): Color;
color: Color;
getSequence (): Sequence;
setSequence (sequence: Sequence): void;
sequence: Sequence;
}