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