@eva/spine-base 1.2.2-mini.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.
package/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # @eva/spine-base
2
+
3
+ More Introduction
4
+
5
+ - [EN](https://eva.js.org)
6
+ - [中文](https://eva-engine.gitee.io)
@@ -0,0 +1,705 @@
1
+ function _extends() { _extends = Object.assign || 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
+ window.EVA = window.EVA || {};
4
+ window.EVA.plugin = window.EVA.plugin || {};
5
+
6
+ var _EVA_IIFE_spineBase = function (exports, eva_js, pluginRenderer, pixi_js) {
7
+ 'use strict';
8
+
9
+ var _extendStatics = function extendStatics(d, b) {
10
+ _extendStatics = Object.setPrototypeOf || {
11
+ __proto__: []
12
+ } instanceof Array && function (d, b) {
13
+ d.__proto__ = b;
14
+ } || function (d, b) {
15
+ for (var p in b) {
16
+ if (b.hasOwnProperty(p)) d[p] = b[p];
17
+ }
18
+ };
19
+
20
+ return _extendStatics(d, b);
21
+ };
22
+
23
+ function __extends(d, b) {
24
+ _extendStatics(d, b);
25
+
26
+ function __() {
27
+ this.constructor = d;
28
+ }
29
+
30
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
31
+ }
32
+
33
+ function __decorate(decorators, target, key, desc) {
34
+ var c = arguments.length,
35
+ r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc,
36
+ d;
37
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);else for (var i = decorators.length - 1; i >= 0; i--) {
38
+ if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
39
+ }
40
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
41
+ }
42
+
43
+ function __awaiter(thisArg, _arguments, P, generator) {
44
+ function adopt(value) {
45
+ return value instanceof P ? value : new P(function (resolve) {
46
+ resolve(value);
47
+ });
48
+ }
49
+
50
+ return new (P || (P = Promise))(function (resolve, reject) {
51
+ function fulfilled(value) {
52
+ try {
53
+ step(generator.next(value));
54
+ } catch (e) {
55
+ reject(e);
56
+ }
57
+ }
58
+
59
+ function rejected(value) {
60
+ try {
61
+ step(generator["throw"](value));
62
+ } catch (e) {
63
+ reject(e);
64
+ }
65
+ }
66
+
67
+ function step(result) {
68
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
69
+ }
70
+
71
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
72
+ });
73
+ }
74
+
75
+ function __generator(thisArg, body) {
76
+ var _ = {
77
+ label: 0,
78
+ sent: function sent() {
79
+ if (t[0] & 1) throw t[1];
80
+ return t[1];
81
+ },
82
+ trys: [],
83
+ ops: []
84
+ },
85
+ f,
86
+ y,
87
+ t,
88
+ g;
89
+ return g = {
90
+ next: verb(0),
91
+ "throw": verb(1),
92
+ "return": verb(2)
93
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
94
+ return this;
95
+ }), g;
96
+
97
+ function verb(n) {
98
+ return function (v) {
99
+ return step([n, v]);
100
+ };
101
+ }
102
+
103
+ function step(op) {
104
+ if (f) throw new TypeError("Generator is already executing.");
105
+
106
+ while (_) {
107
+ try {
108
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
109
+ if (y = 0, t) op = [op[0] & 2, t.value];
110
+
111
+ switch (op[0]) {
112
+ case 0:
113
+ case 1:
114
+ t = op;
115
+ break;
116
+
117
+ case 4:
118
+ _.label++;
119
+ return {
120
+ value: op[1],
121
+ done: false
122
+ };
123
+
124
+ case 5:
125
+ _.label++;
126
+ y = op[1];
127
+ op = [0];
128
+ continue;
129
+
130
+ case 7:
131
+ op = _.ops.pop();
132
+
133
+ _.trys.pop();
134
+
135
+ continue;
136
+
137
+ default:
138
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
139
+ _ = 0;
140
+ continue;
141
+ }
142
+
143
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
144
+ _.label = op[1];
145
+ break;
146
+ }
147
+
148
+ if (op[0] === 6 && _.label < t[1]) {
149
+ _.label = t[1];
150
+ t = op;
151
+ break;
152
+ }
153
+
154
+ if (t && _.label < t[2]) {
155
+ _.label = t[2];
156
+
157
+ _.ops.push(op);
158
+
159
+ break;
160
+ }
161
+
162
+ if (t[2]) _.ops.pop();
163
+
164
+ _.trys.pop();
165
+
166
+ continue;
167
+ }
168
+
169
+ op = body.call(thisArg, _);
170
+ } catch (e) {
171
+ op = [6, e];
172
+ y = 0;
173
+ } finally {
174
+ f = t = 0;
175
+ }
176
+ }
177
+
178
+ if (op[0] & 5) throw op[1];
179
+ return {
180
+ value: op[0] ? op[1] : void 0,
181
+ done: true
182
+ };
183
+ }
184
+ }
185
+
186
+ function getIDEPropsPropertyObj(target, propertyKey) {
187
+ if (!target.constructor.IDEProps) {
188
+ target.constructor.IDEProps = {};
189
+ }
190
+
191
+ if (!target.constructor.IDEProps[propertyKey]) {
192
+ target.constructor.IDEProps[propertyKey] = {};
193
+ }
194
+
195
+ var propertyObj = target.constructor.IDEProps[propertyKey];
196
+ return propertyObj;
197
+ }
198
+
199
+ function type(type) {
200
+ return function (target, propertyKey) {
201
+ var prop = getIDEPropsPropertyObj(target, propertyKey);
202
+ prop.key = propertyKey;
203
+ prop.type = type;
204
+ };
205
+ }
206
+
207
+ var Spine = function (_super) {
208
+ __extends(Spine, _super);
209
+
210
+ function Spine() {
211
+ var _this = _super !== null && _super.apply(this, arguments) || this;
212
+
213
+ _this.resource = '';
214
+ _this.animationName = '';
215
+ _this.autoPlay = true;
216
+ return _this;
217
+ }
218
+
219
+ Spine.prototype.init = function (obj) {
220
+ if (!obj) return;
221
+
222
+ _extends(this, obj);
223
+
224
+ if (this.autoPlay) {
225
+ this.play(this.animationName);
226
+ }
227
+ };
228
+
229
+ Spine.prototype.onDestroy = function () {
230
+ this.destroied = true;
231
+ };
232
+
233
+ Spine.prototype.play = function (name, loop, track) {
234
+ try {
235
+ if (name) this.animationName = name;
236
+
237
+ if (!this.armature) {} else {
238
+ if (track === undefined) {
239
+ track = 0;
240
+ }
241
+
242
+ this.armature.state.setAnimation(track, this.animationName, loop);
243
+ }
244
+ } catch (e) {
245
+ console.log(e);
246
+ }
247
+ };
248
+
249
+ Spine.prototype.stop = function (track) {
250
+ if (!this.armature) {
251
+ return;
252
+ }
253
+
254
+ if (track === undefined) {
255
+ track = 0;
256
+ }
257
+
258
+ this.armature.state.setEmptyAnimation(track, 0);
259
+ };
260
+
261
+ Spine.prototype.addAnimation = function (name, delay, loop, track) {
262
+ try {
263
+ if (!this.armature) {} else {
264
+ if (track === undefined) {
265
+ track = 0;
266
+ }
267
+
268
+ this.armature.state.addAnimation(track, name, loop, delay);
269
+ }
270
+ } catch (e) {
271
+ console.log(e);
272
+ }
273
+ };
274
+
275
+ Spine.prototype.setMix = function (from, to, duration) {
276
+ if (!this.armature) ;else {
277
+ this.armature.stateData.setMix(from, to, duration);
278
+ }
279
+ };
280
+
281
+ Spine.prototype.getAnim = function (track) {
282
+ if (track === void 0) {
283
+ track = 0;
284
+ }
285
+
286
+ try {
287
+ if (!this.armature) {} else {
288
+ return this.armature.state.tracks[track].animation.name;
289
+ }
290
+ } catch (e) {
291
+ console.log(e);
292
+ }
293
+ };
294
+
295
+ Spine.prototype.setDefaultMix = function (duration) {
296
+ if (!this.armature) ;else {
297
+ this.armature.stateData.defaultMix = duration;
298
+ }
299
+ };
300
+
301
+ Spine.prototype.setAttachment = function (slotName, attachmentName) {
302
+ if (!this.armature) {
303
+ return;
304
+ }
305
+
306
+ this.armature.skeleton.setAttachment(slotName, attachmentName);
307
+ };
308
+
309
+ Spine.prototype.getBone = function (boneName) {
310
+ if (!this.armature) {
311
+ return;
312
+ }
313
+
314
+ return this.armature.skeleton.findBone(boneName);
315
+ };
316
+
317
+ Spine.componentName = 'Spine';
318
+
319
+ __decorate([type('string')], Spine.prototype, "resource", void 0);
320
+
321
+ __decorate([type('string')], Spine.prototype, "animationName", void 0);
322
+
323
+ __decorate([type('boolean')], Spine.prototype, "autoPlay", void 0);
324
+
325
+ return Spine;
326
+ }(eva_js.Component);
327
+
328
+ var Spine$1 = Spine;
329
+ var texCache = {};
330
+
331
+ function cacheImage(data) {
332
+ var oldImg = data.image;
333
+ return {
334
+ tex: pixi_js.Texture.from(oldImg),
335
+ count: 0
336
+ };
337
+ }
338
+
339
+ function retainTexture(name, data) {
340
+ var cache = texCache[name];
341
+
342
+ if (!cache) {
343
+ cache = cacheImage(data);
344
+ texCache[name] = cache;
345
+ }
346
+
347
+ cache.count++;
348
+ return cache.tex;
349
+ }
350
+
351
+ function getTexture(imageSrc, data) {
352
+ var cache = texCache[imageSrc];
353
+
354
+ if (!cache) {
355
+ cache = cacheImage(data);
356
+ texCache[imageSrc] = cache;
357
+ }
358
+
359
+ return cache.tex;
360
+ }
361
+
362
+ function releaseTexture(imageSrc) {
363
+ if (!imageSrc) return;
364
+ setTimeout(function () {
365
+ var cache = texCache[imageSrc];
366
+
367
+ if (cache) {
368
+ cache.count--;
369
+
370
+ if (cache.count <= 0) {
371
+ if (cache.tex) {
372
+ cache.tex.destroy(true);
373
+ cache.tex = null;
374
+ }
375
+
376
+ delete texCache[imageSrc];
377
+ }
378
+ }
379
+ }, 100);
380
+ }
381
+
382
+ var dataMap = {};
383
+
384
+ function createSpineData(name, data, scale, pixiSpine) {
385
+ var spineData = null;
386
+ var img = getTexture(data.image.src, data);
387
+ new pixiSpine.core.TextureAtlas(data.atlas, function (line, callback) {
388
+ callback(img.baseTexture);
389
+ }, function (spineAtlas) {
390
+ if (spineAtlas) {
391
+ var attachmentLoader = new pixiSpine.core.AtlasAttachmentLoader(spineAtlas);
392
+ var spineJsonParser = new pixiSpine.core.SkeletonJson(attachmentLoader);
393
+
394
+ if (scale) {
395
+ spineJsonParser.scale = scale;
396
+ }
397
+
398
+ spineData = spineJsonParser.readSkeletonData(data.ske);
399
+ }
400
+ });
401
+ var obj = {
402
+ spineData: spineData,
403
+ ref: 0,
404
+ imageSrc: data.image.src
405
+ };
406
+ dataMap[name] = obj;
407
+ return obj;
408
+ }
409
+
410
+ function getSpineData(res, pixiSpine) {
411
+ return __awaiter(this, void 0, void 0, function () {
412
+ var data;
413
+ return __generator(this, function (_a) {
414
+ data = dataMap[res.name];
415
+
416
+ if (!data) {
417
+ if (res.complete) {
418
+ data = createSpineData(res.name, res.data, res.scale, pixiSpine);
419
+ } else if (!data) {
420
+ return [2];
421
+ }
422
+ }
423
+
424
+ retainTexture(res.data.image.src, res.data);
425
+ data.ref++;
426
+ return [2, data.spineData];
427
+ });
428
+ });
429
+ }
430
+
431
+ function releaseSpineData(resourceName, imageSrc) {
432
+ var data = dataMap[resourceName];
433
+
434
+ if (!data) {
435
+ return;
436
+ }
437
+
438
+ data.ref--;
439
+
440
+ if (data.ref <= 0) {
441
+ releaseTexture(imageSrc);
442
+ delete dataMap[resourceName];
443
+ }
444
+ }
445
+
446
+ var MaxRetryCount = 20;
447
+
448
+ var SpineSystem = function (_super) {
449
+ __extends(SpineSystem, _super);
450
+
451
+ function SpineSystem() {
452
+ var _this = _super !== null && _super.apply(this, arguments) || this;
453
+
454
+ _this.armatures = {};
455
+ return _this;
456
+ }
457
+
458
+ SpineSystem.prototype.init = function (_a) {
459
+ var _this = this;
460
+
461
+ var pixiSpine = _a.pixiSpine;
462
+ this.renderSystem = this.game.getSystem(pluginRenderer.RendererSystem);
463
+ this.renderSystem.rendererManager.register(this);
464
+ this.pixiSpine = pixiSpine;
465
+ this.game.canvas.addEventListener('webglcontextrestored', function () {
466
+ var objs = _this.game.gameObjects;
467
+ var toAdd = [];
468
+
469
+ for (var k in _this.armatures) {
470
+ var id = +k;
471
+
472
+ for (var i = 0; i < objs.length; ++i) {
473
+ var obj = objs[i];
474
+
475
+ if (obj.id === id) {
476
+ var sp = obj.getComponent(Spine$1);
477
+
478
+ if (sp) {
479
+ _this.remove({
480
+ type: eva_js.OBSERVER_TYPE.REMOVE,
481
+ gameObject: obj,
482
+ component: sp,
483
+ componentName: Spine$1.componentName
484
+ });
485
+
486
+ toAdd.push({
487
+ type: eva_js.OBSERVER_TYPE.ADD,
488
+ gameObject: obj,
489
+ component: sp,
490
+ componentName: Spine$1.componentName
491
+ });
492
+ }
493
+
494
+ break;
495
+ }
496
+ }
497
+ }
498
+
499
+ setTimeout(function () {
500
+ toAdd.forEach(function (obj) {
501
+ _this.add(obj);
502
+ });
503
+ }, 1000);
504
+ }, false);
505
+ };
506
+
507
+ SpineSystem.prototype.update = function (e) {
508
+ for (var key in this.armatures) {
509
+ this.armatures[key].update(e.deltaTime * 0.001);
510
+ this.armatures[key].updateTransform();
511
+ }
512
+
513
+ _super.prototype.update.call(this);
514
+ };
515
+
516
+ SpineSystem.prototype.componentChanged = function (changed) {
517
+ return __awaiter(this, void 0, void 0, function () {
518
+ return __generator(this, function (_a) {
519
+ if (changed.componentName === 'Spine') {
520
+ if (changed.type === eva_js.OBSERVER_TYPE.ADD) {
521
+ this.add(changed);
522
+ } else if (changed.type === eva_js.OBSERVER_TYPE.CHANGE) {
523
+ switch (changed.prop.prop[0]) {
524
+ case 'resource':
525
+ this.change(changed);
526
+ break;
527
+ }
528
+ } else if (changed.type === eva_js.OBSERVER_TYPE.REMOVE) {
529
+ this.remove(changed);
530
+ }
531
+ }
532
+
533
+ return [2];
534
+ });
535
+ });
536
+ };
537
+
538
+ SpineSystem.prototype.add = function (changed, count) {
539
+ return __awaiter(this, void 0, void 0, function () {
540
+ var component, res, spineData, container, armature, tran;
541
+
542
+ var _this = this;
543
+
544
+ return __generator(this, function (_a) {
545
+ switch (_a.label) {
546
+ case 0:
547
+ component = changed.component;
548
+ clearTimeout(component.addHandler);
549
+ return [4, eva_js.resource.getResource(component.resource)];
550
+
551
+ case 1:
552
+ res = _a.sent();
553
+ return [4, getSpineData(res, this.pixiSpine)];
554
+
555
+ case 2:
556
+ spineData = _a.sent();
557
+
558
+ if (!spineData) {
559
+ component.addHandler = setTimeout(function () {
560
+ if (!component.destroied) {
561
+ if (count === undefined) {
562
+ count = MaxRetryCount;
563
+ }
564
+
565
+ count--;
566
+
567
+ if (count > 0) {
568
+ _this.add(changed, count);
569
+ } else {
570
+ console.log('retry exceed max times', component.resource);
571
+ }
572
+ }
573
+ }, 1000);
574
+ return [2];
575
+ }
576
+
577
+ this.remove(changed);
578
+ container = this.renderSystem.containerManager.getContainer(changed.gameObject.id);
579
+
580
+ if (!container) {
581
+ return [2];
582
+ }
583
+
584
+ component.lastResource = component.resource;
585
+ armature = new this.pixiSpine.Spine(spineData);
586
+ this.armatures[changed.gameObject.id] = armature;
587
+
588
+ if (changed.gameObject && changed.gameObject.transform) {
589
+ tran = changed.gameObject.transform;
590
+ armature.x = tran.size.width * tran.origin.x;
591
+ armature.y = tran.size.height * tran.origin.y;
592
+ }
593
+
594
+ container.addChildAt(armature, 0);
595
+ armature.update();
596
+ armature.updateTransform();
597
+ component.armature = armature;
598
+
599
+ if (component.autoPlay) {
600
+ try {
601
+ armature.state.setAnimation(0, component.animationName, true);
602
+ } catch (e) {
603
+ console.log(e);
604
+ }
605
+ }
606
+
607
+ component.emit('loaded', {
608
+ resource: component.resource
609
+ });
610
+ armature.state.addListener({
611
+ start: function start(track, event) {
612
+ component.emit('start', {
613
+ track: track,
614
+ name: track.animation.name
615
+ });
616
+ },
617
+ complete: function complete(track, event) {
618
+ component.emit('complete', {
619
+ track: track,
620
+ name: track.animation.name
621
+ });
622
+ },
623
+ interrupt: function interrupt(track, event) {
624
+ component.emit('interrupt', {
625
+ track: track,
626
+ name: track.animation.name
627
+ });
628
+ },
629
+ end: function end(track, event) {
630
+ component.emit('end', {
631
+ track: track,
632
+ name: track.animation.name
633
+ });
634
+ },
635
+ event: function event(track, _event) {
636
+ component.emit('event', track, _event);
637
+ }
638
+ });
639
+ return [2];
640
+ }
641
+ });
642
+ });
643
+ };
644
+
645
+ SpineSystem.prototype.change = function (changed) {
646
+ this.remove(changed);
647
+ this.add(changed);
648
+ };
649
+
650
+ SpineSystem.prototype.remove = function (changed) {
651
+ var _a, _b;
652
+
653
+ return __awaiter(this, void 0, void 0, function () {
654
+ var component, armature, container, res;
655
+ return __generator(this, function (_c) {
656
+ switch (_c.label) {
657
+ case 0:
658
+ component = changed.component;
659
+ clearTimeout(component.addHandler);
660
+ armature = this.armatures[changed.gameObject.id];
661
+ container = this.renderSystem.containerManager.getContainer(changed.gameObject.id);
662
+
663
+ if (container && armature) {
664
+ container.removeChild(armature);
665
+ }
666
+
667
+ if (!component.armature) return [3, 2];
668
+ component.armature.destroy({
669
+ children: true
670
+ });
671
+ return [4, eva_js.resource.getResource(component.lastResource)];
672
+
673
+ case 1:
674
+ res = _c.sent();
675
+ releaseSpineData(res.name, (_b = (_a = res.data) === null || _a === void 0 ? void 0 : _a.image) === null || _b === void 0 ? void 0 : _b.src);
676
+ _c.label = 2;
677
+
678
+ case 2:
679
+ component.armature = null;
680
+ delete this.armatures[changed.gameObject.id];
681
+ if (changed.type === eva_js.OBSERVER_TYPE.CHANGE) ;
682
+ return [2];
683
+ }
684
+ });
685
+ });
686
+ };
687
+
688
+ SpineSystem.systemName = 'SpineSystem';
689
+ SpineSystem = __decorate([eva_js.decorators.componentObserver({
690
+ Spine: ['resource']
691
+ })], SpineSystem);
692
+ return SpineSystem;
693
+ }(pluginRenderer.Renderer);
694
+
695
+ var SpineSystem$1 = SpineSystem;
696
+ eva_js.resource.registerResourceType('SPINE');
697
+ exports.Spine = Spine$1;
698
+ exports.SpineSystem = SpineSystem$1;
699
+ Object.defineProperty(exports, '__esModule', {
700
+ value: true
701
+ });
702
+ return exports;
703
+ }({}, EVA, EVA.plugin.renderer, PIXI);
704
+
705
+ window.EVA.plugin.spineBase = window.EVA.plugin.spineBase || _EVA_IIFE_spineBase;