File

projects/wms-framework/src/lib/helpers/Animation.ts

Description

Keyframe for object values

Index

Properties

Properties

KeyTime
Type : KeyTime

Time mark

Value
Type : unknown

Value to set in this keyframe

import { SubscriptionEvent } from '../utils/SubscriptionEvent';
import { DependencyObject } from '../basecomponentmodel/DependencyObject';
import { PresentationFrameworkCollectionModel } from '../models/controls/ItemsControlModel';
import { PropertyPath } from '../basecomponentmodel/Bindings/PropertyPath';
import { DependencyProperty } from '../basecomponentmodel/DependencyProperty';
import { TimeRange } from '../baseframework/TimeRange';

/**
 * Base class for timeline objets
 *
 * @export
 * @abstract
 * @class TimelineModel
 * @extends {DependencyObject}
 * @wType System.Windows.Media.Animation.Timeline
 */
export abstract class TimelineModel extends DependencyObject {
  /**
   *  Start time
   *
   * @type {TimeRange}
   * @memberof TimelineModel
   */
  BeginTime: TimeRange;

  /**
   * Creates an instance of TimelineModel.
   *
   * @memberof TimelineModel
   */
  constructor() {
    super();
    this.Completed = new SubscriptionEvent<any>();
  }

  /**
   * Executes the animation frames
   *
   * @abstract
   * @memberof TimelineModel
   * @wIgnore
   */
  Execute(): void {
    // Not required implementation for abstract class.
  }

  /**
   * Event to be triggered when the storyboard completes
   *
   * @type {SubscriptionEvent<any>}
   * @memberof TimelineModel
   */
  public Completed: SubscriptionEvent<any>;
}

/**
 * Storyboard compatibility class
 *
 * @export
 * @class Storyboard
 * @extends {DependencyObject}
 * @wType System.Windows.Media.Animation.Storyboard
 */
export class Storyboard extends TimelineModel {
  /**
   *  Property information for the `Target` property
   *
   * @static
   * @memberof Storyboard
   * @wIgnore
   */
  public static readonly TargetProperty = new DependencyProperty(
    'Target',
    null,
    null
  );

  /**
   * Property information for the `TargetProperty` property
   *
   * @static
   * @memberof Storyboard
   */
  public static readonly TargetPropertyProperty = new DependencyProperty(
    'TargetProperty',
    null,
    null
  );

  /**
   *  Storyboard name
   *
   * @type {string}
   * @memberof Storyboard
   * @wIgnore
   */
  public Name: string = null;

  /**
   *  Begin time
   *
   * @type {TimeRange}
   * @memberof Storyboard
   * @wIgnore
   */
  public BeginTime: TimeRange = new TimeRange();

  /**
   *  Children of this storyboard
   *
   * @type {PresentationFrameworkCollectionModel<TimelineModel>}
   * @memberof Storyboard
   */
  public readonly Children: PresentationFrameworkCollectionModel<TimelineModel> =
    new TimelineCollection();

  /**
   *  current request for animation
   *
   * @private
   * @type {number}
   * @memberof Storyboard
   */
  private animationId: any = null;

  /**
   * Creates an instance of Storyboard.
   *
   * @memberof Storyboard
   */
  constructor() {
    super();
  }

  /**
   *  Gets the value of the `Target` property
   *
   * @static
   * @param {TimelineModel} obj
   * @return {*}  {*}
   * @memberof Storyboard
   * @wIgnore
   */
  static GetTarget(obj: TimelineModel): any {
    return obj.getValue(Storyboard.TargetProperty);
  }

  /**
   *  Sets the value of the `Target` property
   *
   * @static
   * @param {TimelineModel} obj
   * @param {*} target
   * @memberof Storyboard
   */
  public static SetTarget(obj: TimelineModel, target: any) {
    obj.setValue(Storyboard.TargetProperty, target);
  }

  /**
   *  Gets the value of the `TargetProperty` property
   *
   * @static
   * @param {TimelineModel} obj
   * @return {*}  {PropertyPath}
   * @memberof Storyboard
   */
  static GetTargetProperty(obj: TimelineModel): PropertyPath {
    return obj.getValue(Storyboard.TargetPropertyProperty);
  }

