babylon.animatable.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. module BABYLON {
  2. export class Animatable {
  3. private _localDelayOffset: number;
  4. private _pausedDelay: number;
  5. private _animations = new Array<Animation>();
  6. private _paused = false;
  7. private _scene: Scene;
  8. public animationStarted = false;
  9. constructor(scene: Scene, public target, public fromFrame: number = 0, public toFrame: number = 100, public loopAnimation: boolean = false, public speedRatio: number = 1.0, public onAnimationEnd?, animations?: any) {
  10. if (animations) {
  11. this.appendAnimations(target, animations);
  12. }
  13. this._scene = scene;
  14. scene._activeAnimatables.push(this);
  15. }
  16. // Methods
  17. public getAnimations(): Animation[] {
  18. return this._animations;
  19. }
  20. public appendAnimations(target: any, animations: Animation[]): void {
  21. for (var index = 0; index < animations.length; index++) {
  22. var animation = animations[index];
  23. animation._target = target;
  24. this._animations.push(animation);
  25. }
  26. }
  27. public getAnimationByTargetProperty(property: string) {
  28. var animations = this._animations;
  29. for (var index = 0; index < animations.length; index++) {
  30. if (animations[index].targetProperty === property) {
  31. return animations[index];
  32. }
  33. }
  34. return null;
  35. }
  36. public reset(): void {
  37. var animations = this._animations;
  38. for (var index = 0; index < animations.length; index++) {
  39. animations[index].reset();
  40. }
  41. this._localDelayOffset = null;
  42. this._pausedDelay = null;
  43. }
  44. public enableBlending(blendingSpeed: number): void {
  45. var animations = this._animations;
  46. for (var index = 0; index < animations.length; index++) {
  47. animations[index].enableBlending = true;
  48. animations[index].blendingSpeed = blendingSpeed;
  49. }
  50. }
  51. public disableBlending(): void {
  52. var animations = this._animations;
  53. for (var index = 0; index < animations.length; index++) {
  54. animations[index].enableBlending = false;
  55. }
  56. }
  57. public goToFrame(frame: number): void {
  58. var animations = this._animations;
  59. if (animations[0]) {
  60. var fps = animations[0].framePerSecond;
  61. var currentFrame = animations[0].currentFrame;
  62. var adjustTime = frame - currentFrame;
  63. var delay = adjustTime * 1000 / fps;
  64. this._localDelayOffset -= delay;
  65. }
  66. for (var index = 0; index < animations.length; index++) {
  67. animations[index].goToFrame(frame);
  68. }
  69. }
  70. public pause(): void {
  71. if (this._paused) {
  72. return;
  73. }
  74. this._paused = true;
  75. }
  76. public restart(): void {
  77. this._paused = false;
  78. }
  79. public stop(animationName?: string): void {
  80. var idx = this._scene._activeAnimatables.indexOf(this);
  81. if (idx > -1) {
  82. if(animationName){
  83. var animations = this._animations;
  84. var numberOfAnimationsStopped = 0;
  85. for (var index = animations.length - 1; index >= 0; index--) {
  86. if (typeof animationName === "string" && animations[index].name != animationName) {
  87. continue;
  88. }
  89. animations[index].reset();
  90. animations.splice(index, 1);
  91. numberOfAnimationsStopped ++;
  92. }
  93. if (animations.length == numberOfAnimationsStopped) {
  94. this._scene._activeAnimatables.splice(idx, 1);
  95. if (this.onAnimationEnd) {
  96. this.onAnimationEnd();
  97. }
  98. }
  99. } else {
  100. this._scene._activeAnimatables.splice(index, 1);
  101. var animations = this._animations;
  102. for (var index = 0; index < animations.length; index++) {
  103. animations[index].reset();
  104. }
  105. if (this.onAnimationEnd) {
  106. this.onAnimationEnd();
  107. }
  108. }
  109. }
  110. }
  111. public _animate(delay: number): boolean {
  112. if (this._paused) {
  113. this.animationStarted = false;
  114. if (!this._pausedDelay) {
  115. this._pausedDelay = delay;
  116. }
  117. return true;
  118. }
  119. if (!this._localDelayOffset) {
  120. this._localDelayOffset = delay;
  121. } else if (this._pausedDelay) {
  122. this._localDelayOffset += delay - this._pausedDelay;
  123. this._pausedDelay = null;
  124. }
  125. // Animating
  126. var running = false;
  127. var animations = this._animations;
  128. var index: number;
  129. for (index = 0; index < animations.length; index++) {
  130. var animation = animations[index];
  131. var isRunning = animation.animate(delay - this._localDelayOffset, this.fromFrame, this.toFrame, this.loopAnimation, this.speedRatio);
  132. running = running || isRunning;
  133. }
  134. this.animationStarted = running;
  135. if (!running) {
  136. // Remove from active animatables
  137. index = this._scene._activeAnimatables.indexOf(this);
  138. this._scene._activeAnimatables.splice(index, 1);
  139. }
  140. if (!running && this.onAnimationEnd) {
  141. this.onAnimationEnd();
  142. this.onAnimationEnd = null;
  143. }
  144. return running;
  145. }
  146. }
  147. }