babylon.interpolateValueAction.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. module BABYLON {
  2. export class InterpolateValueAction extends Action {
  3. private _target: any;
  4. private _effectiveTarget: any;
  5. private _property: string;
  6. public onInterpolationDoneObservable = new Observable<InterpolateValueAction>();
  7. constructor(triggerOptions: any, target: any, public propertyPath: string, public value: any, public duration: number = 1000, condition?: Condition, public stopOtherAnimations?: boolean, public onInterpolationDone?: () => void) {
  8. super(triggerOptions, condition);
  9. this._target = this._effectiveTarget = target;
  10. }
  11. public _prepare(): void {
  12. this._effectiveTarget = this._getEffectiveTarget(this._effectiveTarget, this.propertyPath);
  13. this._property = this._getProperty(this.propertyPath);
  14. }
  15. public execute(): void {
  16. var scene = this._actionManager.getScene();
  17. var keys = [
  18. {
  19. frame: 0,
  20. value: this._effectiveTarget[this._property]
  21. }, {
  22. frame: 100,
  23. value: this.value
  24. }
  25. ];
  26. var dataType: number;
  27. if (typeof this.value === "number") {
  28. dataType = Animation.ANIMATIONTYPE_FLOAT;
  29. } else if (this.value instanceof Color3) {
  30. dataType = Animation.ANIMATIONTYPE_COLOR3;
  31. } else if (this.value instanceof Vector3) {
  32. dataType = Animation.ANIMATIONTYPE_VECTOR3;
  33. } else if (this.value instanceof Matrix) {
  34. dataType = Animation.ANIMATIONTYPE_MATRIX;
  35. } else if (this.value instanceof Quaternion) {
  36. dataType = Animation.ANIMATIONTYPE_QUATERNION;
  37. } else {
  38. Tools.Warn("InterpolateValueAction: Unsupported type (" + typeof this.value + ")");
  39. return;
  40. }
  41. var animation = new Animation("InterpolateValueAction", this._property, 100 * (1000.0 / this.duration), dataType, Animation.ANIMATIONLOOPMODE_CONSTANT);
  42. animation.setKeys(keys);
  43. if (this.stopOtherAnimations) {
  44. scene.stopAnimation(this._effectiveTarget);
  45. }
  46. let wrapper = ()=> {
  47. this.onInterpolationDoneObservable.notifyObservers(this);
  48. if (this.onInterpolationDone) {
  49. this.onInterpolationDone();
  50. }
  51. }
  52. scene.beginDirectAnimation(this._effectiveTarget, [animation], 0, 100, false, 1, wrapper);
  53. }
  54. public serialize(parent: any): any {
  55. return super._serialize({
  56. name: "InterpolateValueAction",
  57. properties: [
  58. Action._GetTargetProperty(this._target),
  59. { name: "propertyPath", value: this.propertyPath },
  60. { name: "value", value: Action._SerializeValueAsString(this.value) },
  61. { name: "duration", value: Action._SerializeValueAsString(this.duration) },
  62. { name: "stopOtherAnimations", value: Action._SerializeValueAsString(this.stopOtherAnimations) || false }
  63. ]
  64. }, parent);
  65. }
  66. }
  67. }