  /**
   *  Sets the value of the `TargetProperty` property
   *
   * @static
   * @param {*} obj
   * @param {PropertyPath} targetProperty
   * @memberof Storyboard
   */
  public static SetTargetProperty(obj: any, targetProperty: PropertyPath) {
    obj.setValue(Storyboard.TargetPropertyProperty, targetProperty);
  }

  /**
   * Starts the animation logic
   *
   * @memberof Storyboard
   */
  public Begin() {
    this.animationId = setTimeout(() => {
      for (const obj of this.Children) {
        obj.Execute();
      }
      this.Completed.fire(null);
      this.animationId = null;
      clearTimeout(this.animationId);
    });
  }

  /**
   *  Stops the current animation
   *
   * @memberof Storyboard
   */
  public Stop() {
    if (this.animationId !== null) {
      clearTimeout(this.animationId);
      this.animationId = null;
    }
  }

  /**
   * Sets the name of the current storyboard object
   *
   * @param {string} name
   * @return {*}  {Storyboard}
   * @memberof Storyboard
   */
  public withName(name: string): Storyboard {
    this.Name = name;
    return this;
  }
}

/**
 *  Collection of timeline objects
 *
 * @export
 * @class TimelineCollection
 * @extends {PresentationFrameworkCollectionModel<TimelineModel>}
 * @wType System.Windows.Media.Animation.TimelineCollection
 */
export class TimelineCollection extends PresentationFrameworkCollectionModel<TimelineModel> {}

/**
 *  Keytime
 *
 * @export
 * @class KeyTime
 * @wType System.Windows.Media.Animation.KeyTime
 */
export class KeyTime {
  /**
   * Time mark
   *
   * @type {TimeRange}
   * @memberof KeyTime
   * @wProperty TimeSpan
   */
  TimeRange: TimeRange;

  /**
   *  Creates an instance of the current `KeyTime` class
   *
   * @static
   * @param {TimeRange} timeRange
   * @return {*}  {KeyTime}
   * @memberof KeyTime
   */
  public static FromTimeSpan(timeRange: TimeRange): KeyTime {
    const result = new KeyTime();
    result.TimeRange = timeRange;
    return result;
  }

  /**
   * Parse a time range string into a `KeyTime`.
   *
   * The format for the input is `dd.hh:mm:ss.ff`, where:
   * - `dd` = days - optional
   * - `hh` = hours - mandatory
   * - `mm` = minutes - optional
   * - `ss` = seconds - optional
   * - `ff` = fractionalSeconds - optional
   *
   * Valid input examples are:
   * - `1.2:3:4.5` = 1 day, 2 hours, 3 minutes, 4 seconds, 500 milliseconds.
   * - `02:30` = 2 hours, 30 minutes.
   * - `3.2:15` = 3 days, 2 hours, 15 minutes.
   * - `13` = *special* - this is interpreted as 13 days, not 13 hours.
   *
   * Invalid input values:
   * - `1.2` = ambiguous input. It could mean 1 day and 2 hours, or 1 second and 200 milliseconds.
   * - `15:52.3` = 15 minutes, 52 seconds and 300 milliseconds. Is invalid because hours is mandatory.
   * - `3:14:` = lacks seconds. In general, a number should always follow `:` or `.`.
   *
   * @static
   * @param {string} input
   * @return {*}  {KeyTime}
   * @memberof KeyTime
   * @wIgnore
   */
  public static Parse(input: string): KeyTime {
    return KeyTime.FromTimeSpan(TimeRange.Parse(input));
  }
}

/**
 *  Keyframe for object values
 *
 * @export
 * @class ObjectKeyFrame
 * @wType System.Windows.Media.Animation.ObjectKeyFrame
 */
export class ObjectKeyFrame {
  /**
   *  Time mark
   *
   * @type {KeyTime}
   * @memberof ObjectKeyFrame
   */
  KeyTime: KeyTime;

  /**
   * Value to set in this keyframe
   *
   * @type {unknown}
   * @memberof ObjectKeyFrame
   */
  Value: unknown;
}

/**
 *  Double value keyframe
 *
 * @export
 * @class DoubleKeyFrame
 * @wType System.Windows.Media.Animation.DoubleKeyFrame
 */
export class DoubleKeyFrame {
  /**
   * Time mark
   *
   * @type {KeyTime}
   * @memberof DoubleKeyFrame
   */
  KeyTime: KeyTime;

  /**
   *  Double value to set
   *
   * @type {number}
   * @memberof DoubleKeyFrame
   */
  Value: number;
}

