@eva/plugin-renderer-spine 1.2.2-type.0 → 1.2.4

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.
@@ -2,10 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var spineBase = require('@eva/spine-base');
5
6
  var pixi_js = require('pixi.js');
6
- var eva_js = require('@eva/eva.js');
7
- var pluginRenderer = require('@eva/plugin-renderer');
8
- var inspectorDecorator = require('@eva/inspector-decorator');
9
7
 
10
8
  /*! *****************************************************************************
11
9
  Copyright (c) Microsoft Corporation. All rights reserved.
@@ -34,459 +32,8 @@ function __extends$1(d, b) {
34
32
  extendStatics(d, b);
35
33
  function __() { this.constructor = d; }
36
34
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
37
- }
38
-
39
- function __decorate(decorators, target, key, desc) {
40
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
41
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
42
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
43
- return c > 3 && r && Object.defineProperty(target, key, r), r;
44
- }
45
-
46
- function __awaiter(thisArg, _arguments, P, generator) {
47
- return new (P || (P = Promise))(function (resolve, reject) {
48
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
49
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
50
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
51
- step((generator = generator.apply(thisArg, _arguments || [])).next());
52
- });
53
- }
54
-
55
- function __generator(thisArg, body) {
56
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
57
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
58
- function verb(n) { return function (v) { return step([n, v]); }; }
59
- function step(op) {
60
- if (f) throw new TypeError("Generator is already executing.");
61
- while (_) try {
62
- 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;
63
- if (y = 0, t) op = [op[0] & 2, t.value];
64
- switch (op[0]) {
65
- case 0: case 1: t = op; break;
66
- case 4: _.label++; return { value: op[1], done: false };
67
- case 5: _.label++; y = op[1]; op = [0]; continue;
68
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
69
- default:
70
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
71
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
72
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
73
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
74
- if (t[2]) _.ops.pop();
75
- _.trys.pop(); continue;
76
- }
77
- op = body.call(thisArg, _);
78
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
79
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
80
- }
81
35
  }
82
36
 
83
- var Spine$2 = (function (_super) {
84
- __extends$1(Spine, _super);
85
- function Spine() {
86
- var _this = _super !== null && _super.apply(this, arguments) || this;
87
- _this.resource = '';
88
- _this.animationName = '';
89
- _this.autoPlay = true;
90
- return _this;
91
- }
92
- Spine.prototype.init = function (obj) {
93
- if (!obj)
94
- return;
95
- Object.assign(this, obj);
96
- if (this.autoPlay) {
97
- this.play(this.animationName);
98
- }
99
- };
100
- Spine.prototype.onDestroy = function () {
101
- this.destroied = true;
102
- };
103
- Spine.prototype.play = function (name, loop, track) {
104
- try {
105
- if (name)
106
- this.animationName = name;
107
- if (!this.armature) {
108
- }
109
- else {
110
- if (track === undefined) {
111
- track = 0;
112
- }
113
- this.armature.state.setAnimation(track, this.animationName, loop);
114
- }
115
- }
116
- catch (e) {
117
- console.log(e);
118
- }
119
- };
120
- Spine.prototype.stop = function (track) {
121
- if (!this.armature) {
122
- return;
123
- }
124
- if (track === undefined) {
125
- track = 0;
126
- }
127
- this.armature.state.setEmptyAnimation(track, 0);
128
- };
129
- Spine.prototype.addAnimation = function (name, delay, loop, track) {
130
- try {
131
- if (!this.armature) {
132
- }
133
- else {
134
- if (track === undefined) {
135
- track = 0;
136
- }
137
- this.armature.state.addAnimation(track, name, loop, delay);
138
- }
139
- }
140
- catch (e) {
141
- console.log(e);
142
- }
143
- };
144
- Spine.prototype.setMix = function (from, to, duration) {
145
- if (!this.armature) ;
146
- else {
147
- this.armature.stateData.setMix(from, to, duration);
148
- }
149
- };
150
- Spine.prototype.getAnim = function (track) {
151
- if (track === void 0) { track = 0; }
152
- try {
153
- if (!this.armature) {
154
- }
155
- else {
156
- return this.armature.state.tracks[track].animation.name;
157
- }
158
- }
159
- catch (e) {
160
- console.log(e);
161
- }
162
- };
163
- Spine.prototype.setDefaultMix = function (duration) {
164
- if (!this.armature) ;
165
- else {
166
- this.armature.stateData.defaultMix = duration;
167
- }
168
- };
169
- Spine.prototype.setAttachment = function (slotName, attachmentName) {
170
- if (!this.armature) {
171
- return;
172
- }
173
- this.armature.skeleton.setAttachment(slotName, attachmentName);
174
- };
175
- Spine.prototype.getBone = function (boneName) {
176
- if (!this.armature) {
177
- return;
178
- }
179
- return this.armature.skeleton.findBone(boneName);
180
- };
181
- Spine.componentName = 'Spine';
182
- __decorate([
183
- inspectorDecorator.type('string')
184
- ], Spine.prototype, "resource", void 0);
185
- __decorate([
186
- inspectorDecorator.type('string')
187
- ], Spine.prototype, "animationName", void 0);
188
- __decorate([
189
- inspectorDecorator.type('boolean')
190
- ], Spine.prototype, "autoPlay", void 0);
191
- return Spine;
192
- }(eva_js.Component));
193
- var SpineBase = Spine$2;
194
-
195
- var texCache = {};
196
- function cacheImage(data) {
197
- var oldImg = data.image;
198
- return {
199
- tex: pixi_js.Texture.from(oldImg),
200
- count: 0,
201
- };
202
- }
203
- function retainTexture(name, data) {
204
- var cache = texCache[name];
205
- if (!cache) {
206
- cache = cacheImage(data);
207
- texCache[name] = cache;
208
- }
209
- cache.count++;
210
- return cache.tex;
211
- }
212
- function getTexture(imageSrc, data) {
213
- var cache = texCache[imageSrc];
214
- if (!cache) {
215
- cache = cacheImage(data);
216
- texCache[imageSrc] = cache;
217
- }
218
- return cache.tex;
219
- }
220
- function releaseTexture(imageSrc) {
221
- if (!imageSrc)
222
- return;
223
- setTimeout(function () {
224
- var cache = texCache[imageSrc];
225
- if (cache) {
226
- cache.count--;
227
- if (cache.count <= 0) {
228
- if (cache.tex) {
229
- cache.tex.destroy(true);
230
- cache.tex = null;
231
- }
232
- delete texCache[imageSrc];
233
- }
234
- }
235
- }, 100);
236
- }
237
-
238
- var dataMap = {};
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
- if (scale) {
249
- spineJsonParser.scale = scale;
250
- }
251
- spineData = spineJsonParser.readSkeletonData(data.ske);
252
- }
253
- });
254
- var obj = { spineData: spineData, ref: 0, imageSrc: data.image.src };
255
- dataMap[name] = obj;
256
- return obj;
257
- }
258
- function getSpineData(res, pixiSpine) {
259
- return __awaiter(this, void 0, void 0, function () {
260
- var data;
261
- return __generator(this, function (_a) {
262
- data = dataMap[res.name];
263
- if (!data) {
264
- if (res.complete) {
265
- data = createSpineData(res.name, res.data, res.scale, pixiSpine);
266
- }
267
- else if (!data) {
268
- return [2];
269
- }
270
- }
271
- retainTexture(res.data.image.src, res.data);
272
- data.ref++;
273
- return [2, data.spineData];
274
- });
275
- });
276
- }
277
- function releaseSpineData(resourceName, imageSrc) {
278
- var data = dataMap[resourceName];
279
- if (!data) {
280
- return;
281
- }
282
- data.ref--;
283
- if (data.ref <= 0) {
284
- releaseTexture(imageSrc);
285
- delete dataMap[resourceName];
286
- }
287
- }
288
-
289
- var MaxRetryCount = 20;
290
- var SpineSystem$2 = (function (_super) {
291
- __extends$1(SpineSystem, _super);
292
- function SpineSystem() {
293
- var _this = _super !== null && _super.apply(this, arguments) || this;
294
- _this.armatures = {};
295
- return _this;
296
- }
297
- SpineSystem.prototype.init = function (_a) {
298
- var _this = this;
299
- var pixiSpine = _a.pixiSpine;
300
- this.renderSystem = this.game.getSystem(pluginRenderer.RendererSystem);
301
- this.renderSystem.rendererManager.register(this);
302
- this.pixiSpine = pixiSpine;
303
- this.game.canvas.addEventListener('webglcontextrestored', function () {
304
- var objs = _this.game.gameObjects;
305
- var toAdd = [];
306
- for (var k in _this.armatures) {
307
- var id = +k;
308
- for (var i = 0; i < objs.length; ++i) {
309
- var obj = objs[i];
310
- if (obj.id === id) {
311
- var sp = obj.getComponent(SpineBase);
312
- if (sp) {
313
- _this.remove({
314
- type: eva_js.OBSERVER_TYPE.REMOVE,
315
- gameObject: obj,
316
- component: sp,
317
- componentName: SpineBase.componentName,
318
- });
319
- toAdd.push({
320
- type: eva_js.OBSERVER_TYPE.ADD,
321
- gameObject: obj,
322
- component: sp,
323
- componentName: SpineBase.componentName,
324
- });
325
- }
326
- break;
327
- }
328
- }
329
- }
330
- setTimeout(function () {
331
- toAdd.forEach(function (obj) {
332
- _this.add(obj);
333
- });
334
- }, 1000);
335
- }, false);
336
- };
337
- SpineSystem.prototype.update = function (e) {
338
- for (var key in this.armatures) {
339
- this.armatures[key].update(e.deltaTime * 0.001);
340
- this.armatures[key].updateTransform();
341
- }
342
- _super.prototype.update.call(this);
343
- };
344
- SpineSystem.prototype.componentChanged = function (changed) {
345
- return __awaiter(this, void 0, void 0, function () {
346
- return __generator(this, function (_a) {
347
- if (changed.componentName === 'Spine') {
348
- if (changed.type === eva_js.OBSERVER_TYPE.ADD) {
349
- this.add(changed);
350
- }
351
- else if (changed.type === eva_js.OBSERVER_TYPE.CHANGE) {
352
- switch (changed.prop.prop[0]) {
353
- case 'resource':
354
- this.change(changed);
355
- break;
356
- }
357
- }
358
- else if (changed.type === eva_js.OBSERVER_TYPE.REMOVE) {
359
- this.remove(changed);
360
- }
361
- }
362
- return [2];
363
- });
364
- });
365
- };
366
- SpineSystem.prototype.add = function (changed, count) {
367
- return __awaiter(this, void 0, void 0, function () {
368
- var component, res, spineData, container, armature, tran;
369
- var _this = this;
370
- return __generator(this, function (_a) {
371
- switch (_a.label) {
372
- case 0:
373
- component = changed.component;
374
- clearTimeout(component.addHandler);
375
- return [4, eva_js.resource.getResource(component.resource)];
376
- case 1:
377
- res = _a.sent();
378
- return [4, getSpineData(res, this.pixiSpine)];
379
- case 2:
380
- spineData = _a.sent();
381
- if (!spineData) {
382
- component.addHandler = setTimeout(function () {
383
- if (!component.destroied) {
384
- if (count === undefined) {
385
- count = MaxRetryCount;
386
- }
387
- count--;
388
- if (count > 0) {
389
- _this.add(changed, count);
390
- }
391
- else {
392
- console.log('retry exceed max times', component.resource);
393
- }
394
- }
395
- }, 1000);
396
- return [2];
397
- }
398
- this.remove(changed);
399
- container = this.renderSystem.containerManager.getContainer(changed.gameObject.id);
400
- if (!container) {
401
- return [2];
402
- }
403
- component.lastResource = component.resource;
404
- armature = new this.pixiSpine.Spine(spineData);
405
- this.armatures[changed.gameObject.id] = armature;
406
- if (changed.gameObject && changed.gameObject.transform) {
407
- tran = changed.gameObject.transform;
408
- armature.x = tran.size.width * tran.origin.x;
409
- armature.y = tran.size.height * tran.origin.y;
410
- }
411
- container.addChildAt(armature, 0);
412
- armature.update();
413
- armature.updateTransform();
414
- component.armature = armature;
415
- if (component.autoPlay) {
416
- try {
417
- armature.state.setAnimation(0, component.animationName, true);
418
- }
419
- catch (e) {
420
- console.log(e);
421
- }
422
- }
423
- component.emit('loaded', { resource: component.resource });
424
- armature.state.addListener({
425
- start: function (track, event) {
426
- component.emit('start', { track: track, name: track.animation.name });
427
- },
428
- complete: function (track, event) {
429
- component.emit('complete', { track: track, name: track.animation.name });
430
- },
431
- interrupt: function (track, event) {
432
- component.emit('interrupt', { track: track, name: track.animation.name });
433
- },
434
- end: function (track, event) {
435
- component.emit('end', { track: track, name: track.animation.name });
436
- },
437
- event: function (track, event) {
438
- component.emit('event', track, event);
439
- },
440
- });
441
- return [2];
442
- }
443
- });
444
- });
445
- };
446
- SpineSystem.prototype.change = function (changed) {
447
- this.remove(changed);
448
- this.add(changed);
449
- };
450
- SpineSystem.prototype.remove = function (changed) {
451
- var _a, _b;
452
- return __awaiter(this, void 0, void 0, function () {
453
- var component, armature, container, res;
454
- return __generator(this, function (_c) {
455
- switch (_c.label) {
456
- case 0:
457
- component = changed.component;
458
- clearTimeout(component.addHandler);
459
- armature = this.armatures[changed.gameObject.id];
460
- container = this.renderSystem.containerManager.getContainer(changed.gameObject.id);
461
- if (container && armature) {
462
- container.removeChild(armature);
463
- }
464
- if (!component.armature) return [3, 2];
465
- component.armature.destroy({ children: true });
466
- return [4, eva_js.resource.getResource(component.lastResource)];
467
- case 1:
468
- res = _c.sent();
469
- releaseSpineData(res.name, (_b = (_a = res.data) === null || _a === void 0 ? void 0 : _a.image) === null || _b === void 0 ? void 0 : _b.src);
470
- _c.label = 2;
471
- case 2:
472
- component.armature = null;
473
- delete this.armatures[changed.gameObject.id];
474
- if (changed.type === eva_js.OBSERVER_TYPE.CHANGE) ;
475
- return [2];
476
- }
477
- });
478
- });
479
- };
480
- SpineSystem.systemName = 'SpineSystem';
481
- SpineSystem = __decorate([
482
- eva_js.decorators.componentObserver({
483
- Spine: ['resource'],
484
- })
485
- ], SpineSystem);
486
- return SpineSystem;
487
- }(pluginRenderer.Renderer));
488
- var SpineSystemBase = SpineSystem$2;
489
-
490
37
  // https://github.com/pixijs/spine/tree/pixi4
491
38
 
492
39
  var VERSION = '4.8.9';
@@ -8138,7 +7685,7 @@ var SpineSystem = (function (_super) {
8138
7685
  _super.prototype.init.call(this, { pixiSpine: pixiSpine });
8139
7686
  };
8140
7687
  return SpineSystem;
8141
- }(SpineSystemBase));
7688
+ }(spineBase.SpineSystem));
8142
7689
  var SpineSystem$1 = SpineSystem;
8143
7690
 
8144
7691
  var Spine = (function (_super) {
@@ -8147,7 +7694,7 @@ var Spine = (function (_super) {
8147
7694
  return _super !== null && _super.apply(this, arguments) || this;
8148
7695
  }
8149
7696
  return Spine;
8150
- }(SpineBase));
7697
+ }(spineBase.Spine));
8151
7698
  var Spine$1 = Spine;
8152
7699
 
8153
7700
  exports.Spine = Spine$1;