babylon.animatable.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. module BABYLON {
  2. export class Animatable {
  3. private _localDelayOffset: number;
  4. private _animations = new Array<Animation>();
  5. private _paused = false;
  6. private _scene: Scene;
  7. public animationStarted = false;
  8. 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) {
  9. if (animations) {
  10. this.appendAnimations(target, animations);
  11. }
  12. this._scene = scene;
  13. scene._activeAnimatables.push(this);
  14. }
  15. // Methods
  16. public appendAnimations(target: any, animations: Animation[]): void {
  17. for (var index = 0; index < animations.length; index++) {
  18. var animation = animations[index];
  19. animation._target = target;
  20. this._animations.push(animation);
  21. }
  22. }
  23. public getAnimationByTargetProperty(property: string) {
  24. var animations = this._animations;
  25. for (var index = 0; index < animations.length; index++) {
  26. if (animations[index].targetProperty === property) {
  27. return animations[index];
  28. }
  29. }
  30. return null;
  31. }
  32. public pause(): void {
  33. this._paused = true;
  34. }
  35. public restart(): void {
  36. this._paused = false;
  37. }
  38. public stop(): void {
  39. var index = this._scene._activeAnimatables.indexOf(this);
  40. if (index > -1) {
  41. this._scene._activeAnimatables.splice(index, 1);
  42. }
  43. if (this.onAnimationEnd) {
  44. this.onAnimationEnd();
  45. }
  46. }
  47. public _animate(delay: number): boolean {
  48. if (this._paused) {
  49. return true;
  50. }
  51. if (!this._localDelayOffset) {
  52. this._localDelayOffset = delay;
  53. }
  54. // Animating
  55. var running = false;
  56. var animations = this._animations;
  57. for (var index = 0; index < animations.length; index++) {
  58. var animation = animations[index];
  59. var isRunning = animation.animate(delay - this._localDelayOffset, this.fromFrame, this.toFrame, this.loopAnimation, this.speedRatio);
  60. running = running || isRunning;
  61. }
  62. if (!running && this.onAnimationEnd) {
  63. this.onAnimationEnd();
  64. }
  65. return running;
  66. }
  67. }
  68. }