babylon.animatable.js 5.2 KB

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