/
SkeletonData.ts
229 lines (197 loc) · 9.12 KB
/
SkeletonData.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated July 28, 2023. Replaces all prior versions.
*
* Copyright (c) 2013-2023, 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.
*
* THE SPINE RUNTIMES ARE 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 THE
* SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
import { Animation } from "./Animation"
import { BoneData } from "./BoneData.js";
import { EventData } from "./EventData.js";
import { IkConstraintData } from "./IkConstraintData.js";
import { PathConstraintData } from "./PathConstraintData.js";
import { PhysicsConstraintData } from "./PhysicsConstraintData.js";
import { Skin } from "./Skin.js";
import { SlotData } from "./SlotData.js";
import { TransformConstraintData } from "./TransformConstraintData.js";
/** Stores the setup pose and all of the stateless data for a skeleton.
*
* See [Data objects](http://esotericsoftware.com/spine-runtime-architecture#Data-objects) in the Spine Runtimes
* Guide. */
export class SkeletonData {
/** The skeleton's name, which by default is the name of the skeleton data file, if possible. May be null. */
name: string | null = null;
/** The skeleton's bones, sorted parent first. The root bone is always the first bone. */
bones = new Array<BoneData>(); // Ordered parents first.
/** The skeleton's slots in the setup pose draw order. */
slots = new Array<SlotData>(); // Setup pose draw order.
skins = new Array<Skin>();
/** The skeleton's default skin. By default this skin contains all attachments that were not in a skin in Spine.
*
* See {@link Skeleton#getAttachmentByName()}.
* May be null. */
defaultSkin: Skin | null = null;
/** The skeleton's events. */
events = new Array<EventData>();
/** The skeleton's animations. */
animations = new Array<Animation>();
/** The skeleton's IK constraints. */
ikConstraints = new Array<IkConstraintData>();
/** The skeleton's transform constraints. */
transformConstraints = new Array<TransformConstraintData>();
/** The skeleton's path constraints. */
pathConstraints = new Array<PathConstraintData>();
/** The skeleton's physics constraints. */
physicsConstraints = new Array<PhysicsConstraintData>();
/** The X coordinate of the skeleton's axis aligned bounding box in the setup pose. */
x: number = 0;
/** The Y coordinate of the skeleton's axis aligned bounding box in the setup pose. */
y: number = 0;
/** The width of the skeleton's axis aligned bounding box in the setup pose. */
width: number = 0;
/** The height of the skeleton's axis aligned bounding box in the setup pose. */
height: number = 0;
/** Baseline scale factor for applying distance-dependent effects on non-scalable properties, such as angle or scale. Default
* is 100. */
referenceScale = 100;
/** The Spine version used to export the skeleton data, or null. */
version: string | null = null;
/** The skeleton data hash. This value will change if any of the skeleton data has changed. May be null. */
hash: string | null = null;
// Nonessential
/** The dopesheet FPS in Spine. Available only when nonessential data was exported. */
fps = 0;
/** The path to the images directory as defined in Spine. Available only when nonessential data was exported. May be null. */
imagesPath: string | null = null;
/** The path to the audio directory as defined in Spine. Available only when nonessential data was exported. May be null. */
audioPath: string | null = null;
/** Finds a bone by comparing each bone's name. It is more efficient to cache the results of this method than to call it
* multiple times.
* @returns May be null. */
findBone (boneName: string) {
if (!boneName) throw new Error("boneName cannot be null.");
let bones = this.bones;
for (let i = 0, n = bones.length; i < n; i++) {
let bone = bones[i];
if (bone.name == boneName) return bone;
}
return null;
}
/** Finds a slot by comparing each slot's name. It is more efficient to cache the results of this method than to call it
* multiple times.
* @returns May be null. */
findSlot (slotName: string) {
if (!slotName) throw new Error("slotName cannot be null.");
let slots = this.slots;
for (let i = 0, n = slots.length; i < n; i++) {
let slot = slots[i];
if (slot.name == slotName) return slot;
}
return null;
}
/** Finds a skin by comparing each skin's name. It is more efficient to cache the results of this method than to call it
* multiple times.
* @returns May be null. */
findSkin (skinName: string) {
if (!skinName) throw new Error("skinName cannot be null.");
let skins = this.skins;
for (let i = 0, n = skins.length; i < n; i++) {
let skin = skins[i];
if (skin.name == skinName) return skin;
}
return null;
}
/** Finds an event by comparing each events's name. It is more efficient to cache the results of this method than to call it
* multiple times.
* @returns May be null. */
findEvent (eventDataName: string) {
if (!eventDataName) throw new Error("eventDataName cannot be null.");
let events = this.events;
for (let i = 0, n = events.length; i < n; i++) {
let event = events[i];
if (event.name == eventDataName) return event;
}
return null;
}
/** Finds an animation by comparing each animation's name. It is more efficient to cache the results of this method than to
* call it multiple times.
* @returns May be null. */
findAnimation (animationName: string) {
if (!animationName) throw new Error("animationName cannot be null.");
let animations = this.animations;
for (let i = 0, n = animations.length; i < n; i++) {
let animation = animations[i];
if (animation.name == animationName) return animation;
}
return null;
}
/** Finds an IK constraint by comparing each IK constraint's name. It is more efficient to cache the results of this method
* than to call it multiple times.
* @return May be null. */
findIkConstraint (constraintName: string) {
if (!constraintName) throw new Error("constraintName cannot be null.");
const ikConstraints = this.ikConstraints;
for (let i = 0, n = ikConstraints.length; i < n; i++) {
const constraint = ikConstraints[i];
if (constraint.name == constraintName) return constraint;
}
return null;
}
/** Finds a transform constraint by comparing each transform constraint's name. It is more efficient to cache the results of
* this method than to call it multiple times.
* @return May be null. */
findTransformConstraint (constraintName: string) {
if (!constraintName) throw new Error("constraintName cannot be null.");
const transformConstraints = this.transformConstraints;
for (let i = 0, n = transformConstraints.length; i < n; i++) {
const constraint = transformConstraints[i];
if (constraint.name == constraintName) return constraint;
}
return null;
}
/** Finds a path constraint by comparing each path constraint's name. It is more efficient to cache the results of this method
* than to call it multiple times.
* @return May be null. */
findPathConstraint (constraintName: string) {
if (!constraintName) throw new Error("constraintName cannot be null.");
const pathConstraints = this.pathConstraints;
for (let i = 0, n = pathConstraints.length; i < n; i++) {
const constraint = pathConstraints[i];
if (constraint.name == constraintName) return constraint;
}
return null;
}
/** Finds a physics constraint by comparing each physics constraint's name. It is more efficient to cache the results of this method
* than to call it multiple times.
* @return May be null. */
findPhysicsConstraint (constraintName: string) {
if (!constraintName) throw new Error("constraintName cannot be null.");
const physicsConstraints = this.physicsConstraints;
for (let i = 0, n = physicsConstraints.length; i < n; i++) {
const constraint = physicsConstraints[i];
if (constraint.name == constraintName) return constraint;
}
return null;
}
}