/**
 *  Double (easing) keyframe
 *
 * @export
 * @class EasingDoubleKeyFrame
 * @extends {DoubleKeyFrame}
 * @wType System.Windows.Media.Animation.EasingDoubleKeyFrame
 */
export class EasingDoubleKeyFrame extends DoubleKeyFrame {}

/**
 *  Keyframe for object properties
 *
 * @export
 * @class DiscreteObjectKeyFrame
 * @extends {ObjectKeyFrame}
 * @wType System.Windows.Media.Animation.DiscreteObjectKeyFrame
 */
export class DiscreteObjectKeyFrame extends ObjectKeyFrame {}

/**
 *  Keyframe collection of object properties
 *
 * @export
 * @class ObjectKeyFrameCollection
 * @extends {PresentationFrameworkCollectionModel<ObjectKeyFrame>}
 * @wType System.Windows.Media.Animation.ObjectKeyFrameCollection
 */
export class ObjectKeyFrameCollection extends PresentationFrameworkCollectionModel<ObjectKeyFrame> {}

/**
 *  Keyframe collection of double property values
 *
 * @export
 * @class DoubleKeyFrameCollection
 * @extends {PresentationFrameworkCollectionModel<DoubleKeyFrame>}
 * @wType System.Windows.Media.Animation.DoubleKeyFrameCollection
 */
export class DoubleKeyFrameCollection extends PresentationFrameworkCollectionModel<DoubleKeyFrame> {}

/**
 *  Animation using object properties and keyframes
 *
 * @export
 * @class ObjectAnimationUsingKeyFrames
 * @extends {TimelineModel}
 * @wType System.Windows.Media.Animation.ObjectAnimationUsingKeyFrames
 */
export class ObjectAnimationUsingKeyFrames extends TimelineModel {
  /**
   *  Collection of keyframes
   *
   * @memberof ObjectAnimationUsingKeyFrames
   */
  KeyFrames = new ObjectKeyFrameCollection();

  /**
   *  Executes the current animation
   *
   * @memberof ObjectAnimationUsingKeyFrames
   * @wIgnore
   */
  Execute() {
    const obj = Storyboard.GetTarget(this);
    const path = Storyboard.GetTargetProperty(this);
    if (path) {
      for (const keyFrame of this.KeyFrames) {
        path.setValueContextObject(obj, keyFrame.Value);
      }
    }
  }
}

/**
 *  Animation of double
 *
 * @export
 * @class DoubleAnimationUsingKeyFrames
 * @wType System.Windows.Media.Animation.DoubleAnimationUsingKeyFrames
 */
export class DoubleAnimationUsingKeyFrames extends TimelineModel {
  /**
   *  Collection of keyframes
   *
   * @memberof DoubleKeyFrameCollection
   */
  KeyFrames = new DoubleKeyFrameCollection();

  /**
   *  Executes the current animation
   *
   * @memberof ObjectAnimationUsingKeyFrames
   * @wIgnore
   */
  Execute() {
    const obj = Storyboard.GetTarget(this);
    const path = Storyboard.GetTargetProperty(this);
    if (path) {
      for (const keyFrame of this.KeyFrames) {
        path.setValueContextObject(obj, keyFrame.Value);
      }
    }
  }
}

/**
 *  Duration object
 *
 * @export
 * @class Duration
 * @wType System.Windows.Duration
 */
export class Duration {
  /**
   * Automatic duration
   *
   * @static
   * @type {Duration}
   * @memberof Duration
   */
  public static Automatic: Duration = new Duration(undefined);

  /**
   * 'Forever' duration
   *
   * @static
   * @type {Duration}
   * @memberof Duration
   */
  public static Forever: Duration = new Duration(undefined);

  /**
   * Duration time
   *
   * @type {TimeRange}
   * @memberof Duration
   */
  public TimeSpan: TimeRange = new TimeRange();

  /**
   *  Verifies if this object has time
   *
   * @readonly
   * @memberof Duration
   */
  public get HasTimeSpan() {
    return typeof this.TimeSpan !== 'undefined';
  }

  /**
   * Creates an instance of Duration.
   * @param {TimeRange} time
   * @memberof Duration
   */
  constructor(time: TimeRange) {
    this.TimeSpan = time;
  }
}

result-matching ""

    No results matching ""