pixi-dragonbones-runtime
Version:
DragonBones Runtime for Pixi.js
3 lines (2 loc) • 17.5 kB
JavaScript
import{BaseObject as t}from"../core/BaseObject.mjs";import"../core/DragonBones.mjs";import{EventObject as e}from"../event/EventObject.mjs";import{IKConstraintTimelineState as i,SurfaceTimelineState as s,AlphaTimelineState as n,BoneScaleTimelineState as a,BoneRotateTimelineState as o,BoneTranslateTimelineState as h,BoneAllTimelineState as r,DeformTimelineState as l,SlotColorTimelineState as m,SlotZIndexTimelineState as _,SlotDisplayTimelineState as c,ActionTimelineState as u,ZOrderTimelineState as d,AnimationParametersTimelineState as T,AnimationWeightTimelineState as f,AnimationProgressTimelineState as p}from"./TimelineState.mjs";class g extends t{constructor(){super(...arguments),this._boneMask=[],this._boneTimelines=[],this._boneBlendTimelines=[],this._slotTimelines=[],this._slotBlendTimelines=[],this._constraintTimelines=[],this._animationTimelines=[],this._poseTimelines=[],this._actionTimeline=null,this._zOrderTimeline=null}static toString(){return"[class dragonBones.AnimationState]"}_onClear(){for(const t of this._boneTimelines)t.returnToPool();for(const t of this._boneBlendTimelines)t.returnToPool();for(const t of this._slotTimelines)t.returnToPool();for(const t of this._slotBlendTimelines)t.returnToPool();for(const t of this._constraintTimelines)t.returnToPool();for(const t of this._animationTimelines){const e=t.target;e._parent===this&&(e._fadeState=1,e._subFadeState=1,e._parent=null),t.returnToPool()}null!==this._actionTimeline&&this._actionTimeline.returnToPool(),null!==this._zOrderTimeline&&this._zOrderTimeline.returnToPool(),this.actionEnabled=!1,this.additive=!1,this.displayControl=!1,this.resetToPose=!1,this.blendType=0,this.playTimes=1,this.layer=0,this.timeScale=1,this._weight=1,this.parameterX=0,this.parameterY=0,this.positionX=0,this.positionY=0,this.autoFadeOutTime=0,this.fadeTotalTime=0,this.name="",this.group="",this._timelineDirty=2,this._playheadState=0,this._fadeState=-1,this._subFadeState=-1,this._position=0,this._duration=0,this._fadeTime=0,this._time=0,this._fadeProgress=0,this._weightResult=0,this._boneMask.length=0,this._boneTimelines.length=0,this._boneBlendTimelines.length=0,this._slotTimelines.length=0,this._slotBlendTimelines.length=0,this._constraintTimelines.length=0,this._animationTimelines.length=0,this._poseTimelines.length=0,this._animationData=null,this._armature=null,this._actionTimeline=null,this._zOrderTimeline=null,this._activeChildA=null,this._activeChildB=null,this._parent=null}_updateTimelines(){for(const e of this._armature._constraints){const s=this._animationData.getConstraintTimelines(e.name);if(null!==s)for(const n of s)switch(n.type){case 30:{const s=t.borrowObject(i);s.target=e,s.init(this._armature,this,n),this._constraintTimelines.push(s);break}}else if(this.resetToPose){const s=t.borrowObject(i);s.target=e,s.init(this._armature,this,null),this._constraintTimelines.push(s),this._poseTimelines.push(s)}}}_updateBoneAndSlotTimelines(){{const e={};for(const t of this._boneTimelines){const i=t.target.target.name;i in e||(e[i]=[]),e[i].push(t)}for(const t of this._boneBlendTimelines){const i=t.target.target.name;i in e||(e[i]=[]),e[i].push(t)}for(const i of this._armature.getBones()){const l=i.name;if(this.containsBoneMask(l))if(l in e)delete e[l];else{const e=this._animationData.getBoneTimelines(l),m=this._armature.animation.getBlendState(b.BONE_TRANSFORM,i.name,i);if(null!==e)for(const l of e)switch(l.type){case 10:{const e=t.borrowObject(r);e.target=m,e.init(this._armature,this,l),this._boneTimelines.push(e);break}case 11:{const e=t.borrowObject(h);e.target=m,e.init(this._armature,this,l),this._boneTimelines.push(e);break}case 12:{const e=t.borrowObject(o);e.target=m,e.init(this._armature,this,l),this._boneTimelines.push(e);break}case 13:{const e=t.borrowObject(a);e.target=m,e.init(this._armature,this,l),this._boneTimelines.push(e);break}case 60:{const e=t.borrowObject(n);e.target=this._armature.animation.getBlendState(b.BONE_ALPHA,i.name,i),e.init(this._armature,this,l),this._boneBlendTimelines.push(e);break}case 50:{const e=t.borrowObject(s);e.target=this._armature.animation.getBlendState(b.SURFACE,i.name,i),e.init(this._armature,this,l),this._boneBlendTimelines.push(e);break}}else if(this.resetToPose)if(0===i._boneData.type){const e=t.borrowObject(r);e.target=m,e.init(this._armature,this,null),this._boneTimelines.push(e),this._poseTimelines.push(e)}else{const e=t.borrowObject(s);e.target=this._armature.animation.getBlendState(b.SURFACE,i.name,i),e.init(this._armature,this,null),this._boneBlendTimelines.push(e),this._poseTimelines.push(e)}}}for(let t in e)for(const i of e[t]){let t=this._boneTimelines.indexOf(i);t>=0&&(this._boneTimelines.splice(t,1),i.returnToPool()),t=this._boneBlendTimelines.indexOf(i),t>=0&&(this._boneBlendTimelines.splice(t,1),i.returnToPool())}}{const e={},i=[];for(const t of this._slotTimelines){const i=t.target.name;i in e||(e[i]=[]),e[i].push(t)}for(const t of this._slotBlendTimelines){const i=t.target.target.name;i in e||(e[i]=[]),e[i].push(t)}for(const s of this._armature.getSlots()){const a=s.parent.name;if(!this.containsBoneMask(a))continue;const o=s.name;if(o in e)delete e[o];else{let e=!1,a=!1;i.length=0;const h=this._animationData.getSlotTimelines(o);if(null!==h)for(const o of h)switch(o.type){case 20:{const i=t.borrowObject(c);i.target=s,i.init(this._armature,this,o),this._slotTimelines.push(i),e=!0;break}case 23:{const e=t.borrowObject(_);e.target=this._armature.animation.getBlendState(b.SLOT_Z_INDEX,s.name,s),e.init(this._armature,this,o),this._slotBlendTimelines.push(e);break}case 21:{const e=t.borrowObject(m);e.target=s,e.init(this._armature,this,o),this._slotTimelines.push(e),a=!0;break}case 22:{const e=this._animationData.parent.parent,n=e.timelineArray,a=this._animationData.frameIntOffset+n[o.offset+3];let h=e.frameIntArray[a+0];h<0&&(h+=65536);for(let e=0,n=s.displayFrameCount;e<n;++e){const n=s.getDisplayFrameAt(e),a=n.getGeometryData();if(null!==a&&a.offset===h){const e=t.borrowObject(l);e.target=this._armature.animation.getBlendState(b.SLOT_DEFORM,n.rawDisplayData.name,s),e.displayFrame=n,e.init(this._armature,this,o),this._slotBlendTimelines.push(e),n.updateDeformVertices(),i.push(h);break}}break}case 24:{const e=t.borrowObject(n);e.target=this._armature.animation.getBlendState(b.SLOT_ALPHA,s.name,s),e.init(this._armature,this,o),this._slotBlendTimelines.push(e);break}}if(this.resetToPose){if(!e){const e=t.borrowObject(c);e.target=s,e.init(this._armature,this,null),this._slotTimelines.push(e),this._poseTimelines.push(e)}if(!a){const e=t.borrowObject(m);e.target=s,e.init(this._armature,this,null),this._slotTimelines.push(e),this._poseTimelines.push(e)}for(let e=0,n=s.displayFrameCount;e<n;++e){const n=s.getDisplayFrameAt(e);if(0===n.deformVertices.length)continue;const a=n.getGeometryData();if(null!==a&&i.indexOf(a.offset)<0){const e=t.borrowObject(l);e.displayFrame=n,e.target=this._armature.animation.getBlendState(b.SLOT_DEFORM,s.name,s),e.init(this._armature,this,null),this._slotBlendTimelines.push(e),this._poseTimelines.push(e)}}}}}for(let t in e)for(const i of e[t]){let t=this._slotTimelines.indexOf(i);t>=0&&(this._slotTimelines.splice(t,1),i.returnToPool()),t=this._slotBlendTimelines.indexOf(i),t>=0&&(this._slotBlendTimelines.splice(t,1),i.returnToPool())}}}_advanceFadeTime(i){const s=this._fadeState>0;if(this._subFadeState<0){this._subFadeState=0;if(null===this._parent&&this.actionEnabled){const i=s?e.FADE_OUT:e.FADE_IN;if(this._armature.eventDispatcher.hasDBEventListener(i)){const s=t.borrowObject(e);s.type=i,s.armature=this._armature,s.animationState=this,this._armature._dragonBones.bufferEvent(s)}}}if(i<0&&(i=-i),this._fadeTime+=i,this._fadeTime>=this.fadeTotalTime?(this._subFadeState=1,this._fadeProgress=s?0:1):this._fadeTime>0?this._fadeProgress=s?1-this._fadeTime/this.fadeTotalTime:this._fadeTime/this.fadeTotalTime:this._fadeProgress=s?1:0,this._subFadeState>0){s||(this._playheadState|=1,this._fadeState=0);if(null===this._parent&&this.actionEnabled){const i=s?e.FADE_OUT_COMPLETE:e.FADE_IN_COMPLETE;if(this._armature.eventDispatcher.hasDBEventListener(i)){const s=t.borrowObject(e);s.type=i,s.armature=this._armature,s.animationState=this,this._armature._dragonBones.bufferEvent(s)}}}}init(e,i,s){if(null===this._armature){if(this._armature=e,this._animationData=i,this.resetToPose=s.resetToPose,this.additive=s.additive,this.displayControl=s.displayControl,this.actionEnabled=s.actionEnabled,this.blendType=i.blendType,this.layer=s.layer,this.playTimes=s.playTimes,this.timeScale=s.timeScale,this.fadeTotalTime=s.fadeInTime,this.autoFadeOutTime=s.autoFadeOutTime,this.name=s.name.length>0?s.name:s.animation,this.group=s.group,this._weight=s.weight,s.pauseFadeIn?this._playheadState=2:this._playheadState=3,s.duration<0?(this._position=0,this._duration=this._animationData.duration,0!==s.position?this.timeScale>=0?this._time=s.position:this._time=s.position-this._duration:this._time=0):(this._position=s.position,this._duration=s.duration,this._time=0),this.timeScale<0&&0===this._time&&(this._time=-1e-6),this.fadeTotalTime<=0&&(this._fadeProgress=.999999),s.boneMask.length>0){this._boneMask.length=s.boneMask.length;for(let t=0,e=this._boneMask.length;t<e;++t)this._boneMask[t]=s.boneMask[t]}this._actionTimeline=t.borrowObject(u),this._actionTimeline.init(this._armature,this,this._animationData.actionTimeline),this._actionTimeline.currentTime=this._time,this._actionTimeline.currentTime<0&&(this._actionTimeline.currentTime=this._duration-this._actionTimeline.currentTime),null!==this._animationData.zOrderTimeline&&(this._zOrderTimeline=t.borrowObject(d),this._zOrderTimeline.init(this._armature,this,this._animationData.zOrderTimeline))}}advanceTime(t,e){0===this._fadeState&&0===this._subFadeState||this._advanceFadeTime(t),3===this._playheadState&&(1!==this.timeScale&&(t*=this.timeScale),this._time+=t),0!==this._timelineDirty&&(2===this._timelineDirty&&this._updateTimelines(),this._timelineDirty=0,this._updateBoneAndSlotTimelines());const i=0!==this._fadeState||0===this._subFadeState,s=0===this._fadeState&&e>0;let n=!0,a=!0,o=this._time;if(this._weightResult=this._weight*this._fadeProgress,null!==this._parent&&(this._weightResult*=this._parent._weightResult),this._actionTimeline.playState<=0&&this._actionTimeline.update(o),0!==this._weight){if(s){const t=2*e;this._actionTimeline.currentTime=Math.floor(this._actionTimeline.currentTime*t)/t}if(null!==this._zOrderTimeline&&this._zOrderTimeline.playState<=0&&this._zOrderTimeline.update(o),s){const t=Math.floor(this._actionTimeline.currentTime*e);this._armature._cacheFrameIndex===t?(n=!1,a=!1):(this._armature._cacheFrameIndex=t,this._animationData.cachedFrames[t]?a=!1:this._animationData.cachedFrames[t]=!0)}if(n){let t=!1,e=null;if(a)for(let s=0,n=this._boneTimelines.length;s<n;++s){const n=this._boneTimelines[s];if(n.playState<=0&&n.update(o),n.target!==e){const i=n.target;if(t=i.update(this),e=i,1===i.dirty){const t=i.target.animationPose;t.x=0,t.y=0,t.rotation=0,t.skew=0,t.scaleX=1,t.scaleY=1}}t&&n.blend(i)}for(let t=0,e=this._boneBlendTimelines.length;t<e;++t){const e=this._boneBlendTimelines[t];e.playState<=0&&e.update(o),e.target.update(this)&&e.blend(i)}if(this.displayControl)for(let t=0,e=this._slotTimelines.length;t<e;++t){const e=this._slotTimelines[t];if(e.playState<=0){const t=e.target.displayController;null!==t&&t!==this.name&&t!==this.group||e.update(o)}}for(let t=0,e=this._slotBlendTimelines.length;t<e;++t){const e=this._slotBlendTimelines[t];if(e.playState<=0){const t=e.target;e.update(o),t.update(this)&&e.blend(i)}}for(let t=0,e=this._constraintTimelines.length;t<e;++t){const e=this._constraintTimelines[t];e.playState<=0&&e.update(o)}if(this._animationTimelines.length>0){let t=100,e=100,i=null,s=null;for(let n=0,a=this._animationTimelines.length;n<a;++n){const a=this._animationTimelines[n];if(a.playState<=0&&a.update(o),1===this.blendType){const n=a.target,o=this.parameterX-n.positionX;o>=0?o<t&&(t=o,i=n):-o<e&&(e=-o,s=n)}}null!==i&&(this._activeChildA!==i&&(null!==this._activeChildA&&(this._activeChildA.weight=0),this._activeChildA=i,this._activeChildA.activeTimeline()),this._activeChildB!==s&&(null!==this._activeChildB&&(this._activeChildB.weight=0),this._activeChildB=s),i.weight=e/(t+e),s&&(s.weight=1-i.weight))}}if(0===this._fadeState){if(this._subFadeState>0&&(this._subFadeState=0,this._poseTimelines.length>0)){for(const t of this._poseTimelines){let e=this._boneTimelines.indexOf(t);e>=0?(this._boneTimelines.splice(e,1),t.returnToPool()):(e=this._boneBlendTimelines.indexOf(t),e>=0?(this._boneBlendTimelines.splice(e,1),t.returnToPool()):(e=this._slotTimelines.indexOf(t),e>=0?(this._slotTimelines.splice(e,1),t.returnToPool()):(e=this._slotBlendTimelines.indexOf(t),e>=0?(this._slotBlendTimelines.splice(e,1),t.returnToPool()):(e=this._constraintTimelines.indexOf(t),e>=0&&(this._constraintTimelines.splice(e,1),t.returnToPool())))))}this._poseTimelines.length=0}this._actionTimeline.playState>0&&this.autoFadeOutTime>=0&&this.fadeOut(this.autoFadeOutTime)}}}play(){this._playheadState=3}stop(){this._playheadState&=1}fadeOut(t,e=!0){if(t<0&&(t=0),e&&(this._playheadState&=2),this._fadeState>0){if(t>this.fadeTotalTime-this._fadeTime)return}else{this._fadeState=1,this._subFadeState=-1,(t<=0||this._fadeProgress<=0)&&(this._fadeProgress=1e-6);for(const t of this._boneTimelines)t.fadeOut();for(const t of this._boneBlendTimelines)t.fadeOut();for(const t of this._slotTimelines)t.fadeOut();for(const t of this._slotBlendTimelines)t.fadeOut();for(const t of this._constraintTimelines)t.fadeOut();for(const t of this._animationTimelines){t.fadeOut();t.target.fadeOut(999999,!0)}}this.displayControl=!1,this.fadeTotalTime=this._fadeProgress>1e-6?t/this._fadeProgress:0,this._fadeTime=this.fadeTotalTime*(1-this._fadeProgress)}containsBoneMask(t){return 0===this._boneMask.length||this._boneMask.indexOf(t)>=0}addBoneMask(t,e=!0){const i=this._armature.getBone(t);if(null!==i){if(this._boneMask.indexOf(t)<0&&this._boneMask.push(t),e)for(const t of this._armature.getBones())this._boneMask.indexOf(t.name)<0&&i.contains(t)&&this._boneMask.push(t.name);this._timelineDirty=1}}removeBoneMask(t,e=!0){const i=this._boneMask.indexOf(t);if(i>=0&&this._boneMask.splice(i,1),e){const e=this._armature.getBone(t);if(null!==e){const t=this._armature.getBones();if(this._boneMask.length>0)for(const i of t){const t=this._boneMask.indexOf(i.name);t>=0&&e.contains(i)&&this._boneMask.splice(t,1)}else for(const i of t)i!==e&&(e.contains(i)||this._boneMask.push(i.name))}}this._timelineDirty=1}removeAllBoneMask(){this._boneMask.length=0,this._timelineDirty=1}addState(e,i=null){if(null!==i)for(const s of i)switch(s.type){case 40:{const i=t.borrowObject(p);if(i.target=e,i.init(this._armature,this,s),this._animationTimelines.push(i),0!==this.blendType){const t=s;e.positionX=t.x,e.positionY=t.y,e.weight=0}e._parent=this,this.resetToPose=!1;break}case 41:{const i=t.borrowObject(f);i.target=e,i.init(this._armature,this,s),this._animationTimelines.push(i);break}case 42:{const i=t.borrowObject(T);i.target=e,i.init(this._armature,this,s),this._animationTimelines.push(i);break}}null===e._parent&&(e._parent=this)}activeTimeline(){for(const t of this._slotTimelines)t.dirty=!0,t.currentTime=-1}get isFadeIn(){return this._fadeState<0}get isFadeOut(){return this._fadeState>0}get isFadeComplete(){return 0===this._fadeState}get isPlaying(){return!!(2&this._playheadState)&&this._actionTimeline.playState<=0}get isCompleted(){return this._actionTimeline.playState>0}get currentPlayTimes(){return this._actionTimeline.currentPlayTimes}get totalTime(){return this._duration}get currentTime(){return this._actionTimeline.currentTime}set currentTime(t){const e=this._actionTimeline.currentPlayTimes-(this._actionTimeline.playState>0?1:0);if((t<0||this._duration<t)&&(t=t%this._duration+e*this._duration)<0&&(t+=this._duration),this.playTimes>0&&e===this.playTimes-1&&t===this._duration&&null===this._parent&&(t=this._duration-1e-6),this._time!==t){this._time=t,this._actionTimeline.setCurrentTime(this._time),null!==this._zOrderTimeline&&(this._zOrderTimeline.playState=-1);for(const t of this._boneTimelines)t.playState=-1;for(const t of this._slotTimelines)t.playState=-1}}get weight(){return this._weight}set weight(t){if(this._weight!==t){this._weight=t;for(const t of this._boneTimelines)t.dirty=!0;for(const t of this._boneBlendTimelines)t.dirty=!0;for(const t of this._slotBlendTimelines)t.dirty=!0}}get animationData(){return this._animationData}}class b extends t{static toString(){return"[class dragonBones.BlendState]"}_onClear(){this.reset(),this.target=null}update(t){const e=t.layer;let i=t._weightResult;if(this.dirty>0){if(this.leftWeight>0){if(this.layer!==e){if(this.layerWeight>=this.leftWeight)return this.dirty++,this.layer=e,this.leftWeight=0,this.blendWeight=0,!1;this.layer=e,this.leftWeight-=this.layerWeight,this.layerWeight=0}return i*=this.leftWeight,this.dirty++,this.blendWeight=i,this.layerWeight+=this.blendWeight,!0}return!1}return this.dirty++,this.layer=e,this.leftWeight=1,this.blendWeight=i,this.layerWeight=i,!0}reset(){this.dirty=0,this.layer=0,this.leftWeight=0,this.layerWeight=0,this.blendWeight=0}}b.BONE_TRANSFORM="boneTransform",b.BONE_ALPHA="boneAlpha",b.SURFACE="surface",b.SLOT_DEFORM="slotDeform",b.SLOT_ALPHA="slotAlpha",b.SLOT_Z_INDEX="slotZIndex";export{g as AnimationState,b as BlendState};
//# sourceMappingURL=AnimationState.mjs.map