@eva/spine-base 1.2.8 → 2.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,550 +0,0 @@
1
- function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
-
3
- import { Component, OBSERVER_TYPE, resource, decorators } from '@eva/eva.js/dist/miniprogram';
4
- import { __extends, __values, __decorate, __awaiter, __generator } from 'tslib';
5
- import { RendererSystem, Renderer } from '@eva/plugin-renderer/dist/miniprogram';
6
- import { type } from '@eva/inspector-decorator';
7
- import { Texture } from '@eva/miniprogram-pixi';
8
-
9
- var Spine = function (_super) {
10
- __extends(Spine, _super);
11
-
12
- function Spine() {
13
- var _this = _super !== null && _super.apply(this, arguments) || this;
14
-
15
- _this.resource = '';
16
- _this.animationName = '';
17
- _this.autoPlay = true;
18
- _this.waitExecuteInfos = [];
19
- return _this;
20
- }
21
-
22
- Object.defineProperty(Spine.prototype, "armature", {
23
- get: function get() {
24
- return this._armature;
25
- },
26
- set: function set(val) {
27
- var e_1, _a;
28
-
29
- this._armature = val;
30
- if (!val) return;
31
-
32
- if (this.autoPlay) {
33
- this.play(this.animationName);
34
- }
35
-
36
- try {
37
- for (var _b = __values(this.waitExecuteInfos), _c = _b.next(); !_c.done; _c = _b.next()) {
38
- var info = _c.value;
39
-
40
- if (info.playType) {
41
- var name_1 = info.name,
42
- loop = info.loop,
43
- track = info.track;
44
- this.play(name_1, loop, track);
45
- } else {
46
- this.stop(info.track);
47
- }
48
- }
49
- } catch (e_1_1) {
50
- e_1 = {
51
- error: e_1_1
52
- };
53
- } finally {
54
- try {
55
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
56
- } finally {
57
- if (e_1) throw e_1.error;
58
- }
59
- }
60
-
61
- this.waitExecuteInfos = [];
62
- },
63
- enumerable: false,
64
- configurable: true
65
- });
66
-
67
- Spine.prototype.init = function (obj) {
68
- if (!obj) return;
69
-
70
- _extends(this, obj);
71
- };
72
-
73
- Spine.prototype.onDestroy = function () {
74
- this.destroied = true;
75
- };
76
-
77
- Spine.prototype.play = function (name, loop, track) {
78
- try {
79
- if (name) this.animationName = name;
80
-
81
- if (!this.armature) {
82
- this.waitExecuteInfos.push({
83
- playType: true,
84
- name: name,
85
- loop: loop !== null && loop !== void 0 ? loop : this.autoPlay,
86
- track: track
87
- });
88
- } else {
89
- if (track === undefined) {
90
- track = 0;
91
- }
92
-
93
- this.armature.state.setAnimation(track, this.animationName, loop);
94
- }
95
- } catch (e) {
96
- console.log(e);
97
- }
98
- };
99
-
100
- Spine.prototype.stop = function (track) {
101
- if (!this.armature) {
102
- this.waitExecuteInfos.push({
103
- playType: false,
104
- track: track
105
- });
106
- return;
107
- }
108
-
109
- if (track === undefined) {
110
- track = 0;
111
- }
112
-
113
- this.armature.state.setEmptyAnimation(track, 0);
114
- };
115
-
116
- Spine.prototype.addAnimation = function (name, delay, loop, track) {
117
- try {
118
- if (!this.armature) {} else {
119
- if (track === undefined) {
120
- track = 0;
121
- }
122
-
123
- this.armature.state.addAnimation(track, name, loop, delay);
124
- }
125
- } catch (e) {
126
- console.log(e);
127
- }
128
- };
129
-
130
- Spine.prototype.setMix = function (from, to, duration) {
131
- if (!this.armature) ;else {
132
- this.armature.stateData.setMix(from, to, duration);
133
- }
134
- };
135
-
136
- Spine.prototype.getAnim = function (track) {
137
- if (track === void 0) {
138
- track = 0;
139
- }
140
-
141
- try {
142
- if (!this.armature) {} else {
143
- return this.armature.state.tracks[track].animation.name;
144
- }
145
- } catch (e) {
146
- console.log(e);
147
- }
148
- };
149
-
150
- Spine.prototype.setDefaultMix = function (duration) {
151
- if (!this.armature) ;else {
152
- this.armature.stateData.defaultMix = duration;
153
- }
154
- };
155
-
156
- Spine.prototype.setAttachment = function (slotName, attachmentName) {
157
- if (!this.armature) {
158
- return;
159
- }
160
-
161
- this.armature.skeleton.setAttachment(slotName, attachmentName);
162
- };
163
-
164
- Spine.prototype.getBone = function (boneName) {
165
- if (!this.armature) {
166
- return;
167
- }
168
-
169
- return this.armature.skeleton.findBone(boneName);
170
- };
171
-
172
- Spine.componentName = 'Spine';
173
-
174
- __decorate([type('string')], Spine.prototype, "resource", void 0);
175
-
176
- __decorate([type('string')], Spine.prototype, "animationName", void 0);
177
-
178
- __decorate([type('boolean')], Spine.prototype, "autoPlay", void 0);
179
-
180
- return Spine;
181
- }(Component);
182
-
183
- var Spine$1 = Spine;
184
- var texCache = {};
185
-
186
- function cacheImage(data) {
187
- var oldImg = data.image;
188
- return {
189
- tex: Texture.from(oldImg),
190
- count: 0
191
- };
192
- }
193
-
194
- function retainTexture(name, data) {
195
- var cache = texCache[name];
196
-
197
- if (!cache) {
198
- cache = cacheImage(data);
199
- texCache[name] = cache;
200
- }
201
-
202
- cache.count++;
203
- return cache.tex;
204
- }
205
-
206
- function getTexture(imageSrc, data) {
207
- var cache = texCache[imageSrc];
208
-
209
- if (!cache) {
210
- cache = cacheImage(data);
211
- texCache[imageSrc] = cache;
212
- }
213
-
214
- return cache.tex;
215
- }
216
-
217
- function releaseTexture(imageSrc) {
218
- if (!imageSrc) return;
219
- setTimeout(function () {
220
- var cache = texCache[imageSrc];
221
-
222
- if (cache) {
223
- cache.count--;
224
-
225
- if (cache.count <= 0) {
226
- if (cache.tex) {
227
- cache.tex.destroy(true);
228
- cache.tex = null;
229
- }
230
-
231
- delete texCache[imageSrc];
232
- }
233
- }
234
- }, 100);
235
- }
236
-
237
- var dataMap = {};
238
-
239
- function createSpineData(name, data, scale, pixiSpine) {
240
- var spineData = null;
241
- var img = getTexture(data.image.src, data);
242
- new pixiSpine.core.TextureAtlas(data.atlas, function (line, callback) {
243
- callback(img.baseTexture);
244
- }, function (spineAtlas) {
245
- if (spineAtlas) {
246
- var attachmentLoader = new pixiSpine.core.AtlasAttachmentLoader(spineAtlas);
247
- var spineJsonParser = new pixiSpine.core.SkeletonJson(attachmentLoader);
248
-
249
- if (scale) {
250
- spineJsonParser.scale = scale;
251
- }
252
-
253
- spineData = spineJsonParser.readSkeletonData(data.ske);
254
- }
255
- });
256
- var obj = {
257
- spineData: spineData,
258
- ref: 0,
259
- imageSrc: data.image.src
260
- };
261
- dataMap[name] = obj;
262
- return obj;
263
- }
264
-
265
- function getSpineData(res, pixiSpine) {
266
- return __awaiter(this, void 0, void 0, function () {
267
- var data;
268
- return __generator(this, function (_a) {
269
- data = dataMap[res.name];
270
-
271
- if (!data) {
272
- if (res.complete) {
273
- data = createSpineData(res.name, res.data, res.scale, pixiSpine);
274
- } else if (!data) {
275
- return [2];
276
- }
277
- }
278
-
279
- retainTexture(res.data.image.src, res.data);
280
- data.ref++;
281
- return [2, data.spineData];
282
- });
283
- });
284
- }
285
-
286
- function releaseSpineData(resourceName, imageSrc) {
287
- var data = dataMap[resourceName];
288
-
289
- if (!data) {
290
- return;
291
- }
292
-
293
- data.ref--;
294
-
295
- if (data.ref <= 0) {
296
- releaseTexture(imageSrc);
297
- delete dataMap[resourceName];
298
- }
299
- }
300
-
301
- var MaxRetryCount = 20;
302
-
303
- var SpineSystem = function (_super) {
304
- __extends(SpineSystem, _super);
305
-
306
- function SpineSystem() {
307
- var _this = _super !== null && _super.apply(this, arguments) || this;
308
-
309
- _this.armatures = {};
310
- return _this;
311
- }
312
-
313
- SpineSystem.prototype.init = function (_a) {
314
- var _this = this;
315
-
316
- var pixiSpine = _a.pixiSpine;
317
- this.renderSystem = this.game.getSystem(RendererSystem);
318
- this.renderSystem.rendererManager.register(this);
319
- this.pixiSpine = pixiSpine;
320
- this.game.canvas.addEventListener('webglcontextrestored', function () {
321
- var objs = _this.game.gameObjects;
322
- var toAdd = [];
323
-
324
- for (var k in _this.armatures) {
325
- var id = +k;
326
-
327
- for (var i = 0; i < objs.length; ++i) {
328
- var obj = objs[i];
329
-
330
- if (obj.id === id) {
331
- var sp = obj.getComponent(Spine$1);
332
-
333
- if (sp) {
334
- _this.remove({
335
- type: OBSERVER_TYPE.REMOVE,
336
- gameObject: obj,
337
- component: sp,
338
- componentName: Spine$1.componentName
339
- });
340
-
341
- toAdd.push({
342
- type: OBSERVER_TYPE.ADD,
343
- gameObject: obj,
344
- component: sp,
345
- componentName: Spine$1.componentName
346
- });
347
- }
348
-
349
- break;
350
- }
351
- }
352
- }
353
-
354
- setTimeout(function () {
355
- toAdd.forEach(function (obj) {
356
- _this.add(obj);
357
- });
358
- }, 1000);
359
- }, false);
360
- };
361
-
362
- SpineSystem.prototype.update = function (e) {
363
- for (var key in this.armatures) {
364
- this.armatures[key].update(e.deltaTime * 0.001);
365
- this.armatures[key].updateTransform();
366
- }
367
-
368
- _super.prototype.update.call(this);
369
- };
370
-
371
- SpineSystem.prototype.componentChanged = function (changed) {
372
- return __awaiter(this, void 0, void 0, function () {
373
- return __generator(this, function (_a) {
374
- if (changed.componentName === 'Spine') {
375
- if (changed.type === OBSERVER_TYPE.ADD) {
376
- this.add(changed);
377
- } else if (changed.type === OBSERVER_TYPE.CHANGE) {
378
- switch (changed.prop.prop[0]) {
379
- case 'resource':
380
- this.change(changed);
381
- break;
382
- }
383
- } else if (changed.type === OBSERVER_TYPE.REMOVE) {
384
- this.remove(changed);
385
- }
386
- }
387
-
388
- return [2];
389
- });
390
- });
391
- };
392
-
393
- SpineSystem.prototype.add = function (changed, count) {
394
- var _a, _b;
395
-
396
- return __awaiter(this, void 0, void 0, function () {
397
- var component, gameObjectId, asyncId, res, spineData, container, armature, tran;
398
-
399
- var _this = this;
400
-
401
- return __generator(this, function (_c) {
402
- switch (_c.label) {
403
- case 0:
404
- component = changed.component;
405
- clearTimeout(component.addHandler);
406
- gameObjectId = changed.gameObject.id;
407
- asyncId = this.increaseAsyncId(gameObjectId);
408
- return [4, resource.getResource(component.resource)];
409
-
410
- case 1:
411
- res = _c.sent();
412
- if (!this.validateAsyncId(gameObjectId, asyncId)) return [2];
413
- return [4, getSpineData(res, this.pixiSpine)];
414
-
415
- case 2:
416
- spineData = _c.sent();
417
- if (!this.validateAsyncId(gameObjectId, asyncId)) return [2];
418
-
419
- if (!spineData) {
420
- component.addHandler = setTimeout(function () {
421
- if (!component.destroied) {
422
- if (count === undefined) {
423
- count = MaxRetryCount;
424
- }
425
-
426
- count--;
427
-
428
- if (count > 0) {
429
- _this.add(changed, count);
430
- } else {
431
- console.log('retry exceed max times', component.resource);
432
- }
433
- }
434
- }, 1000);
435
- return [2];
436
- }
437
-
438
- this.remove(changed);
439
- container = (_b = (_a = this.renderSystem) === null || _a === void 0 ? void 0 : _a.containerManager) === null || _b === void 0 ? void 0 : _b.getContainer(changed.gameObject.id);
440
-
441
- if (!container) {
442
- return [2];
443
- }
444
-
445
- component.lastResource = component.resource;
446
- armature = new this.pixiSpine.Spine(spineData);
447
- this.armatures[changed.gameObject.id] = armature;
448
-
449
- if (changed.gameObject && changed.gameObject.transform) {
450
- tran = changed.gameObject.transform;
451
- armature.x = tran.size.width * tran.origin.x;
452
- armature.y = tran.size.height * tran.origin.y;
453
- }
454
-
455
- container.addChildAt(armature, 0);
456
- armature.update();
457
- armature.updateTransform();
458
- component.armature = armature;
459
- component.emit('loaded', {
460
- resource: component.resource
461
- });
462
- armature.state.addListener({
463
- start: function start(track, event) {
464
- component.emit('start', {
465
- track: track,
466
- name: track.animation.name
467
- });
468
- },
469
- complete: function complete(track, event) {
470
- component.emit('complete', {
471
- track: track,
472
- name: track.animation.name
473
- });
474
- },
475
- interrupt: function interrupt(track, event) {
476
- component.emit('interrupt', {
477
- track: track,
478
- name: track.animation.name
479
- });
480
- },
481
- end: function end(track, event) {
482
- component.emit('end', {
483
- track: track,
484
- name: track.animation.name
485
- });
486
- },
487
- event: function event(track, _event) {
488
- component.emit('event', track, _event);
489
- }
490
- });
491
- return [2];
492
- }
493
- });
494
- });
495
- };
496
-
497
- SpineSystem.prototype.change = function (changed) {
498
- this.remove(changed);
499
- this.add(changed);
500
- };
501
-
502
- SpineSystem.prototype.remove = function (changed) {
503
- var _a, _b, _c, _d;
504
-
505
- return __awaiter(this, void 0, void 0, function () {
506
- var component, armature, container, res;
507
- return __generator(this, function (_e) {
508
- switch (_e.label) {
509
- case 0:
510
- this.increaseAsyncId(changed.gameObject.id);
511
- component = changed.component;
512
- clearTimeout(component.addHandler);
513
- armature = this.armatures[changed.gameObject.id];
514
- container = (_b = (_a = this.renderSystem) === null || _a === void 0 ? void 0 : _a.containerManager) === null || _b === void 0 ? void 0 : _b.getContainer(changed.gameObject.id);
515
-
516
- if (container && armature) {
517
- container.removeChild(armature);
518
- }
519
-
520
- if (!component.armature) return [3, 2];
521
- component.armature.destroy({
522
- children: true
523
- });
524
- return [4, resource.getResource(component.lastResource)];
525
-
526
- case 1:
527
- res = _e.sent();
528
- releaseSpineData(res.name, (_d = (_c = res.data) === null || _c === void 0 ? void 0 : _c.image) === null || _d === void 0 ? void 0 : _d.src);
529
- _e.label = 2;
530
-
531
- case 2:
532
- component.armature = null;
533
- delete this.armatures[changed.gameObject.id];
534
- if (changed.type === OBSERVER_TYPE.CHANGE) ;
535
- return [2];
536
- }
537
- });
538
- });
539
- };
540
-
541
- SpineSystem.systemName = 'SpineSystem';
542
- SpineSystem = __decorate([decorators.componentObserver({
543
- Spine: ['resource']
544
- })], SpineSystem);
545
- return SpineSystem;
546
- }(Renderer);
547
-
548
- var SpineSystem$1 = SpineSystem;
549
- resource.registerResourceType('SPINE');
550
- export { Spine$1 as Spine, SpineSystem$1 as SpineSystem };