forked from cocos2d/cocos2d-html5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCCSkeleton.js
381 lines (333 loc) · 13.6 KB
/
CCSkeleton.js
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/****************************************************************************
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
Copyright (c) 2014 Shengxiang Chen (Nero Chan)
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
/**
* The main namespace of Spine, all classes, functions, properties and constants of Spine are defined in this namespace
* @namespace
* @name sp
*/
var sp = sp || {};
var spine = sp.spine;
/**
* <p>
* The skeleton of Spine. <br/>
* Skeleton has a reference to a SkeletonData and stores the state for skeleton instance,
* which consists of the current pose's bone SRT, slot colors, and which slot attachments are visible. <br/>
* Multiple skeletons can use the same SkeletonData (which includes all animations, skins, and attachments). <br/>
* </p>
* @class
* @extends cc.Node
*/
sp.Skeleton = cc.Node.extend(/** @lends sp.Skeleton# */{
_skeleton: null,
_rootBone: null,
_timeScale: 1,
_debugSlots: false,
_debugBones: false,
_premultipliedAlpha: false,
_ownsSkeletonData: null,
_atlas: null,
/**
* The constructor of sp.Skeleton. override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
*/
ctor:function(skeletonDataFile, atlasFile, scale){
cc.Node.prototype.ctor.call(this);
if(arguments.length === 0)
this.init();
else
this.initWithArgs(skeletonDataFile, atlasFile, scale);
},
_createRenderCmd:function () {
if(cc._renderType === cc.game.RENDER_TYPE_CANVAS)
return new sp.Skeleton.CanvasRenderCmd(this);
else
return new sp.Skeleton.WebGLRenderCmd(this);
},
/**
* Initializes a sp.Skeleton. please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
*/
init: function () {
cc.Node.prototype.init.call(this);
this._premultipliedAlpha = (cc._renderType === cc.game.RENDER_TYPE_WEBGL && cc.OPTIMIZE_BLEND_FUNC_FOR_PREMULTIPLIED_ALPHA);
},
onEnter: function () {
cc.Node.prototype.onEnter.call(this);
this.scheduleUpdate();
},
onExit: function () {
this.unscheduleUpdate();
cc.Node.prototype.onExit.call(this);
},
/**
* Sets whether open debug slots.
* @param {boolean} enable true to open, false to close.
*/
setDebugSolots:function(enable){
this._debugSlots = enable;
},
/**
* Sets whether open debug bones.
* @param {boolean} enable
*/
setDebugBones:function(enable){
this._debugBones = enable;
},
/**
* Sets whether open debug slots.
* @param {boolean} enabled true to open, false to close.
*/
setDebugSlotsEnabled: function(enabled) {
this._debugSlots = enabled;
},
/**
* Gets whether open debug slots.
* @returns {boolean} true to open, false to close.
*/
getDebugSlotsEnabled: function() {
return this._debugSlots;
},
/**
* Sets whether open debug bones.
* @param {boolean} enabled
*/
setDebugBonesEnabled: function(enabled) {
this._debugBones = enabled;
},
/**
* Gets whether open debug bones.
* @returns {boolean} true to open, false to close.
*/
getDebugBonesEnabled: function() {
return this._debugBones;
},
/**
* Sets the time scale of sp.Skeleton.
* @param {Number} scale
*/
setTimeScale:function(scale){
this._timeScale = scale;
},
getTimeScale: function(){
return this._timeScale;
},
/**
* Initializes sp.Skeleton with Data.
* @param {sp.spine.SkeletonData|String} skeletonDataFile
* @param {String|spine.Atlas|spine.SkeletonData} atlasFile atlas filename or atlas data or owns SkeletonData
* @param {Number} [scale] scale can be specified on the JSON or binary loader which will scale the bone positions, image sizes, and animation translations.
*/
initWithArgs: function (skeletonDataFile, atlasFile, scale) {
var argSkeletonFile = skeletonDataFile, argAtlasFile = atlasFile,
skeletonData, atlas, ownsSkeletonData;
if (cc.isString(argSkeletonFile)) {
if (cc.isString(argAtlasFile)) {
var data = cc.loader.getRes(argAtlasFile);
sp._atlasLoader.setAtlasFile(argAtlasFile);
atlas = new spine.TextureAtlas(data, sp._atlasLoader.load.bind(sp._atlasLoader));
} else {
atlas = atlasFile;
}
scale = scale || 1 / cc.director.getContentScaleFactor();
var attachmentLoader = new spine.AtlasAttachmentLoader(atlas);
var skeletonJsonReader = new spine.SkeletonJson(attachmentLoader);
skeletonJsonReader.scale = scale;
var skeletonJson = cc.loader.getRes(argSkeletonFile);
skeletonData = skeletonJsonReader.readSkeletonData(skeletonJson);
atlas.dispose(skeletonJsonReader);
ownsSkeletonData = true;
} else {
skeletonData = skeletonDataFile;
ownsSkeletonData = atlasFile;
}
this.setSkeletonData(skeletonData, ownsSkeletonData);
this.init();
},
/**
* Returns the bounding box of sp.Skeleton.
* @returns {cc.Rect}
*/
getBoundingBox: function () {
var minX = cc.FLT_MAX, minY = cc.FLT_MAX, maxX = cc.FLT_MIN, maxY = cc.FLT_MIN;
var scaleX = this.getScaleX(), scaleY = this.getScaleY(), vertices,
slots = this._skeleton.slots, VERTEX = spine.RegionAttachment;
for (var i = 0, slotCount = slots.length; i < slotCount; ++i) {
var slot = slots[i];
var attachment = slot.attachment;
if (!attachment || !(attachment instanceof spine.RegionAttachment))
continue;
vertices = spine.Utils.setArraySize(new Array(), 8, 0);
attachment.computeWorldVertices(slot.bone, vertices, 0, 2);
minX = Math.min(minX, vertices[VERTEX.OX1] * scaleX, vertices[VERTEX.OX4] * scaleX, vertices[VERTEX.OX2] * scaleX, vertices[VERTEX.OX3] * scaleX);
minY = Math.min(minY, vertices[VERTEX.OY1] * scaleY, vertices[VERTEX.OY4] * scaleY, vertices[VERTEX.OY2] * scaleY, vertices[VERTEX.OY3] * scaleY);
maxX = Math.max(maxX, vertices[VERTEX.OX1] * scaleX, vertices[VERTEX.OX4] * scaleX, vertices[VERTEX.OX2] * scaleX, vertices[VERTEX.OX3] * scaleX);
maxY = Math.max(maxY, vertices[VERTEX.OY1] * scaleY, vertices[VERTEX.OY4] * scaleY, vertices[VERTEX.OY2] * scaleY, vertices[VERTEX.OY3] * scaleY);
}
var position = this.getPosition();
return cc.rect(position.x + minX, position.y + minY, maxX - minX, maxY - minY);
},
/**
* Computes the world SRT from the local SRT for each bone.
*/
updateWorldTransform: function () {
this._skeleton.updateWorldTransform();
},
/**
* Sets the bones and slots to the setup pose.
*/
setToSetupPose: function () {
this._skeleton.setToSetupPose();
},
/**
* Sets the bones to the setup pose, using the values from the `BoneData` list in the `SkeletonData`.
*/
setBonesToSetupPose: function () {
this._skeleton.setBonesToSetupPose();
},
/**
* Sets the slots to the setup pose, using the values from the `SlotData` list in the `SkeletonData`.
*/
setSlotsToSetupPose: function () {
this._skeleton.setSlotsToSetupPose();
},
/**
* Finds a bone by name. This does a string comparison for every bone.
* @param {String} boneName
* @returns {sp.spine.Bone}
*/
findBone: function (boneName) {
return this._skeleton.findBone(boneName);
},
/**
* Finds a slot by name. This does a string comparison for every slot.
* @param {String} slotName
* @returns {sp.spine.Slot}
*/
findSlot: function (slotName) {
return this._skeleton.findSlot(slotName);
},
/**
* Finds a skin by name and makes it the active skin. This does a string comparison for every skin. Note that setting the skin does not change which attachments are visible.
* @param {string} skinName
* @returns {sp.spine.Skin}
*/
setSkin: function (skinName) {
return this._skeleton.setSkinByName(skinName);
},
/**
* Returns the attachment for the slot and attachment name. The skeleton looks first in its skin, then in the skeleton data’s default skin.
* @param {String} slotName
* @param {String} attachmentName
* @returns {sp.spine.Attachment}
*/
getAttachment: function (slotName, attachmentName) {
return this._skeleton.getAttachmentByName(slotName, attachmentName);
},
/**
* Sets the attachment for the slot and attachment name. The skeleton looks first in its skin, then in the skeleton data’s default skin.
* @param {String} slotName
* @param {String} attachmentName
*/
setAttachment: function (slotName, attachmentName) {
this._skeleton.setAttachment(slotName, attachmentName);
},
/**
* Sets the premultiplied alpha value to sp.Skeleton.
* @param {Number} alpha
*/
setPremultipliedAlpha: function (premultiplied) {
this._premultipliedAlpha = premultiplied;
},
/**
* Returns whether to enable premultiplied alpha.
* @returns {boolean}
*/
isPremultipliedAlpha: function () {
return this._premultipliedAlpha;
},
/**
* Sets skeleton data to sp.Skeleton.
* @param {sp.spine.SkeletonData} skeletonData
* @param {sp.spine.SkeletonData} ownsSkeletonData
*/
setSkeletonData: function (skeletonData, ownsSkeletonData) {
if(skeletonData.width != null && skeletonData.height != null)
this.setContentSize(skeletonData.width / cc.director.getContentScaleFactor(), skeletonData.height / cc.director.getContentScaleFactor());
this._skeleton = new spine.Skeleton(skeletonData);
this._skeleton.updateWorldTransform();
this._rootBone = this._skeleton.getRootBone();
this._ownsSkeletonData = ownsSkeletonData;
this._renderCmd._createChildFormSkeletonData();
},
/**
* Return the renderer of attachment.
* @param {sp.spine.RegionAttachment|sp.spine.BoundingBoxAttachment} regionAttachment
* @returns {sp.spine.TextureAtlasRegion}
*/
getTextureAtlas: function (regionAttachment) {
return regionAttachment.region;
},
/**
* Returns the blendFunc of sp.Skeleton.
* @returns {cc.BlendFunc}
*/
getBlendFunc: function () {
var slot = this._skeleton.drawOrder[0];
if (slot) {
var blend = this._renderCmd._getBlendFunc(slot.data.blendMode, this._premultipliedAlpha);
return blend;
}
else {
return {};
}
},
/**
* Sets the blendFunc of sp.Skeleton, it won't have any effect for skeleton, skeleton is using slot's data to determine the blend function.
* @param {cc.BlendFunc|Number} src
* @param {Number} [dst]
*/
setBlendFunc: function (src, dst) {
return;
},
/**
* Update will be called automatically every frame if "scheduleUpdate" is called when the node is "live".
* @param {Number} dt Delta time since last update
*/
update: function (dt) {
this._skeleton.update(dt);
}
});
cc.defineGetterSetter(sp.Skeleton.prototype, "opacityModifyRGB", sp.Skeleton.prototype.isOpacityModifyRGB);
// For renderer webgl to identify skeleton's default texture and blend function
cc.defineGetterSetter(sp.Skeleton.prototype, "_blendFunc", sp.Skeleton.prototype.getBlendFunc);
cc.defineGetterSetter(sp.Skeleton.prototype, '_texture', function () {
return this._renderCmd._currTexture;
});
/**
* Creates a skeleton object.
* @deprecated since v3.0, please use new sp.Skeleton(skeletonDataFile, atlasFile, scale) instead.
* @param {spine.SkeletonData|String} skeletonDataFile
* @param {String|spine.Atlas|spine.SkeletonData} atlasFile atlas filename or atlas data or owns SkeletonData
* @param {Number} [scale] scale can be specified on the JSON or binary loader which will scale the bone positions, image sizes, and animation translations.
* @returns {sp.Skeleton}
*/
sp.Skeleton.create = function (skeletonDataFile, atlasFile/* or atlas*/, scale) {
return new sp.Skeleton(skeletonDataFile, atlasFile, scale);
};