@eva/plugin-renderer-spine 1.2.2-alpha.1 → 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.
@@ -2,10 +2,10 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var pixi_js = require('pixi.js');
5
6
  var eva_js = require('@eva/eva.js');
6
7
  var pluginRenderer = require('@eva/plugin-renderer');
7
8
  var inspectorDecorator = require('@eva/inspector-decorator');
8
- var pixi_js = require('pixi.js');
9
9
 
10
10
  /*! *****************************************************************************
11
11
  Copyright (c) Microsoft Corporation. All rights reserved.
@@ -80,7 +80,7 @@ function __generator(thisArg, body) {
80
80
  }
81
81
  }
82
82
 
83
- var Spine = (function (_super) {
83
+ var Spine$2 = (function (_super) {
84
84
  __extends$1(Spine, _super);
85
85
  function Spine() {
86
86
  var _this = _super !== null && _super.apply(this, arguments) || this;
@@ -190,222 +190,519 @@ var Spine = (function (_super) {
190
190
  ], Spine.prototype, "autoPlay", void 0);
191
191
  return Spine;
192
192
  }(eva_js.Component));
193
- var Spine$1 = Spine;
193
+ var SpineBase = Spine$2;
194
194
 
195
- const VERSION = '4.8.9';
196
- const loader = pixi_js.loaders.shared;
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
+ }
197
237
 
198
- const PIXI = {
199
- Texture: pixi_js.Texture,
200
- Rectangle: pixi_js.Rectangle,
201
- Sprite: pixi_js.Sprite,
202
- Graphics: pixi_js.Graphics,
203
- mesh: pixi_js.mesh,
204
- Matrix: pixi_js.Matrix,
205
- Container: pixi_js.Container,
206
- VERSION,
207
- loaders: pixi_js.loaders,
208
- loader,
209
- SCALE_MODES: pixi_js.SCALE_MODES,
210
- utils: pixi_js.utils,
211
- TransformBase: pixi_js.TransformBase,
212
- TransformStatic: pixi_js.TransformStatic,
213
- DisplayObject: pixi_js.DisplayObject,
214
- Polygon: pixi_js.Polygon,
215
- BaseTexture: pixi_js.BaseTexture,
216
- };
217
- var __extends =
218
- (undefined && undefined.__extends) ||
219
- (function () {
220
- var extendStatics =
221
- Object.setPrototypeOf ||
222
- ({ __proto__: [] } instanceof Array &&
223
- function (d, b) {
224
- d.__proto__ = b;
225
- }) ||
226
- function (d, b) {
227
- for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
228
- };
229
- return function (d, b) {
230
- extendStatics(d, b);
231
- function __() {
232
- this.constructor = d;
233
- }
234
- d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
235
- };
236
- })();
237
- var pixi_spine;
238
- (function (pixi_spine) {
239
- (function (core) {
240
- var Animation = (function () {
241
- function Animation(name, timelines, duration) {
242
- if (name == null) throw new Error('name cannot be null.');
243
- if (timelines == null) throw new Error('timelines cannot be null.');
244
- this.name = name;
245
- this.timelines = timelines;
246
- this.duration = duration;
247
- }
248
- Animation.prototype.apply = function (skeleton, lastTime, time, loop, events, alpha, blend, direction) {
249
- if (skeleton == null) throw new Error('skeleton cannot be null.');
250
- if (loop && this.duration != 0) {
251
- time %= this.duration;
252
- if (lastTime > 0) lastTime %= this.duration;
253
- }
254
- var timelines = this.timelines;
255
- for (var i = 0, n = timelines.length; i < n; i++)
256
- timelines[i].apply(skeleton, lastTime, time, events, alpha, blend, direction);
257
- };
258
- Animation.binarySearch = function (values, target, step) {
259
- if (step === void 0) {
260
- step = 1;
261
- }
262
- var low = 0;
263
- var high = values.length / step - 2;
264
- if (high == 0) return step;
265
- var current = high >>> 1;
266
- while (true) {
267
- if (values[(current + 1) * step] <= target) low = current + 1;
268
- else high = current;
269
- if (low == high) return (low + 1) * step;
270
- current = (low + high) >>> 1;
271
- }
272
- };
273
- Animation.linearSearch = function (values, target, step) {
274
- for (var i = 0, last = values.length - step; i <= last; i += step) if (values[i] > target) return i;
275
- return -1;
276
- };
277
- return Animation;
278
- })();
279
- core.Animation = Animation;
280
- var MixBlend;
281
- (function (MixBlend) {
282
- MixBlend[(MixBlend['setup'] = 0)] = 'setup';
283
- MixBlend[(MixBlend['first'] = 1)] = 'first';
284
- MixBlend[(MixBlend['replace'] = 2)] = 'replace';
285
- MixBlend[(MixBlend['add'] = 3)] = 'add';
286
- })((MixBlend = core.MixBlend || (core.MixBlend = {})));
287
- var MixDirection;
288
- (function (MixDirection) {
289
- MixDirection[(MixDirection['in'] = 0)] = 'in';
290
- MixDirection[(MixDirection['out'] = 1)] = 'out';
291
- })((MixDirection = core.MixDirection || (core.MixDirection = {})));
292
- var TimelineType;
293
- (function (TimelineType) {
294
- TimelineType[(TimelineType['rotate'] = 0)] = 'rotate';
295
- TimelineType[(TimelineType['translate'] = 1)] = 'translate';
296
- TimelineType[(TimelineType['scale'] = 2)] = 'scale';
297
- TimelineType[(TimelineType['shear'] = 3)] = 'shear';
298
- TimelineType[(TimelineType['attachment'] = 4)] = 'attachment';
299
- TimelineType[(TimelineType['color'] = 5)] = 'color';
300
- TimelineType[(TimelineType['deform'] = 6)] = 'deform';
301
- TimelineType[(TimelineType['event'] = 7)] = 'event';
302
- TimelineType[(TimelineType['drawOrder'] = 8)] = 'drawOrder';
303
- TimelineType[(TimelineType['ikConstraint'] = 9)] = 'ikConstraint';
304
- TimelineType[(TimelineType['transformConstraint'] = 10)] = 'transformConstraint';
305
- TimelineType[(TimelineType['pathConstraintPosition'] = 11)] = 'pathConstraintPosition';
306
- TimelineType[(TimelineType['pathConstraintSpacing'] = 12)] = 'pathConstraintSpacing';
307
- TimelineType[(TimelineType['pathConstraintMix'] = 13)] = 'pathConstraintMix';
308
- TimelineType[(TimelineType['twoColor'] = 14)] = 'twoColor';
309
- })((TimelineType = core.TimelineType || (core.TimelineType = {})));
310
- var CurveTimeline = (function () {
311
- function CurveTimeline(frameCount) {
312
- if (frameCount <= 0) throw new Error('frameCount must be > 0: ' + frameCount);
313
- this.curves = core.Utils.newFloatArray((frameCount - 1) * CurveTimeline.BEZIER_SIZE);
314
- }
315
- CurveTimeline.prototype.getFrameCount = function () {
316
- return this.curves.length / CurveTimeline.BEZIER_SIZE + 1;
317
- };
318
- CurveTimeline.prototype.setLinear = function (frameIndex) {
319
- this.curves[frameIndex * CurveTimeline.BEZIER_SIZE] = CurveTimeline.LINEAR;
320
- };
321
- CurveTimeline.prototype.setStepped = function (frameIndex) {
322
- this.curves[frameIndex * CurveTimeline.BEZIER_SIZE] = CurveTimeline.STEPPED;
323
- };
324
- CurveTimeline.prototype.getCurveType = function (frameIndex) {
325
- var index = frameIndex * CurveTimeline.BEZIER_SIZE;
326
- if (index == this.curves.length) return CurveTimeline.LINEAR;
327
- var type = this.curves[index];
328
- if (type == CurveTimeline.LINEAR) return CurveTimeline.LINEAR;
329
- if (type == CurveTimeline.STEPPED) return CurveTimeline.STEPPED;
330
- return CurveTimeline.BEZIER;
331
- };
332
- CurveTimeline.prototype.setCurve = function (frameIndex, cx1, cy1, cx2, cy2) {
333
- var tmpx = (-cx1 * 2 + cx2) * 0.03,
334
- tmpy = (-cy1 * 2 + cy2) * 0.03;
335
- var dddfx = ((cx1 - cx2) * 3 + 1) * 0.006,
336
- dddfy = ((cy1 - cy2) * 3 + 1) * 0.006;
337
- var ddfx = tmpx * 2 + dddfx,
338
- ddfy = tmpy * 2 + dddfy;
339
- var dfx = cx1 * 0.3 + tmpx + dddfx * 0.16666667,
340
- dfy = cy1 * 0.3 + tmpy + dddfy * 0.16666667;
341
- var i = frameIndex * CurveTimeline.BEZIER_SIZE;
342
- var curves = this.curves;
343
- curves[i++] = CurveTimeline.BEZIER;
344
- var x = dfx,
345
- y = dfy;
346
- for (var n = i + CurveTimeline.BEZIER_SIZE - 1; i < n; i += 2) {
347
- curves[i] = x;
348
- curves[i + 1] = y;
349
- dfx += ddfx;
350
- dfy += ddfy;
351
- ddfx += dddfx;
352
- ddfy += dddfy;
353
- x += dfx;
354
- y += dfy;
355
- }
356
- };
357
- CurveTimeline.prototype.getCurvePercent = function (frameIndex, percent) {
358
- percent = core.MathUtils.clamp(percent, 0, 1);
359
- var curves = this.curves;
360
- var i = frameIndex * CurveTimeline.BEZIER_SIZE;
361
- var type = curves[i];
362
- if (type == CurveTimeline.LINEAR) return percent;
363
- if (type == CurveTimeline.STEPPED) return 0;
364
- i++;
365
- var x = 0;
366
- for (var start = i, n = i + CurveTimeline.BEZIER_SIZE - 1; i < n; i += 2) {
367
- x = curves[i];
368
- if (x >= percent) {
369
- var prevX = void 0,
370
- prevY = void 0;
371
- if (i == start) {
372
- prevX = 0;
373
- prevY = 0;
374
- } else {
375
- prevX = curves[i - 2];
376
- prevY = curves[i - 1];
377
- }
378
- return prevY + ((curves[i + 1] - prevY) * (percent - prevX)) / (x - prevX);
379
- }
380
- }
381
- var y = curves[i - 1];
382
- return y + ((1 - y) * (percent - x)) / (1 - x);
383
- };
384
- CurveTimeline.LINEAR = 0;
385
- CurveTimeline.STEPPED = 1;
386
- CurveTimeline.BEZIER = 2;
387
- CurveTimeline.BEZIER_SIZE = 10 * 2 - 1;
388
- return CurveTimeline;
389
- })();
390
- core.CurveTimeline = CurveTimeline;
391
- var RotateTimeline = (function (_super) {
392
- __extends(RotateTimeline, _super);
393
- function RotateTimeline(frameCount) {
394
- var _this = _super.call(this, frameCount) || this;
395
- _this.frames = core.Utils.newFloatArray(frameCount << 1);
396
- return _this;
397
- }
398
- RotateTimeline.prototype.getPropertyId = function () {
399
- return (TimelineType.rotate << 24) + this.boneIndex;
400
- };
401
- RotateTimeline.prototype.setFrame = function (frameIndex, time, degrees) {
402
- frameIndex <<= 1;
403
- this.frames[frameIndex] = time;
404
- this.frames[frameIndex + RotateTimeline.ROTATION] = degrees;
405
- };
406
- RotateTimeline.prototype.apply = function (skeleton, lastTime, time, events, alpha, blend, direction) {
407
- var frames = this.frames;
408
- var bone = skeleton.bones[this.boneIndex];
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
+ // https://github.com/pixijs/spine/tree/pixi4
491
+
492
+ var VERSION = '4.8.9';
493
+ var loader = pixi_js.loaders.shared;
494
+
495
+ var PIXI = {
496
+ Texture: pixi_js.Texture,
497
+ Rectangle: pixi_js.Rectangle,
498
+ Sprite: pixi_js.Sprite,
499
+ Graphics: pixi_js.Graphics,
500
+ mesh: pixi_js.mesh,
501
+ Matrix: pixi_js.Matrix,
502
+ Container: pixi_js.Container,
503
+ VERSION,
504
+ loaders: pixi_js.loaders,
505
+ loader,
506
+ SCALE_MODES: pixi_js.SCALE_MODES,
507
+ utils: pixi_js.utils,
508
+ TransformBase: pixi_js.TransformBase,
509
+ TransformStatic: pixi_js.TransformStatic,
510
+ DisplayObject: pixi_js.DisplayObject,
511
+ Polygon: pixi_js.Polygon,
512
+ BaseTexture: pixi_js.BaseTexture,
513
+ };
514
+ var __extends =
515
+ (undefined && undefined.__extends) ||
516
+ (function () {
517
+ var extendStatics =
518
+ Object.setPrototypeOf ||
519
+ ({ __proto__: [] } instanceof Array &&
520
+ function (d, b) {
521
+ d.__proto__ = b;
522
+ }) ||
523
+ function (d, b) {
524
+ for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
525
+ };
526
+ return function (d, b) {
527
+ extendStatics(d, b);
528
+ function __() {
529
+ this.constructor = d;
530
+ }
531
+ d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
532
+ };
533
+ })();
534
+ var pixi_spine;
535
+ (function (pixi_spine) {
536
+ (function (core) {
537
+ var Animation = (function () {
538
+ function Animation(name, timelines, duration) {
539
+ if (name == null) throw new Error('name cannot be null.');
540
+ if (timelines == null) throw new Error('timelines cannot be null.');
541
+ this.name = name;
542
+ this.timelines = timelines;
543
+ this.duration = duration;
544
+ }
545
+ Animation.prototype.apply = function (skeleton, lastTime, time, loop, events, alpha, blend, direction) {
546
+ if (skeleton == null) throw new Error('skeleton cannot be null.');
547
+ if (loop && this.duration != 0) {
548
+ time %= this.duration;
549
+ if (lastTime > 0) lastTime %= this.duration;
550
+ }
551
+ var timelines = this.timelines;
552
+ for (var i = 0, n = timelines.length; i < n; i++)
553
+ timelines[i].apply(skeleton, lastTime, time, events, alpha, blend, direction);
554
+ };
555
+ Animation.binarySearch = function (values, target, step) {
556
+ if (step === void 0) {
557
+ step = 1;
558
+ }
559
+ var low = 0;
560
+ var high = values.length / step - 2;
561
+ if (high == 0) return step;
562
+ var current = high >>> 1;
563
+ while (true) {
564
+ if (values[(current + 1) * step] <= target) low = current + 1;
565
+ else high = current;
566
+ if (low == high) return (low + 1) * step;
567
+ current = (low + high) >>> 1;
568
+ }
569
+ };
570
+ Animation.linearSearch = function (values, target, step) {
571
+ for (var i = 0, last = values.length - step; i <= last; i += step) if (values[i] > target) return i;
572
+ return -1;
573
+ };
574
+ return Animation;
575
+ })();
576
+ core.Animation = Animation;
577
+ var MixBlend;
578
+ (function (MixBlend) {
579
+ MixBlend[(MixBlend['setup'] = 0)] = 'setup';
580
+ MixBlend[(MixBlend['first'] = 1)] = 'first';
581
+ MixBlend[(MixBlend['replace'] = 2)] = 'replace';
582
+ MixBlend[(MixBlend['add'] = 3)] = 'add';
583
+ })((MixBlend = core.MixBlend || (core.MixBlend = {})));
584
+ var MixDirection;
585
+ (function (MixDirection) {
586
+ MixDirection[(MixDirection['in'] = 0)] = 'in';
587
+ MixDirection[(MixDirection['out'] = 1)] = 'out';
588
+ })((MixDirection = core.MixDirection || (core.MixDirection = {})));
589
+ var TimelineType;
590
+ (function (TimelineType) {
591
+ TimelineType[(TimelineType['rotate'] = 0)] = 'rotate';
592
+ TimelineType[(TimelineType['translate'] = 1)] = 'translate';
593
+ TimelineType[(TimelineType['scale'] = 2)] = 'scale';
594
+ TimelineType[(TimelineType['shear'] = 3)] = 'shear';
595
+ TimelineType[(TimelineType['attachment'] = 4)] = 'attachment';
596
+ TimelineType[(TimelineType['color'] = 5)] = 'color';
597
+ TimelineType[(TimelineType['deform'] = 6)] = 'deform';
598
+ TimelineType[(TimelineType['event'] = 7)] = 'event';
599
+ TimelineType[(TimelineType['drawOrder'] = 8)] = 'drawOrder';
600
+ TimelineType[(TimelineType['ikConstraint'] = 9)] = 'ikConstraint';
601
+ TimelineType[(TimelineType['transformConstraint'] = 10)] = 'transformConstraint';
602
+ TimelineType[(TimelineType['pathConstraintPosition'] = 11)] = 'pathConstraintPosition';
603
+ TimelineType[(TimelineType['pathConstraintSpacing'] = 12)] = 'pathConstraintSpacing';
604
+ TimelineType[(TimelineType['pathConstraintMix'] = 13)] = 'pathConstraintMix';
605
+ TimelineType[(TimelineType['twoColor'] = 14)] = 'twoColor';
606
+ })((TimelineType = core.TimelineType || (core.TimelineType = {})));
607
+ var CurveTimeline = (function () {
608
+ function CurveTimeline(frameCount) {
609
+ if (frameCount <= 0) throw new Error('frameCount must be > 0: ' + frameCount);
610
+ this.curves = core.Utils.newFloatArray((frameCount - 1) * CurveTimeline.BEZIER_SIZE);
611
+ }
612
+ CurveTimeline.prototype.getFrameCount = function () {
613
+ return this.curves.length / CurveTimeline.BEZIER_SIZE + 1;
614
+ };
615
+ CurveTimeline.prototype.setLinear = function (frameIndex) {
616
+ this.curves[frameIndex * CurveTimeline.BEZIER_SIZE] = CurveTimeline.LINEAR;
617
+ };
618
+ CurveTimeline.prototype.setStepped = function (frameIndex) {
619
+ this.curves[frameIndex * CurveTimeline.BEZIER_SIZE] = CurveTimeline.STEPPED;
620
+ };
621
+ CurveTimeline.prototype.getCurveType = function (frameIndex) {
622
+ var index = frameIndex * CurveTimeline.BEZIER_SIZE;
623
+ if (index == this.curves.length) return CurveTimeline.LINEAR;
624
+ var type = this.curves[index];
625
+ if (type == CurveTimeline.LINEAR) return CurveTimeline.LINEAR;
626
+ if (type == CurveTimeline.STEPPED) return CurveTimeline.STEPPED;
627
+ return CurveTimeline.BEZIER;
628
+ };
629
+ CurveTimeline.prototype.setCurve = function (frameIndex, cx1, cy1, cx2, cy2) {
630
+ var tmpx = (-cx1 * 2 + cx2) * 0.03,
631
+ tmpy = (-cy1 * 2 + cy2) * 0.03;
632
+ var dddfx = ((cx1 - cx2) * 3 + 1) * 0.006,
633
+ dddfy = ((cy1 - cy2) * 3 + 1) * 0.006;
634
+ var ddfx = tmpx * 2 + dddfx,
635
+ ddfy = tmpy * 2 + dddfy;
636
+ var dfx = cx1 * 0.3 + tmpx + dddfx * 0.16666667,
637
+ dfy = cy1 * 0.3 + tmpy + dddfy * 0.16666667;
638
+ var i = frameIndex * CurveTimeline.BEZIER_SIZE;
639
+ var curves = this.curves;
640
+ curves[i++] = CurveTimeline.BEZIER;
641
+ var x = dfx,
642
+ y = dfy;
643
+ for (var n = i + CurveTimeline.BEZIER_SIZE - 1; i < n; i += 2) {
644
+ curves[i] = x;
645
+ curves[i + 1] = y;
646
+ dfx += ddfx;
647
+ dfy += ddfy;
648
+ ddfx += dddfx;
649
+ ddfy += dddfy;
650
+ x += dfx;
651
+ y += dfy;
652
+ }
653
+ };
654
+ CurveTimeline.prototype.getCurvePercent = function (frameIndex, percent) {
655
+ percent = core.MathUtils.clamp(percent, 0, 1);
656
+ var curves = this.curves;
657
+ var i = frameIndex * CurveTimeline.BEZIER_SIZE;
658
+ var type = curves[i];
659
+ if (type == CurveTimeline.LINEAR) return percent;
660
+ if (type == CurveTimeline.STEPPED) return 0;
661
+ i++;
662
+ var x = 0;
663
+ for (var start = i, n = i + CurveTimeline.BEZIER_SIZE - 1; i < n; i += 2) {
664
+ x = curves[i];
665
+ if (x >= percent) {
666
+ var prevX = void 0,
667
+ prevY = void 0;
668
+ if (i == start) {
669
+ prevX = 0;
670
+ prevY = 0;
671
+ } else {
672
+ prevX = curves[i - 2];
673
+ prevY = curves[i - 1];
674
+ }
675
+ return prevY + ((curves[i + 1] - prevY) * (percent - prevX)) / (x - prevX);
676
+ }
677
+ }
678
+ var y = curves[i - 1];
679
+ return y + ((1 - y) * (percent - x)) / (1 - x);
680
+ };
681
+ CurveTimeline.LINEAR = 0;
682
+ CurveTimeline.STEPPED = 1;
683
+ CurveTimeline.BEZIER = 2;
684
+ CurveTimeline.BEZIER_SIZE = 10 * 2 - 1;
685
+ return CurveTimeline;
686
+ })();
687
+ core.CurveTimeline = CurveTimeline;
688
+ var RotateTimeline = (function (_super) {
689
+ __extends(RotateTimeline, _super);
690
+ function RotateTimeline(frameCount) {
691
+ var _this = _super.call(this, frameCount) || this;
692
+ _this.frames = core.Utils.newFloatArray(frameCount << 1);
693
+ return _this;
694
+ }
695
+ RotateTimeline.prototype.getPropertyId = function () {
696
+ return (TimelineType.rotate << 24) + this.boneIndex;
697
+ };
698
+ RotateTimeline.prototype.setFrame = function (frameIndex, time, degrees) {
699
+ frameIndex <<= 1;
700
+ this.frames[frameIndex] = time;
701
+ this.frames[frameIndex + RotateTimeline.ROTATION] = degrees;
702
+ };
703
+ RotateTimeline.prototype.apply = function (skeleton, lastTime, time, events, alpha, blend, direction) {
704
+ var frames = this.frames;
705
+ var bone = skeleton.bones[this.boneIndex];
409
706
  if (time < frames[0]) {
410
707
  switch (blend) {
411
708
  case MixBlend.setup:
@@ -7810,329 +8107,48 @@ var pixi_spine;
7810
8107
  function syncImageLoaderAdapter(baseUrl, crossOrigin) {
7811
8108
  if (baseUrl && baseUrl.lastIndexOf('/') !== baseUrl.length - 1) {
7812
8109
  baseUrl += '/';
7813
- }
7814
- return function (line, callback) {
7815
- callback(PIXI.BaseTexture.fromImage(line, crossOrigin));
7816
- };
7817
- }
7818
- pixi_spine.syncImageLoaderAdapter = syncImageLoaderAdapter;
7819
- function staticImageLoader(pages) {
7820
- return function (line, callback) {
7821
- var page = pages[line] || pages['default'];
7822
- if (page && page.baseTexture) callback(page.baseTexture);
7823
- else callback(page);
7824
- };
7825
- }
7826
- pixi_spine.staticImageLoader = staticImageLoader;
7827
- if (PIXI.loaders.Loader) {
7828
- PIXI.loaders.Loader.addPixiMiddleware(atlasParser);
7829
- PIXI.loader.use(atlasParser());
7830
- }
7831
- })(pixi_spine || (pixi_spine = {}));
7832
-
7833
- var pixispine = pixi_spine;
7834
-
7835
- var texCache = {};
7836
- function cacheImage(data) {
7837
- var oldImg = data.image;
7838
- return {
7839
- tex: pixi_js.Texture.from(oldImg),
7840
- count: 0,
7841
- };
7842
- }
7843
- function retainTexture(name, data) {
7844
- var cache = texCache[name];
7845
- if (!cache) {
7846
- cache = cacheImage(data);
7847
- texCache[name] = cache;
7848
- }
7849
- cache.count++;
7850
- return cache.tex;
7851
- }
7852
- function getTexture(imageSrc, data) {
7853
- var cache = texCache[imageSrc];
7854
- if (!cache) {
7855
- cache = cacheImage(data);
7856
- texCache[imageSrc] = cache;
7857
- }
7858
- return cache.tex;
7859
- }
7860
- function releaseTexture(imageSrc) {
7861
- if (!imageSrc)
7862
- return;
7863
- setTimeout(function () {
7864
- var cache = texCache[imageSrc];
7865
- if (cache) {
7866
- cache.count--;
7867
- if (cache.count <= 0) {
7868
- if (cache.tex) {
7869
- cache.tex.destroy(true);
7870
- cache.tex = null;
7871
- }
7872
- delete texCache[imageSrc];
7873
- }
7874
- }
7875
- }, 100);
7876
- }
8110
+ }
8111
+ return function (line, callback) {
8112
+ callback(PIXI.BaseTexture.fromImage(line, crossOrigin));
8113
+ };
8114
+ }
8115
+ pixi_spine.syncImageLoaderAdapter = syncImageLoaderAdapter;
8116
+ function staticImageLoader(pages) {
8117
+ return function (line, callback) {
8118
+ var page = pages[line] || pages['default'];
8119
+ if (page && page.baseTexture) callback(page.baseTexture);
8120
+ else callback(page);
8121
+ };
8122
+ }
8123
+ pixi_spine.staticImageLoader = staticImageLoader;
8124
+ if (PIXI.loaders.Loader) {
8125
+ PIXI.loaders.Loader.addPixiMiddleware(atlasParser);
8126
+ PIXI.loader.use(atlasParser());
8127
+ }
8128
+ })(pixi_spine || (pixi_spine = {}));
7877
8129
 
7878
- var dataMap = {};
7879
- function createSpineData(name, data, scale) {
7880
- var spineData = null;
7881
- var img = getTexture(data.image.src, data);
7882
- new pixispine.core.TextureAtlas(data.atlas, function (line, callback) {
7883
- callback(img.baseTexture);
7884
- }, function (spineAtlas) {
7885
- if (spineAtlas) {
7886
- var attachmentLoader = new pixispine.core.AtlasAttachmentLoader(spineAtlas);
7887
- var spineJsonParser = new pixispine.core.SkeletonJson(attachmentLoader);
7888
- if (scale) {
7889
- spineJsonParser.scale = scale;
7890
- }
7891
- spineData = spineJsonParser.readSkeletonData(data.ske);
7892
- }
7893
- });
7894
- var obj = { spineData: spineData, ref: 0, imageSrc: data.image.src };
7895
- dataMap[name] = obj;
7896
- return obj;
7897
- }
7898
- eva_js.resource.registerInstance('SPINE', function (info) {
7899
- return createSpineData(info.name, info.data, info.scale);
7900
- });
7901
- eva_js.resource.registerDestroy('SPINE', function (info) {
7902
- if (info.instance) {
7903
- releaseTexture(info.data.image.src);
7904
- info.instance = null;
7905
- }
7906
- });
7907
- function getSpineData(res) {
7908
- return __awaiter(this, void 0, void 0, function () {
7909
- var data;
7910
- return __generator(this, function (_a) {
7911
- data = dataMap[res.name];
7912
- if (!data) {
7913
- if (res.complete) {
7914
- data = createSpineData(res.name, res.data, res.scale);
7915
- }
7916
- else if (!data) {
7917
- return [2];
7918
- }
7919
- }
7920
- retainTexture(res.data.image.src, res.data);
7921
- data.ref++;
7922
- return [2, data.spineData];
7923
- });
7924
- });
7925
- }
7926
- function releaseSpineData(resourceName, imageSrc) {
7927
- var data = dataMap[resourceName];
7928
- if (!data) {
7929
- return;
7930
- }
7931
- data.ref--;
7932
- if (data.ref <= 0) {
7933
- releaseTexture(imageSrc);
7934
- delete dataMap[resourceName];
7935
- }
7936
- }
8130
+ var pixiSpine = pixi_spine;
7937
8131
 
7938
- var MaxRetryCount = 20;
7939
8132
  var SpineSystem = (function (_super) {
7940
8133
  __extends$1(SpineSystem, _super);
7941
8134
  function SpineSystem() {
7942
- var _this = _super !== null && _super.apply(this, arguments) || this;
7943
- _this.armatures = {};
7944
- return _this;
8135
+ return _super !== null && _super.apply(this, arguments) || this;
7945
8136
  }
7946
8137
  SpineSystem.prototype.init = function () {
7947
- var _this = this;
7948
- this.renderSystem = this.game.getSystem(pluginRenderer.RendererSystem);
7949
- this.renderSystem.rendererManager.register(this);
7950
- this.game.canvas.addEventListener('webglcontextrestored', function () {
7951
- var objs = _this.game.gameObjects;
7952
- var toAdd = [];
7953
- for (var k in _this.armatures) {
7954
- var id = +k;
7955
- for (var i = 0; i < objs.length; ++i) {
7956
- var obj = objs[i];
7957
- if (obj.id === id) {
7958
- var sp = obj.getComponent(Spine$1);
7959
- if (sp) {
7960
- _this.remove({
7961
- type: eva_js.OBSERVER_TYPE.REMOVE,
7962
- gameObject: obj,
7963
- component: sp,
7964
- componentName: Spine$1.componentName,
7965
- });
7966
- toAdd.push({
7967
- type: eva_js.OBSERVER_TYPE.ADD,
7968
- gameObject: obj,
7969
- component: sp,
7970
- componentName: Spine$1.componentName,
7971
- });
7972
- }
7973
- break;
7974
- }
7975
- }
7976
- }
7977
- setTimeout(function () {
7978
- toAdd.forEach(function (obj) {
7979
- _this.add(obj);
7980
- });
7981
- }, 1000);
7982
- }, false);
7983
- };
7984
- SpineSystem.prototype.update = function (e) {
7985
- for (var key in this.armatures) {
7986
- this.armatures[key].update(e.deltaTime * 0.001);
7987
- this.armatures[key].updateTransform();
7988
- }
7989
- _super.prototype.update.call(this);
7990
- };
7991
- SpineSystem.prototype.componentChanged = function (changed) {
7992
- return __awaiter(this, void 0, void 0, function () {
7993
- return __generator(this, function (_a) {
7994
- if (changed.componentName === 'Spine') {
7995
- if (changed.type === eva_js.OBSERVER_TYPE.ADD) {
7996
- this.add(changed);
7997
- }
7998
- else if (changed.type === eva_js.OBSERVER_TYPE.CHANGE) {
7999
- switch (changed.prop.prop[0]) {
8000
- case 'resource':
8001
- this.change(changed);
8002
- break;
8003
- }
8004
- }
8005
- else if (changed.type === eva_js.OBSERVER_TYPE.REMOVE) {
8006
- this.remove(changed);
8007
- }
8008
- }
8009
- return [2];
8010
- });
8011
- });
8012
- };
8013
- SpineSystem.prototype.add = function (changed, count) {
8014
- return __awaiter(this, void 0, void 0, function () {
8015
- var component, res, spineData, container, armature, tran;
8016
- var _this = this;
8017
- return __generator(this, function (_a) {
8018
- switch (_a.label) {
8019
- case 0:
8020
- component = changed.component;
8021
- clearTimeout(component.addHandler);
8022
- return [4, eva_js.resource.getResource(component.resource)];
8023
- case 1:
8024
- res = _a.sent();
8025
- return [4, getSpineData(res)];
8026
- case 2:
8027
- spineData = _a.sent();
8028
- if (!spineData) {
8029
- component.addHandler = setTimeout(function () {
8030
- if (!component.destroied) {
8031
- if (count === undefined) {
8032
- count = MaxRetryCount;
8033
- }
8034
- count--;
8035
- if (count > 0) {
8036
- _this.add(changed, count);
8037
- }
8038
- else {
8039
- console.log('retry exceed max times', component.resource);
8040
- }
8041
- }
8042
- }, 1000);
8043
- return [2];
8044
- }
8045
- this.remove(changed);
8046
- container = this.renderSystem.containerManager.getContainer(changed.gameObject.id);
8047
- if (!container) {
8048
- return [2];
8049
- }
8050
- component.lastResource = component.resource;
8051
- armature = new pixispine.Spine(spineData);
8052
- this.armatures[changed.gameObject.id] = armature;
8053
- if (changed.gameObject && changed.gameObject.transform) {
8054
- tran = changed.gameObject.transform;
8055
- armature.x = tran.size.width * tran.origin.x;
8056
- armature.y = tran.size.height * tran.origin.y;
8057
- }
8058
- container.addChildAt(armature, 0);
8059
- armature.update();
8060
- armature.updateTransform();
8061
- component.armature = armature;
8062
- if (component.autoPlay) {
8063
- try {
8064
- armature.state.setAnimation(0, component.animationName, true);
8065
- }
8066
- catch (e) {
8067
- console.log(e);
8068
- }
8069
- }
8070
- component.emit('loaded', { resource: component.resource });
8071
- armature.state.addListener({
8072
- start: function (track, event) {
8073
- component.emit('start', { track: track, name: track.animation.name });
8074
- },
8075
- complete: function (track, event) {
8076
- component.emit('complete', { track: track, name: track.animation.name });
8077
- },
8078
- interrupt: function (track, event) {
8079
- component.emit('interrupt', { track: track, name: track.animation.name });
8080
- },
8081
- end: function (track, event) {
8082
- component.emit('end', { track: track, name: track.animation.name });
8083
- },
8084
- event: function (track, event) {
8085
- component.emit('event', track, event);
8086
- },
8087
- });
8088
- return [2];
8089
- }
8090
- });
8091
- });
8092
- };
8093
- SpineSystem.prototype.change = function (changed) {
8094
- this.remove(changed);
8095
- this.add(changed);
8096
- };
8097
- SpineSystem.prototype.remove = function (changed) {
8098
- var _a, _b;
8099
- return __awaiter(this, void 0, void 0, function () {
8100
- var component, armature, container, res;
8101
- return __generator(this, function (_c) {
8102
- switch (_c.label) {
8103
- case 0:
8104
- component = changed.component;
8105
- clearTimeout(component.addHandler);
8106
- armature = this.armatures[changed.gameObject.id];
8107
- container = this.renderSystem.containerManager.getContainer(changed.gameObject.id);
8108
- if (container && armature) {
8109
- container.removeChild(armature);
8110
- }
8111
- if (!component.armature) return [3, 2];
8112
- component.armature.destroy({ children: true });
8113
- return [4, eva_js.resource.getResource(component.lastResource)];
8114
- case 1:
8115
- res = _c.sent();
8116
- releaseSpineData(res.name, (_b = (_a = res.data) === null || _a === void 0 ? void 0 : _a.image) === null || _b === void 0 ? void 0 : _b.src);
8117
- _c.label = 2;
8118
- case 2:
8119
- component.armature = null;
8120
- delete this.armatures[changed.gameObject.id];
8121
- if (changed.type === eva_js.OBSERVER_TYPE.CHANGE) ;
8122
- return [2];
8123
- }
8124
- });
8125
- });
8138
+ _super.prototype.init.call(this, { pixiSpine: pixiSpine });
8126
8139
  };
8127
- SpineSystem.systemName = 'SpineSystem';
8128
- SpineSystem = __decorate([
8129
- eva_js.decorators.componentObserver({
8130
- Spine: ['resource'],
8131
- })
8132
- ], SpineSystem);
8133
8140
  return SpineSystem;
8134
- }(pluginRenderer.Renderer));
8141
+ }(SpineSystemBase));
8135
8142
  var SpineSystem$1 = SpineSystem;
8136
8143
 
8144
+ var Spine = (function (_super) {
8145
+ __extends$1(Spine, _super);
8146
+ function Spine() {
8147
+ return _super !== null && _super.apply(this, arguments) || this;
8148
+ }
8149
+ return Spine;
8150
+ }(SpineBase));
8151
+ var Spine$1 = Spine;
8152
+
8137
8153
  exports.Spine = Spine$1;
8138
8154
  exports.SpineSystem = SpineSystem$1;