babylon.animatable.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 appendAnimations(target: any, animations: Animation[]): void {
  18. for (var index = 0; index < animations.length; index++) {
  19. var animation = animations[index];
  20. animation._target = target;
  21. this._animations.push(animation);
  22. }
  23. }
  24. public getAnimationByTargetProperty(property: string) {
  25. var animations = this._animations;
  26. for (var index = 0; index < animations.length; index++) {
  27. if (animations[index].targetProperty === property) {
  28. return animations[index];
  29. }
  30. }
  31. return null;
  32. }
  33. public pause(): void {
  34. if (this._paused) {
  35. return;
  36. }
  37. this._paused = true;
  38. }
  39. public restart(): void {
  40. this._paused = false;
  41. }
  42. public stop(): void {
  43. var index = this._scene._activeAnimatables.indexOf(this);
  44. if (index > -1) {
  45. this._scene._activeAnimatables.splice(index, 1);
  46. }
  47. if (this.onAnimationEnd) {
  48. this.onAnimationEnd();
  49. }
  50. }
  51. public _animate(delay: number): boolean {
  52. if (this._paused) {
  53. if (!this._pausedDelay) {
  54. this._pausedDelay = delay;
  55. }
  56. return true;
  57. }
  58. if (!this._localDelayOffset) {
  59. this._localDelayOffset = delay;
  60. } else if (this._pausedDelay) {
  61. this._localDelayOffset += delay - this._pausedDelay;
  62. this._pausedDelay = null;
  63. }
  64. // Animating
  65. var running = false;
  66. var animations = this._animations;
  67. for (var index = 0; index < animations.length; index++) {
  68. var animation = animations[index];
  69. var isRunning = animation.animate(delay - this._localDelayOffset, this.fromFrame, this.toFrame, this.loopAnimation, this.speedRatio);
  70. running = running || isRunning;
  71. }
  72. if (!running) {
  73. // Remove from active animatables
  74. index = this._scene._activeAnimatables.indexOf(this);
  75. this._scene._activeAnimatables.splice(index, 1);
  76. }
  77. if (!running && this.onAnimationEnd) {
  78. this.onAnimationEnd();
  79. }
  80. return running;
  81. }
  82. }
  83. }