babylon.animatable.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 pause(): void {
  45. if (this._paused) {
  46. return;
  47. }
  48. this._paused = true;
  49. }
  50. public restart(): void {
  51. this._paused = false;
  52. }
  53. public stop(): void {
  54. var index = this._scene._activeAnimatables.indexOf(this);
  55. if (index > -1) {
  56. this._scene._activeAnimatables.splice(index, 1);
  57. }
  58. if (this.onAnimationEnd) {
  59. this.onAnimationEnd();
  60. }
  61. }
  62. public _animate(delay: number): boolean {
  63. if (this._paused) {
  64. this.animationStarted = false;
  65. if (!this._pausedDelay) {
  66. this._pausedDelay = delay;
  67. }
  68. return true;
  69. }
  70. if (!this._localDelayOffset) {
  71. this._localDelayOffset = delay;
  72. } else if (this._pausedDelay) {
  73. this._localDelayOffset += delay - this._pausedDelay;
  74. this._pausedDelay = null;
  75. }
  76. // Animating
  77. var running = false;
  78. var animations = this._animations;
  79. var index: number;
  80. for (index = 0; index < animations.length; index++) {
  81. var animation = animations[index];
  82. var isRunning = animation.animate(delay - this._localDelayOffset, this.fromFrame, this.toFrame, this.loopAnimation, this.speedRatio);
  83. running = running || isRunning;
  84. }
  85. this.animationStarted = running;
  86. if (!running) {
  87. // Remove from active animatables
  88. index = this._scene._activeAnimatables.indexOf(this);
  89. this._scene._activeAnimatables.splice(index, 1);
  90. }
  91. if (!running && this.onAnimationEnd) {
  92. this.onAnimationEnd();
  93. }
  94. return running;
  95. }
  96. }
  97. }