@eva/spine-base 1.2.2-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/dist/EVA.plugin.spineBase.js +705 -0
- package/dist/EVA.plugin.spineBase.min.js +1 -0
- package/dist/global.d.ts +6 -0
- package/dist/miniprogram.js +499 -0
- package/dist/spine-base.cjs.js +493 -0
- package/dist/spine-base.cjs.prod.js +1 -0
- package/dist/spine-base.d.ts +55 -0
- package/dist/spine-base.esm.js +488 -0
- package/index.js +7 -0
- package/package.json +26 -0
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var eva_js = require('@eva/eva.js');
|
|
6
|
+
var pluginRenderer = require('@eva/plugin-renderer');
|
|
7
|
+
var inspectorDecorator = require('@eva/inspector-decorator');
|
|
8
|
+
var pixi_js = require('pixi.js');
|
|
9
|
+
|
|
10
|
+
/*! *****************************************************************************
|
|
11
|
+
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
12
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
13
|
+
this file except in compliance with the License. You may obtain a copy of the
|
|
14
|
+
License at http://www.apache.org/licenses/LICENSE-2.0
|
|
15
|
+
|
|
16
|
+
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
17
|
+
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
|
|
18
|
+
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
|
19
|
+
MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
20
|
+
|
|
21
|
+
See the Apache Version 2.0 License for specific language governing permissions
|
|
22
|
+
and limitations under the License.
|
|
23
|
+
***************************************************************************** */
|
|
24
|
+
/* global Reflect, Promise */
|
|
25
|
+
|
|
26
|
+
var extendStatics = function(d, b) {
|
|
27
|
+
extendStatics = Object.setPrototypeOf ||
|
|
28
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
29
|
+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
30
|
+
return extendStatics(d, b);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
function __extends(d, b) {
|
|
34
|
+
extendStatics(d, b);
|
|
35
|
+
function __() { this.constructor = d; }
|
|
36
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function __decorate(decorators, target, key, desc) {
|
|
40
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
41
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
42
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
43
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
47
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
48
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
49
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
50
|
+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
51
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function __generator(thisArg, body) {
|
|
56
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
57
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
58
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
59
|
+
function step(op) {
|
|
60
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
61
|
+
while (_) try {
|
|
62
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
63
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
64
|
+
switch (op[0]) {
|
|
65
|
+
case 0: case 1: t = op; break;
|
|
66
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
67
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
68
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
69
|
+
default:
|
|
70
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
71
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
72
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
73
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
74
|
+
if (t[2]) _.ops.pop();
|
|
75
|
+
_.trys.pop(); continue;
|
|
76
|
+
}
|
|
77
|
+
op = body.call(thisArg, _);
|
|
78
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
79
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
var Spine = (function (_super) {
|
|
84
|
+
__extends(Spine, _super);
|
|
85
|
+
function Spine() {
|
|
86
|
+
var _this = _super !== null && _super.apply(this, arguments) || this;
|
|
87
|
+
_this.resource = '';
|
|
88
|
+
_this.animationName = '';
|
|
89
|
+
_this.autoPlay = true;
|
|
90
|
+
return _this;
|
|
91
|
+
}
|
|
92
|
+
Spine.prototype.init = function (obj) {
|
|
93
|
+
if (!obj)
|
|
94
|
+
return;
|
|
95
|
+
Object.assign(this, obj);
|
|
96
|
+
if (this.autoPlay) {
|
|
97
|
+
this.play(this.animationName);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
Spine.prototype.onDestroy = function () {
|
|
101
|
+
this.destroied = true;
|
|
102
|
+
};
|
|
103
|
+
Spine.prototype.play = function (name, loop, track) {
|
|
104
|
+
try {
|
|
105
|
+
if (name)
|
|
106
|
+
this.animationName = name;
|
|
107
|
+
if (!this.armature) {
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
if (track === undefined) {
|
|
111
|
+
track = 0;
|
|
112
|
+
}
|
|
113
|
+
this.armature.state.setAnimation(track, this.animationName, loop);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch (e) {
|
|
117
|
+
console.log(e);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
Spine.prototype.stop = function (track) {
|
|
121
|
+
if (!this.armature) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (track === undefined) {
|
|
125
|
+
track = 0;
|
|
126
|
+
}
|
|
127
|
+
this.armature.state.setEmptyAnimation(track, 0);
|
|
128
|
+
};
|
|
129
|
+
Spine.prototype.addAnimation = function (name, delay, loop, track) {
|
|
130
|
+
try {
|
|
131
|
+
if (!this.armature) {
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
if (track === undefined) {
|
|
135
|
+
track = 0;
|
|
136
|
+
}
|
|
137
|
+
this.armature.state.addAnimation(track, name, loop, delay);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
catch (e) {
|
|
141
|
+
console.log(e);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
Spine.prototype.setMix = function (from, to, duration) {
|
|
145
|
+
if (!this.armature) ;
|
|
146
|
+
else {
|
|
147
|
+
this.armature.stateData.setMix(from, to, duration);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
Spine.prototype.getAnim = function (track) {
|
|
151
|
+
if (track === void 0) { track = 0; }
|
|
152
|
+
try {
|
|
153
|
+
if (!this.armature) {
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
return this.armature.state.tracks[track].animation.name;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
catch (e) {
|
|
160
|
+
console.log(e);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
Spine.prototype.setDefaultMix = function (duration) {
|
|
164
|
+
if (!this.armature) ;
|
|
165
|
+
else {
|
|
166
|
+
this.armature.stateData.defaultMix = duration;
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
Spine.prototype.setAttachment = function (slotName, attachmentName) {
|
|
170
|
+
if (!this.armature) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
this.armature.skeleton.setAttachment(slotName, attachmentName);
|
|
174
|
+
};
|
|
175
|
+
Spine.prototype.getBone = function (boneName) {
|
|
176
|
+
if (!this.armature) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
return this.armature.skeleton.findBone(boneName);
|
|
180
|
+
};
|
|
181
|
+
Spine.componentName = 'Spine';
|
|
182
|
+
__decorate([
|
|
183
|
+
inspectorDecorator.type('string')
|
|
184
|
+
], Spine.prototype, "resource", void 0);
|
|
185
|
+
__decorate([
|
|
186
|
+
inspectorDecorator.type('string')
|
|
187
|
+
], Spine.prototype, "animationName", void 0);
|
|
188
|
+
__decorate([
|
|
189
|
+
inspectorDecorator.type('boolean')
|
|
190
|
+
], Spine.prototype, "autoPlay", void 0);
|
|
191
|
+
return Spine;
|
|
192
|
+
}(eva_js.Component));
|
|
193
|
+
var Spine$1 = Spine;
|
|
194
|
+
|
|
195
|
+
var texCache = {};
|
|
196
|
+
function cacheImage(data) {
|
|
197
|
+
var oldImg = data.image;
|
|
198
|
+
return {
|
|
199
|
+
tex: pixi_js.Texture.from(oldImg),
|
|
200
|
+
count: 0,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function retainTexture(name, data) {
|
|
204
|
+
var cache = texCache[name];
|
|
205
|
+
if (!cache) {
|
|
206
|
+
cache = cacheImage(data);
|
|
207
|
+
texCache[name] = cache;
|
|
208
|
+
}
|
|
209
|
+
cache.count++;
|
|
210
|
+
return cache.tex;
|
|
211
|
+
}
|
|
212
|
+
function getTexture(imageSrc, data) {
|
|
213
|
+
var cache = texCache[imageSrc];
|
|
214
|
+
if (!cache) {
|
|
215
|
+
cache = cacheImage(data);
|
|
216
|
+
texCache[imageSrc] = cache;
|
|
217
|
+
}
|
|
218
|
+
return cache.tex;
|
|
219
|
+
}
|
|
220
|
+
function releaseTexture(imageSrc) {
|
|
221
|
+
if (!imageSrc)
|
|
222
|
+
return;
|
|
223
|
+
setTimeout(function () {
|
|
224
|
+
var cache = texCache[imageSrc];
|
|
225
|
+
if (cache) {
|
|
226
|
+
cache.count--;
|
|
227
|
+
if (cache.count <= 0) {
|
|
228
|
+
if (cache.tex) {
|
|
229
|
+
cache.tex.destroy(true);
|
|
230
|
+
cache.tex = null;
|
|
231
|
+
}
|
|
232
|
+
delete texCache[imageSrc];
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}, 100);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
var dataMap = {};
|
|
239
|
+
function createSpineData(name, data, scale, pixiSpine) {
|
|
240
|
+
var spineData = null;
|
|
241
|
+
var img = getTexture(data.image.src, data);
|
|
242
|
+
new pixiSpine.core.TextureAtlas(data.atlas, function (line, callback) {
|
|
243
|
+
callback(img.baseTexture);
|
|
244
|
+
}, function (spineAtlas) {
|
|
245
|
+
if (spineAtlas) {
|
|
246
|
+
var attachmentLoader = new pixiSpine.core.AtlasAttachmentLoader(spineAtlas);
|
|
247
|
+
var spineJsonParser = new pixiSpine.core.SkeletonJson(attachmentLoader);
|
|
248
|
+
if (scale) {
|
|
249
|
+
spineJsonParser.scale = scale;
|
|
250
|
+
}
|
|
251
|
+
spineData = spineJsonParser.readSkeletonData(data.ske);
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
var obj = { spineData: spineData, ref: 0, imageSrc: data.image.src };
|
|
255
|
+
dataMap[name] = obj;
|
|
256
|
+
return obj;
|
|
257
|
+
}
|
|
258
|
+
function getSpineData(res, pixiSpine) {
|
|
259
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
260
|
+
var data;
|
|
261
|
+
return __generator(this, function (_a) {
|
|
262
|
+
data = dataMap[res.name];
|
|
263
|
+
if (!data) {
|
|
264
|
+
if (res.complete) {
|
|
265
|
+
data = createSpineData(res.name, res.data, res.scale, pixiSpine);
|
|
266
|
+
}
|
|
267
|
+
else if (!data) {
|
|
268
|
+
return [2];
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
retainTexture(res.data.image.src, res.data);
|
|
272
|
+
data.ref++;
|
|
273
|
+
return [2, data.spineData];
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
function releaseSpineData(resourceName, imageSrc) {
|
|
278
|
+
var data = dataMap[resourceName];
|
|
279
|
+
if (!data) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
data.ref--;
|
|
283
|
+
if (data.ref <= 0) {
|
|
284
|
+
releaseTexture(imageSrc);
|
|
285
|
+
delete dataMap[resourceName];
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
var MaxRetryCount = 20;
|
|
290
|
+
var SpineSystem = (function (_super) {
|
|
291
|
+
__extends(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(Spine$1);
|
|
312
|
+
if (sp) {
|
|
313
|
+
_this.remove({
|
|
314
|
+
type: eva_js.OBSERVER_TYPE.REMOVE,
|
|
315
|
+
gameObject: obj,
|
|
316
|
+
component: sp,
|
|
317
|
+
componentName: Spine$1.componentName,
|
|
318
|
+
});
|
|
319
|
+
toAdd.push({
|
|
320
|
+
type: eva_js.OBSERVER_TYPE.ADD,
|
|
321
|
+
gameObject: obj,
|
|
322
|
+
component: sp,
|
|
323
|
+
componentName: Spine$1.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 SpineSystem$1 = SpineSystem;
|
|
489
|
+
|
|
490
|
+
eva_js.resource.registerResourceType('SPINE');
|
|
491
|
+
|
|
492
|
+
exports.Spine = Spine$1;
|
|
493
|
+
exports.SpineSystem = SpineSystem$1;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@eva/eva.js"),t=require("@eva/plugin-renderer"),r=require("@eva/inspector-decorator"),n=require("pixi.js"),o=function(e,t){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(e,t)};function a(e,t){function r(){this.constructor=e}o(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}function i(e,t,r,n){var o,a=arguments.length,i=a<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(e,t,r,n);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(i=(a<3?o(i):a>3?o(t,r,i):o(t,r))||i);return a>3&&i&&Object.defineProperty(t,r,i),i}function s(e,t,r,n){return new(r||(r=Promise))((function(o,a){function i(e){try{u(n.next(e))}catch(e){a(e)}}function s(e){try{u(n.throw(e))}catch(e){a(e)}}function u(e){e.done?o(e.value):new r((function(t){t(e.value)})).then(i,s)}u((n=n.apply(e,t||[])).next())}))}function u(e,t){var r,n,o,a,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function s(a){return function(s){return function(a){if(r)throw new TypeError("Generator is already executing.");for(;i;)try{if(r=1,n&&(o=2&a[0]?n.return:a[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,a[1])).done)return o;switch(n=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,n=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==a[0]&&2!==a[0])){i=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]<o[3])){i.label=a[1];break}if(6===a[0]&&i.label<o[1]){i.label=o[1],o=a;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(a);break}o[2]&&i.ops.pop(),i.trys.pop();continue}a=t.call(e,i)}catch(e){a=[6,e],n=0}finally{r=o=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,s])}}}var c=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.resource="",t.animationName="",t.autoPlay=!0,t}return a(t,e),t.prototype.init=function(e){e&&(Object.assign(this,e),this.autoPlay&&this.play(this.animationName))},t.prototype.onDestroy=function(){this.destroied=!0},t.prototype.play=function(e,t,r){try{e&&(this.animationName=e),this.armature&&(void 0===r&&(r=0),this.armature.state.setAnimation(r,this.animationName,t))}catch(e){console.log(e)}},t.prototype.stop=function(e){this.armature&&(void 0===e&&(e=0),this.armature.state.setEmptyAnimation(e,0))},t.prototype.addAnimation=function(e,t,r,n){try{this.armature&&(void 0===n&&(n=0),this.armature.state.addAnimation(n,e,r,t))}catch(e){console.log(e)}},t.prototype.setMix=function(e,t,r){this.armature&&this.armature.stateData.setMix(e,t,r)},t.prototype.getAnim=function(e){void 0===e&&(e=0);try{if(this.armature)return this.armature.state.tracks[e].animation.name}catch(e){console.log(e)}},t.prototype.setDefaultMix=function(e){this.armature&&(this.armature.stateData.defaultMix=e)},t.prototype.setAttachment=function(e,t){this.armature&&this.armature.skeleton.setAttachment(e,t)},t.prototype.getBone=function(e){if(this.armature)return this.armature.skeleton.findBone(e)},t.componentName="Spine",i([r.type("string")],t.prototype,"resource",void 0),i([r.type("string")],t.prototype,"animationName",void 0),i([r.type("boolean")],t.prototype,"autoPlay",void 0),t}(e.Component),m={};function p(e){var t=e.image;return{tex:n.Texture.from(t),count:0}}var l={};function f(e,t,r,n){var o=null,a=function(e,t){var r=m[e];return r||(r=p(t),m[e]=r),r.tex}(t.image.src,t);new n.core.TextureAtlas(t.atlas,(function(e,t){t(a.baseTexture)}),(function(e){if(e){var a=new n.core.AtlasAttachmentLoader(e),i=new n.core.SkeletonJson(a);r&&(i.scale=r),o=i.readSkeletonData(t.ske)}}));var i={spineData:o,ref:0,imageSrc:t.image.src};return l[e]=i,i}function d(e,t){return s(this,void 0,void 0,(function(){var r;return u(this,(function(n){if(!(r=l[e.name]))if(e.complete)r=f(e.name,e.data,e.scale,t);else if(!r)return[2];return function(e,t){var r=m[e];r||(r=p(t),m[e]=r),r.count++,r.tex}(e.data.image.src,e.data),r.ref++,[2,r.spineData]}))}))}function h(e,t){var r=l[e];r&&(r.ref--,r.ref<=0&&(!function(e){e&&setTimeout((function(){var t=m[e];t&&(t.count--,t.count<=0&&(t.tex&&(t.tex.destroy(!0),t.tex=null),delete m[e]))}),100)}(t),delete l[e]))}var y=function(r){function n(){var e=null!==r&&r.apply(this,arguments)||this;return e.armatures={},e}return a(n,r),n.prototype.init=function(r){var n=this,o=r.pixiSpine;this.renderSystem=this.game.getSystem(t.RendererSystem),this.renderSystem.rendererManager.register(this),this.pixiSpine=o,this.game.canvas.addEventListener("webglcontextrestored",(function(){var t=n.game.gameObjects,r=[];for(var o in n.armatures)for(var a=+o,i=0;i<t.length;++i){var s=t[i];if(s.id===a){var u=s.getComponent(c);u&&(n.remove({type:e.OBSERVER_TYPE.REMOVE,gameObject:s,component:u,componentName:c.componentName}),r.push({type:e.OBSERVER_TYPE.ADD,gameObject:s,component:u,componentName:c.componentName}));break}}setTimeout((function(){r.forEach((function(e){n.add(e)}))}),1e3)}),!1)},n.prototype.update=function(e){for(var t in this.armatures)this.armatures[t].update(.001*e.deltaTime),this.armatures[t].updateTransform();r.prototype.update.call(this)},n.prototype.componentChanged=function(t){return s(this,void 0,void 0,(function(){return u(this,(function(r){if("Spine"===t.componentName)if(t.type===e.OBSERVER_TYPE.ADD)this.add(t);else if(t.type===e.OBSERVER_TYPE.CHANGE)switch(t.prop.prop[0]){case"resource":this.change(t)}else t.type===e.OBSERVER_TYPE.REMOVE&&this.remove(t);return[2]}))}))},n.prototype.add=function(t,r){return s(this,void 0,void 0,(function(){var n,o,a,i,s,c=this;return u(this,(function(u){switch(u.label){case 0:return n=t.component,clearTimeout(n.addHandler),[4,e.resource.getResource(n.resource)];case 1:return[4,d(u.sent(),this.pixiSpine)];case 2:if(!(o=u.sent()))return n.addHandler=setTimeout((function(){n.destroied||(void 0===r&&(r=20),--r>0?c.add(t,r):console.log("retry exceed max times",n.resource))}),1e3),[2];if(this.remove(t),!(a=this.renderSystem.containerManager.getContainer(t.gameObject.id)))return[2];if(n.lastResource=n.resource,i=new this.pixiSpine.Spine(o),this.armatures[t.gameObject.id]=i,t.gameObject&&t.gameObject.transform&&(s=t.gameObject.transform,i.x=s.size.width*s.origin.x,i.y=s.size.height*s.origin.y),a.addChildAt(i,0),i.update(),i.updateTransform(),n.armature=i,n.autoPlay)try{i.state.setAnimation(0,n.animationName,!0)}catch(e){console.log(e)}return n.emit("loaded",{resource:n.resource}),i.state.addListener({start:function(e,t){n.emit("start",{track:e,name:e.animation.name})},complete:function(e,t){n.emit("complete",{track:e,name:e.animation.name})},interrupt:function(e,t){n.emit("interrupt",{track:e,name:e.animation.name})},end:function(e,t){n.emit("end",{track:e,name:e.animation.name})},event:function(e,t){n.emit("event",e,t)}}),[2]}}))}))},n.prototype.change=function(e){this.remove(e),this.add(e)},n.prototype.remove=function(t){var r,n;return s(this,void 0,void 0,(function(){var o,a,i,s;return u(this,(function(u){switch(u.label){case 0:return o=t.component,clearTimeout(o.addHandler),a=this.armatures[t.gameObject.id],(i=this.renderSystem.containerManager.getContainer(t.gameObject.id))&&a&&i.removeChild(a),o.armature?(o.armature.destroy({children:!0}),[4,e.resource.getResource(o.lastResource)]):[3,2];case 1:h((s=u.sent()).name,null===(n=null===(r=s.data)||void 0===r?void 0:r.image)||void 0===n?void 0:n.src),u.label=2;case 2:return o.armature=null,delete this.armatures[t.gameObject.id],t.type,e.OBSERVER_TYPE.CHANGE,[2]}}))}))},n.systemName="SpineSystem",n=i([e.decorators.componentObserver({Spine:["resource"]})],n)}(t.Renderer);e.resource.registerResourceType("SPINE"),exports.Spine=c,exports.SpineSystem=y;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/// <reference path="./global.d.ts"/>
|
|
2
|
+
import { Component } from '@eva/eva.js';
|
|
3
|
+
import { ComponentChanged } from '@eva/eva.js';
|
|
4
|
+
import { ContainerManager } from '@eva/plugin-renderer';
|
|
5
|
+
import { DisplayObject } from 'pixi.js';
|
|
6
|
+
import { Renderer } from '@eva/plugin-renderer';
|
|
7
|
+
import { RendererManager } from '@eva/plugin-renderer';
|
|
8
|
+
import { RendererSystem } from '@eva/plugin-renderer';
|
|
9
|
+
import { UpdateParams } from '@eva/eva.js';
|
|
10
|
+
|
|
11
|
+
export declare class Spine extends Component<SpineParams> {
|
|
12
|
+
static componentName: string;
|
|
13
|
+
resource: string;
|
|
14
|
+
animationName: string;
|
|
15
|
+
autoPlay: boolean;
|
|
16
|
+
armature: any;
|
|
17
|
+
destroied: boolean;
|
|
18
|
+
addHandler: any;
|
|
19
|
+
lastResource: string;
|
|
20
|
+
init(obj?: SpineParams): void;
|
|
21
|
+
onDestroy(): void;
|
|
22
|
+
play(name?: string, loop?: boolean, track?: number): void;
|
|
23
|
+
stop(track?: number): void;
|
|
24
|
+
addAnimation(name?: string, delay?: number, loop?: boolean, track?: number): void;
|
|
25
|
+
setMix(from: string, to: string, duration: number): void;
|
|
26
|
+
getAnim(track?: number): any;
|
|
27
|
+
setDefaultMix(duration: number): void;
|
|
28
|
+
setAttachment(slotName: string, attachmentName: string): void;
|
|
29
|
+
getBone(boneName: string): any;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export declare interface SpineParams {
|
|
33
|
+
resource: string;
|
|
34
|
+
animationName?: string;
|
|
35
|
+
autoPlay?: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export declare class SpineSystem extends Renderer {
|
|
39
|
+
static systemName: string;
|
|
40
|
+
armatures: Record<number, DisplayObject>;
|
|
41
|
+
renderSystem: RendererSystem;
|
|
42
|
+
rendererManager: RendererManager;
|
|
43
|
+
containerManager: ContainerManager;
|
|
44
|
+
pixiSpine: any;
|
|
45
|
+
init({ pixiSpine }: {
|
|
46
|
+
pixiSpine: any;
|
|
47
|
+
}): void;
|
|
48
|
+
update(e: UpdateParams): void;
|
|
49
|
+
componentChanged(changed: ComponentChanged): Promise<void>;
|
|
50
|
+
add(changed: ComponentChanged, count?: number): Promise<void>;
|
|
51
|
+
change(changed: ComponentChanged): void;
|
|
52
|
+
remove(changed: ComponentChanged): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export { }
|