UNPKG

pixi-dragonbones-runtime

Version:
3 lines (2 loc) 9.18 kB
"use strict";var t=require("../core/BaseObject.cjs");require("../core/DragonBones.cjs");var i=require("../model/AnimationConfig.cjs");require("../model/ArmatureData.cjs"),require("../geom/Transform.cjs");var a=require("./AnimationState.cjs");class n extends t.BaseObject{constructor(){super(...arguments),this._animationNames=[],this._animationStates=[],this._animations={},this._blendStates={},this._animationConfig=null}static toString(){return"[class dragonBones.Animation]"}_onClear(){for(const t of this._animationStates)t.returnToPool();for(let t in this._animations)delete this._animations[t];for(let t in this._blendStates){const i=this._blendStates[t];for(let t in i)i[t].returnToPool();delete this._blendStates[t]}null!==this._animationConfig&&this._animationConfig.returnToPool(),this.timeScale=1,this._animationDirty=!1,this._inheritTimeScale=1,this._animationNames.length=0,this._animationStates.length=0,this._armature=null,this._animationConfig=null,this._lastAnimationState=null}_fadeOut(t){switch(t.fadeOutMode){case 1:for(const i of this._animationStates)null===i._parent&&i.layer===t.layer&&i.fadeOut(t.fadeOutTime,t.pauseFadeOut);break;case 2:for(const i of this._animationStates)null===i._parent&&i.group===t.group&&i.fadeOut(t.fadeOutTime,t.pauseFadeOut);break;case 3:for(const i of this._animationStates)null===i._parent&&i.layer===t.layer&&i.group===t.group&&i.fadeOut(t.fadeOutTime,t.pauseFadeOut);break;case 4:for(const i of this._animationStates)null===i._parent&&i.fadeOut(t.fadeOutTime,t.pauseFadeOut)}}init(a){null===this._armature&&(this._armature=a,this._animationConfig=t.BaseObject.borrowObject(i.AnimationConfig))}advanceTime(t){t<0&&(t=-t),this._armature.inheritAnimation&&null!==this._armature._parent?this._inheritTimeScale=this._armature._parent._armature.animation._inheritTimeScale*this.timeScale:this._inheritTimeScale=this.timeScale,1!==this._inheritTimeScale&&(t*=this._inheritTimeScale);for(let t in this._blendStates){const i=this._blendStates[t];for(let t in i)i[t].reset()}const i=this._animationStates.length;if(1===i){const i=this._animationStates[0];if(i._fadeState>0&&i._subFadeState>0)this._armature._dragonBones.bufferObject(i),this._animationStates.length=0,this._lastAnimationState=null;else{const a=i.animationData,n=a.cacheFrameRate;if(this._animationDirty&&n>0){this._animationDirty=!1;for(const t of this._armature.getBones())t._cachedFrameIndices=a.getBoneCachedFrameIndices(t.name);for(const t of this._armature.getSlots()){if(t.displayFrameCount>0){const i=t.getDisplayFrameAt(0).rawDisplayData;if(null!==i&&i.parent===this._armature.armatureData.defaultSkin){t._cachedFrameIndices=a.getSlotCachedFrameIndices(t.name);continue}}t._cachedFrameIndices=null}}i.advanceTime(t,n)}}else if(i>1){for(let a=0,n=0;a<i;++a){const e=this._animationStates[a];e._fadeState>0&&e._subFadeState>0?(n++,this._armature._dragonBones.bufferObject(e),this._animationDirty=!0,this._lastAnimationState===e&&(this._lastAnimationState=null)):(n>0&&(this._animationStates[a-n]=e),e.advanceTime(t,0)),a===i-1&&n>0&&(this._animationStates.length-=n,null===this._lastAnimationState&&this._animationStates.length>0&&(this._lastAnimationState=this._animationStates[this._animationStates.length-1]))}this._armature._cacheFrameIndex=-1}else this._armature._cacheFrameIndex=-1}reset(){for(const t of this._animationStates)t.returnToPool();this._animationDirty=!1,this._animationConfig.clear(),this._animationStates.length=0,this._lastAnimationState=null}stop(t=null){if(null!==t){const i=this.getState(t);null!==i&&i.stop()}else for(const t of this._animationStates)t.stop()}playConfig(i){const n=i.animation;if(!(n in this._animations))return console.warn("Non-existent animation.\n","DragonBones name: "+this._armature.armatureData.parent.name,"Armature name: "+this._armature.name,"Animation name: "+n),null;const e=this._animations[n];if(5===i.fadeOutMode)for(const t of this._animationStates)if(t._fadeState<1&&t.layer===i.layer&&t.animationData===e)return t;0===this._animationStates.length?i.fadeInTime=0:i.fadeInTime<0&&(i.fadeInTime=e.fadeInTime),i.fadeOutTime<0&&(i.fadeOutTime=i.fadeInTime),i.timeScale<=-100&&(i.timeScale=1/e.scale),e.frameCount>0?(i.position<0?(i.position%=e.duration,i.position=e.duration-i.position):i.position===e.duration?i.position-=1e-6:i.position>e.duration&&(i.position%=e.duration),i.duration>0&&i.position+i.duration>e.duration&&(i.duration=e.duration-i.position),i.playTimes<0&&(i.playTimes=e.playTimes)):(i.playTimes=1,i.position=0,i.duration>0&&(i.duration=0)),0===i.duration&&(i.duration=-1),this._fadeOut(i);const o=t.BaseObject.borrowObject(a.AnimationState);if(o.init(this._armature,e,i),this._animationDirty=!0,this._armature._cacheFrameIndex=-1,this._animationStates.length>0){let t=!1;for(let i=0,a=this._animationStates.length;i<a;++i){if(o.layer>this._animationStates[i].layer){t=!0,this._animationStates.splice(i,0,o);break}if(i!==a-1&&o.layer>this._animationStates[i+1].layer){t=!0,this._animationStates.splice(i+1,0,o);break}}t||this._animationStates.push(o)}else this._animationStates.push(o);for(const t of this._armature.getSlots()){const i=t.childArmature;null!==i&&i.inheritAnimation&&i.animation.hasAnimation(n)&&null===i.animation.getState(n)&&i.animation.fadeIn(n)}for(let t in e.animationTimelines){const i=this.fadeIn(t,0,1,o.layer,"",5);if(null===i)continue;const a=e.animationTimelines[t];i.actionEnabled=!1,i.resetToPose=!1,i.stop(),o.addState(i,a);const n=this._animationStates.indexOf(o),s=this._animationStates.indexOf(i);s<n&&(this._animationStates.splice(n,1),this._animationStates.splice(s,0,o))}return this._lastAnimationState=o,o}play(t=null,i=-1){if(this._animationConfig.clear(),this._animationConfig.resetToPose=!0,this._animationConfig.playTimes=i,this._animationConfig.fadeInTime=0,this._animationConfig.animation=null!==t?t:"",null!==t&&t.length>0)this.playConfig(this._animationConfig);else if(null===this._lastAnimationState){const t=this._armature.armatureData.defaultAnimation;null!==t&&(this._animationConfig.animation=t.name,this.playConfig(this._animationConfig))}else this._lastAnimationState.isPlaying||this._lastAnimationState.isCompleted?(this._animationConfig.animation=this._lastAnimationState.name,this.playConfig(this._animationConfig)):this._lastAnimationState.play();return this._lastAnimationState}fadeIn(t,i=-1,a=-1,n=0,e=null,o=3){return this._animationConfig.clear(),this._animationConfig.fadeOutMode=o,this._animationConfig.playTimes=a,this._animationConfig.layer=n,this._animationConfig.fadeInTime=i,this._animationConfig.animation=t,this._animationConfig.group=null!==e?e:"",this.playConfig(this._animationConfig)}gotoAndPlayByTime(t,i=0,a=-1){return this._animationConfig.clear(),this._animationConfig.resetToPose=!0,this._animationConfig.playTimes=a,this._animationConfig.position=i,this._animationConfig.fadeInTime=0,this._animationConfig.animation=t,this.playConfig(this._animationConfig)}gotoAndPlayByFrame(t,i=0,a=-1){this._animationConfig.clear(),this._animationConfig.resetToPose=!0,this._animationConfig.playTimes=a,this._animationConfig.fadeInTime=0,this._animationConfig.animation=t;const n=t in this._animations?this._animations[t]:null;return null!==n&&(this._animationConfig.position=n.frameCount>0?n.duration*i/n.frameCount:0),this.playConfig(this._animationConfig)}gotoAndPlayByProgress(t,i=0,a=-1){this._animationConfig.clear(),this._animationConfig.resetToPose=!0,this._animationConfig.playTimes=a,this._animationConfig.fadeInTime=0,this._animationConfig.animation=t;const n=t in this._animations?this._animations[t]:null;return null!==n&&(this._animationConfig.position=n.duration*(i>0?i:0)),this.playConfig(this._animationConfig)}gotoAndStopByTime(t,i=0){const a=this.gotoAndPlayByTime(t,i,1);return null!==a&&a.stop(),a}gotoAndStopByFrame(t,i=0){const a=this.gotoAndPlayByFrame(t,i,1);return null!==a&&a.stop(),a}gotoAndStopByProgress(t,i=0){const a=this.gotoAndPlayByProgress(t,i,1);return null!==a&&a.stop(),a}getBlendState(i,n,e){i in this._blendStates||(this._blendStates[i]={});const o=this._blendStates[i];if(!(n in o)){(o[n]=t.BaseObject.borrowObject(a.BlendState)).target=e}return o[n]}getState(t,i=-1){let a=this._animationStates.length;for(;a--;){const n=this._animationStates[a];if(n.name===t&&(i<0||n.layer===i))return n}return null}hasAnimation(t){return t in this._animations}getStates(){return this._animationStates}get isPlaying(){for(const t of this._animationStates)if(t.isPlaying)return!0;return!1}get isCompleted(){for(const t of this._animationStates)if(!t.isCompleted)return!1;return this._animationStates.length>0}get lastAnimationName(){return null!==this._lastAnimationState?this._lastAnimationState.name:""}get animationNames(){return this._animationNames}get animations(){return this._animations}set animations(t){if(this._animations!==t){this._animationNames.length=0;for(let t in this._animations)delete this._animations[t];for(let i in t)this._animationNames.push(i),this._animations[i]=t[i]}}get animationConfig(){return this._animationConfig.clear(),this._animationConfig}get lastAnimationState(){return this._lastAnimationState}}exports.Animation=n; //# sourceMappingURL=Animation.cjs.map