interpolateValueAction.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { Action, Condition } from "Actions";
  2. import { Observable, Tools } from "Tools";
  3. import { Color3, Vector3, Matrix, Quaternion } from "Math";
  4. /**
  5. * This defines an action responsible to change the value of a property
  6. * by interpolating between its current value and the newly set one once triggered.
  7. * @see http://doc.babylonjs.com/how_to/how_to_use_actions
  8. */
  9. export class InterpolateValueAction extends Action {
  10. /**
  11. * Defines the path of the property where the value should be interpolated
  12. */
  13. public propertyPath: string;
  14. /**
  15. * Defines the target value at the end of the interpolation.
  16. */
  17. public value: any;
  18. /**
  19. * Defines the time it will take for the property to interpolate to the value.
  20. */
  21. public duration: number = 1000;
  22. /**
  23. * Defines if the other scene animations should be stopped when the action has been triggered
  24. */
  25. public stopOtherAnimations?: boolean;
  26. /**
  27. * Defines a callback raised once the interpolation animation has been done.
  28. */
  29. public onInterpolationDone?: () => void;
  30. /**
  31. * Observable triggered once the interpolation animation has been done.
  32. */
  33. public onInterpolationDoneObservable = new Observable<InterpolateValueAction>();
  34. private _target: any;
  35. private _effectiveTarget: any;
  36. private _property: string;
  37. /**
  38. * Instantiate the action
  39. * @param triggerOptions defines the trigger options
  40. * @param target defines the object containing the value to interpolate
  41. * @param propertyPath defines the path to the property in the target object
  42. * @param value defines the target value at the end of the interpolation
  43. * @param duration deines the time it will take for the property to interpolate to the value.
  44. * @param condition defines the trigger related conditions
  45. * @param stopOtherAnimations defines if the other scene animations should be stopped when the action has been triggered
  46. * @param onInterpolationDone defines a callback raised once the interpolation animation has been done
  47. */
  48. constructor(triggerOptions: any, target: any, propertyPath: string, value: any, duration: number = 1000, condition?: Condition, stopOtherAnimations?: boolean, onInterpolationDone?: () => void) {
  49. super(triggerOptions, condition);
  50. this.propertyPath = propertyPath;
  51. this.value = value;
  52. this.duration = duration;
  53. this.stopOtherAnimations = stopOtherAnimations;
  54. this.onInterpolationDone = onInterpolationDone;
  55. this._target = this._effectiveTarget = target;
  56. }
  57. /** @hidden */
  58. public _prepare(): void {
  59. this._effectiveTarget = this._getEffectiveTarget(this._effectiveTarget, this.propertyPath);
  60. this._property = this._getProperty(this.propertyPath);
  61. }
  62. /**
  63. * Execute the action starts the value interpolation.
  64. */
  65. public execute(): void {
  66. var scene = this._actionManager.getScene();
  67. var keys = [
  68. {
  69. frame: 0,
  70. value: this._effectiveTarget[this._property]
  71. }, {
  72. frame: 100,
  73. value: this.value
  74. }
  75. ];
  76. var dataType: number;
  77. if (typeof this.value === "number") {
  78. dataType = Animation.ANIMATIONTYPE_FLOAT;
  79. } else if (this.value instanceof Color3) {
  80. dataType = Animation.ANIMATIONTYPE_COLOR3;
  81. } else if (this.value instanceof Vector3) {
  82. dataType = Animation.ANIMATIONTYPE_VECTOR3;
  83. } else if (this.value instanceof Matrix) {
  84. dataType = Animation.ANIMATIONTYPE_MATRIX;
  85. } else if (this.value instanceof Quaternion) {
  86. dataType = Animation.ANIMATIONTYPE_QUATERNION;
  87. } else {
  88. Tools.Warn("InterpolateValueAction: Unsupported type (" + typeof this.value + ")");
  89. return;
  90. }
  91. var animation = new Animation("InterpolateValueAction", this._property, 100 * (1000.0 / this.duration), dataType, Animation.ANIMATIONLOOPMODE_CONSTANT);
  92. animation.setKeys(keys);
  93. if (this.stopOtherAnimations) {
  94. scene.stopAnimation(this._effectiveTarget);
  95. }
  96. let wrapper = () => {
  97. this.onInterpolationDoneObservable.notifyObservers(this);
  98. if (this.onInterpolationDone) {
  99. this.onInterpolationDone();
  100. }
  101. };
  102. scene.beginDirectAnimation(this._effectiveTarget, [animation], 0, 100, false, 1, wrapper);
  103. }
  104. /**
  105. * Serializes the actions and its related information.
  106. * @param parent defines the object to serialize in
  107. * @returns the serialized object
  108. */
  109. public serialize(parent: any): any {
  110. return super._serialize({
  111. name: "InterpolateValueAction",
  112. properties: [
  113. Action._GetTargetProperty(this._target),
  114. { name: "propertyPath", value: this.propertyPath },
  115. { name: "value", value: Action._SerializeValueAsString(this.value) },
  116. { name: "duration", value: Action._SerializeValueAsString(this.duration) },
  117. { name: "stopOtherAnimations", value: Action._SerializeValueAsString(this.stopOtherAnimations) || false }
  118. ]
  119. }, parent);
  120. }
  121. }