interpolateValueAction.ts 5.6 KB

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