@eva/plugin-renderer-spine 1.2.2-alpha.0 → 1.2.2-alpha.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,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.
@@ -78,24 +78,69 @@ function __generator(thisArg, body) {
78
78
  } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
79
79
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
80
80
  }
81
+ }
82
+
83
+ function __values(o) {
84
+ var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
85
+ if (m) return m.call(o);
86
+ return {
87
+ next: function () {
88
+ if (o && i >= o.length) o = void 0;
89
+ return { value: o && o[i++], done: !o };
90
+ }
91
+ };
81
92
  }
82
93
 
83
- var Spine = (function (_super) {
94
+ var Spine$2 = (function (_super) {
84
95
  __extends$1(Spine, _super);
85
96
  function Spine() {
86
97
  var _this = _super !== null && _super.apply(this, arguments) || this;
87
98
  _this.resource = '';
88
99
  _this.animationName = '';
89
100
  _this.autoPlay = true;
101
+ _this.waitExecuteInfos = [];
90
102
  return _this;
91
103
  }
104
+ Object.defineProperty(Spine.prototype, "armature", {
105
+ get: function () {
106
+ return this._armature;
107
+ },
108
+ set: function (val) {
109
+ var e_1, _a;
110
+ this._armature = val;
111
+ if (!val)
112
+ return;
113
+ if (this.autoPlay) {
114
+ this.play(this.animationName);
115
+ }
116
+ try {
117
+ for (var _b = __values(this.waitExecuteInfos), _c = _b.next(); !_c.done; _c = _b.next()) {
118
+ var info = _c.value;
119
+ if (info.playType) {
120
+ var name_1 = info.name, loop = info.loop, track = info.track;
121
+ this.play(name_1, loop, track);
122
+ }
123
+ else {
124
+ this.stop(info.track);
125
+ }
126
+ }
127
+ }
128
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
129
+ finally {
130
+ try {
131
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
132
+ }
133
+ finally { if (e_1) throw e_1.error; }
134
+ }
135
+ this.waitExecuteInfos = [];
136
+ },
137
+ enumerable: false,
138
+ configurable: true
139
+ });
92
140
  Spine.prototype.init = function (obj) {
93
141
  if (!obj)
94
142
  return;
95
143
  Object.assign(this, obj);
96
- if (this.autoPlay) {
97
- this.play(this.animationName);
98
- }
99
144
  };
100
145
  Spine.prototype.onDestroy = function () {
101
146
  this.destroied = true;
@@ -105,6 +150,12 @@ var Spine = (function (_super) {
105
150
  if (name)
106
151
  this.animationName = name;
107
152
  if (!this.armature) {
153
+ this.waitExecuteInfos.push({
154
+ playType: true,
155
+ name: name,
156
+ loop: loop,
157
+ track: track
158
+ });
108
159
  }
109
160
  else {
110
161
  if (track === undefined) {
@@ -119,6 +170,10 @@ var Spine = (function (_super) {
119
170
  };
120
171
  Spine.prototype.stop = function (track) {
121
172
  if (!this.armature) {
173
+ this.waitExecuteInfos.push({
174
+ playType: false,
175
+ track: track
176
+ });
122
177
  return;
123
178
  }
124
179
  if (track === undefined) {
@@ -190,206 +245,495 @@ var Spine = (function (_super) {
190
245
  ], Spine.prototype, "autoPlay", void 0);
191
246
  return Spine;
192
247
  }(eva_js.Component));
193
- var Spine$1 = Spine;
248
+ var SpineBase = Spine$2;
194
249
 
195
- const VERSION = '4.8.9';
196
- const loader = pixi_js.loaders.shared;
250
+ var texCache = {};
251
+ function cacheImage(data) {
252
+ var oldImg = data.image;
253
+ return {
254
+ tex: pixi_js.Texture.from(oldImg),
255
+ count: 0,
256
+ };
257
+ }
258
+ function retainTexture(name, data) {
259
+ var cache = texCache[name];
260
+ if (!cache) {
261
+ cache = cacheImage(data);
262
+ texCache[name] = cache;
263
+ }
264
+ cache.count++;
265
+ return cache.tex;
266
+ }
267
+ function getTexture(imageSrc, data) {
268
+ var cache = texCache[imageSrc];
269
+ if (!cache) {
270
+ cache = cacheImage(data);
271
+ texCache[imageSrc] = cache;
272
+ }
273
+ return cache.tex;
274
+ }
275
+ function releaseTexture(imageSrc) {
276
+ if (!imageSrc)
277
+ return;
278
+ setTimeout(function () {
279
+ var cache = texCache[imageSrc];
280
+ if (cache) {
281
+ cache.count--;
282
+ if (cache.count <= 0) {
283
+ if (cache.tex) {
284
+ cache.tex.destroy(true);
285
+ cache.tex = null;
286
+ }
287
+ delete texCache[imageSrc];
288
+ }
289
+ }
290
+ }, 100);
291
+ }
197
292
 
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);
293
+ var dataMap = {};
294
+ function createSpineData(name, data, scale, pixiSpine) {
295
+ var spineData = null;
296
+ var img = getTexture(data.image.src, data);
297
+ new pixiSpine.core.TextureAtlas(data.atlas, function (line, callback) {
298
+ callback(img.baseTexture);
299
+ }, function (spineAtlas) {
300
+ if (spineAtlas) {
301
+ var attachmentLoader = new pixiSpine.core.AtlasAttachmentLoader(spineAtlas);
302
+ var spineJsonParser = new pixiSpine.core.SkeletonJson(attachmentLoader);
303
+ if (scale) {
304
+ spineJsonParser.scale = scale;
305
+ }
306
+ spineData = spineJsonParser.readSkeletonData(data.ske);
307
+ }
308
+ });
309
+ var obj = { spineData: spineData, ref: 0, imageSrc: data.image.src };
310
+ dataMap[name] = obj;
311
+ return obj;
312
+ }
313
+ function getSpineData(res, pixiSpine) {
314
+ return __awaiter(this, void 0, void 0, function () {
315
+ var data;
316
+ return __generator(this, function (_a) {
317
+ data = dataMap[res.name];
318
+ if (!data) {
319
+ if (res.complete) {
320
+ data = createSpineData(res.name, res.data, res.scale, pixiSpine);
321
+ }
322
+ else if (!data) {
323
+ return [2];
324
+ }
325
+ }
326
+ retainTexture(res.data.image.src, res.data);
327
+ data.ref++;
328
+ return [2, data.spineData];
329
+ });
330
+ });
331
+ }
332
+ function releaseSpineData(resourceName, imageSrc) {
333
+ var data = dataMap[resourceName];
334
+ if (!data) {
335
+ return;
336
+ }
337
+ data.ref--;
338
+ if (data.ref <= 0) {
339
+ releaseTexture(imageSrc);
340
+ delete dataMap[resourceName];
341
+ }
342
+ }
343
+
344
+ var MaxRetryCount = 20;
345
+ var SpineSystem$2 = (function (_super) {
346
+ __extends$1(SpineSystem, _super);
347
+ function SpineSystem() {
348
+ var _this = _super !== null && _super.apply(this, arguments) || this;
349
+ _this.armatures = {};
350
+ return _this;
351
+ }
352
+ SpineSystem.prototype.init = function (_a) {
353
+ var _this = this;
354
+ var pixiSpine = _a.pixiSpine;
355
+ this.renderSystem = this.game.getSystem(pluginRenderer.RendererSystem);
356
+ this.renderSystem.rendererManager.register(this);
357
+ this.pixiSpine = pixiSpine;
358
+ this.game.canvas.addEventListener('webglcontextrestored', function () {
359
+ var objs = _this.game.gameObjects;
360
+ var toAdd = [];
361
+ for (var k in _this.armatures) {
362
+ var id = +k;
363
+ for (var i = 0; i < objs.length; ++i) {
364
+ var obj = objs[i];
365
+ if (obj.id === id) {
366
+ var sp = obj.getComponent(SpineBase);
367
+ if (sp) {
368
+ _this.remove({
369
+ type: eva_js.OBSERVER_TYPE.REMOVE,
370
+ gameObject: obj,
371
+ component: sp,
372
+ componentName: SpineBase.componentName,
373
+ });
374
+ toAdd.push({
375
+ type: eva_js.OBSERVER_TYPE.ADD,
376
+ gameObject: obj,
377
+ component: sp,
378
+ componentName: SpineBase.componentName,
379
+ });
380
+ }
381
+ break;
382
+ }
383
+ }
384
+ }
385
+ setTimeout(function () {
386
+ toAdd.forEach(function (obj) {
387
+ _this.add(obj);
388
+ });
389
+ }, 1000);
390
+ }, false);
391
+ };
392
+ SpineSystem.prototype.update = function (e) {
393
+ for (var key in this.armatures) {
394
+ this.armatures[key].update(e.deltaTime * 0.001);
395
+ this.armatures[key].updateTransform();
396
+ }
397
+ _super.prototype.update.call(this);
398
+ };
399
+ SpineSystem.prototype.componentChanged = function (changed) {
400
+ return __awaiter(this, void 0, void 0, function () {
401
+ return __generator(this, function (_a) {
402
+ if (changed.componentName === 'Spine') {
403
+ if (changed.type === eva_js.OBSERVER_TYPE.ADD) {
404
+ this.add(changed);
405
+ }
406
+ else if (changed.type === eva_js.OBSERVER_TYPE.CHANGE) {
407
+ switch (changed.prop.prop[0]) {
408
+ case 'resource':
409
+ this.change(changed);
410
+ break;
411
+ }
412
+ }
413
+ else if (changed.type === eva_js.OBSERVER_TYPE.REMOVE) {
414
+ this.remove(changed);
415
+ }
416
+ }
417
+ return [2];
418
+ });
419
+ });
420
+ };
421
+ SpineSystem.prototype.add = function (changed, count) {
422
+ return __awaiter(this, void 0, void 0, function () {
423
+ var component, res, spineData, container, armature, tran;
424
+ var _this = this;
425
+ return __generator(this, function (_a) {
426
+ switch (_a.label) {
427
+ case 0:
428
+ component = changed.component;
429
+ clearTimeout(component.addHandler);
430
+ return [4, eva_js.resource.getResource(component.resource)];
431
+ case 1:
432
+ res = _a.sent();
433
+ return [4, getSpineData(res, this.pixiSpine)];
434
+ case 2:
435
+ spineData = _a.sent();
436
+ if (!spineData) {
437
+ component.addHandler = setTimeout(function () {
438
+ if (!component.destroied) {
439
+ if (count === undefined) {
440
+ count = MaxRetryCount;
441
+ }
442
+ count--;
443
+ if (count > 0) {
444
+ _this.add(changed, count);
445
+ }
446
+ else {
447
+ console.log('retry exceed max times', component.resource);
448
+ }
449
+ }
450
+ }, 1000);
451
+ return [2];
452
+ }
453
+ this.remove(changed);
454
+ container = this.renderSystem.containerManager.getContainer(changed.gameObject.id);
455
+ if (!container) {
456
+ return [2];
457
+ }
458
+ component.lastResource = component.resource;
459
+ armature = new this.pixiSpine.Spine(spineData);
460
+ this.armatures[changed.gameObject.id] = armature;
461
+ if (changed.gameObject && changed.gameObject.transform) {
462
+ tran = changed.gameObject.transform;
463
+ armature.x = tran.size.width * tran.origin.x;
464
+ armature.y = tran.size.height * tran.origin.y;
465
+ }
466
+ container.addChildAt(armature, 0);
467
+ armature.update();
468
+ armature.updateTransform();
469
+ component.armature = armature;
470
+ component.emit('loaded', { resource: component.resource });
471
+ armature.state.addListener({
472
+ start: function (track, event) {
473
+ component.emit('start', { track: track, name: track.animation.name });
474
+ },
475
+ complete: function (track, event) {
476
+ component.emit('complete', { track: track, name: track.animation.name });
477
+ },
478
+ interrupt: function (track, event) {
479
+ component.emit('interrupt', { track: track, name: track.animation.name });
480
+ },
481
+ end: function (track, event) {
482
+ component.emit('end', { track: track, name: track.animation.name });
483
+ },
484
+ event: function (track, event) {
485
+ component.emit('event', track, event);
486
+ },
487
+ });
488
+ return [2];
489
+ }
490
+ });
491
+ });
492
+ };
493
+ SpineSystem.prototype.change = function (changed) {
494
+ this.remove(changed);
495
+ this.add(changed);
496
+ };
497
+ SpineSystem.prototype.remove = function (changed) {
498
+ var _a, _b;
499
+ return __awaiter(this, void 0, void 0, function () {
500
+ var component, armature, container, res;
501
+ return __generator(this, function (_c) {
502
+ switch (_c.label) {
503
+ case 0:
504
+ component = changed.component;
505
+ clearTimeout(component.addHandler);
506
+ armature = this.armatures[changed.gameObject.id];
507
+ container = this.renderSystem.containerManager.getContainer(changed.gameObject.id);
508
+ if (container && armature) {
509
+ container.removeChild(armature);
510
+ }
511
+ if (!component.armature) return [3, 2];
512
+ component.armature.destroy({ children: true });
513
+ return [4, eva_js.resource.getResource(component.lastResource)];
514
+ case 1:
515
+ res = _c.sent();
516
+ releaseSpineData(res.name, (_b = (_a = res.data) === null || _a === void 0 ? void 0 : _a.image) === null || _b === void 0 ? void 0 : _b.src);
517
+ _c.label = 2;
518
+ case 2:
519
+ component.armature = null;
520
+ delete this.armatures[changed.gameObject.id];
521
+ if (changed.type === eva_js.OBSERVER_TYPE.CHANGE) ;
522
+ return [2];
523
+ }
524
+ });
525
+ });
526
+ };
527
+ SpineSystem.systemName = 'SpineSystem';
528
+ SpineSystem = __decorate([
529
+ eva_js.decorators.componentObserver({
530
+ Spine: ['resource'],
531
+ })
532
+ ], SpineSystem);
533
+ return SpineSystem;
534
+ }(pluginRenderer.Renderer));
535
+ var SpineSystemBase = SpineSystem$2;
536
+
537
+ // https://github.com/pixijs/spine/tree/pixi4
538
+
539
+ var VERSION = '4.8.9';
540
+ var loader = pixi_js.loaders.shared;
541
+
542
+ var PIXI = {
543
+ Texture: pixi_js.Texture,
544
+ Rectangle: pixi_js.Rectangle,
545
+ Sprite: pixi_js.Sprite,
546
+ Graphics: pixi_js.Graphics,
547
+ mesh: pixi_js.mesh,
548
+ Matrix: pixi_js.Matrix,
549
+ Container: pixi_js.Container,
550
+ VERSION,
551
+ loaders: pixi_js.loaders,
552
+ loader,
553
+ SCALE_MODES: pixi_js.SCALE_MODES,
554
+ utils: pixi_js.utils,
555
+ TransformBase: pixi_js.TransformBase,
556
+ TransformStatic: pixi_js.TransformStatic,
557
+ DisplayObject: pixi_js.DisplayObject,
558
+ Polygon: pixi_js.Polygon,
559
+ BaseTexture: pixi_js.BaseTexture,
560
+ };
561
+ var __extends =
562
+ (undefined && undefined.__extends) ||
563
+ (function () {
564
+ var extendStatics =
565
+ Object.setPrototypeOf ||
566
+ ({ __proto__: [] } instanceof Array &&
567
+ function (d, b) {
568
+ d.__proto__ = b;
569
+ }) ||
570
+ function (d, b) {
571
+ for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
572
+ };
573
+ return function (d, b) {
574
+ extendStatics(d, b);
575
+ function __() {
576
+ this.constructor = d;
577
+ }
578
+ d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
579
+ };
580
+ })();
581
+ var pixi_spine;
582
+ (function (pixi_spine) {
583
+ (function (core) {
584
+ var Animation = (function () {
585
+ function Animation(name, timelines, duration) {
586
+ if (name == null) throw new Error('name cannot be null.');
587
+ if (timelines == null) throw new Error('timelines cannot be null.');
588
+ this.name = name;
589
+ this.timelines = timelines;
590
+ this.duration = duration;
591
+ }
592
+ Animation.prototype.apply = function (skeleton, lastTime, time, loop, events, alpha, blend, direction) {
593
+ if (skeleton == null) throw new Error('skeleton cannot be null.');
594
+ if (loop && this.duration != 0) {
595
+ time %= this.duration;
596
+ if (lastTime > 0) lastTime %= this.duration;
597
+ }
598
+ var timelines = this.timelines;
599
+ for (var i = 0, n = timelines.length; i < n; i++)
600
+ timelines[i].apply(skeleton, lastTime, time, events, alpha, blend, direction);
601
+ };
602
+ Animation.binarySearch = function (values, target, step) {
603
+ if (step === void 0) {
604
+ step = 1;
605
+ }
606
+ var low = 0;
607
+ var high = values.length / step - 2;
608
+ if (high == 0) return step;
609
+ var current = high >>> 1;
610
+ while (true) {
611
+ if (values[(current + 1) * step] <= target) low = current + 1;
612
+ else high = current;
613
+ if (low == high) return (low + 1) * step;
614
+ current = (low + high) >>> 1;
615
+ }
616
+ };
617
+ Animation.linearSearch = function (values, target, step) {
618
+ for (var i = 0, last = values.length - step; i <= last; i += step) if (values[i] > target) return i;
619
+ return -1;
620
+ };
621
+ return Animation;
622
+ })();
623
+ core.Animation = Animation;
624
+ var MixBlend;
625
+ (function (MixBlend) {
626
+ MixBlend[(MixBlend['setup'] = 0)] = 'setup';
627
+ MixBlend[(MixBlend['first'] = 1)] = 'first';
628
+ MixBlend[(MixBlend['replace'] = 2)] = 'replace';
629
+ MixBlend[(MixBlend['add'] = 3)] = 'add';
630
+ })((MixBlend = core.MixBlend || (core.MixBlend = {})));
631
+ var MixDirection;
632
+ (function (MixDirection) {
633
+ MixDirection[(MixDirection['in'] = 0)] = 'in';
634
+ MixDirection[(MixDirection['out'] = 1)] = 'out';
635
+ })((MixDirection = core.MixDirection || (core.MixDirection = {})));
636
+ var TimelineType;
637
+ (function (TimelineType) {
638
+ TimelineType[(TimelineType['rotate'] = 0)] = 'rotate';
639
+ TimelineType[(TimelineType['translate'] = 1)] = 'translate';
640
+ TimelineType[(TimelineType['scale'] = 2)] = 'scale';
641
+ TimelineType[(TimelineType['shear'] = 3)] = 'shear';
642
+ TimelineType[(TimelineType['attachment'] = 4)] = 'attachment';
643
+ TimelineType[(TimelineType['color'] = 5)] = 'color';
644
+ TimelineType[(TimelineType['deform'] = 6)] = 'deform';
645
+ TimelineType[(TimelineType['event'] = 7)] = 'event';
646
+ TimelineType[(TimelineType['drawOrder'] = 8)] = 'drawOrder';
647
+ TimelineType[(TimelineType['ikConstraint'] = 9)] = 'ikConstraint';
648
+ TimelineType[(TimelineType['transformConstraint'] = 10)] = 'transformConstraint';
649
+ TimelineType[(TimelineType['pathConstraintPosition'] = 11)] = 'pathConstraintPosition';
650
+ TimelineType[(TimelineType['pathConstraintSpacing'] = 12)] = 'pathConstraintSpacing';
651
+ TimelineType[(TimelineType['pathConstraintMix'] = 13)] = 'pathConstraintMix';
652
+ TimelineType[(TimelineType['twoColor'] = 14)] = 'twoColor';
653
+ })((TimelineType = core.TimelineType || (core.TimelineType = {})));
654
+ var CurveTimeline = (function () {
655
+ function CurveTimeline(frameCount) {
656
+ if (frameCount <= 0) throw new Error('frameCount must be > 0: ' + frameCount);
657
+ this.curves = core.Utils.newFloatArray((frameCount - 1) * CurveTimeline.BEZIER_SIZE);
658
+ }
659
+ CurveTimeline.prototype.getFrameCount = function () {
660
+ return this.curves.length / CurveTimeline.BEZIER_SIZE + 1;
661
+ };
662
+ CurveTimeline.prototype.setLinear = function (frameIndex) {
663
+ this.curves[frameIndex * CurveTimeline.BEZIER_SIZE] = CurveTimeline.LINEAR;
664
+ };
665
+ CurveTimeline.prototype.setStepped = function (frameIndex) {
666
+ this.curves[frameIndex * CurveTimeline.BEZIER_SIZE] = CurveTimeline.STEPPED;
667
+ };
668
+ CurveTimeline.prototype.getCurveType = function (frameIndex) {
669
+ var index = frameIndex * CurveTimeline.BEZIER_SIZE;
670
+ if (index == this.curves.length) return CurveTimeline.LINEAR;
671
+ var type = this.curves[index];
672
+ if (type == CurveTimeline.LINEAR) return CurveTimeline.LINEAR;
673
+ if (type == CurveTimeline.STEPPED) return CurveTimeline.STEPPED;
674
+ return CurveTimeline.BEZIER;
675
+ };
676
+ CurveTimeline.prototype.setCurve = function (frameIndex, cx1, cy1, cx2, cy2) {
677
+ var tmpx = (-cx1 * 2 + cx2) * 0.03,
678
+ tmpy = (-cy1 * 2 + cy2) * 0.03;
679
+ var dddfx = ((cx1 - cx2) * 3 + 1) * 0.006,
680
+ dddfy = ((cy1 - cy2) * 3 + 1) * 0.006;
681
+ var ddfx = tmpx * 2 + dddfx,
682
+ ddfy = tmpy * 2 + dddfy;
683
+ var dfx = cx1 * 0.3 + tmpx + dddfx * 0.16666667,
684
+ dfy = cy1 * 0.3 + tmpy + dddfy * 0.16666667;
685
+ var i = frameIndex * CurveTimeline.BEZIER_SIZE;
686
+ var curves = this.curves;
687
+ curves[i++] = CurveTimeline.BEZIER;
688
+ var x = dfx,
689
+ y = dfy;
690
+ for (var n = i + CurveTimeline.BEZIER_SIZE - 1; i < n; i += 2) {
691
+ curves[i] = x;
692
+ curves[i + 1] = y;
693
+ dfx += ddfx;
694
+ dfy += ddfy;
695
+ ddfx += dddfx;
696
+ ddfy += dddfy;
697
+ x += dfx;
698
+ y += dfy;
699
+ }
700
+ };
701
+ CurveTimeline.prototype.getCurvePercent = function (frameIndex, percent) {
702
+ percent = core.MathUtils.clamp(percent, 0, 1);
703
+ var curves = this.curves;
704
+ var i = frameIndex * CurveTimeline.BEZIER_SIZE;
705
+ var type = curves[i];
706
+ if (type == CurveTimeline.LINEAR) return percent;
707
+ if (type == CurveTimeline.STEPPED) return 0;
708
+ i++;
709
+ var x = 0;
710
+ for (var start = i, n = i + CurveTimeline.BEZIER_SIZE - 1; i < n; i += 2) {
711
+ x = curves[i];
712
+ if (x >= percent) {
713
+ var prevX = void 0,
714
+ prevY = void 0;
715
+ if (i == start) {
716
+ prevX = 0;
717
+ prevY = 0;
718
+ } else {
719
+ prevX = curves[i - 2];
720
+ prevY = curves[i - 1];
721
+ }
722
+ return prevY + ((curves[i + 1] - prevY) * (percent - prevX)) / (x - prevX);
723
+ }
724
+ }
725
+ var y = curves[i - 1];
726
+ return y + ((1 - y) * (percent - x)) / (1 - x);
727
+ };
728
+ CurveTimeline.LINEAR = 0;
729
+ CurveTimeline.STEPPED = 1;
730
+ CurveTimeline.BEZIER = 2;
731
+ CurveTimeline.BEZIER_SIZE = 10 * 2 - 1;
732
+ return CurveTimeline;
733
+ })();
734
+ core.CurveTimeline = CurveTimeline;
735
+ var RotateTimeline = (function (_super) {
736
+ __extends(RotateTimeline, _super);
393
737
  function RotateTimeline(frameCount) {
394
738
  var _this = _super.call(this, frameCount) || this;
395
739
  _this.frames = core.Utils.newFloatArray(frameCount << 1);
@@ -7803,336 +8147,55 @@ var pixi_spine;
7803
8147
  callback(null);
7804
8148
  }
7805
8149
  });
7806
- }
7807
- };
7808
- }
7809
- pixi_spine.imageLoaderAdapter = imageLoaderAdapter;
7810
- function syncImageLoaderAdapter(baseUrl, crossOrigin) {
7811
- if (baseUrl && baseUrl.lastIndexOf('/') !== baseUrl.length - 1) {
7812
- 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
- }
8150
+ }
8151
+ };
8152
+ }
8153
+ pixi_spine.imageLoaderAdapter = imageLoaderAdapter;
8154
+ function syncImageLoaderAdapter(baseUrl, crossOrigin) {
8155
+ if (baseUrl && baseUrl.lastIndexOf('/') !== baseUrl.length - 1) {
8156
+ baseUrl += '/';
8157
+ }
8158
+ return function (line, callback) {
8159
+ callback(PIXI.BaseTexture.fromImage(line, crossOrigin));
8160
+ };
8161
+ }
8162
+ pixi_spine.syncImageLoaderAdapter = syncImageLoaderAdapter;
8163
+ function staticImageLoader(pages) {
8164
+ return function (line, callback) {
8165
+ var page = pages[line] || pages['default'];
8166
+ if (page && page.baseTexture) callback(page.baseTexture);
8167
+ else callback(page);
8168
+ };
8169
+ }
8170
+ pixi_spine.staticImageLoader = staticImageLoader;
8171
+ if (PIXI.loaders.Loader) {
8172
+ PIXI.loaders.Loader.addPixiMiddleware(atlasParser);
8173
+ PIXI.loader.use(atlasParser());
8174
+ }
8175
+ })(pixi_spine || (pixi_spine = {}));
7877
8176
 
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
- }
8177
+ var pixiSpine = pixi_spine;
7937
8178
 
7938
- var MaxRetryCount = 20;
7939
8179
  var SpineSystem = (function (_super) {
7940
8180
  __extends$1(SpineSystem, _super);
7941
8181
  function SpineSystem() {
7942
- var _this = _super !== null && _super.apply(this, arguments) || this;
7943
- _this.armatures = {};
7944
- return _this;
8182
+ return _super !== null && _super.apply(this, arguments) || this;
7945
8183
  }
7946
8184
  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
- });
8185
+ _super.prototype.init.call(this, { pixiSpine: pixiSpine });
8126
8186
  };
8127
- SpineSystem.systemName = 'SpineSystem';
8128
- SpineSystem = __decorate([
8129
- eva_js.decorators.componentObserver({
8130
- Spine: ['resource'],
8131
- })
8132
- ], SpineSystem);
8133
8187
  return SpineSystem;
8134
- }(pluginRenderer.Renderer));
8188
+ }(SpineSystemBase));
8135
8189
  var SpineSystem$1 = SpineSystem;
8136
8190
 
8191
+ var Spine = (function (_super) {
8192
+ __extends$1(Spine, _super);
8193
+ function Spine() {
8194
+ return _super !== null && _super.apply(this, arguments) || this;
8195
+ }
8196
+ return Spine;
8197
+ }(SpineBase));
8198
+ var Spine$1 = Spine;
8199
+
8137
8200
  exports.Spine = Spine$1;
8138
8201
  exports.SpineSystem = SpineSystem$1;