@eva/spine-base 2.0.0-beta.1 → 2.0.0-beta.2

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 +1,16 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@eva/eva.js"),t=require("@eva/plugin-renderer"),n=require("@eva/inspector-decorator"),r=require("pixi.js"),a=function(e,t){return a=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])},a(e,t)};function o(e,t){function n(){this.constructor=e}a(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}function i(e,t,n,r){var a,o=arguments.length,i=o<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(a=e[s])&&(i=(o<3?a(i):o>3?a(t,n,i):a(t,n))||i);return o>3&&i&&Object.defineProperty(t,n,i),i}function s(e,t,n,r){return new(n||(n=Promise))((function(a,o){function i(e){try{u(r.next(e))}catch(e){o(e)}}function s(e){try{u(r.throw(e))}catch(e){o(e)}}function u(e){e.done?a(e.value):new n((function(t){t(e.value)})).then(i,s)}u((r=r.apply(e,t||[])).next())}))}function u(e,t){var n,r,a,o,i={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;i;)try{if(n=1,r&&(a=2&o[0]?r.return:o[0]?r.throw||((a=r.return)&&a.call(r),0):r.next)&&!(a=a.call(r,o[1])).done)return a;switch(r=0,a&&(o=[2&o[0],a.value]),o[0]){case 0:case 1:a=o;break;case 4:return i.label++,{value:o[1],done:!1};case 5:i.label++,r=o[1],o=[0];continue;case 7:o=i.ops.pop(),i.trys.pop();continue;default:if(!(a=i.trys,(a=a.length>0&&a[a.length-1])||6!==o[0]&&2!==o[0])){i=0;continue}if(3===o[0]&&(!a||o[1]>a[0]&&o[1]<a[3])){i.label=o[1];break}if(6===o[0]&&i.label<a[1]){i.label=a[1],a=o;break}if(a&&i.label<a[2]){i.label=a[2],i.ops.push(o);break}a[2]&&i.ops.pop(),i.trys.pop();continue}o=t.call(e,i)}catch(e){o=[6,e],r=0}finally{n=a=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}}var c=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.resource="",t.scale=1,t.animationName="",t.autoPlay=!0,t.waitExecuteInfos=[],t}return o(t,e),Object.defineProperty(t.prototype,"armature",{get:function(){return this._armature},set:function(e){var t,n;if(this._armature=e,e){this.autoPlay&&this.play(this.animationName);try{for(var r=function(e){var t="function"==typeof Symbol&&e[Symbol.iterator],n=0;return t?t.call(e):{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}}}(this.waitExecuteInfos),a=r.next();!a.done;a=r.next()){var o=a.value;if(o.playType){var i=o.name,s=o.loop,u=o.track;this.play(i,s,u)}else this.stop(o.track)}}catch(e){t={error:e}}finally{try{a&&!a.done&&(n=r.return)&&n.call(r)}finally{if(t)throw t.error}}this.waitExecuteInfos=[]}},enumerable:!1,configurable:!0}),t.prototype.init=function(e){e&&Object.assign(this,e)},t.prototype.onDestroy=function(){this.destroied=!0},t.prototype.play=function(e,t,n){try{e&&(this.animationName=e),this.armature?(void 0===n&&(n=0),this.armature.state.setAnimation(n,this.animationName,t)):this.waitExecuteInfos.push({playType:!0,name:e,loop:null!=t?t:this.autoPlay,track:n})}catch(e){console.log(e)}},t.prototype.stop=function(e){this.armature?(void 0===e&&(e=0),this.armature.state.setEmptyAnimation(e,0)):this.waitExecuteInfos.push({playType:!1,track:e})},t.prototype.addAnimation=function(e,t,n,r){try{this.armature&&(void 0===r&&(r=0),this.armature.state.addAnimation(r,e,n,t))}catch(e){console.log(e)}},t.prototype.setMix=function(e,t,n){this.armature&&this.armature.stateData.setMix(e,t,n)},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([n.type("string")],t.prototype,"resource",void 0),i([n.type("number")],t.prototype,"scale",void 0),i([n.type("string")],t.prototype,"animationName",void 0),i([n.type("boolean")],t.prototype,"autoPlay",void 0),t}(e.Component),p={};function l(e,t){var n=p[e];return n||(n=function(e){var t=e.image;return{tex:t instanceof r.Texture?t:r.Texture.from(t),count:0}}(t),p[e]=n),n.count++,n.tex}var m={};function d(e,t,n){return s(this,void 0,void 0,(function(){var r;return u(this,(function(a){if(!(r=m[e.name]))if(e.complete)r=function(e,t,n,r){var a=t.ske,o=t.atlas,i=new r.AtlasAttachmentLoader(o),s=a instanceof Uint8Array?new r.SkeletonBinary(i):new r.SkeletonJson(i);s.scale=n||1;var u={spineData:s.readSkeletonData(a),ref:0,imageSrc:t.image.label};return m[e]=u,u}(e.name,e.data,t,n);else if(!r)return[2];return l(e.data.image.label,e.data),r.ref++,[2,r.spineData]}))}))}function f(e,t){var n=m[e];n&&(n.ref--,n.ref<=0&&(!function(e){e&&setTimeout((function(){var t=p[e];t&&(t.count--,t.count<=0&&(t.tex&&(t.tex.destroy(!0),t.tex=null),delete p[e]))}),100)}(t),delete m[e]))}var h=function(n){function r(){var e=null!==n&&n.apply(this,arguments)||this;return e.armatures={},e}return o(r,n),r.prototype.init=function(n){var r=this,a=n.pixiSpine;this.renderSystem=this.game.getSystem(t.RendererSystem),this.renderSystem.rendererManager.register(this),this.pixiSpine=a,this.game.canvas.addEventListener("webglcontextrestored",(function(){var t=r.game.gameObjects,n=[];for(var a in r.armatures)for(var o=+a,i=0;i<t.length;++i){var s=t[i];if(s.id===o){var u=s.getComponent(c);u&&(r.remove({type:e.OBSERVER_TYPE.REMOVE,gameObject:s,component:u,componentName:c.componentName}),n.push({type:e.OBSERVER_TYPE.ADD,gameObject:s,component:u,componentName:c.componentName}));break}}setTimeout((function(){n.forEach((function(e){r.add(e)}))}),1e3)}),!1)},r.prototype.update=function(e){for(var t in this.armatures)this.armatures[t].update(.001*e.deltaTime);n.prototype.update.call(this)},r.prototype.componentChanged=function(t){return s(this,void 0,void 0,(function(){return u(this,(function(n){if("Spine"===t.componentName)if(t.type===e.OBSERVER_TYPE.ADD)this.add(t);else if(t.type===e.OBSERVER_TYPE.CHANGE){if("resource"===t.prop.prop[0])this.change(t)}else t.type===e.OBSERVER_TYPE.REMOVE&&this.remove(t);return[2]}))}))},r.prototype.add=function(t,n){var r,a;return s(this,void 0,void 0,(function(){var o,i,s,c,p,l,m,f,h=this;return u(this,(function(u){switch(u.label){case 0:return o=t.component,clearTimeout(o.addHandler),i=t.gameObject.id,s=this.increaseAsyncId(i),[4,e.resource.getResource(o.resource)];case 1:return c=u.sent(),this.validateAsyncId(i,s)?[4,d(c,o.scale,this.pixiSpine)]:[2];case 2:return p=u.sent(),this.validateAsyncId(i,s)?p?(this.remove(t),(l=null===(a=null===(r=this.renderSystem)||void 0===r?void 0:r.containerManager)||void 0===a?void 0:a.getContainer(t.gameObject.id))?(o.lastResource=o.resource,m=new this.pixiSpine.Spine({skeletonData:p,autoUpdate:!1}),this.armatures[t.gameObject.id]=m,t.gameObject&&t.gameObject.transform&&(f=t.gameObject.transform,m.x=f.size.width*f.origin.x,m.y=f.size.height*f.origin.y),l.addChildAt(m,0),m.update(),o.armature=m,o.emit("loaded",{resource:o.resource}),m.state.addListener({start:function(e,t){o.emit("start",{track:e,name:e.animation.name})},complete:function(e,t){o.emit("complete",{track:e,name:e.animation.name})},interrupt:function(e,t){o.emit("interrupt",{track:e,name:e.animation.name})},end:function(e,t){o.emit("end",{track:e,name:e.animation.name})},event:function(e,t){o.emit("event",e,t)}}),[2]):[2]):(o.addHandler=setTimeout((function(){o.destroied||(void 0===n&&(n=20),--n>0?h.add(t,n):console.log("retry exceed max times",o.resource))}),1e3),[2]):[2]}}))}))},r.prototype.change=function(e){this.remove(e),this.add(e)},r.prototype.remove=function(t){var n,r,a,o;return s(this,void 0,void 0,(function(){var i,s,c,p;return u(this,(function(u){switch(u.label){case 0:return this.increaseAsyncId(t.gameObject.id),i=t.component,clearTimeout(i.addHandler),s=this.armatures[t.gameObject.id],(c=null===(r=null===(n=this.renderSystem)||void 0===n?void 0:n.containerManager)||void 0===r?void 0:r.getContainer(t.gameObject.id))&&s&&c.removeChild(s),i.armature?(i.armature.destroy({children:!0}),[4,e.resource.getResource(i.lastResource)]):[3,2];case 1:f((p=u.sent()).name,null===(o=null===(a=p.data)||void 0===a?void 0:a.image)||void 0===o?void 0:o.src),u.label=2;case 2:return i.armature=null,delete this.armatures[t.gameObject.id],t.type,e.OBSERVER_TYPE.CHANGE,[2]}}))}))},r.systemName="SpineSystem",r=i([e.decorators.componentObserver({Spine:["resource"]})],r)}(t.Renderer);e.resource.registerResourceType("SPINE"),exports.Spine=c,exports.SpineSystem=h;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@eva/eva.js"),t=require("@eva/plugin-renderer"),a=require("@eva/inspector-decorator"),r=require("pixi.js");
2
+ /*! *****************************************************************************
3
+ Copyright (c) Microsoft Corporation. All rights reserved.
4
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
5
+ this file except in compliance with the License. You may obtain a copy of the
6
+ License at http://www.apache.org/licenses/LICENSE-2.0
7
+
8
+ THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9
+ KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
10
+ WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
11
+ MERCHANTABLITY OR NON-INFRINGEMENT.
12
+
13
+ See the Apache Version 2.0 License for specific language governing permissions
14
+ and limitations under the License.
15
+ ***************************************************************************** */
16
+ function i(e,t,a,r){var i,n=arguments.length,o=n<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,a):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)o=Reflect.decorate(e,t,a,r);else for(var s=e.length-1;s>=0;s--)(i=e[s])&&(o=(n<3?i(o):n>3?i(t,a,o):i(t,a))||o);return n>3&&o&&Object.defineProperty(t,a,o),o}function n(e,t,a,r){return new(a||(a=Promise))((function(i,n){function o(e){try{c(r.next(e))}catch(e){n(e)}}function s(e){try{c(r.throw(e))}catch(e){n(e)}}function c(e){e.done?i(e.value):new a((function(t){t(e.value)})).then(o,s)}c((r=r.apply(e,t||[])).next())}))}class o extends e.Component{constructor(){super(...arguments),this.resource="",this.scale=1,this.animationName="",this.autoPlay=!0,this.waitExecuteInfos=[]}set armature(e){if(this._armature=e,e){this.autoPlay&&this.play(this.animationName);for(const e of this.waitExecuteInfos)if(e.playType){const{name:t,loop:a,track:r}=e;this.play(t,a,r)}else this.stop(e.track);this.waitExecuteInfos=[]}}get armature(){return this._armature}init(e){e&&Object.assign(this,e)}onDestroy(){this.destroied=!0}play(e,t,a){try{e&&(this.animationName=e),this.armature?(void 0===a&&(a=0),this.armature.state.setAnimation(a,this.animationName,t)):this.waitExecuteInfos.push({playType:!0,name:e,loop:null!=t?t:this.autoPlay,track:a})}catch(e){console.log(e)}}stop(e){this.armature?(void 0===e&&(e=0),this.armature.state.setEmptyAnimation(e,0)):this.waitExecuteInfos.push({playType:!1,track:e})}addAnimation(e,t,a,r){try{this.armature&&(void 0===r&&(r=0),this.armature.state.addAnimation(r,e,a,t))}catch(e){console.log(e)}}setMix(e,t,a){this.armature&&this.armature.stateData.setMix(e,t,a)}getAnim(e=0){try{if(this.armature)return this.armature.state.tracks[e].animation.name}catch(e){console.log(e)}}setDefaultMix(e){this.armature&&(this.armature.stateData.defaultMix=e)}setAttachment(e,t){this.armature&&this.armature.skeleton.setAttachment(e,t)}getBone(e){if(this.armature)return this.armature.skeleton.findBone(e)}}o.componentName="Spine",i([a.type("string")],o.prototype,"resource",void 0),i([a.type("number")],o.prototype,"scale",void 0),i([a.type("string")],o.prototype,"animationName",void 0),i([a.type("boolean")],o.prototype,"autoPlay",void 0);let s={};function c(e,t){let a=s[e];return a||(a=function(e){const t=e.image;return{tex:t instanceof r.Texture?t:r.Texture.from(t),count:0}}(t),s[e]=a),a.count++,a.tex}let m={};function u(e,t,a){return n(this,void 0,void 0,(function*(){let r=m[e.name];if(!r)if(e.complete)r=function(e,t,a,r){const i=t.ske,n=t.atlas,o=new r.AtlasAttachmentLoader(n),s=i instanceof Uint8Array?new r.SkeletonBinary(o):new r.SkeletonJson(o);s.scale=a||1;const c={spineData:s.readSkeletonData(i),ref:0,imageSrc:t.image.label};return m[e]=c,c}(e.name,e.data,t,a);else if(!r)return;return c(e.data.image.label,e.data),r.ref++,r.spineData}))}function d(e,t){const a=m[e];a&&(a.ref--,a.ref<=0&&(!function(e){e&&setTimeout((()=>{const t=s[e];t&&(t.count--,t.count<=0&&(t.tex&&(t.tex.destroy(!0),t.tex=null),delete s[e]))}),100)}(t),delete m[e]))}let l=class extends t.Renderer{constructor(){super(...arguments),this.armatures={}}init({pixiSpine:a}){this.renderSystem=this.game.getSystem(t.RendererSystem),this.renderSystem.rendererManager.register(this),this.pixiSpine=a,this.game.canvas.addEventListener("webglcontextrestored",(()=>{const t=this.game.gameObjects;let a=[];for(let r in this.armatures){const i=+r;for(let r=0;r<t.length;++r){let n=t[r];if(n.id===i){let t=n.getComponent(o);t&&(this.remove({type:e.OBSERVER_TYPE.REMOVE,gameObject:n,component:t,componentName:o.componentName}),a.push({type:e.OBSERVER_TYPE.ADD,gameObject:n,component:t,componentName:o.componentName}));break}}}setTimeout((()=>{a.forEach((e=>{this.add(e)}))}),1e3)}),!1)}update(e){for(let t in this.armatures)this.armatures[t].update(.001*e.deltaTime);super.update()}componentChanged(t){return n(this,void 0,void 0,(function*(){if("Spine"===t.componentName)if(t.type===e.OBSERVER_TYPE.ADD)this.add(t);else if(t.type===e.OBSERVER_TYPE.CHANGE){if("resource"===t.prop.prop[0])this.change(t)}else t.type===e.OBSERVER_TYPE.REMOVE&&this.remove(t)}))}add(t,a){var r,i;return n(this,void 0,void 0,(function*(){const n=t.component;clearTimeout(n.addHandler);const o=t.gameObject.id,s=this.increaseAsyncId(o),c=yield e.resource.getResource(n.resource);if(!this.validateAsyncId(o,s))return;const m=yield u(c,n.scale,this.pixiSpine);if(!this.validateAsyncId(o,s))return;if(!m)return void(n.addHandler=setTimeout((()=>{n.destroied||(void 0===a&&(a=20),--a>0?this.add(t,a):console.log("retry exceed max times",n.resource))}),1e3));this.remove(t);const d=null===(i=null===(r=this.renderSystem)||void 0===r?void 0:r.containerManager)||void 0===i?void 0:i.getContainer(t.gameObject.id);if(!d)return;n.lastResource=n.resource;const l=new this.pixiSpine.Spine({skeletonData:m,autoUpdate:!1});if(this.armatures[t.gameObject.id]=l,t.gameObject&&t.gameObject.transform){const e=t.gameObject.transform;l.x=e.size.width*e.origin.x,l.y=e.size.height*e.origin.y}d.addChildAt(l,0),l.update(),n.armature=l,n.emit("loaded",{resource:n.resource}),l.state.addListener({start:(e,t)=>{n.emit("start",{track:e,name:e.animation.name})},complete:(e,t)=>{n.emit("complete",{track:e,name:e.animation.name})},interrupt:(e,t)=>{n.emit("interrupt",{track:e,name:e.animation.name})},end:(e,t)=>{n.emit("end",{track:e,name:e.animation.name})},event:(e,t)=>{n.emit("event",e,t)}})}))}change(e){this.remove(e),this.add(e)}remove(t){var a,r,i,o;return n(this,void 0,void 0,(function*(){this.increaseAsyncId(t.gameObject.id);const n=t.component;clearTimeout(n.addHandler);const s=this.armatures[t.gameObject.id],c=null===(r=null===(a=this.renderSystem)||void 0===a?void 0:a.containerManager)||void 0===r?void 0:r.getContainer(t.gameObject.id);if(c&&s&&c.removeChild(s),n.armature){n.armature.destroy({children:!0});const t=yield e.resource.getResource(n.lastResource);d(t.name,null===(o=null===(i=t.data)||void 0===i?void 0:i.image)||void 0===o?void 0:o.src)}n.armature=null,delete this.armatures[t.gameObject.id],t.type,e.OBSERVER_TYPE.CHANGE}))}};l.systemName="SpineSystem",l=i([e.decorators.componentObserver({Spine:["resource"]})],l);var h=l;e.resource.registerResourceType("SPINE"),exports.Spine=o,exports.SpineSystem=h;