interpolateValueAction.ts 5.7 KB

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