interpolateValueAction.ts 5.9 KB

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