/
Bone.as
325 lines (298 loc) · 12 KB
/
Bone.as
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, 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.
*****************************************************************************/
package spine {
public class Bone implements Updatable {
static public var yDown : Boolean;
internal var _data : BoneData;
internal var _skeleton : Skeleton;
internal var _parent : Bone;
internal var _children : Vector.<Bone> = new Vector.<Bone>();
public var x : Number;
public var y : Number;
public var rotation : Number;
public var scaleX : Number;
public var scaleY : Number;
public var shearX : Number;
public var shearY : Number;
public var ax : Number;
public var ay : Number;
public var arotation : Number;
public var ascaleX : Number;
public var ascaleY : Number;
public var ashearX : Number;
public var ashearY : Number;
public var appliedValid : Boolean;
public var a : Number;
public var b : Number;
public var c : Number;
public var d : Number;
public var worldX : Number;
public var worldY : Number;
internal var _sorted : Boolean;
public var active : Boolean;
/** @param parent May be null. */
public function Bone(data : BoneData, skeleton : Skeleton, parent : Bone) {
if (data == null) throw new ArgumentError("data cannot be null.");
if (skeleton == null) throw new ArgumentError("skeleton cannot be null.");
_data = data;
_skeleton = skeleton;
_parent = parent;
setToSetupPose();
}
public function isActive() : Boolean {
return active;
}
/** Same as updateWorldTransform(). This method exists for Bone to implement Updatable. */
public function update() : void {
updateWorldTransformWith(x, y, rotation, scaleX, scaleY, shearX, shearY);
}
/** Computes the world SRT using the parent bone and this bone's local SRT. */
public function updateWorldTransform() : void {
updateWorldTransformWith(x, y, rotation, scaleX, scaleY, shearX, shearY);
}
/** Computes the world SRT using the parent bone and the specified local SRT. */
public function updateWorldTransformWith(x : Number, y : Number, rotation : Number, scaleX : Number, scaleY : Number, shearX : Number, shearY : Number) : void {
ax = x;
ay = y;
arotation = rotation;
ascaleX = scaleX;
ascaleY = scaleY;
ashearX = shearX;
ashearY = shearY;
appliedValid = true;
var rotationY : Number = 0, la : Number = 0, lb : Number = 0, lc : Number = 0, ld : Number = 0;
var sin : Number = 0, cos : Number = 0;
var s : Number = 0;
var sx : Number = _skeleton.scaleX;
var sy : Number = _skeleton.scaleY * (yDown ? -1 : 1);
var parent : Bone = _parent;
if (!parent) { // Root bone.
rotationY = rotation + 90 + shearY;
var skeleton : Skeleton = _skeleton;
this.a = MathUtils.cosDeg(rotation + shearX) * scaleX * sx;
this.b = MathUtils.cosDeg(rotationY) * scaleY * sx;
this.c = MathUtils.sinDeg(rotation + shearX) * scaleX * sy;
this.d = MathUtils.sinDeg(rotationY) * scaleY * sy;
worldX = x * sx + skeleton.x;
worldY = y * sy + skeleton.y;
return;
}
var pa : Number = parent.a, pb : Number = parent.b, pc : Number = parent.c, pd : Number = parent.d;
worldX = pa * x + pb * y + parent.worldX;
worldY = pc * x + pd * y + parent.worldY;
switch (this.data.transformMode) {
case TransformMode.normal: {
rotationY = rotation + 90 + shearY;
la = MathUtils.cosDeg(rotation + shearX) * scaleX;
lb = MathUtils.cosDeg(rotationY) * scaleY;
lc = MathUtils.sinDeg(rotation + shearX) * scaleX;
ld = MathUtils.sinDeg(rotationY) * scaleY;
this.a = pa * la + pb * lc;
this.b = pa * lb + pb * ld;
this.c = pc * la + pd * lc;
this.d = pc * lb + pd * ld;
return;
}
case TransformMode.onlyTranslation: {
rotationY = rotation + 90 + shearY;
this.a = MathUtils.cosDeg(rotation + shearX) * scaleX;
this.b = MathUtils.cosDeg(rotationY) * scaleY;
this.c = MathUtils.sinDeg(rotation + shearX) * scaleX;
this.d = MathUtils.sinDeg(rotationY) * scaleY;
break;
}
case TransformMode.noRotationOrReflection: {
s = pa * pa + pc * pc;
var prx : Number = 0;
if (s > 0.0001) {
s = Math.abs(pa * pd - pb * pc) / s;
pb = pc * s;
pd = pa * s;
prx = Math.atan2(pc, pa) * MathUtils.radDeg;
} else {
pa = 0;
pc = 0;
prx = 90 - Math.atan2(pd, pb) * MathUtils.radDeg;
}
var rx : Number = rotation + shearX - prx;
var ry : Number = rotation + shearY - prx + 90;
la = MathUtils.cosDeg(rx) * scaleX;
lb = MathUtils.cosDeg(ry) * scaleY;
lc = MathUtils.sinDeg(rx) * scaleX;
ld = MathUtils.sinDeg(ry) * scaleY;
this.a = pa * la - pb * lc;
this.b = pa * lb - pb * ld;
this.c = pc * la + pd * lc;
this.d = pc * lb + pd * ld;
return;
}
case TransformMode.noScale:
case TransformMode.noScaleOrReflection: {
cos = MathUtils.cosDeg(rotation);
sin = MathUtils.sinDeg(rotation);
var za : Number = (pa * cos + pb * sin) / sx;
var zc : Number = (pc * cos + pd * sin) / sy;
s = Math.sqrt(za * za + zc * zc);
if (s > 0.00001) s = 1 / s;
za *= s;
zc *= s;
s = Math.sqrt(za * za + zc * zc);
if (data.transformMode == TransformMode.noScale
&& (pa * pd - pb * pc < 0) != (sx < 0 != sy < 0)) s = -s;
var r : Number = Math.PI / 2 + Math.atan2(zc, za);
var zb : Number = Math.cos(r) * s;
var zd : Number = Math.sin(r) * s;
la = MathUtils.cosDeg(shearX) * scaleX;
lb = MathUtils.cosDeg(90 + shearY) * scaleY;
lc = MathUtils.sinDeg(shearX) * scaleX;
ld = MathUtils.sinDeg(90 + shearY) * scaleY;
this.a = za * la + zb * lc;
this.b = za * lb + zb * ld;
this.c = zc * la + zd * lc;
this.d = zc * lb + zd * ld;
break;
}
}
this.a *= sx;
this.b *= sx;
this.c *= sy;
this.d *= sy;
}
public function setToSetupPose() : void {
x = this.data.x;
y = this.data.y;
rotation = this.data.rotation;
scaleX = this.data.scaleX;
scaleY = this.data.scaleY;
shearX = this.data.shearX;
shearY = this.data.shearY;
}
public function get data() : BoneData {
return _data;
}
public function get skeleton() : Skeleton {
return _skeleton;
}
public function get parent() : Bone {
return _parent;
}
public function get children() : Vector.<Bone> {
;
return _children;
}
public function get worldRotationX() : Number {
return Math.atan2(this.c, this.a) * MathUtils.radDeg;
}
public function get worldRotationY() : Number {
return Math.atan2(this.d, this.b) * MathUtils.radDeg;
}
public function get worldScaleX() : Number {
return Math.sqrt(this.a * this.a + this.c * this.c);
}
public function get worldScaleY() : Number {
return Math.sqrt(this.b * this.b + this.d * this.d);
}
/** Computes the individual applied transform values from the world transform. This can be useful to perform processing using
* the applied transform after the world transform has been modified directly (eg, by a constraint).
* <p>
* Some information is ambiguous in the world transform, such as -1,-1 scale versus 180 rotation. */
internal function updateAppliedTransform() : void {
appliedValid = true;
var parent : Bone = this.parent;
if (parent == null) {
ax = worldX;
ay = worldY;
arotation = Math.atan2(c, a) * MathUtils.radDeg;
ascaleX = Math.sqrt(a * a + c * c);
ascaleY = Math.sqrt(b * b + d * d);
ashearX = 0;
ashearY = Math.atan2(a * b + c * d, a * d - b * c) * MathUtils.radDeg;
return;
}
var pa : Number = parent.a, pb : Number = parent.b, pc : Number = parent.c, pd : Number = parent.d;
var pid : Number = 1 / (pa * pd - pb * pc);
var dx : Number = worldX - parent.worldX, dy : Number = worldY - parent.worldY;
ax = (dx * pd * pid - dy * pb * pid);
ay = (dy * pa * pid - dx * pc * pid);
var ia : Number = pid * pd;
var id : Number = pid * pa;
var ib : Number = pid * pb;
var ic : Number = pid * pc;
var ra : Number = ia * a - ib * c;
var rb : Number = ia * b - ib * d;
var rc : Number = id * c - ic * a;
var rd : Number = id * d - ic * b;
ashearX = 0;
ascaleX = Math.sqrt(ra * ra + rc * rc);
if (scaleX > 0.0001) {
var det : Number = ra * rd - rb * rc;
ascaleY = det / ascaleX;
ashearY = Math.atan2(ra * rb + rc * rd, det) * MathUtils.radDeg;
arotation = Math.atan2(rc, ra) * MathUtils.radDeg;
} else {
ascaleX = 0;
ascaleY = Math.sqrt(rb * rb + rd * rd);
ashearY = 0;
arotation = 90 - Math.atan2(rd, rb) * MathUtils.radDeg;
}
}
public function worldToLocal(world : Vector.<Number>) : void {
var a : Number = this.a, b : Number = this.b, c : Number = this.c, d : Number = this.d;
var invDet : Number = 1 / (a * d - b * c);
var x : Number = world[0] - this.worldX, y : Number = world[1] - this.worldY;
world[0] = (x * d * invDet - y * b * invDet);
world[1] = (y * a * invDet - x * c * invDet);
}
public function localToWorld(local : Vector.<Number>) : void {
var localX : Number = local[0], localY : Number = local[1];
local[0] = localX * this.a + localY * this.b + this.worldX;
local[1] = localX * this.c + localY * this.d + this.worldY;
}
public function worldToLocalRotation(worldRotation : Number) : Number {
var sin : Number = MathUtils.sinDeg(worldRotation), cos : Number = MathUtils.cosDeg(worldRotation); return Math.atan2(this.a * sin - this.c * cos, this.d * cos - this.b * sin) * MathUtils.radDeg + rotation - shearX;
}
public function localToWorldRotation(localRotation : Number) : Number {
localRotation -= rotation - shearX;
var sin : Number = MathUtils.sinDeg(localRotation), cos : Number = MathUtils.cosDeg(localRotation);
return Math.atan2(cos * this.c + sin * this.d, cos * this.a + sin * this.b) * MathUtils.radDeg;
}
public function rotateWorld(degrees : Number) : void {
var a : Number = this.a, b : Number = this.b, c : Number = this.c, d : Number = this.d;
var cos : Number = MathUtils.cosDeg(degrees), sin : Number = MathUtils.sinDeg(degrees);
this.a = cos * a - sin * c;
this.b = cos * b - sin * d;
this.c = sin * a + cos * c;
this.d = sin * b + cos * d;
this.appliedValid = false;
}
public function toString() : String {
return this.data._name;
}
}
}