123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- import { Vector3 } from "../Maths/math.vector";
- import { Nullable } from "../types";
- import { ActionManager } from "../Actions/actionManager";
- import { ISpriteManager } from "./spriteManager";
- import { Color4 } from '../Maths/math.color';
- import { Observable } from '../Misc/observable';
- /**
- * Class used to represent a sprite
- * @see http://doc.babylonjs.com/babylon101/sprites
- */
- export class Sprite {
- /** Gets or sets the current world position */
- public position: Vector3;
- /** Gets or sets the main color */
- public color = new Color4(1.0, 1.0, 1.0, 1.0);
- /** Gets or sets the width */
- public width = 1.0;
- /** Gets or sets the height */
- public height = 1.0;
- /** Gets or sets rotation angle */
- public angle = 0;
- /** Gets or sets the cell index in the sprite sheet */
- public cellIndex: number;
- /** Gets or sets the cell reference in the sprite sheet, uses sprite's filename when added to sprite sheet */
- public cellRef: string;
- /** Gets or sets a boolean indicating if UV coordinates should be inverted in U axis */
- public invertU = 0;
- /** Gets or sets a boolean indicating if UV coordinates should be inverted in B axis */
- public invertV = 0;
- /** Gets or sets a boolean indicating that this sprite should be disposed after animation ends */
- public disposeWhenFinishedAnimating: boolean;
- /** Gets the list of attached animations */
- public animations = new Array<Animation>();
- /** Gets or sets a boolean indicating if the sprite can be picked */
- public isPickable = false;
- /** Gets or sets a boolean indicating that sprite texture alpha will be used for precise picking (false by default) */
- public useAlphaForPicking = false;
- /** @hidden */
- public _xOffset: number;
- /** @hidden */
- public _yOffset: number;
- /** @hidden */
- public _xSize: number;
- /** @hidden */
- public _ySize: number;
- /**
- * Gets or sets the associated action manager
- */
- public actionManager: Nullable<ActionManager>;
- /**
- * An event triggered when the control has been disposed
- */
- public onDisposeObservable = new Observable<Sprite>();
- private _animationStarted = false;
- private _loopAnimation = false;
- private _fromIndex = 0;
- private _toIndex = 0;
- private _delay = 0;
- private _direction = 1;
- private _manager: ISpriteManager;
- private _time = 0;
- private _onAnimationEnd: () => void;
- /**
- * Gets or sets a boolean indicating if the sprite is visible (renderable). Default is true
- */
- public isVisible = true;
- /**
- * Gets or sets the sprite size
- */
- public get size(): number {
- return this.width;
- }
- public set size(value: number) {
- this.width = value;
- this.height = value;
- }
- /**
- * Gets or sets the unique id of the sprite
- */
- public uniqueId: number;
- /**
- * Creates a new Sprite
- * @param name defines the name
- * @param manager defines the manager
- */
- constructor(
- /** defines the name */
- public name: string,
- manager: ISpriteManager) {
- this._manager = manager;
- this._manager.sprites.push(this);
- this.uniqueId = this._manager.scene.getUniqueId();
- this.position = Vector3.Zero();
- }
- /**
- * Returns the string "Sprite"
- * @returns "Sprite"
- */
- public getClassName(): string {
- return "Sprite";
- }
- /**
- * Starts an animation
- * @param from defines the initial key
- * @param to defines the end key
- * @param loop defines if the animation must loop
- * @param delay defines the start delay (in ms)
- * @param onAnimationEnd defines a callback to call when animation ends
- */
- public playAnimation(from: number, to: number, loop: boolean, delay: number, onAnimationEnd: () => void): void {
- this._fromIndex = from;
- this._toIndex = to;
- this._loopAnimation = loop;
- this._delay = delay;
- this._animationStarted = true;
- if (from < to) {
- this._direction = 1;
- } else {
- this._direction = -1;
- this._toIndex = from;
- this._fromIndex = to;
- }
- this.cellIndex = from;
- this._time = 0;
- this._onAnimationEnd = onAnimationEnd;
- }
- /** Stops current animation (if any) */
- public stopAnimation(): void {
- this._animationStarted = false;
- }
- /** @hidden */
- public _animate(deltaTime: number): void {
- if (!this._animationStarted) {
- return;
- }
- this._time += deltaTime;
- if (this._time > this._delay) {
- this._time = this._time % this._delay;
- this.cellIndex += this._direction;
- if (this._direction > 0 && this.cellIndex > this._toIndex || this._direction < 0 && this.cellIndex < this._fromIndex) {
- if (this._loopAnimation) {
- this.cellIndex = this._direction > 0 ? this._fromIndex : this._toIndex;
- } else {
- this.cellIndex = this._toIndex;
- this._animationStarted = false;
- if (this._onAnimationEnd) {
- this._onAnimationEnd();
- }
- if (this.disposeWhenFinishedAnimating) {
- this.dispose();
- }
- }
- }
- }
- }
- /** Release associated resources */
- public dispose(): void {
- for (var i = 0; i < this._manager.sprites.length; i++) {
- if (this._manager.sprites[i] == this) {
- this._manager.sprites.splice(i, 1);
- }
- }
- // Callback
- this.onDisposeObservable.notifyObservers(this);
- this.onDisposeObservable.clear();
- }
- }
|