/
ArmatureDisplay.js
541 lines (485 loc) · 17.1 KB
/
ArmatureDisplay.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/****************************************************************************
Copyright (c) 2016 Chukong Technologies Inc.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Chukong Aipu reserves all rights not expressly granted to you.
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.
****************************************************************************/
var DefaultArmaturesEnum = cc.Enum({ 'default': -1 });
var DefaultAnimsEnum = cc.Enum({ '<None>': 0 });
function setEnumAttr (obj, propName, enumDef) {
cc.Class.attr(obj, propName, {
type: 'Enum',
enumList: cc.Enum.getList(enumDef)
});
}
/**
* !#en
* The Armature Display of DragonBones <br/>
* <br/>
* (Armature Display has a reference to a DragonBonesAsset and stores the state for ArmatureDisplay instance,
* which consists of the current pose's bone SRT, slot colors, and which slot attachments are visible. <br/>
* Multiple Armature Display can use the same DragonBonesAsset which includes all animations, skins, and attachments.) <br/>
* !#zh
* DragonBones 骨骼动画 <br/>
* <br/>
* (Armature Display 具有对骨骼数据的引用并且存储了骨骼实例的状态,
* 它由当前的骨骼动作,slot 颜色,和可见的 slot attachments 组成。<br/>
* 多个 Armature Display 可以使用相同的骨骼数据,其中包括所有的动画,皮肤和 attachments。)<br/>
*
* @class ArmatureDisplay
* @extends _RendererUnderSG
*/
dragonBones.ArmatureDisplay = cc.Class({
name: 'dragonBones.ArmatureDisplay',
extends: cc._RendererUnderSG,
editor: CC_EDITOR && {
menu: 'i18n:MAIN_MENU.component.renderers/DragonBones',
//help: 'app://docs/html/components/spine.html', // TODO help document of dragonBones
},
properties: {
_factory: {
default: null,
type: dragonBones.CCFactory,
serializable: false,
},
_dragonBonesData: {
default: null,
type: dragonBones.DragonBonesData,
serializable: false,
},
/**
* !#en
* The DragonBones data contains the armatures information (bind pose bones, slots, draw order,
* attachments, skins, etc) and animations but does not hold any state.<br/>
* Multiple ArmatureDisplay can share the same DragonBones data.
* !#zh
* 骨骼数据包含了骨骼信息(绑定骨骼动作,slots,渲染顺序,
* attachments,皮肤等等)和动画但不持有任何状态。<br/>
* 多个 ArmatureDisplay 可以共用相同的骨骼数据。
* @property {DragonBonesAsset} dragonAsset
*/
dragonAsset : {
default: null,
type : dragonBones.DragonBonesAsset,
notify: function () {
// parse the asset data
this._parseDragonAsset();
this._refresh();
if (CC_EDITOR) {
this._defaultArmatureIndex = 0;
this._animationIndex = 0;
}
},
tooltip: 'i18n:COMPONENT.dragon_bones.dragon_bones_asset'
},
/**
* !#en
* The atlas asset for the DragonBones.
* !#zh
* 骨骼数据所需的 Atlas Texture 数据。
* @property {DragonBonesAtlasAsset} dragonAtlasAsset
*/
dragonAtlasAsset : {
default: null,
type: dragonBones.DragonBonesAtlasAsset,
notify: function () {
// parse the atlas asset data
this._parseDragonAtlasAsset();
this._refreshSgNode();
},
tooltip: 'i18n:COMPONENT.dragon_bones.dragon_bones_atlas_asset'
},
_armatureName : '',
/**
* !#en The name of current armature.
* !#zh 当前的 Armature 名称。
* @property {String} armatureName
*/
armatureName: {
get : function () {
return this._armatureName;
},
set : function (value) {
this._armatureName = value;
var animNames = this.getAnimationNames(this._armatureName);
if (!this.animationName || animNames.indexOf(this.animationName) < 0) {
if (CC_EDITOR) {
this.animationName = animNames[0];
} else {
// Not use default animation name at runtime
this.animationName = '';
}
}
this._refresh();
},
visible: false
},
_animationName : '',
/**
* !#en The name of current playing animation.
* !#zh 当前播放的动画名称。
* @property {String} animationName
*/
animationName: {
get : function () {
return this._animationName;
},
set : function (value) {
this._animationName = value;
},
visible: false
},
/**
* @property {Number} _defaultArmatureIndex
*/
_defaultArmatureIndex: {
default: 0,
notify: function () {
var armatureName = '';
if (this.dragonAsset) {
var armaturesEnum;
if (this.dragonAsset) {
armaturesEnum = this.dragonAsset.getArmatureEnum();
}
if ( !armaturesEnum ) {
return cc.errorID(7400, this.name);
}
armatureName = armaturesEnum[this._defaultArmatureIndex];
}
if (armatureName !== undefined) {
this.armatureName = armatureName;
}
else {
cc.errorID(7401, this.name);
}
},
type: DefaultArmaturesEnum,
visible: true,
editorOnly: true,
displayName: "Armature",
tooltip: 'i18n:COMPONENT.dragon_bones.armature_name'
},
// value of 0 represents no animation
_animationIndex: {
default: 0,
notify: function () {
if (this._animationIndex === 0) {
this.animationName = '';
return;
}
var animsEnum;
if (this.dragonAsset) {
animsEnum = this.dragonAsset.getAnimsEnum(this.armatureName);
}
if ( !animsEnum ) {
return;
}
var animName = animsEnum[this._animationIndex];
if (animName !== undefined) {
this.animationName = animName;
}
else {
cc.errorID(7402, this.name);
}
},
type: DefaultAnimsEnum,
visible: true,
editorOnly: true,
displayName: 'Animation',
tooltip: 'i18n:COMPONENT.dragon_bones.animation_name'
},
/**
* !#en The time scale of this armature.
* !#zh 当前骨骼中所有动画的时间缩放率。
* @property {Number} timeScale
* @default 1
*/
timeScale: {
default: 1,
notify: function () {
if (this._sgNode) {
this._sgNode.animation().timeScale = this.timeScale;
}
},
tooltip: 'i18n:COMPONENT.dragon_bones.time_scale'
},
/**
* !#en The play times of the default animation.
* -1 means using the value of config file;
* 0 means repeat for ever
* >0 means repeat times
* !#zh 播放默认动画的循环次数
* -1 表示使用配置文件中的默认值;
* 0 表示无限循环
* >0 表示循环次数
* @property {Number} playTimes
* @default -1
*/
playTimes: {
default: -1,
tooltip: 'i18n:COMPONENT.dragon_bones.play_times'
},
/**
* !#en Indicates whether open debug bones.
* !#zh 是否显示 bone 的 debug 信息。
* @property {Boolean} debugBones
* @default false
*/
debugBones: {
default: false,
notify: function () {
if (this._sgNode) {
this._sgNode.setDebugBones(this.debugBones);
}
},
editorOnly: true,
tooltip: 'i18n:COMPONENT.dragon_bones.debug_bones'
},
},
// IMPLEMENT
ctor : function () {
this._factory = dragonBones.CCFactory.getFactory();
},
__preload : function () {
this._parseDragonAsset();
this._parseDragonAtlasAsset();
this._refresh();
},
_createSgNode: function () {
if (this.dragonAsset && this.dragonAtlasAsset && this.armatureName) {
return this._factory.buildArmatureDisplay(this.armatureName, this._dragonBonesData.name);
}
return null;
},
_initSgNode: function () {
// set the time scale
var sgNode = this._sgNode;
sgNode.animation().timeScale = this.timeScale;
if (this.animationName) {
this.playAnimation(this.animationName, this.playTimes);
}
if (CC_EDITOR) {
sgNode.setDebugBones(this.debugBones);
}
},
_parseDragonAsset : function() {
if (this.dragonAsset) {
var jsonObj = JSON.parse(this.dragonAsset.dragonBonesJson);
var data = this._factory.getDragonBonesData(jsonObj.name);
if (data) {
// already added asset
this._dragonBonesData = data;
return;
}
if (CC_JSB) {
this._dragonBonesData = this._factory.parseDragonBonesData(this.dragonAsset.dragonBonesJson);
} else {
this._dragonBonesData = this._factory.parseDragonBonesData(jsonObj);
}
}
},
_parseDragonAtlasAsset : function() {
if (this.dragonAtlasAsset) {
if (CC_JSB) {
// TODO parse the texture atlas data from json string & texture path
this._factory.parseTextureAtlasData(this.dragonAtlasAsset.atlasJson, this.dragonAtlasAsset.texture);
} else {
var atlasJsonObj = JSON.parse(this.dragonAtlasAsset.atlasJson);
var texture = cc.textureCache.getTextureForKey(this.dragonAtlasAsset.texture);
this._factory.parseTextureAtlasData(atlasJsonObj, texture);
}
}
},
_refreshSgNode : function() {
var self = this;
// discard exists sgNode
if (self._sgNode) {
if ( self.node._sizeProvider === self._sgNode ) {
self.node._sizeProvider = null;
}
self._removeSgNode();
self._sgNode = null;
}
// recreate sgNode...
var sgNode = self._sgNode = self._createSgNode();
if (sgNode) {
if (CC_JSB) {
sgNode.retain();
}
if ( !self.enabledInHierarchy ) {
sgNode.setVisible(false);
}
self._initSgNode();
self._appendSgNode(sgNode);
self._registSizeProvider();
}
},
_refresh : function () {
this._refreshSgNode();
if (CC_EDITOR) {
// update inspector
this._updateArmatureEnum();
this._updateAnimEnum();
Editor.Utils.refreshSelectedInspector('node', this.node.uuid);
}
},
// update animation list for editor
_updateAnimEnum: CC_EDITOR && function () {
var animEnum;
if (this.dragonAsset) {
animEnum = this.dragonAsset.getAnimsEnum(this.armatureName);
}
// change enum
setEnumAttr(this, '_animationIndex', animEnum || DefaultAnimsEnum);
},
// update armature list for editor
_updateArmatureEnum: CC_EDITOR && function () {
var armatureEnum;
if (this.dragonAsset) {
armatureEnum = this.dragonAsset.getArmatureEnum();
}
// change enum
setEnumAttr(this, '_defaultArmatureIndex', armatureEnum || DefaultArmaturesEnum);
},
/**
* !#en
* Play the specified animation.
* Parameter animName specify the animation name.
* Parameter playTimes specify the repeat times of the animation.
* -1 means use the value of the config file.
* 0 means play the animation for ever.
* >0 means repeat times.
* !#zh
* 播放指定的动画.
* animName 指定播放动画的名称。
* playTimes 指定播放动画的次数。
* -1 为使用配置文件中的次数。
* 0 为无限循环播放。
* >0 为动画的重复次数。
* @method playAnimation
* @param {String} animName
* @param {Number} playTimes
* @return {dragonBones.AnimationState}
*/
playAnimation: function(animName, playTimes) {
if (this._sgNode) {
this.animationName = animName;
return this._sgNode.animation().play(animName, playTimes);
}
return null;
},
/**
* !#en
* Get the all armature names in the DragonBones Data.
* !#zh
* 获取 DragonBones 数据中所有的 armature 名称
* @method getArmatureNames
* @returns {Array}
*/
getArmatureNames : function () {
if (this._dragonBonesData) {
return this._dragonBonesData.armatureNames;
}
return [];
},
/**
* !#en
* Get the all animation names of specified armature.
* !#zh
* 获取指定的 armature 的所有动画名称。
* @method getAnimationNames
* @param {String} armatureName
* @returns {Array}
*/
getAnimationNames : function (armatureName) {
var ret = [];
if (this._dragonBonesData) {
var armatureData = this._dragonBonesData.getArmature(armatureName);
if (armatureData) {
for (var animName in armatureData.animations) {
if (armatureData.animations.hasOwnProperty(animName)) {
ret.push(animName);
}
}
}
}
return ret;
},
/**
* !#en
* Add event listener for the DragonBones Event.
* !#zh
* 添加 DragonBones 事件监听器。
* @method addEventListener
* @param {dragonBones.EventObject} eventType
* @param {function} listener
* @param {Object} target
*/
addEventListener : function (eventType, listener, target) {
if (this._sgNode) {
this._sgNode.addEvent(eventType, listener, target);
}
},
/**
* !#en
* Remove the event listener for the DragonBones Event.
* !#zh
* 移除 DragonBones 事件监听器。
* @method removeEventListener
* @param {dragonBones.EventObject} eventType
* @param {function} listener
* @param {Object} target
*/
removeEventListener : function (eventType, listener, target) {
if (this._sgNode) {
this._sgNode.removeEvent(eventType, listener, target);
}
},
/**
* !#en
* Build the armature for specified name.
* !#zh
* 构建指定名称的 armature 对象
* @method buildArmature
* @param {String} armatureName
* @return {dragonBones.Armature}
*/
buildArmature : function (armatureName) {
if (this._factory) {
return this._factory.buildArmature(armatureName);
}
return null;
},
/**
* !#en
* Get the current armature object of the ArmatureDisplay.
* !#zh
* 获取 ArmatureDisplay 当前使用的 Armature 对象
* @method armature
* @returns {Object}
*/
armature : function () {
if (this._sgNode) {
return this._sgNode.armature();
}
return null;
}
});
/**
* module that contains all Dragon Bones runtime API
* @module dragonBones
*/